DisplayProperties.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include <AK/StringBuilder.h>
  2. #include <LibCore/CDirIterator.h>
  3. #include <LibGUI/GAction.h>
  4. #include <LibGUI/GApplication.h>
  5. #include <LibGUI/GBoxLayout.h>
  6. #include <LibGUI/GButton.h>
  7. #include <LibGUI/GDesktop.h>
  8. #include <LibGUI/GEventLoop.h>
  9. #include <LibGUI/GFileSystemModel.h>
  10. #include <LibGUI/GGroupBox.h>
  11. #include <LibGUI/GListView.h>
  12. #include <LibGUI/GScrollBar.h>
  13. #include <LibGUI/GSplitter.h>
  14. #include <LibGUI/GTabWidget.h>
  15. #include <LibGUI/GToolBar.h>
  16. #include <LibGUI/GWidget.h>
  17. #include <LibGUI/GWindow.h>
  18. #include <Servers/WindowServer/WSWindowManager.h>
  19. #include "DisplayProperties.h"
  20. #include "ItemListModel.h"
  21. DisplayPropertiesWidget::DisplayPropertiesWidget()
  22. : m_wm_config(CConfigFile::get_for_app("WindowManager"))
  23. {
  24. create_root_widget();
  25. create_frame();
  26. create_resolution_list();
  27. create_wallpaper_list();
  28. }
  29. void DisplayPropertiesWidget::create_resolution_list()
  30. {
  31. // TODO: Find a better way to get the default resolution
  32. m_resolutions.append({ 640, 480 });
  33. m_resolutions.append({ 800, 600 });
  34. m_resolutions.append({ 1024, 768 });
  35. m_resolutions.append({ 1280, 1024 });
  36. m_resolutions.append({ 1366, 768 });
  37. m_resolutions.append({ 1440, 900 });
  38. m_resolutions.append({ 1600, 900 });
  39. m_resolutions.append({ 1920, 1080 });
  40. m_resolutions.append({ 2560, 1080 });
  41. m_selected_resolution = m_resolutions.at(0);
  42. }
  43. void DisplayPropertiesWidget::create_root_widget()
  44. {
  45. m_root_widget = new GWidget;
  46. m_root_widget->set_layout(make<GBoxLayout>(Orientation::Vertical));
  47. m_root_widget->set_fill_with_background_color(true);
  48. m_root_widget->layout()->set_margins({ 4, 4, 4, 16 });
  49. }
  50. void DisplayPropertiesWidget::create_wallpaper_list()
  51. {
  52. CDirIterator iterator("/res/wallpapers/", CDirIterator::Flags::SkipDots);
  53. while (iterator.has_next())
  54. m_wallpapers.append(iterator.next_path());
  55. }
  56. void DisplayPropertiesWidget::create_frame()
  57. {
  58. auto* tab_widget = new GTabWidget(m_root_widget);
  59. // First, let's create the "Background" tab
  60. auto* background_splitter = new GSplitter(Orientation::Vertical, nullptr);
  61. tab_widget->add_widget("Wallpaper", background_splitter);
  62. auto* background_content = new GWidget(background_splitter);
  63. background_content->set_layout(make<GBoxLayout>(Orientation::Vertical));
  64. background_content->layout()->add_spacer();
  65. background_content->layout()->set_margins({ 4, 4, 4, 4 });
  66. auto* wallpaper_list = new GListView(background_content);
  67. wallpaper_list->set_background_color(Color::White);
  68. wallpaper_list->set_model(*ItemListModel<AK::String>::create(m_wallpapers));
  69. wallpaper_list->horizontal_scrollbar().set_visible(false);
  70. wallpaper_list->on_selection = [this](auto& index) {
  71. m_selected_wallpaper = m_wallpapers.at(index.row());
  72. };
  73. // Let's add the settings tab
  74. auto* settings_splitter = new GSplitter(Orientation::Vertical, nullptr);
  75. tab_widget->add_widget("Settings", settings_splitter);
  76. auto* settings_content = new GWidget(settings_splitter);
  77. settings_content->set_layout(make<GBoxLayout>(Orientation::Vertical));
  78. settings_content->layout()->add_spacer();
  79. settings_content->layout()->set_margins({ 4, 4, 4, 4 });
  80. auto* resolution_list = new GListView(settings_content);
  81. resolution_list->set_background_color(Color::White);
  82. resolution_list->set_model(*ItemListModel<Size>::create(m_resolutions));
  83. resolution_list->horizontal_scrollbar().set_visible(false);
  84. resolution_list->on_selection = [this](auto& index) {
  85. m_selected_resolution = m_resolutions.at(index.row());
  86. };
  87. // Add the apply and cancel buttons
  88. auto* bottom_widget = new GWidget(m_root_widget);
  89. bottom_widget->set_layout(make<GBoxLayout>(Orientation::Horizontal));
  90. bottom_widget->layout()->add_spacer();
  91. bottom_widget->set_size_policy(Orientation::Vertical, SizePolicy::Fixed);
  92. bottom_widget->set_preferred_size(1, 22);
  93. auto* apply_button = new GButton(bottom_widget);
  94. apply_button->set_text("Apply");
  95. apply_button->set_size_policy(Orientation::Vertical, SizePolicy::Fixed);
  96. apply_button->set_size_policy(Orientation::Horizontal, SizePolicy::Fixed);
  97. apply_button->set_preferred_size(60, 22);
  98. apply_button->on_click = [this, tab_widget](GButton&) {
  99. send_settings_to_window_server(tab_widget->active_tab_index());
  100. };
  101. auto* ok_button = new GButton(bottom_widget);
  102. ok_button->set_text("OK");
  103. ok_button->set_size_policy(Orientation::Vertical, SizePolicy::Fixed);
  104. ok_button->set_size_policy(Orientation::Horizontal, SizePolicy::Fixed);
  105. ok_button->set_preferred_size(60, 22);
  106. ok_button->on_click = [this, tab_widget](GButton&) {
  107. send_settings_to_window_server(tab_widget->active_tab_index());
  108. GApplication::the().quit();
  109. };
  110. auto* cancel_button = new GButton(bottom_widget);
  111. cancel_button->set_text("Cancel");
  112. cancel_button->set_size_policy(Orientation::Vertical, SizePolicy::Fixed);
  113. cancel_button->set_size_policy(Orientation::Horizontal, SizePolicy::Fixed);
  114. cancel_button->set_preferred_size(60, 22);
  115. cancel_button->on_click = [this](GButton&) {
  116. GApplication::the().quit();
  117. };
  118. }
  119. void DisplayPropertiesWidget::send_settings_to_window_server(int tab_index)
  120. {
  121. if (tab_index == TabIndices::Wallpaper) {
  122. StringBuilder builder;
  123. builder.append("/res/wallpapers/");
  124. builder.append(m_selected_wallpaper);
  125. GDesktop::the().set_wallpaper(builder.to_string());
  126. } else if (tab_index == TabIndices::Settings) {
  127. WSAPI_ClientMessage request;
  128. request.type = WSAPI_ClientMessage::Type::SetResolution;
  129. dbg() << "Attempting to set resolution " << m_selected_resolution;
  130. request.wm_conf.resolution = { m_selected_resolution.width(), m_selected_resolution.height() };
  131. auto response = GWindowServerConnection::the().sync_request(request, WSAPI_ServerMessage::Type::DidSetResolution);
  132. ASSERT(response.value == 1);
  133. } else {
  134. dbg() << "Invalid tab index " << tab_index;
  135. }
  136. }