1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 15:38:10 +00:00

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.
This commit is contained in:
Timothy Flynn 2021-04-04 12:17:24 -04:00 committed by Andreas Kling
parent d40f3aa0d0
commit 1b2123af80
4 changed files with 65 additions and 11 deletions

View file

@ -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;
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_node_or_label;
set_needs_display();
}