From 684f8a6b15b9f8681d8dce6b6e9b694d26f3887f Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Wed, 27 Oct 2021 16:59:59 +0100 Subject: [PATCH] ThemeEditor: Add MetricRole editing The editing UI at the bottom is now split into two groups, one for colors and one for metrics. --- .../Applications/ThemeEditor/ThemeEditor.gml | 26 +++++++- Userland/Applications/ThemeEditor/main.cpp | 63 +++++++++++++++---- 2 files changed, 75 insertions(+), 14 deletions(-) diff --git a/Userland/Applications/ThemeEditor/ThemeEditor.gml b/Userland/Applications/ThemeEditor/ThemeEditor.gml index 31621f989bc..91348e199d7 100644 --- a/Userland/Applications/ThemeEditor/ThemeEditor.gml +++ b/Userland/Applications/ThemeEditor/ThemeEditor.gml @@ -7,9 +7,12 @@ name: "preview_frame" } - @GUI::Widget { - layout: @GUI::HorizontalBoxLayout - fixed_height: 20 + @GUI::GroupBox { + layout: @GUI::HorizontalBoxLayout { + margins: [16, 8, 8, 8] + } + shrink_to_fit: true + title: "Colors" @GUI::ComboBox { name: "color_combo_box" @@ -20,4 +23,21 @@ name: "color_input" } } + + @GUI::GroupBox { + layout: @GUI::HorizontalBoxLayout { + margins: [16, 8, 8, 8] + } + shrink_to_fit: true + title: "Metrics" + + @GUI::ComboBox { + name: "metric_combo_box" + model_only: true + } + + @GUI::SpinBox { + name: "metric_input" + } + } } diff --git a/Userland/Applications/ThemeEditor/main.cpp b/Userland/Applications/ThemeEditor/main.cpp index 2639f70a4f5..2cf4224f54a 100644 --- a/Userland/Applications/ThemeEditor/main.cpp +++ b/Userland/Applications/ThemeEditor/main.cpp @@ -1,6 +1,7 @@ /* * Copyright (c) 2020, Andreas Kling * Copyright (c) 2021, Jakob-Niklas See + * Copyright (c) 2021, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -18,6 +19,8 @@ #include #include #include +#include +#include #include #include @@ -39,6 +42,24 @@ public: } }; +class MetricRoleModel final : public GUI::ItemListModel { +public: + explicit MetricRoleModel(Vector const& data) + : ItemListModel(data) + { + } + + virtual GUI::Variant data(GUI::ModelIndex const& index, GUI::ModelRole role) const override + { + if (role == GUI::ModelRole::Display) + return Gfx::to_string(static_cast(m_data[(size_t)index.row()])); + if (role == GUI::ModelRole::Custom) + return m_data[(size_t)index.row()]; + + return ItemListModel::data(index, role); + } +}; + int main(int argc, char** argv) { if (pledge("stdio recvfd sendfd thread rpath cpath wpath unix", nullptr) < 0) { @@ -98,30 +119,51 @@ int main(int argc, char** argv) ENUMERATE_COLOR_ROLES(__ENUMERATE_COLOR_ROLE) #undef __ENUMERATE_COLOR_ROLE + Vector metric_roles; +#define __ENUMERATE_METRIC_ROLE(role) metric_roles.append(Gfx::MetricRole::role); + ENUMERATE_METRIC_ROLES(__ENUMERATE_METRIC_ROLE) +#undef __ENUMERATE_METRIC_ROLE + auto& main_widget = window->set_main_widget(); main_widget.load_from_gml(theme_editor_gml); auto& preview_widget = main_widget.find_descendant_of_type_named("preview_frame") ->add(startup_preview_palette); - auto& combo_box = *main_widget.find_descendant_of_type_named("color_combo_box"); + auto& color_combo_box = *main_widget.find_descendant_of_type_named("color_combo_box"); auto& color_input = *main_widget.find_descendant_of_type_named("color_input"); + auto& metric_combo_box = *main_widget.find_descendant_of_type_named("metric_combo_box"); + auto& metric_input = *main_widget.find_descendant_of_type_named("metric_input"); - combo_box.set_model(adopt_ref(*new ColorRoleModel(color_roles))); - combo_box.on_change = [&](auto&, auto& index) { + color_combo_box.set_model(adopt_ref(*new ColorRoleModel(color_roles))); + color_combo_box.on_change = [&](auto&, auto& index) { auto role = index.model()->data(index, GUI::ModelRole::Custom).to_color_role(); color_input.set_color(preview_widget.preview_palette().color(role)); }; - - combo_box.set_selected_index((size_t)Gfx::ColorRole::Window - 1); + color_combo_box.set_selected_index((size_t)Gfx::ColorRole::Window - 1); color_input.on_change = [&] { - auto role = combo_box.model()->index(combo_box.selected_index()).data(GUI::ModelRole::Custom).to_color_role(); + auto role = color_combo_box.model()->index(color_combo_box.selected_index()).data(GUI::ModelRole::Custom).to_color_role(); auto preview_palette = preview_widget.preview_palette(); preview_palette.set_color(role, color_input.color()); preview_widget.set_preview_palette(preview_palette); }; color_input.set_color(startup_preview_palette.color(Gfx::ColorRole::Window)); + metric_combo_box.set_model(adopt_ref(*new MetricRoleModel(metric_roles))); + metric_combo_box.on_change = [&](auto&, auto& index) { + auto role = index.model()->data(index, GUI::ModelRole::Custom).to_metric_role(); + metric_input.set_value(preview_widget.preview_palette().metric(role), GUI::AllowCallback::No); + }; + metric_combo_box.set_selected_index((size_t)Gfx::MetricRole::TitleButtonHeight - 1); + + metric_input.on_change = [&](int value) { + auto role = metric_combo_box.model()->index(metric_combo_box.selected_index()).data(GUI::ModelRole::Custom).to_metric_role(); + auto preview_palette = preview_widget.preview_palette(); + preview_palette.set_metric(role, value); + preview_widget.set_preview_palette(preview_palette); + }; + metric_input.set_value(startup_preview_palette.metric(Gfx::MetricRole::TitleButtonHeight), GUI::AllowCallback::No); + auto& file_menu = window->add_menu("&File"); auto update_window_title = [&] { @@ -145,10 +187,9 @@ int main(int argc, char** argv) theme->write_entry("Colors", to_string(role), preview_widget.preview_palette().color(role).to_string()); } -#define __ENUMERATE_METRIC_ROLE(role) \ - theme->write_num_entry("Metrics", #role, preview_widget.preview_palette().metric(Gfx::MetricRole::role)); - ENUMERATE_METRIC_ROLES(__ENUMERATE_METRIC_ROLE) -#undef __ENUMERATE_METRIC_ROLE + for (auto role : metric_roles) { + theme->write_num_entry("Metrics", to_string(role), preview_widget.preview_palette().metric(role)); + } #define __ENUMERATE_PATH_ROLE(role) \ theme->write_entry("Paths", #role, preview_widget.preview_palette().path(Gfx::PathRole::role)); @@ -186,7 +227,7 @@ int main(int argc, char** argv) update_window_title(); - window->resize(480, 385); + window->resize(480, 460); window->set_resizable(false); window->show(); window->set_icon(app_icon.bitmap_for_size(16));