diff options
author | crupest <crupest@outlook.com> | 2022-02-14 22:46:54 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-02-14 22:46:54 +0800 |
commit | 6593a25baa01687b9462f792ce91bd46909df229 (patch) | |
tree | e9ee5ae8d67ef68fdfccca7e07c7d4b26f20c1d7 /src/theme_builder | |
parent | 091c0419a254a28f80963193b36bf0a7f30d6590 (diff) | |
download | cru-6593a25baa01687b9462f792ce91bd46909df229.tar.gz cru-6593a25baa01687b9462f792ce91bd46909df229.tar.bz2 cru-6593a25baa01687b9462f792ce91bd46909df229.zip |
...
Diffstat (limited to 'src/theme_builder')
11 files changed, 136 insertions, 104 deletions
diff --git a/src/theme_builder/components/properties/ColorPropertyEditor.cpp b/src/theme_builder/components/properties/ColorPropertyEditor.cpp index 2f4e7762..7c3b9b46 100644 --- a/src/theme_builder/components/properties/ColorPropertyEditor.cpp +++ b/src/theme_builder/components/properties/ColorPropertyEditor.cpp @@ -21,7 +21,7 @@ ColorPropertyEditor::ColorPropertyEditor() { color_ = *color; color_cube_brush_->SetColor(*color); is_color_text_valid_ = true; - color_change_event_.Raise(*color); + change_event_.Raise(nullptr); } else { is_color_text_valid_ = false; // TODO: Show error! @@ -31,10 +31,11 @@ ColorPropertyEditor::ColorPropertyEditor() { ColorPropertyEditor::~ColorPropertyEditor() { container_.RemoveFromParent(); } -void ColorPropertyEditor::SetColor(const ui::Color &color) { +void ColorPropertyEditor::SetValue(const ui::Color &color, + bool trigger_change) { color_cube_brush_->SetColor(color); color_text_.SetText(color.ToString()); is_color_text_valid_ = true; - color_change_event_.Raise(color); + if (trigger_change) change_event_.Raise(nullptr); } } // namespace cru::theme_builder::components::properties diff --git a/src/theme_builder/components/properties/ColorPropertyEditor.h b/src/theme_builder/components/properties/ColorPropertyEditor.h index 3ee0933b..3265e8e9 100644 --- a/src/theme_builder/components/properties/ColorPropertyEditor.h +++ b/src/theme_builder/components/properties/ColorPropertyEditor.h @@ -9,6 +9,8 @@ namespace cru::theme_builder::components::properties { class ColorPropertyEditor : public ui::components::Component { public: + using PropertyType = ui::Color; + ColorPropertyEditor(); ~ColorPropertyEditor() override; @@ -18,10 +20,10 @@ class ColorPropertyEditor : public ui::components::Component { String GetLabel() const { return label_.GetText(); } void SetLabel(String label) { label_.SetText(std::move(label)); } - ui::Color GetColor() const { return color_; } - void SetColor(const ui::Color& color); + ui::Color GetValue() const { return color_; } + void SetValue(const ui::Color& color, bool trigger_change = true); - IEvent<ui::Color>* ColorChangeEvent() { return &color_change_event_; } + IEvent<std::nullptr_t>* ChangeEvent() { return &change_event_; } private: ui::Color color_ = ui::colors::transparent; @@ -33,6 +35,6 @@ class ColorPropertyEditor : public ui::components::Component { ui::controls::TextBox color_text_; bool is_color_text_valid_; - Event<ui::Color> color_change_event_; + Event<std::nullptr_t> change_event_; }; } // namespace cru::theme_builder::components::properties diff --git a/src/theme_builder/components/properties/CornerRadiusPropertyEditor.cpp b/src/theme_builder/components/properties/CornerRadiusPropertyEditor.cpp index 0da7e18c..9dc8d20e 100644 --- a/src/theme_builder/components/properties/CornerRadiusPropertyEditor.cpp +++ b/src/theme_builder/components/properties/CornerRadiusPropertyEditor.cpp @@ -13,40 +13,37 @@ CornerRadiusPropertyEditor::CornerRadiusPropertyEditor() { container_.AddChild(left_bottom_editor_.GetRootControl()); container_.AddChild(right_bottom_editor_.GetRootControl()); - left_top_editor_.PointChangeEvent()->AddHandler( - [this](const ui::Point& point) { - corner_radius_.left_top = point; - corner_radius_change_event_.Raise(corner_radius_); - }); - - right_top_editor_.PointChangeEvent()->AddHandler( - [this](const ui::Point& point) { - corner_radius_.right_top = point; - corner_radius_change_event_.Raise(corner_radius_); - }); - - left_bottom_editor_.PointChangeEvent()->AddHandler( - [this](const ui::Point& point) { - corner_radius_.left_bottom = point; - corner_radius_change_event_.Raise(corner_radius_); - }); - - right_bottom_editor_.PointChangeEvent()->AddHandler( - [this](const ui::Point& point) { - corner_radius_.right_bottom = point; - corner_radius_change_event_.Raise(corner_radius_); - }); + left_top_editor_.ChangeEvent()->AddHandler([this](std::nullptr_t) { + corner_radius_.left_top = left_top_editor_.GetValue(); + change_event_.Raise(nullptr); + }); + + right_top_editor_.ChangeEvent()->AddHandler([this](std::nullptr_t) { + corner_radius_.right_top = left_top_editor_.GetValue(); + change_event_.Raise(nullptr); + }); + + left_bottom_editor_.ChangeEvent()->AddHandler([this](std::nullptr_t) { + corner_radius_.left_bottom = left_bottom_editor_.GetValue(); + change_event_.Raise(nullptr); + }); + + right_bottom_editor_.ChangeEvent()->AddHandler([this](std::nullptr_t) { + corner_radius_.right_bottom = right_bottom_editor_.GetValue(); + change_event_.Raise(nullptr); + }); } CornerRadiusPropertyEditor::~CornerRadiusPropertyEditor() { container_.RemoveFromParent(); } -void CornerRadiusPropertyEditor::SetCornerRadius( - const ui::CornerRadius& corner_radius) { - left_top_editor_.SetPoint(corner_radius_.left_top); - right_top_editor_.SetPoint(corner_radius_.right_top); - left_bottom_editor_.SetPoint(corner_radius_.left_bottom); - right_bottom_editor_.SetPoint(corner_radius_.right_bottom); +void CornerRadiusPropertyEditor::SetValue(const ui::CornerRadius& corner_radius, + bool trigger_change) { + left_top_editor_.SetValue(corner_radius_.left_top, false); + right_top_editor_.SetValue(corner_radius_.right_top, false); + left_bottom_editor_.SetValue(corner_radius_.left_bottom, false); + right_bottom_editor_.SetValue(corner_radius_.right_bottom, false); + if (trigger_change) change_event_.Raise(nullptr); } } // namespace cru::theme_builder::components::properties diff --git a/src/theme_builder/components/properties/CornerRadiusPropertyEditor.h b/src/theme_builder/components/properties/CornerRadiusPropertyEditor.h index 04de2adf..06e1f024 100644 --- a/src/theme_builder/components/properties/CornerRadiusPropertyEditor.h +++ b/src/theme_builder/components/properties/CornerRadiusPropertyEditor.h @@ -1,22 +1,24 @@ #pragma once #include "PointPropertyEditor.h" +#include "cru/ui/Base.h" #include "cru/ui/components/Component.h" #include "cru/ui/controls/FlexLayout.h" namespace cru::theme_builder::components::properties { class CornerRadiusPropertyEditor : public ui::components::Component { public: + using PropertyType = ui::CornerRadius; + CornerRadiusPropertyEditor(); ~CornerRadiusPropertyEditor() override; ui::controls::Control* GetRootControl() override { return &container_; } - ui::CornerRadius GetCornerRadius() const { return corner_radius_; } - void SetCornerRadius(const ui::CornerRadius& corner_radius); + ui::CornerRadius GetValue() const { return corner_radius_; } + void SetValue(const ui::CornerRadius& corner_radius, + bool trigger_change = true); - IEvent<ui::CornerRadius>* CornerRadiusChangeEvent() { - return &corner_radius_change_event_; - } + IEvent<std::nullptr_t>* ChangeEvent() { return &change_event_; } private: ui::CornerRadius corner_radius_; @@ -27,6 +29,6 @@ class CornerRadiusPropertyEditor : public ui::components::Component { PointPropertyEditor left_bottom_editor_; PointPropertyEditor right_bottom_editor_; - Event<ui::CornerRadius> corner_radius_change_event_; + Event<std::nullptr_t> change_event_; }; } // namespace cru::theme_builder::components::properties diff --git a/src/theme_builder/components/properties/OptionalPropertyEditor.h b/src/theme_builder/components/properties/OptionalPropertyEditor.h index e343727f..87ddcaa2 100644 --- a/src/theme_builder/components/properties/OptionalPropertyEditor.h +++ b/src/theme_builder/components/properties/OptionalPropertyEditor.h @@ -3,26 +3,59 @@ #include "cru/ui/controls/CheckBox.h" #include "cru/ui/controls/FlexLayout.h" +#include <optional> + namespace cru::theme_builder::components::properties { template <typename TEditor> class OptionalPropertyEditor : public ui::components::Component { public: + using PropertyType = typename TEditor::PropertyType; + OptionalPropertyEditor() { container_.AddChild(&check_box_); container_.AddChild(editor_->GetRootControl()); + + editor_.ChangeEvent()->AddHandler([this](std::nullptr_t) { + if (IsEnabled()) { + change_event_.Raise(nullptr); + } + }); } ~OptionalPropertyEditor() override { container_.RemoveFromParent(); } ui::controls::Control* GetRootControl() override { return &container_; } bool IsEnabled() const { return check_box_.IsChecked(); } - void SetEnabled(bool enabled) { check_box_.SetChecked(enabled); } + void SetEnabled(bool enabled, bool trigger_change = true) { + check_box_.SetChecked(enabled); + if (trigger_change) { + change_event_.Raise(nullptr); + } + } + + std::optional<PropertyType> GetValue() const { + return IsEnabled() ? editor_.GetValue() : std::nullopt; + } + + void SetValue(std::optional<PropertyType> value, bool trigger_change = true) { + if (value) { + SetEnabled(true, false); + editor_.SetValue(*value); + if (trigger_change) change_event_.Raise(nullptr); + } else { + SetEnabled(false, trigger_change); + } + } TEditor* GetEditor() { return &editor_; } + IEvent<std::nullptr_t>* ChangeEvent() { return &change_event_; } + private: ui::controls::FlexLayout container_; ui::controls::CheckBox check_box_; TEditor editor_; + + Event<std::nullptr_t> change_event_; }; } // namespace cru::theme_builder::components::properties diff --git a/src/theme_builder/components/properties/PointPropertyEditor.cpp b/src/theme_builder/components/properties/PointPropertyEditor.cpp index dde97b79..9b5a5e83 100644 --- a/src/theme_builder/components/properties/PointPropertyEditor.cpp +++ b/src/theme_builder/components/properties/PointPropertyEditor.cpp @@ -16,7 +16,7 @@ PointPropertyEditor::PointPropertyEditor() { auto point = point_mapper->MapFromString(text.ToString()); point_ = point; is_text_valid_ = true; - point_change_event_.Raise(point); + change_event_.Raise(nullptr); } catch (const Exception&) { is_text_valid_ = false; // TODO: Show error! @@ -26,11 +26,12 @@ PointPropertyEditor::PointPropertyEditor() { PointPropertyEditor::~PointPropertyEditor() { container_.RemoveFromParent(); } -void PointPropertyEditor::SetPoint(const ui::Point& point) { +void PointPropertyEditor::SetValue(const ui::Point& point, + bool trigger_change) { point_ = point; text_.SetText(ConvertPointToString(point)); is_text_valid_ = true; - point_change_event_.Raise(point); + if (trigger_change) change_event_.Raise(nullptr); } String PointPropertyEditor::ConvertPointToString(const ui::Point& point) { diff --git a/src/theme_builder/components/properties/PointPropertyEditor.h b/src/theme_builder/components/properties/PointPropertyEditor.h index d1c84c33..e3f52925 100644 --- a/src/theme_builder/components/properties/PointPropertyEditor.h +++ b/src/theme_builder/components/properties/PointPropertyEditor.h @@ -7,6 +7,8 @@ namespace cru::theme_builder::components::properties { class PointPropertyEditor : public ui::components::Component { public: + using PropertyType = ui::Point; + PointPropertyEditor(); ~PointPropertyEditor() override; @@ -16,10 +18,10 @@ class PointPropertyEditor : public ui::components::Component { String GetLabel() const { return label_.GetText(); } void SetLabel(String label) { label_.SetText(std::move(label)); } - ui::Point GetPoint() const { return point_; } - void SetPoint(const ui::Point& point); + ui::Point GetValue() const { return point_; } + void SetValue(const ui::Point& point, bool trigger_change = true); - IEvent<ui::Point>* PointChangeEvent() { return &point_change_event_; } + IEvent<std::nullptr_t>* ChangeEvent() { return &change_event_; } private: static String ConvertPointToString(const ui::Point& point); @@ -32,6 +34,6 @@ class PointPropertyEditor : public ui::components::Component { ui::controls::TextBox text_; bool is_text_valid_; - Event<ui::Point> point_change_event_; + Event<std::nullptr_t> change_event_; }; } // namespace cru::theme_builder::components::properties diff --git a/src/theme_builder/components/properties/ThicknessPropertyEditor.cpp b/src/theme_builder/components/properties/ThicknessPropertyEditor.cpp index 56374e86..2d922b69 100644 --- a/src/theme_builder/components/properties/ThicknessPropertyEditor.cpp +++ b/src/theme_builder/components/properties/ThicknessPropertyEditor.cpp @@ -15,7 +15,7 @@ ThicknessPropertyEditor::ThicknessPropertyEditor() { auto thickness = thickness_mapper->MapFromString(text.ToString()); thickness_ = thickness; is_text_valid_ = true; - thickness_change_event_.Raise(thickness); + change_event_.Raise(nullptr); } catch (const Exception &) { is_text_valid_ = false; // TODO: Show error! @@ -27,11 +27,12 @@ ThicknessPropertyEditor::~ThicknessPropertyEditor() { container_.RemoveFromParent(); } -void ThicknessPropertyEditor::SetThickness(const ui::Thickness &thickness) { +void ThicknessPropertyEditor::SetValue(const ui::Thickness &thickness, + bool trigger_change) { thickness_ = thickness; text_.SetText(Format(u"{} {} {} {}", thickness_.left, thickness_.top, thickness_.right, thickness_.bottom)); is_text_valid_ = true; - thickness_change_event_.Raise(thickness_); + if (trigger_change) change_event_.Raise(nullptr); } } // namespace cru::theme_builder::components::properties diff --git a/src/theme_builder/components/properties/ThicknessPropertyEditor.h b/src/theme_builder/components/properties/ThicknessPropertyEditor.h index 62157b6f..87b160c6 100644 --- a/src/theme_builder/components/properties/ThicknessPropertyEditor.h +++ b/src/theme_builder/components/properties/ThicknessPropertyEditor.h @@ -7,6 +7,8 @@ namespace cru::theme_builder::components::properties { class ThicknessPropertyEditor : public ui::components::Component { public: + using PropertyType = ui::Thickness; + ThicknessPropertyEditor(); ~ThicknessPropertyEditor() override; @@ -15,12 +17,10 @@ class ThicknessPropertyEditor : public ui::components::Component { String GetLabel() const { return label_.GetText(); } void SetLabel(String label) { label_.SetText(std::move(label)); } - ui::Thickness GetThickness() const { return thickness_; } - void SetThickness(const ui::Thickness& thickness); + ui::Thickness GetValue() const { return thickness_; } + void SetValue(const ui::Thickness& thickness, bool trigger_change = true); - IEvent<ui::Thickness>* ThicknessChangeEvent() { - return &thickness_change_event_; - } + IEvent<std::nullptr_t>* ChangeEvent() { return &change_event_; } private: ui::Thickness thickness_; @@ -30,6 +30,6 @@ class ThicknessPropertyEditor : public ui::components::Component { ui::controls::TextBox text_; bool is_text_valid_; - Event<ui::Thickness> thickness_change_event_; + Event<std::nullptr_t> change_event_; }; } // namespace cru::theme_builder::components::properties diff --git a/src/theme_builder/components/styler/BorderStylerEditor.cpp b/src/theme_builder/components/styler/BorderStylerEditor.cpp index 6c87ad32..819e67c0 100644 --- a/src/theme_builder/components/styler/BorderStylerEditor.cpp +++ b/src/theme_builder/components/styler/BorderStylerEditor.cpp @@ -1,5 +1,4 @@ #include "BorderStylerEditor.h" -#include <memory> #include "cru/common/ClonablePtr.h" #include "cru/platform/graphics/Brush.h" #include "cru/platform/graphics/Factory.h" @@ -15,85 +14,81 @@ BorderStylerEditor::BorderStylerEditor() { container_.AddChild(foreground_brush_editor_.GetRootControl()); container_.AddChild(background_brush_editor_.GetRootControl()); - // TODO: Add change listener. + auto connect = [this](IEvent<std::nullptr_t>* event) { + event->AddHandler( + [this](std::nullptr_t) { this->change_event_.Raise(nullptr); }); + }; + + connect(corner_radius_editor_.ChangeEvent()); + connect(thickness_editor_.ChangeEvent()); + connect(brush_editor_.ChangeEvent()); + connect(foreground_brush_editor_.ChangeEvent()); + connect(background_brush_editor_.ChangeEvent()); } BorderStylerEditor::~BorderStylerEditor() { container_.RemoveFromParent(); } -ClonablePtr<ui::style::BorderStyler> BorderStylerEditor::GetStyler() { +ClonablePtr<ui::style::BorderStyler> BorderStylerEditor::GetValue() { auto graphics_factory = platform::gui::IUiApplication::GetInstance()->GetGraphicsFactory(); ui::style::ApplyBorderStyleInfo border_style; - if (corner_radius_editor_.IsEnabled()) { - border_style.border_radius = - corner_radius_editor_.GetEditor()->GetCornerRadius(); - } - - if (thickness_editor_.IsEnabled()) { - border_style.border_thickness = - thickness_editor_.GetEditor()->GetThickness(); - } + border_style.border_radius = corner_radius_editor_.GetValue(); + border_style.border_thickness = thickness_editor_.GetValue(); if (brush_editor_.IsEnabled()) { border_style.border_brush = graphics_factory->CreateSolidColorBrush( - brush_editor_.GetEditor()->GetColor()); + brush_editor_.GetEditor()->GetValue()); } if (foreground_brush_editor_.IsEnabled()) { border_style.foreground_brush = graphics_factory->CreateSolidColorBrush( - foreground_brush_editor_.GetEditor()->GetColor()); + foreground_brush_editor_.GetEditor()->GetValue()); } if (background_brush_editor_.IsEnabled()) { border_style.background_brush = graphics_factory->CreateSolidColorBrush( - background_brush_editor_.GetEditor()->GetColor()); + background_brush_editor_.GetEditor()->GetValue()); } return ui::style::BorderStyler::Create(border_style); } -void BorderStylerEditor::SetStyler( - const ClonablePtr<ui::style::BorderStyler> &styler) { +void BorderStylerEditor::SetValue( + const ClonablePtr<ui::style::BorderStyler>& styler) { Expects(styler); auto border_style = styler->GetBorderStyle(); - corner_radius_editor_.SetEnabled(border_style.border_radius.has_value()); - if (border_style.border_radius.has_value()) { - corner_radius_editor_.GetEditor()->SetCornerRadius( - *border_style.border_radius); - } - - thickness_editor_.SetEnabled(border_style.border_thickness.has_value()); - if (border_style.border_thickness.has_value()) { - thickness_editor_.GetEditor()->SetThickness(*border_style.border_thickness); - } + corner_radius_editor_.SetValue(border_style.border_radius, false); + thickness_editor_.SetValue(border_style.border_thickness, false); - brush_editor_.SetEnabled(border_style.border_brush.has_value()); + brush_editor_.SetEnabled(border_style.border_brush.has_value(), false); if (border_style.border_brush.has_value()) { - brush_editor_.GetEditor()->SetColor( + brush_editor_.GetEditor()->SetValue( std::dynamic_pointer_cast<platform::graphics::ISolidColorBrush>( border_style.border_brush.value()) - ->GetColor()); + ->GetColor(), + false); } - foreground_brush_editor_.SetEnabled( - border_style.foreground_brush.has_value()); + foreground_brush_editor_.SetEnabled(border_style.foreground_brush.has_value(), + false); if (border_style.foreground_brush.has_value()) { - foreground_brush_editor_.GetEditor()->SetColor( + foreground_brush_editor_.GetEditor()->SetValue( std::dynamic_pointer_cast<platform::graphics::ISolidColorBrush>( border_style.foreground_brush.value()) - ->GetColor()); + ->GetColor(), + false); } - background_brush_editor_.SetEnabled( - border_style.background_brush.has_value()); - + background_brush_editor_.SetEnabled(border_style.background_brush.has_value(), + false); if (border_style.background_brush.has_value()) { - background_brush_editor_.GetEditor()->SetColor( + background_brush_editor_.GetEditor()->SetValue( std::dynamic_pointer_cast<platform::graphics::ISolidColorBrush>( border_style.background_brush.value()) - ->GetColor()); + ->GetColor(), + false); } } diff --git a/src/theme_builder/components/styler/BorderStylerEditor.h b/src/theme_builder/components/styler/BorderStylerEditor.h index 16ae64fb..991647e7 100644 --- a/src/theme_builder/components/styler/BorderStylerEditor.h +++ b/src/theme_builder/components/styler/BorderStylerEditor.h @@ -18,12 +18,10 @@ class BorderStylerEditor : public ui::components::Component { ui::controls::Control* GetRootControl() override { return nullptr; } - ClonablePtr<ui::style::BorderStyler> GetStyler(); - void SetStyler(const ClonablePtr<ui::style::BorderStyler>& styler); + ClonablePtr<ui::style::BorderStyler> GetValue(); + void SetValue(const ClonablePtr<ui::style::BorderStyler>& styler); - IEvent<ClonablePtr<ui::style::BorderStyler>>* ChangeEvent() { - return &change_event_; - } + IEvent<std::nullptr_t>* ChangeEvent() { return &change_event_; } private: ui::controls::FlexLayout container_; @@ -38,6 +36,6 @@ class BorderStylerEditor : public ui::components::Component { properties::OptionalPropertyEditor<properties::ColorPropertyEditor> background_brush_editor_; - Event<ClonablePtr<ui::style::BorderStyler>> change_event_; + Event<std::nullptr_t> change_event_; }; } // namespace cru::theme_builder::components::styler |