2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2023-02-20 19:03:44 +01:00
|
|
|
* Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2019-04-04 15:20:02 +02:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/Action.h>
|
|
|
|
|
#include <LibGUI/ActionGroup.h>
|
|
|
|
|
#include <LibGUI/Button.h>
|
2021-04-05 14:33:13 +02:00
|
|
|
#include <LibGUI/Menu.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/Painter.h>
|
2022-01-24 14:22:02 -05:00
|
|
|
#include <LibGUI/Window.h>
|
2022-04-09 09:28:38 +02:00
|
|
|
#include <LibGfx/Font/Font.h>
|
2020-02-14 23:53:11 +01:00
|
|
|
#include <LibGfx/Palette.h>
|
|
|
|
|
#include <LibGfx/StylePainter.h>
|
2018-10-11 01:48:09 +02:00
|
|
|
|
2021-01-02 16:30:13 -07:00
|
|
|
REGISTER_WIDGET(GUI, Button)
|
2022-06-10 22:58:25 +02:00
|
|
|
REGISTER_WIDGET(GUI, DialogButton)
|
2021-01-02 16:30:13 -07:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
namespace GUI {
|
|
|
|
|
|
2023-02-12 10:53:11 +01:00
|
|
|
Button::Button(String text)
|
2020-12-28 13:18:10 +01:00
|
|
|
: AbstractButton(move(text))
|
2019-05-24 22:47:01 +02:00
|
|
|
{
|
2022-02-22 20:25:30 +01:00
|
|
|
set_min_size({ 40, 22 });
|
|
|
|
|
set_preferred_size({ SpecialDimension::OpportunisticGrow, 22 });
|
2020-10-30 10:58:27 +01:00
|
|
|
set_focus_policy(GUI::FocusPolicy::StrongFocus);
|
2020-12-30 13:58:38 +03:30
|
|
|
|
2022-01-24 14:22:02 -05:00
|
|
|
on_focus_change = [this](bool has_focus, auto) {
|
|
|
|
|
if (!is_default())
|
|
|
|
|
return;
|
|
|
|
|
if (!has_focus && is<Button>(window()->focused_widget()))
|
|
|
|
|
m_another_button_has_focus = true;
|
|
|
|
|
else
|
|
|
|
|
m_another_button_has_focus = false;
|
|
|
|
|
update();
|
|
|
|
|
};
|
|
|
|
|
|
2020-12-30 13:58:38 +03:30
|
|
|
REGISTER_ENUM_PROPERTY(
|
|
|
|
|
"button_style", button_style, set_button_style, Gfx::ButtonStyle,
|
|
|
|
|
{ Gfx::ButtonStyle::Normal, "Normal" },
|
2021-04-13 16:18:20 +02:00
|
|
|
{ Gfx::ButtonStyle::Coolbar, "Coolbar" });
|
2022-01-06 16:59:58 +00:00
|
|
|
|
2022-12-08 11:00:55 -05:00
|
|
|
REGISTER_WRITE_ONLY_STRING_PROPERTY("icon", set_icon_from_path);
|
2022-07-31 15:59:48 +02:00
|
|
|
REGISTER_BOOL_PROPERTY("default", is_default, set_default);
|
2019-05-24 22:47:01 +02:00
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
Button::~Button()
|
2018-10-11 01:48:09 +02:00
|
|
|
{
|
2019-04-12 02:53:27 +02:00
|
|
|
if (m_action)
|
2019-06-07 11:46:02 +02:00
|
|
|
m_action->unregister_button({}, *this);
|
2018-10-11 01:48:09 +02:00
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void Button::paint_event(PaintEvent& event)
|
2018-10-11 01:48:09 +02:00
|
|
|
{
|
2020-02-02 15:07:41 +01:00
|
|
|
Painter painter(*this);
|
2019-03-29 15:01:54 +01:00
|
|
|
painter.add_clip_rect(event.rect());
|
2018-10-12 14:58:16 +02:00
|
|
|
|
2023-01-17 17:15:25 -05:00
|
|
|
bool paint_pressed = is_being_pressed() || m_mimic_pressed || (m_menu && m_menu->is_visible());
|
2021-04-05 14:33:13 +02:00
|
|
|
|
2022-02-01 17:45:31 -05:00
|
|
|
Gfx::StylePainter::paint_button(painter, rect(), palette(), m_button_style, paint_pressed, is_hovered(), is_checked(), is_enabled(), is_focused(), is_default() && !another_button_has_focus());
|
2018-10-12 14:58:16 +02:00
|
|
|
|
2023-02-12 11:40:58 +01:00
|
|
|
if (text().is_empty() && !m_icon)
|
2019-04-04 15:20:02 +02:00
|
|
|
return;
|
|
|
|
|
|
2019-11-29 15:46:38 +01:00
|
|
|
auto content_rect = rect().shrunken(8, 2);
|
2020-06-10 10:57:59 +02:00
|
|
|
auto icon_location = m_icon ? content_rect.center().translated(-(m_icon->width() / 2), -(m_icon->height() / 2)) : Gfx::IntPoint();
|
2023-02-12 11:40:58 +01:00
|
|
|
if (m_icon && !text().is_empty())
|
2019-04-13 16:59:55 +02:00
|
|
|
icon_location.set_x(content_rect.x());
|
2020-10-25 15:22:21 +01:00
|
|
|
|
2021-04-05 14:33:13 +02:00
|
|
|
if (paint_pressed || is_checked()) {
|
2019-04-13 16:59:55 +02:00
|
|
|
painter.translate(1, 1);
|
2021-04-13 16:18:20 +02:00
|
|
|
} else if (m_icon && is_enabled() && is_hovered() && button_style() == Gfx::ButtonStyle::Coolbar) {
|
2020-12-27 18:43:34 +01:00
|
|
|
auto shadow_color = palette().button().darkened(0.7f);
|
|
|
|
|
painter.blit_filtered(icon_location.translated(1, 1), *m_icon, m_icon->rect(), [&shadow_color](auto) {
|
2020-10-25 15:22:21 +01:00
|
|
|
return shadow_color;
|
|
|
|
|
});
|
2021-04-12 11:47:09 -07:00
|
|
|
icon_location.translate_by(-1, -1);
|
2020-10-25 15:22:21 +01:00
|
|
|
}
|
|
|
|
|
|
2019-04-12 02:53:27 +02:00
|
|
|
if (m_icon) {
|
2022-05-09 00:08:06 +01:00
|
|
|
auto solid_color = m_icon->solid_color(60);
|
2022-06-27 14:40:27 +01:00
|
|
|
bool should_invert_icon = false;
|
|
|
|
|
if (solid_color.has_value()) {
|
|
|
|
|
auto contrast_ratio = palette().button().contrast_ratio(*solid_color);
|
|
|
|
|
// Note: 4.5 is the minimum recommended contrast ratio for text on the web:
|
|
|
|
|
// (https://developer.mozilla.org/en-US/docs/Web/Accessibility/Understanding_WCAG/Perceivable/Color_contrast)
|
|
|
|
|
// Reusing that threshold here as it seems to work reasonably well.
|
|
|
|
|
should_invert_icon = contrast_ratio < 4.5f && contrast_ratio < palette().button().contrast_ratio(solid_color->inverted());
|
|
|
|
|
}
|
2023-02-19 22:43:49 +01:00
|
|
|
auto icon = should_invert_icon ? m_icon->inverted().release_value_but_fixme_should_propagate_errors() : NonnullRefPtr { *m_icon };
|
2020-03-30 19:40:44 +02:00
|
|
|
if (is_enabled()) {
|
|
|
|
|
if (is_hovered())
|
2023-02-19 22:43:49 +01:00
|
|
|
painter.blit_brightened(icon_location, *icon, icon->rect());
|
2020-03-30 19:40:44 +02:00
|
|
|
else
|
2023-02-19 22:43:49 +01:00
|
|
|
painter.blit(icon_location, *icon, icon->rect());
|
2020-03-30 19:40:44 +02:00
|
|
|
} else {
|
2023-02-19 22:43:49 +01:00
|
|
|
painter.blit_disabled(icon_location, *icon, icon->rect(), palette());
|
2020-03-30 19:40:44 +02:00
|
|
|
}
|
2019-04-12 02:53:27 +02:00
|
|
|
}
|
2021-05-20 18:40:48 +02:00
|
|
|
auto& font = is_checked() ? this->font().bold_variant() : this->font();
|
2023-02-12 11:40:58 +01:00
|
|
|
if (m_icon && !text().is_empty()) {
|
2021-04-12 11:47:09 -07:00
|
|
|
content_rect.translate_by(m_icon->width() + icon_spacing(), 0);
|
2021-03-25 22:41:40 +01:00
|
|
|
content_rect.set_width(content_rect.width() - m_icon->width() - icon_spacing());
|
2019-04-13 16:59:55 +02:00
|
|
|
}
|
2019-05-24 22:54:37 +02:00
|
|
|
|
2023-03-03 19:32:19 +01:00
|
|
|
Gfx::IntRect text_rect { 0, 0, static_cast<int>(ceilf(font.width(text()))), font.pixel_size_rounded_up() };
|
2019-05-25 04:01:22 +02:00
|
|
|
if (text_rect.width() > content_rect.width())
|
|
|
|
|
text_rect.set_width(content_rect.width());
|
2019-05-25 20:15:52 +02:00
|
|
|
text_rect.align_within(content_rect, text_alignment());
|
2021-07-27 19:33:03 +00:00
|
|
|
paint_text(painter, text_rect, font, text_alignment());
|
2020-10-26 20:43:59 +01:00
|
|
|
|
|
|
|
|
if (is_focused()) {
|
|
|
|
|
Gfx::IntRect focus_rect;
|
2023-02-12 11:40:58 +01:00
|
|
|
if (m_icon && !text().is_empty())
|
2021-12-19 10:51:32 +01:00
|
|
|
focus_rect = text_rect.inflated(4, 4);
|
2020-10-26 20:43:59 +01:00
|
|
|
else
|
|
|
|
|
focus_rect = rect().shrunken(8, 8);
|
|
|
|
|
painter.draw_focus_rect(focus_rect, palette().focus_outline());
|
|
|
|
|
}
|
2018-10-11 01:48:09 +02:00
|
|
|
}
|
|
|
|
|
|
2020-05-12 20:30:33 +02:00
|
|
|
void Button::click(unsigned modifiers)
|
2019-03-19 01:41:00 +01:00
|
|
|
{
|
2019-04-12 02:53:27 +02:00
|
|
|
if (!is_enabled())
|
|
|
|
|
return;
|
2020-12-14 19:37:55 +01:00
|
|
|
|
|
|
|
|
NonnullRefPtr protector = *this;
|
|
|
|
|
|
2019-07-09 22:10:03 +02:00
|
|
|
if (is_checkable()) {
|
|
|
|
|
if (is_checked() && !is_uncheckable())
|
|
|
|
|
return;
|
2019-06-12 05:57:26 +02:00
|
|
|
set_checked(!is_checked());
|
2019-07-09 22:10:03 +02:00
|
|
|
}
|
2023-01-17 17:31:45 -05:00
|
|
|
|
|
|
|
|
mimic_pressed();
|
|
|
|
|
|
2019-03-19 01:41:00 +01:00
|
|
|
if (on_click)
|
2020-05-12 20:30:33 +02:00
|
|
|
on_click(modifiers);
|
2019-08-25 21:42:37 +02:00
|
|
|
if (m_action)
|
2019-12-09 21:25:48 +01:00
|
|
|
m_action->activate(this);
|
2019-03-19 01:41:00 +01:00
|
|
|
}
|
|
|
|
|
|
2023-02-17 11:13:25 +00:00
|
|
|
void Button::double_click(unsigned int modifiers)
|
|
|
|
|
{
|
|
|
|
|
if (on_double_click)
|
|
|
|
|
on_double_click(modifiers);
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-10 17:20:04 +02:00
|
|
|
void Button::middle_mouse_click(unsigned int modifiers)
|
|
|
|
|
{
|
|
|
|
|
if (!is_enabled())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
NonnullRefPtr protector = *this;
|
|
|
|
|
|
|
|
|
|
if (on_middle_mouse_click)
|
|
|
|
|
on_middle_mouse_click(modifiers);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-18 21:25:28 -04:00
|
|
|
void Button::context_menu_event(ContextMenuEvent& context_menu_event)
|
|
|
|
|
{
|
|
|
|
|
if (!is_enabled())
|
|
|
|
|
return;
|
|
|
|
|
if (on_context_menu_request)
|
|
|
|
|
on_context_menu_request(context_menu_event);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void Button::set_action(Action& action)
|
2019-04-12 02:53:27 +02:00
|
|
|
{
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-29 16:26:13 -06:00
|
|
|
m_action = action;
|
2019-06-07 11:46:02 +02:00
|
|
|
action.register_button({}, *this);
|
2022-12-07 21:18:02 +01:00
|
|
|
set_visible(action.is_visible());
|
2019-04-12 02:53:27 +02:00
|
|
|
set_enabled(action.is_enabled());
|
2019-04-26 21:09:56 +02:00
|
|
|
set_checkable(action.is_checkable());
|
|
|
|
|
if (action.is_checkable())
|
|
|
|
|
set_checked(action.is_checked());
|
2019-04-12 02:53:27 +02:00
|
|
|
}
|
2019-04-13 16:59:55 +02:00
|
|
|
|
2023-02-20 19:03:44 +01:00
|
|
|
void Button::set_icon(RefPtr<Gfx::Bitmap const> icon)
|
2019-04-13 16:59:55 +02:00
|
|
|
{
|
2019-04-14 02:36:06 +02:00
|
|
|
if (m_icon == icon)
|
2019-04-13 16:59:55 +02:00
|
|
|
return;
|
|
|
|
|
m_icon = move(icon);
|
|
|
|
|
update();
|
|
|
|
|
}
|
2019-07-09 22:10:03 +02:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
void Button::set_icon_from_path(DeprecatedString const& path)
|
2022-01-06 16:59:58 +00:00
|
|
|
{
|
2023-01-20 20:06:05 +01:00
|
|
|
auto maybe_bitmap = Gfx::Bitmap::load_from_file(path);
|
2022-01-06 16:59:58 +00:00
|
|
|
if (maybe_bitmap.is_error()) {
|
|
|
|
|
dbgln("Unable to load bitmap `{}` for button icon", path);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
set_icon(maybe_bitmap.release_value());
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
bool Button::is_uncheckable() const
|
2019-07-09 22:10:03 +02:00
|
|
|
{
|
|
|
|
|
if (!m_action)
|
|
|
|
|
return true;
|
|
|
|
|
if (!m_action->group())
|
|
|
|
|
return true;
|
|
|
|
|
return m_action->group()->is_unchecking_allowed();
|
|
|
|
|
}
|
2020-02-02 15:07:41 +01:00
|
|
|
|
2021-04-05 14:33:13 +02:00
|
|
|
void Button::set_menu(RefPtr<GUI::Menu> menu)
|
|
|
|
|
{
|
|
|
|
|
if (m_menu == menu)
|
|
|
|
|
return;
|
|
|
|
|
if (m_menu)
|
|
|
|
|
m_menu->on_visibility_change = nullptr;
|
|
|
|
|
m_menu = menu;
|
|
|
|
|
if (m_menu) {
|
|
|
|
|
m_menu->on_visibility_change = [&](bool) {
|
|
|
|
|
update();
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Button::mousedown_event(MouseEvent& event)
|
|
|
|
|
{
|
|
|
|
|
if (m_menu) {
|
2022-09-04 19:48:37 -04:00
|
|
|
m_menu->popup(screen_relative_rect().bottom_left(), {}, rect());
|
2021-04-05 14:33:13 +02:00
|
|
|
update();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
AbstractButton::mousedown_event(event);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 00:43:30 +02:00
|
|
|
void Button::mousemove_event(MouseEvent& event)
|
|
|
|
|
{
|
|
|
|
|
if (m_menu) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
AbstractButton::mousemove_event(event);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-24 14:22:02 -05:00
|
|
|
bool Button::is_default() const
|
|
|
|
|
{
|
|
|
|
|
if (!window())
|
|
|
|
|
return false;
|
|
|
|
|
return this == window()->default_return_key_widget();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Button::set_default(bool default_button)
|
|
|
|
|
{
|
|
|
|
|
deferred_invoke([this, default_button] {
|
|
|
|
|
VERIFY(window());
|
|
|
|
|
window()->set_default_return_key_widget(default_button ? this : nullptr);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-17 17:21:51 -05:00
|
|
|
void Button::mimic_pressed()
|
2022-02-06 13:40:07 -05:00
|
|
|
{
|
2023-01-17 17:31:45 -05:00
|
|
|
if (!is_being_pressed() && !was_being_pressed()) {
|
2023-01-17 17:21:51 -05:00
|
|
|
m_mimic_pressed = true;
|
2022-04-03 15:50:32 +10:00
|
|
|
|
|
|
|
|
stop_timer();
|
|
|
|
|
start_timer(80, Core::TimerShouldFireWhenNotVisible::Yes);
|
|
|
|
|
|
|
|
|
|
update();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Button::timer_event(Core::TimerEvent&)
|
|
|
|
|
{
|
2023-01-17 17:15:25 -05:00
|
|
|
if (m_mimic_pressed) {
|
2022-04-03 15:50:32 +10:00
|
|
|
m_mimic_pressed = false;
|
|
|
|
|
|
|
|
|
|
update();
|
|
|
|
|
}
|
2022-02-06 13:40:07 -05:00
|
|
|
}
|
|
|
|
|
|
2022-02-22 20:25:30 +01:00
|
|
|
Optional<UISize> Button::calculated_min_size() const
|
|
|
|
|
{
|
|
|
|
|
int horizontal = 0, vertical = 0;
|
|
|
|
|
|
2023-02-12 11:40:58 +01:00
|
|
|
if (!text().is_empty()) {
|
2022-02-22 20:25:30 +01:00
|
|
|
auto& font = this->font();
|
2023-02-28 18:17:43 +01:00
|
|
|
horizontal = static_cast<int>(ceilf(font.width(text()))) + 2;
|
2023-03-03 19:38:05 +01:00
|
|
|
vertical = font.pixel_size_rounded_up() + 4; // FIXME: Use actual maximum total height
|
2022-02-22 20:25:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_icon) {
|
|
|
|
|
vertical = max(vertical, m_icon->height());
|
|
|
|
|
|
|
|
|
|
horizontal += m_icon->width() + icon_spacing();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
horizontal += 8;
|
|
|
|
|
vertical += 4;
|
|
|
|
|
|
|
|
|
|
return UISize(horizontal, vertical);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
}
|