diff options
author | crupest <crupest@outlook.com> | 2018-12-05 15:51:05 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2018-12-05 15:51:05 +0800 |
commit | 01b0378ed32eb2011863393892717483004cc375 (patch) | |
tree | 5cd487e9359c4680a81cd77c304935539c10406e | |
parent | 1166da612f01172270114921eb6785998ef88476 (diff) | |
download | cru-01b0378ed32eb2011863393892717483004cc375.tar.gz cru-01b0378ed32eb2011863393892717483004cc375.tar.bz2 cru-01b0378ed32eb2011863393892717483004cc375.zip |
Avoid search for ancestor in measure.
-rw-r--r-- | src/ui/control.cpp | 89 | ||||
-rw-r--r-- | src/ui/control.hpp | 13 | ||||
-rw-r--r-- | src/ui/controls/linear_layout.cpp | 10 | ||||
-rw-r--r-- | src/ui/controls/linear_layout.hpp | 2 | ||||
-rw-r--r-- | src/ui/controls/scroll_control.cpp | 20 | ||||
-rw-r--r-- | src/ui/controls/scroll_control.hpp | 2 | ||||
-rw-r--r-- | src/ui/controls/text_control.cpp | 2 | ||||
-rw-r--r-- | src/ui/controls/text_control.hpp | 2 | ||||
-rw-r--r-- | src/ui/controls/toggle_button.cpp | 2 | ||||
-rw-r--r-- | src/ui/controls/toggle_button.hpp | 2 | ||||
-rw-r--r-- | src/ui/window.cpp | 4 | ||||
-rw-r--r-- | src/ui/window.hpp | 1 |
12 files changed, 50 insertions, 99 deletions
diff --git a/src/ui/control.cpp b/src/ui/control.cpp index 0fec1ef8..91301e0b 100644 --- a/src/ui/control.cpp +++ b/src/ui/control.cpp @@ -377,11 +377,12 @@ namespace cru::ui window->WindowInvalidateLayout(); } - void Control::Measure(const Size& available_size) + void Control::Measure(const Size& available_size, const AdditionalMeasureInfo& additional_info) { - SetDesiredSize(OnMeasureCore(available_size)); + SetDesiredSize(OnMeasureCore(available_size, additional_info)); } + void Control::Layout(const Rect& rect) { SetPositionRelative(rect.GetLeftTop()); @@ -644,13 +645,28 @@ namespace cru::ui return Size(thickness.left + thickness.right, thickness.top + thickness.bottom); } - Size Control::OnMeasureCore(const Size& available_size) + Size Control::OnMeasureCore(const Size& available_size, const AdditionalMeasureInfo& additional_info) { const auto layout_params = GetLayoutParams(); if (!layout_params->Validate()) throw std::runtime_error("LayoutParams is not valid. Please check it."); + auto my_additional_info = additional_info; + + if (layout_params->width.mode == MeasureMode::Content) + my_additional_info.horizontal_stretchable = false; + else if (layout_params->width.mode == MeasureMode::Exactly) + my_additional_info.horizontal_stretchable = true; + // if stretch, then inherent parent's value + + if (layout_params->height.mode == MeasureMode::Content) + my_additional_info.vertical_stretchable = false; + else if (layout_params->height.mode == MeasureMode::Exactly) + my_additional_info.vertical_stretchable = true; + // if stretch, then inherent parent's value + + auto border_size = Size::Zero(); if (is_bordered_) { @@ -681,66 +697,9 @@ namespace cru::ui get_content_measure_length(layout_params->height, available_size.height, outer_size.height) ); - const auto content_actual_size = OnMeasureContent(content_measure_size); - - auto stretch_width = false; - auto stretch_width_determined = true; - auto stretch_height = false; - auto stretch_height_determined = true; - - // if it is stretch, init is stretch, and undetermined. - if (layout_params->width.mode == MeasureMode::Stretch) - { - stretch_width = true; - stretch_width_determined = false; - } - if (layout_params->height.mode == MeasureMode::Stretch) - { - stretch_height = true; - stretch_width_determined = false; - } - - if (!stretch_width_determined || !stretch_height_determined) - { - auto parent = GetParent(); - while (parent != nullptr) - { - const auto lp = parent->GetLayoutParams(); + const auto content_actual_size = OnMeasureContent(content_measure_size, my_additional_info); - if (!stretch_width_determined) - { - if (lp->width.mode == MeasureMode::Content) // if the first ancestor that is not stretch is content, then it can't stretch. - { - stretch_width = false; - stretch_width_determined = true; - } - if (lp->width.mode == MeasureMode::Exactly) // if the first ancestor that is not stretch is content, then it must be stretch. - { - stretch_width = true; - stretch_width_determined = true; - } - } - if (!stretch_height_determined) // the same as width - { - if (lp->height.mode == MeasureMode::Content) // if the first ancestor that is not stretch is content, then it can't stretch. - { - stretch_height = false; - stretch_height_determined = true; - } - if (lp->height.mode == MeasureMode::Exactly) // if the first ancestor that is not stretch is content, then it must be stretch. - { - stretch_height = true; - stretch_height_determined = true; - } - } - - if (stretch_width_determined && stretch_height_determined) // if both are determined. - break; - - parent = GetParent(); - } - } auto&& calculate_final_length = [](const bool stretch, const std::optional<float> min_length, const float measure_length, const float actual_length) -> float { @@ -751,8 +710,8 @@ namespace cru::ui }; const auto final_size = Size( - calculate_final_length(stretch_width, layout_params->width.min, content_measure_size.width, content_actual_size.width), - calculate_final_length(stretch_height, layout_params->height.min, content_measure_size.height, content_actual_size.height) + calculate_final_length(my_additional_info.horizontal_stretchable, layout_params->width.min, content_measure_size.width, content_actual_size.width), + calculate_final_length(my_additional_info.vertical_stretchable, layout_params->height.min, content_measure_size.height, content_actual_size.height) ) + outer_size; return final_size; @@ -783,12 +742,12 @@ namespace cru::ui OnLayoutContent(content_rect); } - Size Control::OnMeasureContent(const Size& available_size) + Size Control::OnMeasureContent(const Size& available_size, const AdditionalMeasureInfo& additional_info) { auto max_child_size = Size::Zero(); for (auto control: GetChildren()) { - control->Measure(available_size); + control->Measure(available_size, additional_info); const auto&& size = control->GetDesiredSize(); if (max_child_size.width < size.width) max_child_size.width = size.width; diff --git a/src/ui/control.hpp b/src/ui/control.hpp index f3751ddf..b2dad9fd 100644 --- a/src/ui/control.hpp +++ b/src/ui/control.hpp @@ -22,6 +22,13 @@ namespace cru::ui class Window; + struct AdditionalMeasureInfo + { + bool horizontal_stretchable = true; + bool vertical_stretchable = true; + }; + + //the position cache struct ControlPositionCache { @@ -194,7 +201,7 @@ namespace cru::ui void InvalidateLayout(); - void Measure(const Size& available_size); + void Measure(const Size& available_size, const AdditionalMeasureInfo& additional_info); void Layout(const Rect& rect); @@ -314,11 +321,11 @@ namespace cru::ui //*************** region: layout *************** private: - Size OnMeasureCore(const Size& available_size); + Size OnMeasureCore(const Size& available_size, const AdditionalMeasureInfo& additional_info); void OnLayoutCore(const Rect& rect); protected: - virtual Size OnMeasureContent(const Size& available_size); + virtual Size OnMeasureContent(const Size& available_size, const AdditionalMeasureInfo& additional_info); virtual void OnLayoutContent(const Rect& rect); // Called by Layout after set position and size. diff --git a/src/ui/controls/linear_layout.cpp b/src/ui/controls/linear_layout.cpp index 8fb91513..8f2e7e80 100644 --- a/src/ui/controls/linear_layout.cpp +++ b/src/ui/controls/linear_layout.cpp @@ -17,7 +17,7 @@ namespace cru::ui::controls return control_type; } - Size LinearLayout::OnMeasureContent(const Size& available_size) + Size LinearLayout::OnMeasureContent(const Size& available_size, const AdditionalMeasureInfo& additional_info) { auto actual_size_for_children = Size::Zero(); @@ -33,7 +33,7 @@ namespace cru::ui::controls if (mode == MeasureMode::Content || mode == MeasureMode::Exactly) { Size current_available_size(AtLeast0(available_size.width - actual_size_for_children.width), available_size.height); - control->Measure(current_available_size); + control->Measure(current_available_size, additional_info); const auto size = control->GetDesiredSize(); actual_size_for_children.width += size.width; secondary_side_child_max_length = std::max(size.height, secondary_side_child_max_length); @@ -48,7 +48,7 @@ namespace cru::ui::controls if (mode == MeasureMode::Content || mode == MeasureMode::Exactly) { Size current_available_size(available_size.width, AtLeast0(available_size.height - actual_size_for_children.height)); - control->Measure(current_available_size); + control->Measure(current_available_size, additional_info); const auto size = control->GetDesiredSize(); actual_size_for_children.height += size.height; secondary_side_child_max_length = std::max(size.width, secondary_side_child_max_length); @@ -62,7 +62,7 @@ namespace cru::ui::controls const auto available_width = AtLeast0(available_size.width - actual_size_for_children.width) / stretch_control_list.size(); for (const auto control : stretch_control_list) { - control->Measure(Size(available_width, available_size.height)); + control->Measure(Size(available_width, available_size.height), additional_info); const auto size = control->GetDesiredSize(); actual_size_for_children.width += size.width; secondary_side_child_max_length = std::max(size.height, secondary_side_child_max_length); @@ -73,7 +73,7 @@ namespace cru::ui::controls const auto available_height = AtLeast0(available_size.height - actual_size_for_children.height) / stretch_control_list.size(); for (const auto control : stretch_control_list) { - control->Measure(Size(available_size.width, available_height)); + control->Measure(Size(available_size.width, available_height), additional_info); const auto size = control->GetDesiredSize(); actual_size_for_children.height += size.height; secondary_side_child_max_length = std::max(size.width, secondary_side_child_max_length); diff --git a/src/ui/controls/linear_layout.hpp b/src/ui/controls/linear_layout.hpp index deb51bd1..9606b8a6 100644 --- a/src/ui/controls/linear_layout.hpp +++ b/src/ui/controls/linear_layout.hpp @@ -41,7 +41,7 @@ namespace cru::ui::controls StringView GetControlType() const override final; protected: - Size OnMeasureContent(const Size& available_size) override; + Size OnMeasureContent(const Size& available_size, const AdditionalMeasureInfo& additional_info) override; void OnLayoutContent(const Rect& rect) override; private: diff --git a/src/ui/controls/scroll_control.cpp b/src/ui/controls/scroll_control.cpp index 9681924d..db8ac438 100644 --- a/src/ui/controls/scroll_control.cpp +++ b/src/ui/controls/scroll_control.cpp @@ -224,7 +224,7 @@ namespace cru::ui::controls view_height_ = length; } - Size ScrollControl::OnMeasureContent(const Size& available_size) + Size ScrollControl::OnMeasureContent(const Size& available_size, const AdditionalMeasureInfo& additional_info) { const auto layout_params = GetLayoutParams(); @@ -234,13 +234,6 @@ namespace cru::ui::controls if (layout_params->width.mode == MeasureMode::Content) debug::DebugMessage(L"ScrollControl: Width measure mode is Content and horizontal scroll is enabled. So Stretch is used instead."); - for (auto child : GetChildren()) - { - const auto child_layout_params = child->GetLayoutParams(); - if (child_layout_params->width.mode == MeasureMode::Stretch) - throw std::runtime_error(Format("ScrollControl: Horizontal scroll is enabled but a child {} 's width measure mode is Stretch which may cause infinite length.", ToUtf8String(child->GetControlType()))); - } - available_size_for_children.width = std::numeric_limits<float>::max(); } @@ -249,20 +242,13 @@ namespace cru::ui::controls if (layout_params->height.mode == MeasureMode::Content) debug::DebugMessage(L"ScrollControl: Height measure mode is Content and vertical scroll is enabled. So Stretch is used instead."); - for (auto child : GetChildren()) - { - const auto child_layout_params = child->GetLayoutParams(); - if (child_layout_params->height.mode == MeasureMode::Stretch) - throw std::runtime_error(Format("ScrollControl: Vertical scroll is enabled but a child {} 's height measure mode is Stretch which may cause infinite length.", ToUtf8String(child->GetControlType()))); - } - available_size_for_children.height = std::numeric_limits<float>::max(); } auto max_child_size = Size::Zero(); for (auto control: GetChildren()) { - control->Measure(available_size_for_children); + control->Measure(available_size_for_children, AdditionalMeasureInfo{false, false}); const auto&& size = control->GetDesiredSize(); if (max_child_size.width < size.width) max_child_size.width = size.width; @@ -270,7 +256,7 @@ namespace cru::ui::controls max_child_size.height = size.height; } - // coerce size fro stretch. + // coerce size for stretch. for (auto control: GetChildren()) { auto size = control->GetDesiredSize(); diff --git a/src/ui/controls/scroll_control.hpp b/src/ui/controls/scroll_control.hpp index 4eabc605..d70a7b35 100644 --- a/src/ui/controls/scroll_control.hpp +++ b/src/ui/controls/scroll_control.hpp @@ -117,7 +117,7 @@ namespace cru::ui::controls void SetViewWidth(float length); void SetViewHeight(float length); - Size OnMeasureContent(const Size& available_size) override final; + Size OnMeasureContent(const Size& available_size, const AdditionalMeasureInfo& additional_info) override final; void OnLayoutContent(const Rect& rect) override final; void AfterLayoutSelf() override; diff --git a/src/ui/controls/text_control.cpp b/src/ui/controls/text_control.cpp index f32c068f..9a799916 100644 --- a/src/ui/controls/text_control.cpp +++ b/src/ui/controls/text_control.cpp @@ -177,7 +177,7 @@ namespace cru::ui::controls } } - Size TextControl::OnMeasureContent(const Size& available_size) + Size TextControl::OnMeasureContent(const Size& available_size, const AdditionalMeasureInfo&) { ThrowIfFailed(text_layout_->SetMaxWidth(available_size.width)); ThrowIfFailed(text_layout_->SetMaxHeight(available_size.height)); diff --git a/src/ui/controls/text_control.hpp b/src/ui/controls/text_control.hpp index 1e4c985d..fddd8ec7 100644 --- a/src/ui/controls/text_control.hpp +++ b/src/ui/controls/text_control.hpp @@ -62,7 +62,7 @@ namespace cru::ui::controls protected: void SetSelectable(bool is_selectable); - Size OnMeasureContent(const Size& available_size) override final; + Size OnMeasureContent(const Size& available_size, const AdditionalMeasureInfo&) override final; virtual void RequestChangeCaretPosition(unsigned position); diff --git a/src/ui/controls/toggle_button.cpp b/src/ui/controls/toggle_button.cpp index 874a245f..6eb0bc40 100644 --- a/src/ui/controls/toggle_button.cpp +++ b/src/ui/controls/toggle_button.cpp @@ -105,7 +105,7 @@ namespace cru::ui::controls SetState(!GetState()); } - Size ToggleButton::OnMeasureContent(const Size& available_size) + Size ToggleButton::OnMeasureContent(const Size& available_size, const AdditionalMeasureInfo&) { const Size result_size( half_width * 2 + stroke_width, diff --git a/src/ui/controls/toggle_button.hpp b/src/ui/controls/toggle_button.hpp index 8b7402c8..091f908a 100644 --- a/src/ui/controls/toggle_button.hpp +++ b/src/ui/controls/toggle_button.hpp @@ -43,7 +43,7 @@ namespace cru::ui::controls Event<events::ToggleEventArgs> toggle_event; protected: - Size OnMeasureContent(const Size& available_size) override; + Size OnMeasureContent(const Size& available_size, const AdditionalMeasureInfo&) override; private: bool state_ = false; diff --git a/src/ui/window.cpp b/src/ui/window.cpp index ac79392b..b4569366 100644 --- a/src/ui/window.cpp +++ b/src/ui/window.cpp @@ -499,14 +499,14 @@ namespace cru::ui void Window::Relayout() { - Measure(GetSize()); + Measure(GetSize(), AdditionalMeasureInfo{}); OnLayoutCore(Rect(Point::Zero(), GetSize())); is_layout_invalid_ = false; } void Window::SetSizeFitContent(const Size& max_size) { - Measure(max_size); + Measure(max_size, AdditionalMeasureInfo{}); SetClientSize(GetDesiredSize()); OnLayoutCore(Rect(Point::Zero(), GetSize())); is_layout_invalid_ = false; diff --git a/src/ui/window.hpp b/src/ui/window.hpp index d7301eb5..7287c7e5 100644 --- a/src/ui/window.hpp +++ b/src/ui/window.hpp @@ -4,7 +4,6 @@ #include "pre.hpp" #include "system_headers.hpp" -#include <list> #include <map> #include <memory> |