diff options
author | crupest <crupest@outlook.com> | 2022-02-09 23:53:18 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-02-09 23:53:18 +0800 |
commit | b8863c403a44c1c7ac35f1a1da92bbf3c8858552 (patch) | |
tree | 7e38f029f0657e6c1210a53f1cba331cdb8feab6 /src | |
parent | d18b5453d7ffd19667ee8ac125b34ab5328f0dc3 (diff) | |
download | cru-b8863c403a44c1c7ac35f1a1da92bbf3c8858552.tar.gz cru-b8863c403a44c1c7ac35f1a1da92bbf3c8858552.tar.bz2 cru-b8863c403a44c1c7ac35f1a1da92bbf3c8858552.zip |
...
Diffstat (limited to 'src')
-rw-r--r-- | src/ui/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/ui/components/Menu.cpp | 2 | ||||
-rw-r--r-- | src/ui/controls/Button.cpp | 12 | ||||
-rw-r--r-- | src/ui/controls/Container.cpp | 11 | ||||
-rw-r--r-- | src/ui/controls/ContentControl.cpp | 31 | ||||
-rw-r--r-- | src/ui/controls/Control.cpp | 93 | ||||
-rw-r--r-- | src/ui/controls/FlexLayout.cpp | 53 | ||||
-rw-r--r-- | src/ui/controls/LayoutControl.cpp | 24 | ||||
-rw-r--r-- | src/ui/controls/NoChildControl.cpp | 7 | ||||
-rw-r--r-- | src/ui/controls/RootControl.cpp | 11 | ||||
-rw-r--r-- | src/ui/controls/ScrollView.cpp | 16 | ||||
-rw-r--r-- | src/ui/controls/StackLayout.cpp | 22 | ||||
-rw-r--r-- | src/ui/controls/TextBox.cpp | 4 | ||||
-rw-r--r-- | src/ui/controls/Window.cpp | 7 | ||||
-rw-r--r-- | src/ui/host/WindowHost.cpp | 5 | ||||
-rw-r--r-- | src/ui/render/FlexLayoutRenderObject.cpp | 4 | ||||
-rw-r--r-- | src/ui/render/StackLayoutRenderObject.cpp | 4 |
17 files changed, 39 insertions, 269 deletions
diff --git a/src/ui/CMakeLists.txt b/src/ui/CMakeLists.txt index d358940f..98d21c9e 100644 --- a/src/ui/CMakeLists.txt +++ b/src/ui/CMakeLists.txt @@ -5,10 +5,8 @@ add_library(cru_ui SHARED components/Menu.cpp controls/Button.cpp controls/Container.cpp - controls/ContentControl.cpp controls/Control.cpp controls/FlexLayout.cpp - controls/LayoutControl.cpp controls/NoChildControl.cpp controls/Popup.cpp controls/RootControl.cpp diff --git a/src/ui/components/Menu.cpp b/src/ui/components/Menu.cpp index c6db8e4a..3134bbb6 100644 --- a/src/ui/components/Menu.cpp +++ b/src/ui/components/Menu.cpp @@ -33,7 +33,7 @@ void MenuItem::SetText(String text) { text_->SetText(std::move(text)); } Menu::Menu() { container_ = controls::FlexLayout::Create(); - container_->SetFlexDirection(FlexDirection::Vertical); + container_->SetFlexDirection(controls::FlexDirection::Vertical); } Menu::~Menu() { diff --git a/src/ui/controls/Button.cpp b/src/ui/controls/Button.cpp index e42bfae7..d7d157c5 100644 --- a/src/ui/controls/Button.cpp +++ b/src/ui/controls/Button.cpp @@ -10,11 +10,7 @@ namespace cru::ui::controls { Button::Button() : click_detector_(this) { - render_object_ = std::make_unique<render::BorderRenderObject>(); - render_object_->SetAttachedControl(this); - SetContainerRenderObject(render_object_.get()); - render_object_->SetBorderEnabled(true); - + GetContainerRenderObject()->SetBorderEnabled(true); auto default_button_style = ThemeManager::GetInstance()->GetResourceStyleRuleSet(u"button.style"); GetStyleRuleSet()->SetParent(std::move(default_button_style)); @@ -22,11 +18,7 @@ Button::Button() : click_detector_(this) { Button::~Button() = default; -render::RenderObject* Button::GetRenderObject() const { - return render_object_.get(); -} - void Button::ApplyBorderStyle(const style::ApplyBorderStyleInfo& style) { - render_object_->ApplyBorderStyle(style); + GetContainerRenderObject()->ApplyBorderStyle(style); } } // namespace cru::ui::controls diff --git a/src/ui/controls/Container.cpp b/src/ui/controls/Container.cpp index 92ac3847..7b0c10a9 100644 --- a/src/ui/controls/Container.cpp +++ b/src/ui/controls/Container.cpp @@ -5,16 +5,7 @@ #include "cru/ui/render/RenderObject.h" namespace cru::ui::controls { -Container::Container() { - render_object_ = std::make_unique<render::BorderRenderObject>(); - render_object_->SetBorderEnabled(false); - render_object_->SetAttachedControl(this); - SetContainerRenderObject(render_object_.get()); -} +Container::Container() {} Container::~Container() = default; - -render::RenderObject* Container::GetRenderObject() const { - return render_object_.get(); -} } // namespace cru::ui::controls diff --git a/src/ui/controls/ContentControl.cpp b/src/ui/controls/ContentControl.cpp deleted file mode 100644 index 8b421e09..00000000 --- a/src/ui/controls/ContentControl.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include "cru/ui/controls/ContentControl.h" - -namespace cru::ui::controls { -Control* ContentControl::GetChild() const { - if (GetChildren().empty()) return nullptr; - return GetChildren()[0]; -} - -void ContentControl::SetChild(Control* child) { - Control* old_child = nullptr; - if (!GetChildren().empty()) { - old_child = GetChildren()[0]; - this->RemoveChild(0); - } - if (child) { - this->AddChild(child, 0); - } - OnChildChanged(old_child, child); -} - -void ContentControl::OnChildChanged(Control* old_child, Control* new_child) { - if (container_render_object_) { - if (old_child) { - container_render_object_->RemoveChild(0); - } - if (new_child) { - container_render_object_->AddChild(new_child->GetRenderObject(), 0); - } - } -} -} // namespace cru::ui::controls diff --git a/src/ui/controls/Control.cpp b/src/ui/controls/Control.cpp index 36d5cd60..df71f660 100644 --- a/src/ui/controls/Control.cpp +++ b/src/ui/controls/Control.cpp @@ -31,31 +31,21 @@ Control::Control() { }); } -Control::~Control() { - if (auto_delete_children_) { - for (const auto child : children_) { - delete child; - } - } -} - -host::WindowHost* Control::GetWindowHost() const { return window_host_; } +Control::~Control() {} -Index Control::IndexOf(Control* child) const { - for (Index i = 0; i < children_.size(); ++i) { - if (children_[i] == child) return i; +host::WindowHost* Control::GetWindowHost() const { + auto parent = GetParent(); + if (parent) { + return parent->GetWindowHost(); } - return -1; + return nullptr; } -void Control::TraverseDescendants( - const std::function<void(Control*)>& predicate) { - predicate(this); - for (auto c : GetChildren()) c->TraverseDescendants(predicate); -} - -void Control::RemoveFromParent() { - if (parent_) parent_->RemoveChild(parent_->IndexOf(this)); +void Control::SetParent(Control* parent) { + if (parent_ == parent) return; + auto old_parent = parent_; + parent_ = parent; + OnParentChanged(old_parent, parent); } bool Control::HasFocus() { @@ -117,65 +107,4 @@ void Control::SetCursor(std::shared_ptr<ICursor> cursor) { std::shared_ptr<style::StyleRuleSet> Control::GetStyleRuleSet() { return style_rule_set_; } - -void Control::AddChild(Control* control, const Index position) { - Expects(control->GetParent() == - nullptr); // The control already has a parent. - Expects(position >= 0); - Expects(position <= static_cast<Index>( - children_.size())); // The position is out of range. - - children_.insert(children_.cbegin() + position, control); - - const auto old_parent = control->parent_; - control->parent_ = this; - - OnAddChild(control, position); - control->OnParentChanged(old_parent, this); - - if (window_host_) - control->TraverseDescendants([this](Control* control) { - control->window_host_ = window_host_; - control->OnAttachToHost(window_host_); - }); -} - -void Control::RemoveChild(const Index position) { - Expects(position >= 0); - Expects(position < static_cast<Index>( - children_.size())); // The position is out of range. - - const auto i = children_.cbegin() + position; - const auto control = *i; - - children_.erase(i); - control->parent_ = nullptr; - - OnRemoveChild(control, position); - control->OnParentChanged(this, nullptr); - - if (window_host_) - control->TraverseDescendants([this](Control* control) { - control->window_host_ = nullptr; - control->OnDetachFromHost(window_host_); - }); -} - -void Control::OnAddChild(Control* child, Index position) { - CRU_UNUSED(child) - CRU_UNUSED(position) -} -void Control::OnRemoveChild(Control* child, Index position) { - CRU_UNUSED(child) - CRU_UNUSED(position) -} - -void Control::OnParentChanged(Control* old_parent, Control* new_parent) { - CRU_UNUSED(old_parent) - CRU_UNUSED(new_parent) -} - -void Control::OnAttachToHost(host::WindowHost* host) { CRU_UNUSED(host) } - -void Control::OnDetachFromHost(host::WindowHost* host) { CRU_UNUSED(host) } } // namespace cru::ui::controls diff --git a/src/ui/controls/FlexLayout.cpp b/src/ui/controls/FlexLayout.cpp index ecf4d428..8d71cfdb 100644 --- a/src/ui/controls/FlexLayout.cpp +++ b/src/ui/controls/FlexLayout.cpp @@ -1,70 +1,31 @@ #include "cru/ui/controls/FlexLayout.h" namespace cru::ui::controls { -using render::FlexLayoutRenderObject; - -FlexLayout::FlexLayout() { - render_object_.reset(new FlexLayoutRenderObject()); - render_object_->SetAttachedControl(this); - SetContainerRenderObject(render_object_.get()); -} +FlexLayout::FlexLayout() = default; FlexLayout::~FlexLayout() = default; -render::RenderObject* FlexLayout::GetRenderObject() const { - return render_object_.get(); -} - -namespace { -Index FindPosition(render::RenderObject* parent, render::RenderObject* child) { - const auto& render_objects = parent->GetChildren(); - const auto find_result = - std::find(render_objects.cbegin(), render_objects.cend(), child); - if (find_result == render_objects.cend()) { - throw std::logic_error("Control is not a child of FlexLayout."); - } - return static_cast<Index>(find_result - render_objects.cbegin()); -} -} // namespace - -FlexChildLayoutData FlexLayout::GetChildLayoutData(Control* control) { - Expects(control); - return render_object_->GetChildLayoutData( - FindPosition(render_object_.get(), control->GetRenderObject())); -} - -void FlexLayout::SetChildLayoutData(Control* control, - FlexChildLayoutData data) { - Expects(control); - render_object_->SetChildLayoutData( - FindPosition(render_object_.get(), control->GetRenderObject()), - std::move(data)); -} - FlexMainAlignment FlexLayout::GetContentMainAlign() const { - return render_object_->GetContentMainAlign(); + return GetContainerRenderObject()->GetContentMainAlign(); } void FlexLayout::SetContentMainAlign(FlexMainAlignment value) { - if (value == GetContentMainAlign()) return; - render_object_->SetContentMainAlign(value); + GetContainerRenderObject()->SetContentMainAlign(value); } FlexDirection FlexLayout::GetFlexDirection() const { - return render_object_->GetFlexDirection(); + return GetContainerRenderObject()->GetFlexDirection(); } void FlexLayout::SetFlexDirection(FlexDirection direction) { - if (direction == GetFlexDirection()) return; - render_object_->SetFlexDirection(direction); + GetContainerRenderObject()->SetFlexDirection(direction); } FlexCrossAlignment FlexLayout::GetItemCrossAlign() const { - return render_object_->GetItemCrossAlign(); + return GetContainerRenderObject()->GetItemCrossAlign(); } void FlexLayout::SetItemCrossAlign(FlexCrossAlignment alignment) { - if (alignment == GetItemCrossAlign()) return; - render_object_->SetItemCrossAlign(alignment); + GetContainerRenderObject()->SetItemCrossAlign(alignment); } } // namespace cru::ui::controls diff --git a/src/ui/controls/LayoutControl.cpp b/src/ui/controls/LayoutControl.cpp deleted file mode 100644 index 5c67bc86..00000000 --- a/src/ui/controls/LayoutControl.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "cru/ui/controls/LayoutControl.h" - -#include "cru/ui/render/RenderObject.h" - -namespace cru::ui::controls { -void LayoutControl::ClearChildren() { - while (GetChildren().size() > 0) { - RemoveChild(0); - } -} - -void LayoutControl::OnAddChild(Control* child, Index position) { - if (container_render_object_ != nullptr) { - container_render_object_->AddChild(child->GetRenderObject(), position); - } -} - -void LayoutControl::OnRemoveChild(Control* child, Index position) { - CRU_UNUSED(child) - if (container_render_object_ != nullptr) { - container_render_object_->RemoveChild(position); - } -} -} // namespace cru::ui::controls diff --git a/src/ui/controls/NoChildControl.cpp b/src/ui/controls/NoChildControl.cpp index 29177828..4a9002ed 100644 --- a/src/ui/controls/NoChildControl.cpp +++ b/src/ui/controls/NoChildControl.cpp @@ -1,3 +1,8 @@ #include "cru/ui/controls/NoChildControl.h" -namespace cru::ui::controls {} +namespace cru::ui::controls { +void NoChildControl::ForEachChild( + const std::function<void(Control*)>& callback) { + CRU_UNUSED(callback); +} +} // namespace cru::ui::controls diff --git a/src/ui/controls/RootControl.cpp b/src/ui/controls/RootControl.cpp index 828822c6..07647024 100644 --- a/src/ui/controls/RootControl.cpp +++ b/src/ui/controls/RootControl.cpp @@ -12,21 +12,14 @@ namespace cru::ui::controls { 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()); + GetContainerRenderObject()->SetDefaultHorizontalAlignment(Alignment::Stretch); + GetContainerRenderObject()->SetDefaultVertialAlignment(Alignment::Stretch); window_host_ = std::make_unique<host::WindowHost>(this); window_host_->SetLayoutPreferToFillWindow(true); } RootControl::~RootControl() {} -render::RenderObject* RootControl::GetRenderObject() const { - return render_object_.get(); -} - platform::gui::INativeWindow* RootControl::GetNativeWindow() { return window_host_->GetNativeWindow(); } diff --git a/src/ui/controls/ScrollView.cpp b/src/ui/controls/ScrollView.cpp index e87ede4b..f3b3750f 100644 --- a/src/ui/controls/ScrollView.cpp +++ b/src/ui/controls/ScrollView.cpp @@ -1,19 +1,7 @@ #include "cru/ui/controls/ScrollView.h" -#include "cru/ui/render/RenderObject.h" -#include "cru/ui/render/ScrollRenderObject.h" - -#include <memory> - namespace cru::ui::controls { -ScrollView::ScrollView() { - scroll_render_object_ = std::make_unique<render::ScrollRenderObject>(); - scroll_render_object_->SetAttachedControl(this); - - SetContainerRenderObject(scroll_render_object_.get()); -} +ScrollView::ScrollView() {} -render::RenderObject* ScrollView::GetRenderObject() const { - return scroll_render_object_.get(); -} +ScrollView::~ScrollView() {} } // namespace cru::ui::controls diff --git a/src/ui/controls/StackLayout.cpp b/src/ui/controls/StackLayout.cpp index 7e45a555..55964bcd 100644 --- a/src/ui/controls/StackLayout.cpp +++ b/src/ui/controls/StackLayout.cpp @@ -1,29 +1,9 @@ #include "cru/ui/controls/StackLayout.h" -#include <memory> - -#include "cru/ui/render/StackLayoutRenderObject.h" namespace cru::ui::controls { using render::StackLayoutRenderObject; -StackLayout::StackLayout() { - render_object_ = std::make_unique<StackLayoutRenderObject>(); - render_object_->SetAttachedControl(this); - SetContainerRenderObject(render_object_.get()); -} +StackLayout::StackLayout() = default; 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/controls/TextBox.cpp b/src/ui/controls/TextBox.cpp index 11b77e99..66b6bd43 100644 --- a/src/ui/controls/TextBox.cpp +++ b/src/ui/controls/TextBox.cpp @@ -24,8 +24,8 @@ TextBox::TextBox() theme_manager->GetResourceBrush(u"text.caret.brush")); text_render_object_->SetEditMode(true); - border_render_object_->AddChild(scroll_render_object_.get(), 0); - scroll_render_object_->AddChild(text_render_object_.get(), 0); + border_render_object_->SetChild(scroll_render_object_.get()); + scroll_render_object_->SetChild(text_render_object_.get()); border_render_object_->SetAttachedControl(this); scroll_render_object_->SetAttachedControl(this); diff --git a/src/ui/controls/Window.cpp b/src/ui/controls/Window.cpp index 998395f3..6891c918 100644 --- a/src/ui/controls/Window.cpp +++ b/src/ui/controls/Window.cpp @@ -1,12 +1,5 @@ #include "cru/ui/controls/Window.h" -#include "cru/common/Base.h" -#include "cru/platform/gui/Base.h" -#include "cru/ui/controls/RootControl.h" -#include "cru/ui/host/WindowHost.h" -#include "cru/ui/render/Base.h" -#include "cru/ui/render/StackLayoutRenderObject.h" - namespace cru::ui::controls { Window* Window::Create(Control* attached_control) { return new Window(attached_control); diff --git a/src/ui/host/WindowHost.cpp b/src/ui/host/WindowHost.cpp index 9773c117..6849d000 100644 --- a/src/ui/host/WindowHost.cpp +++ b/src/ui/host/WindowHost.cpp @@ -105,11 +105,6 @@ inline void BindNativeEvent( WindowHost::WindowHost(controls::Control* root_control) : root_control_(root_control), focus_control_(root_control) { - root_control_->TraverseDescendants([this](controls::Control* control) { - control->window_host_ = this; - control->OnAttachToHost(this); - }); - root_render_object_ = root_control->GetRenderObject(); this->layout_paint_cycler_ = std::make_unique<LayoutPaintCycler>(this); diff --git a/src/ui/render/FlexLayoutRenderObject.cpp b/src/ui/render/FlexLayoutRenderObject.cpp index ebe60395..6475d005 100644 --- a/src/ui/render/FlexLayoutRenderObject.cpp +++ b/src/ui/render/FlexLayoutRenderObject.cpp @@ -333,7 +333,7 @@ Size FlexLayoutRenderObject::OnMeasureContent( std::vector<FlexChildLayoutData> layout_data_list; for (int i = 0; i < GetChildCount(); i++) { children.push_back(GetChildAt(i)); - layout_data_list.push_back(GetChildLayoutData(i)); + layout_data_list.push_back(GetChildLayoutDataAt(i)); } if (horizontal) { @@ -353,7 +353,7 @@ void FlexLayoutRenderObject::OnLayoutContent(const Rect& content_rect) { std::vector<FlexChildLayoutData> layout_data_list; for (int i = 0; i < child_count; i++) { children.push_back(GetChildAt(i)); - layout_data_list.push_back(GetChildLayoutData(i)); + layout_data_list.push_back(GetChildLayoutDataAt(i)); } if (direction_ == FlexDirection::Horizontal) { diff --git a/src/ui/render/StackLayoutRenderObject.cpp b/src/ui/render/StackLayoutRenderObject.cpp index 6c79716f..fda9a420 100644 --- a/src/ui/render/StackLayoutRenderObject.cpp +++ b/src/ui/render/StackLayoutRenderObject.cpp @@ -35,7 +35,7 @@ Size StackLayoutRenderObject::OnMeasureContent( child_max_size = Max(requirement.min.GetSizeOr0(), child_max_size); for (Index i = 0; i < GetChildCount(); ++i) { - auto child_layout_data = GetChildLayoutData(i); + auto child_layout_data = GetChildLayoutDataAt(i); auto horizontal_stretch = child_layout_data.horizontal.value_or(default_horizontal_alignment_) == Alignment::Stretch; @@ -67,7 +67,7 @@ void StackLayoutRenderObject::OnLayoutContent(const Rect& content_rect) { for (int i = 0; i < count; i++) { const auto child = GetChildAt(i); - const auto& layout_data = GetChildLayoutData(i); + const auto& layout_data = GetChildLayoutDataAt(i); const auto& size = child->GetDesiredSize(); child->Layout(Point{ CalculateAnchorByAlignment( |