diff --git a/Userland/Applications/ThemeEditor/ThemeEditor.gml b/Userland/Applications/ThemeEditor/ThemeEditor.gml index ab47b4499c..9e3aab90f2 100644 --- a/Userland/Applications/ThemeEditor/ThemeEditor.gml +++ b/Userland/Applications/ThemeEditor/ThemeEditor.gml @@ -25,6 +25,24 @@ } } + @GUI::GroupBox { + layout: @GUI::HorizontalBoxLayout { + margins: [4, 4, 4, 4] + } + shrink_to_fit: true + title: "Alignments" + + @GUI::ComboBox { + name: "alignment_combo_box" + model_only: true + fixed_width: 230 + } + + @GUI::ComboBox { + name: "alignment_input" + } + } + @GUI::GroupBox { layout: @GUI::HorizontalBoxLayout { margins: [4, 4, 4, 4] diff --git a/Userland/Applications/ThemeEditor/main.cpp b/Userland/Applications/ThemeEditor/main.cpp index b6b67792a0..19df0a644c 100644 --- a/Userland/Applications/ThemeEditor/main.cpp +++ b/Userland/Applications/ThemeEditor/main.cpp @@ -3,6 +3,7 @@ * Copyright (c) 2021, Jakob-Niklas See * Copyright (c) 2021, Sam Atkins * Copyright (c) 2021, Antonio Di Stefano + * Copyright (c) 2022, Filiph Sandström * * SPDX-License-Identifier: BSD-2-Clause */ @@ -50,6 +51,58 @@ public: } }; +struct AlignmentValue { + String title; + Gfx::TextAlignment setting_value; +}; + +class AlignmentModel final : public GUI::Model { + +public: + AlignmentModel() + { + m_alignments.empend("Center", Gfx::TextAlignment::Center); + m_alignments.empend("Left", Gfx::TextAlignment::CenterLeft); + m_alignments.empend("Right", Gfx::TextAlignment::CenterRight); + } + + virtual ~AlignmentModel() = default; + + virtual int row_count(GUI::ModelIndex const& = GUI::ModelIndex()) const override { return 3; } + virtual int column_count(GUI::ModelIndex const& = GUI::ModelIndex()) const override { return 2; } + + virtual GUI::Variant data(GUI::ModelIndex const& index, GUI::ModelRole role) const override + { + if (role == GUI::ModelRole::Display) + return m_alignments[index.row()].title; + if (role == GUI::ModelRole::Custom) + return m_alignments[index.row()].setting_value; + + return {}; + } + +private: + Vector m_alignments; +}; + +class AlignmentRoleModel final : public GUI::ItemListModel { +public: + explicit AlignmentRoleModel(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(m_data[(size_t)index.row()]); + if (role == GUI::ModelRole::Custom) + return m_data[(size_t)index.row()]; + + return ItemListModel::data(index, role); + } +}; + class FlagRoleModel final : public GUI::ItemListModel { public: explicit FlagRoleModel(Vector const& data) @@ -145,6 +198,11 @@ ErrorOr serenity_main(Main::Arguments arguments) ENUMERATE_COLOR_ROLES(__ENUMERATE_COLOR_ROLE) #undef __ENUMERATE_COLOR_ROLE + Vector alignment_roles; +#define __ENUMERATE_ALIGNMENT_ROLE(role) alignment_roles.append(Gfx::AlignmentRole::role); + ENUMERATE_ALIGNMENT_ROLES(__ENUMERATE_ALIGNMENT_ROLE) +#undef __ENUMERATE_ALIGNMENT_ROLE + Vector flag_roles; #define __ENUMERATE_FLAG_ROLE(role) flag_roles.append(Gfx::FlagRole::role); ENUMERATE_FLAG_ROLES(__ENUMERATE_FLAG_ROLE) @@ -167,10 +225,16 @@ ErrorOr serenity_main(Main::Arguments arguments) ->add(startup_preview_palette); 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& alignment_combo_box = *main_widget->find_descendant_of_type_named("alignment_combo_box"); + auto& alignment_input = *main_widget->find_descendant_of_type_named("alignment_input"); + auto& flag_combo_box = *main_widget->find_descendant_of_type_named("flag_combo_box"); auto& flag_input = *main_widget->find_descendant_of_type_named("flag_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"); + auto& path_combo_box = *main_widget->find_descendant_of_type_named("path_combo_box"); auto& path_input = *main_widget->find_descendant_of_type_named("path_input"); auto& path_picker_button = *main_widget->find_descendant_of_type_named("path_picker_button"); @@ -190,6 +254,24 @@ ErrorOr serenity_main(Main::Arguments arguments) }; color_input.set_color(startup_preview_palette.color(Gfx::ColorRole::Window)); + alignment_combo_box.set_model(adopt_ref(*new AlignmentRoleModel(alignment_roles))); + alignment_combo_box.on_change = [&](auto&, auto& index) { + auto role = index.model()->data(index, GUI::ModelRole::Custom).to_alignment_role(); + alignment_input.set_selected_index((size_t)preview_widget.preview_palette().alignment(role), GUI::AllowCallback::No); + }; + alignment_combo_box.set_selected_index((size_t)Gfx::AlignmentRole::TitleAlignment - 1); + + alignment_input.set_only_allow_values_from_model(true); + alignment_input.set_model(adopt_ref(*new AlignmentModel())); + alignment_input.set_selected_index((size_t)startup_preview_palette.alignment(Gfx::AlignmentRole::TitleAlignment)); + alignment_input.on_change = [&](auto&, auto& index) { + auto role = alignment_combo_box.model()->index(alignment_combo_box.selected_index()).data(GUI::ModelRole::Custom).to_alignment_role(); + auto preview_palette = preview_widget.preview_palette(); + + preview_palette.set_alignment(role, index.data(GUI::ModelRole::Custom).to_text_alignment(Gfx::TextAlignment::CenterLeft)); + preview_widget.set_preview_palette(preview_palette); + }; + flag_combo_box.set_model(adopt_ref(*new FlagRoleModel(flag_roles))); flag_combo_box.on_change = [&](auto&, auto& index) { auto role = index.model()->data(index, GUI::ModelRole::Custom).to_flag_role();