diff options
author | implicitfield <114500360+implicitfield@users.noreply.github.com> | 2023-02-12 15:48:23 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-03-10 22:03:49 +0100 |
commit | e9e4baee777c82db2191af0a016411f46aa4b71c (patch) | |
tree | 36f9abb50b3759c4aab98260b4aa515dfcf076e8 /Userland/Applications/DisplaySettings | |
parent | fba0cee622701638b6853c33464f7c36b64b90e6 (diff) | |
download | serenity-e9e4baee777c82db2191af0a016411f46aa4b71c.zip |
Everywhere: Support overriding the system color scheme
Diffstat (limited to 'Userland/Applications/DisplaySettings')
3 files changed, 83 insertions, 2 deletions
diff --git a/Userland/Applications/DisplaySettings/ThemesSettings.gml b/Userland/Applications/DisplaySettings/ThemesSettings.gml index 56ed183fc4..a701c96ef6 100644 --- a/Userland/Applications/DisplaySettings/ThemesSettings.gml +++ b/Userland/Applications/DisplaySettings/ThemesSettings.gml @@ -39,6 +39,24 @@ } @GUI::GroupBox { + title: "Color Scheme" + preferred_height: "fit" + layout: @GUI::VerticalBoxLayout { + margins: [14, 14, 14] + } + + @GUI::CheckBox { + name: "custom_color_scheme_checkbox" + text: "Use a custom color scheme" + } + + @GUI::ComboBox { + name: "color_scheme_combo" + enabled: "false" + } + } + + @GUI::GroupBox { layout: @GUI::VerticalBoxLayout { margins: [14, 14, 4] } diff --git a/Userland/Applications/DisplaySettings/ThemesSettingsWidget.cpp b/Userland/Applications/DisplaySettings/ThemesSettingsWidget.cpp index 3f66e7f301..24f46e47ac 100644 --- a/Userland/Applications/DisplaySettings/ThemesSettingsWidget.cpp +++ b/Userland/Applications/DisplaySettings/ThemesSettingsWidget.cpp @@ -11,6 +11,7 @@ #include <Applications/DisplaySettings/ThemesSettingsGML.h> #include <LibCore/DirIterator.h> #include <LibGUI/Application.h> +#include <LibGUI/CheckBox.h> #include <LibGUI/ConnectionToWindowServer.h> #include <LibGUI/ItemListModel.h> #include <LibGUI/MessageBox.h> @@ -18,6 +19,11 @@ namespace DisplaySettings { +static DeprecatedString get_color_scheme_name_from_pathname(DeprecatedString const& color_scheme_path) +{ + 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 } { @@ -49,6 +55,47 @@ 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(); + + m_color_scheme_names.clear(); + Core::DirIterator iterator("/res/color-schemes", Core::DirIterator::SkipParentAndBaseDir); + while (iterator.has_next()) { + auto path = iterator.next_path(); + m_color_scheme_names.append(path.replace(".ini"sv, ""sv, ReplaceMode::FirstOnly)); + } + quick_sort(m_color_scheme_names); + auto& color_scheme_combo = *find_descendant_of_type_named<GUI::ComboBox>("color_scheme_combo"); + color_scheme_combo.set_only_allow_values_from_model(true); + color_scheme_combo.set_model(*GUI::ItemListModel<DeprecatedString>::create(m_color_scheme_names)); + m_selected_color_scheme_name = get_color_scheme_name_from_pathname(GUI::Widget::palette().color_scheme_path()); + auto selected_color_scheme_index = m_color_scheme_names.find_first_index(m_selected_color_scheme_name); + if (selected_color_scheme_index.has_value()) + color_scheme_combo.set_selected_index(selected_color_scheme_index.value()); + else { + color_scheme_combo.set_text("Custom"); + m_color_scheme_is_file_based = false; + if (m_color_scheme_names.size() > 1) { + color_scheme_combo.set_enabled(true); + find_descendant_of_type_named<GUI::CheckBox>("custom_color_scheme_checkbox")->set_checked(true); + } + } + color_scheme_combo.on_change = [this](auto&, const GUI::ModelIndex& index) { + m_selected_color_scheme_name = index.data().as_string(); + m_color_scheme_is_file_based = true; + set_modified(true); + }; + + find_descendant_of_type_named<GUI::CheckBox>("custom_color_scheme_checkbox")->on_checked = [this](bool) { + if (m_color_scheme_names.size() <= 1) + return; + + if (find_descendant_of_type_named<GUI::CheckBox>("custom_color_scheme_checkbox")->is_checked()) + find_descendant_of_type_named<GUI::ComboBox>("color_scheme_combo")->set_enabled(true); + else { + find_descendant_of_type_named<GUI::ComboBox>("color_scheme_combo")->set_enabled(false); + set_modified(true); + } + }; + m_cursor_themes_button = *find_descendant_of_type_named<GUI::Button>("cursor_themes_button"); m_cursor_themes_button->set_icon(mouse_settings_icon); m_cursor_themes_button->on_click = [&](auto) { @@ -82,8 +129,21 @@ ThemesSettingsWidget::ThemesSettingsWidget(bool& background_settings_changed) void ThemesSettingsWidget::apply_settings() { - if (m_selected_theme && m_selected_theme->name != GUI::ConnectionToWindowServer::the().get_system_theme()) - VERIFY(GUI::ConnectionToWindowServer::the().set_system_theme(m_selected_theme->path, m_selected_theme->name, m_background_settings_changed)); + 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())); + // 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"))); + 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 3ec7a9fb28..b0778537db 100644 --- a/Userland/Applications/DisplaySettings/ThemesSettingsWidget.h +++ b/Userland/Applications/DisplaySettings/ThemesSettingsWidget.h @@ -25,14 +25,17 @@ public: private: Vector<Gfx::SystemThemeMetaData> m_themes; Vector<DeprecatedString> m_theme_names; + Vector<DeprecatedString> m_color_scheme_names; RefPtr<GUI::ComboBox> m_themes_combo; RefPtr<ThemePreviewWidget> m_theme_preview; Gfx::SystemThemeMetaData const* m_selected_theme { nullptr }; + DeprecatedString m_selected_color_scheme_name = ""; RefPtr<GUI::Button> m_cursor_themes_button; bool& m_background_settings_changed; + bool m_color_scheme_is_file_based = true; ThemesSettingsWidget(bool& background_settings_changed); }; |