Sfoglia il codice sorgente

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.
Timothy Flynn 4 anni fa
parent
commit
1b2123af80

+ 42 - 7
Userland/Libraries/LibWeb/Layout/ButtonBox.cpp

@@ -30,12 +30,13 @@
 #include <LibGfx/StylePainter.h>
 #include <LibWeb/DOM/Document.h>
 #include <LibWeb/Layout/ButtonBox.h>
+#include <LibWeb/Layout/Label.h>
 #include <LibWeb/Page/Frame.h>
 
 namespace Web::Layout {
 
 ButtonBox::ButtonBox(DOM::Document& document, HTML::HTMLInputElement& element, NonnullRefPtr<CSS::StyleProperties> 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<EventHandler>, 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<EventHandler>, 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_node_or_label;
+    set_needs_display();
+}
+
+void ButtonBox::handle_associated_label_mousedown(Badge<Label>)
+{
+    m_being_pressed = true;
+    set_needs_display();
+}
+
+void ButtonBox::handle_associated_label_mouseup(Badge<Label>)
+{
+    // NOTE: Handling the click may run arbitrary JS, which could disappear this node.
+    NonnullRefPtr protected_this = *this;
+    NonnullRefPtr protected_frame = frame();
+
+    dom_node().did_click_button({});
+    m_being_pressed = false;
+}
+
+void ButtonBox::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();
 }
 

+ 8 - 4
Userland/Libraries/LibWeb/Layout/ButtonBox.h

@@ -27,11 +27,11 @@
 #pragma once
 
 #include <LibWeb/HTML/HTMLInputElement.h>
-#include <LibWeb/Layout/ReplacedBox.h>
+#include <LibWeb/Layout/LabelableNode.h>
 
 namespace Web::Layout {
 
-class ButtonBox : public ReplacedBox {
+class ButtonBox : public LabelableNode {
 public:
     ButtonBox(DOM::Document&, HTML::HTMLInputElement&, NonnullRefPtr<CSS::StyleProperties>);
     virtual ~ButtonBox() override;
@@ -39,8 +39,8 @@ public:
     virtual void prepare_for_replaced_layout() 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; }
@@ -48,6 +48,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 };
 };

+ 14 - 0
Userland/Libraries/LibWeb/Layout/Label.cpp

@@ -32,6 +32,7 @@
 #include <LibWeb/Layout/InitialContainingBlockBox.h>
 #include <LibWeb/Layout/Label.h>
 #include <LibWeb/Layout/LabelableNode.h>
+#include <LibWeb/Layout/TextNode.h>
 #include <LibWeb/Page/Frame.h>
 
 namespace Web::Layout {
@@ -95,6 +96,19 @@ bool Label::is_inside_associated_label(LabelableNode& control, const Gfx::IntPoi
     return false;
 }
 
+bool Label::is_associated_label_hovered(LabelableNode& control)
+{
+    if (auto* label = label_for_control_node(control); label) {
+        if (label->document().hovered_node() == &label->dom_node())
+            return true;
+
+        if (auto* child = label->first_child_of_type<TextNode>(); child)
+            return label->document().hovered_node() == &child->dom_node();
+    }
+
+    return false;
+}
+
 Label* Label::label_for_control_node(LabelableNode& control)
 {
     Label* label = nullptr;

+ 1 - 0
Userland/Libraries/LibWeb/Layout/Label.h

@@ -37,6 +37,7 @@ public:
     virtual ~Label() override;
 
     static bool is_inside_associated_label(LabelableNode&, const Gfx::IntPoint&);
+    static bool is_associated_label_hovered(LabelableNode&);
 
     const HTML::HTMLLabelElement& dom_node() const { return static_cast<const HTML::HTMLLabelElement&>(*BlockBox::dom_node()); }
     HTML::HTMLLabelElement& dom_node() { return static_cast<HTML::HTMLLabelElement&>(*BlockBox::dom_node()); }