DisplayProperties.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Copyright (c) 2019-2020, Jesse Buhagiar <jooster669@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "DisplayProperties.h"
  27. #include "ItemListModel.h"
  28. #include <AK/StringBuilder.h>
  29. #include <LibCore/ConfigFile.h>
  30. #include <LibCore/DirIterator.h>
  31. #include <LibGUI/Application.h>
  32. #include <LibGUI/BoxLayout.h>
  33. #include <LibGUI/Button.h>
  34. #include <LibGUI/ComboBox.h>
  35. #include <LibGUI/Desktop.h>
  36. #include <LibGUI/FilePicker.h>
  37. #include <LibGUI/Label.h>
  38. #include <LibGUI/MessageBox.h>
  39. #include <LibGUI/WindowServerConnection.h>
  40. #include <LibGfx/Palette.h>
  41. #include <LibGfx/SystemTheme.h>
  42. DisplayPropertiesWidget::DisplayPropertiesWidget()
  43. {
  44. create_resolution_list();
  45. create_wallpaper_list();
  46. create_frame();
  47. load_current_settings();
  48. }
  49. void DisplayPropertiesWidget::create_resolution_list()
  50. {
  51. // TODO: Find a better way to get the default resolution
  52. m_resolutions.append({ 640, 480 });
  53. m_resolutions.append({ 800, 600 });
  54. m_resolutions.append({ 1024, 768 });
  55. m_resolutions.append({ 1280, 720 });
  56. m_resolutions.append({ 1280, 768 });
  57. m_resolutions.append({ 1280, 1024 });
  58. m_resolutions.append({ 1360, 768 });
  59. m_resolutions.append({ 1368, 768 });
  60. m_resolutions.append({ 1440, 900 });
  61. m_resolutions.append({ 1600, 900 });
  62. m_resolutions.append({ 1920, 1080 });
  63. m_resolutions.append({ 2560, 1080 });
  64. }
  65. void DisplayPropertiesWidget::create_wallpaper_list()
  66. {
  67. Core::DirIterator iterator("/res/wallpapers/", Core::DirIterator::Flags::SkipDots);
  68. m_wallpapers.append("Use background color");
  69. while (iterator.has_next()) {
  70. m_wallpapers.append(iterator.next_path());
  71. }
  72. m_modes.append("simple");
  73. m_modes.append("tile");
  74. m_modes.append("center");
  75. m_modes.append("scaled");
  76. }
  77. void DisplayPropertiesWidget::create_frame()
  78. {
  79. m_root_widget = GUI::Widget::construct();
  80. m_root_widget->set_layout<GUI::VerticalBoxLayout>();
  81. m_root_widget->set_fill_with_background_color(true);
  82. m_root_widget->layout()->set_margins({ 4, 4, 4, 4 });
  83. auto& settings_content = m_root_widget->add<GUI::Widget>();
  84. settings_content.set_layout<GUI::VerticalBoxLayout>();
  85. settings_content.set_backcolor("red");
  86. settings_content.set_background_color(Color::Blue);
  87. settings_content.set_background_role(Gfx::ColorRole::Window);
  88. settings_content.layout()->set_margins({ 4, 4, 4, 4 });
  89. /// Wallpaper Preview /////////////////////////////////////////////////////////////////////////
  90. m_monitor_widget = settings_content.add<MonitorWidget>();
  91. m_monitor_widget->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  92. m_monitor_widget->set_preferred_size(338, 248);
  93. /// Wallpaper Row /////////////////////////////////////////////////////////////////////////////
  94. auto& wallpaper_selection_container = settings_content.add<GUI::Widget>();
  95. wallpaper_selection_container.set_layout<GUI::HorizontalBoxLayout>();
  96. wallpaper_selection_container.layout()->set_margins({ 0, 4, 0, 0 });
  97. wallpaper_selection_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  98. wallpaper_selection_container.set_preferred_size(0, 22);
  99. auto& wallpaper_label = wallpaper_selection_container.add<GUI::Label>();
  100. wallpaper_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  101. wallpaper_label.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  102. wallpaper_label.set_preferred_size({ 70, 0 });
  103. wallpaper_label.set_text("Wallpaper:");
  104. m_wallpaper_combo = wallpaper_selection_container.add<GUI::ComboBox>();
  105. m_wallpaper_combo->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  106. m_wallpaper_combo->set_preferred_size(0, 22);
  107. m_wallpaper_combo->set_only_allow_values_from_model(true);
  108. m_wallpaper_combo->set_model(*ItemListModel<AK::String>::create(m_wallpapers));
  109. m_wallpaper_combo->on_change = [this](auto& text, const GUI::ModelIndex& index) {
  110. String path = text;
  111. if (index.row() == 0) {
  112. path = "";
  113. } else {
  114. if (index.is_valid()) {
  115. StringBuilder builder;
  116. builder.append("/res/wallpapers/");
  117. builder.append(path);
  118. path = builder.to_string();
  119. }
  120. }
  121. this->m_monitor_widget->set_wallpaper(path);
  122. this->m_monitor_widget->update();
  123. };
  124. auto& button = wallpaper_selection_container.add<GUI::Button>();
  125. button.set_tooltip("Select Wallpaper from file system.");
  126. button.set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png"));
  127. button.set_button_style(Gfx::ButtonStyle::CoolBar);
  128. button.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  129. button.set_preferred_size(22, 22);
  130. button.on_click = [this]() {
  131. Optional<String> open_path = GUI::FilePicker::get_open_filepath("Select wallpaper from file system.");
  132. if (!open_path.has_value())
  133. return;
  134. m_wallpaper_combo->set_only_allow_values_from_model(false);
  135. this->m_wallpaper_combo->set_text(open_path.value());
  136. m_wallpaper_combo->set_only_allow_values_from_model(true);
  137. };
  138. /// Mode //////////////////////////////////////////////////////////////////////////////////////
  139. auto& mode_selection_container = settings_content.add<GUI::Widget>();
  140. mode_selection_container.set_layout<GUI::HorizontalBoxLayout>();
  141. mode_selection_container.layout()->set_margins({ 0, 4, 0, 0 });
  142. mode_selection_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  143. mode_selection_container.set_preferred_size(0, 22);
  144. auto& mode_label = mode_selection_container.add<GUI::Label>();
  145. mode_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  146. mode_label.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  147. mode_label.set_preferred_size({ 70, 0 });
  148. mode_label.set_text("Mode:");
  149. m_mode_combo = mode_selection_container.add<GUI::ComboBox>();
  150. m_mode_combo->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  151. m_mode_combo->set_preferred_size(0, 22);
  152. m_mode_combo->set_only_allow_values_from_model(true);
  153. m_mode_combo->set_model(*ItemListModel<AK::String>::create(m_modes));
  154. m_mode_combo->on_change = [this](auto&, const GUI::ModelIndex& index) {
  155. this->m_monitor_widget->set_wallpaper_mode(m_modes.at(index.row()));
  156. this->m_monitor_widget->update();
  157. };
  158. /// Resulation Row ////////////////////////////////////////////////////////////////////////////
  159. auto& resolution_selection_container = settings_content.add<GUI::Widget>();
  160. resolution_selection_container.set_layout<GUI::HorizontalBoxLayout>();
  161. resolution_selection_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  162. resolution_selection_container.set_preferred_size(0, 22);
  163. auto& m_resolution_label = resolution_selection_container.add<GUI::Label>();
  164. m_resolution_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  165. m_resolution_label.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  166. m_resolution_label.set_preferred_size({ 70, 0 });
  167. m_resolution_label.set_text("Resolution:");
  168. m_resolution_combo = resolution_selection_container.add<GUI::ComboBox>();
  169. m_resolution_combo->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  170. m_resolution_combo->set_preferred_size(0, 22);
  171. m_resolution_combo->set_only_allow_values_from_model(true);
  172. m_resolution_combo->set_model(*ItemListModel<Gfx::Size>::create(m_resolutions));
  173. m_resolution_combo->on_change = [this](auto&, const GUI::ModelIndex& index) {
  174. this->m_monitor_widget->set_desktop_resolution(m_resolutions.at(index.row()));
  175. this->m_monitor_widget->update();
  176. };
  177. /// Background Color Row //////////////////////////////////////////////////////////////////////
  178. auto& color_selection_container = settings_content.add<GUI::Widget>();
  179. color_selection_container.set_layout<GUI::HorizontalBoxLayout>();
  180. color_selection_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  181. color_selection_container.set_preferred_size(0, 22);
  182. auto& color_label = color_selection_container.add<GUI::Label>();
  183. color_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  184. color_label.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  185. color_label.set_preferred_size({ 70, 0 });
  186. color_label.set_text("Color Name:");
  187. m_color_input = color_selection_container.add<GUI::ColorInput>();
  188. m_color_input->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  189. m_color_input->set_preferred_size(90, 0);
  190. m_color_input->set_color_picker_title("Select color for desktop");
  191. m_color_input->on_change = [this] {
  192. this->m_monitor_widget->set_background_color(m_color_input->color());
  193. this->m_monitor_widget->update();
  194. };
  195. /// Add the apply and cancel buttons //////////////////////////////////////////////////////////
  196. auto& bottom_widget = settings_content.add<GUI::Widget>();
  197. bottom_widget.set_layout<GUI::HorizontalBoxLayout>();
  198. bottom_widget.layout()->add_spacer();
  199. //bottom_widget.layout()->set_margins({ 4, 10, 4, 10 });
  200. bottom_widget.set_size_policy(Orientation::Vertical, GUI::SizePolicy::Fixed);
  201. bottom_widget.set_preferred_size(1, 22);
  202. auto& apply_button = bottom_widget.add<GUI::Button>();
  203. apply_button.set_text("Apply");
  204. apply_button.set_size_policy(Orientation::Horizontal, GUI::SizePolicy::Fixed);
  205. apply_button.set_preferred_size(60, 22);
  206. apply_button.on_click = [this] {
  207. send_settings_to_window_server();
  208. };
  209. auto& ok_button = bottom_widget.add<GUI::Button>();
  210. ok_button.set_text("OK");
  211. ok_button.set_size_policy(Orientation::Horizontal, GUI::SizePolicy::Fixed);
  212. ok_button.set_preferred_size(60, 22);
  213. ok_button.on_click = [this] {
  214. send_settings_to_window_server();
  215. GUI::Application::the().quit();
  216. };
  217. auto& cancel_button = bottom_widget.add<GUI::Button>();
  218. cancel_button.set_text("Cancel");
  219. cancel_button.set_size_policy(Orientation::Horizontal, GUI::SizePolicy::Fixed);
  220. cancel_button.set_preferred_size(60, 22);
  221. cancel_button.on_click = [] {
  222. GUI::Application::the().quit();
  223. };
  224. }
  225. void DisplayPropertiesWidget::load_current_settings()
  226. {
  227. auto ws_config(Core::ConfigFile::open("/etc/WindowServer/WindowServer.ini"));
  228. auto wm_config = Core::ConfigFile::get_for_app("WindowManager");
  229. /// Wallpaper path ////////////////////////////////////////////////////////////////////////////
  230. /// Read wallpaper path from config file and set value to monitor widget and combo box.
  231. auto selected_wallpaper = wm_config->read_entry("Background", "Wallpaper", "");
  232. if (!selected_wallpaper.is_empty()) {
  233. m_monitor_widget->set_wallpaper(selected_wallpaper);
  234. Optional<size_t> optional_index;
  235. if (selected_wallpaper.starts_with("/res/wallpapers/")) {
  236. auto name_parts = selected_wallpaper.split('/');
  237. optional_index = m_wallpapers.find_first_index(name_parts[2]);
  238. if (optional_index.has_value()) {
  239. m_wallpaper_combo->set_selected_index(optional_index.value());
  240. }
  241. }
  242. if (!optional_index.has_value()) {
  243. m_wallpaper_combo->set_only_allow_values_from_model(false);
  244. m_wallpaper_combo->set_text(selected_wallpaper);
  245. m_wallpaper_combo->set_only_allow_values_from_model(true);
  246. }
  247. } else {
  248. m_wallpaper_combo->set_selected_index(0);
  249. }
  250. /// Mode //////////////////////////////////////////////////////////////////////////////////////
  251. auto mode = ws_config->read_entry("Background", "Mode");
  252. if (!mode.is_empty()) {
  253. this->m_monitor_widget->set_wallpaper_mode(mode);
  254. auto index = m_modes.find_first_index(mode).value();
  255. m_mode_combo->set_selected_index(index);
  256. }
  257. /// Resolution ////////////////////////////////////////////////////////////////////////////////
  258. Gfx::Size find_size;
  259. bool okay = false;
  260. // Let's attempt to find the current resolution and select it!
  261. find_size.set_width(ws_config->read_entry("Screen", "Width", "1024").to_int(okay));
  262. if (!okay) {
  263. fprintf(stderr, "DisplayProperties: failed to convert width to int!");
  264. ASSERT_NOT_REACHED();
  265. }
  266. find_size.set_height(ws_config->read_entry("Screen", "Height", "768").to_int(okay));
  267. if (!okay) {
  268. fprintf(stderr, "DisplayProperties: failed to convert height to int!");
  269. ASSERT_NOT_REACHED();
  270. }
  271. size_t index = m_resolutions.find_first_index(find_size).value_or(0);
  272. Gfx::Size m_current_resolution = m_resolutions.at(index);
  273. m_monitor_widget->set_desktop_resolution(m_current_resolution);
  274. m_resolution_combo->set_selected_index(index);
  275. /// Color /////////////////////////////////////////////////////////////////////////////////////
  276. /// If presend read from config file. If not paint with palet color.
  277. Color palette_desktop_color = this->palette().desktop_background();
  278. auto background_color = ws_config->read_entry("Background", "Color", "");
  279. if (!background_color.is_empty()) {
  280. auto opt_color = Color::from_string(background_color);
  281. if (opt_color.has_value())
  282. palette_desktop_color = opt_color.value();
  283. }
  284. m_color_input->set_color(palette_desktop_color);
  285. m_monitor_widget->set_background_color(palette_desktop_color);
  286. m_monitor_widget->update();
  287. }
  288. void DisplayPropertiesWidget::send_settings_to_window_server()
  289. {
  290. auto result = GUI::WindowServerConnection::the().send_sync<Messages::WindowServer::SetResolution>(m_monitor_widget->desktop_resolution());
  291. if (!result->success()) {
  292. GUI::MessageBox::show(String::format("Reverting to resolution %dx%d", result->resolution().width(), result->resolution().height()),
  293. "Unable to set resolution", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK);
  294. }
  295. if (!m_monitor_widget->wallpaper().is_empty()) {
  296. GUI::Desktop::the().set_wallpaper(m_monitor_widget->wallpaper());
  297. } else {
  298. dbg() << "Setting color input: __" << m_color_input->text() << "__";
  299. GUI::Desktop::the().set_wallpaper("");
  300. GUI::Desktop::the().set_background_color(m_color_input->text());
  301. }
  302. GUI::Desktop::the().set_wallpaper_mode(m_monitor_widget->wallpaper_mode());
  303. }