diff options
author | crupest <crupest@outlook.com> | 2020-03-03 00:39:10 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2020-03-03 00:39:10 +0800 |
commit | b0946c0e6dc163fe981f230302a1976449150907 (patch) | |
tree | 3786417609ec00e42c0e9a102c39238135dfc9b5 | |
parent | ebb8f7beba71fc0d3cd81f60559e4005e05e48d5 (diff) | |
download | cru-b0946c0e6dc163fe981f230302a1976449150907.tar.gz cru-b0946c0e6dc163fe981f230302a1976449150907.tar.bz2 cru-b0946c0e6dc163fe981f230302a1976449150907.zip |
...
-rw-r--r-- | include/cru/ui/controls/text_box.hpp | 13 | ||||
-rw-r--r-- | include/cru/ui/render/canvas_render_object.hpp | 4 | ||||
-rw-r--r-- | include/cru/ui/render/render_object.hpp | 3 | ||||
-rw-r--r-- | include/cru/ui/ui_manager.hpp | 28 | ||||
-rw-r--r-- | src/ui/controls/text_block.cpp | 8 | ||||
-rw-r--r-- | src/ui/controls/text_box.cpp | 28 | ||||
-rw-r--r-- | src/ui/render/canvas_render_object.cpp | 6 | ||||
-rw-r--r-- | src/ui/ui_manager.cpp | 18 |
8 files changed, 68 insertions, 40 deletions
diff --git a/include/cru/ui/controls/text_box.hpp b/include/cru/ui/controls/text_box.hpp index 48168e9d..76a6299e 100644 --- a/include/cru/ui/controls/text_box.hpp +++ b/include/cru/ui/controls/text_box.hpp @@ -1,6 +1,14 @@ #pragma once #include "../no_child_control.hpp" +#include <memory> + +namespace cru::ui::render { +class BorderRenderObject; +class TextRenderObject; +class CanvasRenderObject; +} // namespace cru::ui::render + namespace cru::ui::controls { class TextBox : public NoChildControl { public: @@ -16,5 +24,10 @@ class TextBox : public NoChildControl { ~TextBox() override; std::string_view GetControlType() const final { return control_type; } + + private: + std::unique_ptr<render::BorderRenderObject> border_render_object_; + std::unique_ptr<render::TextRenderObject> text_render_object_; + std::unique_ptr<render::CanvasRenderObject> caret_render_object_; }; } // namespace cru::ui::controls diff --git a/include/cru/ui/render/canvas_render_object.hpp b/include/cru/ui/render/canvas_render_object.hpp index fea68576..5fea8755 100644 --- a/include/cru/ui/render/canvas_render_object.hpp +++ b/include/cru/ui/render/canvas_render_object.hpp @@ -37,6 +37,10 @@ class CanvasRenderObject : public RenderObject { IEvent<CanvasPaintEventArgs>* PaintEvent() { return &paint_event_; } + protected: + Size OnMeasureContent(const Size& available_size) override; + void OnLayoutContent(const Rect& content_rect) override; + private: Event<CanvasPaintEventArgs> paint_event_; }; diff --git a/include/cru/ui/render/render_object.hpp b/include/cru/ui/render/render_object.hpp index b0a65819..0f0cb8df 100644 --- a/include/cru/ui/render/render_object.hpp +++ b/include/cru/ui/render/render_object.hpp @@ -32,6 +32,9 @@ struct IRenderHost : Interface { virtual IEvent<AfterLayoutEventArgs>* AfterLayoutEvent() = 0; }; +// Render object will not destroy its children when destroyed. Control must +// manage lifecycle of its render objects. Since control will destroy its +// children when destroyed, render objects will be destroyed along with it. class RenderObject : public Object { protected: enum class ChildMode { diff --git a/include/cru/ui/ui_manager.hpp b/include/cru/ui/ui_manager.hpp index 199ff28d..f196444c 100644 --- a/include/cru/ui/ui_manager.hpp +++ b/include/cru/ui/ui_manager.hpp @@ -11,26 +11,10 @@ struct IFont; } // namespace cru::platform::graph namespace cru::ui { -// TODO: Make this theme resource. -class PredefineResources : public Object { - public: - PredefineResources(); - PredefineResources(const PredefineResources& other) = delete; - PredefineResources(PredefineResources&& other) = delete; - PredefineResources& operator=(const PredefineResources& other) = delete; - PredefineResources& operator=(PredefineResources&& other) = delete; - ~PredefineResources() override = default; - - // region Button - std::shared_ptr<platform::graph::IBrush> button_normal_border_brush; - - // region TextBlock - std::shared_ptr<platform::graph::IBrush> text_block_selection_brush; - std::shared_ptr<platform::graph::IBrush> text_block_text_brush; - std::shared_ptr<platform::graph::IFont> text_block_font; -}; - struct ThemeResources { + std::shared_ptr<platform::graph::IFont> default_font; + std::shared_ptr<platform::graph::IBrush> text_brush; + std::shared_ptr<platform::graph::IBrush> text_selection_brush; controls::ButtonStyle button_style; }; @@ -48,15 +32,9 @@ class UiManager : public Object { UiManager& operator=(UiManager&& other) = delete; ~UiManager() override = default; - const PredefineResources* GetPredefineResources() const { - return predefine_resources_.get(); - } - ThemeResources* GetThemeResources() { return &theme_resource_; } private: - std::unique_ptr<PredefineResources> predefine_resources_; - ThemeResources theme_resource_; }; } // namespace cru::ui diff --git a/src/ui/controls/text_block.cpp b/src/ui/controls/text_block.cpp index 333b44c8..9d01dee9 100644 --- a/src/ui/controls/text_block.cpp +++ b/src/ui/controls/text_block.cpp @@ -7,12 +7,10 @@ namespace cru::ui::controls { using render::TextRenderObject; TextBlock::TextBlock() { - const auto predefined_resources = - UiManager::GetInstance()->GetPredefineResources(); + const auto theme_resources = UiManager::GetInstance()->GetThemeResources(); render_object_ = std::make_unique<TextRenderObject>( - predefined_resources->text_block_text_brush, - predefined_resources->text_block_font, - predefined_resources->text_block_selection_brush); + theme_resources->text_brush, theme_resources->default_font, + theme_resources->text_selection_brush); render_object_->SetAttachedControl(this); } diff --git a/src/ui/controls/text_box.cpp b/src/ui/controls/text_box.cpp index 7fde06d9..07b70b94 100644 --- a/src/ui/controls/text_box.cpp +++ b/src/ui/controls/text_box.cpp @@ -1 +1,29 @@ #include "cru/ui/controls/text_box.hpp" + +#include "cru/ui/render/border_render_object.hpp" +#include "cru/ui/render/canvas_render_object.hpp" +#include "cru/ui/render/text_render_object.hpp" +#include "cru/ui/ui_manager.hpp" + +namespace cru::ui::controls { +using render::BorderRenderObject; +using render::CanvasRenderObject; +using render::TextRenderObject; + +TextBox::TextBox() + : border_render_object_(new BorderRenderObject()), + text_render_object_(), + caret_render_object_(new CanvasRenderObject()) { + const auto theme_resources = UiManager::GetInstance()->GetThemeResources(); + + text_render_object_ = std::make_unique<TextRenderObject>( + theme_resources->text_brush, theme_resources->default_font, + theme_resources->text_selection_brush); + + border_render_object_->SetAttachedControl(this); + text_render_object_->SetAttachedControl(this); + caret_render_object_->SetAttachedControl(this); +} + +TextBox::~TextBox() {} +} // namespace cru::ui::controls diff --git a/src/ui/render/canvas_render_object.cpp b/src/ui/render/canvas_render_object.cpp index 7a2a1af0..0a442317 100644 --- a/src/ui/render/canvas_render_object.cpp +++ b/src/ui/render/canvas_render_object.cpp @@ -15,4 +15,10 @@ RenderObject* CanvasRenderObject::HitTest(const Point& point) { const auto padding_rect = GetPaddingRect(); return padding_rect.IsPointInside(point) ? this : nullptr; } + +Size CanvasRenderObject::OnMeasureContent(const Size& available_size) { + return Size{}; +} + +void CanvasRenderObject::OnLayoutContent(const Rect& content_rect) {} } // namespace cru::ui::render diff --git a/src/ui/ui_manager.cpp b/src/ui/ui_manager.cpp index 6c61083b..f71f900f 100644 --- a/src/ui/ui_manager.cpp +++ b/src/ui/ui_manager.cpp @@ -18,15 +18,6 @@ std::unique_ptr<ISolidColorBrush> CreateSolidColorBrush(IGraphFactory* factory, } } // namespace -PredefineResources::PredefineResources() { - const auto factory = GetGraphFactory(); - - button_normal_border_brush = CreateSolidColorBrush(factory, colors::black); - text_block_selection_brush = CreateSolidColorBrush(factory, colors::skyblue); - text_block_text_brush = CreateSolidColorBrush(factory, colors::black); - text_block_font = factory->CreateFont("等线", 24.0f); -} - UiManager* UiManager::GetInstance() { static UiManager* instance = new UiManager(); GetUiApplication()->AddOnQuitHandler([] { @@ -36,8 +27,15 @@ UiManager* UiManager::GetInstance() { return instance; } -UiManager::UiManager() : predefine_resources_(new PredefineResources()) { +UiManager::UiManager() { const auto factory = GetGraphFactory(); + + theme_resource_.default_font = factory->CreateFont("等线", 24.0f); + + theme_resource_.text_brush = CreateSolidColorBrush(factory, colors::black); + theme_resource_.text_selection_brush = + CreateSolidColorBrush(factory, colors::skyblue); + theme_resource_.button_style.normal.border_brush = CreateSolidColorBrush(factory, Color::FromHex(0x00bfff)); theme_resource_.button_style.hover.border_brush = |