1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 22:38:13 +00:00

ThemeEditor: Add "alignment" support

This commit is contained in:
Filiph Sandström 2022-01-01 19:19:10 +01:00 committed by Andreas Kling
parent 35dac843b4
commit 022b4a3ed3
2 changed files with 100 additions and 0 deletions

View file

@ -3,6 +3,7 @@
* Copyright (c) 2021, Jakob-Niklas See <git@nwex.de>
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2021, Antonio Di Stefano <tonio9681@gmail.com>
* Copyright (c) 2022, Filiph Sandström <filiph.sandstrom@filfatstudios.com>
*
* 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<AlignmentValue> m_alignments;
};
class AlignmentRoleModel final : public GUI::ItemListModel<Gfx::AlignmentRole> {
public:
explicit AlignmentRoleModel(Vector<Gfx::AlignmentRole> const& data)
: ItemListModel<Gfx::AlignmentRole>(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<Gfx::FlagRole> {
public:
explicit FlagRoleModel(Vector<Gfx::FlagRole> const& data)
@ -145,6 +198,11 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
ENUMERATE_COLOR_ROLES(__ENUMERATE_COLOR_ROLE)
#undef __ENUMERATE_COLOR_ROLE
Vector<Gfx::AlignmentRole> alignment_roles;
#define __ENUMERATE_ALIGNMENT_ROLE(role) alignment_roles.append(Gfx::AlignmentRole::role);
ENUMERATE_ALIGNMENT_ROLES(__ENUMERATE_ALIGNMENT_ROLE)
#undef __ENUMERATE_ALIGNMENT_ROLE
Vector<Gfx::FlagRole> flag_roles;
#define __ENUMERATE_FLAG_ROLE(role) flag_roles.append(Gfx::FlagRole::role);
ENUMERATE_FLAG_ROLES(__ENUMERATE_FLAG_ROLE)
@ -167,10 +225,16 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
->add<ThemeEditor::PreviewWidget>(startup_preview_palette);
auto& color_combo_box = *main_widget->find_descendant_of_type_named<GUI::ComboBox>("color_combo_box");
auto& color_input = *main_widget->find_descendant_of_type_named<GUI::ColorInput>("color_input");
auto& alignment_combo_box = *main_widget->find_descendant_of_type_named<GUI::ComboBox>("alignment_combo_box");
auto& alignment_input = *main_widget->find_descendant_of_type_named<GUI::ComboBox>("alignment_input");
auto& flag_combo_box = *main_widget->find_descendant_of_type_named<GUI::ComboBox>("flag_combo_box");
auto& flag_input = *main_widget->find_descendant_of_type_named<GUI::CheckBox>("flag_input");
auto& metric_combo_box = *main_widget->find_descendant_of_type_named<GUI::ComboBox>("metric_combo_box");
auto& metric_input = *main_widget->find_descendant_of_type_named<GUI::SpinBox>("metric_input");
auto& path_combo_box = *main_widget->find_descendant_of_type_named<GUI::ComboBox>("path_combo_box");
auto& path_input = *main_widget->find_descendant_of_type_named<GUI::TextBox>("path_input");
auto& path_picker_button = *main_widget->find_descendant_of_type_named<GUI::Button>("path_picker_button");
@ -190,6 +254,24 @@ ErrorOr<int> 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();