summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGUI/AbstractButton.cpp
blob: 18b0c7d8d6715d2b1abad984d4b2a2d370e60590 (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
 * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
 * Copyright (c) 2022, the SerenityOS developers.
 * Copyright (c) 2022, networkException <networkexception@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/JsonObject.h>
#include <LibCore/Timer.h>
#include <LibGUI/AbstractButton.h>
#include <LibGUI/Painter.h>
#include <LibGUI/Window.h>
#include <LibGfx/Palette.h>

namespace GUI {

AbstractButton::AbstractButton(String text)
{
    set_text(move(text));

    set_focus_policy(GUI::FocusPolicy::StrongFocus);
    set_background_role(Gfx::ColorRole::Button);
    set_foreground_role(Gfx::ColorRole::ButtonText);

    m_auto_repeat_timer = add<Core::Timer>();
    m_auto_repeat_timer->on_timeout = [this] {
        click();
    };

    REGISTER_STRING_PROPERTY("text", text, set_text);
    REGISTER_BOOL_PROPERTY("checked", is_checked, set_checked);
    REGISTER_BOOL_PROPERTY("checkable", is_checkable, set_checkable);
    REGISTER_BOOL_PROPERTY("exclusive", is_exclusive, set_exclusive);
}

void AbstractButton::set_text(String text)
{
    if (m_text == text)
        return;
    m_text = move(text);
    update();
}

void AbstractButton::set_checked(bool checked, AllowCallback allow_callback)
{
    if (m_checked == checked)
        return;
    m_checked = checked;

    if (is_exclusive() && checked && parent_widget()) {
        bool sibling_had_focus = false;
        parent_widget()->for_each_child_of_type<AbstractButton>([&](auto& sibling) {
            if (!sibling.is_exclusive())
                return IterationDecision::Continue;
            if (window() && window()->focused_widget() == &sibling)
                sibling_had_focus = true;
            if (!sibling.is_checked())
                return IterationDecision::Continue;
            sibling.m_checked = false;
            sibling.update();
            if (sibling.on_checked)
                sibling.on_checked(false);
            return IterationDecision::Continue;
        });
        m_checked = true;
        if (sibling_had_focus)
            set_focus(true);
    }

    if (is_exclusive() && parent_widget()) {
        // In a group of exclusive checkable buttons, only the currently checked button is focusable.
        parent_widget()->for_each_child_of_type<GUI::AbstractButton>([&](auto& button) {
            if (button.is_exclusive() && button.is_checkable())
                button.set_focus_policy(button.is_checked() ? GUI::FocusPolicy::StrongFocus : GUI::FocusPolicy::NoFocus);
            return IterationDecision::Continue;
        });
    }

    update();
    if (on_checked && allow_callback == AllowCallback::Yes)
        on_checked(checked);
}

void AbstractButton::set_checkable(bool checkable)
{
    if (m_checkable == checkable)
        return;
    m_checkable = checkable;
    update();
}

void AbstractButton::mousemove_event(MouseEvent& event)
{
    bool is_over = rect().contains(event.position());
    m_hovered = is_over;
    if (event.buttons() & m_pressed_mouse_button) {
        bool being_pressed = is_over;
        if (being_pressed != m_being_pressed) {
            m_being_pressed = being_pressed;
            if (m_auto_repeat_interval) {
                if (!m_being_pressed)
                    m_auto_repeat_timer->stop();
                else
                    m_auto_repeat_timer->start(m_auto_repeat_interval);
            }
            update();
        }
    }
    Widget::mousemove_event(event);
}

void AbstractButton::mousedown_event(MouseEvent& event)
{
    if (event.button() & m_allowed_mouse_buttons_for_pressing) {
        m_being_pressed = true;
        m_pressed_mouse_button = event.button();
        repaint();

        if (m_auto_repeat_interval) {
            click();
            m_auto_repeat_timer->start(m_auto_repeat_interval);
        }
        event.accept();
    }
    Widget::mousedown_event(event);
}

void AbstractButton::mouseup_event(MouseEvent& event)
{
    if (event.button() == m_pressed_mouse_button && m_being_pressed) {
        bool was_auto_repeating = m_auto_repeat_timer->is_active();
        m_auto_repeat_timer->stop();
        m_was_being_pressed = m_being_pressed;
        ScopeGuard update_was_being_pressed { [this] { m_was_being_pressed = m_being_pressed; } };
        m_being_pressed = false;
        m_pressed_mouse_button = MouseButton::None;
        if (!is_checkable() || is_checked())
            repaint();
        if (m_was_being_pressed && !was_auto_repeating) {
            switch (event.button()) {
            case MouseButton::Primary:
                click(event.modifiers());
                break;
            case MouseButton::Middle:
                middle_mouse_click(event.modifiers());
                break;
            default:
                VERIFY_NOT_REACHED();
            }
        }
    }
    Widget::mouseup_event(event);
}

void AbstractButton::doubleclick_event(GUI::MouseEvent& event)
{
    double_click(event.modifiers());
    Widget::doubleclick_event(event);
}

void AbstractButton::enter_event(Core::Event&)
{
    m_hovered = true;
    update();
}

void AbstractButton::leave_event(Core::Event& event)
{
    m_hovered = false;
    if (m_being_keyboard_pressed)
        m_being_keyboard_pressed = m_being_pressed = false;
    update();
    event.accept();
    Widget::leave_event(event);
}

void AbstractButton::focusout_event(GUI::FocusEvent& event)
{
    if (m_being_keyboard_pressed) {
        m_being_pressed = m_being_keyboard_pressed = false;
        event.accept();
        update();
    }
    Widget::focusout_event(event);
}

void AbstractButton::keydown_event(KeyEvent& event)
{
    if (event.key() == KeyCode::Key_Return || event.key() == KeyCode::Key_Space) {
        m_being_pressed = m_being_keyboard_pressed = true;
        update();
        event.accept();
        return;
    } else if (m_being_pressed && event.key() == KeyCode::Key_Escape) {
        m_being_pressed = m_being_keyboard_pressed = false;
        update();
        event.accept();
        return;
    }

    // Arrow keys switch the currently checked option within an exclusive group of checkable buttons.
    if (event.is_arrow_key() && !event.modifiers() && is_exclusive() && is_checkable() && parent_widget()) {
        event.accept();
        Vector<GUI::AbstractButton&> exclusive_siblings;
        size_t this_index = 0;
        parent_widget()->for_each_child_of_type<GUI::AbstractButton>([&](auto& sibling) {
            if (&sibling == this) {
                VERIFY(is_enabled());
                this_index = exclusive_siblings.size();
            }
            if (sibling.is_exclusive() && sibling.is_checkable() && sibling.is_enabled())
                exclusive_siblings.append(sibling);
            return IterationDecision::Continue;
        });
        if (exclusive_siblings.size() <= 1)
            return;
        size_t new_checked_index;
        if (event.key() == KeyCode::Key_Left || event.key() == KeyCode::Key_Up)
            new_checked_index = this_index == 0 ? exclusive_siblings.size() - 1 : this_index - 1;
        else
            new_checked_index = this_index == exclusive_siblings.size() - 1 ? 0 : this_index + 1;
        exclusive_siblings[new_checked_index].click();
        return;
    }
    Widget::keydown_event(event);
}

void AbstractButton::keyup_event(KeyEvent& event)
{
    bool was_being_pressed = m_being_pressed;
    if (was_being_pressed && (event.key() == KeyCode::Key_Return || event.key() == KeyCode::Key_Space)) {
        m_being_pressed = m_being_keyboard_pressed = false;
        click(event.modifiers());
        update();
        event.accept();
        return;
    }
    Widget::keyup_event(event);
}

void AbstractButton::paint_text(Painter& painter, Gfx::IntRect const& rect, Gfx::Font const& font, Gfx::TextAlignment text_alignment, Gfx::TextWrapping text_wrapping)
{
    auto clipped_rect = rect.intersected(this->rect());

    if (!is_enabled()) {
        painter.draw_text(clipped_rect.translated(1, 1), text(), font, text_alignment, palette().disabled_text_back(), Gfx::TextElision::Right, text_wrapping);
        painter.draw_text(clipped_rect, text(), font, text_alignment, palette().disabled_text_front(), Gfx::TextElision::Right, text_wrapping);
        return;
    }

    if (text().is_empty())
        return;
    painter.draw_text(clipped_rect, text(), font, text_alignment, palette().color(foreground_role()), Gfx::TextElision::Right, text_wrapping);
}

void AbstractButton::change_event(Event& event)
{
    if (event.type() == Event::Type::EnabledChange) {
        if (m_auto_repeat_timer->is_active())
            m_auto_repeat_timer->stop();
        if (!is_enabled()) {
            bool was_being_pressed = m_being_pressed;
            m_being_pressed = false;
            if (was_being_pressed)
                update();
        }
    }
    Widget::change_event(event);
}

}