diff options
| author | Yuqian Yang <crupest@crupest.life> | 2025-11-21 21:43:42 +0800 |
|---|---|---|
| committer | Yuqian Yang <crupest@crupest.life> | 2025-11-21 21:43:42 +0800 |
| commit | 3b875091c445b7465b9bd044914318989a94d2ad (patch) | |
| tree | a358aebb488ec1ddc86bf87b8038bacd5d7515cb /include | |
| parent | 3cda35dbcbbe1e3854b880169c0efa0fc7a79264 (diff) | |
| download | cru-3b875091c445b7465b9bd044914318989a94d2ad.tar.gz cru-3b875091c445b7465b9bd044914318989a94d2ad.tar.bz2 cru-3b875091c445b7465b9bd044914318989a94d2ad.zip | |
Clean codes. Remove member function const.
Diffstat (limited to 'include')
24 files changed, 135 insertions, 197 deletions
diff --git a/include/cru/platform/gui/win/Keyboard.h b/include/cru/platform/gui/win/Keyboard.h index c98c49a3..8b02d0e7 100644 --- a/include/cru/platform/gui/win/Keyboard.h +++ b/include/cru/platform/gui/win/Keyboard.h @@ -5,5 +5,5 @@ namespace cru::platform::gui::win { KeyCode CRU_WIN_GUI_API VirtualKeyToKeyCode(int virtual_key); -KeyModifier CRU_WIN_GUI_API RetrieveKeyMofifier(); +KeyModifier CRU_WIN_GUI_API RetrieveKeyModifier(); } // namespace cru::platform::gui::win diff --git a/include/cru/ui/components/Component.h b/include/cru/ui/components/Component.h index 1e002e5f..86012e59 100644 --- a/include/cru/ui/components/Component.h +++ b/include/cru/ui/components/Component.h @@ -15,7 +15,7 @@ class CRU_UI_API Component public: virtual controls::Control* GetRootControl() = 0; - bool IsDeleteByParent() const { return delete_by_parent_; } + bool IsDeleteByParent() { return delete_by_parent_; } void SetDeleteByParent(bool delete_by_parent) { delete_by_parent_ = delete_by_parent; } diff --git a/include/cru/ui/components/Input.h b/include/cru/ui/components/Input.h index 0f077983..3d56f5fa 100644 --- a/include/cru/ui/components/Input.h +++ b/include/cru/ui/components/Input.h @@ -9,7 +9,7 @@ struct CRU_UI_API InputValidateResult { }; struct CRU_UI_API IInputValidator : public virtual Interface { - virtual InputValidateResult Validate(std::string_view text) const = 0; + virtual InputValidateResult Validate(std::string_view text) = 0; }; struct CRU_UI_API InputChangeEventArgs { @@ -26,14 +26,14 @@ class CRU_UI_API Input : public Component { public: controls::Control* GetRootControl() override; - std::string GetText() const; + std::string GetText(); void SetText(std::string text); - IInputValidator* GetValidator() const; + IInputValidator* GetValidator(); void SetValidator(IInputValidator* validator); InputValidateResult Validate(); - InputValidateResult GetLastValidateResult() const; + InputValidateResult GetLastValidateResult(); IEvent<InputChangeEventArgs>* ChangeEvent() { return &change_event_; } @@ -48,7 +48,7 @@ class CRU_UI_API Input : public Component { class CRU_UI_API FloatInputValidator : public Object, public virtual IInputValidator { public: - InputValidateResult Validate(std::string_view text) const override; + InputValidateResult Validate(std::string_view text) override; std::optional<float> min; std::optional<float> max; @@ -60,13 +60,13 @@ class CRU_UI_API FloatInput : public Input { ~FloatInput() override; public: - float GetValue() const; + float GetValue(); void SetValue(float value); - std::optional<float> GetMax() const; + std::optional<float> GetMax(); void SetMax(std::optional<float> max); - std::optional<float> GetMin() const; + std::optional<float> GetMin(); void SetMin(std::optional<float> min); private: diff --git a/include/cru/ui/components/Menu.h b/include/cru/ui/components/Menu.h index a8a90ed7..aee23894 100644 --- a/include/cru/ui/components/Menu.h +++ b/include/cru/ui/components/Menu.h @@ -38,7 +38,7 @@ class CRU_UI_API Menu : public Component { public: controls::Control* GetRootControl() override { return &container_; } - Index GetItemCount() const { return static_cast<Index>(items_.size()); } + Index GetItemCount() { return static_cast<Index>(items_.size()); } void AddItem(Component* component) { AddItemAt(component, GetItemCount()); } void AddItemAt(Component* component, Index index); diff --git a/include/cru/ui/components/PopupButton.h b/include/cru/ui/components/PopupButton.h index 0e3d5314..61ac6280 100644 --- a/include/cru/ui/components/PopupButton.h +++ b/include/cru/ui/components/PopupButton.h @@ -18,7 +18,9 @@ class CRU_UI_API PopupMenuTextButton : public Component { ui::controls::Button* GetButton() { return &button_; } std::string GetButtonText() { return button_text_.GetText(); } - void SetButtonText(std::string text) { button_text_.SetText(std::move(text)); } + void SetButtonText(std::string text) { + button_text_.SetText(std::move(text)); + } void SetButtonTextColor(const Color& color) { button_text_.SetTextColor(color); diff --git a/include/cru/ui/components/Select.h b/include/cru/ui/components/Select.h index 9efe9a04..67b18383 100644 --- a/include/cru/ui/components/Select.h +++ b/include/cru/ui/components/Select.h @@ -13,10 +13,10 @@ class CRU_UI_API Select : public Component { public: ui::controls::Control* GetRootControl() override { return &button_; } - std::vector<std::string> GetItems() const { return items_; } + std::vector<std::string> GetItems() { return items_; } void SetItems(std::vector<std::string> items); - Index GetSelectedIndex() const { return selected_index_; } + Index GetSelectedIndex() { return selected_index_; } void SetSelectedIndex(Index index); IEvent<Index>* ItemSelectedEvent() { return &item_selected_event_; } diff --git a/include/cru/ui/controls/Button.h b/include/cru/ui/controls/Button.h index 64b9bba0..80d21d08 100644 --- a/include/cru/ui/controls/Button.h +++ b/include/cru/ui/controls/Button.h @@ -1,25 +1,21 @@ #pragma once -#include "SingleChildControl.h" - #include "../helper/ClickDetector.h" #include "../render/BorderRenderObject.h" #include "IBorderControl.h" #include "IClickableControl.h" -#include "cru/base/Event.h" +#include "SingleChildControl.h" namespace cru::ui::controls { class CRU_UI_API Button : public SingleChildControl<render::BorderRenderObject>, public virtual IClickableControl, public virtual IBorderControl { public: - static constexpr std::string_view kControlType = "Button"; + static constexpr auto kControlName = "Button"; public: Button(); ~Button() override; - std::string GetControlType() const final { return std::string(kControlType); } - public: helper::ClickState GetClickState() override { return click_detector_.GetState(); @@ -29,7 +25,7 @@ class CRU_UI_API Button : public SingleChildControl<render::BorderRenderObject>, return click_detector_.StateChangeEvent(); } - IEvent<const helper::ClickEventArgs&>* ClickEvent() { + IEvent<const helper::ClickEventArgs&>* ClickEvent() override { return click_detector_.ClickEvent(); } diff --git a/include/cru/ui/controls/CheckBox.h b/include/cru/ui/controls/CheckBox.h index 6f4eae2e..2e4685d5 100644 --- a/include/cru/ui/controls/CheckBox.h +++ b/include/cru/ui/controls/CheckBox.h @@ -12,20 +12,16 @@ class CRU_UI_API CheckBox : public Control, public virtual ICheckableControl, public virtual IClickableControl { public: - static constexpr std::string_view kControlType = "CheckBox"; + static constexpr auto kControlName = "CheckBox"; CheckBox(); ~CheckBox() override; - std::string GetControlType() const override { - return std::string(kControlType); + render::RenderObject* GetRenderObject() override { + return &container_render_object_; } - render::RenderObject* GetRenderObject() const override { - return container_render_object_.get(); - } - - bool IsChecked() const override { return checked_; } + bool IsChecked() override { return checked_; } void SetChecked(bool checked) override; void Toggle() { SetChecked(!checked_); } @@ -41,11 +37,15 @@ class CRU_UI_API CheckBox : public Control, return click_detector_.StateChangeEvent(); } + IEvent<const helper::ClickEventArgs&>* ClickEvent() override { + return click_detector_.ClickEvent(); + } + private: - bool checked_ = false; + bool checked_; Event<bool> checked_change_event_; - std::unique_ptr<render::BorderRenderObject> container_render_object_; + render::BorderRenderObject container_render_object_; helper::ClickDetector click_detector_; }; diff --git a/include/cru/ui/controls/Container.h b/include/cru/ui/controls/Container.h index 4fee178d..ba7dbd18 100644 --- a/include/cru/ui/controls/Container.h +++ b/include/cru/ui/controls/Container.h @@ -1,31 +1,26 @@ #pragma once -#include "SingleChildControl.h" - #include "../render/BorderRenderObject.h" #include "IBorderControl.h" +#include "SingleChildControl.h" namespace cru::ui::controls { class CRU_UI_API Container : public SingleChildControl<render::BorderRenderObject>, public virtual IBorderControl { - static constexpr std::string_view kControlType = "Container"; + static constexpr auto kControlName = "Container"; public: Container(); - CRU_DELETE_COPY(Container) - CRU_DELETE_MOVE(Container) - - ~Container() override; public: - bool IsBorderEnabled() const { + bool IsBorderEnabled() { return GetContainerRenderObject()->IsBorderEnabled(); } void SetBorderEnabled(bool enabled) { GetContainerRenderObject()->SetBorderEnabled(enabled); } - std::shared_ptr<platform::graphics::IBrush> GetForegroundBrush() const { + std::shared_ptr<platform::graphics::IBrush> GetForegroundBrush() { return GetContainerRenderObject()->GetForegroundBrush(); } void SetForegroundBrush( @@ -33,7 +28,7 @@ class CRU_UI_API Container GetContainerRenderObject()->SetForegroundBrush(brush); } - std::shared_ptr<platform::graphics::IBrush> GetBackgroundBrush() const { + std::shared_ptr<platform::graphics::IBrush> GetBackgroundBrush() { return GetContainerRenderObject()->GetBackgroundBrush(); } void SetBackgroundBrush( @@ -44,8 +39,5 @@ class CRU_UI_API Container void ApplyBorderStyle(const style::ApplyBorderStyleInfo& style) override { GetContainerRenderObject()->ApplyBorderStyle(style); } - - public: - std::string GetControlType() const final { return std::string(kControlType); } }; } // namespace cru::ui::controls diff --git a/include/cru/ui/controls/Control.h b/include/cru/ui/controls/Control.h index 6a85d25a..544d33e6 100644 --- a/include/cru/ui/controls/Control.h +++ b/include/cru/ui/controls/Control.h @@ -17,9 +17,6 @@ namespace cru::ui::controls { * methods: * - GetControlType() * - GetRenderObject() - * - ForEachChild(const std::function<void(Control*)>& predicate) - * - RemoveChild(Control* child) - * The last two methods are totally for convenient control tree management. */ class CRU_UI_API Control : public Object, public cru::platform::gui::DeleteLaterImpl<Control>, @@ -29,15 +26,14 @@ class CRU_UI_API Control : public Object, CRU_DEFINE_CLASS_LOG_TAG("cru::ui::controls::Control") protected: - Control(); + explicit Control(std::string name); public: ~Control() override; public: - virtual std::string GetControlType() const = 0; - - std::string GetDebugId() const; + std::string GetName(); + std::string GetDebugId(); //*************** region: tree *************** public: @@ -46,6 +42,10 @@ class CRU_UI_API Control : public Object, Control* GetParent(); bool HasAncestor(Control* control); const std::vector<Control*>& GetChildren(); + Index GetChildCount() { return GetChildren().size(); } + Control* GetChildAt(Index index) { return GetChildren()[index]; } + Index IndexOfChild(Control* control); + void RemoveChild(Control* child); void RemoveAllChild(); void RemoveFromParent(); @@ -69,23 +69,21 @@ class CRU_UI_API Control : public Object, void AddChild(Control* control); public: - virtual render::RenderObject* GetRenderObject() const = 0; + virtual render::RenderObject* GetRenderObject() = 0; - virtual render::MeasureSize GetPreferredSize() const { + virtual render::MeasureSize GetPreferredSize() { return GetRenderObject()->GetPreferredSize(); } virtual void SetPreferredSize(const render::MeasureSize& size) { GetRenderObject()->SetPreferredSize(size); } - virtual Thickness GetMargin() const { return GetRenderObject()->GetMargin(); } + virtual Thickness GetMargin() { return GetRenderObject()->GetMargin(); } virtual void SetMargin(const Thickness& margin) { GetRenderObject()->SetMargin(margin); } - virtual Thickness GetPadding() const { - return GetRenderObject()->GetPadding(); - } + virtual Thickness GetPadding() { return GetRenderObject()->GetPadding(); } virtual void SetPadding(const Thickness& padding) { GetRenderObject()->SetPadding(padding); } @@ -123,10 +121,10 @@ class CRU_UI_API Control : public Object, //*************** region: events *************** public: - // Raised when mouse enter the control. Even when the control itself captures - // the mouse, this event is raised as regular. But if mouse is captured by - // another control, the control will not receive any mouse enter event. You - // can use `IsMouseCaptured` to get more info. + // Raised when mouse enter the control. Even when the control itself + // captures the mouse, this event is raised as regular. But if mouse is + // captured by another control, the control will not receive any mouse enter + // event. You can use `IsMouseCaptured` to get more info. CRU_DEFINE_ROUTED_EVENT(MouseEnter, events::MouseEventArgs) // Raised when mouse is leave the control. Even when the control itself @@ -151,11 +149,12 @@ class CRU_UI_API Control : public Object, virtual void OnChildRemoved(Control* control, Index index); private: - ControlHost* host_ = nullptr; - Control* parent_ = nullptr; + std::string name_; + ControlHost* host_; + Control* parent_; std::vector<Control*> children_; - std::shared_ptr<platform::gui::ICursor> cursor_ = nullptr; + std::shared_ptr<platform::gui::ICursor> cursor_; std::shared_ptr<style::StyleRuleSet> style_rule_set_; std::unique_ptr<style::StyleRuleSetBind> style_rule_set_bind_; diff --git a/include/cru/ui/controls/FlexLayout.h b/include/cru/ui/controls/FlexLayout.h index 8940e38e..e5ea0703 100644 --- a/include/cru/ui/controls/FlexLayout.h +++ b/include/cru/ui/controls/FlexLayout.h @@ -13,25 +13,18 @@ using render::FlexMainAlignment; class CRU_UI_API FlexLayout : public LayoutControl<render::FlexLayoutRenderObject> { public: - static constexpr std::string_view kControlType = "FlexLayout"; + static constexpr auto kControlName = "FlexLayout"; public: FlexLayout(); - FlexLayout(const FlexLayout& other) = delete; - FlexLayout(FlexLayout&& other) = delete; - FlexLayout& operator=(const FlexLayout& other) = delete; - FlexLayout& operator=(FlexLayout&& other) = delete; - ~FlexLayout() override; - std::string GetControlType() const final { return std::string(kControlType); } - - FlexMainAlignment GetContentMainAlign() const; + FlexMainAlignment GetContentMainAlign(); void SetContentMainAlign(FlexMainAlignment value); - FlexDirection GetFlexDirection() const; + FlexDirection GetFlexDirection(); void SetFlexDirection(FlexDirection direction); - FlexCrossAlignment GetItemCrossAlign() const; + FlexCrossAlignment GetItemCrossAlign(); void SetItemCrossAlign(FlexCrossAlignment alignment); }; } // namespace cru::ui::controls diff --git a/include/cru/ui/controls/ICheckableControl.h b/include/cru/ui/controls/ICheckableControl.h index 7a6d7b8e..b58a3f8c 100644 --- a/include/cru/ui/controls/ICheckableControl.h +++ b/include/cru/ui/controls/ICheckableControl.h @@ -4,7 +4,7 @@ namespace cru::ui::controls { struct CRU_UI_API ICheckableControl : virtual Interface { - virtual bool IsChecked() const = 0; + virtual bool IsChecked() = 0; virtual void SetChecked(bool checked) = 0; virtual IEvent<bool>* CheckedChangeEvent() = 0; }; diff --git a/include/cru/ui/controls/IClickableControl.h b/include/cru/ui/controls/IClickableControl.h index 4ddea730..1cf29f84 100644 --- a/include/cru/ui/controls/IClickableControl.h +++ b/include/cru/ui/controls/IClickableControl.h @@ -7,5 +7,6 @@ namespace cru::ui::controls { struct CRU_UI_API IClickableControl : virtual Interface { virtual helper::ClickState GetClickState() = 0; virtual IEvent<helper::ClickState>* ClickStateChangeEvent() = 0; + virtual IEvent<const helper::ClickEventArgs&>* ClickEvent() = 0; }; } // namespace cru::ui::controls diff --git a/include/cru/ui/controls/IContentBrushControl.h b/include/cru/ui/controls/IContentBrushControl.h index d7a4c1b8..1c872e7a 100644 --- a/include/cru/ui/controls/IContentBrushControl.h +++ b/include/cru/ui/controls/IContentBrushControl.h @@ -4,8 +4,7 @@ namespace cru::ui::controls { struct CRU_UI_API IContentBrushControl : virtual Interface { - virtual std::shared_ptr<platform::graphics::IBrush> GetContentBrush() - const = 0; + virtual std::shared_ptr<platform::graphics::IBrush> GetContentBrush() = 0; virtual void SetContentBrush( std::shared_ptr<platform::graphics::IBrush> brush) = 0; }; diff --git a/include/cru/ui/controls/IFontControl.h b/include/cru/ui/controls/IFontControl.h index 4167de27..33a3f954 100644 --- a/include/cru/ui/controls/IFontControl.h +++ b/include/cru/ui/controls/IFontControl.h @@ -1,10 +1,11 @@ #pragma once #include "../Base.h" -#include "cru/platform/graphics/Font.h" + +#include <cru/platform/graphics/Font.h> namespace cru::ui::controls { struct CRU_UI_API IFontControl : virtual Interface { - virtual std::shared_ptr<platform::graphics::IFont> GetFont() const = 0; + virtual std::shared_ptr<platform::graphics::IFont> GetFont() = 0; virtual void SetFont(std::shared_ptr<platform::graphics::IFont> font) = 0; }; } // namespace cru::ui::controls diff --git a/include/cru/ui/controls/IconButton.h b/include/cru/ui/controls/IconButton.h index 0bbda327..b061bc87 100644 --- a/include/cru/ui/controls/IconButton.h +++ b/include/cru/ui/controls/IconButton.h @@ -16,17 +16,15 @@ class CRU_UI_API IconButton : public Control, public virtual IBorderControl, public virtual IContentBrushControl { public: - static constexpr std::string_view kControlType = "IconButton"; + static constexpr auto kControlName = "IconButton"; public: IconButton(); IconButton(std::string_view icon_svg_path_data_string, const Rect& view_port); ~IconButton() override; - std::string GetControlType() const final { return std::string(kControlType); } - - render::RenderObject* GetRenderObject() const override { - return container_render_object_.get(); + render::RenderObject* GetRenderObject() override { + return &container_render_object_; } public: @@ -38,48 +36,46 @@ class CRU_UI_API IconButton : public Control, return click_detector_.StateChangeEvent(); } - IEvent<const helper::ClickEventArgs&>* ClickEvent() { + IEvent<const helper::ClickEventArgs&>* ClickEvent() override { return click_detector_.ClickEvent(); } void ApplyBorderStyle(const style::ApplyBorderStyleInfo& style) override { - container_render_object_->ApplyBorderStyle(style); + container_render_object_.ApplyBorderStyle(style); } - std::shared_ptr<platform::graphics::IGeometry> GetIconGeometry() const { - return geometry_render_object_->GetGeometry(); + std::shared_ptr<platform::graphics::IGeometry> GetIconGeometry() { + return geometry_render_object_.GetGeometry(); } void SetIconGeometry(std::shared_ptr<platform::graphics::IGeometry> geometry, std::optional<Rect> view_port = std::nullopt) { - geometry_render_object_->SetGeometry(std::move(geometry), view_port); + geometry_render_object_.SetGeometry(std::move(geometry), view_port); } - Rect GetIconViewPort() const { - return geometry_render_object_->GetViewPort(); - } + Rect GetIconViewPort() { return geometry_render_object_.GetViewPort(); } void SetIconViewPort(const Rect& view_port) { - geometry_render_object_->SetViewPort(view_port); + geometry_render_object_.SetViewPort(view_port); } - std::shared_ptr<platform::graphics::IBrush> GetIconFillBrush() const { - return geometry_render_object_->GetFillBrush(); + std::shared_ptr<platform::graphics::IBrush> GetIconFillBrush() { + return geometry_render_object_.GetFillBrush(); } void SetIconFillBrush(std::shared_ptr<platform::graphics::IBrush> brush) { - geometry_render_object_->SetFillBrush(std::move(brush)); + geometry_render_object_.SetFillBrush(std::move(brush)); } - std::shared_ptr<platform::graphics::IBrush> GetIconStrokeBrush() const { - return geometry_render_object_->GetStrokeBrush(); + std::shared_ptr<platform::graphics::IBrush> GetIconStrokeBrush() { + return geometry_render_object_.GetStrokeBrush(); } void SetIconStrokeBrush(std::shared_ptr<platform::graphics::IBrush> brush) { - geometry_render_object_->SetStrokeBrush(std::move(brush)); + geometry_render_object_.SetStrokeBrush(std::move(brush)); } - float GetIconStrokeWidth() const { - return geometry_render_object_->GetStrokeWidth(); + float GetIconStrokeWidth() { + return geometry_render_object_.GetStrokeWidth(); } void SetIconStrokeWidth(float width) { - geometry_render_object_->SetStrokeWidth(width); + geometry_render_object_.SetStrokeWidth(width); } void SetIconFillColor(const Color& color); @@ -89,7 +85,7 @@ class CRU_UI_API IconButton : public Control, std::string_view icon_svg_path_data_string_resource_key, const Rect& view_port); - std::shared_ptr<platform::graphics::IBrush> GetContentBrush() const override { + std::shared_ptr<platform::graphics::IBrush> GetContentBrush() override { return GetIconFillBrush(); } @@ -99,8 +95,8 @@ class CRU_UI_API IconButton : public Control, } private: - std::unique_ptr<render::BorderRenderObject> container_render_object_; - std::unique_ptr<render::GeometryRenderObject> geometry_render_object_; + render::BorderRenderObject container_render_object_; + render::GeometryRenderObject geometry_render_object_; helper::ClickDetector click_detector_; }; } // namespace cru::ui::controls diff --git a/include/cru/ui/controls/LayoutControl.h b/include/cru/ui/controls/LayoutControl.h index fad86530..866a8c03 100644 --- a/include/cru/ui/controls/LayoutControl.h +++ b/include/cru/ui/controls/LayoutControl.h @@ -5,8 +5,8 @@ namespace cru::ui::controls { template <typename TRenderObject> class LayoutControl : public Control { protected: - LayoutControl() : container_render_object_(new TRenderObject()) { - container_render_object_->SetAttachedControl(this); + LayoutControl(std::string name) : Control(std::move(name)) { + container_render_object_.SetAttachedControl(this); } public: @@ -14,47 +14,34 @@ class LayoutControl : public Control { using Control::InsertChildAt; using Control::RemoveChildAt; - Index GetChildCount() const { return children_.size(); } - Control* GetChildAt(Index index) const { return children_[index]; } - - Index IndexOfChild(Control* control) const { - auto it = std::find(children_.begin(), children_.end(), control); - if (it == children_.end()) { - return -1; - } - return it - children_.begin(); - } - - render::RenderObject* GetRenderObject() const override { - return container_render_object_.get(); + render::RenderObject* GetRenderObject() override { + return &container_render_object_; } - TRenderObject* GetContainerRenderObject() const { - return container_render_object_.get(); + TRenderObject* GetContainerRenderObject() { + return &container_render_object_; } const typename TRenderObject::ChildLayoutData& GetChildLayoutData( Index position) { - return container_render_object_->GetChildLayoutDataAt(position); + return container_render_object_.GetChildLayoutDataAt(position); } void SetChildLayoutData(Index position, typename TRenderObject::ChildLayoutData data) { - container_render_object_->SetChildLayoutDataAt(position, data); + container_render_object_.SetChildLayoutDataAt(position, data); } protected: void OnChildInserted(Control* control, Index index) override { - container_render_object_->AddChild(control->GetRenderObject(), index); + container_render_object_.AddChild(control->GetRenderObject(), index); } void OnChildRemoved([[maybe_unused]] Control* control, Index index) override { - container_render_object_->RemoveChild(index); + container_render_object_.RemoveChild(index); } private: - std::unique_ptr<TRenderObject> container_render_object_; - - std::vector<Control*> children_; + TRenderObject container_render_object_; }; } // namespace cru::ui::controls diff --git a/include/cru/ui/controls/ScrollView.h b/include/cru/ui/controls/ScrollView.h index cefda747..181f4ba5 100644 --- a/include/cru/ui/controls/ScrollView.h +++ b/include/cru/ui/controls/ScrollView.h @@ -1,20 +1,13 @@ #pragma once #include "SingleChildControl.h" - #include "cru/ui/render/ScrollRenderObject.h" namespace cru::ui::controls { class CRU_UI_API ScrollView : public SingleChildControl<render::ScrollRenderObject> { public: - static constexpr std::string_view kControlType = "ScrollView"; + static constexpr auto kControlName = "ScrollView"; ScrollView(); - CRU_DELETE_COPY(ScrollView) - CRU_DELETE_MOVE(ScrollView) - ~ScrollView() override; - - public: - std::string GetControlType() const override { return std::string(kControlType); } }; } // namespace cru::ui::controls diff --git a/include/cru/ui/controls/SingleChildControl.h b/include/cru/ui/controls/SingleChildControl.h index f748a38d..684b8403 100644 --- a/include/cru/ui/controls/SingleChildControl.h +++ b/include/cru/ui/controls/SingleChildControl.h @@ -1,41 +1,46 @@ #pragma once #include "Control.h" +#include <cassert> + namespace cru::ui::controls { template <typename TRenderObject> class SingleChildControl : public Control { protected: - SingleChildControl() : container_render_object_(new TRenderObject()) { - container_render_object_->SetAttachedControl(this); + SingleChildControl(std::string name) : Control(std::move(name)) { + container_render_object_.SetAttachedControl(this); } public: Control* GetChild() { - return GetChildren().empty() ? nullptr : GetChildren().front(); + const auto& children = GetChildren(); + assert(children.empty() || children.size() == 1); + return children.empty() ? nullptr : children.front(); } void SetChild(Control* child) { - if (GetChild() == child) return; - if (!GetChildren().empty()) { + auto old_child = GetChild(); + if (old_child == child) return; + if (old_child) { RemoveChildAt(0); } if (child) { InsertChildAt(child, 0); } - container_render_object_->SetChild( - child == nullptr ? nullptr : child->GetRenderObject()); + container_render_object_.SetChild(child ? child->GetRenderObject() + : nullptr); } - render::RenderObject* GetRenderObject() const override { - return container_render_object_.get(); + render::RenderObject* GetRenderObject() override { + return &container_render_object_; } - TRenderObject* GetContainerRenderObject() const { - return container_render_object_.get(); + TRenderObject* GetContainerRenderObject() { + return &container_render_object_; } private: - std::unique_ptr<TRenderObject> container_render_object_; + TRenderObject container_render_object_; }; } // namespace cru::ui::controls diff --git a/include/cru/ui/controls/StackLayout.h b/include/cru/ui/controls/StackLayout.h index db646807..455a99be 100644 --- a/include/cru/ui/controls/StackLayout.h +++ b/include/cru/ui/controls/StackLayout.h @@ -1,6 +1,5 @@ #pragma once #include "LayoutControl.h" - #include "../render/StackLayoutRenderObject.h" namespace cru::ui::controls { @@ -9,13 +8,8 @@ using render::StackChildLayoutData; class CRU_UI_API StackLayout : public LayoutControl<render::StackLayoutRenderObject> { public: - static constexpr std::string_view kControlType = "StackLayout"; + static constexpr auto kControlName = "StackLayout"; StackLayout(); - CRU_DELETE_COPY(StackLayout) - CRU_DELETE_MOVE(StackLayout) - ~StackLayout() override; - - std::string GetControlType() const final { return std::string(kControlType); } }; } // namespace cru::ui::controls diff --git a/include/cru/ui/controls/TextBlock.h b/include/cru/ui/controls/TextBlock.h index af9865c4..9b8875e6 100644 --- a/include/cru/ui/controls/TextBlock.h +++ b/include/cru/ui/controls/TextBlock.h @@ -1,6 +1,6 @@ #pragma once -#include "Control.h" #include "../render/TextRenderObject.h" +#include "Control.h" #include "IContentBrushControl.h" #include "IFontControl.h" #include "TextHostControlService.h" @@ -14,7 +14,7 @@ class CRU_UI_API TextBlock : public Control, public virtual IFontControl, public virtual IContentBrushControl { public: - static constexpr std::string_view kControlType = "TextBlock"; + static constexpr auto kControlName = "TextBlock"; static std::unique_ptr<TextBlock> Create(std::string text, bool selectable = false) { @@ -26,23 +26,16 @@ class CRU_UI_API TextBlock : public Control, public: TextBlock(); - TextBlock(const TextBlock& other) = delete; - TextBlock(TextBlock&& other) = delete; - TextBlock& operator=(const TextBlock& other) = delete; - TextBlock& operator=(TextBlock&& other) = delete; - ~TextBlock() override; - std::string GetControlType() const final { return std::string(kControlType); } + render::RenderObject* GetRenderObject() override; - render::RenderObject* GetRenderObject() const override; - - std::string GetText() const; + std::string GetText(); void SetText(std::string text); - bool IsSelectable() const; + bool IsSelectable(); void SetSelectable(bool value); - std::shared_ptr<platform::graphics::IBrush> GetTextBrush() const { + std::shared_ptr<platform::graphics::IBrush> GetTextBrush() { return text_render_object_->GetBrush(); } void SetTextBrush(std::shared_ptr<platform::graphics::IBrush> brush) { @@ -55,14 +48,14 @@ class CRU_UI_API TextBlock : public Control, return nullptr; } - std::shared_ptr<platform::graphics::IFont> GetFont() const override { + std::shared_ptr<platform::graphics::IFont> GetFont() override { return text_render_object_->GetFont(); } void SetFont(std::shared_ptr<platform::graphics::IFont> font) override { text_render_object_->SetFont(std::move(font)); } - std::shared_ptr<platform::graphics::IBrush> GetContentBrush() const override { + std::shared_ptr<platform::graphics::IBrush> GetContentBrush() override { return GetTextBrush(); } @@ -73,7 +66,6 @@ class CRU_UI_API TextBlock : public Control, private: std::unique_ptr<render::TextRenderObject> text_render_object_; - std::unique_ptr<TextHostControlService> service_; }; } // namespace cru::ui::controls diff --git a/include/cru/ui/controls/TextBox.h b/include/cru/ui/controls/TextBox.h index 4c6ef384..7b3ab323 100644 --- a/include/cru/ui/controls/TextBox.h +++ b/include/cru/ui/controls/TextBox.h @@ -1,5 +1,6 @@ #pragma once #include "../render/BorderRenderObject.h" +#include "../render/ScrollRenderObject.h" #include "../render/TextRenderObject.h" #include "Control.h" #include "IBorderControl.h" @@ -18,48 +19,43 @@ class CRU_UI_API TextBox : public Control, public virtual IContentBrushControl, public virtual IFontControl { public: - static constexpr std::string_view control_type = "TextBox"; + static constexpr auto kControlName = "TextBox"; TextBox(); - CRU_DELETE_COPY(TextBox) - CRU_DELETE_MOVE(TextBox) - ~TextBox() override; - std::string GetControlType() const final { return std::string(control_type); } - - render::RenderObject* GetRenderObject() const override; + render::RenderObject* GetRenderObject() override; render::TextRenderObject* GetTextRenderObject() override; render::ScrollRenderObject* GetScrollRenderObject() override; - bool GetMultiLine() const; + bool GetMultiLine(); void SetMultiLine(bool value); void ApplyBorderStyle(const style::ApplyBorderStyleInfo& style) override; - std::string GetText() const { return service_->GetText(); } - std::string_view GetTextView() const { return service_->GetTextView(); } + std::string GetText() { return service_->GetText(); } + std::string_view GetTextView() { return service_->GetTextView(); } void SetText(std::string text) { service_->SetText(std::move(text)); } IEvent<std::nullptr_t>* TextChangeEvent() { return service_->TextChangeEvent(); } - std::shared_ptr<platform::graphics::IFont> GetFont() const override { + std::shared_ptr<platform::graphics::IFont> GetFont() override { return text_render_object_->GetFont(); } void SetFont(std::shared_ptr<platform::graphics::IFont> font) override { text_render_object_->SetFont(std::move(font)); } - std::shared_ptr<platform::graphics::IBrush> GetTextBrush() const { + std::shared_ptr<platform::graphics::IBrush> GetTextBrush() { return text_render_object_->GetBrush(); } void SetTextBrush(std::shared_ptr<platform::graphics::IBrush> brush) { text_render_object_->SetBrush(std::move(brush)); } - std::shared_ptr<platform::graphics::IBrush> GetContentBrush() const override { + std::shared_ptr<platform::graphics::IBrush> GetContentBrush() override { return GetTextBrush(); } diff --git a/include/cru/ui/controls/TreeView.h b/include/cru/ui/controls/TreeView.h index 62b1c69b..08b62c1a 100644 --- a/include/cru/ui/controls/TreeView.h +++ b/include/cru/ui/controls/TreeView.h @@ -54,17 +54,11 @@ class CRU_UI_API TreeView : public Control { using Control::AddChild; public: - constexpr static std::string_view kControlType = "TreeView"; + constexpr static auto kControlType = "TreeView"; TreeView(); - CRU_DELETE_COPY(TreeView) - CRU_DELETE_MOVE(TreeView) - ~TreeView() override; - std::string GetControlType() const override { - return std::string(kControlType); - } - render::TreeRenderObject* GetRenderObject() { return &render_object_; } + render::TreeRenderObject* GetRenderObject() override { return &render_object_; } TreeViewItem* GetRootItem() { return &root_item_; } diff --git a/include/cru/ui/controls/Window.h b/include/cru/ui/controls/Window.h index 3d3ff0b0..2c650c41 100644 --- a/include/cru/ui/controls/Window.h +++ b/include/cru/ui/controls/Window.h @@ -19,14 +19,12 @@ class CRU_UI_API Window friend Control; public: - static constexpr std::string_view kControlType = "Window"; + static constexpr auto kControlName = "Window"; Window(); static Window* CreatePopup(); - std::string GetControlType() const override; - void SetAttachedControl(Control* control); platform::gui::INativeWindow* GetNativeWindow(); |
