2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2021-03-25 23:41:39 +03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2020-08-22 14:08:39 +03:00
|
|
|
#include <AK/NeverDestroyed.h>
|
2020-02-16 11:17:49 +03:00
|
|
|
#include <LibCore/EventLoop.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/Action.h>
|
|
|
|
#include <LibGUI/Application.h>
|
2020-05-14 23:51:15 +03:00
|
|
|
#include <LibGUI/Clipboard.h>
|
2022-02-25 13:39:33 +03:00
|
|
|
#include <LibGUI/ConnectionToWindowServer.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/Desktop.h>
|
|
|
|
#include <LibGUI/Label.h>
|
2021-04-13 17:18:20 +03:00
|
|
|
#include <LibGUI/Menubar.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/Painter.h>
|
|
|
|
#include <LibGUI/Window.h>
|
2022-04-09 10:28:38 +03:00
|
|
|
#include <LibGfx/Font/Font.h>
|
2020-02-15 01:53:11 +03:00
|
|
|
#include <LibGfx/Palette.h>
|
2019-02-11 16:43:43 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
namespace GUI {
|
2019-02-11 16:56:23 +03:00
|
|
|
|
2020-12-28 23:26:47 +03:00
|
|
|
class Application::TooltipWindow final : public Window {
|
|
|
|
C_OBJECT(TooltipWindow);
|
|
|
|
|
|
|
|
public:
|
2022-04-01 20:58:27 +03:00
|
|
|
void set_tooltip(String const& tooltip)
|
2020-12-28 23:26:47 +03:00
|
|
|
{
|
2021-04-11 02:09:44 +03:00
|
|
|
m_label->set_text(Gfx::parse_ampersand_string(tooltip));
|
2021-06-02 23:46:15 +03:00
|
|
|
int tooltip_width = m_label->min_width() + 10;
|
2021-07-22 03:24:16 +03:00
|
|
|
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;
|
2021-06-02 23:46:15 +03:00
|
|
|
|
|
|
|
Gfx::IntRect desktop_rect = Desktop::the().rect();
|
|
|
|
if (tooltip_width > desktop_rect.width())
|
|
|
|
tooltip_width = desktop_rect.width();
|
|
|
|
|
2021-07-04 11:41:51 +03:00
|
|
|
set_rect(rect().x(), rect().y(), tooltip_width, tooltip_height);
|
2020-12-28 23:26:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2021-04-11 02:09:44 +03:00
|
|
|
m_label->set_autosize(true);
|
2020-12-28 23:26:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<Label> m_label;
|
|
|
|
};
|
|
|
|
|
2020-08-22 14:08:39 +03:00
|
|
|
static NeverDestroyed<WeakPtr<Application>> s_the;
|
2020-02-02 17:07:41 +03:00
|
|
|
|
2020-07-04 17:52:01 +03:00
|
|
|
Application* Application::the()
|
2019-02-11 16:56:23 +03:00
|
|
|
{
|
2020-12-29 23:14:21 +03:00
|
|
|
// 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!
|
2020-08-22 14:08:39 +03:00
|
|
|
return *s_the;
|
2019-02-11 16:56:23 +03:00
|
|
|
}
|
|
|
|
|
2021-07-26 00:02:51 +03:00
|
|
|
Application::Application(int argc, char** argv, Core::EventLoop::MakeInspectable make_inspectable)
|
2019-02-11 16:43:43 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(!*s_the);
|
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-30 01:26:13 +03:00
|
|
|
*s_the = *this;
|
2021-07-26 00:02:51 +03:00
|
|
|
m_event_loop = make<Core::EventLoop>(make_inspectable);
|
2022-02-25 13:39:33 +03:00
|
|
|
ConnectionToWindowServer::the();
|
2020-05-14 23:51:15 +03:00
|
|
|
Clipboard::initialize({});
|
2019-10-06 14:38:19 +03:00
|
|
|
if (argc > 0)
|
|
|
|
m_invoked_as = argv[0];
|
2020-05-12 16:47:13 +03:00
|
|
|
|
2020-08-14 20:55:25 +03:00
|
|
|
if (getenv("GUI_FOCUS_DEBUG"))
|
|
|
|
m_focus_debugging_enabled = true;
|
2020-05-12 16:47:13 +03:00
|
|
|
|
2021-07-28 01:58:01 +03:00
|
|
|
if (getenv("GUI_HOVER_DEBUG"))
|
|
|
|
m_hover_debugging_enabled = true;
|
|
|
|
|
2021-01-09 02:11:17 +03:00
|
|
|
if (getenv("GUI_DND_DEBUG"))
|
|
|
|
m_dnd_debugging_enabled = true;
|
|
|
|
|
2020-08-14 20:55:25 +03:00
|
|
|
for (int i = 1; i < argc; i++) {
|
|
|
|
String arg(argv[i]);
|
2020-05-12 16:47:13 +03:00
|
|
|
m_args.append(move(arg));
|
|
|
|
}
|
2020-12-28 23:26:47 +03:00
|
|
|
|
|
|
|
m_tooltip_show_timer = Core::Timer::create_single_shot(700, [this] {
|
2021-08-07 14:58:53 +03:00
|
|
|
request_tooltip_show();
|
2020-12-28 23:26:47 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
m_tooltip_hide_timer = Core::Timer::create_single_shot(50, [this] {
|
|
|
|
tooltip_hide_timer_did_fire();
|
|
|
|
});
|
2019-02-11 16:43:43 +03:00
|
|
|
}
|
|
|
|
|
2021-11-03 21:53:33 +03:00
|
|
|
static bool s_in_teardown;
|
|
|
|
|
|
|
|
bool Application::in_teardown()
|
|
|
|
{
|
|
|
|
return s_in_teardown;
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
Application::~Application()
|
2019-02-11 16:43:43 +03:00
|
|
|
{
|
2021-11-03 21:53:33 +03:00
|
|
|
s_in_teardown = true;
|
2020-07-04 17:52:01 +03:00
|
|
|
revoke_weak_ptrs();
|
2019-02-11 16:43:43 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
int Application::exec()
|
2019-02-11 16:43:43 +03:00
|
|
|
{
|
2020-07-01 21:49:00 +03:00
|
|
|
return m_event_loop->exec();
|
2019-02-11 16:43:43 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Application::quit(int exit_code)
|
2019-02-11 16:56:23 +03:00
|
|
|
{
|
2019-02-17 11:58:35 +03:00
|
|
|
m_event_loop->quit(exit_code);
|
2019-02-11 16:56:23 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Application::register_global_shortcut_action(Badge<Action>, Action& action)
|
2019-03-03 14:32:15 +03:00
|
|
|
{
|
2019-04-20 22:56:56 +03:00
|
|
|
m_global_shortcut_actions.set(action.shortcut(), &action);
|
2021-06-24 12:42:50 +03:00
|
|
|
m_global_shortcut_actions.set(action.alternate_shortcut(), &action);
|
2019-03-03 14:32:15 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Application::unregister_global_shortcut_action(Badge<Action>, Action& action)
|
2019-03-03 14:32:15 +03:00
|
|
|
{
|
2019-04-20 22:56:56 +03:00
|
|
|
m_global_shortcut_actions.remove(action.shortcut());
|
2021-06-24 12:42:50 +03:00
|
|
|
m_global_shortcut_actions.remove(action.alternate_shortcut());
|
2019-03-03 14:32:15 +03:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
Action* Application::action_for_key_event(KeyEvent const& event)
|
2019-03-03 14:32:15 +03:00
|
|
|
{
|
2020-02-02 17:07:41 +03:00
|
|
|
auto it = m_global_shortcut_actions.find(Shortcut(event.modifiers(), (KeyCode)event.key()));
|
2019-04-20 22:56:56 +03:00
|
|
|
if (it == m_global_shortcut_actions.end())
|
2019-03-03 14:32:15 +03:00
|
|
|
return nullptr;
|
|
|
|
return (*it).value;
|
|
|
|
}
|
2019-04-08 19:58:44 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
void Application::show_tooltip(String tooltip, Widget const* tooltip_source_widget)
|
2019-04-08 19:58:44 +03:00
|
|
|
{
|
2020-08-15 12:44:34 +03:00
|
|
|
m_tooltip_source_widget = tooltip_source_widget;
|
2019-04-08 19:58:44 +03:00
|
|
|
if (!m_tooltip_window) {
|
2020-02-15 03:27:37 +03:00
|
|
|
m_tooltip_window = TooltipWindow::construct();
|
2019-04-08 19:58:44 +03:00
|
|
|
m_tooltip_window->set_double_buffering_enabled(false);
|
|
|
|
}
|
2020-12-28 23:26:47 +03:00
|
|
|
m_tooltip_window->set_tooltip(move(tooltip));
|
|
|
|
|
|
|
|
if (m_tooltip_window->is_visible()) {
|
2021-08-07 14:58:53 +03:00
|
|
|
request_tooltip_show();
|
2020-12-28 23:26:47 +03:00
|
|
|
m_tooltip_show_timer->stop();
|
|
|
|
m_tooltip_hide_timer->stop();
|
|
|
|
} else {
|
|
|
|
m_tooltip_show_timer->restart();
|
|
|
|
m_tooltip_hide_timer->stop();
|
2019-12-05 19:59:06 +03:00
|
|
|
}
|
2019-04-08 19:58:44 +03:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
void Application::show_tooltip_immediately(String tooltip, Widget const* tooltip_source_widget)
|
2021-08-07 14:58:53 +03:00
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Application::hide_tooltip()
|
2019-04-08 19:58:44 +03:00
|
|
|
{
|
2020-12-28 23:26:47 +03:00
|
|
|
m_tooltip_show_timer->stop();
|
|
|
|
m_tooltip_hide_timer->start();
|
2019-04-08 19:58:44 +03:00
|
|
|
}
|
2019-07-23 19:16:25 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Application::did_create_window(Badge<Window>)
|
2019-10-25 08:23:35 +03:00
|
|
|
{
|
|
|
|
if (m_event_loop->was_exit_requested())
|
|
|
|
m_event_loop->unquit();
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Application::did_delete_last_window(Badge<Window>)
|
2019-07-23 19:16:25 +03:00
|
|
|
{
|
|
|
|
if (m_quit_when_last_window_deleted)
|
|
|
|
m_event_loop->quit(0);
|
|
|
|
}
|
2019-12-24 22:57:54 +03:00
|
|
|
|
2021-01-16 19:20:53 +03:00
|
|
|
void Application::set_system_palette(Core::AnonymousBuffer& buffer)
|
2019-12-24 22:57:54 +03:00
|
|
|
{
|
|
|
|
if (!m_system_palette)
|
2021-01-16 19:20:53 +03:00
|
|
|
m_system_palette = Gfx::PaletteImpl::create_with_anonymous_buffer(buffer);
|
2019-12-24 22:57:54 +03:00
|
|
|
else
|
|
|
|
m_system_palette->replace_internal_buffer({}, buffer);
|
|
|
|
|
|
|
|
if (!m_palette)
|
|
|
|
m_palette = m_system_palette;
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
void Application::set_palette(Palette const& palette)
|
2019-12-24 22:57:54 +03:00
|
|
|
{
|
2019-12-29 02:47:49 +03:00
|
|
|
m_palette = palette.impl();
|
2019-12-24 22:57:54 +03:00
|
|
|
}
|
2020-02-02 17:07:41 +03:00
|
|
|
|
2020-02-15 03:18:12 +03:00
|
|
|
Gfx::Palette Application::palette() const
|
|
|
|
{
|
|
|
|
return Palette(*m_palette);
|
|
|
|
}
|
|
|
|
|
2021-08-07 14:58:53 +03:00
|
|
|
void Application::request_tooltip_show()
|
2020-12-28 23:26:47 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(m_tooltip_window);
|
2020-12-28 23:26:47 +03:00
|
|
|
Gfx::IntRect desktop_rect = Desktop::the().rect();
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
int const margin = 30;
|
2022-02-25 13:39:33 +03:00
|
|
|
Gfx::IntPoint adjusted_pos = ConnectionToWindowServer::the().get_global_cursor_position();
|
2020-12-28 23:26:47 +03:00
|
|
|
|
2021-04-12 21:47:09 +03:00
|
|
|
adjusted_pos.translate_by(0, 18);
|
2020-12-28 23:26:47 +03:00
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
2021-06-02 23:46:15 +03:00
|
|
|
if (adjusted_pos.x() < 0)
|
|
|
|
adjusted_pos.set_x(0);
|
2020-12-28 23:26:47 +03:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2021-01-05 17:43:59 +03:00
|
|
|
void Application::window_did_become_active(Badge<Window>, Window& window)
|
|
|
|
{
|
|
|
|
m_active_window = window.make_weak_ptr<Window>();
|
2021-07-28 21:22:42 +03:00
|
|
|
window.update();
|
2021-01-05 17:43:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Application::window_did_become_inactive(Badge<Window>, Window& window)
|
|
|
|
{
|
|
|
|
if (m_active_window.ptr() != &window)
|
|
|
|
return;
|
2021-07-28 21:22:42 +03:00
|
|
|
window.update();
|
2021-01-05 17:43:59 +03:00
|
|
|
m_active_window = nullptr;
|
|
|
|
}
|
|
|
|
|
2021-01-09 02:11:17 +03:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
void Application::set_drag_hovered_widget_impl(Widget* widget, Gfx::IntPoint const& position, Vector<String> mime_types)
|
2021-01-09 00:23:06 +03:00
|
|
|
{
|
|
|
|
if (widget == m_drag_hovered_widget)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (m_drag_hovered_widget) {
|
2021-01-09 02:11:17 +03:00
|
|
|
Event leave_event(Event::DragLeave);
|
|
|
|
m_drag_hovered_widget->dispatch_event(leave_event, m_drag_hovered_widget->window());
|
2021-01-09 00:23:06 +03:00
|
|
|
}
|
|
|
|
|
2021-01-09 02:11:17 +03:00
|
|
|
set_pending_drop_widget(nullptr);
|
2021-01-09 00:23:06 +03:00
|
|
|
m_drag_hovered_widget = widget;
|
|
|
|
|
|
|
|
if (m_drag_hovered_widget) {
|
2021-01-09 13:01:41 +03:00
|
|
|
DragEvent enter_event(Event::DragEnter, position, move(mime_types));
|
2021-01-09 02:11:17 +03:00
|
|
|
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);
|
2021-01-09 00:23:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 13:39:33 +03:00
|
|
|
void Application::notify_drag_cancelled(Badge<ConnectionToWindowServer>)
|
2021-01-09 00:23:06 +03:00
|
|
|
{
|
|
|
|
set_drag_hovered_widget_impl(nullptr);
|
|
|
|
}
|
|
|
|
|
2021-04-17 19:21:15 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
}
|