diff options
author | crupest <crupest@outlook.com> | 2018-09-19 23:43:26 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2018-09-19 23:43:26 +0800 |
commit | dea11f34bfab5bfd5f66dec9f2fa0239abf44d89 (patch) | |
tree | 89ba9f88b86c22b25506e43277c3a2fc51a9c2c9 /CruUI/ui/controls | |
parent | 9caec47adf266946fdce290aaf5ec0c865197650 (diff) | |
download | cru-dea11f34bfab5bfd5f66dec9f2fa0239abf44d89.tar.gz cru-dea11f34bfab5bfd5f66dec9f2fa0239abf44d89.tar.bz2 cru-dea11f34bfab5bfd5f66dec9f2fa0239abf44d89.zip |
Improve linear layout. Add debug border visualization.
Diffstat (limited to 'CruUI/ui/controls')
-rw-r--r-- | CruUI/ui/controls/linear_layout.cpp | 60 | ||||
-rw-r--r-- | CruUI/ui/controls/text_block.cpp | 6 | ||||
-rw-r--r-- | CruUI/ui/controls/toggle_button.cpp | 1 |
3 files changed, 57 insertions, 10 deletions
diff --git a/CruUI/ui/controls/linear_layout.cpp b/CruUI/ui/controls/linear_layout.cpp index 33a7855f..5f55ba0a 100644 --- a/CruUI/ui/controls/linear_layout.cpp +++ b/CruUI/ui/controls/linear_layout.cpp @@ -40,23 +40,61 @@ namespace cru::ui::controls auto rest_available_size_for_children = total_available_size_for_children; - ForeachChild([this, &rest_available_size_for_children](Control* const control) + std::list<Control*> stretch_control_list; + + // First measure Content and Exactly and count Stretch. + if (orientation_ == Orientation::Horizontal) + ForeachChild([&rest_available_size_for_children, &stretch_control_list](Control* const control) { - control->Measure(rest_available_size_for_children); - if (orientation_ == Orientation::Horizontal) + const auto mode = control->GetLayoutParams()->width.mode; + if (mode == MeasureMode::Content || mode == MeasureMode::Exactly) { + control->Measure(rest_available_size_for_children); rest_available_size_for_children.width -= control->GetDesiredSize().width; if (rest_available_size_for_children.width < 0) rest_available_size_for_children.width = 0; } else + stretch_control_list.push_back(control); + }); + else + ForeachChild([&rest_available_size_for_children, &stretch_control_list](Control* const control) + { + const auto mode = control->GetLayoutParams()->height.mode; + if (mode == MeasureMode::Content || mode == MeasureMode::Exactly) { + control->Measure(rest_available_size_for_children); rest_available_size_for_children.height -= control->GetDesiredSize().height; if (rest_available_size_for_children.height < 0) rest_available_size_for_children.height = 0; } + else + stretch_control_list.push_back(control); }); + if (orientation_ == Orientation::Horizontal) + { + const auto available_width = rest_available_size_for_children.width / stretch_control_list.size(); + for (const auto control : stretch_control_list) + { + control->Measure(Size(available_width, rest_available_size_for_children.height)); + rest_available_size_for_children.width -= control->GetDesiredSize().width; + if (rest_available_size_for_children.width < 0) + rest_available_size_for_children.width = 0; + } + } + else + { + const auto available_height = rest_available_size_for_children.height / stretch_control_list.size(); + for (const auto control : stretch_control_list) + { + control->Measure(Size(rest_available_size_for_children.width, available_height)); + rest_available_size_for_children.height -= control->GetDesiredSize().height; + if (rest_available_size_for_children.height < 0) + rest_available_size_for_children.height = 0; + } + } + auto actual_size_for_children = total_available_size_for_children; if (orientation_ == Orientation::Horizontal) actual_size_for_children.width -= rest_available_size_for_children.width; @@ -85,16 +123,20 @@ namespace cru::ui::controls void LinearLayout::OnLayout(const Rect& rect) { - auto current_anchor = Point::zero; - ForeachChild([this, ¤t_anchor](Control* control) + float current_anchor_length = 0; + ForeachChild([this, ¤t_anchor_length, rect](Control* control) { const auto size = control->GetDesiredSize(); - control->Layout(Rect(current_anchor, size)); - if (orientation_ == Orientation::Horizontal) - current_anchor.x += size.width; + { + control->Layout(Rect(Point(current_anchor_length, (rect.height - size.height) / 2), size)); + current_anchor_length += size.width; + } else - current_anchor.y += size.height; + { + control->Layout(Rect(Point((rect.width - size.width) / 2, current_anchor_length), size)); + current_anchor_length += size.height; + } }); } } diff --git a/CruUI/ui/controls/text_block.cpp b/CruUI/ui/controls/text_block.cpp index 294c456b..8921198b 100644 --- a/CruUI/ui/controls/text_block.cpp +++ b/CruUI/ui/controls/text_block.cpp @@ -76,6 +76,7 @@ namespace cru void TextBlock::OnDraw(ID2D1DeviceContext* device_context) { + Control::OnDraw(device_context); if (text_layout_ != nullptr) { if (selected_range_.has_value()) @@ -179,7 +180,7 @@ namespace cru Size TextBlock::OnMeasure(const Size& available_size) { if (text_.empty()) - return Size::zero; + return Size::Zero(); const auto layout_params = GetLayoutParams(); @@ -252,6 +253,9 @@ namespace cru 24.0, L"zh-cn", &text_format_ )); + + ThrowIfFailed(text_format_->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER)); + ThrowIfFailed(text_format_->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER)); } void TextBlock::RecreateTextLayout() diff --git a/CruUI/ui/controls/toggle_button.cpp b/CruUI/ui/controls/toggle_button.cpp index 001f46eb..94685b6e 100644 --- a/CruUI/ui/controls/toggle_button.cpp +++ b/CruUI/ui/controls/toggle_button.cpp @@ -85,6 +85,7 @@ namespace cru::ui::controls void ToggleButton::OnDraw(ID2D1DeviceContext* device_context) { + Control::OnDraw(device_context); const auto size = GetSize(); graph::WithTransform(device_context, D2D1::Matrix3x2F::Translation(size.width / 2, size.height / 2), [this](ID2D1DeviceContext* device_context) { |