diff options
Diffstat (limited to 'include/cru')
24 files changed, 89 insertions, 100 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_; |