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 /src/ui/render | |
parent | e18c4fb46d7913f337cc25b9a6e3a25359c3f10a (diff) | |
download | cru-d18b5453d7ffd19667ee8ac125b34ab5328f0dc3.tar.gz cru-d18b5453d7ffd19667ee8ac125b34ab5328f0dc3.tar.bz2 cru-d18b5453d7ffd19667ee8ac125b34ab5328f0dc3.zip |
...
Diffstat (limited to 'src/ui/render')
-rw-r--r-- | src/ui/render/BorderRenderObject.cpp | 88 | ||||
-rw-r--r-- | src/ui/render/CanvasRenderObject.cpp | 2 | ||||
-rw-r--r-- | src/ui/render/FlexLayoutRenderObject.cpp | 3 | ||||
-rw-r--r-- | src/ui/render/LayoutHelper.cpp | 6 | ||||
-rw-r--r-- | src/ui/render/ScrollBar.cpp | 16 | ||||
-rw-r--r-- | src/ui/render/ScrollRenderObject.cpp | 52 | ||||
-rw-r--r-- | src/ui/render/SingleChildRenderObject.cpp | 22 | ||||
-rw-r--r-- | src/ui/render/TextRenderObject.cpp | 2 |
8 files changed, 114 insertions, 77 deletions
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> |