diff options
author | crupest <crupest@outlook.com> | 2020-04-02 21:09:06 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2020-04-02 21:09:06 +0800 |
commit | 7fd9ca808c908c67d9bec2183034ad16ea86a912 (patch) | |
tree | dd4287e511404aa2da07ea078b4bc2c76d4874fc | |
parent | 381d0831f72dc52831bbfb66d9e7db10a86760e5 (diff) | |
download | cru-7fd9ca808c908c67d9bec2183034ad16ea86a912.tar.gz cru-7fd9ca808c908c67d9bec2183034ad16ea86a912.tar.bz2 cru-7fd9ca808c908c67d9bec2183034ad16ea86a912.zip |
...
-rw-r--r-- | .vscode/settings.json | 3 | ||||
-rw-r--r-- | include/cru/common/base.hpp | 2 | ||||
-rw-r--r-- | include/cru/ui/controls/flex_layout.hpp | 4 | ||||
-rw-r--r-- | include/cru/ui/controls/stack_layout.hpp | 4 | ||||
-rw-r--r-- | include/cru/ui/layout_control.hpp | 8 | ||||
-rw-r--r-- | include/cru/ui/render/layout_render_object.hpp | 12 | ||||
-rw-r--r-- | include/cru/ui/render/render_object.hpp | 10 | ||||
-rw-r--r-- | src/ui/controls/flex_layout.cpp | 4 | ||||
-rw-r--r-- | src/ui/controls/stack_layout.cpp | 4 | ||||
-rw-r--r-- | src/ui/layout_control.cpp | 27 | ||||
-rw-r--r-- | src/ui/render/render_object.cpp | 12 |
11 files changed, 47 insertions, 43 deletions
diff --git a/.vscode/settings.json b/.vscode/settings.json index c7947909..1de11236 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -65,6 +65,7 @@ "xlocmon": "cpp", "xloctime": "cpp", "string_view": "cpp", - "cassert": "cpp" + "cassert": "cpp", + "gsl_util": "cpp" } } diff --git a/include/cru/common/base.hpp b/include/cru/common/base.hpp index daee971f..20dd811d 100644 --- a/include/cru/common/base.hpp +++ b/include/cru/common/base.hpp @@ -42,4 +42,6 @@ struct Interface { [[noreturn]] inline void UnreachableCode() { throw std::runtime_error("Unreachable code."); } + +using Index = gsl::index; } // namespace cru diff --git a/include/cru/ui/controls/flex_layout.hpp b/include/cru/ui/controls/flex_layout.hpp index 52eaec75..ab08a80b 100644 --- a/include/cru/ui/controls/flex_layout.hpp +++ b/include/cru/ui/controls/flex_layout.hpp @@ -32,8 +32,8 @@ class FlexLayout : public LayoutControl { void SetChildLayoutData(Control* control, const FlexChildLayoutData& data); protected: - void OnAddChild(Control* child, int position) override; - void OnRemoveChild(Control* child, int position) override; + void OnAddChild(Control* child, Index position) override; + void OnRemoveChild(Control* child, Index position) override; private: std::shared_ptr<render::FlexLayoutRenderObject> render_object_; diff --git a/include/cru/ui/controls/stack_layout.hpp b/include/cru/ui/controls/stack_layout.hpp index 93861c19..20da0e82 100644 --- a/include/cru/ui/controls/stack_layout.hpp +++ b/include/cru/ui/controls/stack_layout.hpp @@ -22,8 +22,8 @@ class StackLayout : public LayoutControl { render::RenderObject* GetRenderObject() const override; protected: - void OnAddChild(Control* child, int position) override; - void OnRemoveChild(Control* child, int position) override; + void OnAddChild(Control* child, Index position) override; + void OnRemoveChild(Control* child, Index position) override; private: std::shared_ptr<render::StackLayoutRenderObject> render_object_; diff --git a/include/cru/ui/layout_control.hpp b/include/cru/ui/layout_control.hpp index 187f0e0d..e1856403 100644 --- a/include/cru/ui/layout_control.hpp +++ b/include/cru/ui/layout_control.hpp @@ -17,13 +17,13 @@ class LayoutControl : public Control { return children_; } - void AddChild(Control* control, int position); + void AddChild(Control* control, Index position); - void RemoveChild(int position); + void RemoveChild(Index position); protected: - virtual void OnAddChild(Control* child, int position); - virtual void OnRemoveChild(Control* child, int position); + virtual void OnAddChild(Control* child, Index position); + virtual void OnRemoveChild(Control* child, Index position); private: std::vector<Control*> children_; diff --git a/include/cru/ui/render/layout_render_object.hpp b/include/cru/ui/render/layout_render_object.hpp index 8700b8ba..5c4c9c5c 100644 --- a/include/cru/ui/render/layout_render_object.hpp +++ b/include/cru/ui/render/layout_render_object.hpp @@ -18,9 +18,9 @@ class LayoutRenderObject : public RenderObject { ~LayoutRenderObject() override = default; - ChildLayoutData* GetChildLayoutData(int position) { + ChildLayoutData* GetChildLayoutData(Index position) { Expects(position >= 0 && - position < static_cast<int>(child_layout_data_.size())); + position < static_cast<Index>(child_layout_data_.size())); return &child_layout_data_[position]; } @@ -29,8 +29,8 @@ class LayoutRenderObject : public RenderObject { RenderObject* HitTest(const Point& point) override; protected: - void OnAddChild(RenderObject* new_child, int position) override; - void OnRemoveChild(RenderObject* removed_child, int position) override; + void OnAddChild(RenderObject* new_child, Index position) override; + void OnRemoveChild(RenderObject* removed_child, Index position) override; private: std::vector<ChildLayoutData> child_layout_data_{}; @@ -72,7 +72,7 @@ RenderObject* LayoutRenderObject<TChildLayoutData>::HitTest( template <typename TChildLayoutData> void LayoutRenderObject<TChildLayoutData>::OnAddChild(RenderObject* new_child, - int position) { + const Index position) { CRU_UNUSED(new_child) child_layout_data_.emplace(child_layout_data_.cbegin() + position); @@ -80,7 +80,7 @@ void LayoutRenderObject<TChildLayoutData>::OnAddChild(RenderObject* new_child, template <typename TChildLayoutData> void LayoutRenderObject<TChildLayoutData>::OnRemoveChild( - RenderObject* removed_child, int position) { + RenderObject* removed_child, const Index position) { CRU_UNUSED(removed_child) child_layout_data_.erase(child_layout_data_.cbegin() + position); diff --git a/include/cru/ui/render/render_object.hpp b/include/cru/ui/render/render_object.hpp index ca31386a..33ef3d1a 100644 --- a/include/cru/ui/render/render_object.hpp +++ b/include/cru/ui/render/render_object.hpp @@ -53,9 +53,9 @@ class RenderObject : public Object { RenderObject* GetParent() const { return parent_; } const std::vector<RenderObject*>& GetChildren() const { return children_; } - int GetChildCount() const { return static_cast<int>(children_.size()); } - void AddChild(RenderObject* render_object, int position); - void RemoveChild(int position); + Index GetChildCount() const { return static_cast<Index>(children_.size()); } + void AddChild(RenderObject* render_object, Index position); + void RemoveChild(Index position); Point GetOffset() const { return offset_; } void SetOffset(const Point& offset) { offset_ = offset; } @@ -99,9 +99,9 @@ class RenderObject : public Object { RenderObject* new_parent); // default is to invalidate both layout and paint - virtual void OnAddChild(RenderObject* new_child, int position); + virtual void OnAddChild(RenderObject* new_child, Index position); // default is to invalidate both layout and paint - virtual void OnRemoveChild(RenderObject* removed_child, int position); + virtual void OnRemoveChild(RenderObject* removed_child, Index position); virtual void OnMeasureCore(const Size& available_size); virtual void OnLayoutCore(const Rect& rect); diff --git a/src/ui/controls/flex_layout.cpp b/src/ui/controls/flex_layout.cpp index c881b6f1..5412164a 100644 --- a/src/ui/controls/flex_layout.cpp +++ b/src/ui/controls/flex_layout.cpp @@ -59,11 +59,11 @@ void FlexLayout::SetFlexDirection(FlexDirection direction) { render_object_->SetFlexDirection(direction); } -void FlexLayout::OnAddChild(Control* child, int position) { +void FlexLayout::OnAddChild(Control* child, const Index position) { render_object_->AddChild(child->GetRenderObject(), position); } -void FlexLayout::OnRemoveChild(Control* child, int position) { +void FlexLayout::OnRemoveChild(Control* child, const Index position) { CRU_UNUSED(child) render_object_->RemoveChild(position); diff --git a/src/ui/controls/stack_layout.cpp b/src/ui/controls/stack_layout.cpp index b9abb510..47511f33 100644 --- a/src/ui/controls/stack_layout.cpp +++ b/src/ui/controls/stack_layout.cpp @@ -15,11 +15,11 @@ render::RenderObject* StackLayout::GetRenderObject() const { return render_object_.get(); } -void StackLayout::OnAddChild(Control* child, int position) { +void StackLayout::OnAddChild(Control* child, const Index position) { render_object_->AddChild(child->GetRenderObject(), position); } -void StackLayout::OnRemoveChild(Control* child, int position) { +void StackLayout::OnRemoveChild(Control* child, const Index position) { CRU_UNUSED(child) render_object_->RemoveChild(position); diff --git a/src/ui/layout_control.cpp b/src/ui/layout_control.cpp index cb71da30..90a825ff 100644 --- a/src/ui/layout_control.cpp +++ b/src/ui/layout_control.cpp @@ -7,13 +7,14 @@ LayoutControl::~LayoutControl() { for (const auto child : children_) delete child; } -void LayoutControl::AddChild(Control* control, const int position) { - Expects(control->GetParent() == nullptr); // The control already has a parent. +void LayoutControl::AddChild(Control* control, const Index position) { + Expects(control->GetParent() == + nullptr); // The control already has a parent. Expects(!dynamic_cast<Window*>(control)); // Can't add a window as child. - Expects(position >= 0 || - position <= - static_cast<int>( - this->children_.size())); // The position is out of range. + Expects(position >= 0); + Expects(position <= + static_cast<Index>( + this->children_.size())); // The position is out of range. children_.insert(this->children_.cbegin() + position, control); @@ -23,11 +24,11 @@ void LayoutControl::AddChild(Control* control, const int position) { OnAddChild(control, position); } -void LayoutControl::RemoveChild(const int position) { - Expects(position >= 0 && - position < - static_cast<int>( - this->children_.size())); // The position is out of range. +void LayoutControl::RemoveChild(const Index position) { + Expects(position >= 0); + Expects(position < + static_cast<Index>( + this->children_.size())); // The position is out of range. const auto i = children_.cbegin() + position; const auto child = *i; @@ -40,12 +41,12 @@ void LayoutControl::RemoveChild(const int position) { OnRemoveChild(child, position); } -void LayoutControl::OnAddChild(Control* child, int position) { +void LayoutControl::OnAddChild(Control* child, const Index position) { CRU_UNUSED(child) CRU_UNUSED(position) } -void LayoutControl::OnRemoveChild(Control* child, int position) { +void LayoutControl::OnRemoveChild(Control* child, const Index position) { CRU_UNUSED(child) CRU_UNUSED(position) } diff --git a/src/ui/render/render_object.cpp b/src/ui/render/render_object.cpp index c1b1b19b..e329d150 100644 --- a/src/ui/render/render_object.cpp +++ b/src/ui/render/render_object.cpp @@ -5,7 +5,7 @@ #include <algorithm> namespace cru::ui::render { -void RenderObject::AddChild(RenderObject* render_object, const int position) { +void RenderObject::AddChild(RenderObject* render_object, const Index position) { Expects(child_mode_ != ChildMode::None); Expects(!(child_mode_ == ChildMode::Single && children_.size() > 0)); @@ -14,7 +14,7 @@ void RenderObject::AddChild(RenderObject* render_object, const int position) { Expects(position >= 0); // Position index is less than 0. Expects( position <= - static_cast<int>(children_.size())); // Position index is out of bound. + static_cast<Index>(children_.size())); // Position index is out of bound. children_.insert(children_.cbegin() + position, render_object); render_object->SetParent(this); @@ -22,9 +22,9 @@ void RenderObject::AddChild(RenderObject* render_object, const int position) { OnAddChild(render_object, position); } -void RenderObject::RemoveChild(const int position) { +void RenderObject::RemoveChild(const Index position) { Expects(position >= 0); // Position index is less than 0. - Expects(position < static_cast<int>( + Expects(position < static_cast<Index>( children_.size())); // Position index is out of bound. const auto i = children_.cbegin() + position; @@ -72,7 +72,7 @@ void RenderObject::OnParentChanged(RenderObject* old_parent, CRU_UNUSED(new_parent) } -void RenderObject::OnAddChild(RenderObject* new_child, int position) { +void RenderObject::OnAddChild(RenderObject* new_child, Index position) { CRU_UNUSED(new_child) CRU_UNUSED(position) @@ -80,7 +80,7 @@ void RenderObject::OnAddChild(RenderObject* new_child, int position) { InvalidatePaint(); } -void RenderObject::OnRemoveChild(RenderObject* removed_child, int position) { +void RenderObject::OnRemoveChild(RenderObject* removed_child, Index position) { CRU_UNUSED(removed_child) CRU_UNUSED(position) |