From 21c1c3281cd4186d9810d68122e9e8e92da714ad Mon Sep 17 00:00:00 2001 From: crupest Date: Mon, 14 Feb 2022 22:51:37 +0800 Subject: ... --- include/cru/common/ClonablePtr.h | 2 +- src/theme_builder/CMakeLists.txt | 1 + .../components/properties/OptionalPropertyEditor.h | 5 +- .../components/styler/BorderStylerEditor.cpp | 95 ---------------------- .../components/styler/BorderStylerEditor.h | 41 ---------- .../components/stylers/BorderStylerEditor.cpp | 95 ++++++++++++++++++++++ .../components/stylers/BorderStylerEditor.h | 41 ++++++++++ 7 files changed, 141 insertions(+), 139 deletions(-) delete mode 100644 src/theme_builder/components/styler/BorderStylerEditor.cpp delete mode 100644 src/theme_builder/components/styler/BorderStylerEditor.h create mode 100644 src/theme_builder/components/stylers/BorderStylerEditor.cpp create mode 100644 src/theme_builder/components/stylers/BorderStylerEditor.h diff --git a/include/cru/common/ClonablePtr.h b/include/cru/common/ClonablePtr.h index 39b5b454..a2a88758 100644 --- a/include/cru/common/ClonablePtr.h +++ b/include/cru/common/ClonablePtr.h @@ -88,7 +88,7 @@ class ClonablePtr { public: pointer get() const noexcept { return ptr_.get(); } - operator bool() const noexcept { return ptr_; } + operator bool() const noexcept { return ptr_ != nullptr; } element_type& operator*() const noexcept { return *ptr_; } pointer operator->() const noexcept { return ptr_.get(); } diff --git a/src/theme_builder/CMakeLists.txt b/src/theme_builder/CMakeLists.txt index 5c57f12b..902173fd 100644 --- a/src/theme_builder/CMakeLists.txt +++ b/src/theme_builder/CMakeLists.txt @@ -9,6 +9,7 @@ add_executable(cru_theme_builder components/properties/PointPropertyEditor.cpp components/properties/TextPropertyEditor.cpp components/properties/ThicknessPropertyEditor.cpp + components/stylers/BorderStylerEditor.cpp ) if(APPLE) diff --git a/src/theme_builder/components/properties/OptionalPropertyEditor.h b/src/theme_builder/components/properties/OptionalPropertyEditor.h index 87ddcaa2..03eb759c 100644 --- a/src/theme_builder/components/properties/OptionalPropertyEditor.h +++ b/src/theme_builder/components/properties/OptionalPropertyEditor.h @@ -13,7 +13,7 @@ class OptionalPropertyEditor : public ui::components::Component { OptionalPropertyEditor() { container_.AddChild(&check_box_); - container_.AddChild(editor_->GetRootControl()); + container_.AddChild(editor_.GetRootControl()); editor_.ChangeEvent()->AddHandler([this](std::nullptr_t) { if (IsEnabled()) { @@ -34,7 +34,8 @@ class OptionalPropertyEditor : public ui::components::Component { } std::optional GetValue() const { - return IsEnabled() ? editor_.GetValue() : std::nullopt; + return IsEnabled() ? std::optional(editor_.GetValue()) + : std::nullopt; } void SetValue(std::optional value, bool trigger_change = true) { diff --git a/src/theme_builder/components/styler/BorderStylerEditor.cpp b/src/theme_builder/components/styler/BorderStylerEditor.cpp deleted file mode 100644 index 819e67c0..00000000 --- a/src/theme_builder/components/styler/BorderStylerEditor.cpp +++ /dev/null @@ -1,95 +0,0 @@ -#include "BorderStylerEditor.h" -#include "cru/common/ClonablePtr.h" -#include "cru/platform/graphics/Brush.h" -#include "cru/platform/graphics/Factory.h" -#include "cru/platform/gui/UiApplication.h" -#include "cru/ui/style/ApplyBorderStyleInfo.h" -#include "cru/ui/style/Styler.h" - -namespace cru::theme_builder::components::styler { -BorderStylerEditor::BorderStylerEditor() { - container_.AddChild(corner_radius_editor_.GetRootControl()); - container_.AddChild(thickness_editor_.GetRootControl()); - container_.AddChild(brush_editor_.GetRootControl()); - container_.AddChild(foreground_brush_editor_.GetRootControl()); - container_.AddChild(background_brush_editor_.GetRootControl()); - - auto connect = [this](IEvent* 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 BorderStylerEditor::GetValue() { - auto graphics_factory = - platform::gui::IUiApplication::GetInstance()->GetGraphicsFactory(); - - ui::style::ApplyBorderStyleInfo border_style; - 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()->GetValue()); - } - - if (foreground_brush_editor_.IsEnabled()) { - border_style.foreground_brush = graphics_factory->CreateSolidColorBrush( - foreground_brush_editor_.GetEditor()->GetValue()); - } - - if (background_brush_editor_.IsEnabled()) { - border_style.background_brush = graphics_factory->CreateSolidColorBrush( - background_brush_editor_.GetEditor()->GetValue()); - } - - return ui::style::BorderStyler::Create(border_style); -} - -void BorderStylerEditor::SetValue( - const ClonablePtr& styler) { - Expects(styler); - - auto border_style = styler->GetBorderStyle(); - 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(), false); - if (border_style.border_brush.has_value()) { - brush_editor_.GetEditor()->SetValue( - std::dynamic_pointer_cast( - border_style.border_brush.value()) - ->GetColor(), - false); - } - - foreground_brush_editor_.SetEnabled(border_style.foreground_brush.has_value(), - false); - if (border_style.foreground_brush.has_value()) { - foreground_brush_editor_.GetEditor()->SetValue( - std::dynamic_pointer_cast( - border_style.foreground_brush.value()) - ->GetColor(), - false); - } - - background_brush_editor_.SetEnabled(border_style.background_brush.has_value(), - false); - if (border_style.background_brush.has_value()) { - background_brush_editor_.GetEditor()->SetValue( - std::dynamic_pointer_cast( - border_style.background_brush.value()) - ->GetColor(), - false); - } -} - -} // namespace cru::theme_builder::components::styler diff --git a/src/theme_builder/components/styler/BorderStylerEditor.h b/src/theme_builder/components/styler/BorderStylerEditor.h deleted file mode 100644 index 991647e7..00000000 --- a/src/theme_builder/components/styler/BorderStylerEditor.h +++ /dev/null @@ -1,41 +0,0 @@ -#pragma once -#include "../properties/ColorPropertyEditor.h" -#include "../properties/CornerRadiusPropertyEditor.h" -#include "../properties/OptionalPropertyEditor.h" -#include "../properties/ThicknessPropertyEditor.h" -#include "cru/common/ClonablePtr.h" -#include "cru/common/Event.h" -#include "cru/ui/components/Component.h" -#include "cru/ui/controls/CheckBox.h" -#include "cru/ui/controls/FlexLayout.h" -#include "cru/ui/style/Styler.h" - -namespace cru::theme_builder::components::styler { -class BorderStylerEditor : public ui::components::Component { - public: - BorderStylerEditor(); - ~BorderStylerEditor() override; - - ui::controls::Control* GetRootControl() override { return nullptr; } - - ClonablePtr GetValue(); - void SetValue(const ClonablePtr& styler); - - IEvent* ChangeEvent() { return &change_event_; } - - private: - ui::controls::FlexLayout container_; - properties::OptionalPropertyEditor - corner_radius_editor_; - properties::OptionalPropertyEditor - thickness_editor_; - properties::OptionalPropertyEditor - brush_editor_; - properties::OptionalPropertyEditor - foreground_brush_editor_; - properties::OptionalPropertyEditor - background_brush_editor_; - - Event change_event_; -}; -} // namespace cru::theme_builder::components::styler diff --git a/src/theme_builder/components/stylers/BorderStylerEditor.cpp b/src/theme_builder/components/stylers/BorderStylerEditor.cpp new file mode 100644 index 00000000..4f36aec8 --- /dev/null +++ b/src/theme_builder/components/stylers/BorderStylerEditor.cpp @@ -0,0 +1,95 @@ +#include "BorderStylerEditor.h" +#include "cru/common/ClonablePtr.h" +#include "cru/platform/graphics/Brush.h" +#include "cru/platform/graphics/Factory.h" +#include "cru/platform/gui/UiApplication.h" +#include "cru/ui/style/ApplyBorderStyleInfo.h" +#include "cru/ui/style/Styler.h" + +namespace cru::theme_builder::components::stylers { +BorderStylerEditor::BorderStylerEditor() { + container_.AddChild(corner_radius_editor_.GetRootControl()); + container_.AddChild(thickness_editor_.GetRootControl()); + container_.AddChild(brush_editor_.GetRootControl()); + container_.AddChild(foreground_brush_editor_.GetRootControl()); + container_.AddChild(background_brush_editor_.GetRootControl()); + + auto connect = [this](IEvent* 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 BorderStylerEditor::GetValue() { + auto graphics_factory = + platform::gui::IUiApplication::GetInstance()->GetGraphicsFactory(); + + ui::style::ApplyBorderStyleInfo border_style; + 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()->GetValue()); + } + + if (foreground_brush_editor_.IsEnabled()) { + border_style.foreground_brush = graphics_factory->CreateSolidColorBrush( + foreground_brush_editor_.GetEditor()->GetValue()); + } + + if (background_brush_editor_.IsEnabled()) { + border_style.background_brush = graphics_factory->CreateSolidColorBrush( + background_brush_editor_.GetEditor()->GetValue()); + } + + return ui::style::BorderStyler::Create(border_style); +} + +void BorderStylerEditor::SetValue( + const ClonablePtr& styler) { + Expects(styler); + + auto border_style = styler->GetBorderStyle(); + 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(), false); + if (border_style.border_brush.has_value()) { + brush_editor_.GetEditor()->SetValue( + std::dynamic_pointer_cast( + border_style.border_brush.value()) + ->GetColor(), + false); + } + + foreground_brush_editor_.SetEnabled(border_style.foreground_brush.has_value(), + false); + if (border_style.foreground_brush.has_value()) { + foreground_brush_editor_.GetEditor()->SetValue( + std::dynamic_pointer_cast( + border_style.foreground_brush.value()) + ->GetColor(), + false); + } + + background_brush_editor_.SetEnabled(border_style.background_brush.has_value(), + false); + if (border_style.background_brush.has_value()) { + background_brush_editor_.GetEditor()->SetValue( + std::dynamic_pointer_cast( + border_style.background_brush.value()) + ->GetColor(), + false); + } +} + +} // namespace cru::theme_builder::components::stylers diff --git a/src/theme_builder/components/stylers/BorderStylerEditor.h b/src/theme_builder/components/stylers/BorderStylerEditor.h new file mode 100644 index 00000000..8e9a4e6f --- /dev/null +++ b/src/theme_builder/components/stylers/BorderStylerEditor.h @@ -0,0 +1,41 @@ +#pragma once +#include "../properties/ColorPropertyEditor.h" +#include "../properties/CornerRadiusPropertyEditor.h" +#include "../properties/OptionalPropertyEditor.h" +#include "../properties/ThicknessPropertyEditor.h" +#include "cru/common/ClonablePtr.h" +#include "cru/common/Event.h" +#include "cru/ui/components/Component.h" +#include "cru/ui/controls/CheckBox.h" +#include "cru/ui/controls/FlexLayout.h" +#include "cru/ui/style/Styler.h" + +namespace cru::theme_builder::components::stylers { +class BorderStylerEditor : public ui::components::Component { + public: + BorderStylerEditor(); + ~BorderStylerEditor() override; + + ui::controls::Control* GetRootControl() override { return nullptr; } + + ClonablePtr GetValue(); + void SetValue(const ClonablePtr& styler); + + IEvent* ChangeEvent() { return &change_event_; } + + private: + ui::controls::FlexLayout container_; + properties::OptionalPropertyEditor + corner_radius_editor_; + properties::OptionalPropertyEditor + thickness_editor_; + properties::OptionalPropertyEditor + brush_editor_; + properties::OptionalPropertyEditor + foreground_brush_editor_; + properties::OptionalPropertyEditor + background_brush_editor_; + + Event change_event_; +}; +} // namespace cru::theme_builder::components::stylers -- cgit v1.2.3