diff options
author | crupest <crupest@outlook.com> | 2022-01-27 21:37:38 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-01-27 21:37:38 +0800 |
commit | 103bcb137273729a0f23ff3771e26150a64454ba (patch) | |
tree | 5d75e869cc4b1bf0f2afdfbc9ed5ff0f78e0d030 | |
parent | 560c0ead613658a2b7444907c3d1d69e49be8c32 (diff) | |
download | cru-103bcb137273729a0f23ff3771e26150a64454ba.tar.gz cru-103bcb137273729a0f23ff3771e26150a64454ba.tar.bz2 cru-103bcb137273729a0f23ff3771e26150a64454ba.zip |
...
-rw-r--r-- | assets/cru/ui/DefaultResources.xml | 13 | ||||
-rw-r--r-- | include/cru/ui/ThemeManager.hpp | 2 | ||||
-rw-r--r-- | include/cru/ui/UiManager.hpp | 44 | ||||
-rw-r--r-- | include/cru/ui/mapper/FontMapper.hpp | 19 | ||||
-rw-r--r-- | src/common/String.cpp | 2 | ||||
-rw-r--r-- | src/ui/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/ui/ThemeManager.cpp | 5 | ||||
-rw-r--r-- | src/ui/UiManager.cpp | 54 | ||||
-rw-r--r-- | src/ui/components/Menu.cpp | 1 | ||||
-rw-r--r-- | src/ui/controls/Button.cpp | 1 | ||||
-rw-r--r-- | src/ui/controls/TextBlock.cpp | 10 | ||||
-rw-r--r-- | src/ui/controls/TextBox.cpp | 8 | ||||
-rw-r--r-- | src/ui/mapper/FontMapper.cpp | 20 | ||||
-rw-r--r-- | src/ui/mapper/MapperRegistry.cpp | 2 | ||||
-rw-r--r-- | src/ui/render/ScrollBar.cpp | 1 |
15 files changed, 73 insertions, 111 deletions
diff --git a/assets/cru/ui/DefaultResources.xml b/assets/cru/ui/DefaultResources.xml index 68b7a31a..89f2b4a5 100644 --- a/assets/cru/ui/DefaultResources.xml +++ b/assets/cru/ui/DefaultResources.xml @@ -1,4 +1,17 @@ <Theme> + <Resource key="text.font"> + <Font /> + </Resource> + <Resource key="text.brush"> + <Color value="black" /> + </Resource> + <Resource key="text.selection.brush"> + <Color value="skyblue" /> + </Resource> + <Resource key="text.caret.brush"> + <Color value="black" /> + </Resource> + <Resource key="scrollbar.collapse-thumb.color"> <Color value="gray" alpha="0.5" /> </Resource> diff --git a/include/cru/ui/ThemeManager.hpp b/include/cru/ui/ThemeManager.hpp index 7d52fa48..be9daa47 100644 --- a/include/cru/ui/ThemeManager.hpp +++ b/include/cru/ui/ThemeManager.hpp @@ -56,6 +56,8 @@ class CRU_UI_API ThemeManager : public Object { std::shared_ptr<platform::graphics::IBrush> GetResourceBrush( const String& key); + std::shared_ptr<platform::graphics::IFont> GetResourceFont(const String& key); + std::shared_ptr<style::StyleRuleSet> GetResourceStyleRuleSet( const String& key); diff --git a/include/cru/ui/UiManager.hpp b/include/cru/ui/UiManager.hpp deleted file mode 100644 index 9ac7f416..00000000 --- a/include/cru/ui/UiManager.hpp +++ /dev/null @@ -1,44 +0,0 @@ -#pragma once -#include "Base.hpp" - -#include "controls/Base.hpp" -#include "cru/platform/graphics/Brush.hpp" -#include "cru/ui/helper/ClickDetector.hpp" -#include "style/StyleRuleSet.hpp" - -#include <gsl/pointers> -#include <memory> -#include <string> -#include <unordered_map> - -namespace cru::ui { -struct ThemeResources { - String default_font_family; - std::shared_ptr<platform::graphics::IFont> default_font; - std::shared_ptr<platform::graphics::IBrush> text_brush; - std::shared_ptr<platform::graphics::IBrush> text_selection_brush; - std::shared_ptr<platform::graphics::IBrush> caret_brush; -}; - -class CRU_UI_API UiManager : public Object { - public: - static UiManager* GetInstance(); - - private: - UiManager(); - - public: - UiManager(const UiManager& other) = delete; - UiManager(UiManager&& other) = delete; - UiManager& operator=(const UiManager& other) = delete; - UiManager& operator=(UiManager&& other) = delete; - ~UiManager() override; - - ThemeResources* GetThemeResources() { return &theme_resource_; } - - void ReadResourcesFile(const String& file_path); - - private: - ThemeResources theme_resource_; -}; -} // namespace cru::ui diff --git a/include/cru/ui/mapper/FontMapper.hpp b/include/cru/ui/mapper/FontMapper.hpp new file mode 100644 index 00000000..ad8c43b5 --- /dev/null +++ b/include/cru/ui/mapper/FontMapper.hpp @@ -0,0 +1,19 @@ +#pragma once +#include "Mapper.hpp" +#include "cru/common/Base.hpp" +#include "cru/platform/graphics/Font.hpp" + +namespace cru::ui::mapper { +class FontMapper : public BasicRefMapper<platform::graphics::IFont> { + public: + CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(FontMapper) + + public: + bool SupportMapFromXml() override { return true; } + bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; + + protected: + std::shared_ptr<platform::graphics::IFont> DoMapFromXml( + xml::XmlElementNode* node) override; +}; +} // namespace cru::ui::mapper diff --git a/src/common/String.cpp b/src/common/String.cpp index e791d119..a43c2abd 100644 --- a/src/common/String.cpp +++ b/src/common/String.cpp @@ -290,7 +290,7 @@ String& String::Trim() { } void String::AppendCodePoint(CodePoint code_point) { - if (Utf16EncodeCodePointAppend(code_point, *this)) { + if (!Utf16EncodeCodePointAppend(code_point, *this)) { throw TextEncodeException(u"Code point out of range."); } } diff --git a/src/ui/CMakeLists.txt b/src/ui/CMakeLists.txt index d19784b2..88603f48 100644 --- a/src/ui/CMakeLists.txt +++ b/src/ui/CMakeLists.txt @@ -1,7 +1,6 @@ add_library(cru_ui SHARED Helper.cpp ThemeManager.cpp - UiManager.cpp components/Component.cpp components/Menu.cpp controls/Button.cpp @@ -29,6 +28,7 @@ add_library(cru_ui SHARED mapper/ColorMapper.cpp mapper/CornerRadiusMapper.cpp mapper/CursorMapper.cpp + mapper/FontMapper.cpp mapper/Mapper.cpp mapper/MapperRegistry.cpp mapper/PointMapper.cpp diff --git a/src/ui/ThemeManager.cpp b/src/ui/ThemeManager.cpp index 4ed5c5b6..dc08edb3 100644 --- a/src/ui/ThemeManager.cpp +++ b/src/ui/ThemeManager.cpp @@ -35,6 +35,11 @@ std::shared_ptr<platform::graphics::IBrush> ThemeManager::GetResourceBrush( return GetResource<std::shared_ptr<platform::graphics::IBrush>>(key); } +std::shared_ptr<platform::graphics::IFont> ThemeManager::GetResourceFont( + const String& key) { + return GetResource<std::shared_ptr<platform::graphics::IFont>>(key); +} + std::shared_ptr<style::StyleRuleSet> ThemeManager::GetResourceStyleRuleSet( const String& key) { return GetResource<std::shared_ptr<style::StyleRuleSet>>(key); diff --git a/src/ui/UiManager.cpp b/src/ui/UiManager.cpp deleted file mode 100644 index 0c8f258d..00000000 --- a/src/ui/UiManager.cpp +++ /dev/null @@ -1,54 +0,0 @@ -#include "cru/ui/UiManager.hpp" - -#include "Helper.hpp" -#include "cru/platform/graphics/Brush.hpp" -#include "cru/platform/graphics/Factory.hpp" -#include "cru/platform/graphics/Font.hpp" -#include "cru/platform/gui/Cursor.hpp" -#include "cru/platform/gui/UiApplication.hpp" -#include "cru/ui/Base.hpp" -#include "cru/ui/helper/ClickDetector.hpp" -#include "cru/ui/mapper/MapperRegistry.hpp" -#include "cru/ui/render/ScrollBar.hpp" -#include "cru/ui/style/ApplyBorderStyleInfo.hpp" -#include "cru/ui/style/Condition.hpp" -#include "cru/ui/style/Styler.hpp" -#include "cru/xml/XmlNode.hpp" -#include "cru/xml/XmlParser.hpp" - -#include <optional> - -namespace cru::ui { -using namespace cru::platform::graphics; -using namespace cru::ui::style; -using namespace cru::ui::helper; - -UiManager* UiManager::GetInstance() { - static UiManager* instance = new UiManager(); - GetUiApplication()->AddOnQuitHandler([] { - delete instance; - instance = nullptr; - }); - return instance; -} - -UiManager::UiManager() { - const auto factory = GetGraphicsFactory(); - - theme_resource_.default_font_family = u""; - - theme_resource_.default_font = - factory->CreateFont(theme_resource_.default_font_family, 24.0f); - - const auto black_brush = - std::shared_ptr<platform::graphics::ISolidColorBrush>( - factory->CreateSolidColorBrush(colors::black)); - theme_resource_.text_brush = black_brush; - theme_resource_.text_selection_brush = - factory->CreateSolidColorBrush(colors::skyblue); - theme_resource_.caret_brush = black_brush; -} - -UiManager::~UiManager() = default; - -} // namespace cru::ui diff --git a/src/ui/components/Menu.cpp b/src/ui/components/Menu.cpp index 7405b23c..22415cd3 100644 --- a/src/ui/components/Menu.cpp +++ b/src/ui/components/Menu.cpp @@ -2,7 +2,6 @@ #include <functional> #include "cru/platform/gui/Window.hpp" #include "cru/ui/ThemeManager.hpp" -#include "cru/ui/UiManager.hpp" #include "cru/ui/controls/Button.hpp" #include "cru/ui/controls/Control.hpp" #include "cru/ui/controls/FlexLayout.hpp" diff --git a/src/ui/controls/Button.cpp b/src/ui/controls/Button.cpp index 887bcae6..1f649819 100644 --- a/src/ui/controls/Button.cpp +++ b/src/ui/controls/Button.cpp @@ -5,7 +5,6 @@ #include "cru/platform/gui/Cursor.hpp" #include "cru/platform/gui/UiApplication.hpp" #include "cru/ui/ThemeManager.hpp" -#include "cru/ui/UiManager.hpp" #include "cru/ui/helper/ClickDetector.hpp" #include "cru/ui/render/BorderRenderObject.hpp" diff --git a/src/ui/controls/TextBlock.cpp b/src/ui/controls/TextBlock.cpp index 16fd5df6..b4d327f3 100644 --- a/src/ui/controls/TextBlock.cpp +++ b/src/ui/controls/TextBlock.cpp @@ -1,6 +1,6 @@ #include "cru/ui/controls/TextBlock.hpp" -#include "cru/ui/UiManager.hpp" +#include "cru/ui/ThemeManager.hpp" #include "cru/ui/render/CanvasRenderObject.hpp" #include "cru/ui/render/StackLayoutRenderObject.hpp" #include "cru/ui/render/TextRenderObject.hpp" @@ -18,11 +18,13 @@ TextBlock* TextBlock::Create(String text, bool selectable) { } TextBlock::TextBlock() { - const auto theme_resources = UiManager::GetInstance()->GetThemeResources(); + const auto theme_manager = ThemeManager::GetInstance(); text_render_object_ = std::make_unique<TextRenderObject>( - theme_resources->text_brush, theme_resources->default_font, - theme_resources->text_selection_brush, theme_resources->caret_brush); + theme_manager->GetResourceBrush(u"text.brush"), + theme_manager->GetResourceFont(u"text.font"), + theme_manager->GetResourceBrush(u"text.selection.brush"), + theme_manager->GetResourceBrush(u"text.caret.brush")); text_render_object_->SetAttachedControl(this); diff --git a/src/ui/controls/TextBox.cpp b/src/ui/controls/TextBox.cpp index 20e89028..a5c8480e 100644 --- a/src/ui/controls/TextBox.cpp +++ b/src/ui/controls/TextBox.cpp @@ -1,7 +1,6 @@ #include "cru/ui/controls/TextBox.hpp" #include "cru/ui/ThemeManager.hpp" -#include "cru/ui/UiManager.hpp" #include "cru/ui/render/BorderRenderObject.hpp" #include "cru/ui/render/CanvasRenderObject.hpp" #include "cru/ui/render/ScrollRenderObject.hpp" @@ -16,12 +15,13 @@ using render::TextRenderObject; TextBox::TextBox() : border_render_object_(new BorderRenderObject()), scroll_render_object_(new ScrollRenderObject()) { - const auto theme_resources = UiManager::GetInstance()->GetThemeResources(); auto theme_manager = ThemeManager::GetInstance(); text_render_object_ = std::make_unique<TextRenderObject>( - theme_resources->text_brush, theme_resources->default_font, - theme_resources->text_selection_brush, theme_resources->caret_brush); + theme_manager->GetResourceBrush(u"text.brush"), + theme_manager->GetResourceFont(u"text.font"), + theme_manager->GetResourceBrush(u"text.selection.brush"), + theme_manager->GetResourceBrush(u"text.caret.brush")); text_render_object_->SetEditMode(true); border_render_object_->AddChild(scroll_render_object_.get(), 0); diff --git a/src/ui/mapper/FontMapper.cpp b/src/ui/mapper/FontMapper.cpp new file mode 100644 index 00000000..26a17c5f --- /dev/null +++ b/src/ui/mapper/FontMapper.cpp @@ -0,0 +1,20 @@ +#include "cru/ui/mapper/FontMapper.hpp" +#include "../Helper.hpp" +#include "cru/platform/graphics/Factory.hpp" + +namespace cru::ui::mapper { +bool FontMapper::XmlElementIsOfThisType(xml::XmlElementNode* node) { + return node->GetTag().CaseInsensitiveEqual(u"font"); +} + +std::shared_ptr<platform::graphics::IFont> FontMapper::DoMapFromXml( + xml::XmlElementNode* node) { + auto font_family_attr = node->GetOptionalAttribute(u"family"); + auto font_size_attr = node->GetOptionalAttribute(u"size"); + + auto font_family = font_family_attr.value_or(u""); + auto font_size = font_size_attr ? font_size_attr->ParseToFloat() : 24.0f; + + return GetGraphicsFactory()->CreateFont(font_family, font_size); +} +} // namespace cru::ui::mapper diff --git a/src/ui/mapper/MapperRegistry.cpp b/src/ui/mapper/MapperRegistry.cpp index 97c5cf21..121a65e7 100644 --- a/src/ui/mapper/MapperRegistry.cpp +++ b/src/ui/mapper/MapperRegistry.cpp @@ -4,6 +4,7 @@ #include "cru/ui/mapper/ColorMapper.hpp" #include "cru/ui/mapper/CornerRadiusMapper.hpp" #include "cru/ui/mapper/CursorMapper.hpp" +#include "cru/ui/mapper/FontMapper.hpp" #include "cru/ui/mapper/PointMapper.hpp" #include "cru/ui/mapper/SizeMapper.hpp" #include "cru/ui/mapper/ThicknessMapper.hpp" @@ -29,6 +30,7 @@ MapperRegistry::MapperRegistry() { RegisterMapper(new BrushMapper()); RegisterMapper(new CornerRadiusMapper()); + RegisterMapper(new FontMapper()); RegisterMapper(new PointMapper()); RegisterMapper(new SizeMapper()); RegisterMapper(new ThicknessMapper()); diff --git a/src/ui/render/ScrollBar.cpp b/src/ui/render/ScrollBar.cpp index 8676de61..37aebeaf 100644 --- a/src/ui/render/ScrollBar.cpp +++ b/src/ui/render/ScrollBar.cpp @@ -11,7 +11,6 @@ #include "cru/platform/gui/Cursor.hpp" #include "cru/ui/Base.hpp" #include "cru/ui/ThemeManager.hpp" -#include "cru/ui/UiManager.hpp" #include "cru/ui/events/UiEvents.hpp" #include "cru/ui/helper/ClickDetector.hpp" #include "cru/ui/host/WindowHost.hpp" |