From 1b2123af80268547d783d439013988cea67b20aa Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sun, 4 Apr 2021 12:17:24 -0400 Subject: [PATCH] LibWeb: Convert ButtonBox to be a LabelableNode This also adds an API to Label to determine if the Label itself or its child TextNode is hovered. This allows ButtonBox to render in a hovered state when the label is hovered. --- .../Libraries/LibWeb/Layout/ButtonBox.cpp | 49 ++++++++++++++++--- Userland/Libraries/LibWeb/Layout/ButtonBox.h | 12 +++-- Userland/Libraries/LibWeb/Layout/Label.cpp | 14 ++++++ Userland/Libraries/LibWeb/Layout/Label.h | 1 + 4 files changed, 65 insertions(+), 11 deletions(-) diff --git a/Userland/Libraries/LibWeb/Layout/ButtonBox.cpp b/Userland/Libraries/LibWeb/Layout/ButtonBox.cpp index c458206e3e..46fea5f0c7 100644 --- a/Userland/Libraries/LibWeb/Layout/ButtonBox.cpp +++ b/Userland/Libraries/LibWeb/Layout/ButtonBox.cpp @@ -30,12 +30,13 @@ #include #include #include +#include #include namespace Web::Layout { ButtonBox::ButtonBox(DOM::Document& document, HTML::HTMLInputElement& element, NonnullRefPtr style) - : ReplacedBox(document, element, move(style)) + : LabelableNode(document, element, move(style)) { } @@ -57,10 +58,13 @@ void ButtonBox::paint(PaintContext& context, PaintPhase phase) if (!is_visible()) return; - ReplacedBox::paint(context, phase); + LabelableNode::paint(context, phase); if (phase == PaintPhase::Foreground) { bool hovered = document().hovered_node() == &dom_node(); + if (!hovered) + hovered = Label::is_associated_label_hovered(*this); + Gfx::StylePainter::paint_button(context.painter(), enclosing_int_rect(absolute_rect()), context.palette(), Gfx::ButtonStyle::Normal, m_being_pressed, hovered, dom_node().checked(), dom_node().enabled()); auto text_rect = enclosing_int_rect(absolute_rect()); @@ -91,8 +95,11 @@ void ButtonBox::handle_mouseup(Badge, const Gfx::IntPoint& positio NonnullRefPtr protected_this = *this; NonnullRefPtr protected_frame = frame(); - 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().did_click_button({}); m_being_pressed = false; @@ -106,11 +113,39 @@ void ButtonBox::handle_mousemove(Badge, const Gfx::IntPoint& posit 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 ButtonBox::handle_associated_label_mousedown(Badge