From 8df1f6951eaef56cf8b33b8ea9107829f906c8ac Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 30 Dec 2020 02:44:03 +0100 Subject: [PATCH] DisplaySettings: Convert the GUI to GML :^) --- Applications/DisplaySettings/CMakeLists.txt | 3 + .../DisplaySettings/DisplaySettings.cpp | 97 +++------------ .../DisplaySettings/DisplaySettings.h | 2 +- .../DisplaySettings/DisplaySettingsWindow.gml | 117 ++++++++++++++++++ .../DisplaySettings/MonitorWidget.cpp | 5 + Applications/DisplaySettings/MonitorWidget.h | 9 +- 6 files changed, 146 insertions(+), 87 deletions(-) create mode 100644 Applications/DisplaySettings/DisplaySettingsWindow.gml diff --git a/Applications/DisplaySettings/CMakeLists.txt b/Applications/DisplaySettings/CMakeLists.txt index af736e83a8..b541894194 100644 --- a/Applications/DisplaySettings/CMakeLists.txt +++ b/Applications/DisplaySettings/CMakeLists.txt @@ -1,5 +1,8 @@ +compile_gml(DisplaySettingsWindow.gml DisplaySettingsWindowGML.h display_settings_window_gml) + set(SOURCES DisplaySettings.cpp + DisplaySettingsWindowGML.h main.cpp MonitorWidget.cpp ) diff --git a/Applications/DisplaySettings/DisplaySettings.cpp b/Applications/DisplaySettings/DisplaySettings.cpp index 048b5a3758..b3fd395499 100644 --- a/Applications/DisplaySettings/DisplaySettings.cpp +++ b/Applications/DisplaySettings/DisplaySettings.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2019-2020, Jesse Buhagiar + * Copyright (c) 2020, Andreas Kling * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,6 +27,7 @@ #include "DisplaySettings.h" #include +#include #include #include #include @@ -41,6 +43,8 @@ #include #include +REGISTER_WIDGET(DisplaySettings, MonitorWidget) + DisplaySettingsWidget::DisplaySettingsWidget() { create_resolution_list(); @@ -87,34 +91,11 @@ void DisplaySettingsWidget::create_wallpaper_list() void DisplaySettingsWidget::create_frame() { m_root_widget = GUI::Widget::construct(); - m_root_widget->set_layout(); - m_root_widget->set_fill_with_background_color(true); - m_root_widget->layout()->set_margins({ 4, 4, 4, 4 }); + m_root_widget->load_from_gml(display_settings_window_gml); - auto& settings_content = m_root_widget->add(); - settings_content.set_layout(); - settings_content.set_background_color(Color::Blue); - settings_content.set_background_role(Gfx::ColorRole::Window); - settings_content.layout()->set_margins({ 4, 4, 4, 4 }); + m_monitor_widget = static_cast(*m_root_widget->find_descendant_by_name("monitor_widget")); - /// Wallpaper Preview ///////////////////////////////////////////////////////////////////////// - - m_monitor_widget = settings_content.add(); - m_monitor_widget->set_fixed_size(338, 248); - - /// Wallpaper Row ///////////////////////////////////////////////////////////////////////////// - - auto& wallpaper_selection_container = settings_content.add(); - wallpaper_selection_container.set_layout(); - wallpaper_selection_container.layout()->set_margins({ 0, 4, 0, 0 }); - wallpaper_selection_container.set_fixed_height(22); - - auto& wallpaper_label = wallpaper_selection_container.add(); - wallpaper_label.set_text_alignment(Gfx::TextAlignment::CenterLeft); - wallpaper_label.set_fixed_width(70); - wallpaper_label.set_text("Wallpaper:"); - - m_wallpaper_combo = wallpaper_selection_container.add(); + m_wallpaper_combo = static_cast(*m_root_widget->find_descendant_by_name("wallpaper_combo")); m_wallpaper_combo->set_only_allow_values_from_model(true); m_wallpaper_combo->set_model(*GUI::ItemListModel::create(m_wallpapers)); m_wallpaper_combo->on_change = [this](auto& text, const GUI::ModelIndex& index) { @@ -139,11 +120,9 @@ void DisplaySettingsWidget::create_frame() m_monitor_widget->update(); }; - auto& button = wallpaper_selection_container.add(); - button.set_tooltip("Select Wallpaper from file system."); + auto& button = static_cast(*m_root_widget->find_descendant_by_name("wallpaper_open_button")); button.set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png")); button.set_button_style(Gfx::ButtonStyle::CoolBar); - button.set_fixed_size(22, 22); button.on_click = [this](auto) { Optional open_path = GUI::FilePicker::get_open_filepath(root_widget()->window(), "Select wallpaper from file system."); @@ -155,19 +134,7 @@ void DisplaySettingsWidget::create_frame() m_wallpaper_combo->set_only_allow_values_from_model(true); }; - /// Mode ////////////////////////////////////////////////////////////////////////////////////// - - auto& mode_selection_container = settings_content.add(); - mode_selection_container.set_layout(); - mode_selection_container.layout()->set_margins({ 0, 4, 0, 0 }); - mode_selection_container.set_fixed_height(22); - - auto& mode_label = mode_selection_container.add(); - mode_label.set_text_alignment(Gfx::TextAlignment::CenterLeft); - mode_label.set_fixed_width(70); - mode_label.set_text("Mode:"); - - m_mode_combo = mode_selection_container.add(); + m_mode_combo = static_cast(*m_root_widget->find_descendant_by_name("mode_combo")); m_mode_combo->set_only_allow_values_from_model(true); m_mode_combo->set_model(*GUI::ItemListModel::create(m_modes)); m_mode_combo->on_change = [this](auto&, const GUI::ModelIndex& index) { @@ -175,18 +142,7 @@ void DisplaySettingsWidget::create_frame() m_monitor_widget->update(); }; - /// Resolution Row //////////////////////////////////////////////////////////////////////////// - - auto& resolution_selection_container = settings_content.add(); - resolution_selection_container.set_layout(); - resolution_selection_container.set_fixed_height(22); - - auto& m_resolution_label = resolution_selection_container.add(); - m_resolution_label.set_text_alignment(Gfx::TextAlignment::CenterLeft); - m_resolution_label.set_fixed_width(70); - m_resolution_label.set_text("Resolution:"); - - m_resolution_combo = resolution_selection_container.add(); + m_resolution_combo = static_cast(*m_root_widget->find_descendant_by_name("resolution_combo")); m_resolution_combo->set_only_allow_values_from_model(true); m_resolution_combo->set_model(*GUI::ItemListModel::create(m_resolutions)); m_resolution_combo->on_change = [this](auto&, const GUI::ModelIndex& index) { @@ -194,51 +150,26 @@ void DisplaySettingsWidget::create_frame() m_monitor_widget->update(); }; - /// Background Color Row ////////////////////////////////////////////////////////////////////// - - auto& color_selection_container = settings_content.add(); - color_selection_container.set_layout(); - color_selection_container.set_fixed_height(22); - - auto& color_label = color_selection_container.add(); - color_label.set_text_alignment(Gfx::TextAlignment::CenterLeft); - color_label.set_fixed_width(70); - color_label.set_text("Color:"); - - m_color_input = color_selection_container.add(); + m_color_input = static_cast(*m_root_widget->find_descendant_by_name("color_input")); m_color_input->set_color_has_alpha_channel(false); - m_color_input->set_fixed_width(90); m_color_input->set_color_picker_title("Select color for desktop"); m_color_input->on_change = [this] { m_monitor_widget->set_background_color(m_color_input->color()); m_monitor_widget->update(); }; - /// Add the apply and cancel buttons ////////////////////////////////////////////////////////// - - auto& bottom_widget = settings_content.add(); - bottom_widget.set_layout(); - bottom_widget.layout()->add_spacer(); - bottom_widget.set_fixed_height(22); - - auto& ok_button = bottom_widget.add(); - ok_button.set_text("OK"); - ok_button.set_fixed_size(60, 22); + auto& ok_button = static_cast(*m_root_widget->find_descendant_by_name("ok_button")); ok_button.on_click = [this](auto) { send_settings_to_window_server(); GUI::Application::the()->quit(); }; - auto& cancel_button = bottom_widget.add(); - cancel_button.set_text("Cancel"); - cancel_button.set_fixed_size(60, 22); + auto& cancel_button = static_cast(*m_root_widget->find_descendant_by_name("cancel_button")); cancel_button.on_click = [](auto) { GUI::Application::the()->quit(); }; - auto& apply_button = bottom_widget.add(); - apply_button.set_text("Apply"); - apply_button.set_fixed_size(60, 22); + auto& apply_button = static_cast(*m_root_widget->find_descendant_by_name("apply_button")); apply_button.on_click = [this](auto) { send_settings_to_window_server(); }; diff --git a/Applications/DisplaySettings/DisplaySettings.h b/Applications/DisplaySettings/DisplaySettings.h index fafd7adef9..df3452e278 100644 --- a/Applications/DisplaySettings/DisplaySettings.h +++ b/Applications/DisplaySettings/DisplaySettings.h @@ -50,7 +50,7 @@ private: Vector m_resolutions; RefPtr m_root_widget; - RefPtr m_monitor_widget; + RefPtr m_monitor_widget; RefPtr m_wallpaper_combo; RefPtr m_mode_combo; RefPtr m_resolution_combo; diff --git a/Applications/DisplaySettings/DisplaySettingsWindow.gml b/Applications/DisplaySettings/DisplaySettingsWindow.gml new file mode 100644 index 0000000000..ca65f81025 --- /dev/null +++ b/Applications/DisplaySettings/DisplaySettingsWindow.gml @@ -0,0 +1,117 @@ +@GUI::Widget { + fill_with_background_color: true + + layout: @GUI::VerticalBoxLayout { + margins: [4, 4, 4, 4] + } + + @DisplaySettings::MonitorWidget { + name: "monitor_widget" + fixed_width: 338 + fixed_height: 248 + } + + @GUI::Widget { + fixed_height: 22 + layout: @GUI::HorizontalBoxLayout { + margins: [0, 4, 0, 0] + } + + @GUI::Label { + text: "Wallpaper:" + text_alignment: "CenterLeft" + fixed_width: 70 + } + + @GUI::ComboBox { + name: "wallpaper_combo" + } + + @GUI::Button { + name: "wallpaper_open_button" + tooltip: "Select Wallpaper from file system." + fixed_width: 22 + fixed_height: 22 + } + } + + @GUI::Widget { + fixed_height: 22 + layout: @GUI::HorizontalBoxLayout { + margins: [0, 4, 0, 0] + } + + @GUI::Label { + text: "Modes:" + text_alignment: "CenterLeft" + fixed_width: 70 + } + + @GUI::ComboBox { + name: "mode_combo" + } + } + + @GUI::Widget { + fixed_height: 22 + layout: @GUI::HorizontalBoxLayout { + margins: [0, 4, 0, 0] + } + + @GUI::Label { + text: "Resolution:" + text_alignment: "CenterLeft" + fixed_width: 70 + } + + @GUI::ComboBox { + name: "resolution_combo" + } + } + + + @GUI::Widget { + fixed_height: 22 + layout: @GUI::HorizontalBoxLayout { + margins: [0, 4, 0, 0] + } + + @GUI::Label { + text: "Color:" + text_alignment: "CenterLeft" + fixed_width: 70 + } + + @GUI::ColorInput { + name: "color_input" + fixed_width: 90 + } + } + + @GUI::Widget { + fixed_height: 22 + layout: @GUI::HorizontalBoxLayout { + } + + @GUI::Widget { + } + + @GUI::Button { + name: "ok_button" + text: "OK" + fixed_width: 60 + } + + @GUI::Button { + name: "cancel_button" + text: "Cancel" + fixed_width: 60 + } + + @GUI::Button { + name: "apply_button" + text: "Apply" + fixed_width: 60 + } + } +} diff --git a/Applications/DisplaySettings/MonitorWidget.cpp b/Applications/DisplaySettings/MonitorWidget.cpp index 2ef2e573ea..d6b809d449 100644 --- a/Applications/DisplaySettings/MonitorWidget.cpp +++ b/Applications/DisplaySettings/MonitorWidget.cpp @@ -26,6 +26,9 @@ #include "MonitorWidget.h" #include +#include + +namespace DisplaySettings { MonitorWidget::MonitorWidget() { @@ -111,3 +114,5 @@ void MonitorWidget::paint_event(GUI::PaintEvent& event) painter.draw_text(m_monitor_rect, m_desktop_resolution.to_string(), Gfx::TextAlignment::Center, Color::White); } } + +} diff --git a/Applications/DisplaySettings/MonitorWidget.h b/Applications/DisplaySettings/MonitorWidget.h index 9a43348e2d..1961e91330 100644 --- a/Applications/DisplaySettings/MonitorWidget.h +++ b/Applications/DisplaySettings/MonitorWidget.h @@ -26,15 +26,14 @@ #pragma once -#include "LibGfx/Bitmap.h" #include +namespace DisplaySettings { + class MonitorWidget final : public GUI::Widget { C_OBJECT(MonitorWidget); public: - MonitorWidget(); - bool set_wallpaper(String path); String wallpaper(); @@ -48,6 +47,8 @@ public: Gfx::Color background_color(); private: + MonitorWidget(); + virtual void paint_event(GUI::PaintEvent& event) override; Gfx::IntRect m_monitor_rect; @@ -59,3 +60,5 @@ private: Gfx::IntSize m_desktop_resolution; Gfx::Color m_desktop_color; }; + +}