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