DisplayProperties.cpp 9.2 KB

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