aboutsummaryrefslogtreecommitdiff
path: root/src/ui/controls
diff options
context:
space:
mode:
authorYuqian Yang <crupest@crupest.life>2025-11-21 21:43:42 +0800
committerYuqian Yang <crupest@crupest.life>2025-11-21 21:43:42 +0800
commit3b875091c445b7465b9bd044914318989a94d2ad (patch)
treea358aebb488ec1ddc86bf87b8038bacd5d7515cb /src/ui/controls
parent3cda35dbcbbe1e3854b880169c0efa0fc7a79264 (diff)
downloadcru-3b875091c445b7465b9bd044914318989a94d2ad.tar.gz
cru-3b875091c445b7465b9bd044914318989a94d2ad.tar.bz2
cru-3b875091c445b7465b9bd044914318989a94d2ad.zip
Clean codes. Remove member function const.
Diffstat (limited to 'src/ui/controls')
-rw-r--r--src/ui/controls/Button.cpp5
-rw-r--r--src/ui/controls/CheckBox.cpp9
-rw-r--r--src/ui/controls/Container.cpp8
-rw-r--r--src/ui/controls/Control.cpp19
-rw-r--r--src/ui/controls/FlexLayout.cpp11
-rw-r--r--src/ui/controls/IconButton.cpp30
-rw-r--r--src/ui/controls/ScrollView.cpp4
-rw-r--r--src/ui/controls/StackLayout.cpp7
-rw-r--r--src/ui/controls/TextBlock.cpp29
-rw-r--r--src/ui/controls/TextBox.cpp11
-rw-r--r--src/ui/controls/TreeView.cpp5
-rw-r--r--src/ui/controls/Window.cpp6
12 files changed, 68 insertions, 76 deletions
diff --git a/src/ui/controls/Button.cpp b/src/ui/controls/Button.cpp
index a44a7074..4d897f08 100644
--- a/src/ui/controls/Button.cpp
+++ b/src/ui/controls/Button.cpp
@@ -1,11 +1,12 @@
#include "cru/ui/controls/Button.h"
-
#include "cru/ui/ThemeManager.h"
#include "cru/ui/helper/ClickDetector.h"
#include "cru/ui/render/BorderRenderObject.h"
namespace cru::ui::controls {
-Button::Button() : click_detector_(this) {
+Button::Button()
+ : SingleChildControl<render::BorderRenderObject>(kControlName),
+ click_detector_(this) {
GetContainerRenderObject()->SetBorderEnabled(true);
auto default_button_style =
ThemeManager::GetInstance()->GetResourceStyleRuleSet("button.style");
diff --git a/src/ui/controls/CheckBox.cpp b/src/ui/controls/CheckBox.cpp
index 778a7b4f..a6cac1a1 100644
--- a/src/ui/controls/CheckBox.cpp
+++ b/src/ui/controls/CheckBox.cpp
@@ -5,11 +5,10 @@
namespace cru::ui::controls {
CheckBox::CheckBox()
- : container_render_object_(new render::BorderRenderObject()),
- click_detector_(this) {
- container_render_object_->SetAttachedControl(this);
+ : Control(kControlName), checked_(false), click_detector_(this) {
+ container_render_object_.SetAttachedControl(this);
- container_render_object_->SetBorderEnabled(true);
+ container_render_object_.SetBorderEnabled(true);
auto default_checkbox_style =
ThemeManager::GetInstance()->GetResourceStyleRuleSet("checkbox.style");
GetStyleRuleSet()->SetParent(std::move(default_checkbox_style));
@@ -26,6 +25,6 @@ void CheckBox::SetChecked(bool checked) {
}
void CheckBox::ApplyBorderStyle(const style::ApplyBorderStyleInfo& style) {
- container_render_object_->ApplyBorderStyle(style);
+ container_render_object_.ApplyBorderStyle(style);
}
} // namespace cru::ui::controls
diff --git a/src/ui/controls/Container.cpp b/src/ui/controls/Container.cpp
index 7b0c10a9..32efa233 100644
--- a/src/ui/controls/Container.cpp
+++ b/src/ui/controls/Container.cpp
@@ -1,11 +1,7 @@
#include "cru/ui/controls/Container.h"
-
-#include "cru/platform/graphics/Factory.h"
#include "cru/ui/render/BorderRenderObject.h"
-#include "cru/ui/render/RenderObject.h"
namespace cru::ui::controls {
-Container::Container() {}
-
-Container::~Container() = default;
+Container::Container()
+ : SingleChildControl<render::BorderRenderObject>(kControlName) {}
} // namespace cru::ui::controls
diff --git a/src/ui/controls/Control.cpp b/src/ui/controls/Control.cpp
index 70a3b1f3..41644755 100644
--- a/src/ui/controls/Control.cpp
+++ b/src/ui/controls/Control.cpp
@@ -14,7 +14,8 @@ using platform::gui::ICursor;
using platform::gui::IUiApplication;
using platform::gui::SystemCursorType;
-Control::Control() {
+Control::Control(std::string name)
+ : name_(std::move(name)), host_(nullptr), parent_(nullptr) {
style_rule_set_ = std::make_shared<style::StyleRuleSet>();
style_rule_set_bind_ =
std::make_unique<style::StyleRuleSetBind>(this, style_rule_set_);
@@ -30,9 +31,10 @@ Control::~Control() {
RemoveAllChild();
}
-std::string Control::GetDebugId() const {
- return std::format("{}({})", GetControlType(),
- static_cast<const void*>(this));
+std::string Control::GetName() { return name_; }
+
+std::string Control::GetDebugId() {
+ return std::format("{}({})", GetName(), static_cast<const void*>(this));
}
ControlHost* Control::GetControlHost() { return host_; }
@@ -50,6 +52,15 @@ bool Control::HasAncestor(Control* control) {
const std::vector<Control*>& Control::GetChildren() { return children_; }
+Index Control::IndexOfChild(Control* control) {
+ const auto& children = GetChildren();
+ auto iter = std::ranges::find(children, control);
+ if (iter == children.cend()) {
+ return -1;
+ }
+ return iter - children.begin();
+}
+
void Control::RemoveChild(Control* child) {
auto iter = std::ranges::find(children_, child);
if (iter != children_.cend()) {
diff --git a/src/ui/controls/FlexLayout.cpp b/src/ui/controls/FlexLayout.cpp
index 8d71cfdb..ffe953d7 100644
--- a/src/ui/controls/FlexLayout.cpp
+++ b/src/ui/controls/FlexLayout.cpp
@@ -1,11 +1,10 @@
#include "cru/ui/controls/FlexLayout.h"
namespace cru::ui::controls {
-FlexLayout::FlexLayout() = default;
+FlexLayout::FlexLayout()
+ : LayoutControl<render::FlexLayoutRenderObject>(kControlName) {}
-FlexLayout::~FlexLayout() = default;
-
-FlexMainAlignment FlexLayout::GetContentMainAlign() const {
+FlexMainAlignment FlexLayout::GetContentMainAlign() {
return GetContainerRenderObject()->GetContentMainAlign();
}
@@ -13,7 +12,7 @@ void FlexLayout::SetContentMainAlign(FlexMainAlignment value) {
GetContainerRenderObject()->SetContentMainAlign(value);
}
-FlexDirection FlexLayout::GetFlexDirection() const {
+FlexDirection FlexLayout::GetFlexDirection() {
return GetContainerRenderObject()->GetFlexDirection();
}
@@ -21,7 +20,7 @@ void FlexLayout::SetFlexDirection(FlexDirection direction) {
GetContainerRenderObject()->SetFlexDirection(direction);
}
-FlexCrossAlignment FlexLayout::GetItemCrossAlign() const {
+FlexCrossAlignment FlexLayout::GetItemCrossAlign() {
return GetContainerRenderObject()->GetItemCrossAlign();
}
diff --git a/src/ui/controls/IconButton.cpp b/src/ui/controls/IconButton.cpp
index 059a7784..e20e422f 100644
--- a/src/ui/controls/IconButton.cpp
+++ b/src/ui/controls/IconButton.cpp
@@ -1,20 +1,16 @@
#include "cru/ui/controls/IconButton.h"
-
-#include "../Helper.h"
#include "cru/platform/graphics/Factory.h"
#include "cru/platform/graphics/Geometry.h"
+#include "cru/platform/gui/UiApplication.h"
#include "cru/ui/ThemeManager.h"
namespace cru::ui::controls {
-IconButton::IconButton()
- : container_render_object_(new render::BorderRenderObject()),
- geometry_render_object_(new render::GeometryRenderObject()),
- click_detector_(this) {
- container_render_object_->SetChild(geometry_render_object_.get());
- container_render_object_->SetAttachedControl(this);
- geometry_render_object_->SetAttachedControl(this);
-
- container_render_object_->SetBorderEnabled(true);
+IconButton::IconButton() : Control(kControlName), click_detector_(this) {
+ container_render_object_.SetChild(&geometry_render_object_);
+ container_render_object_.SetAttachedControl(this);
+ geometry_render_object_.SetAttachedControl(this);
+
+ container_render_object_.SetBorderEnabled(true);
GetStyleRuleSet()->SetParent(
ThemeManager::GetInstance()->GetResourceStyleRuleSet(
"icon-button.style"));
@@ -29,14 +25,18 @@ IconButton::IconButton(std::string_view icon_svg_path_data_string,
IconButton::~IconButton() {}
void IconButton::SetIconFillColor(const Color& color) {
- SetIconFillBrush(GetGraphicsFactory()->CreateSolidColorBrush(color));
+ SetIconFillBrush(platform::gui::IUiApplication::GetInstance()
+ ->GetGraphicsFactory()
+ ->CreateSolidColorBrush(color));
}
void IconButton::SetIconWithSvgPathDataString(
std::string_view icon_svg_path_data_string, const Rect& view_port) {
- SetIconGeometry(platform::graphics::CreateGeometryFromSvgPathData(
- GetGraphicsFactory(), icon_svg_path_data_string),
- view_port);
+ SetIconGeometry(
+ platform::graphics::CreateGeometryFromSvgPathData(
+ platform::gui::IUiApplication::GetInstance()->GetGraphicsFactory(),
+ icon_svg_path_data_string),
+ view_port);
}
void IconButton::SetIconWithSvgPathDataStringResourceKey(
diff --git a/src/ui/controls/ScrollView.cpp b/src/ui/controls/ScrollView.cpp
index f3b3750f..e156f643 100644
--- a/src/ui/controls/ScrollView.cpp
+++ b/src/ui/controls/ScrollView.cpp
@@ -1,7 +1,7 @@
#include "cru/ui/controls/ScrollView.h"
namespace cru::ui::controls {
-ScrollView::ScrollView() {}
+ScrollView::ScrollView()
+ : SingleChildControl<render::ScrollRenderObject>(kControlName) {}
-ScrollView::~ScrollView() {}
} // namespace cru::ui::controls
diff --git a/src/ui/controls/StackLayout.cpp b/src/ui/controls/StackLayout.cpp
index 55964bcd..b5ca8f35 100644
--- a/src/ui/controls/StackLayout.cpp
+++ b/src/ui/controls/StackLayout.cpp
@@ -1,9 +1,6 @@
#include "cru/ui/controls/StackLayout.h"
namespace cru::ui::controls {
-using render::StackLayoutRenderObject;
-
-StackLayout::StackLayout() = default;
-
-StackLayout::~StackLayout() = default;
+StackLayout::StackLayout()
+ : LayoutControl<render::StackLayoutRenderObject>(kControlName) {}
} // namespace cru::ui::controls
diff --git a/src/ui/controls/TextBlock.cpp b/src/ui/controls/TextBlock.cpp
index 790c534b..a36c6b41 100644
--- a/src/ui/controls/TextBlock.cpp
+++ b/src/ui/controls/TextBlock.cpp
@@ -1,20 +1,15 @@
#include "cru/ui/controls/TextBlock.h"
-
-#include "../Helper.h"
#include "cru/platform/graphics/Factory.h"
#include "cru/platform/gui/UiApplication.h"
#include "cru/ui/ThemeManager.h"
-#include "cru/ui/render/CanvasRenderObject.h"
-#include "cru/ui/render/StackLayoutRenderObject.h"
#include "cru/ui/render/TextRenderObject.h"
-namespace cru::ui::controls {
-using render::TextRenderObject;
-
-TextBlock::TextBlock() {
- const auto theme_manager = ThemeManager::GetInstance();
+#include <memory>
- text_render_object_ = std::make_unique<TextRenderObject>(
+namespace cru::ui::controls {
+TextBlock::TextBlock() : Control(kControlName) {
+ auto theme_manager = ThemeManager::GetInstance();
+ text_render_object_ = std::make_unique<render::TextRenderObject>(
theme_manager->GetResourceBrush("text.brush"),
theme_manager->GetResourceFont("text.font"),
theme_manager->GetResourceBrush("text.selection.brush"),
@@ -23,30 +18,28 @@ TextBlock::TextBlock() {
text_render_object_->SetAttachedControl(this);
service_ = std::make_unique<TextHostControlService>(this);
-
service_->SetEnabled(false);
service_->SetEditable(false);
}
-TextBlock::~TextBlock() = default;
-
-render::RenderObject* TextBlock::GetRenderObject() const {
+render::RenderObject* TextBlock::GetRenderObject() {
return text_render_object_.get();
}
-std::string TextBlock::GetText() const { return service_->GetText(); }
+std::string TextBlock::GetText() { return service_->GetText(); }
void TextBlock::SetText(std::string text) {
service_->SetText(std::move(text));
}
-bool TextBlock::IsSelectable() const { return service_->IsEnabled(); }
+bool TextBlock::IsSelectable() { return service_->IsEnabled(); }
void TextBlock::SetSelectable(bool value) { service_->SetEnabled(value); }
void TextBlock::SetTextColor(const Color& color) {
- text_render_object_->SetBrush(
- GetUiApplication()->GetGraphicsFactory()->CreateSolidColorBrush(color));
+ text_render_object_->SetBrush(platform::gui::IUiApplication::GetInstance()
+ ->GetGraphicsFactory()
+ ->CreateSolidColorBrush(color));
}
render::TextRenderObject* TextBlock::GetTextRenderObject() {
diff --git a/src/ui/controls/TextBox.cpp b/src/ui/controls/TextBox.cpp
index 70695a01..1e35de69 100644
--- a/src/ui/controls/TextBox.cpp
+++ b/src/ui/controls/TextBox.cpp
@@ -2,9 +2,7 @@
#include "cru/ui/ThemeManager.h"
#include "cru/ui/render/BorderRenderObject.h"
-#include "cru/ui/render/CanvasRenderObject.h"
#include "cru/ui/render/ScrollRenderObject.h"
-#include "cru/ui/render/StackLayoutRenderObject.h"
#include "cru/ui/render/TextRenderObject.h"
namespace cru::ui::controls {
@@ -13,7 +11,8 @@ using render::ScrollRenderObject;
using render::TextRenderObject;
TextBox::TextBox()
- : border_render_object_(new BorderRenderObject()),
+ : Control(kControlName),
+ border_render_object_(new BorderRenderObject()),
scroll_render_object_(new ScrollRenderObject()) {
auto theme_manager = ThemeManager::GetInstance();
@@ -43,13 +42,11 @@ TextBox::TextBox()
theme_manager->GetResourceStyleRuleSet("textbox.style"));
}
-TextBox::~TextBox() {}
-
-render::RenderObject* TextBox::GetRenderObject() const {
+render::RenderObject* TextBox::GetRenderObject() {
return border_render_object_.get();
}
-bool TextBox::GetMultiLine() const { return service_->IsMultiLine(); }
+bool TextBox::GetMultiLine() { return service_->IsMultiLine(); }
void TextBox::SetMultiLine(bool value) { service_->SetMultiLine(value); }
diff --git a/src/ui/controls/TreeView.cpp b/src/ui/controls/TreeView.cpp
index 89613763..c2dbcae7 100644
--- a/src/ui/controls/TreeView.cpp
+++ b/src/ui/controls/TreeView.cpp
@@ -65,9 +65,8 @@ void TreeViewItem::TraverseDescendants(
}
TreeView::TreeView()
- : root_item_(this, nullptr, render_object_.GetRootItem()) {}
-
-TreeView::~TreeView() {}
+ : Control(kControlType),
+ root_item_(this, nullptr, render_object_.GetRootItem()) {}
void TreeView::OnChildRemoved(Control* control, Index index) {
root_item_.TraverseDescendants([control](TreeViewItem* item) {
diff --git a/src/ui/controls/Window.cpp b/src/ui/controls/Window.cpp
index a5fbf05f..9722a3c6 100644
--- a/src/ui/controls/Window.cpp
+++ b/src/ui/controls/Window.cpp
@@ -8,7 +8,9 @@
namespace cru::ui::controls {
Window::Window()
- : control_host_(new ControlHost(this)), attached_control_(nullptr) {
+ : LayoutControl<render::StackLayoutRenderObject>(kControlName),
+ control_host_(new ControlHost(this)),
+ attached_control_(nullptr) {
GetContainerRenderObject()->SetDefaultHorizontalAlignment(Alignment::Stretch);
GetContainerRenderObject()->SetDefaultVertialAlignment(Alignment::Stretch);
}
@@ -21,8 +23,6 @@ Window* Window::CreatePopup() {
return window;
}
-std::string Window::GetControlType() const { return std::string(kControlType); }
-
void Window::SetAttachedControl(Control* control) {
attached_control_ = control;
}