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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/NeverDestroyed.h>
#include <LibCore/EventLoop.h>
#include <LibGUI/Action.h>
#include <LibGUI/Application.h>
#include <LibGUI/Clipboard.h>
#include <LibGUI/ConnectionToWindowServer.h>
#include <LibGUI/Desktop.h>
#include <LibGUI/Label.h>
#include <LibGUI/Menubar.h>
#include <LibGUI/Painter.h>
#include <LibGUI/Window.h>
#include <LibGfx/Font/Font.h>
#include <LibGfx/Palette.h>
namespace GUI {
class Application::TooltipWindow final : public Window {
C_OBJECT(TooltipWindow);
public:
void set_tooltip(String const& tooltip)
{
m_label->set_text(Gfx::parse_ampersand_string(tooltip));
int tooltip_width = m_label->min_width() + 10;
int line_count = m_label->text().count("\n");
int glyph_height = m_label->font().glyph_height();
int tooltip_height = glyph_height * (1 + line_count) + ((glyph_height + 1) / 2) * line_count + 8;
Gfx::IntRect desktop_rect = Desktop::the().rect();
if (tooltip_width > desktop_rect.width())
tooltip_width = desktop_rect.width();
set_rect(rect().x(), rect().y(), tooltip_width, tooltip_height);
}
private:
TooltipWindow()
{
set_window_type(WindowType::Tooltip);
m_label = set_main_widget<Label>();
m_label->set_background_role(Gfx::ColorRole::Tooltip);
m_label->set_foreground_role(Gfx::ColorRole::TooltipText);
m_label->set_fill_with_background_color(true);
m_label->set_frame_thickness(1);
m_label->set_frame_shape(Gfx::FrameShape::Container);
m_label->set_frame_shadow(Gfx::FrameShadow::Plain);
m_label->set_autosize(true);
}
RefPtr<Label> m_label;
};
static NeverDestroyed<WeakPtr<Application>> s_the;
Application* Application::the()
{
// NOTE: If we don't explicitly call revoke_weak_ptrs() in the
// ~Application destructor, we would have to change this to
// return s_the->strong_ref().ptr();
// This is because this is using the unsafe operator*/operator->
// that do not have the ability to check the ref count!
return *s_the;
}
Application::Application(int argc, char** argv, Core::EventLoop::MakeInspectable make_inspectable)
{
VERIFY(!*s_the);
*s_the = *this;
m_event_loop = make<Core::EventLoop>(make_inspectable);
ConnectionToWindowServer::the();
Clipboard::initialize({});
if (argc > 0)
m_invoked_as = argv[0];
if (getenv("GUI_FOCUS_DEBUG"))
m_focus_debugging_enabled = true;
if (getenv("GUI_HOVER_DEBUG"))
m_hover_debugging_enabled = true;
if (getenv("GUI_DND_DEBUG"))
m_dnd_debugging_enabled = true;
for (int i = 1; i < argc; i++) {
String arg(argv[i]);
m_args.append(move(arg));
}
m_tooltip_show_timer = Core::Timer::create_single_shot(700, [this] {
request_tooltip_show();
});
m_tooltip_hide_timer = Core::Timer::create_single_shot(50, [this] {
tooltip_hide_timer_did_fire();
});
}
static bool s_in_teardown;
bool Application::in_teardown()
{
return s_in_teardown;
}
Application::~Application()
{
s_in_teardown = true;
revoke_weak_ptrs();
}
int Application::exec()
{
return m_event_loop->exec();
}
void Application::quit(int exit_code)
{
m_event_loop->quit(exit_code);
}
void Application::register_global_shortcut_action(Badge<Action>, Action& action)
{
m_global_shortcut_actions.set(action.shortcut(), &action);
m_global_shortcut_actions.set(action.alternate_shortcut(), &action);
}
void Application::unregister_global_shortcut_action(Badge<Action>, Action& action)
{
m_global_shortcut_actions.remove(action.shortcut());
m_global_shortcut_actions.remove(action.alternate_shortcut());
}
Action* Application::action_for_key_event(KeyEvent const& event)
{
auto it = m_global_shortcut_actions.find(Shortcut(event.modifiers(), (KeyCode)event.key()));
if (it == m_global_shortcut_actions.end())
return nullptr;
return (*it).value;
}
void Application::show_tooltip(String tooltip, Widget const* tooltip_source_widget)
{
m_tooltip_source_widget = tooltip_source_widget;
if (!m_tooltip_window) {
m_tooltip_window = TooltipWindow::construct();
m_tooltip_window->set_double_buffering_enabled(false);
}
m_tooltip_window->set_tooltip(move(tooltip));
if (m_tooltip_window->is_visible()) {
request_tooltip_show();
m_tooltip_show_timer->stop();
m_tooltip_hide_timer->stop();
} else {
m_tooltip_show_timer->restart();
m_tooltip_hide_timer->stop();
}
}
void Application::show_tooltip_immediately(String tooltip, Widget const* tooltip_source_widget)
{
m_tooltip_source_widget = tooltip_source_widget;
if (!m_tooltip_window) {
m_tooltip_window = TooltipWindow::construct();
m_tooltip_window->set_double_buffering_enabled(false);
}
m_tooltip_window->set_tooltip(move(tooltip));
request_tooltip_show();
m_tooltip_show_timer->stop();
m_tooltip_hide_timer->stop();
}
void Application::hide_tooltip()
{
m_tooltip_show_timer->stop();
m_tooltip_hide_timer->start();
}
void Application::did_create_window(Badge<Window>)
{
if (m_event_loop->was_exit_requested())
m_event_loop->unquit();
}
void Application::did_delete_last_window(Badge<Window>)
{
if (m_quit_when_last_window_deleted)
m_event_loop->quit(0);
}
void Application::set_system_palette(Core::AnonymousBuffer& buffer)
{
if (!m_system_palette)
m_system_palette = Gfx::PaletteImpl::create_with_anonymous_buffer(buffer);
else
m_system_palette->replace_internal_buffer({}, buffer);
if (!m_palette)
m_palette = m_system_palette;
}
void Application::set_palette(Palette const& palette)
{
m_palette = palette.impl();
}
Gfx::Palette Application::palette() const
{
return Palette(*m_palette);
}
void Application::request_tooltip_show()
{
VERIFY(m_tooltip_window);
Gfx::IntRect desktop_rect = Desktop::the().rect();
int const margin = 30;
Gfx::IntPoint adjusted_pos = ConnectionToWindowServer::the().get_global_cursor_position();
adjusted_pos.translate_by(0, 18);
if (adjusted_pos.x() + m_tooltip_window->width() >= desktop_rect.width() - margin) {
adjusted_pos = adjusted_pos.translated(-m_tooltip_window->width(), 0);
}
if (adjusted_pos.y() + m_tooltip_window->height() >= desktop_rect.height() - margin) {
adjusted_pos = adjusted_pos.translated(0, -(m_tooltip_window->height() * 2));
}
if (adjusted_pos.x() < 0)
adjusted_pos.set_x(0);
m_tooltip_window->move_to(adjusted_pos);
m_tooltip_window->show();
}
void Application::tooltip_hide_timer_did_fire()
{
m_tooltip_source_widget = nullptr;
if (m_tooltip_window)
m_tooltip_window->hide();
}
void Application::window_did_become_active(Badge<Window>, Window& window)
{
m_active_window = window.make_weak_ptr<Window>();
window.update();
}
void Application::window_did_become_inactive(Badge<Window>, Window& window)
{
if (m_active_window.ptr() != &window)
return;
window.update();
m_active_window = nullptr;
}
void Application::set_pending_drop_widget(Widget* widget)
{
if (m_pending_drop_widget == widget)
return;
if (m_pending_drop_widget)
m_pending_drop_widget->update();
m_pending_drop_widget = widget;
if (m_pending_drop_widget)
m_pending_drop_widget->update();
}
void Application::set_drag_hovered_widget_impl(Widget* widget, Gfx::IntPoint const& position, Vector<String> mime_types)
{
if (widget == m_drag_hovered_widget)
return;
if (m_drag_hovered_widget) {
Event leave_event(Event::DragLeave);
m_drag_hovered_widget->dispatch_event(leave_event, m_drag_hovered_widget->window());
}
set_pending_drop_widget(nullptr);
m_drag_hovered_widget = widget;
if (m_drag_hovered_widget) {
DragEvent enter_event(Event::DragEnter, position, move(mime_types));
enter_event.ignore();
m_drag_hovered_widget->dispatch_event(enter_event, m_drag_hovered_widget->window());
if (enter_event.is_accepted())
set_pending_drop_widget(m_drag_hovered_widget);
}
}
void Application::notify_drag_cancelled(Badge<ConnectionToWindowServer>)
{
set_drag_hovered_widget_impl(nullptr);
}
void Application::event(Core::Event& event)
{
if (event.type() == GUI::Event::ActionEnter || event.type() == GUI::Event::ActionLeave) {
auto& action_event = static_cast<ActionEvent&>(event);
auto& action = action_event.action();
if (action_event.type() == GUI::Event::ActionEnter) {
if (on_action_enter)
on_action_enter(action);
} else {
if (on_action_leave)
on_action_leave(action);
}
}
Object::event(event);
}
}
|