diff options
author | Andreas Kling <kling@serenityos.org> | 2020-07-04 16:52:01 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-07-04 16:54:55 +0200 |
commit | ca93c22ae2706c35828f913976f35e828157bb01 (patch) | |
tree | 6daa1e52973d55636634ceb69fc0fe15b6bd50f4 /Libraries | |
parent | f7577585a6794228659e5e7b496cddfd8a94b54d (diff) | |
download | serenity-ca93c22ae2706c35828f913976f35e828157bb01.zip |
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.
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibGUI/Action.cpp | 9 | ||||
-rw-r--r-- | Libraries/LibGUI/Application.cpp | 11 | ||||
-rw-r--r-- | Libraries/LibGUI/Application.h | 2 | ||||
-rw-r--r-- | Libraries/LibGUI/Widget.cpp | 8 | ||||
-rw-r--r-- | Libraries/LibGUI/Window.cpp | 4 | ||||
-rw-r--r-- | Libraries/LibGUI/WindowServerConnection.cpp | 4 | ||||
-rw-r--r-- | Libraries/LibVT/TerminalWidget.cpp | 2 | ||||
-rw-r--r-- | Libraries/LibWeb/PageView.cpp | 4 |
8 files changed, 23 insertions, 21 deletions
diff --git a/Libraries/LibGUI/Action.cpp b/Libraries/LibGUI/Action.cpp index 56937ebbba..701e3c9c33 100644 --- a/Libraries/LibGUI/Action.cpp +++ b/Libraries/LibGUI/Action.cpp @@ -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) diff --git a/Libraries/LibGUI/Application.cpp b/Libraries/LibGUI/Application.cpp index 127f8471ee..a92559e38d 100644 --- a/Libraries/LibGUI/Application.cpp +++ b/Libraries/LibGUI/Application.cpp @@ -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() diff --git a/Libraries/LibGUI/Application.h b/Libraries/LibGUI/Application.h index e669ea4a79..03960f2b30 100644 --- a/Libraries/LibGUI/Application.h +++ b/Libraries/LibGUI/Application.h @@ -40,7 +40,7 @@ class Application : public Core::Object { C_OBJECT(Application); public: - static Application& the(); + static Application* the(); ~Application(); diff --git a/Libraries/LibGUI/Widget.cpp b/Libraries/LibGUI/Widget.cpp index 709b6f1e18..93fc35e038 100644 --- a/Libraries/LibGUI/Widget.cpp +++ b/Libraries/LibGUI/Widget.cpp @@ -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); } diff --git a/Libraries/LibGUI/Window.cpp b/Libraries/LibGUI/Window.cpp index 46e450050d..a3d910053d 100644 --- a/Libraries/LibGUI/Window.cpp +++ b/Libraries/LibGUI/Window.cpp @@ -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) diff --git a/Libraries/LibGUI/WindowServerConnection.cpp b/Libraries/LibGUI/WindowServerConnection.cpp index 1fd844f2ce..5301186680 100644 --- a/Libraries/LibGUI/WindowServerConnection.cpp +++ b/Libraries/LibGUI/WindowServerConnection.cpp @@ -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 diff --git a/Libraries/LibVT/TerminalWidget.cpp b/Libraries/LibVT/TerminalWidget.cpp index c191c12461..d99870e239 100644 --- a/Libraries/LibVT/TerminalWidget.cpp +++ b/Libraries/LibVT/TerminalWidget.cpp @@ -70,7 +70,7 @@ void TerminalWidget::set_pty_master_fd(int fd) if (nread < 0) { dbgprintf("Terminal read error: %s\n", strerror(errno)); perror("read(ptm)"); - GUI::Application::the().quit(1); + GUI::Application::the()->quit(1); return; } if (nread == 0) { diff --git a/Libraries/LibWeb/PageView.cpp b/Libraries/LibWeb/PageView.cpp index 84c24bc2b1..4ae7dc2abf 100644 --- a/Libraries/LibWeb/PageView.cpp +++ b/Libraries/LibWeb/PageView.cpp @@ -221,12 +221,12 @@ void PageView::page_did_middle_click_link(const String& href, [[maybe_unused]] c void PageView::page_did_enter_tooltip_area(const Gfx::IntPoint& content_position, const String& title) { - GUI::Application::the().show_tooltip(title, screen_relative_rect().location().translated(to_widget_position(content_position))); + GUI::Application::the()->show_tooltip(title, screen_relative_rect().location().translated(to_widget_position(content_position))); } void PageView::page_did_leave_tooltip_area() { - GUI::Application::the().hide_tooltip(); + GUI::Application::the()->hide_tooltip(); } void PageView::page_did_hover_link(const URL& url) |