summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2021-04-04 12:17:24 -0400
committerAndreas Kling <kling@serenityos.org>2021-04-04 18:30:15 +0200
commit1b2123af80268547d783d439013988cea67b20aa (patch)
tree6d91e4e8fd51c9df462d6ec73546209a706fd51b /Userland
parentd40f3aa0d0026db460a1b4a9c6ad0b172f5f0112 (diff)
downloadserenity-1b2123af80268547d783d439013988cea67b20aa.zip
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.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibWeb/Layout/ButtonBox.cpp49
-rw-r--r--Userland/Libraries/LibWeb/Layout/ButtonBox.h12
-rw-r--r--Userland/Libraries/LibWeb/Layout/Label.cpp14
-rw-r--r--Userland/Libraries/LibWeb/Layout/Label.h1
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 <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();
}
diff --git a/Userland/Libraries/LibWeb/Layout/ButtonBox.h b/Userland/Libraries/LibWeb/Layout/ButtonBox.h
index f26c7ed8d8..0719e9197d 100644
--- a/Userland/Libraries/LibWeb/Layout/ButtonBox.h
+++ b/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 };
};
diff --git a/Userland/Libraries/LibWeb/Layout/Label.cpp b/Userland/Libraries/LibWeb/Layout/Label.cpp
index 6de2615ad4..07bef35fea 100644
--- a/Userland/Libraries/LibWeb/Layout/Label.cpp
+++ b/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;
diff --git a/Userland/Libraries/LibWeb/Layout/Label.h b/Userland/Libraries/LibWeb/Layout/Label.h
index ff804d64db..4e7af265b9 100644
--- a/Userland/Libraries/LibWeb/Layout/Label.h
+++ b/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()); }