From e074fc70e1518406476889934e71fdf21f4cac9f Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sun, 4 Apr 2021 11:51:36 -0400 Subject: [PATCH] LibWeb: Convert CheckBox to be a LabelableNode --- Userland/Libraries/LibWeb/Layout/CheckBox.cpp | 45 ++++++++++++++++--- 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 #include #include +#include #include namespace Web::Layout { CheckBox::CheckBox(DOM::Document& document, HTML::HTMLInputElement& element, NonnullRefPtr 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, 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, 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; + m_being_pressed = is_inside_node_or_label; + set_needs_display(); +} + +void CheckBox::handle_associated_label_mousedown(Badge