diff options
author | Timothy Flynn <trflynn89@pm.me> | 2021-04-04 11:51:36 -0400 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-04-04 18:30:15 +0200 |
commit | e074fc70e1518406476889934e71fdf21f4cac9f (patch) | |
tree | 3948e39c71398d5f369fd8be738692e82a000527 /Userland | |
parent | 26c97692471133a7b23af1404d14308725daf8b8 (diff) | |
download | serenity-e074fc70e1518406476889934e71fdf21f4cac9f.zip |
LibWeb: Convert CheckBox to be a LabelableNode
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibWeb/Layout/CheckBox.cpp | 45 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Layout/CheckBox.h | 12 |
2 files changed, 46 insertions, 11 deletions
diff --git a/Userland/Libraries/LibWeb/Layout/CheckBox.cpp b/Userland/Libraries/LibWeb/Layout/CheckBox.cpp index 18e029a853..ab97f1b1bb 100644 --- a/Userland/Libraries/LibWeb/Layout/CheckBox.cpp +++ b/Userland/Libraries/LibWeb/Layout/CheckBox.cpp @@ -29,12 +29,13 @@ #include <LibGfx/Painter.h> #include <LibGfx/StylePainter.h> #include <LibWeb/Layout/CheckBox.h> +#include <LibWeb/Layout/Label.h> #include <LibWeb/Page/Frame.h> namespace Web::Layout { CheckBox::CheckBox(DOM::Document& document, HTML::HTMLInputElement& element, NonnullRefPtr<CSS::StyleProperties> style) - : ReplacedBox(document, element, move(style)) + : LabelableNode(document, element, move(style)) { set_has_intrinsic_width(true); set_has_intrinsic_height(true); @@ -51,7 +52,7 @@ void CheckBox::paint(PaintContext& context, PaintPhase phase) if (!is_visible()) return; - ReplacedBox::paint(context, phase); + LabelableNode::paint(context, phase); if (phase == PaintPhase::Foreground) { Gfx::StylePainter::paint_check_box(context.painter(), enclosing_int_rect(absolute_rect()), context.palette(), dom_node().enabled(), dom_node().checked(), m_being_pressed); @@ -78,8 +79,11 @@ void CheckBox::handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint& position // NOTE: Changing the checked state of the DOM node may run arbitrary JS, which could disappear this node. NonnullRefPtr protect = *this; - bool is_inside = enclosing_int_rect(absolute_rect()).contains(position); - if (is_inside) + bool is_inside_node_or_label = enclosing_int_rect(absolute_rect()).contains(position); + if (!is_inside_node_or_label) + is_inside_node_or_label = Label::is_inside_associated_label(*this, position); + + if (is_inside_node_or_label) dom_node().set_checked(!dom_node().checked()); m_being_pressed = false; @@ -92,11 +96,38 @@ void CheckBox::handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint& positi if (!m_tracking_mouse || !dom_node().enabled()) return; - bool is_inside = enclosing_int_rect(absolute_rect()).contains(position); - if (m_being_pressed == is_inside) + bool is_inside_node_or_label = enclosing_int_rect(absolute_rect()).contains(position); + if (!is_inside_node_or_label) + is_inside_node_or_label = Label::is_inside_associated_label(*this, position); + + if (m_being_pressed == is_inside_node_or_label) + return; + + m_being_pressed = is_inside_node_or_label; + set_needs_display(); +} + +void CheckBox::handle_associated_label_mousedown(Badge<Label>) +{ + m_being_pressed = true; + set_needs_display(); +} + +void CheckBox::handle_associated_label_mouseup(Badge<Label>) +{ + // NOTE: Changing the checked state of the DOM node may run arbitrary JS, which could disappear this node. + NonnullRefPtr protect = *this; + + dom_node().set_checked(!dom_node().checked()); + m_being_pressed = false; +} + +void CheckBox::handle_associated_label_mousemove(Badge<Label>, bool is_inside_node_or_label) +{ + if (m_being_pressed == is_inside_node_or_label) return; - m_being_pressed = is_inside; + m_being_pressed = is_inside_node_or_label; set_needs_display(); } diff --git a/Userland/Libraries/LibWeb/Layout/CheckBox.h b/Userland/Libraries/LibWeb/Layout/CheckBox.h index 47809c07f9..113b7c5c3e 100644 --- a/Userland/Libraries/LibWeb/Layout/CheckBox.h +++ b/Userland/Libraries/LibWeb/Layout/CheckBox.h @@ -27,19 +27,19 @@ #pragma once #include <LibWeb/HTML/HTMLInputElement.h> -#include <LibWeb/Layout/ReplacedBox.h> +#include <LibWeb/Layout/LabelableNode.h> namespace Web::Layout { -class CheckBox : public ReplacedBox { +class CheckBox : public LabelableNode { public: CheckBox(DOM::Document&, HTML::HTMLInputElement&, NonnullRefPtr<CSS::StyleProperties>); virtual ~CheckBox() override; virtual void paint(PaintContext&, PaintPhase) override; - const HTML::HTMLInputElement& dom_node() const { return static_cast<const HTML::HTMLInputElement&>(ReplacedBox::dom_node()); } - HTML::HTMLInputElement& dom_node() { return static_cast<HTML::HTMLInputElement&>(ReplacedBox::dom_node()); } + const HTML::HTMLInputElement& dom_node() const { return static_cast<const HTML::HTMLInputElement&>(LabelableNode::dom_node()); } + HTML::HTMLInputElement& dom_node() { return static_cast<HTML::HTMLInputElement&>(LabelableNode::dom_node()); } private: virtual bool wants_mouse_events() const override { return true; } @@ -47,6 +47,10 @@ private: virtual void handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned modifiers) override; virtual void handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint&, unsigned buttons, unsigned modifiers) override; + virtual void handle_associated_label_mousedown(Badge<Label>) override; + virtual void handle_associated_label_mouseup(Badge<Label>) override; + virtual void handle_associated_label_mousemove(Badge<Label>, bool is_inside_node_or_label) override; + bool m_being_pressed { false }; bool m_tracking_mouse { false }; }; |