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 | |
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.
26 files changed, 51 insertions, 49 deletions
diff --git a/Applications/Browser/Tab.cpp b/Applications/Browser/Tab.cpp index aed2949e66..7c56f4ad7d 100644 --- a/Applications/Browser/Tab.cpp +++ b/Applications/Browser/Tab.cpp @@ -254,7 +254,7 @@ Tab::Tab() app_menu.add_action(*m_reload_action); app_menu.add_separator(); app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) { - GUI::Application::the().quit(); + GUI::Application::the()->quit(); })); auto& view_menu = m_menubar->add_menu("View"); @@ -429,7 +429,7 @@ void Tab::did_become_active() m_toolbar_container->set_visible(!is_fullscreen); m_statusbar->set_visible(!is_fullscreen); - GUI::Application::the().set_menubar(m_menubar); + GUI::Application::the()->set_menubar(m_menubar); } void Tab::context_menu_requested(const Gfx::IntPoint& screen_position) diff --git a/Applications/Calculator/main.cpp b/Applications/Calculator/main.cpp index f3fbabbb10..e61c498026 100644 --- a/Applications/Calculator/main.cpp +++ b/Applications/Calculator/main.cpp @@ -69,7 +69,7 @@ int main(int argc, char** argv) auto& app_menu = menubar->add_menu("Calculator"); app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) { - GUI::Application::the().quit(0); + GUI::Application::the()->quit(); return; })); diff --git a/Applications/Calendar/main.cpp b/Applications/Calendar/main.cpp index f794584968..3e21c591df 100644 --- a/Applications/Calendar/main.cpp +++ b/Applications/Calendar/main.cpp @@ -74,7 +74,7 @@ int main(int argc, char** argv) })); app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) { - GUI::Application::the().quit(0); + GUI::Application::the()->quit(); return; })); diff --git a/Applications/DisplaySettings/DisplaySettings.cpp b/Applications/DisplaySettings/DisplaySettings.cpp index 459e7fa9cf..a3eebeb458 100644 --- a/Applications/DisplaySettings/DisplaySettings.cpp +++ b/Applications/DisplaySettings/DisplaySettings.cpp @@ -247,7 +247,7 @@ void DisplaySettingsWidget::create_frame() ok_button.set_preferred_size(60, 22); ok_button.on_click = [this](auto) { send_settings_to_window_server(); - GUI::Application::the().quit(); + GUI::Application::the()->quit(); }; auto& cancel_button = bottom_widget.add<GUI::Button>(); @@ -255,7 +255,7 @@ void DisplaySettingsWidget::create_frame() cancel_button.set_size_policy(Orientation::Horizontal, GUI::SizePolicy::Fixed); cancel_button.set_preferred_size(60, 22); cancel_button.on_click = [](auto) { - GUI::Application::the().quit(); + GUI::Application::the()->quit(); }; auto& apply_button = bottom_widget.add<GUI::Button>(); diff --git a/Applications/FileManager/main.cpp b/Applications/FileManager/main.cpp index 727d31403b..b227bf51d5 100644 --- a/Applications/FileManager/main.cpp +++ b/Applications/FileManager/main.cpp @@ -225,7 +225,7 @@ int run_in_desktop_mode(RefPtr<Core::ConfigFile> config, String initial_location }; window->show(); - return GUI::Application::the().exec(); + return GUI::Application::the()->exec(); } int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_location) @@ -627,7 +627,7 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio app_menu.add_action(properties_action); app_menu.add_separator(); app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) { - GUI::Application::the().quit(0); + GUI::Application::the()->quit(); })); auto& view_menu = menubar->add_menu("View"); @@ -646,7 +646,7 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio GUI::AboutDialog::show("File Manager", Gfx::Bitmap::load_from_file("/res/icons/32x32/filetype-folder.png"), window); })); - GUI::Application::the().set_menubar(move(menubar)); + GUI::Application::the()->set_menubar(move(menubar)); main_toolbar.add_action(go_back_action); main_toolbar.add_action(go_forward_action); @@ -856,5 +856,5 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio return GUI::Window::CloseRequestDecision::Close; }; - return GUI::Application::the().exec(); + return GUI::Application::the()->exec(); } diff --git a/Applications/FontEditor/main.cpp b/Applications/FontEditor/main.cpp index c5333e28c1..4601b7ba32 100644 --- a/Applications/FontEditor/main.cpp +++ b/Applications/FontEditor/main.cpp @@ -86,8 +86,8 @@ int main(int argc, char** argv) auto menubar = GUI::MenuBar::construct(); auto& app_menu = menubar->add_menu("Font Editor"); - app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) { - GUI::Application::the().quit(0); + app_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) { + app->quit(); return; })); diff --git a/Applications/Help/main.cpp b/Applications/Help/main.cpp index 87824d0947..e1449a9579 100644 --- a/Applications/Help/main.cpp +++ b/Applications/Help/main.cpp @@ -194,7 +194,7 @@ int main(int argc, char* argv[]) })); app_menu.add_separator(); app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) { - GUI::Application::the().quit(0); + GUI::Application::the()->quit(); })); auto& go_menu = menubar->add_menu("Go"); diff --git a/Applications/HexEditor/HexEditorWidget.cpp b/Applications/HexEditor/HexEditorWidget.cpp index f5c920af9e..eabb7cea67 100644 --- a/Applications/HexEditor/HexEditorWidget.cpp +++ b/Applications/HexEditor/HexEditorWidget.cpp @@ -142,7 +142,7 @@ HexEditorWidget::HexEditorWidget() app_menu.add_action(GUI::CommonActions::make_quit_action([this](auto&) { if (!request_close()) return; - GUI::Application::the().quit(0); + GUI::Application::the()->quit(); })); m_goto_decimal_offset_action = GUI::Action::create("Go To Offset (Decimal)...", { Mod_Ctrl | Mod_Shift, Key_G }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png"), [this](const GUI::Action&) { @@ -199,7 +199,7 @@ HexEditorWidget::HexEditorWidget() GUI::AboutDialog::show("Hex Editor", Gfx::Bitmap::load_from_file("/res/icons/32x32/app-hexeditor.png"), window()); })); - GUI::Application::the().set_menubar(move(menubar)); + GUI::Application::the()->set_menubar(move(menubar)); m_editor->set_focus(true); } diff --git a/Applications/IRCClient/IRCAppWindow.cpp b/Applications/IRCClient/IRCAppWindow.cpp index 0fbb1fe213..8a01528b6d 100644 --- a/Applications/IRCClient/IRCAppWindow.cpp +++ b/Applications/IRCClient/IRCAppWindow.cpp @@ -263,7 +263,7 @@ void IRCAppWindow::setup_menus() auto& app_menu = menubar->add_menu("IRC Client"); app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) { dbgprintf("Terminal: Quit menu activated!\n"); - GUI::Application::the().quit(0); + GUI::Application::the()->quit(); return; })); @@ -301,7 +301,7 @@ void IRCAppWindow::setup_menus() GUI::AboutDialog::show("IRC Client", Gfx::Bitmap::load_from_file("/res/icons/32x32/app-irc-client.png"), this); })); - GUI::Application::the().set_menubar(move(menubar)); + GUI::Application::the()->set_menubar(move(menubar)); } void IRCAppWindow::setup_widgets() diff --git a/Applications/Piano/main.cpp b/Applications/Piano/main.cpp index a089508bd2..aec6966a81 100644 --- a/Applications/Piano/main.cpp +++ b/Applications/Piano/main.cpp @@ -96,7 +96,7 @@ int main(int argc, char** argv) auto& app_menu = menubar->add_menu("Piano"); app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) { - GUI::Application::the().quit(0); + GUI::Application::the()->quit(); return; })); app_menu.add_action(GUI::Action::create("Export", { Mod_Ctrl, Key_E }, [&](const GUI::Action&) { diff --git a/Applications/PixelPaint/main.cpp b/Applications/PixelPaint/main.cpp index 67602c2097..b51ea389c7 100644 --- a/Applications/PixelPaint/main.cpp +++ b/Applications/PixelPaint/main.cpp @@ -113,7 +113,7 @@ int main(int argc, char** argv) })); app_menu.add_separator(); app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) { - GUI::Application::the().quit(0); + GUI::Application::the()->quit(); return; })); diff --git a/Applications/SystemMonitor/main.cpp b/Applications/SystemMonitor/main.cpp index 7d280db561..6846a22063 100644 --- a/Applications/SystemMonitor/main.cpp +++ b/Applications/SystemMonitor/main.cpp @@ -223,7 +223,7 @@ int main(int argc, char** argv) auto menubar = GUI::MenuBar::construct(); auto& app_menu = menubar->add_menu("System Monitor"); app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) { - GUI::Application::the().quit(0); + GUI::Application::the()->quit(); return; })); diff --git a/Applications/Terminal/main.cpp b/Applications/Terminal/main.cpp index eb0dc1f3f0..7e87ec4308 100644 --- a/Applications/Terminal/main.cpp +++ b/Applications/Terminal/main.cpp @@ -291,7 +291,7 @@ int main(int argc, char** argv) app_menu.add_separator(); app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) { dbgprintf("Terminal: Quit menu activated!\n"); - GUI::Application::the().quit(0); + GUI::Application::the()->quit(); })); auto& edit_menu = menubar->add_menu("Edit"); diff --git a/Applications/TextEditor/TextEditorWidget.cpp b/Applications/TextEditor/TextEditorWidget.cpp index 39b339de3c..307e280d6e 100644 --- a/Applications/TextEditor/TextEditorWidget.cpp +++ b/Applications/TextEditor/TextEditorWidget.cpp @@ -368,7 +368,7 @@ TextEditorWidget::TextEditorWidget() app_menu.add_action(GUI::CommonActions::make_quit_action([this](auto&) { if (!request_close()) return; - GUI::Application::the().quit(0); + GUI::Application::the()->quit(); })); auto& edit_menu = menubar->add_menu("Edit"); @@ -455,7 +455,7 @@ TextEditorWidget::TextEditorWidget() GUI::AboutDialog::show("Text Editor", Gfx::Bitmap::load_from_file("/res/icons/32x32/app-texteditor.png"), window()); })); - GUI::Application::the().set_menubar(move(menubar)); + GUI::Application::the()->set_menubar(move(menubar)); toolbar.add_action(*m_new_action); toolbar.add_action(*m_open_action); diff --git a/DevTools/Profiler/main.cpp b/DevTools/Profiler/main.cpp index 20fbe4fb5c..2b0be42a15 100644 --- a/DevTools/Profiler/main.cpp +++ b/DevTools/Profiler/main.cpp @@ -144,11 +144,11 @@ bool prompt_to_stop_profiling() auto& stop_button = widget.add<GUI::Button>("Stop"); stop_button.on_click = [&](auto) { - GUI::Application::the().quit(); + GUI::Application::the()->quit(); }; window->show(); - return GUI::Application::the().exec() == 0; + return GUI::Application::the()->exec() == 0; } bool generate_profile(pid_t pid) diff --git a/DevTools/VisualBuilder/main.cpp b/DevTools/VisualBuilder/main.cpp index 460217610d..14e82c2a16 100644 --- a/DevTools/VisualBuilder/main.cpp +++ b/DevTools/VisualBuilder/main.cpp @@ -59,7 +59,7 @@ int main(int argc, char** argv) auto menubar = GUI::MenuBar::construct(); auto& app_menu = menubar->add_menu("Visual Builder"); app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) { - GUI::Application::the().quit(0); + GUI::Application::the()->quit(); return; })); diff --git a/Games/Minesweeper/main.cpp b/Games/Minesweeper/main.cpp index 4f7ce5468f..8556815d89 100644 --- a/Games/Minesweeper/main.cpp +++ b/Games/Minesweeper/main.cpp @@ -104,7 +104,7 @@ int main(int argc, char** argv) app_menu.add_separator(); app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) { - GUI::Application::the().quit(0); + GUI::Application::the()->quit(); return; })); diff --git a/Games/Snake/main.cpp b/Games/Snake/main.cpp index ad24a3449e..6abc482e66 100644 --- a/Games/Snake/main.cpp +++ b/Games/Snake/main.cpp @@ -65,7 +65,7 @@ int main(int argc, char** argv) game.reset(); })); app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) { - GUI::Application::the().quit(0); + GUI::Application::the()->quit(); })); auto& help_menu = menubar->add_menu("Help"); 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) |