mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-12-07 21:59:54 +00:00
LibWeb: Add Time::from_style_value
This method takes a `TimeStyleValue`, `PercentageStyleValue` or fully-simplified `CalculatedStyleValue` with a numeric type of time, as well as a percentage basis and produces the equivalent `Time` value. This saves us having to reimplement this logic in multiple places
This commit is contained in:
parent
165afd8ad1
commit
2f0cd9f739
Notes:
github-actions[bot]
2025-11-23 08:45:07 +00:00
Author: https://github.com/Calme1709
Commit: 2f0cd9f739
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/6854
Reviewed-by: https://github.com/gmta ✅
5 changed files with 33 additions and 41 deletions
|
|
@ -13,7 +13,7 @@
|
|||
#include <LibWeb/Animations/PseudoElementParsing.h>
|
||||
#include <LibWeb/CSS/CSSTransition.h>
|
||||
#include <LibWeb/CSS/StyleValues/KeywordStyleValue.h>
|
||||
#include <LibWeb/CSS/StyleValues/TimeStyleValue.h>
|
||||
#include <LibWeb/CSS/Time.h>
|
||||
#include <LibWeb/DOM/Document.h>
|
||||
#include <LibWeb/DOM/Element.h>
|
||||
|
||||
|
|
@ -151,25 +151,8 @@ void Animatable::add_transitioned_properties(Optional<CSS::PseudoElement> pseudo
|
|||
auto& transition = *maybe_transition;
|
||||
for (size_t i = 0; i < properties.size(); i++) {
|
||||
size_t index_of_this_transition = transition.transition_attributes.size();
|
||||
double delay = 0.0;
|
||||
if (delays[i]->is_time()) {
|
||||
delay = delays[i]->as_time().time().to_milliseconds();
|
||||
} else if (delays[i]->is_calculated() && delays[i]->as_calculated().resolves_to_time()) {
|
||||
auto resolved_time = delays[i]->as_calculated().resolve_time({});
|
||||
if (resolved_time.has_value()) {
|
||||
delay = resolved_time.value().to_milliseconds();
|
||||
}
|
||||
}
|
||||
|
||||
double duration = 0.0;
|
||||
if (durations[i]->is_time()) {
|
||||
duration = durations[i]->as_time().time().to_milliseconds();
|
||||
} else if (durations[i]->is_calculated() && durations[i]->as_calculated().resolves_to_time()) {
|
||||
auto resolved_time = durations[i]->as_calculated().resolve_time({});
|
||||
if (resolved_time.has_value()) {
|
||||
duration = resolved_time.value().to_milliseconds();
|
||||
}
|
||||
}
|
||||
auto delay = CSS::Time::from_style_value(delays[i], {}).to_milliseconds();
|
||||
auto duration = CSS::Time::from_style_value(durations[i], {}).to_milliseconds();
|
||||
auto timing_function = CSS::EasingFunction::from_style_value(timing_functions[i]);
|
||||
auto transition_behavior = CSS::keyword_to_transition_behavior(transition_behaviors[i]->to_keyword()).value_or(CSS::TransitionBehavior::Normal);
|
||||
transition.transition_attributes.empend(delay, duration, timing_function, transition_behavior);
|
||||
|
|
|
|||
|
|
@ -11,7 +11,6 @@
|
|||
#include <LibWeb/CSS/Interpolation.h>
|
||||
#include <LibWeb/CSS/PseudoElement.h>
|
||||
#include <LibWeb/CSS/StyleValues/StyleValue.h>
|
||||
#include <LibWeb/CSS/Time.h>
|
||||
|
||||
namespace Web::CSS {
|
||||
|
||||
|
|
|
|||
|
|
@ -2288,16 +2288,7 @@ Vector<ComputedProperties::AnimationProperties> ComputedProperties::animations()
|
|||
auto animation_fill_mode_style_value = coordinated_properties.get(PropertyID::AnimationFillMode).value()[i];
|
||||
auto animation_composition_style_value = coordinated_properties.get(PropertyID::AnimationComposition).value()[i];
|
||||
|
||||
auto duration = [&] {
|
||||
if (animation_duration_style_value->is_time())
|
||||
return animation_duration_style_value->as_time().time().to_milliseconds();
|
||||
|
||||
if (animation_duration_style_value->is_calculated())
|
||||
return animation_duration_style_value->as_calculated().resolve_time({}).value().to_milliseconds();
|
||||
|
||||
VERIFY_NOT_REACHED();
|
||||
}();
|
||||
|
||||
auto duration = Time::from_style_value(animation_duration_style_value, {}).to_milliseconds();
|
||||
auto timing_function = EasingFunction::from_style_value(animation_timing_function_style_value);
|
||||
|
||||
auto iteration_count = [&] {
|
||||
|
|
@ -2315,15 +2306,7 @@ Vector<ComputedProperties::AnimationProperties> ComputedProperties::animations()
|
|||
|
||||
auto direction = keyword_to_animation_direction(animation_direction_style_value->to_keyword()).value();
|
||||
auto play_state = keyword_to_animation_play_state(animation_play_state_style_value->to_keyword()).value();
|
||||
auto delay = [&] {
|
||||
if (animation_delay_style_value->is_time())
|
||||
return animation_delay_style_value->as_time().time().to_milliseconds();
|
||||
|
||||
if (animation_delay_style_value->is_calculated())
|
||||
return animation_delay_style_value->as_calculated().resolve_time({}).value().to_milliseconds();
|
||||
|
||||
VERIFY_NOT_REACHED();
|
||||
}();
|
||||
auto delay = Time::from_style_value(animation_delay_style_value, {}).to_milliseconds();
|
||||
auto fill_mode = keyword_to_animation_fill_mode(animation_fill_mode_style_value->to_keyword()).value();
|
||||
auto composition = keyword_to_animation_composition(animation_composition_style_value->to_keyword()).value();
|
||||
|
||||
|
|
|
|||
|
|
@ -4,9 +4,11 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include "Time.h"
|
||||
#include <LibWeb/CSS/Percentage.h>
|
||||
#include <LibWeb/CSS/StyleValues/CalculatedStyleValue.h>
|
||||
#include <LibWeb/CSS/Time.h>
|
||||
#include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
|
||||
#include <LibWeb/CSS/StyleValues/TimeStyleValue.h>
|
||||
|
||||
namespace Web::CSS {
|
||||
|
||||
|
|
@ -16,6 +18,30 @@ Time::Time(double value, TimeUnit unit)
|
|||
{
|
||||
}
|
||||
|
||||
Time Time::from_style_value(NonnullRefPtr<StyleValue const> const& style_value, Optional<Time> percentage_basis)
|
||||
{
|
||||
if (style_value->is_time())
|
||||
return style_value->as_time().time();
|
||||
|
||||
if (style_value->is_calculated()) {
|
||||
CalculationResolutionContext::PercentageBasis resolved_percentage_basis;
|
||||
|
||||
if (percentage_basis.has_value()) {
|
||||
resolved_percentage_basis = percentage_basis.value();
|
||||
}
|
||||
|
||||
return style_value->as_calculated().resolve_time({ .percentage_basis = resolved_percentage_basis }).value();
|
||||
}
|
||||
|
||||
if (style_value->is_percentage()) {
|
||||
VERIFY(percentage_basis.has_value());
|
||||
|
||||
return percentage_basis.value().percentage_of(style_value->as_percentage().percentage());
|
||||
}
|
||||
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
Time Time::make_seconds(double value)
|
||||
{
|
||||
return { value, TimeUnit::S };
|
||||
|
|
|
|||
|
|
@ -44,6 +44,7 @@ public:
|
|||
return 0;
|
||||
}
|
||||
|
||||
static Time from_style_value(NonnullRefPtr<StyleValue const> const&, Optional<Time> percentage_basis);
|
||||
static Time resolve_calculated(NonnullRefPtr<CalculatedStyleValue const> const&, Layout::Node const&, Time const& reference_value);
|
||||
|
||||
private:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue