LibWeb: Make computed opacity always available

No need to store opacity as Optional<float> as there's always a value
(and the default initial value is 1.)
This commit is contained in:
Andreas Kling 2021-10-19 15:27:40 +02:00
parent 07f15aa550
commit ff45eb7fb1
Notes: sideshowbarker 2024-07-18 02:09:27 +09:00
6 changed files with 14 additions and 20 deletions

View file

@ -36,6 +36,7 @@ public:
static CSS::PointerEvents pointer_events() { return CSS::PointerEvents::Auto; }
static float flex_grow() { return 0.0f; }
static float flex_shrink() { return 1.0f; }
static float opacity() { return 1.0f; }
};
struct BorderData {
@ -83,7 +84,7 @@ public:
float flex_grow() const { return m_noninherited.flex_grow; }
float flex_shrink() const { return m_noninherited.flex_shrink; }
CSS::AlignItems align_items() const { return m_noninherited.align_items; }
Optional<float> const& opacity() const { return m_noninherited.opacity; }
float opacity() const { return m_noninherited.opacity; }
CSS::JustifyContent justify_content() const { return m_noninherited.justify_content; }
Optional<BoxShadowData> const& box_shadow() const { return m_noninherited.box_shadow; }
CSS::BoxSizing box_sizing() const { return m_noninherited.box_sizing; }
@ -183,7 +184,7 @@ protected:
CSS::JustifyContent justify_content { InitialValues::justify_content() };
CSS::Overflow overflow_x { InitialValues::overflow() };
CSS::Overflow overflow_y { InitialValues::overflow() };
Optional<float> opacity;
float opacity { InitialValues::opacity() };
Optional<BoxShadowData> box_shadow {};
Vector<CSS::Transformation> transformations {};
CSS::BoxSizing box_sizing { InitialValues::box_sizing() };
@ -236,7 +237,7 @@ public:
void set_flex_grow(float value) { m_noninherited.flex_grow = value; }
void set_flex_shrink(float value) { m_noninherited.flex_shrink = value; }
void set_align_items(CSS::AlignItems value) { m_noninherited.align_items = value; }
void set_opacity(Optional<float> value) { m_noninherited.opacity = value; }
void set_opacity(float value) { m_noninherited.opacity = value; }
void set_justify_content(CSS::JustifyContent value) { m_noninherited.justify_content = value; }
void set_box_shadow(Optional<BoxShadowData> value) { m_noninherited.box_shadow = move(value); }
void set_transformations(Vector<CSS::Transformation> value) { m_noninherited.transformations = move(value); }

View file

@ -501,12 +501,8 @@ RefPtr<StyleValue> ResolvedCSSStyleDeclaration::style_value_for_property(Layout:
return NumericStyleValue::create_float(layout_node.computed_values().flex_grow());
case CSS::PropertyID::FlexShrink:
return NumericStyleValue::create_float(layout_node.computed_values().flex_shrink());
case CSS::PropertyID::Opacity: {
auto maybe_opacity = layout_node.computed_values().opacity();
if (!maybe_opacity.has_value())
return {};
return NumericStyleValue::create_float(maybe_opacity.release_value());
}
case CSS::PropertyID::Opacity:
return NumericStyleValue::create_float(layout_node.computed_values().opacity());
case CSS::PropertyID::JustifyContent:
return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().justify_content()));
case CSS::PropertyID::BoxShadow: {

View file

@ -121,11 +121,11 @@ Optional<int> StyleProperties::z_index() const
return {};
}
Optional<float> StyleProperties::opacity() const
float StyleProperties::opacity() const
{
auto maybe_value = property(CSS::PropertyID::Opacity);
if (!maybe_value.has_value())
return {};
return 1.0f;
auto& value = maybe_value.value();
if (value->has_number())
@ -137,7 +137,7 @@ Optional<float> StyleProperties::opacity() const
return clamp(length.raw_value() / 100.0f, 0.0f, 1.0f);
}
return {};
return 1.0f;
}
Optional<CSS::FlexDirection> StyleProperties::flex_direction() const

View file

@ -59,7 +59,7 @@ public:
float flex_grow() const;
float flex_shrink() const;
Optional<CSS::AlignItems> align_items() const;
Optional<float> opacity() const;
float opacity() const;
Optional<CSS::JustifyContent> justify_content() const;
Optional<CSS::Overflow> overflow_x() const;
Optional<CSS::Overflow> overflow_y() const;

View file

@ -77,10 +77,7 @@ bool Node::establishes_stacking_context() const
auto position = computed_values().position();
if (position == CSS::Position::Absolute || position == CSS::Position::Relative || position == CSS::Position::Fixed || position == CSS::Position::Sticky)
return true;
auto opacity = computed_values().opacity();
if (opacity.has_value() && opacity.value() != 1.0f)
return true;
return false;
return computed_values().opacity() < 1.0f;
}
HitTestResult Node::hit_test(const Gfx::IntPoint& position, HitTestType type) const

View file

@ -113,10 +113,10 @@ void StackingContext::paint(PaintContext& context)
}
auto opacity = m_box.computed_values().opacity();
if (opacity.has_value() && opacity.value() == 0.0f)
if (opacity == 0.0f)
return;
if (opacity.has_value() && opacity.value() != 1.0f) {
if (opacity < 1.0f) {
auto bitmap = context.painter().target();
auto new_bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, bitmap->size());
if (!new_bitmap)
@ -124,7 +124,7 @@ void StackingContext::paint(PaintContext& context)
Gfx::Painter painter(*new_bitmap);
PaintContext paint_context(painter, context.palette(), context.scroll_offset());
paint_internal(paint_context);
context.painter().blit(Gfx::IntPoint(m_box.absolute_position()), *new_bitmap, Gfx::IntRect(m_box.absolute_rect()), opacity.value());
context.painter().blit(Gfx::IntPoint(m_box.absolute_position()), *new_bitmap, Gfx::IntRect(m_box.absolute_rect()), opacity);
} else {
paint_internal(context);
}