aboutsummaryrefslogtreecommitdiff
path: root/src/ui/controls/frame_layout.cpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2018-12-06 05:44:38 +0800
committercrupest <crupest@outlook.com>2018-12-06 05:44:38 +0800
commit81fd0725d020e9f302c0d40fd5a5700d3dc871aa (patch)
tree1a99936949271641fbdfa3b4fb7ea448344164f7 /src/ui/controls/frame_layout.cpp
parentf0e2b2d3763dd100a5e9d6cda566724d2b7da965 (diff)
downloadcru-81fd0725d020e9f302c0d40fd5a5700d3dc871aa.tar.gz
cru-81fd0725d020e9f302c0d40fd5a5700d3dc871aa.tar.bz2
cru-81fd0725d020e9f302c0d40fd5a5700d3dc871aa.zip
Done.
Diffstat (limited to 'src/ui/controls/frame_layout.cpp')
-rw-r--r--src/ui/controls/frame_layout.cpp62
1 files changed, 58 insertions, 4 deletions
diff --git a/src/ui/controls/frame_layout.cpp b/src/ui/controls/frame_layout.cpp
index 32d25edc..d68bc338 100644
--- a/src/ui/controls/frame_layout.cpp
+++ b/src/ui/controls/frame_layout.cpp
@@ -2,10 +2,7 @@
namespace cru::ui::controls
{
- FrameLayout::FrameLayout() : Control(true)
- {
-
- }
+ FrameLayout::FrameLayout() = default;
FrameLayout::~FrameLayout() = default;
@@ -13,4 +10,61 @@ namespace cru::ui::controls
{
return control_type;
}
+
+ Size FrameLayout::OnMeasureContent(const Size& available_size, const AdditionalMeasureInfo& additional_info)
+ {
+ auto max_child_size = Size::Zero();
+ for (auto control: GetChildren())
+ {
+ control->Measure(available_size, additional_info);
+ const auto&& size = control->GetDesiredSize();
+ if (max_child_size.width < size.width)
+ max_child_size.width = size.width;
+ if (max_child_size.height < size.height)
+ max_child_size.height = size.height;
+ }
+
+ // coerce size fro stretch.
+ for (auto control: GetChildren())
+ {
+ auto size = control->GetDesiredSize();
+ const auto layout_params = control->GetLayoutParams();
+ if (layout_params->width.mode == MeasureMode::Stretch)
+ size.width = max_child_size.width;
+ if (layout_params->height.mode == MeasureMode::Stretch)
+ size.height = max_child_size.height;
+ control->SetDesiredSize(size);
+ }
+
+ return max_child_size;
+ }
+
+ void FrameLayout::OnLayoutContent(const Rect& rect, const AdditionalLayoutInfo& additional_info)
+ {
+ for (auto control: GetChildren())
+ {
+ const auto layout_params = control->GetLayoutParams();
+ const auto size = control->GetDesiredSize();
+
+ auto&& calculate_anchor = [](const float anchor, const Alignment alignment, const float layout_length, const float control_length) -> float
+ {
+ switch (alignment)
+ {
+ case Alignment::Center:
+ return anchor + (layout_length - control_length) / 2;
+ case Alignment::Start:
+ return anchor;
+ case Alignment::End:
+ return anchor + layout_length - control_length;
+ default:
+ UnreachableCode();
+ }
+ };
+
+ control->Layout(Rect(Point(
+ calculate_anchor(rect.left, layout_params->width.alignment, rect.width, size.width),
+ calculate_anchor(rect.top, layout_params->height.alignment, rect.height, size.height)
+ ), size), additional_info);
+ }
+ }
}