diff options
author | implicitfield <114500360+implicitfield@users.noreply.github.com> | 2023-02-11 18:29:58 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-03-21 10:06:41 +0100 |
commit | 9c7dfd4fd4df211127f846fc3e5dcccacfa527d2 (patch) | |
tree | 218e7f165636a606f60cbfcf5cf8e524ee73f042 /Userland | |
parent | 83eca15e519d94243057a7de9decff8397dabbdf (diff) | |
download | serenity-9c7dfd4fd4df211127f846fc3e5dcccacfa527d2.zip |
DisplaySettings: Propagate errors in ThemesSettingsWidget
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Applications/DisplaySettings/ThemesSettingsWidget.cpp | 58 | ||||
-rw-r--r-- | Userland/Applications/DisplaySettings/ThemesSettingsWidget.h | 4 |
2 files changed, 44 insertions, 18 deletions
diff --git a/Userland/Applications/DisplaySettings/ThemesSettingsWidget.cpp b/Userland/Applications/DisplaySettings/ThemesSettingsWidget.cpp index 24f46e47ac..4ca86602b7 100644 --- a/Userland/Applications/DisplaySettings/ThemesSettingsWidget.cpp +++ b/Userland/Applications/DisplaySettings/ThemesSettingsWidget.cpp @@ -24,15 +24,20 @@ static DeprecatedString get_color_scheme_name_from_pathname(DeprecatedString con return color_scheme_path.replace("/res/color-schemes/"sv, ""sv, ReplaceMode::FirstOnly).replace(".ini"sv, ""sv, ReplaceMode::FirstOnly); }; -ThemesSettingsWidget::ThemesSettingsWidget(bool& background_settings_changed) - : m_background_settings_changed { background_settings_changed } -{ - load_from_gml(themes_settings_gml).release_value_but_fixme_should_propagate_errors(); - m_themes = MUST(Gfx::list_installed_system_themes()); +ErrorOr<NonnullRefPtr<ThemesSettingsWidget>> ThemesSettingsWidget::try_create(bool& background_settings_changed) { + auto theme_settings_widget = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) ThemesSettingsWidget(background_settings_changed))); + TRY(theme_settings_widget->load_from_gml(themes_settings_gml)); + TRY(theme_settings_widget->setup_interface()); + + return theme_settings_widget; +} + +ErrorOr<void> ThemesSettingsWidget::setup_interface() { + m_themes = TRY(Gfx::list_installed_system_themes()); size_t current_theme_index; auto current_theme_name = GUI::ConnectionToWindowServer::the().get_system_theme(); - m_theme_names.ensure_capacity(m_themes.size()); + TRY(m_theme_names.try_ensure_capacity(m_themes.size())); for (auto& theme_meta : m_themes) { m_theme_names.append(theme_meta.name); if (current_theme_name == theme_meta.name) { @@ -40,6 +45,7 @@ ThemesSettingsWidget::ThemesSettingsWidget(bool& background_settings_changed) current_theme_index = m_theme_names.size() - 1; } } + m_theme_preview = find_descendant_of_type_named<GUI::Frame>("preview_frame")->add<ThemePreviewWidget>(palette()); m_themes_combo = *find_descendant_of_type_named<GUI::ComboBox>("themes_combo"); m_themes_combo->set_only_allow_values_from_model(true); @@ -54,7 +60,7 @@ ThemesSettingsWidget::ThemesSettingsWidget(bool& background_settings_changed) }; m_themes_combo->set_selected_index(current_theme_index, GUI::AllowCallback::No); - auto mouse_settings_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/app-mouse.png"sv).release_value_but_fixme_should_propagate_errors(); + auto mouse_settings_icon = TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-mouse.png"sv)); m_color_scheme_names.clear(); Core::DirIterator iterator("/res/color-schemes", Core::DirIterator::SkipParentAndBaseDir); @@ -125,26 +131,44 @@ ThemesSettingsWidget::ThemesSettingsWidget(bool& background_settings_changed) ++index; } }; + + return {}; +} + +ThemesSettingsWidget::ThemesSettingsWidget(bool& background_settings_changed) + : m_background_settings_changed {background_settings_changed } +{ } void ThemesSettingsWidget::apply_settings() { + m_background_settings_changed = false; Optional<DeprecatedString> color_scheme_path = DeprecatedString::formatted("/res/color-schemes/{}.ini", m_selected_color_scheme_name); - if (!m_color_scheme_is_file_based && find_descendant_of_type_named<GUI::CheckBox>("custom_color_scheme_checkbox")->is_checked()) - VERIFY(GUI::ConnectionToWindowServer::the().set_system_theme(m_selected_theme->path, m_selected_theme->name, m_background_settings_changed, "Custom"sv)); - else if (find_descendant_of_type_named<GUI::CheckBox>("custom_color_scheme_checkbox")->is_checked()) - VERIFY(GUI::ConnectionToWindowServer::the().set_system_theme(m_selected_theme->path, m_selected_theme->name, m_background_settings_changed, color_scheme_path)); - else { - VERIFY(GUI::ConnectionToWindowServer::the().set_system_theme(m_selected_theme->path, m_selected_theme->name, m_background_settings_changed, OptionalNone())); + if (!m_color_scheme_is_file_based && find_descendant_of_type_named<GUI::CheckBox>("custom_color_scheme_checkbox")->is_checked()) { + auto set_theme_result = GUI::ConnectionToWindowServer::the().set_system_theme(m_selected_theme->path, m_selected_theme->name, m_background_settings_changed, "Custom"sv); + if(!set_theme_result) + GUI::MessageBox::show_error(window(), "Failed to apply theme settings"sv); + } else if (find_descendant_of_type_named<GUI::CheckBox>("custom_color_scheme_checkbox")->is_checked()) { + auto set_theme_result = GUI::ConnectionToWindowServer::the().set_system_theme(m_selected_theme->path, m_selected_theme->name, m_background_settings_changed, color_scheme_path); + if(!set_theme_result) + GUI::MessageBox::show_error(window(), "Failed to apply theme settings"sv); + } else { + auto set_theme_result = GUI::ConnectionToWindowServer::the().set_system_theme(m_selected_theme->path, m_selected_theme->name, m_background_settings_changed, OptionalNone()); + if (!set_theme_result) { + GUI::MessageBox::show_error(window(), "Failed to apply theme settings"sv); + return; + } // Update the color scheme combobox to match the new theme. - auto const theme_config = Core::ConfigFile::open(m_selected_theme->path).release_value_but_fixme_should_propagate_errors(); - auto const color_scheme_index = m_color_scheme_names.find_first_index(get_color_scheme_name_from_pathname(theme_config->read_entry("Paths", "ColorScheme"))); + auto theme_config = Core::ConfigFile::open(m_selected_theme->path); + if (theme_config.is_error()) { + GUI::MessageBox::show_error(window(), "Failed to open theme config file"sv); + return; + } + auto const color_scheme_index = m_color_scheme_names.find_first_index(get_color_scheme_name_from_pathname(theme_config.release_value()->read_entry("Paths", "ColorScheme"))); if (color_scheme_index.has_value()) find_descendant_of_type_named<GUI::ComboBox>("color_scheme_combo")->set_selected_index(color_scheme_index.value()); } - - m_background_settings_changed = false; } } diff --git a/Userland/Applications/DisplaySettings/ThemesSettingsWidget.h b/Userland/Applications/DisplaySettings/ThemesSettingsWidget.h index b0778537db..8617aff73e 100644 --- a/Userland/Applications/DisplaySettings/ThemesSettingsWidget.h +++ b/Userland/Applications/DisplaySettings/ThemesSettingsWidget.h @@ -17,12 +17,14 @@ namespace DisplaySettings { class ThemesSettingsWidget final : public GUI::SettingsWindow::Tab { - C_OBJECT(ThemesSettingsWidget); + C_OBJECT_ABSTRACT(ThemesSettingsWidget); public: + static ErrorOr<NonnullRefPtr<ThemesSettingsWidget>> try_create(bool& background_settings_changed); virtual void apply_settings() override; private: + ErrorOr<void> setup_interface(); Vector<Gfx::SystemThemeMetaData> m_themes; Vector<DeprecatedString> m_theme_names; Vector<DeprecatedString> m_color_scheme_names; |