mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 21:42:43 +00:00 
			
		
		
		
	Ladybird: Convert Browser::Settings to a singleton
				
					
				
			This commit is contained in:
		
							parent
							
								
									68dae8ab46
								
							
						
					
					
						commit
						6e40e8316e
					
				
					 6 changed files with 23 additions and 20 deletions
				
			
		|  | @ -27,7 +27,6 @@ | ||||||
| #include <QTabBar> | #include <QTabBar> | ||||||
| 
 | 
 | ||||||
| namespace Ladybird { | namespace Ladybird { | ||||||
| extern Settings* s_settings; |  | ||||||
| 
 | 
 | ||||||
| static QIcon const& app_icon() | static QIcon const& app_icon() | ||||||
| { | { | ||||||
|  | @ -356,7 +355,7 @@ BrowserWindow::BrowserWindow(Optional<URL> const& initial_url, Browser::CookieJa | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     QObject::connect(new_tab_action, &QAction::triggered, this, [this] { |     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(open_file_action, &QAction::triggered, this, &BrowserWindow::open_file); | ||||||
|     QObject::connect(settings_action, &QAction::triggered, this, [this] { |     QObject::connect(settings_action, &QAction::triggered, this, [this] { | ||||||
|  | @ -401,7 +400,7 @@ BrowserWindow::BrowserWindow(Optional<URL> const& initial_url, Browser::CookieJa | ||||||
|         auto initial_url_string = qstring_from_ak_deprecated_string(initial_url->serialize()); |         auto initial_url_string = qstring_from_ak_deprecated_string(initial_url->serialize()); | ||||||
|         new_tab(initial_url_string, Web::HTML::ActivateTab::Yes); |         new_tab(initial_url_string, Web::HTML::ActivateTab::Yes); | ||||||
|     } else { |     } 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); |     setCentralWidget(m_tabs_container); | ||||||
|  | @ -696,9 +695,9 @@ bool BrowserWindow::eventFilter(QObject* obj, QEvent* event) | ||||||
| 
 | 
 | ||||||
| void BrowserWindow::closeEvent(QCloseEvent* event) | void BrowserWindow::closeEvent(QCloseEvent* event) | ||||||
| { | { | ||||||
|     s_settings->set_last_position(pos()); |     Settings::the()->set_last_position(pos()); | ||||||
|     s_settings->set_last_size(size()); |     Settings::the()->set_last_size(size()); | ||||||
|     s_settings->set_is_maximized(isMaximized()); |     Settings::the()->set_is_maximized(isMaximized()); | ||||||
| 
 | 
 | ||||||
|     QMainWindow::closeEvent(event); |     QMainWindow::closeEvent(event); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -30,7 +30,7 @@ static QString rebase_default_url_on_serenity_resource_root(StringView default_u | ||||||
| 
 | 
 | ||||||
| Settings::Settings() | Settings::Settings() | ||||||
| { | { | ||||||
|     m_qsettings = new QSettings("Serenity", "Ladybird", this); |     m_qsettings = make<QSettings>("Serenity", "Ladybird", this); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| Optional<QPoint> Settings::last_position() | Optional<QPoint> Settings::last_position() | ||||||
|  |  | ||||||
|  | @ -7,6 +7,7 @@ | ||||||
| #pragma once | #pragma once | ||||||
| 
 | 
 | ||||||
| #include <AK/DeprecatedString.h> | #include <AK/DeprecatedString.h> | ||||||
|  | #include <AK/OwnPtr.h> | ||||||
| #include <QPoint> | #include <QPoint> | ||||||
| #include <QSettings> | #include <QSettings> | ||||||
| #include <QSize> | #include <QSize> | ||||||
|  | @ -15,7 +16,14 @@ namespace Ladybird { | ||||||
| 
 | 
 | ||||||
| class Settings : public QObject { | class Settings : public QObject { | ||||||
| public: | public: | ||||||
|     Settings(); |     Settings(Settings const&) = delete; | ||||||
|  |     Settings& operator=(Settings const&) = delete; | ||||||
|  | 
 | ||||||
|  |     static Settings* the() | ||||||
|  |     { | ||||||
|  |         static Settings instance; | ||||||
|  |         return &instance; | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
|     Optional<QPoint> last_position(); |     Optional<QPoint> last_position(); | ||||||
|     void set_last_position(QPoint const& last_position); |     void set_last_position(QPoint const& last_position); | ||||||
|  | @ -29,8 +37,11 @@ public: | ||||||
|     QString new_tab_page(); |     QString new_tab_page(); | ||||||
|     void set_new_tab_page(QString const& page); |     void set_new_tab_page(QString const& page); | ||||||
| 
 | 
 | ||||||
|  | protected: | ||||||
|  |     Settings(); | ||||||
|  | 
 | ||||||
| private: | private: | ||||||
|     QSettings* m_qsettings; |     OwnPtr<QSettings> m_qsettings; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -13,8 +13,6 @@ | ||||||
| 
 | 
 | ||||||
| namespace Ladybird { | namespace Ladybird { | ||||||
| 
 | 
 | ||||||
| extern Settings* s_settings; |  | ||||||
| 
 |  | ||||||
| SettingsDialog::SettingsDialog(QMainWindow* window) | SettingsDialog::SettingsDialog(QMainWindow* window) | ||||||
|     : m_window(window) |     : m_window(window) | ||||||
| { | { | ||||||
|  | @ -48,7 +46,7 @@ void SettingsDialog::save() | ||||||
|     auto url_string = MUST(ak_string_from_qstring(m_new_tab_page->text())); |     auto url_string = MUST(ak_string_from_qstring(m_new_tab_page->text())); | ||||||
|     if (!URL(url_string).is_valid()) |     if (!URL(url_string).is_valid()) | ||||||
|         return; |         return; | ||||||
|     s_settings->set_new_tab_page(m_new_tab_page->text()); |     Settings::the()->set_new_tab_page(m_new_tab_page->text()); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -33,8 +33,6 @@ extern DeprecatedString s_serenity_resource_root; | ||||||
| 
 | 
 | ||||||
| namespace Ladybird { | namespace Ladybird { | ||||||
| 
 | 
 | ||||||
| extern Settings* s_settings; |  | ||||||
| 
 |  | ||||||
| static QIcon create_tvg_icon_with_theme_colors(QString name, QPalette const& palette) | static QIcon create_tvg_icon_with_theme_colors(QString name, QPalette const& palette) | ||||||
| { | { | ||||||
|     auto path = QString(":/Icons/%1.tvg").arg(name); |     auto path = QString(":/Icons/%1.tvg").arg(name); | ||||||
|  |  | ||||||
|  | @ -25,8 +25,6 @@ | ||||||
| 
 | 
 | ||||||
| namespace Ladybird { | namespace Ladybird { | ||||||
| 
 | 
 | ||||||
| OwnPtr<Ladybird::Settings> s_settings; |  | ||||||
| 
 |  | ||||||
| bool is_using_dark_system_theme(QWidget& widget) | 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
 |     // 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<int> serenity_main(Main::Arguments arguments) | ||||||
|     if (auto url = TRY(get_formatted_url(raw_url)); url.is_valid()) |     if (auto url = TRY(get_formatted_url(raw_url)); url.is_valid()) | ||||||
|         initial_url = move(url); |         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); |     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"); |     window.setWindowTitle("Ladybird"); | ||||||
| 
 | 
 | ||||||
|     if (Ladybird::s_settings->is_maximized()) { |     if (Ladybird::Settings::the()->is_maximized()) { | ||||||
|         window.showMaximized(); |         window.showMaximized(); | ||||||
|     } else { |     } else { | ||||||
|         auto last_position = Ladybird::s_settings->last_position(); |         auto last_position = Ladybird::Settings::the()->last_position(); | ||||||
|         if (last_position.has_value()) |         if (last_position.has_value()) | ||||||
|             window.move(last_position.value()); |             window.move(last_position.value()); | ||||||
|         window.resize(Ladybird::s_settings->last_size()); |         window.resize(Ladybird::Settings::the()->last_size()); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     window.show(); |     window.show(); | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Cameron Youell
						Cameron Youell