diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/ui/controls/Control.cpp | 4 | ||||
| -rw-r--r-- | src/ui/controls/ControlHost.cpp | 10 | ||||
| -rw-r--r-- | src/ui/render/RenderObject.cpp | 39 |
3 files changed, 35 insertions, 18 deletions
diff --git a/src/ui/controls/Control.cpp b/src/ui/controls/Control.cpp index 41644755..2a94252f 100644 --- a/src/ui/controls/Control.cpp +++ b/src/ui/controls/Control.cpp @@ -110,7 +110,7 @@ void Control::InsertChildAt(Control* control, Index index) { OnChildInserted(control, index); if (host_) { - host_->InvalidateLayout(); + host_->ScheduleRelayout(); } } @@ -131,7 +131,7 @@ void Control::RemoveChildAt(Index index) { OnChildRemoved(control, index); if (host_) { - host_->InvalidateLayout(); + host_->ScheduleRelayout(); } } diff --git a/src/ui/controls/ControlHost.cpp b/src/ui/controls/ControlHost.cpp index caa907da..7c934b7b 100644 --- a/src/ui/controls/ControlHost.cpp +++ b/src/ui/controls/ControlHost.cpp @@ -124,9 +124,9 @@ ControlHost::CreateNativeWindow() { return std::unique_ptr<platform::gui::INativeWindow>(native_window); } -void ControlHost::InvalidatePaint() { native_window_->RequestRepaint(); } +void ControlHost::ScheduleRepaint() { native_window_->RequestRepaint(); } -void ControlHost::InvalidateLayout() { +void ControlHost::ScheduleRelayout() { relayout_schedule_canceler_.Reset( platform::gui::IUiApplication::GetInstance()->SetImmediate( [this] { Relayout(); })); @@ -139,7 +139,7 @@ bool ControlHost::IsLayoutPreferToFillWindow() const { void ControlHost::SetLayoutPreferToFillWindow(bool value) { if (value == layout_prefer_to_fill_window_) return; layout_prefer_to_fill_window_ = value; - InvalidateLayout(); + ScheduleRelayout(); } void ControlHost::Repaint() { @@ -172,7 +172,7 @@ void ControlHost::RelayoutWithSize(const Size& available_size, AfterLayoutEvent_.Raise(nullptr); - InvalidatePaint(); + ScheduleRepaint(); } Control* ControlHost::GetFocusControl() { return focus_control_; } @@ -250,7 +250,7 @@ void ControlHost::OnNativeResize(platform::gui::INativeWindow* window, CRU_UNUSED(window) CRU_UNUSED(size) - InvalidateLayout(); + ScheduleRelayout(); } void ControlHost::OnNativeFocus(platform::gui::INativeWindow* window, diff --git a/src/ui/render/RenderObject.cpp b/src/ui/render/RenderObject.cpp index 59dc6d6a..424fd2ee 100644 --- a/src/ui/render/RenderObject.cpp +++ b/src/ui/render/RenderObject.cpp @@ -10,7 +10,10 @@ namespace cru::ui::render { const BoxConstraint BoxConstraint::kNotLimit{Size::kMax, Size::kZero}; RenderObject::RenderObject(std::string name) - : name_(std::move(name)), control_(nullptr), parent_(nullptr) {} + : name_(std::move(name)), + control_(nullptr), + parent_(nullptr), + layout_valid_(false) {} RenderObject::~RenderObject() { DestroyEvent_.Raise(this); } @@ -82,25 +85,38 @@ void RenderObject::Measure(const MeasureRequirement& requirement) { CRU_LOG_TAG_DEBUG("{} Measure begins, requirement {}.", this->GetDebugPathInTree(), requirement); + if (layout_valid_ && requirement == last_measure_requirement_) { + return; + } + measure_result_size_ = OnMeasureCore(requirement); if (measure_result_size_.width < 0 || measure_result_size_.height < 0) { throw Exception("Measure result size is invalid."); } + last_measure_requirement_ = requirement; + CRU_LOG_TAG_DEBUG("{} Measure ends, result size: {}.", this->GetDebugPathInTree(), measure_result_size_); } void RenderObject::Layout(const Point& offset) { - CRU_LOG_TAG_DEBUG("{} Layout begins, offset: {}, size: {}.", - this->GetDebugPathInTree(), offset, measure_result_size_); + Layout({offset, GetMeasureResultSize()}); +} - offset_ = offset; - size_ = measure_result_size_; +void RenderObject::Layout(const Rect& rect) { + CRU_LOG_TAG_DEBUG("{} Layout begins, rect: {}.", this->GetDebugPathInTree(), + rect); - OnResize(size_); + offset_ = rect.GetLeftTop(); + auto new_size = rect.GetSize(); + if (size_ != new_size) { + size_ = new_size; + OnResize(new_size); + } - OnLayoutCore(); + OnLayoutCore(rect); + layout_valid_ = true; CRU_LOG_TAG_DEBUG("{} Layout ends.", this->GetDebugPathInTree()); } @@ -120,8 +136,8 @@ Size RenderObject::OnMeasureCore(const MeasureRequirement& requirement) { return space_size + content_size; } -void RenderObject::OnLayoutCore() { - auto total_size = GetMeasureResultSize(); +void RenderObject::OnLayoutCore(const Rect& rect) { + auto total_size = rect.GetSize(); auto outer_space = GetTotalSpaceThickness(); auto content_size = (total_size - outer_space.GetTotalSize()).AtLeast0(); @@ -165,14 +181,15 @@ controls::ControlHost* RenderObject::GetControlHost() { } void RenderObject::InvalidateLayout() { + WalkUp([](RenderObject* ro) { ro->layout_valid_ = false; }); if (auto host = GetControlHost()) { - host->InvalidateLayout(); + host->ScheduleRelayout(); } } void RenderObject::InvalidatePaint() { if (auto window = GetControlHost()) { - window->InvalidatePaint(); + window->ScheduleRepaint(); } } |
