forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ButtonPaintable.cpp
75 lines (62 loc) 路 2.46 KB
/
ButtonPaintable.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
* Copyright (c) 2018-2022, Andreas Kling <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibGUI/Event.h>
#include <LibWeb/FontCache.h>
#include <LibWeb/HTML/BrowsingContext.h>
#include <LibWeb/HTML/HTMLImageElement.h>
#include <LibWeb/Layout/ButtonBox.h>
#include <LibWeb/Layout/Label.h>
#include <LibWeb/Painting/ButtonPaintable.h>
namespace Web::Painting {
JS::NonnullGCPtr<ButtonPaintable> ButtonPaintable::create(Layout::ButtonBox const& layout_box)
{
return layout_box.heap().allocate_without_realm<ButtonPaintable>(layout_box);
}
ButtonPaintable::ButtonPaintable(Layout::ButtonBox const& layout_box)
: LabelablePaintable(layout_box)
{
}
Layout::ButtonBox const& ButtonPaintable::layout_box() const
{
return static_cast<Layout::ButtonBox const&>(layout_node());
}
Layout::ButtonBox& ButtonPaintable::layout_box()
{
return static_cast<Layout::ButtonBox&>(layout_node());
}
void ButtonPaintable::paint(PaintContext& context, PaintPhase phase) const
{
if (!is_visible())
return;
PaintableBox::paint(context, phase);
auto const& dom_node = layout_box().dom_node();
if (is<HTML::HTMLInputElement>(dom_node) && phase == PaintPhase::Foreground) {
auto button_rect = context.enclosing_device_rect(absolute_rect());
auto text_rect = button_rect;
// Apply CSS text-indent property to text rect
// FIXME: The second parameter to to_px() needs to be the block container鈥檚 own inline-axis inner size:
// https://drafts.csswg.org/css-text-3/#propdef-text-indent
auto text_indent = computed_values().text_indent().to_px(layout_box(), CSSPixels());
text_rect.translate_by(context.rounded_device_pixels(text_indent), 0);
// Apply button pressed state offset
if (being_pressed()) {
auto offset = context.rounded_device_pixels(1);
text_rect.translate_by(offset, offset);
}
// Paint button text clipped to button rect
auto& painter = context.painter();
painter.save();
painter.add_clip_rect(button_rect.to_type<int>());
painter.draw_text(
text_rect.to_type<int>(),
static_cast<HTML::HTMLInputElement const&>(dom_node).value(),
document().style_computer().font_cache().scaled_font(layout_box().font(), context.device_pixels_per_css_pixel()),
Gfx::TextAlignment::Center,
computed_values().color());
painter.restore();
}
}
}