Serenity Operating System
at master 150 lines 7.6 kB view raw
1/* 2 * Copyright (c) 2022, MacDue <macdue@dueutil.tech> 3 * Copyright (c) 2022, networkException <networkexception@serenityos.org> 4 * 5 * SPDX-License-Identifier: BSD-2-Clause 6 */ 7 8#include "ThemesSettingsWidget.h" 9#include <AK/LexicalPath.h> 10#include <AK/QuickSort.h> 11#include <Applications/DisplaySettings/ThemesSettingsGML.h> 12#include <LibCore/DirIterator.h> 13#include <LibGUI/Application.h> 14#include <LibGUI/CheckBox.h> 15#include <LibGUI/ConnectionToWindowServer.h> 16#include <LibGUI/ItemListModel.h> 17#include <LibGUI/MessageBox.h> 18#include <LibGUI/Process.h> 19 20namespace DisplaySettings { 21 22static DeprecatedString get_color_scheme_name_from_pathname(DeprecatedString const& color_scheme_path) 23{ 24 return color_scheme_path.replace("/res/color-schemes/"sv, ""sv, ReplaceMode::FirstOnly).replace(".ini"sv, ""sv, ReplaceMode::FirstOnly); 25}; 26 27ThemesSettingsWidget::ThemesSettingsWidget(bool& background_settings_changed) 28 : m_background_settings_changed { background_settings_changed } 29{ 30 load_from_gml(themes_settings_gml).release_value_but_fixme_should_propagate_errors(); 31 m_themes = MUST(Gfx::list_installed_system_themes()); 32 33 size_t current_theme_index; 34 auto current_theme_name = GUI::ConnectionToWindowServer::the().get_system_theme(); 35 m_theme_names.ensure_capacity(m_themes.size()); 36 for (auto& theme_meta : m_themes) { 37 m_theme_names.append(theme_meta.name); 38 if (current_theme_name == theme_meta.name) { 39 m_selected_theme = &theme_meta; 40 current_theme_index = m_theme_names.size() - 1; 41 } 42 } 43 m_theme_preview = find_descendant_of_type_named<GUI::Frame>("preview_frame")->add<ThemePreviewWidget>(palette()); 44 m_themes_combo = *find_descendant_of_type_named<GUI::ComboBox>("themes_combo"); 45 m_themes_combo->set_only_allow_values_from_model(true); 46 m_themes_combo->set_model(*GUI::ItemListModel<DeprecatedString>::create(m_theme_names)); 47 m_themes_combo->on_change = [this](auto&, const GUI::ModelIndex& index) { 48 m_selected_theme = &m_themes.at(index.row()); 49 auto set_theme_result = m_theme_preview->set_theme(m_selected_theme->path); 50 if (set_theme_result.is_error()) { 51 GUI::MessageBox::show_error(window(), DeprecatedString::formatted("There was an error generating the theme preview: {}", set_theme_result.error())); 52 } 53 set_modified(true); 54 }; 55 m_themes_combo->set_selected_index(current_theme_index, GUI::AllowCallback::No); 56 57 auto mouse_settings_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/app-mouse.png"sv).release_value_but_fixme_should_propagate_errors(); 58 59 m_color_scheme_names.clear(); 60 Core::DirIterator iterator("/res/color-schemes", Core::DirIterator::SkipParentAndBaseDir); 61 while (iterator.has_next()) { 62 auto path = iterator.next_path(); 63 m_color_scheme_names.append(path.replace(".ini"sv, ""sv, ReplaceMode::FirstOnly)); 64 } 65 quick_sort(m_color_scheme_names); 66 auto& color_scheme_combo = *find_descendant_of_type_named<GUI::ComboBox>("color_scheme_combo"); 67 color_scheme_combo.set_only_allow_values_from_model(true); 68 color_scheme_combo.set_model(*GUI::ItemListModel<DeprecatedString>::create(m_color_scheme_names)); 69 m_selected_color_scheme_name = get_color_scheme_name_from_pathname(GUI::Widget::palette().color_scheme_path()); 70 auto selected_color_scheme_index = m_color_scheme_names.find_first_index(m_selected_color_scheme_name); 71 if (selected_color_scheme_index.has_value()) 72 color_scheme_combo.set_selected_index(selected_color_scheme_index.value()); 73 else { 74 color_scheme_combo.set_text("Custom"); 75 m_color_scheme_is_file_based = false; 76 if (m_color_scheme_names.size() > 1) { 77 color_scheme_combo.set_enabled(true); 78 find_descendant_of_type_named<GUI::CheckBox>("custom_color_scheme_checkbox")->set_checked(true); 79 } 80 } 81 color_scheme_combo.on_change = [this](auto&, const GUI::ModelIndex& index) { 82 m_selected_color_scheme_name = index.data().as_string(); 83 m_color_scheme_is_file_based = true; 84 set_modified(true); 85 }; 86 87 find_descendant_of_type_named<GUI::CheckBox>("custom_color_scheme_checkbox")->on_checked = [this](bool) { 88 if (m_color_scheme_names.size() <= 1) 89 return; 90 91 if (find_descendant_of_type_named<GUI::CheckBox>("custom_color_scheme_checkbox")->is_checked()) 92 find_descendant_of_type_named<GUI::ComboBox>("color_scheme_combo")->set_enabled(true); 93 else { 94 find_descendant_of_type_named<GUI::ComboBox>("color_scheme_combo")->set_enabled(false); 95 set_modified(true); 96 } 97 }; 98 99 m_cursor_themes_button = *find_descendant_of_type_named<GUI::Button>("cursor_themes_button"); 100 m_cursor_themes_button->set_icon(mouse_settings_icon); 101 m_cursor_themes_button->on_click = [&](auto) { 102 GUI::Process::spawn_or_show_error(window(), "/bin/MouseSettings"sv, Array { "-t", "cursor-theme" }); 103 }; 104 105 GUI::Application::the()->on_theme_change = [&]() { 106 auto theme_override = GUI::ConnectionToWindowServer::the().get_system_theme_override(); 107 if (theme_override.has_value()) { 108 m_themes_combo->clear_selection(); 109 static_cast<RefPtr<GUI::AbstractThemePreview>>(m_theme_preview)->set_theme(*theme_override); 110 return; 111 } 112 113 auto current_theme_name = GUI::ConnectionToWindowServer::the().get_system_theme(); 114 115 size_t index = 0; 116 for (auto& theme_meta : m_themes) { 117 if (current_theme_name == theme_meta.name) { 118 m_themes_combo->set_selected_index(index, GUI::AllowCallback::No); 119 m_selected_theme = &m_themes.at(index); 120 auto set_theme_result = m_theme_preview->set_theme(m_selected_theme->path); 121 if (set_theme_result.is_error()) { 122 GUI::MessageBox::show_error(window(), DeprecatedString::formatted("There was an error setting the new theme: {}", set_theme_result.error())); 123 } 124 } 125 ++index; 126 } 127 }; 128} 129 130void ThemesSettingsWidget::apply_settings() 131{ 132 Optional<DeprecatedString> color_scheme_path = DeprecatedString::formatted("/res/color-schemes/{}.ini", m_selected_color_scheme_name); 133 134 if (!m_color_scheme_is_file_based && find_descendant_of_type_named<GUI::CheckBox>("custom_color_scheme_checkbox")->is_checked()) 135 VERIFY(GUI::ConnectionToWindowServer::the().set_system_theme(m_selected_theme->path, m_selected_theme->name, m_background_settings_changed, "Custom"sv)); 136 else if (find_descendant_of_type_named<GUI::CheckBox>("custom_color_scheme_checkbox")->is_checked()) 137 VERIFY(GUI::ConnectionToWindowServer::the().set_system_theme(m_selected_theme->path, m_selected_theme->name, m_background_settings_changed, color_scheme_path)); 138 else { 139 VERIFY(GUI::ConnectionToWindowServer::the().set_system_theme(m_selected_theme->path, m_selected_theme->name, m_background_settings_changed, OptionalNone())); 140 // Update the color scheme combobox to match the new theme. 141 auto const theme_config = Core::ConfigFile::open(m_selected_theme->path).release_value_but_fixme_should_propagate_errors(); 142 auto const color_scheme_index = m_color_scheme_names.find_first_index(get_color_scheme_name_from_pathname(theme_config->read_entry("Paths", "ColorScheme"))); 143 if (color_scheme_index.has_value()) 144 find_descendant_of_type_named<GUI::ComboBox>("color_scheme_combo")->set_selected_index(color_scheme_index.value()); 145 } 146 147 m_background_settings_changed = false; 148} 149 150}