1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 22:48:11 +00:00

LibGUI: Turn GUI::Application::the() into a pointer

During app teardown, the Application object may be destroyed before
something else, and so having Application::the() return a reference was
obscuring the truth about its lifetime.

This patch makes the API more honest by returning a pointer. While
this makes call sites look a bit more sketchy, do note that the global
Application pointer only becomes null during app teardown.
This commit is contained in:
Andreas Kling 2020-07-04 16:52:01 +02:00
parent f7577585a6
commit ca93c22ae2
26 changed files with 51 additions and 49 deletions

View file

@ -157,14 +157,17 @@ Action::Action(const StringView& text, const Shortcut& shortcut, RefPtr<Gfx::Bit
m_scope = ShortcutScope::WindowLocal;
} else {
m_scope = ShortcutScope::ApplicationGlobal;
Application::the().register_global_shortcut_action({}, *this);
if (auto* app = Application::the())
app->register_global_shortcut_action({}, *this);
}
}
Action::~Action()
{
if (m_shortcut.is_valid() && m_scope == ShortcutScope::ApplicationGlobal)
Application::the().unregister_global_shortcut_action({}, *this);
if (m_shortcut.is_valid() && m_scope == ShortcutScope::ApplicationGlobal) {
if (auto* app = Application::the())
app->unregister_global_shortcut_action({}, *this);
}
}
void Action::activate(Core::Object* activator)

View file

@ -39,18 +39,17 @@
namespace GUI {
static Application* s_the;
static WeakPtr<Application> s_the;
Application& Application::the()
Application* Application::the()
{
ASSERT(s_the);
return *s_the;
return s_the;
}
Application::Application(int argc, char** argv)
{
ASSERT(!s_the);
s_the = this;
s_the = make_weak_ptr();
m_event_loop = make<Core::EventLoop>();
WindowServerConnection::the();
Clipboard::initialize({});
@ -68,7 +67,7 @@ Application::Application(int argc, char** argv)
Application::~Application()
{
s_the = nullptr;
revoke_weak_ptrs();
}
int Application::exec()

View file

@ -40,7 +40,7 @@ class Application : public Core::Object {
C_OBJECT(Application);
public:
static Application& the();
static Application* the();
~Application();

View file

@ -98,7 +98,7 @@ Widget::Widget()
, m_background_role(Gfx::ColorRole::Window)
, m_foreground_role(Gfx::ColorRole::WindowText)
, m_font(Gfx::Font::default_font())
, m_palette(Application::the().palette().impl())
, m_palette(Application::the()->palette().impl())
{
}
@ -245,7 +245,7 @@ void Widget::handle_paint_event(PaintEvent& event)
painter.draw_rect(rect(), Color::Magenta);
}
if (Application::the().focus_debugging_enabled()) {
if (Application::the()->focus_debugging_enabled()) {
if (is_focused()) {
Painter painter(*this);
painter.draw_rect(rect(), Color::Cyan);
@ -318,13 +318,13 @@ void Widget::handle_mousedoubleclick_event(MouseEvent& event)
void Widget::handle_enter_event(Core::Event& event)
{
if (has_tooltip())
Application::the().show_tooltip(m_tooltip, screen_relative_rect().center().translated(0, height() / 2));
Application::the()->show_tooltip(m_tooltip, screen_relative_rect().center().translated(0, height() / 2));
enter_event(event);
}
void Widget::handle_leave_event(Core::Event& event)
{
Application::the().hide_tooltip();
Application::the()->hide_tooltip();
leave_event(event);
}

View file

@ -113,7 +113,7 @@ void Window::show()
apply_icon();
reified_windows->set(m_window_id, this);
Application::the().did_create_window({});
Application::the()->did_create_window({});
update();
}
@ -158,7 +158,7 @@ void Window::hide()
}
}
if (!app_has_visible_windows)
Application::the().did_delete_last_window({});
Application::the()->did_delete_last_window({});
}
void Window::set_title(const StringView& title)

View file

@ -61,7 +61,7 @@ static void set_system_theme_from_shbuf_id(int id)
auto system_theme = SharedBuffer::create_from_shbuf_id(id);
ASSERT(system_theme);
Gfx::set_system_theme(*system_theme);
Application::the().set_system_palette(*system_theme);
Application::the()->set_system_palette(*system_theme);
}
void WindowServerConnection::handshake()
@ -154,7 +154,7 @@ void WindowServerConnection::handle(const Messages::WindowClient::KeyDown& messa
}
if (!action) {
action = Application::the().action_for_key_event(*key_event);
action = Application::the()->action_for_key_event(*key_event);
#ifdef KEYBOARD_SHORTCUTS_DEBUG
dbg() << " > Asked application, got action: " << action;
#endif