DisplayProperties.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  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/DirIterator.h>
  30. #include <LibGUI/Action.h>
  31. #include <LibGUI/Application.h>
  32. #include <LibGUI/BoxLayout.h>
  33. #include <LibGUI/Button.h>
  34. #include <LibGUI/Desktop.h>
  35. #include <LibGUI/FileSystemModel.h>
  36. #include <LibGUI/GroupBox.h>
  37. #include <LibGUI/Label.h>
  38. #include <LibGUI/ListView.h>
  39. #include <LibGUI/ScrollBar.h>
  40. #include <LibGUI/Splitter.h>
  41. #include <LibGUI/TabWidget.h>
  42. #include <LibGUI/ToolBar.h>
  43. #include <LibGUI/Widget.h>
  44. #include <LibGUI/Window.h>
  45. #include <LibGUI/WindowServerConnection.h>
  46. #include <Servers/WindowServer/WindowManager.h>
  47. DisplayPropertiesWidget::DisplayPropertiesWidget()
  48. : m_wm_config(Core::ConfigFile::open("/etc/WindowServer/WindowServer.ini"))
  49. {
  50. create_resolution_list();
  51. create_wallpaper_list();
  52. create_root_widget();
  53. create_frame();
  54. }
  55. void DisplayPropertiesWidget::create_resolution_list()
  56. {
  57. // TODO: Find a better way to get the default resolution
  58. m_resolutions.append({ 640, 480 });
  59. m_resolutions.append({ 800, 600 });
  60. m_resolutions.append({ 1024, 768 });
  61. m_resolutions.append({ 1280, 720 });
  62. m_resolutions.append({ 1280, 1024 });
  63. m_resolutions.append({ 1440, 900 });
  64. m_resolutions.append({ 1600, 900 });
  65. m_resolutions.append({ 1920, 1080 });
  66. m_resolutions.append({ 2560, 1080 });
  67. Gfx::Size find_size;
  68. bool okay = false;
  69. // Let's attempt to find the current resolution and select it!
  70. find_size.set_width(m_wm_config->read_entry("Screen", "Width", "1024").to_int(okay));
  71. if (!okay) {
  72. fprintf(stderr, "DisplayProperties: failed to convert width to int!");
  73. return;
  74. }
  75. find_size.set_height(m_wm_config->read_entry("Screen", "Height", "768").to_int(okay));
  76. if (!okay) {
  77. fprintf(stderr, "DisplayProperties: failed to convert height to int!");
  78. return;
  79. }
  80. int index = 0;
  81. for (auto& resolution : m_resolutions) {
  82. if (resolution == find_size) {
  83. m_selected_resolution = m_resolutions.at(index);
  84. return; // We don't need to do anything else
  85. }
  86. index++;
  87. }
  88. m_selected_resolution = m_resolutions.at(0);
  89. }
  90. void DisplayPropertiesWidget::create_root_widget()
  91. {
  92. m_root_widget = GUI::Widget::construct();
  93. m_root_widget->set_layout(make<GUI::VerticalBoxLayout>());
  94. m_root_widget->set_fill_with_background_color(true);
  95. m_root_widget->layout()->set_margins({ 4, 4, 4, 16 });
  96. }
  97. void DisplayPropertiesWidget::create_wallpaper_list()
  98. {
  99. m_selected_wallpaper = m_wm_config->read_entry("Background", "Wallpaper");
  100. if (!m_selected_wallpaper.is_empty()) {
  101. auto name_parts = m_selected_wallpaper.split('/');
  102. m_selected_wallpaper = name_parts[2];
  103. }
  104. Core::DirIterator iterator("/res/wallpapers/", Core::DirIterator::Flags::SkipDots);
  105. while (iterator.has_next()) {
  106. m_wallpapers.append(iterator.next_path());
  107. }
  108. }
  109. void DisplayPropertiesWidget::create_frame()
  110. {
  111. auto tab_widget = m_root_widget->add<GUI::TabWidget>();
  112. auto wallpaper_splitter = tab_widget->add_tab<GUI::VerticalSplitter>("Wallpaper");
  113. auto wallpaper_content = wallpaper_splitter->add<GUI::Widget>();
  114. wallpaper_content->set_layout(make<GUI::VerticalBoxLayout>());
  115. wallpaper_content->layout()->set_margins({ 4, 4, 4, 4 });
  116. m_wallpaper_preview = wallpaper_splitter->add<GUI::Label>();
  117. auto wallpaper_list = wallpaper_content->add<GUI::ListView>();
  118. wallpaper_list->set_background_color(Color::White);
  119. wallpaper_list->set_model(*ItemListModel<AK::String>::create(m_wallpapers));
  120. auto wallpaper_model = wallpaper_list->model();
  121. auto find_first_wallpaper_index = m_wallpapers.find_first_index(m_selected_wallpaper);
  122. ASSERT(find_first_wallpaper_index.has_value());
  123. auto wallpaper_index_in_model = wallpaper_model->index(find_first_wallpaper_index.value(), wallpaper_list->model_column());
  124. if (wallpaper_model->is_valid(wallpaper_index_in_model))
  125. wallpaper_list->selection().set(wallpaper_index_in_model);
  126. wallpaper_list->horizontal_scrollbar().set_visible(false);
  127. wallpaper_list->on_selection = [this](auto& index) {
  128. StringBuilder builder;
  129. m_selected_wallpaper = m_wallpapers.at(index.row());
  130. builder.append("/res/wallpapers/");
  131. builder.append(m_selected_wallpaper);
  132. m_wallpaper_preview->set_icon(Gfx::Bitmap::load_from_file(builder.to_string()));
  133. m_wallpaper_preview->set_should_stretch_icon(true);
  134. };
  135. auto settings_splitter = tab_widget->add_tab<GUI::VerticalSplitter>("Settings");
  136. auto settings_content = settings_splitter->add<GUI::Widget>();
  137. settings_content->set_layout(make<GUI::VerticalBoxLayout>());
  138. settings_content->layout()->set_margins({ 4, 4, 4, 4 });
  139. auto resolution_list = settings_content->add<GUI::ListView>();
  140. resolution_list->set_background_color(Color::White);
  141. resolution_list->set_model(*ItemListModel<Gfx::Size>::create(m_resolutions));
  142. auto resolution_model = resolution_list->model();
  143. auto find_first_resolution_index = m_resolutions.find_first_index(m_selected_resolution);
  144. ASSERT(find_first_resolution_index.has_value());
  145. auto resolution_index_in_model = resolution_model->index(find_first_resolution_index.value(), resolution_list->model_column());
  146. if (resolution_model->is_valid(resolution_index_in_model))
  147. resolution_list->selection().set(resolution_index_in_model);
  148. resolution_list->horizontal_scrollbar().set_visible(false);
  149. resolution_list->on_selection = [this](auto& index) {
  150. m_selected_resolution = m_resolutions.at(index.row());
  151. };
  152. settings_content->layout()->add_spacer();
  153. // Add the apply and cancel buttons
  154. auto bottom_widget = m_root_widget->add<GUI::Widget>();
  155. bottom_widget->set_layout(make<GUI::HorizontalBoxLayout>());
  156. bottom_widget->layout()->add_spacer();
  157. bottom_widget->set_size_policy(Orientation::Vertical, GUI::SizePolicy::Fixed);
  158. bottom_widget->set_preferred_size(1, 22);
  159. auto apply_button = bottom_widget->add<GUI::Button>();
  160. apply_button->set_text("Apply");
  161. apply_button->set_size_policy(Orientation::Vertical, GUI::SizePolicy::Fixed);
  162. apply_button->set_size_policy(Orientation::Horizontal, GUI::SizePolicy::Fixed);
  163. apply_button->set_preferred_size(60, 22);
  164. apply_button->on_click = [this, tab_widget](GUI::Button&) {
  165. send_settings_to_window_server(tab_widget->active_tab_index());
  166. };
  167. auto ok_button = bottom_widget->add<GUI::Button>();
  168. ok_button->set_text("OK");
  169. ok_button->set_size_policy(Orientation::Vertical, GUI::SizePolicy::Fixed);
  170. ok_button->set_size_policy(Orientation::Horizontal, GUI::SizePolicy::Fixed);
  171. ok_button->set_preferred_size(60, 22);
  172. ok_button->on_click = [this, tab_widget](GUI::Button&) {
  173. send_settings_to_window_server(tab_widget->active_tab_index());
  174. GUI::Application::the().quit();
  175. };
  176. auto cancel_button = bottom_widget->add<GUI::Button>();
  177. cancel_button->set_text("Cancel");
  178. cancel_button->set_size_policy(Orientation::Vertical, GUI::SizePolicy::Fixed);
  179. cancel_button->set_size_policy(Orientation::Horizontal, GUI::SizePolicy::Fixed);
  180. cancel_button->set_preferred_size(60, 22);
  181. cancel_button->on_click = [](auto&) {
  182. GUI::Application::the().quit();
  183. };
  184. }
  185. void DisplayPropertiesWidget::send_settings_to_window_server(int tab_index)
  186. {
  187. if (tab_index == TabIndices::Wallpaper) {
  188. StringBuilder builder;
  189. builder.append("/res/wallpapers/");
  190. builder.append(m_selected_wallpaper);
  191. GUI::Desktop::the().set_wallpaper(builder.to_string());
  192. } else if (tab_index == TabIndices::Settings) {
  193. dbg() << "Attempting to set resolution " << m_selected_resolution;
  194. GUI::WindowServerConnection::the().send_sync<Messages::WindowServer::SetResolution>(m_selected_resolution);
  195. } else {
  196. dbg() << "Invalid tab index " << tab_index;
  197. }
  198. }