diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/theme_builder/components/MainWindow.cpp | 16 | ||||
-rw-r--r-- | src/theme_builder/components/MainWindow.hpp | 6 | ||||
-rw-r--r-- | src/ui/controls/RootControl.cpp | 3 | ||||
-rw-r--r-- | src/ui/controls/StackLayout.cpp | 9 | ||||
-rw-r--r-- | src/ui/render/StackLayoutRenderObject.cpp | 46 |
5 files changed, 76 insertions, 4 deletions
diff --git a/src/theme_builder/components/MainWindow.cpp b/src/theme_builder/components/MainWindow.cpp index 3d521d44..d52193f1 100644 --- a/src/theme_builder/components/MainWindow.cpp +++ b/src/theme_builder/components/MainWindow.cpp @@ -1,4 +1,7 @@ #include "MainWindow.hpp" +#include "cru/ui/Base.hpp" +#include "cru/ui/controls/StackLayout.hpp" +#include "cru/ui/controls/TextBlock.hpp" namespace cru::theme_builder { using namespace cru::ui; @@ -8,6 +11,19 @@ using namespace cru::platform::gui; MainWindow::MainWindow() { window_ = Window::Create(); window_->GetNativeWindow()->SetTitle(u"CruUI Theme Builder"); + + main_layout_ = FlexLayout::Create(); + main_layout_->SetFlexDirection(FlexDirection::Horizontal); + window_->AddChild(main_layout_, 0); + + preview_layout_ = StackLayout::Create(); + main_layout_->AddChild(preview_layout_, 0); + + preview_button_ = Button::Create(); + preview_button_->SetChild(TextBlock::Create(u"Preview")); + preview_layout_->AddChild(preview_button_, 0); + preview_layout_->SetChildLayoutData( + 0, StackChildLayoutData{Alignment::Center, Alignment::Center}); } MainWindow::~MainWindow() { delete window_; } diff --git a/src/theme_builder/components/MainWindow.hpp b/src/theme_builder/components/MainWindow.hpp index fd7f7996..ef37b7a2 100644 --- a/src/theme_builder/components/MainWindow.hpp +++ b/src/theme_builder/components/MainWindow.hpp @@ -1,5 +1,8 @@ #pragma once #include "cru/ui/components/Component.hpp" +#include "cru/ui/controls/Button.hpp" +#include "cru/ui/controls/FlexLayout.hpp" +#include "cru/ui/controls/StackLayout.hpp" #include "cru/ui/controls/Window.hpp" namespace cru::theme_builder { @@ -18,5 +21,8 @@ class MainWindow : public ui::components::Component { private: ui::controls::Window* window_; + ui::controls::FlexLayout* main_layout_; + ui::controls::StackLayout* preview_layout_; + ui::controls::Button* preview_button_; }; } // namespace cru::theme_builder diff --git a/src/ui/controls/RootControl.cpp b/src/ui/controls/RootControl.cpp index 7edf4a1d..a7366155 100644 --- a/src/ui/controls/RootControl.cpp +++ b/src/ui/controls/RootControl.cpp @@ -3,6 +3,7 @@ #include "cru/common/Base.hpp" #include "cru/platform/gui/Base.hpp" #include "cru/platform/gui/Window.hpp" +#include "cru/ui/Base.hpp" #include "cru/ui/host/WindowHost.hpp" #include "cru/ui/render/Base.hpp" #include "cru/ui/render/StackLayoutRenderObject.hpp" @@ -15,6 +16,8 @@ RootControl::RootControl(Control* attached_control) : attached_control_(attached_control) { render_object_ = std::make_unique<render::StackLayoutRenderObject>(); render_object_->SetAttachedControl(this); + render_object_->SetDefaultHorizontalAlignment(Alignment::Stretch); + render_object_->SetDefaultVertialAlignment(Alignment::Stretch); SetContainerRenderObject(render_object_.get()); window_host_ = std::make_unique<host::WindowHost>(this); } diff --git a/src/ui/controls/StackLayout.cpp b/src/ui/controls/StackLayout.cpp index 89968571..667e6755 100644 --- a/src/ui/controls/StackLayout.cpp +++ b/src/ui/controls/StackLayout.cpp @@ -17,4 +17,13 @@ StackLayout::~StackLayout() = default; render::RenderObject* StackLayout::GetRenderObject() const { return render_object_.get(); } + +const StackChildLayoutData& StackLayout::GetChildLayoutData(Index position) { + return render_object_->GetChildLayoutData(position); +} + +void StackLayout::SetChildLayoutData(Index position, + StackChildLayoutData data) { + render_object_->SetChildLayoutData(position, std::move(data)); +} } // namespace cru::ui::controls diff --git a/src/ui/render/StackLayoutRenderObject.cpp b/src/ui/render/StackLayoutRenderObject.cpp index 753612af..1b6cc9fc 100644 --- a/src/ui/render/StackLayoutRenderObject.cpp +++ b/src/ui/render/StackLayoutRenderObject.cpp @@ -7,6 +7,17 @@ #include <algorithm> namespace cru::ui::render { +void StackLayoutRenderObject::SetDefaultHorizontalAlignment( + Alignment alignment) { + default_horizontal_alignment_ = alignment; + InvalidateLayout(); +} + +void StackLayoutRenderObject::SetDefaultVertialAlignment(Alignment alignment) { + default_vertical_alignment_ = alignment; + InvalidateLayout(); +} + Size StackLayoutRenderObject::OnMeasureContent( const MeasureRequirement& requirement, const MeasureSize& preferred_size) { Size child_max_size; @@ -22,6 +33,31 @@ Size StackLayoutRenderObject::OnMeasureContent( child_max_size = Max(preferred_size.GetSizeOr0(), child_max_size); child_max_size = Max(requirement.min.GetSizeOr0(), child_max_size); + for (Index i = 0; i < GetChildren().size(); ++i) { + auto child_layout_data = GetChildLayoutData(i); + auto horizontal_stretch = + child_layout_data.horizontal.value_or(default_horizontal_alignment_) == + Alignment::Stretch; + auto vertical_stretch = + child_layout_data.vertical.value_or(default_vertical_alignment_) == + Alignment::Stretch; + if (horizontal_stretch || vertical_stretch) { + auto child = GetChildren()[i]; + auto child_size = child->GetSize(); + MeasureRequirement child_requirement(child_size, child_size); + if (horizontal_stretch) { + child_requirement.min.width = child_requirement.max.width = + child_max_size.width; + } + + if (vertical_stretch) { + child_requirement.min.height = child_requirement.max.height = + child_max_size.height; + } + child->Measure(child_requirement, MeasureSize::NotSpecified()); + } + } + return child_max_size; } @@ -34,10 +70,12 @@ void StackLayoutRenderObject::OnLayoutContent(const Rect& content_rect) { const auto& layout_data = GetChildLayoutData(i); const auto& size = child->GetSize(); child->Layout(Point{ - CalculateAnchorByAlignment(layout_data.horizontal, content_rect.left, - content_rect.width, size.width), - CalculateAnchorByAlignment(layout_data.vertical, content_rect.top, - content_rect.height, size.height)}); + CalculateAnchorByAlignment( + layout_data.horizontal.value_or(default_horizontal_alignment_), + content_rect.left, content_rect.width, size.width), + CalculateAnchorByAlignment( + layout_data.vertical.value_or(default_vertical_alignment_), + content_rect.top, content_rect.height, size.height)}); } } } // namespace cru::ui::render |