DisplaySettings.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Copyright (c) 2019-2020, Jesse Buhagiar <jooster669@gmail.com>
  3. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "DisplaySettings.h"
  8. #include <AK/StringBuilder.h>
  9. #include <Applications/DisplaySettings/DisplaySettingsWindowGML.h>
  10. #include <LibCore/ConfigFile.h>
  11. #include <LibCore/DirIterator.h>
  12. #include <LibGUI/Application.h>
  13. #include <LibGUI/BoxLayout.h>
  14. #include <LibGUI/Button.h>
  15. #include <LibGUI/ComboBox.h>
  16. #include <LibGUI/Desktop.h>
  17. #include <LibGUI/FilePicker.h>
  18. #include <LibGUI/ItemListModel.h>
  19. #include <LibGUI/Label.h>
  20. #include <LibGUI/MessageBox.h>
  21. #include <LibGUI/RadioButton.h>
  22. #include <LibGUI/WindowServerConnection.h>
  23. #include <LibGfx/Palette.h>
  24. #include <LibGfx/SystemTheme.h>
  25. REGISTER_WIDGET(DisplaySettings, MonitorWidget)
  26. DisplaySettingsWidget::DisplaySettingsWidget()
  27. {
  28. create_resolution_list();
  29. create_wallpaper_list();
  30. create_frame();
  31. load_current_settings();
  32. }
  33. void DisplaySettingsWidget::create_resolution_list()
  34. {
  35. // TODO: Find a better way to get the default resolution
  36. m_resolutions.append({ 640, 480 });
  37. m_resolutions.append({ 800, 600 });
  38. m_resolutions.append({ 1024, 768 });
  39. m_resolutions.append({ 1280, 720 });
  40. m_resolutions.append({ 1280, 768 });
  41. m_resolutions.append({ 1280, 960 });
  42. m_resolutions.append({ 1280, 1024 });
  43. m_resolutions.append({ 1360, 768 });
  44. m_resolutions.append({ 1368, 768 });
  45. m_resolutions.append({ 1440, 900 });
  46. m_resolutions.append({ 1600, 900 });
  47. m_resolutions.append({ 1600, 1200 });
  48. m_resolutions.append({ 1920, 1080 });
  49. m_resolutions.append({ 2048, 1152 });
  50. m_resolutions.append({ 2560, 1080 });
  51. m_resolutions.append({ 2560, 1440 });
  52. }
  53. void DisplaySettingsWidget::create_wallpaper_list()
  54. {
  55. Core::DirIterator iterator("/res/wallpapers/", Core::DirIterator::Flags::SkipDots);
  56. m_wallpapers.append("Use background color");
  57. while (iterator.has_next()) {
  58. m_wallpapers.append(iterator.next_path());
  59. }
  60. m_modes.append("simple");
  61. m_modes.append("tile");
  62. m_modes.append("center");
  63. m_modes.append("stretch");
  64. }
  65. void DisplaySettingsWidget::create_frame()
  66. {
  67. load_from_gml(display_settings_window_gml);
  68. m_monitor_widget = *find_descendant_of_type_named<DisplaySettings::MonitorWidget>("monitor_widget");
  69. m_wallpaper_combo = *find_descendant_of_type_named<GUI::ComboBox>("wallpaper_combo");
  70. m_wallpaper_combo->set_only_allow_values_from_model(true);
  71. m_wallpaper_combo->set_model(*GUI::ItemListModel<String>::create(m_wallpapers));
  72. m_wallpaper_combo->on_change = [this](auto& text, const GUI::ModelIndex& index) {
  73. String path = text;
  74. if (path.starts_with("/") && m_monitor_widget->set_wallpaper(path)) {
  75. m_monitor_widget->update();
  76. return;
  77. }
  78. if (index.row() == 0) {
  79. path = "";
  80. } else {
  81. if (index.is_valid()) {
  82. StringBuilder builder;
  83. builder.append("/res/wallpapers/");
  84. builder.append(path);
  85. path = builder.to_string();
  86. }
  87. }
  88. m_monitor_widget->set_wallpaper(path);
  89. m_monitor_widget->update();
  90. };
  91. auto& button = *find_descendant_of_type_named<GUI::Button>("wallpaper_open_button");
  92. button.set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png"));
  93. button.on_click = [this](auto) {
  94. Optional<String> open_path = GUI::FilePicker::get_open_filepath(nullptr, "Select wallpaper from file system.");
  95. if (!open_path.has_value())
  96. return;
  97. m_wallpaper_combo->set_only_allow_values_from_model(false);
  98. m_wallpaper_combo->set_text(open_path.value());
  99. m_wallpaper_combo->set_only_allow_values_from_model(true);
  100. };
  101. m_mode_combo = *find_descendant_of_type_named<GUI::ComboBox>("mode_combo");
  102. m_mode_combo->set_only_allow_values_from_model(true);
  103. m_mode_combo->set_model(*GUI::ItemListModel<String>::create(m_modes));
  104. m_mode_combo->on_change = [this](auto&, const GUI::ModelIndex& index) {
  105. m_monitor_widget->set_wallpaper_mode(m_modes.at(index.row()));
  106. m_monitor_widget->update();
  107. };
  108. m_resolution_combo = *find_descendant_of_type_named<GUI::ComboBox>("resolution_combo");
  109. m_resolution_combo->set_only_allow_values_from_model(true);
  110. m_resolution_combo->set_model(*GUI::ItemListModel<Gfx::IntSize>::create(m_resolutions));
  111. m_resolution_combo->on_change = [this](auto&, const GUI::ModelIndex& index) {
  112. m_monitor_widget->set_desktop_resolution(m_resolutions.at(index.row()));
  113. m_monitor_widget->update();
  114. };
  115. m_display_scale_radio_1x = *find_descendant_of_type_named<GUI::RadioButton>("scale_1x");
  116. m_display_scale_radio_1x->on_checked = [this](bool checked) {
  117. if (checked) {
  118. m_monitor_widget->set_desktop_scale_factor(1);
  119. m_monitor_widget->update();
  120. }
  121. };
  122. m_display_scale_radio_2x = *find_descendant_of_type_named<GUI::RadioButton>("scale_2x");
  123. m_display_scale_radio_2x->on_checked = [this](bool checked) {
  124. if (checked) {
  125. m_monitor_widget->set_desktop_scale_factor(2);
  126. m_monitor_widget->update();
  127. }
  128. };
  129. m_color_input = *find_descendant_of_type_named<GUI::ColorInput>("color_input");
  130. m_color_input->set_color_has_alpha_channel(false);
  131. m_color_input->set_color_picker_title("Select color for desktop");
  132. m_color_input->on_change = [this] {
  133. m_monitor_widget->set_background_color(m_color_input->color());
  134. m_monitor_widget->update();
  135. };
  136. auto& ok_button = *find_descendant_of_type_named<GUI::Button>("ok_button");
  137. ok_button.on_click = [this](auto) {
  138. send_settings_to_window_server();
  139. GUI::Application::the()->quit();
  140. };
  141. auto& cancel_button = *find_descendant_of_type_named<GUI::Button>("cancel_button");
  142. cancel_button.on_click = [](auto) {
  143. GUI::Application::the()->quit();
  144. };
  145. auto& apply_button = *find_descendant_of_type_named<GUI::Button>("apply_button");
  146. apply_button.on_click = [this](auto) {
  147. send_settings_to_window_server();
  148. };
  149. }
  150. void DisplaySettingsWidget::load_current_settings()
  151. {
  152. auto ws_config(Core::ConfigFile::open("/etc/WindowServer.ini"));
  153. auto wm_config = Core::ConfigFile::get_for_app("WindowManager");
  154. /// Wallpaper path ////////////////////////////////////////////////////////////////////////////
  155. /// Read wallpaper path from config file and set value to monitor widget and combo box.
  156. auto selected_wallpaper = wm_config->read_entry("Background", "Wallpaper", "");
  157. if (!selected_wallpaper.is_empty()) {
  158. m_monitor_widget->set_wallpaper(selected_wallpaper);
  159. Optional<size_t> optional_index;
  160. if (selected_wallpaper.starts_with("/res/wallpapers/")) {
  161. auto name_parts = selected_wallpaper.split('/', true);
  162. optional_index = m_wallpapers.find_first_index(name_parts[2]);
  163. if (optional_index.has_value()) {
  164. m_wallpaper_combo->set_selected_index(optional_index.value());
  165. }
  166. }
  167. if (!optional_index.has_value()) {
  168. m_wallpaper_combo->set_only_allow_values_from_model(false);
  169. m_wallpaper_combo->set_text(selected_wallpaper);
  170. m_wallpaper_combo->set_only_allow_values_from_model(true);
  171. }
  172. } else {
  173. m_wallpaper_combo->set_selected_index(0);
  174. }
  175. size_t index;
  176. /// Mode //////////////////////////////////////////////////////////////////////////////////////
  177. auto mode = ws_config->read_entry("Background", "Mode", "simple");
  178. if (!m_modes.contains_slow(mode)) {
  179. warnln("Invalid background mode '{}' in WindowServer config, falling back to 'simple'", mode);
  180. mode = "simple";
  181. }
  182. m_monitor_widget->set_wallpaper_mode(mode);
  183. index = m_modes.find_first_index(mode).value();
  184. m_mode_combo->set_selected_index(index);
  185. /// Resolution and scale factor ///////////////////////////////////////////////////////////////
  186. int scale_factor = ws_config->read_num_entry("Screen", "ScaleFactor", 1);
  187. if (scale_factor != 1 && scale_factor != 2) {
  188. dbgln("unexpected ScaleFactor {}, setting to 1", scale_factor);
  189. scale_factor = 1;
  190. }
  191. (scale_factor == 1 ? m_display_scale_radio_1x : m_display_scale_radio_2x)->set_checked(true);
  192. m_monitor_widget->set_desktop_scale_factor(scale_factor);
  193. // Let's attempt to find the current resolution and select it!
  194. Gfx::IntSize find_size;
  195. find_size.set_width(ws_config->read_num_entry("Screen", "Width", 1024));
  196. find_size.set_height(ws_config->read_num_entry("Screen", "Height", 768));
  197. index = m_resolutions.find_first_index(find_size).value_or(0);
  198. Gfx::IntSize m_current_resolution = m_resolutions.at(index);
  199. m_monitor_widget->set_desktop_resolution(m_current_resolution);
  200. m_resolution_combo->set_selected_index(index);
  201. /// Color /////////////////////////////////////////////////////////////////////////////////////
  202. /// If presend read from config file. If not paint with palette color.
  203. Color palette_desktop_color = palette().desktop_background();
  204. auto background_color = ws_config->read_entry("Background", "Color", "");
  205. if (!background_color.is_empty()) {
  206. auto opt_color = Color::from_string(background_color);
  207. if (opt_color.has_value())
  208. palette_desktop_color = opt_color.value();
  209. }
  210. m_color_input->set_color(palette_desktop_color);
  211. m_monitor_widget->set_background_color(palette_desktop_color);
  212. m_monitor_widget->update();
  213. }
  214. void DisplaySettingsWidget::send_settings_to_window_server()
  215. {
  216. // Store the current screen resolution and scale factor in case the user wants to revert to it.
  217. auto ws_config(Core::ConfigFile::open("/etc/WindowServer.ini"));
  218. Gfx::IntSize current_resolution;
  219. current_resolution.set_width(ws_config->read_num_entry("Screen", "Width", 1024));
  220. current_resolution.set_height(ws_config->read_num_entry("Screen", "Height", 768));
  221. int current_scale_factor = ws_config->read_num_entry("Screen", "ScaleFactor", 1);
  222. if (current_scale_factor != 1 && current_scale_factor != 2) {
  223. dbgln("unexpected ScaleFactor {}, setting to 1", current_scale_factor);
  224. current_scale_factor = 1;
  225. }
  226. if (current_resolution != m_monitor_widget->desktop_resolution() || current_scale_factor != m_monitor_widget->desktop_scale_factor()) {
  227. auto result = GUI::WindowServerConnection::the().send_sync<Messages::WindowServer::SetResolution>(m_monitor_widget->desktop_resolution(), m_monitor_widget->desktop_scale_factor());
  228. if (!result->success()) {
  229. GUI::MessageBox::show(nullptr, String::formatted("Reverting to resolution {}x{} @ {}x", result->resolution().width(), result->resolution().height(), result->scale_factor()),
  230. "Unable to set resolution", GUI::MessageBox::Type::Error);
  231. } else {
  232. auto box = GUI::MessageBox::construct(window(), String::formatted("Do you want to keep the new settings? They will be reverted after 10 seconds."),
  233. String::formatted("New screen resolution: {}x{} @ {}x", m_monitor_widget->desktop_resolution().width(), m_monitor_widget->desktop_resolution().height(),
  234. m_monitor_widget->desktop_scale_factor()),
  235. GUI::MessageBox::Type::Question, GUI::MessageBox::InputType::YesNo);
  236. box->set_icon(window()->icon());
  237. // If after 10 seconds the user doesn't close the message box, just close it.
  238. auto timer = Core::Timer::construct(10000, [&] {
  239. box->close();
  240. });
  241. // If the user selects "No", closes the window or the window gets closed by the 10 seconds timer, revert the changes.
  242. if (box->exec() != GUI::MessageBox::ExecYes) {
  243. result = GUI::WindowServerConnection::the().send_sync<Messages::WindowServer::SetResolution>(current_resolution, current_scale_factor);
  244. if (!result->success()) {
  245. GUI::MessageBox::show(nullptr, String::formatted("Reverting to resolution {}x{} @ {}x", result->resolution().width(), result->resolution().height(), result->scale_factor()),
  246. "Unable to set resolution", GUI::MessageBox::Type::Error);
  247. }
  248. }
  249. }
  250. }
  251. if (!m_monitor_widget->wallpaper().is_empty()) {
  252. GUI::Desktop::the().set_wallpaper(m_monitor_widget->wallpaper());
  253. } else {
  254. dbgln("Setting color input: __{}__", m_color_input->text());
  255. GUI::Desktop::the().set_wallpaper("");
  256. GUI::Desktop::the().set_background_color(m_color_input->text());
  257. }
  258. GUI::Desktop::the().set_wallpaper_mode(m_monitor_widget->wallpaper_mode());
  259. }