diff options
author | crupest <crupest@outlook.com> | 2018-11-07 18:54:41 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2018-11-07 18:54:41 +0800 |
commit | 2b5b89e9483063f3af05fb5485043868d447994b (patch) | |
tree | afb0bff66420d0a631416149142549adb5c45dc2 /src/ui | |
parent | 9f7de7f0775b86e3c82d4c5e3427a6f2fd98810b (diff) | |
download | cru-2b5b89e9483063f3af05fb5485043868d447994b.tar.gz cru-2b5b89e9483063f3af05fb5485043868d447994b.tar.bz2 cru-2b5b89e9483063f3af05fb5485043868d447994b.zip |
Add min max.
Diffstat (limited to 'src/ui')
-rw-r--r-- | src/ui/control.cpp | 18 | ||||
-rw-r--r-- | src/ui/controls/linear_layout.h | 2 | ||||
-rw-r--r-- | src/ui/layout_base.h | 17 |
3 files changed, 26 insertions, 11 deletions
diff --git a/src/ui/control.cpp b/src/ui/control.cpp index 79def066..9416d48a 100644 --- a/src/ui/control.cpp +++ b/src/ui/control.cpp @@ -689,11 +689,6 @@ namespace cru { return value < 0 ? 0 : value; } - inline Size AtLeast0(const Size& size) - { - return Size(AtLeast0(size.width), AtLeast0(size.height)); - } - Size Control::OnMeasureCore(const Size& available_size) { const auto layout_params = GetLayoutParams(); @@ -715,11 +710,14 @@ namespace cru { auto&& get_content_measure_length = [](const LayoutSideParams& layout_length, const float available_length, const float outer_length) -> float { + float length; if (layout_length.mode == MeasureMode::Exactly) - return layout_length.length; - if (available_length > outer_length) - return available_length - outer_length; - return 0.0; + length = layout_length.length; + else if (available_length > outer_length) + length = available_length - outer_length; + else + length = 0; + return Coerce(length, layout_length.min, layout_length.max); }; // if padding, margin and border exceeded, then content size is 0. @@ -735,7 +733,7 @@ namespace cru { // only use measure length when stretch and actual length is smaller than measure length, that is "stretch" if (layout_length.mode == MeasureMode::Stretch && actual_length < measure_length) return measure_length; - return actual_length; + return Coerce(actual_length, layout_length.min, std::nullopt); }; const auto final_size = Size( diff --git a/src/ui/controls/linear_layout.h b/src/ui/controls/linear_layout.h index 1c2232bb..021f4b7d 100644 --- a/src/ui/controls/linear_layout.h +++ b/src/ui/controls/linear_layout.h @@ -4,6 +4,8 @@ namespace cru::ui::controls { + // Min length of main side in layout params is of no meaning. + // All children will layout from start and redundant length is blank. class LinearLayout : public Control { public: diff --git a/src/ui/layout_base.h b/src/ui/layout_base.h index 662210bd..4a4c09ea 100644 --- a/src/ui/layout_base.h +++ b/src/ui/layout_base.h @@ -101,12 +101,27 @@ namespace cru constexpr bool Validate() const { - return length >= 0.0; + if (length < 0.0) + return false; + if (min.has_value() && min.value() < 0.0) + return false; + if (max.has_value() && max.value() < 0.0) + return false; + if (min.has_value() && max.has_value() && min.value() > max.value()) + return false; + return true; } + // only used in exactly mode, specify the exactly side length of content. float length = 0.0; MeasureMode mode = MeasureMode::Content; Alignment alignment = Alignment::Center; + + // min and max specify the min/max side length of content. + // they are used as hint and respect the actual size that content needs. + // when mode is exactly, length is coerced into the min-max range. + std::optional<float> min = std::nullopt; + std::optional<float> max = std::nullopt; }; struct BasicLayoutParams final |