LibWeb: Fix typo and use auto where possible

This commit is contained in:
MacDue 2023-02-02 20:41:03 +00:00 committed by Andreas Kling
parent e96df1599c
commit b106fd640b
Notes: sideshowbarker 2024-07-17 00:49:09 +09:00
3 changed files with 29 additions and 18 deletions

View File

@ -22,37 +22,37 @@ public:
~CanvasFillStrokeStyles() = default; ~CanvasFillStrokeStyles() = default;
using FillOrStrokeStyleVariant = Variant<DeprecatedString, JS::Handle<CanvasGradient>>; using FillOrStrokeStyleVariant = Variant<DeprecatedString, JS::Handle<CanvasGradient>>;
static CanvasState::FillOrStrokeStyle to_canvas_state_fill_or_stoke_style(auto const& style) static CanvasState::FillOrStrokeStyle to_canvas_state_fill_or_stroke_style(auto const& style)
{ {
return style.visit( return style.visit(
[&](DeprecatedString const& string) -> CanvasState::FillOrStrokeStyle { [&](DeprecatedString const& string) -> CanvasState::FillOrStrokeStyle {
return Gfx::Color::from_string(string).value_or(Color::Black); return Gfx::Color::from_string(string).value_or(Color::Black);
}, },
[&](JS::Handle<CanvasGradient> gradient) -> CanvasState::FillOrStrokeStyle { [&](auto fill_or_stroke_style) -> CanvasState::FillOrStrokeStyle {
return gradient; return fill_or_stroke_style;
}); });
} }
void set_fill_style(FillOrStrokeStyleVariant style) void set_fill_style(FillOrStrokeStyleVariant style)
{ {
// FIXME: 2. If the given value is a CanvasPattern object that is marked as not origin-clean, then set this's origin-clean flag to false. // FIXME: 2. If the given value is a CanvasPattern object that is marked as not origin-clean, then set this's origin-clean flag to false.
my_drawing_state().fill_style = to_canvas_state_fill_or_stoke_style(style); my_drawing_state().fill_style = to_canvas_state_fill_or_stroke_style(style);
} }
FillOrStrokeStyleVariant fill_style() const FillOrStrokeStyleVariant fill_style() const
{ {
return my_drawing_state().fill_style.to_js_fill_or_stoke_style(); return my_drawing_state().fill_style.to_js_fill_or_stroke_style();
} }
void set_stroke_style(FillOrStrokeStyleVariant style) void set_stroke_style(FillOrStrokeStyleVariant style)
{ {
// FIXME: 2. If the given value is a CanvasPattern object that is marked as not origin-clean, then set this's origin-clean flag to false. // FIXME: 2. If the given value is a CanvasPattern object that is marked as not origin-clean, then set this's origin-clean flag to false.
my_drawing_state().stroke_style = to_canvas_state_fill_or_stoke_style(style); my_drawing_state().stroke_style = to_canvas_state_fill_or_stroke_style(style);
} }
FillOrStrokeStyleVariant stroke_style() const FillOrStrokeStyleVariant stroke_style() const
{ {
return my_drawing_state().stroke_style.to_js_fill_or_stoke_style(); return my_drawing_state().stroke_style.to_js_fill_or_stroke_style();
} }
WebIDL::ExceptionOr<JS::NonnullGCPtr<CanvasGradient>> create_radial_gradient(double x0, double y0, double r0, double x1, double y1, double r1) WebIDL::ExceptionOr<JS::NonnullGCPtr<CanvasGradient>> create_radial_gradient(double x0, double y0, double r0, double x1, double y1, double r1)

View File

@ -42,14 +42,14 @@ bool CanvasState::is_context_lost()
NonnullRefPtr<Gfx::PaintStyle> CanvasState::FillOrStrokeStyle::to_gfx_paint_style() NonnullRefPtr<Gfx::PaintStyle> CanvasState::FillOrStrokeStyle::to_gfx_paint_style()
{ {
return m_fill_or_stoke_style.visit( return m_fill_or_stroke_style.visit(
[&](Gfx::Color color) -> NonnullRefPtr<Gfx::PaintStyle> { [&](Gfx::Color color) -> NonnullRefPtr<Gfx::PaintStyle> {
if (!m_color_paint_style) if (!m_color_paint_style)
m_color_paint_style = Gfx::SolidColorPaintStyle::create(color); m_color_paint_style = Gfx::SolidColorPaintStyle::create(color);
return m_color_paint_style.release_nonnull(); return m_color_paint_style.release_nonnull();
}, },
[&](JS::Handle<CanvasGradient> gradient) { [&](auto handle) {
return gradient->to_gfx_paint_style(); return handle->to_gfx_paint_style();
}); });
} }
@ -60,7 +60,7 @@ Gfx::Color CanvasState::FillOrStrokeStyle::to_color_but_fixme_should_accept_any_
Optional<Gfx::Color> CanvasState::FillOrStrokeStyle::as_color() const Optional<Gfx::Color> CanvasState::FillOrStrokeStyle::as_color() const
{ {
if (auto* color = m_fill_or_stoke_style.get_pointer<Gfx::Color>()) if (auto* color = m_fill_or_stroke_style.get_pointer<Gfx::Color>())
return *color; return *color;
return {}; return {};
} }

View File

@ -30,12 +30,17 @@ public:
struct FillOrStrokeStyle { struct FillOrStrokeStyle {
FillOrStrokeStyle(Gfx::Color color) FillOrStrokeStyle(Gfx::Color color)
: m_fill_or_stoke_style(color) : m_fill_or_stroke_style(color)
{ {
} }
FillOrStrokeStyle(JS::Handle<CanvasGradient> gradient) FillOrStrokeStyle(JS::Handle<CanvasGradient> gradient)
: m_fill_or_stoke_style(gradient) : m_fill_or_stroke_style(gradient)
{
}
FillOrStrokeStyle(JS::Handle<CanvasPattern> pattern)
: m_fill_or_stroke_style(pattern)
{ {
} }
@ -44,15 +49,21 @@ public:
Optional<Gfx::Color> as_color() const; Optional<Gfx::Color> as_color() const;
Gfx::Color to_color_but_fixme_should_accept_any_paint_style() const; Gfx::Color to_color_but_fixme_should_accept_any_paint_style() const;
Variant<DeprecatedString, JS::Handle<CanvasGradient>> to_js_fill_or_stoke_style() const using JsFillOrStrokeStyle = Variant<DeprecatedString, JS::Handle<CanvasGradient>>;
JsFillOrStrokeStyle to_js_fill_or_stroke_style() const
{ {
if (auto* handle = m_fill_or_stoke_style.get_pointer<JS::Handle<CanvasGradient>>()) return m_fill_or_stroke_style.visit(
return *handle; [&](Gfx::Color color) -> JsFillOrStrokeStyle {
return m_fill_or_stoke_style.get<Gfx::Color>().to_deprecated_string(); return color.to_deprecated_string();
},
[&](auto handle) -> JsFillOrStrokeStyle {
return handle;
});
} }
private: private:
FillOrStrokeVariant m_fill_or_stoke_style; FillOrStrokeVariant m_fill_or_stroke_style;
RefPtr<Gfx::PaintStyle> m_color_paint_style { nullptr }; RefPtr<Gfx::PaintStyle> m_color_paint_style { nullptr };
}; };