MainWidget.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include "PreviewWidget.h"
  8. #include <AK/FixedArray.h>
  9. #include <AK/Time.h>
  10. #include <LibGUI/CheckBox.h>
  11. #include <LibGUI/ColorInput.h>
  12. #include <LibGUI/ComboBox.h>
  13. #include <LibGUI/SpinBox.h>
  14. #include <LibGUI/TabWidget.h>
  15. #include <LibGUI/TextBox.h>
  16. #include <LibGUI/Window.h>
  17. #include <LibGfx/SystemTheme.h>
  18. namespace ThemeEditor {
  19. class AlignmentModel final : public GUI::Model {
  20. public:
  21. static ErrorOr<NonnullRefPtr<AlignmentModel>> try_create()
  22. {
  23. return adopt_nonnull_ref_or_enomem(new (nothrow) AlignmentModel());
  24. }
  25. virtual ~AlignmentModel() = default;
  26. virtual int row_count(GUI::ModelIndex const& = GUI::ModelIndex()) const override { return 3; }
  27. virtual int column_count(GUI::ModelIndex const& = GUI::ModelIndex()) const override { return 2; }
  28. virtual GUI::Variant data(GUI::ModelIndex const& index, GUI::ModelRole role) const override
  29. {
  30. if (role == GUI::ModelRole::Display)
  31. return m_alignments[index.row()].title;
  32. if (role == GUI::ModelRole::Custom)
  33. return m_alignments[index.row()].setting_value;
  34. return {};
  35. }
  36. size_t index_of(Gfx::TextAlignment alignment) const
  37. {
  38. auto match = m_alignments.find_if([&](auto& it) { return it.setting_value == alignment; });
  39. return match.index();
  40. }
  41. private:
  42. AlignmentModel() = default;
  43. struct AlignmentValue {
  44. DeprecatedString title;
  45. Gfx::TextAlignment setting_value;
  46. };
  47. Vector<AlignmentValue> m_alignments {
  48. { "Center", Gfx::TextAlignment::Center },
  49. { "Left", Gfx::TextAlignment::CenterLeft },
  50. { "Right", Gfx::TextAlignment::CenterRight },
  51. };
  52. };
  53. struct Property {
  54. Variant<Gfx::AlignmentRole, Gfx::ColorRole, Gfx::FlagRole, Gfx::MetricRole, Gfx::PathRole> role;
  55. };
  56. struct PropertyGroup {
  57. DeprecatedString title;
  58. Vector<Property> properties;
  59. };
  60. struct PropertyTab {
  61. DeprecatedString title;
  62. Vector<PropertyGroup> property_groups;
  63. };
  64. class MainWidget final : public GUI::Widget {
  65. C_OBJECT(MainWidget);
  66. public:
  67. virtual ~MainWidget() override = default;
  68. ErrorOr<void> initialize_menubar(GUI::Window&);
  69. GUI::Window::CloseRequestDecision request_close();
  70. void update_title();
  71. ErrorOr<void> load_from_file(Core::File&);
  72. private:
  73. MainWidget();
  74. void save_to_file(Core::File&);
  75. ErrorOr<Core::AnonymousBuffer> encode();
  76. void set_path(DeprecatedString);
  77. void build_override_controls();
  78. void add_property_tab(PropertyTab const&);
  79. void set_alignment(Gfx::AlignmentRole, Gfx::TextAlignment);
  80. void set_color(Gfx::ColorRole, Gfx::Color);
  81. void set_flag(Gfx::FlagRole, bool);
  82. void set_metric(Gfx::MetricRole, int);
  83. void set_path(Gfx::PathRole, DeprecatedString);
  84. void set_palette(Gfx::Palette);
  85. enum class PathPickerTarget {
  86. File,
  87. Folder,
  88. };
  89. void show_path_picker_dialog(StringView property_display_name, GUI::TextBox&, PathPickerTarget);
  90. RefPtr<PreviewWidget> m_preview_widget;
  91. RefPtr<GUI::TabWidget> m_property_tabs;
  92. RefPtr<GUI::Action> m_save_action;
  93. RefPtr<GUI::Button> m_theme_override_apply;
  94. RefPtr<GUI::Button> m_theme_override_reset;
  95. Optional<DeprecatedString> m_path;
  96. Gfx::Palette m_current_palette;
  97. Time m_last_modified_time { Time::now_monotonic() };
  98. RefPtr<AlignmentModel> m_alignment_model;
  99. Array<RefPtr<GUI::ComboBox>, to_underlying(Gfx::AlignmentRole::__Count)> m_alignment_inputs;
  100. Array<RefPtr<GUI::ColorInput>, to_underlying(Gfx::ColorRole::__Count)> m_color_inputs;
  101. Array<RefPtr<GUI::CheckBox>, to_underlying(Gfx::FlagRole::__Count)> m_flag_inputs;
  102. Array<RefPtr<GUI::SpinBox>, to_underlying(Gfx::MetricRole::__Count)> m_metric_inputs;
  103. Array<RefPtr<GUI::TextBox>, to_underlying(Gfx::PathRole::__Count)> m_path_inputs;
  104. OwnPtr<GUI::ActionGroup> m_preview_type_action_group;
  105. };
  106. }