From 6e40e8316e5bb585fee24b6aebb95e8dd5467a26 Mon Sep 17 00:00:00 2001 From: Cameron Youell Date: Mon, 29 May 2023 19:05:30 +1000 Subject: [PATCH] Ladybird: Convert `Browser::Settings` to a singleton --- Ladybird/Qt/BrowserWindow.cpp | 11 +++++------ Ladybird/Qt/Settings.cpp | 2 +- Ladybird/Qt/Settings.h | 15 +++++++++++++-- Ladybird/Qt/SettingsDialog.cpp | 4 +--- Ladybird/Qt/Tab.cpp | 2 -- Ladybird/Qt/main.cpp | 9 +++------ 6 files changed, 23 insertions(+), 20 deletions(-) diff --git a/Ladybird/Qt/BrowserWindow.cpp b/Ladybird/Qt/BrowserWindow.cpp index 2422f8186cd..0b275d4be90 100644 --- a/Ladybird/Qt/BrowserWindow.cpp +++ b/Ladybird/Qt/BrowserWindow.cpp @@ -27,7 +27,6 @@ #include namespace Ladybird { -extern Settings* s_settings; static QIcon const& app_icon() { @@ -356,7 +355,7 @@ BrowserWindow::BrowserWindow(Optional const& initial_url, Browser::CookieJa }); QObject::connect(new_tab_action, &QAction::triggered, this, [this] { - new_tab(s_settings->new_tab_page(), Web::HTML::ActivateTab::Yes); + new_tab(Settings::the()->new_tab_page(), Web::HTML::ActivateTab::Yes); }); QObject::connect(open_file_action, &QAction::triggered, this, &BrowserWindow::open_file); QObject::connect(settings_action, &QAction::triggered, this, [this] { @@ -401,7 +400,7 @@ BrowserWindow::BrowserWindow(Optional const& initial_url, Browser::CookieJa auto initial_url_string = qstring_from_ak_deprecated_string(initial_url->serialize()); new_tab(initial_url_string, Web::HTML::ActivateTab::Yes); } else { - new_tab(s_settings->new_tab_page(), Web::HTML::ActivateTab::Yes); + new_tab(Settings::the()->new_tab_page(), Web::HTML::ActivateTab::Yes); } setCentralWidget(m_tabs_container); @@ -696,9 +695,9 @@ bool BrowserWindow::eventFilter(QObject* obj, QEvent* event) void BrowserWindow::closeEvent(QCloseEvent* event) { - s_settings->set_last_position(pos()); - s_settings->set_last_size(size()); - s_settings->set_is_maximized(isMaximized()); + Settings::the()->set_last_position(pos()); + Settings::the()->set_last_size(size()); + Settings::the()->set_is_maximized(isMaximized()); QMainWindow::closeEvent(event); } diff --git a/Ladybird/Qt/Settings.cpp b/Ladybird/Qt/Settings.cpp index 016060f996d..ec6844f2246 100644 --- a/Ladybird/Qt/Settings.cpp +++ b/Ladybird/Qt/Settings.cpp @@ -30,7 +30,7 @@ static QString rebase_default_url_on_serenity_resource_root(StringView default_u Settings::Settings() { - m_qsettings = new QSettings("Serenity", "Ladybird", this); + m_qsettings = make("Serenity", "Ladybird", this); } Optional Settings::last_position() diff --git a/Ladybird/Qt/Settings.h b/Ladybird/Qt/Settings.h index 45d126bce4c..e26e3e1f8e9 100644 --- a/Ladybird/Qt/Settings.h +++ b/Ladybird/Qt/Settings.h @@ -7,6 +7,7 @@ #pragma once #include +#include #include #include #include @@ -15,7 +16,14 @@ namespace Ladybird { class Settings : public QObject { public: - Settings(); + Settings(Settings const&) = delete; + Settings& operator=(Settings const&) = delete; + + static Settings* the() + { + static Settings instance; + return &instance; + } Optional last_position(); void set_last_position(QPoint const& last_position); @@ -29,8 +37,11 @@ public: QString new_tab_page(); void set_new_tab_page(QString const& page); +protected: + Settings(); + private: - QSettings* m_qsettings; + OwnPtr m_qsettings; }; } diff --git a/Ladybird/Qt/SettingsDialog.cpp b/Ladybird/Qt/SettingsDialog.cpp index 2890865fae0..9c166c6f2d6 100644 --- a/Ladybird/Qt/SettingsDialog.cpp +++ b/Ladybird/Qt/SettingsDialog.cpp @@ -13,8 +13,6 @@ namespace Ladybird { -extern Settings* s_settings; - SettingsDialog::SettingsDialog(QMainWindow* window) : m_window(window) { @@ -48,7 +46,7 @@ void SettingsDialog::save() auto url_string = MUST(ak_string_from_qstring(m_new_tab_page->text())); if (!URL(url_string).is_valid()) return; - s_settings->set_new_tab_page(m_new_tab_page->text()); + Settings::the()->set_new_tab_page(m_new_tab_page->text()); } } diff --git a/Ladybird/Qt/Tab.cpp b/Ladybird/Qt/Tab.cpp index 67dd6370700..115fffdb28c 100644 --- a/Ladybird/Qt/Tab.cpp +++ b/Ladybird/Qt/Tab.cpp @@ -33,8 +33,6 @@ extern DeprecatedString s_serenity_resource_root; namespace Ladybird { -extern Settings* s_settings; - static QIcon create_tvg_icon_with_theme_colors(QString name, QPalette const& palette) { auto path = QString(":/Icons/%1.tvg").arg(name); diff --git a/Ladybird/Qt/main.cpp b/Ladybird/Qt/main.cpp index 6f51539c3c4..ddbc23927ce 100644 --- a/Ladybird/Qt/main.cpp +++ b/Ladybird/Qt/main.cpp @@ -25,8 +25,6 @@ namespace Ladybird { -OwnPtr s_settings; - bool is_using_dark_system_theme(QWidget& widget) { // FIXME: Qt does not provide any method to query if the system is using a dark theme. We will have to implement @@ -111,17 +109,16 @@ ErrorOr serenity_main(Main::Arguments arguments) if (auto url = TRY(get_formatted_url(raw_url)); url.is_valid()) initial_url = move(url); - Ladybird::s_settings = adopt_own_if_nonnull(new Ladybird::Settings()); Ladybird::BrowserWindow window(initial_url, cookie_jar, webdriver_content_ipc_path, enable_callgrind_profiling ? WebView::EnableCallgrindProfiling::Yes : WebView::EnableCallgrindProfiling::No, use_lagom_networking ? Ladybird::UseLagomNetworking::Yes : Ladybird::UseLagomNetworking::No); window.setWindowTitle("Ladybird"); - if (Ladybird::s_settings->is_maximized()) { + if (Ladybird::Settings::the()->is_maximized()) { window.showMaximized(); } else { - auto last_position = Ladybird::s_settings->last_position(); + auto last_position = Ladybird::Settings::the()->last_position(); if (last_position.has_value()) window.move(last_position.value()); - window.resize(Ladybird::s_settings->last_size()); + window.resize(Ladybird::Settings::the()->last_size()); } window.show();