ThemesSettingsWidget.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. #include "ThemesSettingsWidget.h"
  8. #include <AK/QuickSort.h>
  9. #include <Applications/DisplaySettings/ThemesSettingsGML.h>
  10. #include <LibCore/Directory.h>
  11. #include <LibGUI/Application.h>
  12. #include <LibGUI/CheckBox.h>
  13. #include <LibGUI/ConnectionToWindowServer.h>
  14. #include <LibGUI/ItemListModel.h>
  15. #include <LibGUI/MessageBox.h>
  16. #include <LibGUI/Process.h>
  17. namespace DisplaySettings {
  18. static ErrorOr<String> get_color_scheme_name_from_pathname(StringView color_scheme_path)
  19. {
  20. return TRY(String::from_deprecated_string(color_scheme_path.replace("/res/color-schemes/"sv, ""sv, ReplaceMode::FirstOnly).replace(".ini"sv, ""sv, ReplaceMode::FirstOnly)));
  21. }
  22. ErrorOr<NonnullRefPtr<ThemesSettingsWidget>> ThemesSettingsWidget::try_create(bool& background_settings_changed)
  23. {
  24. auto theme_settings_widget = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) ThemesSettingsWidget(background_settings_changed)));
  25. TRY(theme_settings_widget->load_from_gml(themes_settings_gml));
  26. TRY(theme_settings_widget->setup_interface());
  27. return theme_settings_widget;
  28. }
  29. ErrorOr<void> ThemesSettingsWidget::setup_interface()
  30. {
  31. m_themes = TRY(Gfx::list_installed_system_themes());
  32. size_t current_theme_index;
  33. auto current_theme_name = GUI::ConnectionToWindowServer::the().get_system_theme();
  34. TRY(m_theme_names.try_ensure_capacity(m_themes.size()));
  35. for (auto& theme_meta : m_themes) {
  36. TRY(m_theme_names.try_append(TRY(String::from_deprecated_string(theme_meta.name))));
  37. if (current_theme_name == theme_meta.name) {
  38. m_selected_theme = &theme_meta;
  39. current_theme_index = m_theme_names.size() - 1;
  40. }
  41. }
  42. m_theme_preview = find_descendant_of_type_named<GUI::Frame>("preview_frame")->add<ThemePreviewWidget>(palette());
  43. m_themes_combo = *find_descendant_of_type_named<GUI::ComboBox>("themes_combo");
  44. m_themes_combo->set_only_allow_values_from_model(true);
  45. m_themes_combo->set_model(*GUI::ItemListModel<String>::create(m_theme_names));
  46. m_themes_combo->on_change = [this](auto&, const GUI::ModelIndex& index) {
  47. m_selected_theme = &m_themes.at(index.row());
  48. auto selected_theme_path = String::from_deprecated_string(m_selected_theme->path);
  49. ErrorOr<void> set_theme_result;
  50. if (!selected_theme_path.is_error())
  51. set_theme_result = m_theme_preview->set_theme(selected_theme_path.release_value());
  52. if (set_theme_result.is_error()) {
  53. auto detailed_error_message = String::formatted("There was an error generating the theme preview: {}", set_theme_result.error());
  54. if (!detailed_error_message.is_error())
  55. GUI::MessageBox::show_error(window(), detailed_error_message.release_value());
  56. else
  57. GUI::MessageBox::show_error(window(), "There was an error generating the theme preview"sv);
  58. return;
  59. }
  60. set_modified(true);
  61. };
  62. m_themes_combo->set_selected_index(current_theme_index, GUI::AllowCallback::No);
  63. auto mouse_settings_icon = TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-mouse.png"sv));
  64. m_color_scheme_names.clear();
  65. TRY(Core::Directory::for_each_entry("/res/color-schemes"sv, Core::DirIterator::SkipParentAndBaseDir, [&](auto& entry, auto&) -> ErrorOr<IterationDecision> {
  66. LexicalPath path { entry.name };
  67. TRY(m_color_scheme_names.try_append(TRY(String::from_utf8(path.title()))));
  68. return IterationDecision::Continue;
  69. }));
  70. quick_sort(m_color_scheme_names);
  71. auto& color_scheme_combo = *find_descendant_of_type_named<GUI::ComboBox>("color_scheme_combo");
  72. color_scheme_combo.set_only_allow_values_from_model(true);
  73. color_scheme_combo.set_model(*GUI::ItemListModel<String>::create(m_color_scheme_names));
  74. m_selected_color_scheme_name = TRY(get_color_scheme_name_from_pathname(GUI::Widget::palette().color_scheme_path()));
  75. auto selected_color_scheme_index = m_color_scheme_names.find_first_index(m_selected_color_scheme_name);
  76. if (selected_color_scheme_index.has_value())
  77. color_scheme_combo.set_selected_index(selected_color_scheme_index.value());
  78. else {
  79. color_scheme_combo.set_text("Custom");
  80. m_color_scheme_is_file_based = false;
  81. }
  82. auto theme_config = TRY(Core::ConfigFile::open(m_selected_theme->path));
  83. if (!selected_color_scheme_index.has_value() || GUI::Widget::palette().color_scheme_path() != theme_config->read_entry("Paths", "ColorScheme")) {
  84. if (m_color_scheme_names.size() > 1) {
  85. color_scheme_combo.set_enabled(true);
  86. find_descendant_of_type_named<GUI::CheckBox>("custom_color_scheme_checkbox")->set_checked(true);
  87. }
  88. }
  89. color_scheme_combo.on_change = [this](auto&, const GUI::ModelIndex& index) {
  90. auto result = String::from_deprecated_string(index.data().as_string());
  91. if (result.is_error())
  92. return;
  93. m_selected_color_scheme_name = result.release_value();
  94. m_color_scheme_is_file_based = true;
  95. set_modified(true);
  96. };
  97. find_descendant_of_type_named<GUI::CheckBox>("custom_color_scheme_checkbox")->on_checked = [this](bool) {
  98. if (m_color_scheme_names.size() <= 1)
  99. return;
  100. if (find_descendant_of_type_named<GUI::CheckBox>("custom_color_scheme_checkbox")->is_checked())
  101. find_descendant_of_type_named<GUI::ComboBox>("color_scheme_combo")->set_enabled(true);
  102. else {
  103. find_descendant_of_type_named<GUI::ComboBox>("color_scheme_combo")->set_enabled(false);
  104. set_modified(true);
  105. }
  106. };
  107. m_cursor_themes_button = *find_descendant_of_type_named<GUI::Button>("cursor_themes_button");
  108. m_cursor_themes_button->set_icon(mouse_settings_icon);
  109. m_cursor_themes_button->on_click = [&](auto) {
  110. GUI::Process::spawn_or_show_error(window(), "/bin/MouseSettings"sv, Array { "-t", "cursor-theme" });
  111. };
  112. GUI::Application::the()->on_theme_change = [&]() {
  113. auto theme_override = GUI::ConnectionToWindowServer::the().get_system_theme_override();
  114. if (theme_override.has_value()) {
  115. m_themes_combo->clear_selection();
  116. static_cast<RefPtr<GUI::AbstractThemePreview>>(m_theme_preview)->set_theme(*theme_override);
  117. return;
  118. }
  119. auto current_theme_name = GUI::ConnectionToWindowServer::the().get_system_theme();
  120. size_t index = 0;
  121. for (auto& theme_meta : m_themes) {
  122. if (current_theme_name == theme_meta.name) {
  123. m_themes_combo->set_selected_index(index, GUI::AllowCallback::No);
  124. m_selected_theme = &m_themes.at(index);
  125. auto selected_theme_path = String::from_deprecated_string(m_selected_theme->path);
  126. ErrorOr<void> set_theme_result;
  127. if (!selected_theme_path.is_error())
  128. set_theme_result = m_theme_preview->set_theme(selected_theme_path.release_value());
  129. if (set_theme_result.is_error()) {
  130. auto detailed_error_message = String::formatted("There was an error generating the theme preview: {}", set_theme_result.error());
  131. if (!detailed_error_message.is_error())
  132. GUI::MessageBox::show_error(window(), detailed_error_message.release_value());
  133. else
  134. GUI::MessageBox::show_error(window(), "There was an error generating the theme preview"sv);
  135. }
  136. }
  137. ++index;
  138. }
  139. };
  140. return {};
  141. }
  142. ThemesSettingsWidget::ThemesSettingsWidget(bool& background_settings_changed)
  143. : m_background_settings_changed { background_settings_changed }
  144. {
  145. }
  146. void ThemesSettingsWidget::apply_settings()
  147. {
  148. m_background_settings_changed = false;
  149. auto color_scheme_path_or_error = String::formatted("/res/color-schemes/{}.ini", m_selected_color_scheme_name);
  150. if (color_scheme_path_or_error.is_error()) {
  151. GUI::MessageBox::show_error(window(), "Unable to apply changes"sv);
  152. return;
  153. }
  154. auto color_scheme_path = color_scheme_path_or_error.release_value();
  155. if (!m_color_scheme_is_file_based && find_descendant_of_type_named<GUI::CheckBox>("custom_color_scheme_checkbox")->is_checked()) {
  156. auto set_theme_result = GUI::ConnectionToWindowServer::the().set_system_theme(m_selected_theme->path, m_selected_theme->name, m_background_settings_changed, "Custom"sv);
  157. if (!set_theme_result)
  158. GUI::MessageBox::show_error(window(), "Failed to apply theme settings"sv);
  159. } else if (find_descendant_of_type_named<GUI::CheckBox>("custom_color_scheme_checkbox")->is_checked()) {
  160. 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.to_deprecated_string());
  161. if (!set_theme_result)
  162. GUI::MessageBox::show_error(window(), "Failed to apply theme settings"sv);
  163. } else {
  164. auto theme_config = Core::ConfigFile::open(m_selected_theme->path);
  165. if (theme_config.is_error()) {
  166. GUI::MessageBox::show_error(window(), "Failed to open theme config file"sv);
  167. return;
  168. }
  169. auto preferred_color_scheme_path = get_color_scheme_name_from_pathname(theme_config.release_value()->read_entry("Paths", "ColorScheme"));
  170. auto set_theme_result = GUI::ConnectionToWindowServer::the().set_system_theme(m_selected_theme->path, m_selected_theme->name, m_background_settings_changed, OptionalNone());
  171. if (!set_theme_result || preferred_color_scheme_path.is_error()) {
  172. GUI::MessageBox::show_error(window(), "Failed to apply theme settings"sv);
  173. return;
  174. }
  175. // Update the color scheme combobox to match the new theme.
  176. auto const color_scheme_index = m_color_scheme_names.find_first_index(preferred_color_scheme_path.value());
  177. if (color_scheme_index.has_value())
  178. find_descendant_of_type_named<GUI::ComboBox>("color_scheme_combo")->set_selected_index(color_scheme_index.value());
  179. }
  180. }
  181. }