diff options
author | crupest <crupest@outlook.com> | 2022-02-09 23:00:49 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-02-09 23:00:49 +0800 |
commit | d18b5453d7ffd19667ee8ac125b34ab5328f0dc3 (patch) | |
tree | 10b969c744dd0ef90a183e4ffacddc818b86534e | |
parent | e18c4fb46d7913f337cc25b9a6e3a25359c3f10a (diff) | |
download | cru-d18b5453d7ffd19667ee8ac125b34ab5328f0dc3.tar.gz cru-d18b5453d7ffd19667ee8ac125b34ab5328f0dc3.tar.bz2 cru-d18b5453d7ffd19667ee8ac125b34ab5328f0dc3.zip |
...
34 files changed, 203 insertions, 181 deletions
diff --git a/include/cru/ui/Base.h b/include/cru/ui/Base.h index 44f76907..182c00f2 100644 --- a/include/cru/ui/Base.h +++ b/include/cru/ui/Base.h @@ -128,23 +128,6 @@ class CanvasPaintEventArgs { Size paint_size_; }; -enum class FlexDirection { - Horizontal, - HorizontalReverse, - Vertical, - VertivalReverse -}; - -using FlexMainAlignment = Alignment; -using FlexCrossAlignment = Alignment; - -struct FlexChildLayoutData { - float expand_factor = 0; - float shrink_factor = 1; - // nullopt stands for looking at parent's setting - std::optional<FlexCrossAlignment> cross_alignment = std::nullopt; -}; - struct StackChildLayoutData { std::optional<Alignment> horizontal; std::optional<Alignment> vertical; diff --git a/include/cru/ui/controls/Button.h b/include/cru/ui/controls/Button.h index ab1ef8eb..00253793 100644 --- a/include/cru/ui/controls/Button.h +++ b/include/cru/ui/controls/Button.h @@ -5,12 +5,13 @@ #include "IBorderControl.h" #include "IClickableControl.h" #include "cru/common/Event.h" +#include "cru/ui/render/BorderRenderObject.h" #include "cru/ui/style/ApplyBorderStyleInfo.h" namespace cru::ui::controls { class CRU_UI_API Button : public ContentControl, - public virtual IClickableControl, - public virtual IBorderControl { + public virtual IClickableControl, + public virtual IBorderControl { public: static constexpr StringView control_type = u"Button"; diff --git a/include/cru/ui/controls/Container.h b/include/cru/ui/controls/Container.h index f250807d..926e395e 100644 --- a/include/cru/ui/controls/Container.h +++ b/include/cru/ui/controls/Container.h @@ -1,6 +1,8 @@ #pragma once #include "ContentControl.h" +#include "cru/ui/render/BorderRenderObject.h" + namespace cru::ui::controls { class CRU_UI_API Container : public ContentControl { static constexpr StringView control_type = u"Container"; diff --git a/include/cru/ui/controls/ContentControl.h b/include/cru/ui/controls/ContentControl.h index 86e90dae..5600ec25 100644 --- a/include/cru/ui/controls/ContentControl.h +++ b/include/cru/ui/controls/ContentControl.h @@ -1,7 +1,6 @@ #pragma once #include "Control.h" -#include "cru/ui/render/RenderObject.h" namespace cru::ui::controls { class CRU_UI_API ContentControl : public Control { diff --git a/include/cru/ui/controls/Control.h b/include/cru/ui/controls/Control.h index edbf5919..3927e604 100644 --- a/include/cru/ui/controls/Control.h +++ b/include/cru/ui/controls/Control.h @@ -2,8 +2,8 @@ #include "Base.h" #include "../events/UiEvents.h" -#include "../render/Base.h" #include "cru/common/Event.h" +#include "cru/ui/render/RenderObject.h" namespace cru::ui::controls { class CRU_UI_API Control : public Object { diff --git a/include/cru/ui/controls/FlexLayout.h b/include/cru/ui/controls/FlexLayout.h index 1255b119..8fdb3aef 100644 --- a/include/cru/ui/controls/FlexLayout.h +++ b/include/cru/ui/controls/FlexLayout.h @@ -1,7 +1,15 @@ #pragma once #include "LayoutControl.h" +#include "cru/ui/render/FlexLayoutRenderObject.h" + namespace cru::ui::controls { + +using render::FlexChildLayoutData; +using render::FlexCrossAlignment; +using render::FlexDirection; +using render::FlexMainAlignment; + class CRU_UI_API FlexLayout : public LayoutControl { public: static constexpr StringView control_type = u"FlexLayout"; diff --git a/include/cru/ui/controls/RootControl.h b/include/cru/ui/controls/RootControl.h index c7ed916a..41424286 100644 --- a/include/cru/ui/controls/RootControl.h +++ b/include/cru/ui/controls/RootControl.h @@ -7,6 +7,7 @@ #include "cru/platform/gui/Window.h" #include "cru/ui/Base.h" #include "cru/ui/host/WindowHost.h" +#include "cru/ui/render/StackLayoutRenderObject.h" namespace cru::ui::controls { class CRU_UI_API RootControl : public LayoutControl { diff --git a/include/cru/ui/controls/ScrollView.h b/include/cru/ui/controls/ScrollView.h index 46c23691..7bc9b226 100644 --- a/include/cru/ui/controls/ScrollView.h +++ b/include/cru/ui/controls/ScrollView.h @@ -2,6 +2,7 @@ #include "ContentControl.h" #include "cru/common/Base.h" #include "cru/ui/render/RenderObject.h" + #include "cru/ui/render/ScrollRenderObject.h" #include <memory> diff --git a/include/cru/ui/controls/TextBlock.h b/include/cru/ui/controls/TextBlock.h index 5e5674a2..3f87d96c 100644 --- a/include/cru/ui/controls/TextBlock.h +++ b/include/cru/ui/controls/TextBlock.h @@ -1,10 +1,12 @@ #pragma once #include "NoChildControl.h" +#include "../render/TextRenderObject.h" #include "TextHostControlService.h" namespace cru::ui::controls { -class CRU_UI_API TextBlock : public NoChildControl, public virtual ITextHostControl { +class CRU_UI_API TextBlock : public NoChildControl, + public virtual ITextHostControl { public: static constexpr StringView control_type = u"TextBlock"; diff --git a/include/cru/ui/controls/TextBox.h b/include/cru/ui/controls/TextBox.h index 2ed7db5e..10f67b72 100644 --- a/include/cru/ui/controls/TextBox.h +++ b/include/cru/ui/controls/TextBox.h @@ -1,6 +1,7 @@ #pragma once #include "NoChildControl.h" +#include "../render/TextRenderObject.h" #include "IBorderControl.h" #include "TextHostControlService.h" @@ -8,8 +9,8 @@ namespace cru::ui::controls { class CRU_UI_API TextBox : public NoChildControl, - public virtual IBorderControl, - public virtual ITextHostControl { + public virtual IBorderControl, + public virtual ITextHostControl { public: static constexpr StringView control_type = u"TextBox"; diff --git a/include/cru/ui/controls/TextHostControlService.h b/include/cru/ui/controls/TextHostControlService.h index 5178107f..67b075c5 100644 --- a/include/cru/ui/controls/TextHostControlService.h +++ b/include/cru/ui/controls/TextHostControlService.h @@ -1,6 +1,7 @@ #pragma once #include "Base.h" +#include "../render/TextRenderObject.h" #include "cru/platform/gui/InputMethod.h" #include "cru/platform/gui/TimerHelper.h" #include "cru/platform/gui/UiApplication.h" diff --git a/include/cru/ui/host/WindowHost.h b/include/cru/ui/host/WindowHost.h index 8a8f1da5..80076afd 100644 --- a/include/cru/ui/host/WindowHost.h +++ b/include/cru/ui/host/WindowHost.h @@ -1,7 +1,6 @@ #pragma once #include "../Base.h" -#include "../render/Base.h" #include "cru/common/Event.h" #include "cru/platform/gui/Cursor.h" #include "cru/platform/gui/UiApplication.h" diff --git a/include/cru/ui/render/Base.h b/include/cru/ui/render/Base.h deleted file mode 100644 index fca67086..00000000 --- a/include/cru/ui/render/Base.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once -#include "../Base.h" - -namespace cru::ui::render { -class RenderObject; -class BorderRenderObject; -class CanvasRenderObject; -class FlexLayoutRenderObject; -class ScrollRenderObject; -class StackLayoutRenderObject; -class TextRenderObject; -} // namespace cru::ui::render diff --git a/include/cru/ui/render/BorderRenderObject.h b/include/cru/ui/render/BorderRenderObject.h index 1720d680..2fc74f0f 100644 --- a/include/cru/ui/render/BorderRenderObject.h +++ b/include/cru/ui/render/BorderRenderObject.h @@ -1,10 +1,10 @@ #pragma once -#include "RenderObject.h" +#include "SingleChildRenderObject.h" #include "../style/ApplyBorderStyleInfo.h" namespace cru::ui::render { -class CRU_UI_API BorderRenderObject : public RenderObject { +class CRU_UI_API BorderRenderObject : public SingleChildRenderObject { CRU_DEFINE_CLASS_LOG_TAG(u"cru::ui::render::BorderRenderObject") public: @@ -13,57 +13,31 @@ class CRU_UI_API BorderRenderObject : public RenderObject { CRU_DELETE_MOVE(BorderRenderObject) ~BorderRenderObject() override; - RenderObject* GetChild() const { return child_; } - void SetChild(RenderObject* new_child); - bool IsBorderEnabled() const { return is_border_enabled_; } - void SetBorderEnabled(bool enabled) { is_border_enabled_ = enabled; } + void SetBorderEnabled(bool enabled); std::shared_ptr<platform::graphics::IBrush> GetBorderBrush() { return border_brush_; } - - void SetBorderBrush(std::shared_ptr<platform::graphics::IBrush> brush) { - if (brush == border_brush_) return; - border_brush_ = std::move(brush); - InvalidatePaint(); - } + void SetBorderBrush(std::shared_ptr<platform::graphics::IBrush> brush); Thickness GetBorderThickness() const { return border_thickness_; } - - void SetBorderThickness(const Thickness thickness) { - if (thickness == border_thickness_) return; - border_thickness_ = thickness; - InvalidateLayout(); - } + void SetBorderThickness(const Thickness thickness); CornerRadius GetBorderRadius() { return border_radius_; } - - void SetBorderRadius(const CornerRadius radius) { - if (radius == border_radius_) return; - border_radius_ = radius; - RecreateGeometry(); - } + void SetBorderRadius(const CornerRadius radius); std::shared_ptr<platform::graphics::IBrush> GetForegroundBrush() { return foreground_brush_; } - void SetForegroundBrush(std::shared_ptr<platform::graphics::IBrush> brush) { - if (brush == foreground_brush_) return; - foreground_brush_ = std::move(brush); - InvalidatePaint(); - } + void SetForegroundBrush(std::shared_ptr<platform::graphics::IBrush> brush); std::shared_ptr<platform::graphics::IBrush> GetBackgroundBrush() { return background_brush_; } - void SetBackgroundBrush(std::shared_ptr<platform::graphics::IBrush> brush) { - if (brush == background_brush_) return; - background_brush_ = std::move(brush); - InvalidatePaint(); - } + void SetBackgroundBrush(std::shared_ptr<platform::graphics::IBrush> brush); void ApplyBorderStyle(const style::ApplyBorderStyleInfo& style); @@ -74,7 +48,7 @@ class CRU_UI_API BorderRenderObject : public RenderObject { Rect GetPaddingRect() const override; Rect GetContentRect() const override; - String GetName() const override { return u"BorderRenderObject"; } + String GetName() const override; protected: Size OnMeasureContent(const MeasureRequirement& requirement, @@ -87,8 +61,6 @@ class CRU_UI_API BorderRenderObject : public RenderObject { void RecreateGeometry(); private: - RenderObject* child_ = nullptr; - bool is_border_enabled_ = false; std::shared_ptr<platform::graphics::IBrush> border_brush_; diff --git a/include/cru/ui/render/FlexLayoutRenderObject.h b/include/cru/ui/render/FlexLayoutRenderObject.h index 164bae39..ff7340cb 100644 --- a/include/cru/ui/render/FlexLayoutRenderObject.h +++ b/include/cru/ui/render/FlexLayoutRenderObject.h @@ -2,6 +2,24 @@ #include "LayoutRenderObject.h" namespace cru::ui::render { + +enum class FlexDirection { + Horizontal, + HorizontalReverse, + Vertical, + VertivalReverse +}; + +using FlexMainAlignment = Alignment; +using FlexCrossAlignment = Alignment; + +struct FlexChildLayoutData { + float expand_factor = 0; + float shrink_factor = 1; + // nullopt stands for looking at parent's setting + std::optional<FlexCrossAlignment> cross_alignment = std::nullopt; +}; + // Measure Logic (v0.1): // Cross axis measure logic is the same as stack layout. // diff --git a/include/cru/ui/render/LayoutHelper.h b/include/cru/ui/render/LayoutHelper.h index c2377066..05813489 100644 --- a/include/cru/ui/render/LayoutHelper.h +++ b/include/cru/ui/render/LayoutHelper.h @@ -1,14 +1,8 @@ #pragma once -#include "Base.h" - #include "MeasureRequirement.h" namespace cru::ui::render { float CalculateAnchorByAlignment(Alignment alignment, float start_point, float content_length, float child_length); -MeasureLength StackLayoutCalculateChildMaxLength( - MeasureLength parent_preferred_size, MeasureLength parent_max_size, - MeasureLength child_min_size, std::u16string_view log_tag, - std::u16string_view exceeds_message); } // namespace cru::ui::render diff --git a/include/cru/ui/render/LayoutRenderObject.h b/include/cru/ui/render/LayoutRenderObject.h index 05da1bb8..62ee9005 100644 --- a/include/cru/ui/render/LayoutRenderObject.h +++ b/include/cru/ui/render/LayoutRenderObject.h @@ -1,7 +1,7 @@ #pragma once #include "RenderObject.h" -#include "cru/platform/graphics/util/Painter.h" +#include "cru/platform/graphics/Painter.h" namespace cru::ui::render { template <typename TChildLayoutData> @@ -54,6 +54,16 @@ class CRU_UI_API LayoutRenderObject : public RenderObject { return children_[position].layout_data; } + void Draw(platform::graphics::IPainter* painter) override { + for (auto& child : children_) { + painter->PushState(); + painter->ConcatTransform( + Matrix::Translation(child.render_object->GetOffset())); + child.render_object->Draw(painter); + painter->PopState(); + } + } + RenderObject* HitTest(const Point& point) override { const auto child_count = GetChildCount(); for (auto i = child_count - 1; i >= 0; --i) { diff --git a/include/cru/ui/render/MeasureRequirement.h b/include/cru/ui/render/MeasureRequirement.h index c740385b..832b936c 100644 --- a/include/cru/ui/render/MeasureRequirement.h +++ b/include/cru/ui/render/MeasureRequirement.h @@ -1,11 +1,10 @@ #pragma once -#include "Base.h" +#include "../Base.h" #include "cru/common/String.h" #include <algorithm> #include <limits> -#include <string> namespace cru::ui::render { constexpr Size Min(const Size& left, const Size& right) { diff --git a/include/cru/ui/render/RenderObject.h b/include/cru/ui/render/RenderObject.h index 67ef6162..cfa72b7d 100644 --- a/include/cru/ui/render/RenderObject.h +++ b/include/cru/ui/render/RenderObject.h @@ -1,10 +1,8 @@ #pragma once -#include "Base.h" +#include "../Base.h" #include "MeasureRequirement.h" -#include "cru/common/Base.h" #include "cru/common/String.h" -#include "cru/ui/Base.h" namespace cru::ui::render { // Render object will not destroy its children when destroyed. Control must diff --git a/include/cru/ui/render/ScrollBar.h b/include/cru/ui/render/ScrollBar.h index 84d4375b..c65c0345 100644 --- a/include/cru/ui/render/ScrollBar.h +++ b/include/cru/ui/render/ScrollBar.h @@ -1,5 +1,4 @@ #pragma once -#include "Base.h" #include "cru/common/Base.h" #include "cru/common/Event.h" #include "cru/platform/graphics/Base.h" diff --git a/include/cru/ui/render/ScrollRenderObject.h b/include/cru/ui/render/ScrollRenderObject.h index 0890ec21..53c64c6c 100644 --- a/include/cru/ui/render/ScrollRenderObject.h +++ b/include/cru/ui/render/ScrollRenderObject.h @@ -1,10 +1,8 @@ #pragma once -#include "RenderObject.h" +#include "SingleChildRenderObject.h" +#include "ScrollBar.h" #include "cru/common/Event.h" -#include "cru/platform/graphics/util/Painter.h" -#include "cru/ui/Base.h" -#include "cru/ui/render/ScrollBar.h" #include <memory> #include <optional> @@ -18,7 +16,7 @@ namespace cru::ui::render { // Layout logic: // If child is smaller than content area, layout at lefttop. // Or layout by scroll state. -class CRU_UI_API ScrollRenderObject : public RenderObject { +class CRU_UI_API ScrollRenderObject : public SingleChildRenderObject { public: ScrollRenderObject(); @@ -27,9 +25,6 @@ class CRU_UI_API ScrollRenderObject : public RenderObject { ~ScrollRenderObject() override = default; - RenderObject* GetChild() const { return child_; } - void SetChild(RenderObject* new_child); - RenderObject* HitTest(const Point& point) override; // Return the coerced scroll offset. @@ -91,8 +86,6 @@ class CRU_UI_API ScrollRenderObject : public RenderObject { void InstallMouseWheelHandler(controls::Control* control); private: - RenderObject* child_; - Point scroll_offset_; std::unique_ptr<ScrollBarDelegate> scroll_bar_delegate_; diff --git a/include/cru/ui/render/SingleChildRenderObject.h b/include/cru/ui/render/SingleChildRenderObject.h new file mode 100644 index 00000000..cc9e4126 --- /dev/null +++ b/include/cru/ui/render/SingleChildRenderObject.h @@ -0,0 +1,23 @@ +#pragma once +#include "RenderObject.h" + +namespace cru::ui::render { +class CRU_UI_API SingleChildRenderObject : public RenderObject { + CRU_DEFINE_CLASS_LOG_TAG(u"cru::ui::render::SingleChildRenderObject") + + public: + SingleChildRenderObject() = default; + CRU_DELETE_COPY(SingleChildRenderObject) + CRU_DELETE_MOVE(SingleChildRenderObject) + ~SingleChildRenderObject() override = default; + + RenderObject* GetChild() const { return child_; } + void SetChild(RenderObject* new_child); + + protected: + virtual void OnChildChanged(RenderObject* old_child, RenderObject* new_child); + + private: + RenderObject* child_ = nullptr; +}; +} // namespace cru::ui::render diff --git a/include/cru/ui/render/StackLayoutRenderObject.h b/include/cru/ui/render/StackLayoutRenderObject.h index 68b5b30c..65b81b3d 100644 --- a/include/cru/ui/render/StackLayoutRenderObject.h +++ b/include/cru/ui/render/StackLayoutRenderObject.h @@ -1,6 +1,5 @@ #pragma once #include "LayoutRenderObject.h" -#include "cru/ui/Base.h" namespace cru::ui::render { // Measure Logic: diff --git a/include/cru/ui/render/TextRenderObject.h b/include/cru/ui/render/TextRenderObject.h index f3a7332f..db8ff0d0 100644 --- a/include/cru/ui/render/TextRenderObject.h +++ b/include/cru/ui/render/TextRenderObject.h @@ -101,8 +101,6 @@ class CRU_UI_API TextRenderObject : public RenderObject { const MeasureSize& preferred_size) override; void OnLayoutContent(const Rect& content_rect) override; - void OnResize(const Size& size) override; - private: std::shared_ptr<platform::graphics::IBrush> brush_; std::shared_ptr<platform::graphics::IFont> font_; diff --git a/src/ui/controls/FlexLayout.cpp b/src/ui/controls/FlexLayout.cpp index 02ebb031..ecf4d428 100644 --- a/src/ui/controls/FlexLayout.cpp +++ b/src/ui/controls/FlexLayout.cpp @@ -1,7 +1,5 @@ #include "cru/ui/controls/FlexLayout.h" -#include "cru/ui/render/FlexLayoutRenderObject.h" - namespace cru::ui::controls { using render::FlexLayoutRenderObject; diff --git a/src/ui/controls/RootControl.cpp b/src/ui/controls/RootControl.cpp index ac4d4d6d..828822c6 100644 --- a/src/ui/controls/RootControl.cpp +++ b/src/ui/controls/RootControl.cpp @@ -5,8 +5,6 @@ #include "cru/platform/gui/Window.h" #include "cru/ui/Base.h" #include "cru/ui/host/WindowHost.h" -#include "cru/ui/render/Base.h" -#include "cru/ui/render/StackLayoutRenderObject.h" #include "gsl/pointers" #include <memory> diff --git a/src/ui/render/BorderRenderObject.cpp b/src/ui/render/BorderRenderObject.cpp index 5916c7aa..30493a49 100644 --- a/src/ui/render/BorderRenderObject.cpp +++ b/src/ui/render/BorderRenderObject.cpp @@ -4,10 +4,8 @@ #include "cru/common/Logger.h" #include "cru/platform/graphics/Factory.h" #include "cru/platform/graphics/Geometry.h" -#include "cru/platform/graphics/util/Painter.h" -#include "cru/ui/Base.h" +#include "cru/platform/graphics/Painter.h" #include "cru/ui/DebugFlags.h" -#include "cru/ui/style/ApplyBorderStyleInfo.h" #include <algorithm> @@ -16,14 +14,6 @@ BorderRenderObject::BorderRenderObject() { RecreateGeometry(); } BorderRenderObject::~BorderRenderObject() {} -void BorderRenderObject::SetChild(RenderObject* new_child) { - if (child_ == new_child) return; - if (child_ != nullptr) child_->SetParent(nullptr); - child_ = new_child; - if (child_ != nullptr) child_->SetParent(this); - InvalidateLayout(); -} - void BorderRenderObject::ApplyBorderStyle( const style::ApplyBorderStyleInfo& style) { if (style.border_brush) border_brush_ = *style.border_brush; @@ -34,27 +24,57 @@ void BorderRenderObject::ApplyBorderStyle( InvalidateLayout(); } +void BorderRenderObject::SetBorderEnabled(bool enabled) { + if (is_border_enabled_ == enabled) return; + is_border_enabled_ = enabled; + InvalidateLayout(); +} + +void BorderRenderObject::SetBorderBrush( + std::shared_ptr<platform::graphics::IBrush> brush) { + if (brush == border_brush_) return; + border_brush_ = std::move(brush); + InvalidatePaint(); +} + +void BorderRenderObject::SetBorderThickness(const Thickness thickness) { + if (thickness == border_thickness_) return; + border_thickness_ = thickness; + InvalidateLayout(); +} + +void BorderRenderObject::SetBorderRadius(const CornerRadius radius) { + if (radius == border_radius_) return; + border_radius_ = radius; + RecreateGeometry(); +} + +void BorderRenderObject::SetForegroundBrush( + std::shared_ptr<platform::graphics::IBrush> brush) { + if (brush == foreground_brush_) return; + foreground_brush_ = std::move(brush); + InvalidatePaint(); +} + +void BorderRenderObject::SetBackgroundBrush( + std::shared_ptr<platform::graphics::IBrush> brush) { + if (brush == background_brush_) return; + background_brush_ = std::move(brush); + InvalidatePaint(); +} + RenderObject* BorderRenderObject::HitTest(const Point& point) { - if (child_) { - const auto result = child_->HitTest(point - child_->GetOffset()); + if (auto child = GetChild()) { + const auto result = child->HitTest(point - child->GetOffset()); if (result != nullptr) { return result; } } if (is_border_enabled_) { - const auto contains = - border_outer_geometry_->FillContains(Point{point.x, point.y}); - return contains ? this : nullptr; + return border_outer_geometry_->FillContains(point) ? this : nullptr; } else { - const auto margin = GetMargin(); - const auto size = GetDesiredSize(); - return Rect{margin.left, margin.top, - std::max(size.width - margin.GetHorizontalTotal(), 0.0f), - std::max(size.height - margin.GetVerticalTotal(), 0.0f)} - .IsPointInside(point) - ? this - : nullptr; + return GetPaddingRect().IsPointInside(point) ? this : nullptr; } } @@ -80,10 +100,10 @@ void BorderRenderObject::Draw(platform::graphics::IPainter* painter) { } } - if (child_) { + if (auto child = GetChild()) { painter->PushState(); - painter->ConcatTransform(Matrix::Translation(child_->GetOffset())); - child_->Draw(painter); + painter->ConcatTransform(Matrix::Translation(child->GetOffset())); + child->Draw(painter); painter->PopState(); } @@ -94,17 +114,17 @@ void BorderRenderObject::Draw(platform::graphics::IPainter* painter) { Size BorderRenderObject::OnMeasureContent(const MeasureRequirement& requirement, const MeasureSize& preferred_size) { - if (child_) { - child_->Measure(requirement, preferred_size); - return child_->GetDesiredSize(); + if (auto child = GetChild()) { + child->Measure(requirement, preferred_size); + return child->GetDesiredSize(); } else { return Size{}; } } void BorderRenderObject::OnLayoutContent(const Rect& content_rect) { - if (child_) { - child_->Layout(content_rect.GetLeftTop()); + if (auto child = GetChild()) { + child->Layout(content_rect.GetLeftTop()); } } @@ -117,7 +137,7 @@ Thickness BorderRenderObject::GetOuterSpaceThickness() const { } Rect BorderRenderObject::GetPaddingRect() const { - const auto size = GetDesiredSize(); + const auto size = GetSize(); Rect rect{Point{}, size}; rect = rect.Shrink(GetMargin()); if (is_border_enabled_) rect = rect.Shrink(border_thickness_); @@ -209,4 +229,6 @@ void BorderRenderObject::RecreateGeometry() { geometry_ = builder->Build(); builder.reset(); } + +String BorderRenderObject::GetName() const { return u"BorderRenderObject"; } } // namespace cru::ui::render diff --git a/src/ui/render/CanvasRenderObject.cpp b/src/ui/render/CanvasRenderObject.cpp index e818333c..431b8f56 100644 --- a/src/ui/render/CanvasRenderObject.cpp +++ b/src/ui/render/CanvasRenderObject.cpp @@ -1,7 +1,7 @@ #include "cru/ui/render/CanvasRenderObject.h" namespace cru::ui::render { -CanvasRenderObject::CanvasRenderObject() : RenderObject() {} +CanvasRenderObject::CanvasRenderObject() {} CanvasRenderObject::~CanvasRenderObject() = default; diff --git a/src/ui/render/FlexLayoutRenderObject.cpp b/src/ui/render/FlexLayoutRenderObject.cpp index 261a0f43..ebe60395 100644 --- a/src/ui/render/FlexLayoutRenderObject.cpp +++ b/src/ui/render/FlexLayoutRenderObject.cpp @@ -1,10 +1,7 @@ #include "cru/ui/render/FlexLayoutRenderObject.h" #include "cru/common/Logger.h" -#include "cru/platform/graphics/util/Painter.h" -#include "cru/ui/Base.h" #include "cru/ui/render/LayoutHelper.h" -#include "cru/ui/render/MeasureRequirement.h" #include <algorithm> #include <functional> diff --git a/src/ui/render/LayoutHelper.cpp b/src/ui/render/LayoutHelper.cpp index d7f6df8a..7a82bb2d 100644 --- a/src/ui/render/LayoutHelper.cpp +++ b/src/ui/render/LayoutHelper.cpp @@ -6,11 +6,11 @@ namespace cru::ui::render { float CalculateAnchorByAlignment(Alignment alignment, float start_point, float content_length, float child_length) { switch (alignment) { - case FlexCrossAlignment::Start: + case Alignment::Start: return start_point; - case FlexCrossAlignment::Center: + case Alignment::Center: return start_point + (content_length - child_length) / 2.0f; - case FlexCrossAlignment::End: + case Alignment::End: return start_point + content_length - child_length; default: return start_point; diff --git a/src/ui/render/ScrollBar.cpp b/src/ui/render/ScrollBar.cpp index e6389e16..695caa03 100644 --- a/src/ui/render/ScrollBar.cpp +++ b/src/ui/render/ScrollBar.cpp @@ -448,7 +448,7 @@ void HorizontalScrollBar::DrawDownArrow( } bool HorizontalScrollBar::IsShowBar() { - const auto child = render_object_->GetFirstChild(); + const auto child = render_object_->GetChild(); if (child == nullptr) return false; const auto view_rect = render_object_->GetViewRect(); @@ -466,7 +466,7 @@ std::optional<Rect> HorizontalScrollBar::GetExpandedAreaRect( const auto padding_rect = render_object_->GetPaddingRect(); - const auto child = render_object_->GetFirstChild(); + const auto child = render_object_->GetChild(); const auto view_rect = render_object_->GetViewRect(); const auto child_size = child->GetDesiredSize(); @@ -518,7 +518,7 @@ std::optional<Rect> HorizontalScrollBar::GetCollapsedThumbRect() { auto show = IsShowBar(); if (!show) return std::nullopt; - const auto child = render_object_->GetFirstChild(); + const auto child = render_object_->GetChild(); const auto view_rect = render_object_->GetViewRect(); const auto child_size = child->GetDesiredSize(); @@ -546,7 +546,7 @@ float HorizontalScrollBar::CalculateNewScrollPosition( auto thumb_head_end = scroll_area_end - thumb_original_rect.width; - const auto child = render_object_->GetFirstChild(); + const auto child = render_object_->GetChild(); const auto child_size = child->GetDesiredSize(); new_thumb_start = @@ -597,7 +597,7 @@ void VerticalScrollBar::DrawDownArrow( } bool VerticalScrollBar::IsShowBar() { - const auto child = render_object_->GetFirstChild(); + const auto child = render_object_->GetChild(); if (child == nullptr) return false; const auto view_rect = render_object_->GetViewRect(); @@ -615,7 +615,7 @@ std::optional<Rect> VerticalScrollBar::GetExpandedAreaRect( const auto padding_rect = render_object_->GetPaddingRect(); - const auto child = render_object_->GetFirstChild(); + const auto child = render_object_->GetChild(); const auto view_rect = render_object_->GetViewRect(); const auto child_size = child->GetDesiredSize(); @@ -663,7 +663,7 @@ std::optional<Rect> VerticalScrollBar::GetCollapsedTriggerExpandAreaRect() { } std::optional<Rect> VerticalScrollBar::GetCollapsedThumbRect() { - const auto child = render_object_->GetFirstChild(); + const auto child = render_object_->GetChild(); if (child == nullptr) return std::nullopt; const auto view_rect = render_object_->GetViewRect(); @@ -693,7 +693,7 @@ float VerticalScrollBar::CalculateNewScrollPosition( auto thumb_head_end = scroll_area_end - thumb_original_rect.height; - const auto child = render_object_->GetFirstChild(); + const auto child = render_object_->GetChild(); const auto child_size = child->GetDesiredSize(); new_thumb_start = diff --git a/src/ui/render/ScrollRenderObject.cpp b/src/ui/render/ScrollRenderObject.cpp index e2710de4..03bb1a2d 100644 --- a/src/ui/render/ScrollRenderObject.cpp +++ b/src/ui/render/ScrollRenderObject.cpp @@ -1,8 +1,6 @@ #include "cru/ui/render/ScrollRenderObject.h" #include "cru/platform/graphics/Painter.h" -#include "cru/platform/graphics/util/Painter.h" -#include "cru/ui/Base.h" #include "cru/ui/controls/Control.h" #include "cru/ui/render/ScrollBar.h" @@ -38,19 +36,12 @@ Point CoerceScroll(const Point& scroll_offset, const Size& content_size, } } // namespace -ScrollRenderObject::ScrollRenderObject() : RenderObject() { +ScrollRenderObject::ScrollRenderObject() { scroll_bar_delegate_ = std::make_unique<ScrollBarDelegate>(this); scroll_bar_delegate_->ScrollAttemptEvent()->AddHandler( [this](const struct Scroll& scroll) { this->ApplyScroll(scroll); }); } -void ScrollRenderObject::SetChild(RenderObject* new_child) { - if (child_ == new_child) return; - if (child_) child_->SetParent(nullptr); - child_ = new_child; - if (child_) child_->SetParent(this); -} - void ScrollRenderObject::ApplyScroll(const struct Scroll& scroll) { auto direction = scroll.direction; @@ -79,9 +70,9 @@ void ScrollRenderObject::ApplyScroll(const struct Scroll& scroll) { } RenderObject* ScrollRenderObject::HitTest(const Point& point) { - if (child_) { - const auto offset = child_->GetOffset(); - const auto r = child_->HitTest(point - offset); + if (auto child = GetChild()) { + const auto offset = child->GetOffset(); + const auto r = child->HitTest(point - offset); if (r != nullptr) return r; } @@ -90,12 +81,12 @@ RenderObject* ScrollRenderObject::HitTest(const Point& point) { } // namespace cru::ui::render void ScrollRenderObject::Draw(platform::graphics::IPainter* painter) { - if (child_) { + if (auto child = GetChild()) { painter->PushLayer(this->GetContentRect()); - const auto offset = child_->GetOffset(); + const auto offset = child->GetOffset(); painter->PushState(); painter->ConcatTransform(Matrix::Translation(offset)); - child_->Draw(painter); + child->Draw(painter); painter->PopState(); painter->PopLayer(); } @@ -103,11 +94,12 @@ void ScrollRenderObject::Draw(platform::graphics::IPainter* painter) { } Point ScrollRenderObject::GetScrollOffset() { - if (child_) + if (auto child = GetChild()) { return CoerceScroll(scroll_offset_, GetContentRect().GetSize(), - child_->GetDesiredSize()); - else + child->GetSize()); + } else { return scroll_offset_; + } } void ScrollRenderObject::SetScrollOffset(const Point& offset) { @@ -171,12 +163,12 @@ void ScrollRenderObject::SetMouseWheelScrollEnabled(bool enable) { Size ScrollRenderObject::OnMeasureContent(const MeasureRequirement& requirement, const MeasureSize& preferred_size) { - if (child_) { - child_->Measure(MeasureRequirement{MeasureSize::NotSpecified(), - MeasureSize::NotSpecified()}, - MeasureSize::NotSpecified()); + if (auto child = GetChild()) { + child->Measure(MeasureRequirement{MeasureSize::NotSpecified(), + MeasureSize::NotSpecified()}, + MeasureSize::NotSpecified()); - Size result = requirement.Coerce(child_->GetDesiredSize()); + Size result = requirement.Coerce(child->GetDesiredSize()); if (preferred_size.width.IsSpecified()) { result.width = preferred_size.width.GetLengthOrUndefined(); } @@ -196,8 +188,8 @@ Size ScrollRenderObject::OnMeasureContent(const MeasureRequirement& requirement, } // namespace cru::ui::render void ScrollRenderObject::OnLayoutContent(const Rect& content_rect) { - if (child_) { - child_->Layout(content_rect.GetLeftTop() - GetScrollOffset()); + if (auto child = GetChild()) { + child->Layout(content_rect.GetLeftTop() - GetScrollOffset()); } } @@ -255,8 +247,10 @@ bool ScrollRenderObject::HorizontalCanScrollUp() { } bool ScrollRenderObject::HorizontalCanScrollDown() { + auto child = GetChild(); + if (child == nullptr) return false; return GetScrollOffset().x < - child_->GetDesiredSize().width - GetViewRect().width; + child->GetDesiredSize().width - GetViewRect().width; } bool ScrollRenderObject::VerticalCanScrollUp() { @@ -264,7 +258,9 @@ bool ScrollRenderObject::VerticalCanScrollUp() { } bool ScrollRenderObject::VerticalCanScrollDown() { + auto child = GetChild(); + if (child == nullptr) return false; return GetScrollOffset().y < - child_->GetDesiredSize().height - GetViewRect().height; + child->GetDesiredSize().height - GetViewRect().height; } } // namespace cru::ui::render diff --git a/src/ui/render/SingleChildRenderObject.cpp b/src/ui/render/SingleChildRenderObject.cpp new file mode 100644 index 00000000..7b10b343 --- /dev/null +++ b/src/ui/render/SingleChildRenderObject.cpp @@ -0,0 +1,22 @@ +#include "cru/ui/render/SingleChildRenderObject.h" + +namespace cru::ui::render { +void SingleChildRenderObject::SetChild(RenderObject *new_child) { + assert(new_child->GetParent() == nullptr); + if (child_ == new_child) return; + auto old_child = child_; + if (child_) { + child_->SetParent(nullptr); + } + child_ = new_child; + if (child_) { + child_->SetParent(this); + } + OnChildChanged(old_child, new_child); +} + +void SingleChildRenderObject::OnChildChanged(RenderObject *old_child, + RenderObject *new_child) { + InvalidateLayout(); +} +} // namespace cru::ui::render diff --git a/src/ui/render/TextRenderObject.cpp b/src/ui/render/TextRenderObject.cpp index ec5ce662..6f4a8320 100644 --- a/src/ui/render/TextRenderObject.cpp +++ b/src/ui/render/TextRenderObject.cpp @@ -3,8 +3,8 @@ #include "../Helper.h" #include "cru/common/Logger.h" #include "cru/platform/graphics/Factory.h" +#include "cru/platform/graphics/Painter.h" #include "cru/platform/graphics/TextLayout.h" -#include "cru/platform/graphics/util/Painter.h" #include "cru/ui/DebugFlags.h" #include <algorithm> |