From 74bb9cd27242b9320f99ff4d2b50c3051576cc14 Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 8 Feb 2022 16:53:51 +0800 Subject: ... --- include/cru/ui/Base.h | 152 ++++++++++++ include/cru/ui/Base.hpp | 152 ------------ include/cru/ui/DebugFlags.h | 10 + include/cru/ui/DebugFlags.hpp | 10 - include/cru/ui/ThemeManager.h | 92 ++++++++ include/cru/ui/ThemeManager.hpp | 92 -------- include/cru/ui/components/Component.h | 19 ++ include/cru/ui/components/Component.hpp | 19 -- include/cru/ui/components/Menu.h | 107 +++++++++ include/cru/ui/components/Menu.hpp | 107 --------- include/cru/ui/controls/Base.h | 4 + include/cru/ui/controls/Base.hpp | 4 - include/cru/ui/controls/Button.h | 53 +++++ include/cru/ui/controls/Button.hpp | 53 ----- include/cru/ui/controls/Container.h | 25 ++ include/cru/ui/controls/Container.hpp | 25 -- include/cru/ui/controls/ContentControl.h | 38 +++ include/cru/ui/controls/ContentControl.hpp | 38 --- include/cru/ui/controls/Control.h | 157 +++++++++++++ include/cru/ui/controls/Control.hpp | 157 ------------- include/cru/ui/controls/FlexLayout.h | 40 ++++ include/cru/ui/controls/FlexLayout.hpp | 40 ---- include/cru/ui/controls/IBorderControl.h | 10 + include/cru/ui/controls/IBorderControl.hpp | 10 - include/cru/ui/controls/IClickableControl.h | 12 + include/cru/ui/controls/IClickableControl.hpp | 12 - include/cru/ui/controls/LayoutControl.h | 37 +++ include/cru/ui/controls/LayoutControl.hpp | 37 --- include/cru/ui/controls/NoChildControl.h | 20 ++ include/cru/ui/controls/NoChildControl.hpp | 20 -- include/cru/ui/controls/Popup.h | 26 +++ include/cru/ui/controls/Popup.hpp | 26 --- include/cru/ui/controls/RootControl.h | 39 ++++ include/cru/ui/controls/RootControl.hpp | 39 ---- include/cru/ui/controls/ScrollView.h | 32 +++ include/cru/ui/controls/ScrollView.hpp | 32 --- include/cru/ui/controls/StackLayout.h | 31 +++ include/cru/ui/controls/StackLayout.hpp | 31 --- include/cru/ui/controls/TextBlock.h | 44 ++++ include/cru/ui/controls/TextBlock.hpp | 44 ---- include/cru/ui/controls/TextBox.h | 46 ++++ include/cru/ui/controls/TextBox.hpp | 46 ---- include/cru/ui/controls/TextHostControlService.h | 219 ++++++++++++++++++ include/cru/ui/controls/TextHostControlService.hpp | 219 ------------------ include/cru/ui/controls/Window.h | 27 +++ include/cru/ui/controls/Window.hpp | 27 --- include/cru/ui/events/FocusChangeEventArgs.h | 23 ++ include/cru/ui/events/FocusChangeEventArgs.hpp | 23 -- include/cru/ui/events/KeyEventArgs.h | 29 +++ include/cru/ui/events/KeyEventArgs.hpp | 29 --- include/cru/ui/events/MouseButtonEventArgs.h | 35 +++ include/cru/ui/events/MouseButtonEventArgs.hpp | 35 --- include/cru/ui/events/MouseEventArgs.h | 27 +++ include/cru/ui/events/MouseEventArgs.hpp | 27 --- include/cru/ui/events/MouseWheelEventArgs.h | 29 +++ include/cru/ui/events/MouseWheelEventArgs.hpp | 29 --- include/cru/ui/events/PaintEventArgs.h | 25 ++ include/cru/ui/events/PaintEventArgs.hpp | 25 -- include/cru/ui/events/RoutedEvent.h | 39 ++++ include/cru/ui/events/RoutedEvent.hpp | 39 ---- include/cru/ui/events/UiEventArgs.h | 28 +++ include/cru/ui/events/UiEventArgs.hpp | 28 --- include/cru/ui/events/UiEvents.h | 10 + include/cru/ui/events/UiEvents.hpp | 10 - include/cru/ui/helper/ClickDetector.h | 87 +++++++ include/cru/ui/helper/ClickDetector.hpp | 87 ------- include/cru/ui/helper/ShortcutHub.h | 142 ++++++++++++ include/cru/ui/helper/ShortcutHub.hpp | 142 ------------ include/cru/ui/host/LayoutPaintCycler.h | 40 ++++ include/cru/ui/host/LayoutPaintCycler.hpp | 40 ---- include/cru/ui/host/WindowHost.h | 164 +++++++++++++ include/cru/ui/host/WindowHost.hpp | 164 ------------- include/cru/ui/mapper/BorderStyleMapper.h | 20 ++ include/cru/ui/mapper/BorderStyleMapper.hpp | 20 -- include/cru/ui/mapper/BrushMapper.h | 20 ++ include/cru/ui/mapper/BrushMapper.hpp | 20 -- include/cru/ui/mapper/ColorMapper.h | 19 ++ include/cru/ui/mapper/ColorMapper.hpp | 19 -- include/cru/ui/mapper/CornerRadiusMapper.h | 17 ++ include/cru/ui/mapper/CornerRadiusMapper.hpp | 17 -- include/cru/ui/mapper/CursorMapper.h | 21 ++ include/cru/ui/mapper/CursorMapper.hpp | 21 -- include/cru/ui/mapper/FontMapper.h | 19 ++ include/cru/ui/mapper/FontMapper.hpp | 19 -- include/cru/ui/mapper/Mapper.h | 92 ++++++++ include/cru/ui/mapper/Mapper.hpp | 92 -------- include/cru/ui/mapper/MapperRegistry.h | 58 +++++ include/cru/ui/mapper/MapperRegistry.hpp | 58 ----- include/cru/ui/mapper/PointMapper.h | 18 ++ include/cru/ui/mapper/PointMapper.hpp | 18 -- include/cru/ui/mapper/SizeMapper.h | 18 ++ include/cru/ui/mapper/SizeMapper.hpp | 18 -- include/cru/ui/mapper/ThicknessMapper.h | 22 ++ include/cru/ui/mapper/ThicknessMapper.hpp | 22 -- include/cru/ui/mapper/style/AndConditionMapper.h | 25 ++ include/cru/ui/mapper/style/AndConditionMapper.hpp | 25 -- include/cru/ui/mapper/style/BorderStylerMapper.h | 28 +++ include/cru/ui/mapper/style/BorderStylerMapper.hpp | 28 --- .../ui/mapper/style/ClickStateConditionMapper.h | 26 +++ .../ui/mapper/style/ClickStateConditionMapper.hpp | 26 --- include/cru/ui/mapper/style/CursorStylerMapper.h | 26 +++ include/cru/ui/mapper/style/CursorStylerMapper.hpp | 26 --- include/cru/ui/mapper/style/FocusConditionMapper.h | 26 +++ .../cru/ui/mapper/style/FocusConditionMapper.hpp | 26 --- include/cru/ui/mapper/style/HoverConditionMapper.h | 26 +++ .../cru/ui/mapper/style/HoverConditionMapper.hpp | 26 --- include/cru/ui/mapper/style/IConditionMapper.h | 17 ++ include/cru/ui/mapper/style/IConditionMapper.hpp | 17 -- include/cru/ui/mapper/style/IStylerMapper.h | 17 ++ include/cru/ui/mapper/style/IStylerMapper.hpp | 17 -- include/cru/ui/mapper/style/NoConditionMapper.h | 29 +++ include/cru/ui/mapper/style/NoConditionMapper.hpp | 29 --- include/cru/ui/mapper/style/OrConditionMapper.h | 25 ++ include/cru/ui/mapper/style/OrConditionMapper.hpp | 25 -- include/cru/ui/mapper/style/StyleRuleMapper.h | 20 ++ include/cru/ui/mapper/style/StyleRuleMapper.hpp | 20 -- include/cru/ui/mapper/style/StyleRuleSetMapper.h | 21 ++ include/cru/ui/mapper/style/StyleRuleSetMapper.hpp | 21 -- include/cru/ui/render/Base.h | 12 + include/cru/ui/render/Base.hpp | 12 - include/cru/ui/render/BorderRenderObject.h | 109 +++++++++ include/cru/ui/render/BorderRenderObject.hpp | 109 --------- include/cru/ui/render/CanvasRenderObject.h | 36 +++ include/cru/ui/render/CanvasRenderObject.hpp | 36 --- include/cru/ui/render/FlexLayoutRenderObject.h | 120 ++++++++++ include/cru/ui/render/FlexLayoutRenderObject.hpp | 120 ---------- include/cru/ui/render/LayoutHelper.h | 14 ++ include/cru/ui/render/LayoutHelper.hpp | 14 -- include/cru/ui/render/LayoutRenderObject.h | 86 +++++++ include/cru/ui/render/LayoutRenderObject.hpp | 86 ------- include/cru/ui/render/MeasureRequirement.h | 251 ++++++++++++++++++++ include/cru/ui/render/MeasureRequirement.hpp | 251 -------------------- include/cru/ui/render/RenderObject.h | 240 +++++++++++++++++++ include/cru/ui/render/RenderObject.hpp | 240 ------------------- include/cru/ui/render/ScrollBar.h | 255 +++++++++++++++++++++ include/cru/ui/render/ScrollBar.hpp | 255 --------------------- include/cru/ui/render/ScrollRenderObject.h | 98 ++++++++ include/cru/ui/render/ScrollRenderObject.hpp | 98 -------- include/cru/ui/render/StackLayoutRenderObject.h | 57 +++++ include/cru/ui/render/StackLayoutRenderObject.hpp | 57 ----- include/cru/ui/render/TextRenderObject.h | 121 ++++++++++ include/cru/ui/render/TextRenderObject.hpp | 121 ---------- include/cru/ui/style/ApplyBorderStyleInfo.h | 30 +++ include/cru/ui/style/ApplyBorderStyleInfo.hpp | 30 --- include/cru/ui/style/Condition.h | 133 +++++++++++ include/cru/ui/style/Condition.hpp | 133 ----------- include/cru/ui/style/StyleRule.h | 49 ++++ include/cru/ui/style/StyleRule.hpp | 49 ---- include/cru/ui/style/StyleRuleSet.h | 80 +++++++ include/cru/ui/style/StyleRuleSet.hpp | 80 ------- include/cru/ui/style/Styler.h | 90 ++++++++ include/cru/ui/style/Styler.hpp | 90 -------- 152 files changed, 4309 insertions(+), 4309 deletions(-) create mode 100644 include/cru/ui/Base.h delete mode 100644 include/cru/ui/Base.hpp create mode 100644 include/cru/ui/DebugFlags.h delete mode 100644 include/cru/ui/DebugFlags.hpp create mode 100644 include/cru/ui/ThemeManager.h delete mode 100644 include/cru/ui/ThemeManager.hpp create mode 100644 include/cru/ui/components/Component.h delete mode 100644 include/cru/ui/components/Component.hpp create mode 100644 include/cru/ui/components/Menu.h delete mode 100644 include/cru/ui/components/Menu.hpp create mode 100644 include/cru/ui/controls/Base.h delete mode 100644 include/cru/ui/controls/Base.hpp create mode 100644 include/cru/ui/controls/Button.h delete mode 100644 include/cru/ui/controls/Button.hpp create mode 100644 include/cru/ui/controls/Container.h delete mode 100644 include/cru/ui/controls/Container.hpp create mode 100644 include/cru/ui/controls/ContentControl.h delete mode 100644 include/cru/ui/controls/ContentControl.hpp create mode 100644 include/cru/ui/controls/Control.h delete mode 100644 include/cru/ui/controls/Control.hpp create mode 100644 include/cru/ui/controls/FlexLayout.h delete mode 100644 include/cru/ui/controls/FlexLayout.hpp create mode 100644 include/cru/ui/controls/IBorderControl.h delete mode 100644 include/cru/ui/controls/IBorderControl.hpp create mode 100644 include/cru/ui/controls/IClickableControl.h delete mode 100644 include/cru/ui/controls/IClickableControl.hpp create mode 100644 include/cru/ui/controls/LayoutControl.h delete mode 100644 include/cru/ui/controls/LayoutControl.hpp create mode 100644 include/cru/ui/controls/NoChildControl.h delete mode 100644 include/cru/ui/controls/NoChildControl.hpp create mode 100644 include/cru/ui/controls/Popup.h delete mode 100644 include/cru/ui/controls/Popup.hpp create mode 100644 include/cru/ui/controls/RootControl.h delete mode 100644 include/cru/ui/controls/RootControl.hpp create mode 100644 include/cru/ui/controls/ScrollView.h delete mode 100644 include/cru/ui/controls/ScrollView.hpp create mode 100644 include/cru/ui/controls/StackLayout.h delete mode 100644 include/cru/ui/controls/StackLayout.hpp create mode 100644 include/cru/ui/controls/TextBlock.h delete mode 100644 include/cru/ui/controls/TextBlock.hpp create mode 100644 include/cru/ui/controls/TextBox.h delete mode 100644 include/cru/ui/controls/TextBox.hpp create mode 100644 include/cru/ui/controls/TextHostControlService.h delete mode 100644 include/cru/ui/controls/TextHostControlService.hpp create mode 100644 include/cru/ui/controls/Window.h delete mode 100644 include/cru/ui/controls/Window.hpp create mode 100644 include/cru/ui/events/FocusChangeEventArgs.h delete mode 100644 include/cru/ui/events/FocusChangeEventArgs.hpp create mode 100644 include/cru/ui/events/KeyEventArgs.h delete mode 100644 include/cru/ui/events/KeyEventArgs.hpp create mode 100644 include/cru/ui/events/MouseButtonEventArgs.h delete mode 100644 include/cru/ui/events/MouseButtonEventArgs.hpp create mode 100644 include/cru/ui/events/MouseEventArgs.h delete mode 100644 include/cru/ui/events/MouseEventArgs.hpp create mode 100644 include/cru/ui/events/MouseWheelEventArgs.h delete mode 100644 include/cru/ui/events/MouseWheelEventArgs.hpp create mode 100644 include/cru/ui/events/PaintEventArgs.h delete mode 100644 include/cru/ui/events/PaintEventArgs.hpp create mode 100644 include/cru/ui/events/RoutedEvent.h delete mode 100644 include/cru/ui/events/RoutedEvent.hpp create mode 100644 include/cru/ui/events/UiEventArgs.h delete mode 100644 include/cru/ui/events/UiEventArgs.hpp create mode 100644 include/cru/ui/events/UiEvents.h delete mode 100644 include/cru/ui/events/UiEvents.hpp create mode 100644 include/cru/ui/helper/ClickDetector.h delete mode 100644 include/cru/ui/helper/ClickDetector.hpp create mode 100644 include/cru/ui/helper/ShortcutHub.h delete mode 100644 include/cru/ui/helper/ShortcutHub.hpp create mode 100644 include/cru/ui/host/LayoutPaintCycler.h delete mode 100644 include/cru/ui/host/LayoutPaintCycler.hpp create mode 100644 include/cru/ui/host/WindowHost.h delete mode 100644 include/cru/ui/host/WindowHost.hpp create mode 100644 include/cru/ui/mapper/BorderStyleMapper.h delete mode 100644 include/cru/ui/mapper/BorderStyleMapper.hpp create mode 100644 include/cru/ui/mapper/BrushMapper.h delete mode 100644 include/cru/ui/mapper/BrushMapper.hpp create mode 100644 include/cru/ui/mapper/ColorMapper.h delete mode 100644 include/cru/ui/mapper/ColorMapper.hpp create mode 100644 include/cru/ui/mapper/CornerRadiusMapper.h delete mode 100644 include/cru/ui/mapper/CornerRadiusMapper.hpp create mode 100644 include/cru/ui/mapper/CursorMapper.h delete mode 100644 include/cru/ui/mapper/CursorMapper.hpp create mode 100644 include/cru/ui/mapper/FontMapper.h delete mode 100644 include/cru/ui/mapper/FontMapper.hpp create mode 100644 include/cru/ui/mapper/Mapper.h delete mode 100644 include/cru/ui/mapper/Mapper.hpp create mode 100644 include/cru/ui/mapper/MapperRegistry.h delete mode 100644 include/cru/ui/mapper/MapperRegistry.hpp create mode 100644 include/cru/ui/mapper/PointMapper.h delete mode 100644 include/cru/ui/mapper/PointMapper.hpp create mode 100644 include/cru/ui/mapper/SizeMapper.h delete mode 100644 include/cru/ui/mapper/SizeMapper.hpp create mode 100644 include/cru/ui/mapper/ThicknessMapper.h delete mode 100644 include/cru/ui/mapper/ThicknessMapper.hpp create mode 100644 include/cru/ui/mapper/style/AndConditionMapper.h delete mode 100644 include/cru/ui/mapper/style/AndConditionMapper.hpp create mode 100644 include/cru/ui/mapper/style/BorderStylerMapper.h delete mode 100644 include/cru/ui/mapper/style/BorderStylerMapper.hpp create mode 100644 include/cru/ui/mapper/style/ClickStateConditionMapper.h delete mode 100644 include/cru/ui/mapper/style/ClickStateConditionMapper.hpp create mode 100644 include/cru/ui/mapper/style/CursorStylerMapper.h delete mode 100644 include/cru/ui/mapper/style/CursorStylerMapper.hpp create mode 100644 include/cru/ui/mapper/style/FocusConditionMapper.h delete mode 100644 include/cru/ui/mapper/style/FocusConditionMapper.hpp create mode 100644 include/cru/ui/mapper/style/HoverConditionMapper.h delete mode 100644 include/cru/ui/mapper/style/HoverConditionMapper.hpp create mode 100644 include/cru/ui/mapper/style/IConditionMapper.h delete mode 100644 include/cru/ui/mapper/style/IConditionMapper.hpp create mode 100644 include/cru/ui/mapper/style/IStylerMapper.h delete mode 100644 include/cru/ui/mapper/style/IStylerMapper.hpp create mode 100644 include/cru/ui/mapper/style/NoConditionMapper.h delete mode 100644 include/cru/ui/mapper/style/NoConditionMapper.hpp create mode 100644 include/cru/ui/mapper/style/OrConditionMapper.h delete mode 100644 include/cru/ui/mapper/style/OrConditionMapper.hpp create mode 100644 include/cru/ui/mapper/style/StyleRuleMapper.h delete mode 100644 include/cru/ui/mapper/style/StyleRuleMapper.hpp create mode 100644 include/cru/ui/mapper/style/StyleRuleSetMapper.h delete mode 100644 include/cru/ui/mapper/style/StyleRuleSetMapper.hpp create mode 100644 include/cru/ui/render/Base.h delete mode 100644 include/cru/ui/render/Base.hpp create mode 100644 include/cru/ui/render/BorderRenderObject.h delete mode 100644 include/cru/ui/render/BorderRenderObject.hpp create mode 100644 include/cru/ui/render/CanvasRenderObject.h delete mode 100644 include/cru/ui/render/CanvasRenderObject.hpp create mode 100644 include/cru/ui/render/FlexLayoutRenderObject.h delete mode 100644 include/cru/ui/render/FlexLayoutRenderObject.hpp create mode 100644 include/cru/ui/render/LayoutHelper.h delete mode 100644 include/cru/ui/render/LayoutHelper.hpp create mode 100644 include/cru/ui/render/LayoutRenderObject.h delete mode 100644 include/cru/ui/render/LayoutRenderObject.hpp create mode 100644 include/cru/ui/render/MeasureRequirement.h delete mode 100644 include/cru/ui/render/MeasureRequirement.hpp create mode 100644 include/cru/ui/render/RenderObject.h delete mode 100644 include/cru/ui/render/RenderObject.hpp create mode 100644 include/cru/ui/render/ScrollBar.h delete mode 100644 include/cru/ui/render/ScrollBar.hpp create mode 100644 include/cru/ui/render/ScrollRenderObject.h delete mode 100644 include/cru/ui/render/ScrollRenderObject.hpp create mode 100644 include/cru/ui/render/StackLayoutRenderObject.h delete mode 100644 include/cru/ui/render/StackLayoutRenderObject.hpp create mode 100644 include/cru/ui/render/TextRenderObject.h delete mode 100644 include/cru/ui/render/TextRenderObject.hpp create mode 100644 include/cru/ui/style/ApplyBorderStyleInfo.h delete mode 100644 include/cru/ui/style/ApplyBorderStyleInfo.hpp create mode 100644 include/cru/ui/style/Condition.h delete mode 100644 include/cru/ui/style/Condition.hpp create mode 100644 include/cru/ui/style/StyleRule.h delete mode 100644 include/cru/ui/style/StyleRule.hpp create mode 100644 include/cru/ui/style/StyleRuleSet.h delete mode 100644 include/cru/ui/style/StyleRuleSet.hpp create mode 100644 include/cru/ui/style/Styler.h delete mode 100644 include/cru/ui/style/Styler.hpp (limited to 'include/cru/ui') diff --git a/include/cru/ui/Base.h b/include/cru/ui/Base.h new file mode 100644 index 00000000..44f76907 --- /dev/null +++ b/include/cru/ui/Base.h @@ -0,0 +1,152 @@ +#pragma once +#include "cru/common/Base.h" +#include "cru/platform/graphics/Base.h" +#include "cru/platform/gui/Base.h" + +#include +#include +#include +#include + +#ifdef CRU_PLATFORM_WINDOWS +#ifdef CRU_UI_EXPORT_API +#define CRU_UI_API __declspec(dllexport) +#else +#define CRU_UI_API __declspec(dllimport) +#endif +#else +#define CRU_UI_API +#endif + +namespace cru::ui { +//-------------------- region: import -------------------- +using cru::platform::Color; +using cru::platform::Ellipse; +using cru::platform::Matrix; +using cru::platform::Point; +using cru::platform::Rect; +using cru::platform::RoundedRect; +using cru::platform::Size; +using cru::platform::TextRange; +using cru::platform::Thickness; +using cru::platform::gui::MouseButton; + +namespace mouse_buttons = cru::platform::gui::mouse_buttons; + +namespace colors = cru::platform::colors; + +//-------------------- region: forward declaration -------------------- + +namespace controls { +class Window; +class Control; +} // namespace controls + +namespace host { +class WindowHost; +} + +namespace render { +class RenderObject; +} + +namespace style { +class StyleRuleSet; +class StyleRuleSetBind; +} // namespace style + +//-------------------- region: basic types -------------------- +enum class Direction { Horizontal, Vertical }; + +namespace internal { +constexpr int align_start = 0; +constexpr int align_end = align_start + 1; +constexpr int align_center = align_end + 1; +constexpr int align_stretch = align_center + 1; +} // namespace internal + +enum class Alignment { + Start = internal::align_start, + End = internal::align_end, + Center = internal::align_center, + Stretch = internal::align_stretch +}; + +struct CornerRadius { + constexpr CornerRadius() + : left_top(), right_top(), left_bottom(), right_bottom() {} + constexpr CornerRadius(const float& value) + : CornerRadius(Point{value, value}) {} + constexpr CornerRadius(const Point& value) + : left_top(value), + right_top(value), + left_bottom(value), + right_bottom(value) {} + constexpr CornerRadius(Point left_top, Point right_top, Point left_bottom, + Point right_bottom) + : left_top(left_top), + right_top(right_top), + left_bottom(left_bottom), + right_bottom(right_bottom) {} + + CornerRadius& SetAll(const Point& point) { + left_top = right_top = left_bottom = right_bottom = point; + return *this; + } + + Point left_top; + Point right_top; + Point left_bottom; + Point right_bottom; +}; + +inline bool operator==(const CornerRadius& left, const CornerRadius& right) { + return left.left_top == right.left_top && + left.left_bottom == right.left_bottom && + left.right_top == right.right_top && + left.right_bottom == right.right_bottom; +} + +inline bool operator!=(const CornerRadius& left, const CornerRadius& right) { + return !(left == right); +} + +class CanvasPaintEventArgs { + public: + CanvasPaintEventArgs(platform::graphics::IPainter* painter, + const Size& paint_size) + : painter_(painter), paint_size_(paint_size) {} + CRU_DEFAULT_COPY(CanvasPaintEventArgs) + CRU_DEFAULT_MOVE(CanvasPaintEventArgs) + ~CanvasPaintEventArgs() = default; + + platform::graphics::IPainter* GetPainter() const { return painter_; } + Size GetPaintSize() const { return paint_size_; } + + private: + platform::graphics::IPainter* painter_; + Size paint_size_; +}; + +enum class FlexDirection { + Horizontal, + HorizontalReverse, + Vertical, + VertivalReverse +}; + +using FlexMainAlignment = Alignment; +using FlexCrossAlignment = Alignment; + +struct FlexChildLayoutData { + float expand_factor = 0; + float shrink_factor = 1; + // nullopt stands for looking at parent's setting + std::optional cross_alignment = std::nullopt; +}; + +struct StackChildLayoutData { + std::optional horizontal; + std::optional vertical; +}; +} // namespace cru::ui diff --git a/include/cru/ui/Base.hpp b/include/cru/ui/Base.hpp deleted file mode 100644 index e99c956f..00000000 --- a/include/cru/ui/Base.hpp +++ /dev/null @@ -1,152 +0,0 @@ -#pragma once -#include "cru/common/Base.hpp" -#include "cru/platform/graphics/Base.hpp" -#include "cru/platform/gui/Base.hpp" - -#include -#include -#include -#include - -#ifdef CRU_PLATFORM_WINDOWS -#ifdef CRU_UI_EXPORT_API -#define CRU_UI_API __declspec(dllexport) -#else -#define CRU_UI_API __declspec(dllimport) -#endif -#else -#define CRU_UI_API -#endif - -namespace cru::ui { -//-------------------- region: import -------------------- -using cru::platform::Color; -using cru::platform::Ellipse; -using cru::platform::Matrix; -using cru::platform::Point; -using cru::platform::Rect; -using cru::platform::RoundedRect; -using cru::platform::Size; -using cru::platform::TextRange; -using cru::platform::Thickness; -using cru::platform::gui::MouseButton; - -namespace mouse_buttons = cru::platform::gui::mouse_buttons; - -namespace colors = cru::platform::colors; - -//-------------------- region: forward declaration -------------------- - -namespace controls { -class Window; -class Control; -} // namespace controls - -namespace host { -class WindowHost; -} - -namespace render { -class RenderObject; -} - -namespace style { -class StyleRuleSet; -class StyleRuleSetBind; -} // namespace style - -//-------------------- region: basic types -------------------- -enum class Direction { Horizontal, Vertical }; - -namespace internal { -constexpr int align_start = 0; -constexpr int align_end = align_start + 1; -constexpr int align_center = align_end + 1; -constexpr int align_stretch = align_center + 1; -} // namespace internal - -enum class Alignment { - Start = internal::align_start, - End = internal::align_end, - Center = internal::align_center, - Stretch = internal::align_stretch -}; - -struct CornerRadius { - constexpr CornerRadius() - : left_top(), right_top(), left_bottom(), right_bottom() {} - constexpr CornerRadius(const float& value) - : CornerRadius(Point{value, value}) {} - constexpr CornerRadius(const Point& value) - : left_top(value), - right_top(value), - left_bottom(value), - right_bottom(value) {} - constexpr CornerRadius(Point left_top, Point right_top, Point left_bottom, - Point right_bottom) - : left_top(left_top), - right_top(right_top), - left_bottom(left_bottom), - right_bottom(right_bottom) {} - - CornerRadius& SetAll(const Point& point) { - left_top = right_top = left_bottom = right_bottom = point; - return *this; - } - - Point left_top; - Point right_top; - Point left_bottom; - Point right_bottom; -}; - -inline bool operator==(const CornerRadius& left, const CornerRadius& right) { - return left.left_top == right.left_top && - left.left_bottom == right.left_bottom && - left.right_top == right.right_top && - left.right_bottom == right.right_bottom; -} - -inline bool operator!=(const CornerRadius& left, const CornerRadius& right) { - return !(left == right); -} - -class CanvasPaintEventArgs { - public: - CanvasPaintEventArgs(platform::graphics::IPainter* painter, - const Size& paint_size) - : painter_(painter), paint_size_(paint_size) {} - CRU_DEFAULT_COPY(CanvasPaintEventArgs) - CRU_DEFAULT_MOVE(CanvasPaintEventArgs) - ~CanvasPaintEventArgs() = default; - - platform::graphics::IPainter* GetPainter() const { return painter_; } - Size GetPaintSize() const { return paint_size_; } - - private: - platform::graphics::IPainter* painter_; - Size paint_size_; -}; - -enum class FlexDirection { - Horizontal, - HorizontalReverse, - Vertical, - VertivalReverse -}; - -using FlexMainAlignment = Alignment; -using FlexCrossAlignment = Alignment; - -struct FlexChildLayoutData { - float expand_factor = 0; - float shrink_factor = 1; - // nullopt stands for looking at parent's setting - std::optional cross_alignment = std::nullopt; -}; - -struct StackChildLayoutData { - std::optional horizontal; - std::optional vertical; -}; -} // namespace cru::ui diff --git a/include/cru/ui/DebugFlags.h b/include/cru/ui/DebugFlags.h new file mode 100644 index 00000000..71d2c03e --- /dev/null +++ b/include/cru/ui/DebugFlags.h @@ -0,0 +1,10 @@ +#pragma once + +namespace cru::ui::debug_flags { +constexpr bool routed_event = false; +constexpr bool layout = false; +constexpr bool shortcut = false; +constexpr bool text_service = false; +constexpr int click_detector = 0; +constexpr int draw = 0; +} // namespace cru::ui::debug_flags diff --git a/include/cru/ui/DebugFlags.hpp b/include/cru/ui/DebugFlags.hpp deleted file mode 100644 index 71d2c03e..00000000 --- a/include/cru/ui/DebugFlags.hpp +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -namespace cru::ui::debug_flags { -constexpr bool routed_event = false; -constexpr bool layout = false; -constexpr bool shortcut = false; -constexpr bool text_service = false; -constexpr int click_detector = 0; -constexpr int draw = 0; -} // namespace cru::ui::debug_flags diff --git a/include/cru/ui/ThemeManager.h b/include/cru/ui/ThemeManager.h new file mode 100644 index 00000000..9176d85e --- /dev/null +++ b/include/cru/ui/ThemeManager.h @@ -0,0 +1,92 @@ +#pragma once +#include "Base.h" +#include "cru/common/Base.h" +#include "cru/common/Event.h" +#include "cru/common/Exception.h" +#include "cru/platform/graphics/Brush.h" +#include "cru/ui/mapper/MapperRegistry.h" +#include "cru/ui/style/StyleRuleSet.h" +#include "cru/xml/XmlNode.h" + +#include +#include +#include +#include + +namespace cru::ui { +class CRU_UI_API ThemeResourceKeyNotExistException : public Exception { + public: + using Exception::Exception; +}; + +class CRU_UI_API BadThemeResourceException : public Exception { + public: + using Exception::Exception; +}; + +class CRU_UI_API ThemeManager : public Object { + public: + static ThemeManager* GetInstance(); + + private: + ThemeManager(); + + public: + CRU_DELETE_COPY(ThemeManager) + CRU_DELETE_MOVE(ThemeManager) + + ~ThemeManager() override; + + IEvent* ThemeResourceChangeEvent() { + return &theme_resource_change_event_; + } + + void ReadResourcesFile(const String& file_path); + + void SetThemeXml(xml::XmlElementNode* root); + + template + T GetResource(const String& key) { + auto find_result = theme_resource_map_.find(key); + if (find_result == theme_resource_map_.cend()) { + throw ThemeResourceKeyNotExistException( + Format(u"Theme resource key \"%s\" not exist.", key)); + } + + auto& cache = find_result->second.cache; + auto cache_find_result = cache.find(typeid(T)); + if (cache_find_result != cache.cend()) { + return std::any_cast(cache_find_result->second); + } + + auto mapper_registry = mapper::MapperRegistry::GetInstance(); + auto mapper = mapper_registry->GetMapper(); + auto resource = mapper->MapFromXml(find_result->second.xml_node); + cache[typeid(T)] = resource; + return resource; + } + + std::shared_ptr GetResourceBrush( + const String& key); + + std::shared_ptr GetResourceFont(const String& key); + + std::shared_ptr GetResourceStyleRuleSet( + const String& key); + + private: + struct ResourceEntry { + CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(ResourceEntry) + CRU_DEFAULT_COPY(ResourceEntry) + CRU_DEFAULT_MOVE(ResourceEntry) + + String name; + xml::XmlElementNode* xml_node; + std::unordered_map cache; + }; + + Event theme_resource_change_event_; + std::unique_ptr theme_resource_xml_root_; + std::unordered_map theme_resource_map_; +}; +} // namespace cru::ui diff --git a/include/cru/ui/ThemeManager.hpp b/include/cru/ui/ThemeManager.hpp deleted file mode 100644 index 68c2b121..00000000 --- a/include/cru/ui/ThemeManager.hpp +++ /dev/null @@ -1,92 +0,0 @@ -#pragma once -#include "Base.hpp" -#include "cru/common/Base.hpp" -#include "cru/common/Event.hpp" -#include "cru/common/Exception.hpp" -#include "cru/platform/graphics/Brush.hpp" -#include "cru/ui/mapper/MapperRegistry.hpp" -#include "cru/ui/style/StyleRuleSet.hpp" -#include "cru/xml/XmlNode.hpp" - -#include -#include -#include -#include - -namespace cru::ui { -class CRU_UI_API ThemeResourceKeyNotExistException : public Exception { - public: - using Exception::Exception; -}; - -class CRU_UI_API BadThemeResourceException : public Exception { - public: - using Exception::Exception; -}; - -class CRU_UI_API ThemeManager : public Object { - public: - static ThemeManager* GetInstance(); - - private: - ThemeManager(); - - public: - CRU_DELETE_COPY(ThemeManager) - CRU_DELETE_MOVE(ThemeManager) - - ~ThemeManager() override; - - IEvent* ThemeResourceChangeEvent() { - return &theme_resource_change_event_; - } - - void ReadResourcesFile(const String& file_path); - - void SetThemeXml(xml::XmlElementNode* root); - - template - T GetResource(const String& key) { - auto find_result = theme_resource_map_.find(key); - if (find_result == theme_resource_map_.cend()) { - throw ThemeResourceKeyNotExistException( - Format(u"Theme resource key \"%s\" not exist.", key)); - } - - auto& cache = find_result->second.cache; - auto cache_find_result = cache.find(typeid(T)); - if (cache_find_result != cache.cend()) { - return std::any_cast(cache_find_result->second); - } - - auto mapper_registry = mapper::MapperRegistry::GetInstance(); - auto mapper = mapper_registry->GetMapper(); - auto resource = mapper->MapFromXml(find_result->second.xml_node); - cache[typeid(T)] = resource; - return resource; - } - - std::shared_ptr GetResourceBrush( - const String& key); - - std::shared_ptr GetResourceFont(const String& key); - - std::shared_ptr GetResourceStyleRuleSet( - const String& key); - - private: - struct ResourceEntry { - CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(ResourceEntry) - CRU_DEFAULT_COPY(ResourceEntry) - CRU_DEFAULT_MOVE(ResourceEntry) - - String name; - xml::XmlElementNode* xml_node; - std::unordered_map cache; - }; - - Event theme_resource_change_event_; - std::unique_ptr theme_resource_xml_root_; - std::unordered_map theme_resource_map_; -}; -} // namespace cru::ui diff --git a/include/cru/ui/components/Component.h b/include/cru/ui/components/Component.h new file mode 100644 index 00000000..0b871dc4 --- /dev/null +++ b/include/cru/ui/components/Component.h @@ -0,0 +1,19 @@ +#pragma once +#include "../Base.h" + +namespace cru::ui::components { +// In destructor, component should check all owned controls whether it is +// attached to window, if not, destroy them, otherwise it is host's duty to +// destroy them. +class CRU_UI_API Component : public Object { + public: + Component() = default; + + CRU_DELETE_COPY(Component) + CRU_DELETE_MOVE(Component) + + ~Component() = default; + + virtual controls::Control* GetRootControl() = 0; +}; +} // namespace cru::ui::components diff --git a/include/cru/ui/components/Component.hpp b/include/cru/ui/components/Component.hpp deleted file mode 100644 index 4cbc3791..00000000 --- a/include/cru/ui/components/Component.hpp +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once -#include "../Base.hpp" - -namespace cru::ui::components { -// In destructor, component should check all owned controls whether it is -// attached to window, if not, destroy them, otherwise it is host's duty to -// destroy them. -class CRU_UI_API Component : public Object { - public: - Component() = default; - - CRU_DELETE_COPY(Component) - CRU_DELETE_MOVE(Component) - - ~Component() = default; - - virtual controls::Control* GetRootControl() = 0; -}; -} // namespace cru::ui::components diff --git a/include/cru/ui/components/Menu.h b/include/cru/ui/components/Menu.h new file mode 100644 index 00000000..32936f84 --- /dev/null +++ b/include/cru/ui/components/Menu.h @@ -0,0 +1,107 @@ +#pragma once +#include "Component.h" +#include "cru/common/Base.h" +#include "cru/ui/controls/Button.h" +#include "cru/ui/controls/Control.h" +#include "cru/ui/controls/FlexLayout.h" +#include "cru/ui/controls/Popup.h" +#include "cru/ui/controls/TextBlock.h" + +#include +#include + +namespace cru::ui::components { +class CRU_UI_API MenuItem : public Component { + public: + MenuItem(); + explicit MenuItem(String text); + + CRU_DELETE_COPY(MenuItem) + CRU_DELETE_MOVE(MenuItem) + + ~MenuItem(); + + public: + controls::Control* GetRootControl() override { return container_; } + + void SetText(String text); + + void SetOnClick(std::function on_click) { + on_click_ = std::move(on_click); + } + + private: + controls::Button* container_; + controls::TextBlock* text_; + std::function on_click_; +}; + +class CRU_UI_API Menu : public Component { + public: + Menu(); + + CRU_DELETE_COPY(Menu) + CRU_DELETE_MOVE(Menu) + + ~Menu(); + + public: + controls::Control* GetRootControl() override { return container_; } + + gsl::index GetItemCount() const { + return static_cast(items_.size()); + } + + void AddItem(Component* component) { AddItem(component, GetItemCount()); } + void AddItem(Component* component, gsl::index index); + Component* RemoveItem(gsl::index index); + void ClearItems(); + + void AddTextItem(String text, std::function on_click) { + AddTextItem(std::move(text), GetItemCount(), std::move(on_click)); + } + void AddTextItem(String text, Index index, std::function on_click); + + void SetOnItemClick(std::function on_item_click) { + on_item_click_ = std::move(on_item_click); + } + + private: + controls::FlexLayout* container_; + std::vector items_; + + std::function on_item_click_; +}; + +class CRU_UI_API PopupMenu : public Component { + public: + explicit PopupMenu(controls::Control* attached_control = nullptr); + + CRU_DELETE_COPY(PopupMenu) + CRU_DELETE_MOVE(PopupMenu) + + ~PopupMenu(); + + public: + controls::Control* GetRootControl() override; + + controls::Popup* GetPopup() { return popup_; } + Menu* GetMenu() { return menu_; } + + // position relative to screen left top. + void SetPosition(const Point& position); + void Show(); + // position relative to screen left top. + void Show(const Point& position) { + SetPosition(position); + Show(); + } + void Close(); + + private: + controls::Control* attached_control_; + + controls::Popup* popup_; + Menu* menu_; +}; +} // namespace cru::ui::components diff --git a/include/cru/ui/components/Menu.hpp b/include/cru/ui/components/Menu.hpp deleted file mode 100644 index 9b60eb02..00000000 --- a/include/cru/ui/components/Menu.hpp +++ /dev/null @@ -1,107 +0,0 @@ -#pragma once -#include "Component.hpp" -#include "cru/common/Base.hpp" -#include "cru/ui/controls/Button.hpp" -#include "cru/ui/controls/Control.hpp" -#include "cru/ui/controls/FlexLayout.hpp" -#include "cru/ui/controls/Popup.hpp" -#include "cru/ui/controls/TextBlock.hpp" - -#include -#include - -namespace cru::ui::components { -class CRU_UI_API MenuItem : public Component { - public: - MenuItem(); - explicit MenuItem(String text); - - CRU_DELETE_COPY(MenuItem) - CRU_DELETE_MOVE(MenuItem) - - ~MenuItem(); - - public: - controls::Control* GetRootControl() override { return container_; } - - void SetText(String text); - - void SetOnClick(std::function on_click) { - on_click_ = std::move(on_click); - } - - private: - controls::Button* container_; - controls::TextBlock* text_; - std::function on_click_; -}; - -class CRU_UI_API Menu : public Component { - public: - Menu(); - - CRU_DELETE_COPY(Menu) - CRU_DELETE_MOVE(Menu) - - ~Menu(); - - public: - controls::Control* GetRootControl() override { return container_; } - - gsl::index GetItemCount() const { - return static_cast(items_.size()); - } - - void AddItem(Component* component) { AddItem(component, GetItemCount()); } - void AddItem(Component* component, gsl::index index); - Component* RemoveItem(gsl::index index); - void ClearItems(); - - void AddTextItem(String text, std::function on_click) { - AddTextItem(std::move(text), GetItemCount(), std::move(on_click)); - } - void AddTextItem(String text, Index index, std::function on_click); - - void SetOnItemClick(std::function on_item_click) { - on_item_click_ = std::move(on_item_click); - } - - private: - controls::FlexLayout* container_; - std::vector items_; - - std::function on_item_click_; -}; - -class CRU_UI_API PopupMenu : public Component { - public: - explicit PopupMenu(controls::Control* attached_control = nullptr); - - CRU_DELETE_COPY(PopupMenu) - CRU_DELETE_MOVE(PopupMenu) - - ~PopupMenu(); - - public: - controls::Control* GetRootControl() override; - - controls::Popup* GetPopup() { return popup_; } - Menu* GetMenu() { return menu_; } - - // position relative to screen left top. - void SetPosition(const Point& position); - void Show(); - // position relative to screen left top. - void Show(const Point& position) { - SetPosition(position); - Show(); - } - void Close(); - - private: - controls::Control* attached_control_; - - controls::Popup* popup_; - Menu* menu_; -}; -} // namespace cru::ui::components diff --git a/include/cru/ui/controls/Base.h b/include/cru/ui/controls/Base.h new file mode 100644 index 00000000..21f1c563 --- /dev/null +++ b/include/cru/ui/controls/Base.h @@ -0,0 +1,4 @@ +#pragma once +#include "../Base.h" + +namespace cru::ui::controls {} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/Base.hpp b/include/cru/ui/controls/Base.hpp deleted file mode 100644 index 7c85cdb2..00000000 --- a/include/cru/ui/controls/Base.hpp +++ /dev/null @@ -1,4 +0,0 @@ -#pragma once -#include "../Base.hpp" - -namespace cru::ui::controls {} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/Button.h b/include/cru/ui/controls/Button.h new file mode 100644 index 00000000..ab1ef8eb --- /dev/null +++ b/include/cru/ui/controls/Button.h @@ -0,0 +1,53 @@ +#pragma once +#include "ContentControl.h" + +#include "../helper/ClickDetector.h" +#include "IBorderControl.h" +#include "IClickableControl.h" +#include "cru/common/Event.h" +#include "cru/ui/style/ApplyBorderStyleInfo.h" + +namespace cru::ui::controls { +class CRU_UI_API Button : public ContentControl, + public virtual IClickableControl, + public virtual IBorderControl { + public: + static constexpr StringView control_type = u"Button"; + + static Button* Create() { return new Button(); } + + protected: + Button(); + + public: + Button(const Button& other) = delete; + Button(Button&& other) = delete; + Button& operator=(const Button& other) = delete; + Button& operator=(Button&& other) = delete; + ~Button() override; + + String GetControlType() const final { return control_type.ToString(); } + + render::RenderObject* GetRenderObject() const override; + + public: + helper::ClickState GetClickState() override { + return click_detector_.GetState(); + } + + IEvent* ClickStateChangeEvent() override { + return click_detector_.StateChangeEvent(); + } + + IEvent* ClickEvent() { + return click_detector_.ClickEvent(); + } + + void ApplyBorderStyle(const style::ApplyBorderStyleInfo& style) override; + + private: + std::unique_ptr render_object_{}; + + helper::ClickDetector click_detector_; +}; +} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/Button.hpp b/include/cru/ui/controls/Button.hpp deleted file mode 100644 index 77e0989c..00000000 --- a/include/cru/ui/controls/Button.hpp +++ /dev/null @@ -1,53 +0,0 @@ -#pragma once -#include "ContentControl.hpp" - -#include "../helper/ClickDetector.hpp" -#include "IBorderControl.hpp" -#include "IClickableControl.hpp" -#include "cru/common/Event.hpp" -#include "cru/ui/style/ApplyBorderStyleInfo.hpp" - -namespace cru::ui::controls { -class CRU_UI_API Button : public ContentControl, - public virtual IClickableControl, - public virtual IBorderControl { - public: - static constexpr StringView control_type = u"Button"; - - static Button* Create() { return new Button(); } - - protected: - Button(); - - public: - Button(const Button& other) = delete; - Button(Button&& other) = delete; - Button& operator=(const Button& other) = delete; - Button& operator=(Button&& other) = delete; - ~Button() override; - - String GetControlType() const final { return control_type.ToString(); } - - render::RenderObject* GetRenderObject() const override; - - public: - helper::ClickState GetClickState() override { - return click_detector_.GetState(); - } - - IEvent* ClickStateChangeEvent() override { - return click_detector_.StateChangeEvent(); - } - - IEvent* ClickEvent() { - return click_detector_.ClickEvent(); - } - - void ApplyBorderStyle(const style::ApplyBorderStyleInfo& style) override; - - private: - std::unique_ptr render_object_{}; - - helper::ClickDetector click_detector_; -}; -} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/Container.h b/include/cru/ui/controls/Container.h new file mode 100644 index 00000000..f250807d --- /dev/null +++ b/include/cru/ui/controls/Container.h @@ -0,0 +1,25 @@ +#pragma once +#include "ContentControl.h" + +namespace cru::ui::controls { +class CRU_UI_API Container : public ContentControl { + static constexpr StringView control_type = u"Container"; + + protected: + Container(); + + public: + CRU_DELETE_COPY(Container) + CRU_DELETE_MOVE(Container) + + ~Container() override; + + public: + String GetControlType() const final { return control_type.ToString(); } + + render::RenderObject* GetRenderObject() const override; + + private: + std::unique_ptr render_object_; +}; +} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/Container.hpp b/include/cru/ui/controls/Container.hpp deleted file mode 100644 index 4385c23f..00000000 --- a/include/cru/ui/controls/Container.hpp +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once -#include "ContentControl.hpp" - -namespace cru::ui::controls { -class CRU_UI_API Container : public ContentControl { - static constexpr StringView control_type = u"Container"; - - protected: - Container(); - - public: - CRU_DELETE_COPY(Container) - CRU_DELETE_MOVE(Container) - - ~Container() override; - - public: - String GetControlType() const final { return control_type.ToString(); } - - render::RenderObject* GetRenderObject() const override; - - private: - std::unique_ptr render_object_; -}; -} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/ContentControl.h b/include/cru/ui/controls/ContentControl.h new file mode 100644 index 00000000..86e90dae --- /dev/null +++ b/include/cru/ui/controls/ContentControl.h @@ -0,0 +1,38 @@ +#pragma once +#include "Control.h" + +#include "cru/ui/render/RenderObject.h" + +namespace cru::ui::controls { +class CRU_UI_API ContentControl : public Control { + protected: + ContentControl() = default; + + public: + ContentControl(const ContentControl& other) = delete; + ContentControl(ContentControl&& other) = delete; + ContentControl& operator=(const ContentControl& other) = delete; + ContentControl& operator=(ContentControl&& other) = delete; + ~ContentControl() override = default; + + Control* GetChild() const; + void SetChild(Control* child); + + protected: + virtual void OnChildChanged(Control* old_child, Control* new_child); + + render::RenderObject* GetContainerRenderObject() const { + return container_render_object_; + } + void SetContainerRenderObject(render::RenderObject* ro) { + container_render_object_ = ro; + } + + private: + using Control::AddChild; + using Control::RemoveChild; + + private: + render::RenderObject* container_render_object_ = nullptr; +}; +} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/ContentControl.hpp b/include/cru/ui/controls/ContentControl.hpp deleted file mode 100644 index 7b40de64..00000000 --- a/include/cru/ui/controls/ContentControl.hpp +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once -#include "Control.hpp" - -#include "cru/ui/render/RenderObject.hpp" - -namespace cru::ui::controls { -class CRU_UI_API ContentControl : public Control { - protected: - ContentControl() = default; - - public: - ContentControl(const ContentControl& other) = delete; - ContentControl(ContentControl&& other) = delete; - ContentControl& operator=(const ContentControl& other) = delete; - ContentControl& operator=(ContentControl&& other) = delete; - ~ContentControl() override = default; - - Control* GetChild() const; - void SetChild(Control* child); - - protected: - virtual void OnChildChanged(Control* old_child, Control* new_child); - - render::RenderObject* GetContainerRenderObject() const { - return container_render_object_; - } - void SetContainerRenderObject(render::RenderObject* ro) { - container_render_object_ = ro; - } - - private: - using Control::AddChild; - using Control::RemoveChild; - - private: - render::RenderObject* container_render_object_ = nullptr; -}; -} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/Control.h b/include/cru/ui/controls/Control.h new file mode 100644 index 00000000..c00e3a65 --- /dev/null +++ b/include/cru/ui/controls/Control.h @@ -0,0 +1,157 @@ +#pragma once +#include "Base.h" + +#include "../events/UiEvents.h" +#include "../render/Base.h" +#include "cru/common/Event.h" + +#include + +namespace cru::ui::controls { +class CRU_UI_API Control : public Object { + friend host::WindowHost; + + protected: + Control(); + + public: + Control(const Control& other) = delete; + Control(Control&& other) = delete; + Control& operator=(const Control& other) = delete; + Control& operator=(Control&& other) = delete; + ~Control() override; + + public: + virtual String GetControlType() const = 0; + + //*************** region: tree *************** + public: + host::WindowHost* GetWindowHost() const; + + Control* GetParent() const { return parent_; } + + const std::vector& GetChildren() const { return children_; } + + // Traverse the tree rooted the control including itself. + void TraverseDescendants(const std::function& predicate); + + public: + virtual render::RenderObject* GetRenderObject() const = 0; + + //*************** region: focus *************** + public: + bool HasFocus(); + + void SetFocus(); + + //*************** region: mouse *************** + public: + bool IsMouseOver() const { return is_mouse_over_; } + + bool CaptureMouse(); + + bool ReleaseMouse(); + + bool IsMouseCaptured(); + + //*************** region: cursor *************** + // Cursor is inherited from parent recursively if not set. + public: + // null for not set + std::shared_ptr GetCursor(); + + // will not return nullptr + std::shared_ptr GetInheritedCursor(); + + // null to unset + void SetCursor(std::shared_ptr cursor); + + public: + std::shared_ptr GetStyleRuleSet(); + + //*************** region: events *************** + public: + // Raised when mouse enter the control. Even when the control itself captures + // the mouse, this event is raised as regular. But if mouse is captured by + // another control, the control will not receive any mouse enter event. You + // can use `IsMouseCaptured` to get more info. + events::RoutedEvent* MouseEnterEvent() { + return &mouse_enter_event_; + } + // Raised when mouse is leave the control. Even when the control itself + // captures the mouse, this event is raised as regular. But if mouse is + // captured by another control, the control will not receive any mouse leave + // event. You can use `IsMouseCaptured` to get more info. + events::RoutedEvent* MouseLeaveEvent() { + return &mouse_leave_event_; + } + // Raised when mouse is move in the control. + events::RoutedEvent* MouseMoveEvent() { + return &mouse_move_event_; + } + // Raised when a mouse button is pressed in the control. + events::RoutedEvent* MouseDownEvent() { + return &mouse_down_event_; + } + // Raised when a mouse button is released in the control. + events::RoutedEvent* MouseUpEvent() { + return &mouse_up_event_; + } + events::RoutedEvent* MouseWheelEvent() { + return &mouse_wheel_event_; + } + events::RoutedEvent* KeyDownEvent() { + return &key_down_event_; + } + events::RoutedEvent* KeyUpEvent() { + return &key_up_event_; + } + events::RoutedEvent* GainFocusEvent() { + return &gain_focus_event_; + } + events::RoutedEvent* LoseFocusEvent() { + return &lose_focus_event_; + } + + private: + events::RoutedEvent mouse_enter_event_; + events::RoutedEvent mouse_leave_event_; + events::RoutedEvent mouse_move_event_; + events::RoutedEvent mouse_down_event_; + events::RoutedEvent mouse_up_event_; + events::RoutedEvent mouse_wheel_event_; + + events::RoutedEvent key_down_event_; + events::RoutedEvent key_up_event_; + + events::RoutedEvent gain_focus_event_; + events::RoutedEvent lose_focus_event_; + + //*************** region: tree *************** + protected: + void AddChild(Control* control, Index position); + void RemoveChild(Index position); + virtual void OnAddChild(Control* child, Index position); + virtual void OnRemoveChild(Control* child, Index position); + virtual void OnParentChanged(Control* old_parent, Control* new_parent); + virtual void OnAttachToHost(host::WindowHost* host); + virtual void OnDetachFromHost(host::WindowHost* host); + + protected: + virtual void OnMouseHoverChange(bool newHover) { CRU_UNUSED(newHover) } + + private: + Control* parent_ = nullptr; + std::vector children_; + + host::WindowHost* window_host_ = nullptr; + + private: + bool is_mouse_over_ = false; + + std::shared_ptr cursor_ = nullptr; + + std::shared_ptr style_rule_set_; + std::unique_ptr style_rule_set_bind_; +}; +} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/Control.hpp b/include/cru/ui/controls/Control.hpp deleted file mode 100644 index c51643be..00000000 --- a/include/cru/ui/controls/Control.hpp +++ /dev/null @@ -1,157 +0,0 @@ -#pragma once -#include "Base.hpp" - -#include "../events/UiEvents.hpp" -#include "../render/Base.hpp" -#include "cru/common/Event.hpp" - -#include - -namespace cru::ui::controls { -class CRU_UI_API Control : public Object { - friend host::WindowHost; - - protected: - Control(); - - public: - Control(const Control& other) = delete; - Control(Control&& other) = delete; - Control& operator=(const Control& other) = delete; - Control& operator=(Control&& other) = delete; - ~Control() override; - - public: - virtual String GetControlType() const = 0; - - //*************** region: tree *************** - public: - host::WindowHost* GetWindowHost() const; - - Control* GetParent() const { return parent_; } - - const std::vector& GetChildren() const { return children_; } - - // Traverse the tree rooted the control including itself. - void TraverseDescendants(const std::function& predicate); - - public: - virtual render::RenderObject* GetRenderObject() const = 0; - - //*************** region: focus *************** - public: - bool HasFocus(); - - void SetFocus(); - - //*************** region: mouse *************** - public: - bool IsMouseOver() const { return is_mouse_over_; } - - bool CaptureMouse(); - - bool ReleaseMouse(); - - bool IsMouseCaptured(); - - //*************** region: cursor *************** - // Cursor is inherited from parent recursively if not set. - public: - // null for not set - std::shared_ptr GetCursor(); - - // will not return nullptr - std::shared_ptr GetInheritedCursor(); - - // null to unset - void SetCursor(std::shared_ptr cursor); - - public: - std::shared_ptr GetStyleRuleSet(); - - //*************** region: events *************** - public: - // Raised when mouse enter the control. Even when the control itself captures - // the mouse, this event is raised as regular. But if mouse is captured by - // another control, the control will not receive any mouse enter event. You - // can use `IsMouseCaptured` to get more info. - events::RoutedEvent* MouseEnterEvent() { - return &mouse_enter_event_; - } - // Raised when mouse is leave the control. Even when the control itself - // captures the mouse, this event is raised as regular. But if mouse is - // captured by another control, the control will not receive any mouse leave - // event. You can use `IsMouseCaptured` to get more info. - events::RoutedEvent* MouseLeaveEvent() { - return &mouse_leave_event_; - } - // Raised when mouse is move in the control. - events::RoutedEvent* MouseMoveEvent() { - return &mouse_move_event_; - } - // Raised when a mouse button is pressed in the control. - events::RoutedEvent* MouseDownEvent() { - return &mouse_down_event_; - } - // Raised when a mouse button is released in the control. - events::RoutedEvent* MouseUpEvent() { - return &mouse_up_event_; - } - events::RoutedEvent* MouseWheelEvent() { - return &mouse_wheel_event_; - } - events::RoutedEvent* KeyDownEvent() { - return &key_down_event_; - } - events::RoutedEvent* KeyUpEvent() { - return &key_up_event_; - } - events::RoutedEvent* GainFocusEvent() { - return &gain_focus_event_; - } - events::RoutedEvent* LoseFocusEvent() { - return &lose_focus_event_; - } - - private: - events::RoutedEvent mouse_enter_event_; - events::RoutedEvent mouse_leave_event_; - events::RoutedEvent mouse_move_event_; - events::RoutedEvent mouse_down_event_; - events::RoutedEvent mouse_up_event_; - events::RoutedEvent mouse_wheel_event_; - - events::RoutedEvent key_down_event_; - events::RoutedEvent key_up_event_; - - events::RoutedEvent gain_focus_event_; - events::RoutedEvent lose_focus_event_; - - //*************** region: tree *************** - protected: - void AddChild(Control* control, Index position); - void RemoveChild(Index position); - virtual void OnAddChild(Control* child, Index position); - virtual void OnRemoveChild(Control* child, Index position); - virtual void OnParentChanged(Control* old_parent, Control* new_parent); - virtual void OnAttachToHost(host::WindowHost* host); - virtual void OnDetachFromHost(host::WindowHost* host); - - protected: - virtual void OnMouseHoverChange(bool newHover) { CRU_UNUSED(newHover) } - - private: - Control* parent_ = nullptr; - std::vector children_; - - host::WindowHost* window_host_ = nullptr; - - private: - bool is_mouse_over_ = false; - - std::shared_ptr cursor_ = nullptr; - - std::shared_ptr style_rule_set_; - std::unique_ptr style_rule_set_bind_; -}; -} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/FlexLayout.h b/include/cru/ui/controls/FlexLayout.h new file mode 100644 index 00000000..1255b119 --- /dev/null +++ b/include/cru/ui/controls/FlexLayout.h @@ -0,0 +1,40 @@ +#pragma once +#include "LayoutControl.h" + +namespace cru::ui::controls { +class CRU_UI_API FlexLayout : public LayoutControl { + public: + static constexpr StringView control_type = u"FlexLayout"; + + static FlexLayout* Create() { return new FlexLayout(); } + + protected: + FlexLayout(); + + public: + FlexLayout(const FlexLayout& other) = delete; + FlexLayout(FlexLayout&& other) = delete; + FlexLayout& operator=(const FlexLayout& other) = delete; + FlexLayout& operator=(FlexLayout&& other) = delete; + ~FlexLayout() override; + + String GetControlType() const final { return control_type.ToString(); } + + render::RenderObject* GetRenderObject() const override; + + FlexMainAlignment GetContentMainAlign() const; + void SetContentMainAlign(FlexMainAlignment value); + + FlexDirection GetFlexDirection() const; + void SetFlexDirection(FlexDirection direction); + + FlexCrossAlignment GetItemCrossAlign() const; + void SetItemCrossAlign(FlexCrossAlignment alignment); + + FlexChildLayoutData GetChildLayoutData(Control* control); + void SetChildLayoutData(Control* control, FlexChildLayoutData data); + + private: + std::shared_ptr render_object_; +}; +} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/FlexLayout.hpp b/include/cru/ui/controls/FlexLayout.hpp deleted file mode 100644 index 7724c4c6..00000000 --- a/include/cru/ui/controls/FlexLayout.hpp +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once -#include "LayoutControl.hpp" - -namespace cru::ui::controls { -class CRU_UI_API FlexLayout : public LayoutControl { - public: - static constexpr StringView control_type = u"FlexLayout"; - - static FlexLayout* Create() { return new FlexLayout(); } - - protected: - FlexLayout(); - - public: - FlexLayout(const FlexLayout& other) = delete; - FlexLayout(FlexLayout&& other) = delete; - FlexLayout& operator=(const FlexLayout& other) = delete; - FlexLayout& operator=(FlexLayout&& other) = delete; - ~FlexLayout() override; - - String GetControlType() const final { return control_type.ToString(); } - - render::RenderObject* GetRenderObject() const override; - - FlexMainAlignment GetContentMainAlign() const; - void SetContentMainAlign(FlexMainAlignment value); - - FlexDirection GetFlexDirection() const; - void SetFlexDirection(FlexDirection direction); - - FlexCrossAlignment GetItemCrossAlign() const; - void SetItemCrossAlign(FlexCrossAlignment alignment); - - FlexChildLayoutData GetChildLayoutData(Control* control); - void SetChildLayoutData(Control* control, FlexChildLayoutData data); - - private: - std::shared_ptr render_object_; -}; -} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/IBorderControl.h b/include/cru/ui/controls/IBorderControl.h new file mode 100644 index 00000000..3178d7f2 --- /dev/null +++ b/include/cru/ui/controls/IBorderControl.h @@ -0,0 +1,10 @@ +#pragma once +#include "../style/ApplyBorderStyleInfo.h" +#include "Base.h" +#include "cru/common/Base.h" + +namespace cru::ui::controls { +struct CRU_UI_API IBorderControl : virtual Interface { + virtual void ApplyBorderStyle(const style::ApplyBorderStyleInfo& style) = 0; +}; +} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/IBorderControl.hpp b/include/cru/ui/controls/IBorderControl.hpp deleted file mode 100644 index a8bc0b51..00000000 --- a/include/cru/ui/controls/IBorderControl.hpp +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once -#include "../style/ApplyBorderStyleInfo.hpp" -#include "Base.hpp" -#include "cru/common/Base.hpp" - -namespace cru::ui::controls { -struct CRU_UI_API IBorderControl : virtual Interface { - virtual void ApplyBorderStyle(const style::ApplyBorderStyleInfo& style) = 0; -}; -} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/IClickableControl.h b/include/cru/ui/controls/IClickableControl.h new file mode 100644 index 00000000..48b3ad5f --- /dev/null +++ b/include/cru/ui/controls/IClickableControl.h @@ -0,0 +1,12 @@ +#pragma once +#include "Base.h" + +#include "cru/common/Event.h" +#include "cru/ui/helper/ClickDetector.h" + +namespace cru::ui::controls { +struct CRU_UI_API IClickableControl : virtual Interface { + virtual helper::ClickState GetClickState() = 0; + virtual IEvent* ClickStateChangeEvent() = 0; +}; +} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/IClickableControl.hpp b/include/cru/ui/controls/IClickableControl.hpp deleted file mode 100644 index af9ae100..00000000 --- a/include/cru/ui/controls/IClickableControl.hpp +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once -#include "Base.hpp" - -#include "cru/common/Event.hpp" -#include "cru/ui/helper/ClickDetector.hpp" - -namespace cru::ui::controls { -struct CRU_UI_API IClickableControl : virtual Interface { - virtual helper::ClickState GetClickState() = 0; - virtual IEvent* ClickStateChangeEvent() = 0; -}; -} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/LayoutControl.h b/include/cru/ui/controls/LayoutControl.h new file mode 100644 index 00000000..639ecc74 --- /dev/null +++ b/include/cru/ui/controls/LayoutControl.h @@ -0,0 +1,37 @@ +#pragma once +#include "Control.h" + +namespace cru::ui::controls { +class CRU_UI_API LayoutControl : public Control { + protected: + LayoutControl() = default; + explicit LayoutControl(render::RenderObject* container_render_object) + : container_render_object_(container_render_object) {} + + public: + LayoutControl(const LayoutControl& other) = delete; + LayoutControl(LayoutControl&& other) = delete; + LayoutControl& operator=(const LayoutControl& other) = delete; + LayoutControl& operator=(LayoutControl&& other) = delete; + ~LayoutControl() override = default; + + using Control::AddChild; + using Control::RemoveChild; + + void ClearChildren(); + + protected: + // If container render object is not null. Render object of added or removed + // child control will automatically sync to the container render object. + render::RenderObject* GetContainerRenderObject() const; + void SetContainerRenderObject(render::RenderObject* ro) { + container_render_object_ = ro; + } + + void OnAddChild(Control* child, Index position) override; + void OnRemoveChild(Control* child, Index position) override; + + private: + render::RenderObject* container_render_object_ = nullptr; +}; +} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/LayoutControl.hpp b/include/cru/ui/controls/LayoutControl.hpp deleted file mode 100644 index 3999e9bc..00000000 --- a/include/cru/ui/controls/LayoutControl.hpp +++ /dev/null @@ -1,37 +0,0 @@ -#pragma once -#include "Control.hpp" - -namespace cru::ui::controls { -class CRU_UI_API LayoutControl : public Control { - protected: - LayoutControl() = default; - explicit LayoutControl(render::RenderObject* container_render_object) - : container_render_object_(container_render_object) {} - - public: - LayoutControl(const LayoutControl& other) = delete; - LayoutControl(LayoutControl&& other) = delete; - LayoutControl& operator=(const LayoutControl& other) = delete; - LayoutControl& operator=(LayoutControl&& other) = delete; - ~LayoutControl() override = default; - - using Control::AddChild; - using Control::RemoveChild; - - void ClearChildren(); - - protected: - // If container render object is not null. Render object of added or removed - // child control will automatically sync to the container render object. - render::RenderObject* GetContainerRenderObject() const; - void SetContainerRenderObject(render::RenderObject* ro) { - container_render_object_ = ro; - } - - void OnAddChild(Control* child, Index position) override; - void OnRemoveChild(Control* child, Index position) override; - - private: - render::RenderObject* container_render_object_ = nullptr; -}; -} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/NoChildControl.h b/include/cru/ui/controls/NoChildControl.h new file mode 100644 index 00000000..6d2168cb --- /dev/null +++ b/include/cru/ui/controls/NoChildControl.h @@ -0,0 +1,20 @@ +#pragma once +#include "Control.h" + +namespace cru::ui::controls { +class CRU_UI_API NoChildControl : public Control { + protected: + NoChildControl() = default; + + public: + NoChildControl(const NoChildControl& other) = delete; + NoChildControl(NoChildControl&& other) = delete; + NoChildControl& operator=(const NoChildControl& other) = delete; + NoChildControl& operator=(NoChildControl&& other) = delete; + ~NoChildControl() override = default; + + private: + using Control::AddChild; + using Control::RemoveChild; +}; +} // namespace cru::ui diff --git a/include/cru/ui/controls/NoChildControl.hpp b/include/cru/ui/controls/NoChildControl.hpp deleted file mode 100644 index 0c432532..00000000 --- a/include/cru/ui/controls/NoChildControl.hpp +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once -#include "Control.hpp" - -namespace cru::ui::controls { -class CRU_UI_API NoChildControl : public Control { - protected: - NoChildControl() = default; - - public: - NoChildControl(const NoChildControl& other) = delete; - NoChildControl(NoChildControl&& other) = delete; - NoChildControl& operator=(const NoChildControl& other) = delete; - NoChildControl& operator=(NoChildControl&& other) = delete; - ~NoChildControl() override = default; - - private: - using Control::AddChild; - using Control::RemoveChild; -}; -} // namespace cru::ui diff --git a/include/cru/ui/controls/Popup.h b/include/cru/ui/controls/Popup.h new file mode 100644 index 00000000..271ad4b7 --- /dev/null +++ b/include/cru/ui/controls/Popup.h @@ -0,0 +1,26 @@ +#pragma once +#include "RootControl.h" + +#include "cru/platform/gui/Base.h" + +#include + +namespace cru::ui::controls { +class CRU_UI_API Popup : public RootControl { + public: + static constexpr StringView kControlType = u"Popup"; + + static Popup* Create(Control* attached_control = nullptr) { + return new Popup(attached_control); + } + + explicit Popup(Control* attached_control = nullptr); + + CRU_DELETE_COPY(Popup) + CRU_DELETE_MOVE(Popup) + + ~Popup() override; + + String GetControlType() const override { return kControlType.ToString(); } +}; +} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/Popup.hpp b/include/cru/ui/controls/Popup.hpp deleted file mode 100644 index 4076e45b..00000000 --- a/include/cru/ui/controls/Popup.hpp +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once -#include "RootControl.hpp" - -#include "cru/platform/gui/Base.hpp" - -#include - -namespace cru::ui::controls { -class CRU_UI_API Popup : public RootControl { - public: - static constexpr StringView kControlType = u"Popup"; - - static Popup* Create(Control* attached_control = nullptr) { - return new Popup(attached_control); - } - - explicit Popup(Control* attached_control = nullptr); - - CRU_DELETE_COPY(Popup) - CRU_DELETE_MOVE(Popup) - - ~Popup() override; - - String GetControlType() const override { return kControlType.ToString(); } -}; -} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/RootControl.h b/include/cru/ui/controls/RootControl.h new file mode 100644 index 00000000..c7ed916a --- /dev/null +++ b/include/cru/ui/controls/RootControl.h @@ -0,0 +1,39 @@ +#pragma once +#include "LayoutControl.h" + +#include "cru/common/Base.h" +#include "cru/common/Event.h" +#include "cru/platform/gui/Base.h" +#include "cru/platform/gui/Window.h" +#include "cru/ui/Base.h" +#include "cru/ui/host/WindowHost.h" + +namespace cru::ui::controls { +class CRU_UI_API RootControl : public LayoutControl { + protected: + explicit RootControl(Control* attached_control); + + public: + CRU_DELETE_COPY(RootControl) + CRU_DELETE_MOVE(RootControl) + ~RootControl() override; + + public: + render::RenderObject* GetRenderObject() const override; + + platform::gui::INativeWindow* GetNativeWindow(); + + protected: + void SetGainFocusOnCreateAndDestroyWhenLoseFocus(bool value); + + private: + std::unique_ptr window_host_; + + std::unique_ptr render_object_; + + Control* attached_control_; + + EventRevokerListGuard + gain_focus_on_create_and_destroy_when_lose_focus_event_guard_; +}; +} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/RootControl.hpp b/include/cru/ui/controls/RootControl.hpp deleted file mode 100644 index f70f2eeb..00000000 --- a/include/cru/ui/controls/RootControl.hpp +++ /dev/null @@ -1,39 +0,0 @@ -#pragma once -#include "LayoutControl.hpp" - -#include "cru/common/Base.hpp" -#include "cru/common/Event.hpp" -#include "cru/platform/gui/Base.hpp" -#include "cru/platform/gui/Window.hpp" -#include "cru/ui/Base.hpp" -#include "cru/ui/host/WindowHost.hpp" - -namespace cru::ui::controls { -class CRU_UI_API RootControl : public LayoutControl { - protected: - explicit RootControl(Control* attached_control); - - public: - CRU_DELETE_COPY(RootControl) - CRU_DELETE_MOVE(RootControl) - ~RootControl() override; - - public: - render::RenderObject* GetRenderObject() const override; - - platform::gui::INativeWindow* GetNativeWindow(); - - protected: - void SetGainFocusOnCreateAndDestroyWhenLoseFocus(bool value); - - private: - std::unique_ptr window_host_; - - std::unique_ptr render_object_; - - Control* attached_control_; - - EventRevokerListGuard - gain_focus_on_create_and_destroy_when_lose_focus_event_guard_; -}; -} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/ScrollView.h b/include/cru/ui/controls/ScrollView.h new file mode 100644 index 00000000..46c23691 --- /dev/null +++ b/include/cru/ui/controls/ScrollView.h @@ -0,0 +1,32 @@ +#pragma once +#include "ContentControl.h" +#include "cru/common/Base.h" +#include "cru/ui/render/RenderObject.h" +#include "cru/ui/render/ScrollRenderObject.h" + +#include +#include + +namespace cru::ui::controls { +class CRU_UI_API ScrollView : public ContentControl { + public: + static ScrollView* Create() { return new ScrollView(); } + + static constexpr StringView control_type = u"ScrollView"; + + ScrollView(); + + CRU_DELETE_COPY(ScrollView) + CRU_DELETE_MOVE(ScrollView) + + ~ScrollView() override = default; + + public: + String GetControlType() const override { return control_type.ToString(); } + + render::RenderObject* GetRenderObject() const override; + + private: + std::unique_ptr scroll_render_object_; +}; +} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/ScrollView.hpp b/include/cru/ui/controls/ScrollView.hpp deleted file mode 100644 index 18a9bf97..00000000 --- a/include/cru/ui/controls/ScrollView.hpp +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once -#include "ContentControl.hpp" -#include "cru/common/Base.hpp" -#include "cru/ui/render/RenderObject.hpp" -#include "cru/ui/render/ScrollRenderObject.hpp" - -#include -#include - -namespace cru::ui::controls { -class CRU_UI_API ScrollView : public ContentControl { - public: - static ScrollView* Create() { return new ScrollView(); } - - static constexpr StringView control_type = u"ScrollView"; - - ScrollView(); - - CRU_DELETE_COPY(ScrollView) - CRU_DELETE_MOVE(ScrollView) - - ~ScrollView() override = default; - - public: - String GetControlType() const override { return control_type.ToString(); } - - render::RenderObject* GetRenderObject() const override; - - private: - std::unique_ptr scroll_render_object_; -}; -} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/StackLayout.h b/include/cru/ui/controls/StackLayout.h new file mode 100644 index 00000000..36e8ab43 --- /dev/null +++ b/include/cru/ui/controls/StackLayout.h @@ -0,0 +1,31 @@ +#pragma once +#include "LayoutControl.h" +#include "cru/ui/Base.h" + +namespace cru::ui::controls { +class CRU_UI_API StackLayout : public LayoutControl { + public: + static constexpr StringView control_type = u"StackLayout"; + + static StackLayout* Create() { return new StackLayout(); } + + protected: + StackLayout(); + + public: + CRU_DELETE_COPY(StackLayout) + CRU_DELETE_MOVE(StackLayout) + + ~StackLayout() override; + + String GetControlType() const final { return control_type.ToString(); } + + render::RenderObject* GetRenderObject() const override; + + const StackChildLayoutData& GetChildLayoutData(Index position); + void SetChildLayoutData(Index position, StackChildLayoutData data); + + private: + std::shared_ptr render_object_; +}; +} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/StackLayout.hpp b/include/cru/ui/controls/StackLayout.hpp deleted file mode 100644 index 5d90dfda..00000000 --- a/include/cru/ui/controls/StackLayout.hpp +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once -#include "LayoutControl.hpp" -#include "cru/ui/Base.hpp" - -namespace cru::ui::controls { -class CRU_UI_API StackLayout : public LayoutControl { - public: - static constexpr StringView control_type = u"StackLayout"; - - static StackLayout* Create() { return new StackLayout(); } - - protected: - StackLayout(); - - public: - CRU_DELETE_COPY(StackLayout) - CRU_DELETE_MOVE(StackLayout) - - ~StackLayout() override; - - String GetControlType() const final { return control_type.ToString(); } - - render::RenderObject* GetRenderObject() const override; - - const StackChildLayoutData& GetChildLayoutData(Index position); - void SetChildLayoutData(Index position, StackChildLayoutData data); - - private: - std::shared_ptr render_object_; -}; -} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/TextBlock.h b/include/cru/ui/controls/TextBlock.h new file mode 100644 index 00000000..5e5674a2 --- /dev/null +++ b/include/cru/ui/controls/TextBlock.h @@ -0,0 +1,44 @@ +#pragma once +#include "NoChildControl.h" + +#include "TextHostControlService.h" + +namespace cru::ui::controls { +class CRU_UI_API TextBlock : public NoChildControl, public virtual ITextHostControl { + public: + static constexpr StringView control_type = u"TextBlock"; + + static TextBlock* Create(); + static TextBlock* Create(String text, bool selectable = false); + + protected: + TextBlock(); + + public: + TextBlock(const TextBlock& other) = delete; + TextBlock(TextBlock&& other) = delete; + TextBlock& operator=(const TextBlock& other) = delete; + TextBlock& operator=(TextBlock&& other) = delete; + ~TextBlock() override; + + String GetControlType() const final { return control_type.ToString(); } + + render::RenderObject* GetRenderObject() const override; + + String GetText() const; + void SetText(String text); + + bool IsSelectable() const; + void SetSelectable(bool value); + + gsl::not_null GetTextRenderObject() override; + render::ScrollRenderObject* GetScrollRenderObject() override { + return nullptr; + } + + private: + std::unique_ptr text_render_object_; + + std::unique_ptr service_; +}; +} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/TextBlock.hpp b/include/cru/ui/controls/TextBlock.hpp deleted file mode 100644 index 68e9ced1..00000000 --- a/include/cru/ui/controls/TextBlock.hpp +++ /dev/null @@ -1,44 +0,0 @@ -#pragma once -#include "NoChildControl.hpp" - -#include "TextHostControlService.hpp" - -namespace cru::ui::controls { -class CRU_UI_API TextBlock : public NoChildControl, public virtual ITextHostControl { - public: - static constexpr StringView control_type = u"TextBlock"; - - static TextBlock* Create(); - static TextBlock* Create(String text, bool selectable = false); - - protected: - TextBlock(); - - public: - TextBlock(const TextBlock& other) = delete; - TextBlock(TextBlock&& other) = delete; - TextBlock& operator=(const TextBlock& other) = delete; - TextBlock& operator=(TextBlock&& other) = delete; - ~TextBlock() override; - - String GetControlType() const final { return control_type.ToString(); } - - render::RenderObject* GetRenderObject() const override; - - String GetText() const; - void SetText(String text); - - bool IsSelectable() const; - void SetSelectable(bool value); - - gsl::not_null GetTextRenderObject() override; - render::ScrollRenderObject* GetScrollRenderObject() override { - return nullptr; - } - - private: - std::unique_ptr text_render_object_; - - std::unique_ptr service_; -}; -} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/TextBox.h b/include/cru/ui/controls/TextBox.h new file mode 100644 index 00000000..2ed7db5e --- /dev/null +++ b/include/cru/ui/controls/TextBox.h @@ -0,0 +1,46 @@ +#pragma once +#include "NoChildControl.h" + +#include "IBorderControl.h" +#include "TextHostControlService.h" + +#include + +namespace cru::ui::controls { +class CRU_UI_API TextBox : public NoChildControl, + public virtual IBorderControl, + public virtual ITextHostControl { + public: + static constexpr StringView control_type = u"TextBox"; + + static TextBox* Create() { return new TextBox(); } + + protected: + TextBox(); + + public: + CRU_DELETE_COPY(TextBox) + CRU_DELETE_MOVE(TextBox) + + ~TextBox() override; + + String GetControlType() const final { return control_type.ToString(); } + + render::RenderObject* GetRenderObject() const override; + + gsl::not_null GetTextRenderObject() override; + render::ScrollRenderObject* GetScrollRenderObject() override; + + bool GetMultiLine() const; + void SetMultiLine(bool value); + + void ApplyBorderStyle(const style::ApplyBorderStyleInfo& style) override; + + private: + std::unique_ptr border_render_object_; + std::unique_ptr scroll_render_object_; + std::unique_ptr text_render_object_; + + std::unique_ptr service_; +}; +} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/TextBox.hpp b/include/cru/ui/controls/TextBox.hpp deleted file mode 100644 index 241a8a16..00000000 --- a/include/cru/ui/controls/TextBox.hpp +++ /dev/null @@ -1,46 +0,0 @@ -#pragma once -#include "NoChildControl.hpp" - -#include "IBorderControl.hpp" -#include "TextHostControlService.hpp" - -#include - -namespace cru::ui::controls { -class CRU_UI_API TextBox : public NoChildControl, - public virtual IBorderControl, - public virtual ITextHostControl { - public: - static constexpr StringView control_type = u"TextBox"; - - static TextBox* Create() { return new TextBox(); } - - protected: - TextBox(); - - public: - CRU_DELETE_COPY(TextBox) - CRU_DELETE_MOVE(TextBox) - - ~TextBox() override; - - String GetControlType() const final { return control_type.ToString(); } - - render::RenderObject* GetRenderObject() const override; - - gsl::not_null GetTextRenderObject() override; - render::ScrollRenderObject* GetScrollRenderObject() override; - - bool GetMultiLine() const; - void SetMultiLine(bool value); - - void ApplyBorderStyle(const style::ApplyBorderStyleInfo& style) override; - - private: - std::unique_ptr border_render_object_; - std::unique_ptr scroll_render_object_; - std::unique_ptr text_render_object_; - - std::unique_ptr service_; -}; -} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/TextHostControlService.h b/include/cru/ui/controls/TextHostControlService.h new file mode 100644 index 00000000..5178107f --- /dev/null +++ b/include/cru/ui/controls/TextHostControlService.h @@ -0,0 +1,219 @@ +#pragma once +#include "Base.h" + +#include "cru/platform/gui/InputMethod.h" +#include "cru/platform/gui/TimerHelper.h" +#include "cru/platform/gui/UiApplication.h" +#include "cru/ui/controls/Control.h" +#include "cru/ui/helper/ShortcutHub.h" + +#include +#include + +namespace cru::ui::render { +class TextRenderObject; +class ScrollRenderObject; +} // namespace cru::ui::render + +namespace cru::ui::components { +class PopupMenu; +} + +namespace cru::ui::controls { +constexpr int k_default_caret_blink_duration = 500; + +struct CRU_UI_API ITextHostControl : virtual Interface { + virtual gsl::not_null GetTextRenderObject() = 0; + // May return nullptr. + virtual render::ScrollRenderObject* GetScrollRenderObject() = 0; +}; + +class TextHostControlService; + +class TextControlMovePattern : public Object { + public: + static TextControlMovePattern kLeft; + static TextControlMovePattern kRight; + static TextControlMovePattern kCtrlLeft; + static TextControlMovePattern kCtrlRight; + static TextControlMovePattern kUp; + static TextControlMovePattern kDown; + static TextControlMovePattern kHome; + static TextControlMovePattern kEnd; + static TextControlMovePattern kCtrlHome; + static TextControlMovePattern kCtrlEnd; + static TextControlMovePattern kPageUp; + static TextControlMovePattern kPageDown; + + static std::vector kDefaultPatterns; + + using MoveFunction = + std::function; + + TextControlMovePattern(String name, helper::ShortcutKeyBind key_bind, + MoveFunction move_function) + : name_(std::move(name)), + key_bind_(key_bind), + move_function_(move_function) {} + + CRU_DEFAULT_COPY(TextControlMovePattern) + CRU_DEFAULT_MOVE(TextControlMovePattern) + + ~TextControlMovePattern() override = default; + + public: + String GetName() const { return name_; } + helper::ShortcutKeyBind GetKeyBind() const { return key_bind_; } + gsl::index Move(TextHostControlService* service, StringView text, + gsl::index current_position) const { + return move_function_(service, text, current_position); + } + + private: + String name_; + helper::ShortcutKeyBind key_bind_; + MoveFunction move_function_; +}; + +class CRU_UI_API TextHostControlService : public Object { + CRU_DEFINE_CLASS_LOG_TAG(u"cru::ui::controls::TextControlService") + + public: + TextHostControlService(gsl::not_null control); + + CRU_DELETE_COPY(TextHostControlService) + CRU_DELETE_MOVE(TextHostControlService) + + ~TextHostControlService(); + + public: + bool IsEnabled() { return enable_; } + void SetEnabled(bool enable); + + bool IsEditable() { return this->editable_; } + void SetEditable(bool editable); + + bool IsContextMenuEnabled() { return this->context_menu_enabled_; } + void SetContextMenuEnabled(bool enabled); + + bool IsMultiLine() { return this->multi_line_; } + // If text contains line feed characters, it will be converted to space. + void SetMultiLine(bool multi_line); + + String GetText() { return this->text_; } + StringView GetTextView() { return this->text_; } + void SetText(String text, bool stop_composition = false); + + void InsertText(gsl::index position, StringView text, + bool stop_composition = false); + void DeleteChar(gsl::index position, bool stop_composition = false); + + // Return the position of deleted character. + gsl::index DeleteCharPrevious(gsl::index position, + bool stop_composition = false); + void DeleteText(TextRange range, bool stop_composition = false); + + void CancelComposition(); + + std::optional GetCompositionInfo(); + + bool IsCaretVisible() { return caret_visible_; } + void SetCaretVisible(bool visible); + + int GetCaretBlinkDuration() { return caret_blink_duration_; } + void SetCaretBlinkDuration(int milliseconds); + + gsl::index GetCaretPosition() { return selection_.GetEnd(); } + TextRange GetSelection() { return selection_; } + + StringView GetSelectedText(); + + void SetSelection(gsl::index caret_position); + void SetSelection(TextRange selection, bool scroll_to_caret = true); + + void SelectAll(); + + void ChangeSelectionEnd(gsl::index new_end); + void AbortSelection(); + + void DeleteSelectedText(); + // If some text is selected, then they are deleted first. Then insert text + // into caret position. + void ReplaceSelectedText(StringView text); + + void ScrollToCaret(); + + void Cut(); + void Copy(); + void Paste(); + + gsl::not_null GetTextRenderObject(); + render::ScrollRenderObject* GetScrollRenderObject(); + + private: + // May return nullptr. + platform::gui::IInputMethodContext* GetInputMethodContext(); + + void CoerceSelection(); + + void SetupCaret(); + void TearDownCaret(); + + void SyncTextRenderObject(); + + void UpdateInputMethodPosition(); + + template + void SetupOneHandler(events::RoutedEvent* (Control::*event)(), + void (TextHostControlService::*handler)( + typename events::RoutedEvent::EventArgs)) { + this->event_guard_ += (this->control_->*event)()->Bubble()->AddHandler( + std::bind(handler, this, std::placeholders::_1)); + } + + void MouseMoveHandler(events::MouseEventArgs& args); + void MouseDownHandler(events::MouseButtonEventArgs& args); + void MouseUpHandler(events::MouseButtonEventArgs& args); + void GainFocusHandler(events::FocusChangeEventArgs& args); + void LoseFocusHandler(events::FocusChangeEventArgs& args); + + void SetUpShortcuts(); + + enum ContextMenuItem : unsigned { + kSelectAll = 0b1, + kCut = 0b10, + kCopy = 0b100, + kPaste = 0b1000 + }; + + void OpenContextMenu(const Point& position, ContextMenuItem items); + + private: + gsl::not_null control_; + gsl::not_null text_host_control_; + + EventRevokerListGuard event_guard_; + EventRevokerListGuard input_method_context_event_guard_; + + String text_; + TextRange selection_; + + bool enable_ = false; + bool editable_ = false; + bool multi_line_ = false; + bool context_menu_enabled_ = true; + + bool caret_visible_ = false; + platform::gui::TimerAutoCanceler caret_timer_canceler_; + int caret_blink_duration_ = k_default_caret_blink_duration; + + helper::ShortcutHub shortcut_hub_; + + // true if left mouse is down and selecting + bool mouse_move_selecting_ = false; + + bool context_menu_dirty_ = true; + std::unique_ptr context_menu_; +}; +} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/TextHostControlService.hpp b/include/cru/ui/controls/TextHostControlService.hpp deleted file mode 100644 index be6a7c39..00000000 --- a/include/cru/ui/controls/TextHostControlService.hpp +++ /dev/null @@ -1,219 +0,0 @@ -#pragma once -#include "Base.hpp" - -#include "cru/platform/gui/InputMethod.hpp" -#include "cru/platform/gui/TimerHelper.hpp" -#include "cru/platform/gui/UiApplication.hpp" -#include "cru/ui/controls/Control.hpp" -#include "cru/ui/helper/ShortcutHub.hpp" - -#include -#include - -namespace cru::ui::render { -class TextRenderObject; -class ScrollRenderObject; -} // namespace cru::ui::render - -namespace cru::ui::components { -class PopupMenu; -} - -namespace cru::ui::controls { -constexpr int k_default_caret_blink_duration = 500; - -struct CRU_UI_API ITextHostControl : virtual Interface { - virtual gsl::not_null GetTextRenderObject() = 0; - // May return nullptr. - virtual render::ScrollRenderObject* GetScrollRenderObject() = 0; -}; - -class TextHostControlService; - -class TextControlMovePattern : public Object { - public: - static TextControlMovePattern kLeft; - static TextControlMovePattern kRight; - static TextControlMovePattern kCtrlLeft; - static TextControlMovePattern kCtrlRight; - static TextControlMovePattern kUp; - static TextControlMovePattern kDown; - static TextControlMovePattern kHome; - static TextControlMovePattern kEnd; - static TextControlMovePattern kCtrlHome; - static TextControlMovePattern kCtrlEnd; - static TextControlMovePattern kPageUp; - static TextControlMovePattern kPageDown; - - static std::vector kDefaultPatterns; - - using MoveFunction = - std::function; - - TextControlMovePattern(String name, helper::ShortcutKeyBind key_bind, - MoveFunction move_function) - : name_(std::move(name)), - key_bind_(key_bind), - move_function_(move_function) {} - - CRU_DEFAULT_COPY(TextControlMovePattern) - CRU_DEFAULT_MOVE(TextControlMovePattern) - - ~TextControlMovePattern() override = default; - - public: - String GetName() const { return name_; } - helper::ShortcutKeyBind GetKeyBind() const { return key_bind_; } - gsl::index Move(TextHostControlService* service, StringView text, - gsl::index current_position) const { - return move_function_(service, text, current_position); - } - - private: - String name_; - helper::ShortcutKeyBind key_bind_; - MoveFunction move_function_; -}; - -class CRU_UI_API TextHostControlService : public Object { - CRU_DEFINE_CLASS_LOG_TAG(u"cru::ui::controls::TextControlService") - - public: - TextHostControlService(gsl::not_null control); - - CRU_DELETE_COPY(TextHostControlService) - CRU_DELETE_MOVE(TextHostControlService) - - ~TextHostControlService(); - - public: - bool IsEnabled() { return enable_; } - void SetEnabled(bool enable); - - bool IsEditable() { return this->editable_; } - void SetEditable(bool editable); - - bool IsContextMenuEnabled() { return this->context_menu_enabled_; } - void SetContextMenuEnabled(bool enabled); - - bool IsMultiLine() { return this->multi_line_; } - // If text contains line feed characters, it will be converted to space. - void SetMultiLine(bool multi_line); - - String GetText() { return this->text_; } - StringView GetTextView() { return this->text_; } - void SetText(String text, bool stop_composition = false); - - void InsertText(gsl::index position, StringView text, - bool stop_composition = false); - void DeleteChar(gsl::index position, bool stop_composition = false); - - // Return the position of deleted character. - gsl::index DeleteCharPrevious(gsl::index position, - bool stop_composition = false); - void DeleteText(TextRange range, bool stop_composition = false); - - void CancelComposition(); - - std::optional GetCompositionInfo(); - - bool IsCaretVisible() { return caret_visible_; } - void SetCaretVisible(bool visible); - - int GetCaretBlinkDuration() { return caret_blink_duration_; } - void SetCaretBlinkDuration(int milliseconds); - - gsl::index GetCaretPosition() { return selection_.GetEnd(); } - TextRange GetSelection() { return selection_; } - - StringView GetSelectedText(); - - void SetSelection(gsl::index caret_position); - void SetSelection(TextRange selection, bool scroll_to_caret = true); - - void SelectAll(); - - void ChangeSelectionEnd(gsl::index new_end); - void AbortSelection(); - - void DeleteSelectedText(); - // If some text is selected, then they are deleted first. Then insert text - // into caret position. - void ReplaceSelectedText(StringView text); - - void ScrollToCaret(); - - void Cut(); - void Copy(); - void Paste(); - - gsl::not_null GetTextRenderObject(); - render::ScrollRenderObject* GetScrollRenderObject(); - - private: - // May return nullptr. - platform::gui::IInputMethodContext* GetInputMethodContext(); - - void CoerceSelection(); - - void SetupCaret(); - void TearDownCaret(); - - void SyncTextRenderObject(); - - void UpdateInputMethodPosition(); - - template - void SetupOneHandler(events::RoutedEvent* (Control::*event)(), - void (TextHostControlService::*handler)( - typename events::RoutedEvent::EventArgs)) { - this->event_guard_ += (this->control_->*event)()->Bubble()->AddHandler( - std::bind(handler, this, std::placeholders::_1)); - } - - void MouseMoveHandler(events::MouseEventArgs& args); - void MouseDownHandler(events::MouseButtonEventArgs& args); - void MouseUpHandler(events::MouseButtonEventArgs& args); - void GainFocusHandler(events::FocusChangeEventArgs& args); - void LoseFocusHandler(events::FocusChangeEventArgs& args); - - void SetUpShortcuts(); - - enum ContextMenuItem : unsigned { - kSelectAll = 0b1, - kCut = 0b10, - kCopy = 0b100, - kPaste = 0b1000 - }; - - void OpenContextMenu(const Point& position, ContextMenuItem items); - - private: - gsl::not_null control_; - gsl::not_null text_host_control_; - - EventRevokerListGuard event_guard_; - EventRevokerListGuard input_method_context_event_guard_; - - String text_; - TextRange selection_; - - bool enable_ = false; - bool editable_ = false; - bool multi_line_ = false; - bool context_menu_enabled_ = true; - - bool caret_visible_ = false; - platform::gui::TimerAutoCanceler caret_timer_canceler_; - int caret_blink_duration_ = k_default_caret_blink_duration; - - helper::ShortcutHub shortcut_hub_; - - // true if left mouse is down and selecting - bool mouse_move_selecting_ = false; - - bool context_menu_dirty_ = true; - std::unique_ptr context_menu_; -}; -} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/Window.h b/include/cru/ui/controls/Window.h new file mode 100644 index 00000000..5fb6d594 --- /dev/null +++ b/include/cru/ui/controls/Window.h @@ -0,0 +1,27 @@ +#pragma once +#include "cru/platform/gui/Base.h" +#include "cru/ui/controls/RootControl.h" + +#include "cru/common/Base.h" + +namespace cru::ui::controls { +class CRU_UI_API Window final : public RootControl { + public: + static constexpr StringView control_type = u"Window"; + + public: + static Window* Create(Control* attached_control = nullptr); + + private: + explicit Window(Control* attached_control); + + public: + CRU_DELETE_COPY(Window) + CRU_DELETE_MOVE(Window) + + ~Window() override; + + public: + String GetControlType() const final { return control_type.ToString(); } +}; +} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/Window.hpp b/include/cru/ui/controls/Window.hpp deleted file mode 100644 index c304a839..00000000 --- a/include/cru/ui/controls/Window.hpp +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once -#include "cru/platform/gui/Base.hpp" -#include "cru/ui/controls/RootControl.hpp" - -#include "cru/common/Base.hpp" - -namespace cru::ui::controls { -class CRU_UI_API Window final : public RootControl { - public: - static constexpr StringView control_type = u"Window"; - - public: - static Window* Create(Control* attached_control = nullptr); - - private: - explicit Window(Control* attached_control); - - public: - CRU_DELETE_COPY(Window) - CRU_DELETE_MOVE(Window) - - ~Window() override; - - public: - String GetControlType() const final { return control_type.ToString(); } -}; -} // namespace cru::ui::controls diff --git a/include/cru/ui/events/FocusChangeEventArgs.h b/include/cru/ui/events/FocusChangeEventArgs.h new file mode 100644 index 00000000..aaef25b4 --- /dev/null +++ b/include/cru/ui/events/FocusChangeEventArgs.h @@ -0,0 +1,23 @@ +#pragma once +#include "UiEventArgs.h" + +namespace cru::ui::events { + +class CRU_UI_API FocusChangeEventArgs : public UiEventArgs { + public: + FocusChangeEventArgs(Object* sender, Object* original_sender, + const bool is_window = false) + : UiEventArgs(sender, original_sender), is_window_(is_window) {} + FocusChangeEventArgs(const FocusChangeEventArgs& other) = default; + FocusChangeEventArgs(FocusChangeEventArgs&& other) = default; + FocusChangeEventArgs& operator=(const FocusChangeEventArgs& other) = default; + FocusChangeEventArgs& operator=(FocusChangeEventArgs&& other) = default; + ~FocusChangeEventArgs() override = default; + + // Return whether the focus change is caused by the window-wide focus change. + bool IsWindow() const { return is_window_; } + + private: + bool is_window_; +}; +} // namespace cru::ui::event diff --git a/include/cru/ui/events/FocusChangeEventArgs.hpp b/include/cru/ui/events/FocusChangeEventArgs.hpp deleted file mode 100644 index b72790a5..00000000 --- a/include/cru/ui/events/FocusChangeEventArgs.hpp +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once -#include "UiEventArgs.hpp" - -namespace cru::ui::events { - -class CRU_UI_API FocusChangeEventArgs : public UiEventArgs { - public: - FocusChangeEventArgs(Object* sender, Object* original_sender, - const bool is_window = false) - : UiEventArgs(sender, original_sender), is_window_(is_window) {} - FocusChangeEventArgs(const FocusChangeEventArgs& other) = default; - FocusChangeEventArgs(FocusChangeEventArgs&& other) = default; - FocusChangeEventArgs& operator=(const FocusChangeEventArgs& other) = default; - FocusChangeEventArgs& operator=(FocusChangeEventArgs&& other) = default; - ~FocusChangeEventArgs() override = default; - - // Return whether the focus change is caused by the window-wide focus change. - bool IsWindow() const { return is_window_; } - - private: - bool is_window_; -}; -} // namespace cru::ui::event diff --git a/include/cru/ui/events/KeyEventArgs.h b/include/cru/ui/events/KeyEventArgs.h new file mode 100644 index 00000000..0ece9126 --- /dev/null +++ b/include/cru/ui/events/KeyEventArgs.h @@ -0,0 +1,29 @@ +#pragma once +#include "UiEventArgs.h" + +#include "cru/platform/gui/Keyboard.h" + +namespace cru::ui::events { +class CRU_UI_API KeyEventArgs : public UiEventArgs { + public: + KeyEventArgs(Object* sender, Object* original_sender, + platform::gui::KeyCode key_code, + platform::gui::KeyModifier key_modifier) + : UiEventArgs(sender, original_sender), + key_code_(key_code), + key_modifier_(key_modifier) {} + KeyEventArgs(const KeyEventArgs& other) = default; + KeyEventArgs(KeyEventArgs&& other) = default; + KeyEventArgs& operator=(const KeyEventArgs& other) = default; + KeyEventArgs& operator=(KeyEventArgs&& other) = default; + ~KeyEventArgs() override = default; + + platform::gui::KeyCode GetKeyCode() const { return key_code_; } + platform::gui::KeyModifier GetKeyModifier() const { return key_modifier_; } + + private: + platform::gui::KeyCode key_code_; + platform::gui::KeyModifier key_modifier_; +}; + +} // namespace cru::ui::event diff --git a/include/cru/ui/events/KeyEventArgs.hpp b/include/cru/ui/events/KeyEventArgs.hpp deleted file mode 100644 index 133db1df..00000000 --- a/include/cru/ui/events/KeyEventArgs.hpp +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once -#include "UiEventArgs.hpp" - -#include "cru/platform/gui/Keyboard.hpp" - -namespace cru::ui::events { -class CRU_UI_API KeyEventArgs : public UiEventArgs { - public: - KeyEventArgs(Object* sender, Object* original_sender, - platform::gui::KeyCode key_code, - platform::gui::KeyModifier key_modifier) - : UiEventArgs(sender, original_sender), - key_code_(key_code), - key_modifier_(key_modifier) {} - KeyEventArgs(const KeyEventArgs& other) = default; - KeyEventArgs(KeyEventArgs&& other) = default; - KeyEventArgs& operator=(const KeyEventArgs& other) = default; - KeyEventArgs& operator=(KeyEventArgs&& other) = default; - ~KeyEventArgs() override = default; - - platform::gui::KeyCode GetKeyCode() const { return key_code_; } - platform::gui::KeyModifier GetKeyModifier() const { return key_modifier_; } - - private: - platform::gui::KeyCode key_code_; - platform::gui::KeyModifier key_modifier_; -}; - -} // namespace cru::ui::event diff --git a/include/cru/ui/events/MouseButtonEventArgs.h b/include/cru/ui/events/MouseButtonEventArgs.h new file mode 100644 index 00000000..13b3b0a4 --- /dev/null +++ b/include/cru/ui/events/MouseButtonEventArgs.h @@ -0,0 +1,35 @@ +#pragma once +#include "MouseEventArgs.h" + +#include "cru/platform/gui/Keyboard.h" + +namespace cru::ui::events { + +class CRU_UI_API MouseButtonEventArgs : public MouseEventArgs { + public: + MouseButtonEventArgs(Object* sender, Object* original_sender, + const Point& point, const MouseButton button, + platform::gui::KeyModifier key_modifier) + : MouseEventArgs(sender, original_sender, point), + button_(button), + key_modifier_(key_modifier) {} + MouseButtonEventArgs(Object* sender, Object* original_sender, + const MouseButton button, + platform::gui::KeyModifier key_modifier) + : MouseEventArgs(sender, original_sender), + button_(button), + key_modifier_(key_modifier) {} + MouseButtonEventArgs(const MouseButtonEventArgs& other) = default; + MouseButtonEventArgs(MouseButtonEventArgs&& other) = default; + MouseButtonEventArgs& operator=(const MouseButtonEventArgs& other) = default; + MouseButtonEventArgs& operator=(MouseButtonEventArgs&& other) = default; + ~MouseButtonEventArgs() override = default; + + MouseButton GetButton() const { return button_; } + platform::gui::KeyModifier GetKeyModifier() const { return key_modifier_; } + + private: + MouseButton button_; + platform::gui::KeyModifier key_modifier_; +}; +} // namespace cru::ui::event diff --git a/include/cru/ui/events/MouseButtonEventArgs.hpp b/include/cru/ui/events/MouseButtonEventArgs.hpp deleted file mode 100644 index 791ed03b..00000000 --- a/include/cru/ui/events/MouseButtonEventArgs.hpp +++ /dev/null @@ -1,35 +0,0 @@ -#pragma once -#include "MouseEventArgs.hpp" - -#include "cru/platform/gui/Keyboard.hpp" - -namespace cru::ui::events { - -class CRU_UI_API MouseButtonEventArgs : public MouseEventArgs { - public: - MouseButtonEventArgs(Object* sender, Object* original_sender, - const Point& point, const MouseButton button, - platform::gui::KeyModifier key_modifier) - : MouseEventArgs(sender, original_sender, point), - button_(button), - key_modifier_(key_modifier) {} - MouseButtonEventArgs(Object* sender, Object* original_sender, - const MouseButton button, - platform::gui::KeyModifier key_modifier) - : MouseEventArgs(sender, original_sender), - button_(button), - key_modifier_(key_modifier) {} - MouseButtonEventArgs(const MouseButtonEventArgs& other) = default; - MouseButtonEventArgs(MouseButtonEventArgs&& other) = default; - MouseButtonEventArgs& operator=(const MouseButtonEventArgs& other) = default; - MouseButtonEventArgs& operator=(MouseButtonEventArgs&& other) = default; - ~MouseButtonEventArgs() override = default; - - MouseButton GetButton() const { return button_; } - platform::gui::KeyModifier GetKeyModifier() const { return key_modifier_; } - - private: - MouseButton button_; - platform::gui::KeyModifier key_modifier_; -}; -} // namespace cru::ui::event diff --git a/include/cru/ui/events/MouseEventArgs.h b/include/cru/ui/events/MouseEventArgs.h new file mode 100644 index 00000000..11ce6463 --- /dev/null +++ b/include/cru/ui/events/MouseEventArgs.h @@ -0,0 +1,27 @@ +#pragma once +#include "UiEventArgs.h" + +#include + +namespace cru::ui::events { +class CRU_UI_API MouseEventArgs : public UiEventArgs { + public: + MouseEventArgs(Object* sender, Object* original_sender, + const std::optional& point = std::nullopt) + : UiEventArgs(sender, original_sender), point_(point) {} + MouseEventArgs(const MouseEventArgs& other) = default; + MouseEventArgs(MouseEventArgs&& other) = default; + MouseEventArgs& operator=(const MouseEventArgs& other) = default; + MouseEventArgs& operator=(MouseEventArgs&& other) = default; + ~MouseEventArgs() override = default; + + // This point is relative to window client lefttop. + Point GetPoint() const { return point_.value_or(Point{}); } + Point GetPoint(render::RenderObject* render_target) const; + Point GetPointToContent(render::RenderObject* render_target) const; + Point GetPointOfScreen() const; + + private: + std::optional point_; +}; +} // namespace cru::ui::events diff --git a/include/cru/ui/events/MouseEventArgs.hpp b/include/cru/ui/events/MouseEventArgs.hpp deleted file mode 100644 index 0adc65b6..00000000 --- a/include/cru/ui/events/MouseEventArgs.hpp +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once -#include "UiEventArgs.hpp" - -#include - -namespace cru::ui::events { -class CRU_UI_API MouseEventArgs : public UiEventArgs { - public: - MouseEventArgs(Object* sender, Object* original_sender, - const std::optional& point = std::nullopt) - : UiEventArgs(sender, original_sender), point_(point) {} - MouseEventArgs(const MouseEventArgs& other) = default; - MouseEventArgs(MouseEventArgs&& other) = default; - MouseEventArgs& operator=(const MouseEventArgs& other) = default; - MouseEventArgs& operator=(MouseEventArgs&& other) = default; - ~MouseEventArgs() override = default; - - // This point is relative to window client lefttop. - Point GetPoint() const { return point_.value_or(Point{}); } - Point GetPoint(render::RenderObject* render_target) const; - Point GetPointToContent(render::RenderObject* render_target) const; - Point GetPointOfScreen() const; - - private: - std::optional point_; -}; -} // namespace cru::ui::events diff --git a/include/cru/ui/events/MouseWheelEventArgs.h b/include/cru/ui/events/MouseWheelEventArgs.h new file mode 100644 index 00000000..85791491 --- /dev/null +++ b/include/cru/ui/events/MouseWheelEventArgs.h @@ -0,0 +1,29 @@ +#pragma once +#include "MouseEventArgs.h" + +#include "cru/platform/gui/Keyboard.h" + +namespace cru::ui::events { +class CRU_UI_API MouseWheelEventArgs : public MouseEventArgs { + public: + MouseWheelEventArgs(Object* sender, Object* original_sender, + const Point& point, const float delta, + platform::gui::KeyModifier key_modifier) + : MouseEventArgs(sender, original_sender, point), + delta_(delta), + key_modifier_(key_modifier) {} + MouseWheelEventArgs(const MouseWheelEventArgs& other) = default; + MouseWheelEventArgs(MouseWheelEventArgs&& other) = default; + MouseWheelEventArgs& operator=(const MouseWheelEventArgs& other) = default; + MouseWheelEventArgs& operator=(MouseWheelEventArgs&& other) = default; + ~MouseWheelEventArgs() override = default; + + // Positive means down; Negative means up. + float GetDelta() const { return delta_; } + platform::gui::KeyModifier GetKeyModifier() const { return key_modifier_; } + + private: + float delta_; + platform::gui::KeyModifier key_modifier_; +}; +} // namespace cru::ui::event diff --git a/include/cru/ui/events/MouseWheelEventArgs.hpp b/include/cru/ui/events/MouseWheelEventArgs.hpp deleted file mode 100644 index c6c0fe10..00000000 --- a/include/cru/ui/events/MouseWheelEventArgs.hpp +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once -#include "MouseEventArgs.hpp" - -#include "cru/platform/gui/Keyboard.hpp" - -namespace cru::ui::events { -class CRU_UI_API MouseWheelEventArgs : public MouseEventArgs { - public: - MouseWheelEventArgs(Object* sender, Object* original_sender, - const Point& point, const float delta, - platform::gui::KeyModifier key_modifier) - : MouseEventArgs(sender, original_sender, point), - delta_(delta), - key_modifier_(key_modifier) {} - MouseWheelEventArgs(const MouseWheelEventArgs& other) = default; - MouseWheelEventArgs(MouseWheelEventArgs&& other) = default; - MouseWheelEventArgs& operator=(const MouseWheelEventArgs& other) = default; - MouseWheelEventArgs& operator=(MouseWheelEventArgs&& other) = default; - ~MouseWheelEventArgs() override = default; - - // Positive means down; Negative means up. - float GetDelta() const { return delta_; } - platform::gui::KeyModifier GetKeyModifier() const { return key_modifier_; } - - private: - float delta_; - platform::gui::KeyModifier key_modifier_; -}; -} // namespace cru::ui::event diff --git a/include/cru/ui/events/PaintEventArgs.h b/include/cru/ui/events/PaintEventArgs.h new file mode 100644 index 00000000..934c6c8c --- /dev/null +++ b/include/cru/ui/events/PaintEventArgs.h @@ -0,0 +1,25 @@ +#pragma once +#include "UiEventArgs.h" + +namespace cru::platform::graphics { +struct IPainter; +} + +namespace cru::ui::events { +class CRU_UI_API PaintEventArgs : public UiEventArgs { + public: + PaintEventArgs(Object* sender, Object* original_sender, + platform::graphics::IPainter* painter) + : UiEventArgs(sender, original_sender), painter_(painter) {} + PaintEventArgs(const PaintEventArgs& other) = default; + PaintEventArgs(PaintEventArgs&& other) = default; + PaintEventArgs& operator=(const PaintEventArgs& other) = default; + PaintEventArgs& operator=(PaintEventArgs&& other) = default; + ~PaintEventArgs() = default; + + platform::graphics::IPainter* GetPainter() const { return painter_; } + + private: + platform::graphics::IPainter* painter_; +}; +} // namespace cru::ui::event diff --git a/include/cru/ui/events/PaintEventArgs.hpp b/include/cru/ui/events/PaintEventArgs.hpp deleted file mode 100644 index f2b53977..00000000 --- a/include/cru/ui/events/PaintEventArgs.hpp +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once -#include "UiEventArgs.hpp" - -namespace cru::platform::graphics { -struct IPainter; -} - -namespace cru::ui::events { -class CRU_UI_API PaintEventArgs : public UiEventArgs { - public: - PaintEventArgs(Object* sender, Object* original_sender, - platform::graphics::IPainter* painter) - : UiEventArgs(sender, original_sender), painter_(painter) {} - PaintEventArgs(const PaintEventArgs& other) = default; - PaintEventArgs(PaintEventArgs&& other) = default; - PaintEventArgs& operator=(const PaintEventArgs& other) = default; - PaintEventArgs& operator=(PaintEventArgs&& other) = default; - ~PaintEventArgs() = default; - - platform::graphics::IPainter* GetPainter() const { return painter_; } - - private: - platform::graphics::IPainter* painter_; -}; -} // namespace cru::ui::event diff --git a/include/cru/ui/events/RoutedEvent.h b/include/cru/ui/events/RoutedEvent.h new file mode 100644 index 00000000..909678f8 --- /dev/null +++ b/include/cru/ui/events/RoutedEvent.h @@ -0,0 +1,39 @@ +#pragma once +#include "UiEventArgs.h" + +#include "cru/common/Event.h" + +namespace cru::ui::events { +// TEventArgs must not be a reference type. This class help add reference. +// EventArgs must be reference because the IsHandled property must be settable. +template +class CRU_UI_API RoutedEvent { + public: + static_assert(std::is_base_of_v, + "TEventArgs must be subclass of UiEventArgs."); + static_assert(!std::is_reference_v, + "TEventArgs must not be reference."); + + using RawEventArgs = TEventArgs; + using IEventType = IEvent; + using EventArgs = typename IEventType::EventArgs; + + RoutedEvent() = default; + RoutedEvent(const RoutedEvent& other) = delete; + RoutedEvent(RoutedEvent&& other) = delete; + RoutedEvent& operator=(const RoutedEvent& other) = delete; + RoutedEvent& operator=(RoutedEvent&& other) = delete; + ~RoutedEvent() = default; + + IEvent* Direct() { return &direct_; } + + IEvent* Bubble() { return &bubble_; } + + IEvent* Tunnel() { return &tunnel_; } + + private: + Event direct_; + Event bubble_; + Event tunnel_; +}; +} // namespace cru::ui::event diff --git a/include/cru/ui/events/RoutedEvent.hpp b/include/cru/ui/events/RoutedEvent.hpp deleted file mode 100644 index dd0bc21f..00000000 --- a/include/cru/ui/events/RoutedEvent.hpp +++ /dev/null @@ -1,39 +0,0 @@ -#pragma once -#include "UiEventArgs.hpp" - -#include "cru/common/Event.hpp" - -namespace cru::ui::events { -// TEventArgs must not be a reference type. This class help add reference. -// EventArgs must be reference because the IsHandled property must be settable. -template -class CRU_UI_API RoutedEvent { - public: - static_assert(std::is_base_of_v, - "TEventArgs must be subclass of UiEventArgs."); - static_assert(!std::is_reference_v, - "TEventArgs must not be reference."); - - using RawEventArgs = TEventArgs; - using IEventType = IEvent; - using EventArgs = typename IEventType::EventArgs; - - RoutedEvent() = default; - RoutedEvent(const RoutedEvent& other) = delete; - RoutedEvent(RoutedEvent&& other) = delete; - RoutedEvent& operator=(const RoutedEvent& other) = delete; - RoutedEvent& operator=(RoutedEvent&& other) = delete; - ~RoutedEvent() = default; - - IEvent* Direct() { return &direct_; } - - IEvent* Bubble() { return &bubble_; } - - IEvent* Tunnel() { return &tunnel_; } - - private: - Event direct_; - Event bubble_; - Event tunnel_; -}; -} // namespace cru::ui::event diff --git a/include/cru/ui/events/UiEventArgs.h b/include/cru/ui/events/UiEventArgs.h new file mode 100644 index 00000000..fff84edd --- /dev/null +++ b/include/cru/ui/events/UiEventArgs.h @@ -0,0 +1,28 @@ +#pragma once +#include "../Base.h" + +namespace cru::ui::events { +class CRU_UI_API UiEventArgs : public Object { + public: + UiEventArgs(Object* sender, Object* original_sender) + : sender_(sender), original_sender_(original_sender), handled_(false) {} + + UiEventArgs(const UiEventArgs& other) = default; + UiEventArgs(UiEventArgs&& other) = default; + UiEventArgs& operator=(const UiEventArgs& other) = default; + UiEventArgs& operator=(UiEventArgs&& other) = default; + ~UiEventArgs() override = default; + + Object* GetSender() const { return sender_; } + + Object* GetOriginalSender() const { return original_sender_; } + + bool IsHandled() const { return handled_; } + void SetHandled(const bool handled = true) { handled_ = handled; } + + private: + Object* sender_; + Object* original_sender_; + bool handled_; +}; +} // namespace cru::ui::event diff --git a/include/cru/ui/events/UiEventArgs.hpp b/include/cru/ui/events/UiEventArgs.hpp deleted file mode 100644 index 1e1f9e93..00000000 --- a/include/cru/ui/events/UiEventArgs.hpp +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once -#include "../Base.hpp" - -namespace cru::ui::events { -class CRU_UI_API UiEventArgs : public Object { - public: - UiEventArgs(Object* sender, Object* original_sender) - : sender_(sender), original_sender_(original_sender), handled_(false) {} - - UiEventArgs(const UiEventArgs& other) = default; - UiEventArgs(UiEventArgs&& other) = default; - UiEventArgs& operator=(const UiEventArgs& other) = default; - UiEventArgs& operator=(UiEventArgs&& other) = default; - ~UiEventArgs() override = default; - - Object* GetSender() const { return sender_; } - - Object* GetOriginalSender() const { return original_sender_; } - - bool IsHandled() const { return handled_; } - void SetHandled(const bool handled = true) { handled_ = handled; } - - private: - Object* sender_; - Object* original_sender_; - bool handled_; -}; -} // namespace cru::ui::event diff --git a/include/cru/ui/events/UiEvents.h b/include/cru/ui/events/UiEvents.h new file mode 100644 index 00000000..301ec694 --- /dev/null +++ b/include/cru/ui/events/UiEvents.h @@ -0,0 +1,10 @@ +#pragma once + +#include "FocusChangeEventArgs.h" +#include "KeyEventArgs.h" +#include "MouseButtonEventArgs.h" +#include "MouseEventArgs.h" +#include "MouseWheelEventArgs.h" +#include "PaintEventArgs.h" +#include "RoutedEvent.h" +#include "UiEventArgs.h" diff --git a/include/cru/ui/events/UiEvents.hpp b/include/cru/ui/events/UiEvents.hpp deleted file mode 100644 index bd6f7d2a..00000000 --- a/include/cru/ui/events/UiEvents.hpp +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -#include "FocusChangeEventArgs.hpp" -#include "KeyEventArgs.hpp" -#include "MouseButtonEventArgs.hpp" -#include "MouseEventArgs.hpp" -#include "MouseWheelEventArgs.hpp" -#include "PaintEventArgs.hpp" -#include "RoutedEvent.hpp" -#include "UiEventArgs.hpp" diff --git a/include/cru/ui/helper/ClickDetector.h b/include/cru/ui/helper/ClickDetector.h new file mode 100644 index 00000000..6e30aaf0 --- /dev/null +++ b/include/cru/ui/helper/ClickDetector.h @@ -0,0 +1,87 @@ +#pragma once +#include "../controls/Control.h" + +namespace cru::ui::helper { +class CRU_UI_API ClickEventArgs : Object { + public: + ClickEventArgs(controls::Control* sender, const Point& down_point, + const Point& up_point, MouseButton button) + : sender_(sender), + down_point_(down_point), + up_point_(up_point), + button_(button) {} + + CRU_DEFAULT_COPY(ClickEventArgs) + CRU_DEFAULT_MOVE(ClickEventArgs) + + ~ClickEventArgs() override = default; + + controls::Control* GetSender() const { return sender_; } + Point GetDownPoint() const { return down_point_; } + Point GetUpPoint() const { return up_point_; } + MouseButton GetButton() const { return button_; } + + private: + controls::Control* sender_; + Point down_point_; + Point up_point_; + MouseButton button_; +}; + +enum class ClickState { + None, // Mouse is outside the control. + Hover, // Mouse hovers on the control but not pressed + Press, // Mouse is pressed and if released click is done. + PressInactive // Mouse is pressed but if released click is canceled. +}; + +class ClickDetector : public Object { + CRU_DEFINE_CLASS_LOG_TAG(u"cru::ui::ClickDetector") + + public: + explicit ClickDetector(controls::Control* control); + + CRU_DELETE_COPY(ClickDetector) + CRU_DELETE_MOVE(ClickDetector) + + ~ClickDetector() override = default; + + controls::Control* GetControl() const { return control_; } + + ClickState GetState() const { return state_; } + + // Default is enable. + bool IsEnabled() const { return enable_; } + // If disable when user is pressing, the pressing is deactivated. + void SetEnabled(bool enable); + + // Default is left and right. + MouseButton GetTriggerButton() const { return trigger_button_; } + // If unset the trigger button when user is pressing, the pressing is + // deactivated. + void SetTriggerButton(MouseButton trigger_button); + + IEvent* ClickEvent() { return &event_; } + + IEvent* StateChangeEvent() { return &state_change_event_; } + + private: + void SetState(ClickState state); + + private: + controls::Control* control_; + + ClickState state_ = ClickState::None; + + bool enable_ = true; + MouseButton trigger_button_ = mouse_buttons::left | mouse_buttons::right; + + Event event_; + Event state_change_event_; + + std::vector event_rovoker_guards_; + + Point down_point_; + MouseButton button_; +}; +} // namespace cru::ui::helper diff --git a/include/cru/ui/helper/ClickDetector.hpp b/include/cru/ui/helper/ClickDetector.hpp deleted file mode 100644 index fd80a953..00000000 --- a/include/cru/ui/helper/ClickDetector.hpp +++ /dev/null @@ -1,87 +0,0 @@ -#pragma once -#include "../controls/Control.hpp" - -namespace cru::ui::helper { -class CRU_UI_API ClickEventArgs : Object { - public: - ClickEventArgs(controls::Control* sender, const Point& down_point, - const Point& up_point, MouseButton button) - : sender_(sender), - down_point_(down_point), - up_point_(up_point), - button_(button) {} - - CRU_DEFAULT_COPY(ClickEventArgs) - CRU_DEFAULT_MOVE(ClickEventArgs) - - ~ClickEventArgs() override = default; - - controls::Control* GetSender() const { return sender_; } - Point GetDownPoint() const { return down_point_; } - Point GetUpPoint() const { return up_point_; } - MouseButton GetButton() const { return button_; } - - private: - controls::Control* sender_; - Point down_point_; - Point up_point_; - MouseButton button_; -}; - -enum class ClickState { - None, // Mouse is outside the control. - Hover, // Mouse hovers on the control but not pressed - Press, // Mouse is pressed and if released click is done. - PressInactive // Mouse is pressed but if released click is canceled. -}; - -class ClickDetector : public Object { - CRU_DEFINE_CLASS_LOG_TAG(u"cru::ui::ClickDetector") - - public: - explicit ClickDetector(controls::Control* control); - - CRU_DELETE_COPY(ClickDetector) - CRU_DELETE_MOVE(ClickDetector) - - ~ClickDetector() override = default; - - controls::Control* GetControl() const { return control_; } - - ClickState GetState() const { return state_; } - - // Default is enable. - bool IsEnabled() const { return enable_; } - // If disable when user is pressing, the pressing is deactivated. - void SetEnabled(bool enable); - - // Default is left and right. - MouseButton GetTriggerButton() const { return trigger_button_; } - // If unset the trigger button when user is pressing, the pressing is - // deactivated. - void SetTriggerButton(MouseButton trigger_button); - - IEvent* ClickEvent() { return &event_; } - - IEvent* StateChangeEvent() { return &state_change_event_; } - - private: - void SetState(ClickState state); - - private: - controls::Control* control_; - - ClickState state_ = ClickState::None; - - bool enable_ = true; - MouseButton trigger_button_ = mouse_buttons::left | mouse_buttons::right; - - Event event_; - Event state_change_event_; - - std::vector event_rovoker_guards_; - - Point down_point_; - MouseButton button_; -}; -} // namespace cru::ui::helper diff --git a/include/cru/ui/helper/ShortcutHub.h b/include/cru/ui/helper/ShortcutHub.h new file mode 100644 index 00000000..84e786aa --- /dev/null +++ b/include/cru/ui/helper/ShortcutHub.h @@ -0,0 +1,142 @@ +#pragma once +#include "../Base.h" + +#include "../events/UiEvents.h" +#include "cru/common/Event.h" +#include "cru/platform/gui/Keyboard.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace cru::ui::helper { + +class ShortcutKeyBind { + public: + ShortcutKeyBind( + platform::gui::KeyCode key, + platform::gui::KeyModifier modifier = platform::gui::KeyModifiers::none) + : key_(key), modifier_(modifier) {} + + CRU_DEFAULT_COPY(ShortcutKeyBind) + CRU_DEFAULT_MOVE(ShortcutKeyBind) + + ~ShortcutKeyBind() = default; + + platform::gui::KeyCode GetKey() const { return key_; } + platform::gui::KeyModifier GetModifier() const { return modifier_; } + + ShortcutKeyBind AddModifier(platform::gui::KeyModifier modifier) const { + return ShortcutKeyBind(key_, modifier_ | modifier); + } + + bool Is(platform::gui::KeyCode key, + platform::gui::KeyModifier modifier) const { + return key == key_ && modifier == modifier_; + } + + bool operator==(const ShortcutKeyBind& other) const { + return this->key_ == other.key_ && this->modifier_ == other.modifier_; + } + + bool operator!=(const ShortcutKeyBind& other) const { + return !this->operator==(other); + } + + String ToString() const { + String result = u"("; + result += platform::gui::ToString(modifier_); + result += u")"; + result += platform::gui::ToString(key_); + return result; + } + + private: + platform::gui::KeyCode key_; + platform::gui::KeyModifier modifier_; +}; + +inline String ToString(const ShortcutKeyBind& key_bind) { + return key_bind.ToString(); +} +} // namespace cru::ui::helper + +namespace std { +template <> +struct hash { + std::size_t operator()(const cru::ui::helper::ShortcutKeyBind& value) const { + std::size_t result = 0; + cru::hash_combine(result, static_cast(value.GetKey())); + cru::hash_combine(result, static_cast(value.GetModifier())); + return result; + } +}; +} // namespace std + +namespace cru::ui::helper { +struct Shortcut { + // Just for debug. + String name; + ShortcutKeyBind key_bind; + // Return true if it consumes the shortcut. Or return false if it does not + // handle the shortcut. + std::function handler; +}; + +struct ShortcutInfo { + int id; + String name; + ShortcutKeyBind key_bind; + std::function handler; +}; + +class CRU_UI_API ShortcutHub : public Object { + public: + ShortcutHub() = default; + + CRU_DELETE_COPY(ShortcutHub) + CRU_DELETE_MOVE(ShortcutHub) + + ~ShortcutHub() override = default; + + int RegisterShortcut(String name, ShortcutKeyBind bind, + std::function handler) { + return RegisterShortcut({std::move(name), bind, std::move(handler)}); + } + + // Return an id used for unregistering. + int RegisterShortcut(Shortcut shortcut); + + void UnregisterShortcut(int id); + + std::vector GetAllShortcuts() const; + std::optional GetShortcut(int id) const; + const std::vector& GetShortcutByKeyBind( + const ShortcutKeyBind& key_bind) const; + + IEvent* FallbackKeyEvent() { return &fallback_event_; } + + void Install(controls::Control* control); + void Uninstall(); + + private: + void OnKeyDown(events::KeyEventArgs& event); + + private: + std::unordered_map> map_; + + std::vector empty_list_; + + int current_id_ = 1; + + Event fallback_event_; + + EventRevokerListGuard event_guard_; +}; +} // namespace cru::ui::helper diff --git a/include/cru/ui/helper/ShortcutHub.hpp b/include/cru/ui/helper/ShortcutHub.hpp deleted file mode 100644 index c7054786..00000000 --- a/include/cru/ui/helper/ShortcutHub.hpp +++ /dev/null @@ -1,142 +0,0 @@ -#pragma once -#include "../Base.hpp" - -#include "../events/UiEvents.hpp" -#include "cru/common/Event.hpp" -#include "cru/platform/gui/Keyboard.hpp" - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace cru::ui::helper { - -class ShortcutKeyBind { - public: - ShortcutKeyBind( - platform::gui::KeyCode key, - platform::gui::KeyModifier modifier = platform::gui::KeyModifiers::none) - : key_(key), modifier_(modifier) {} - - CRU_DEFAULT_COPY(ShortcutKeyBind) - CRU_DEFAULT_MOVE(ShortcutKeyBind) - - ~ShortcutKeyBind() = default; - - platform::gui::KeyCode GetKey() const { return key_; } - platform::gui::KeyModifier GetModifier() const { return modifier_; } - - ShortcutKeyBind AddModifier(platform::gui::KeyModifier modifier) const { - return ShortcutKeyBind(key_, modifier_ | modifier); - } - - bool Is(platform::gui::KeyCode key, - platform::gui::KeyModifier modifier) const { - return key == key_ && modifier == modifier_; - } - - bool operator==(const ShortcutKeyBind& other) const { - return this->key_ == other.key_ && this->modifier_ == other.modifier_; - } - - bool operator!=(const ShortcutKeyBind& other) const { - return !this->operator==(other); - } - - String ToString() const { - String result = u"("; - result += platform::gui::ToString(modifier_); - result += u")"; - result += platform::gui::ToString(key_); - return result; - } - - private: - platform::gui::KeyCode key_; - platform::gui::KeyModifier modifier_; -}; - -inline String ToString(const ShortcutKeyBind& key_bind) { - return key_bind.ToString(); -} -} // namespace cru::ui::helper - -namespace std { -template <> -struct hash { - std::size_t operator()(const cru::ui::helper::ShortcutKeyBind& value) const { - std::size_t result = 0; - cru::hash_combine(result, static_cast(value.GetKey())); - cru::hash_combine(result, static_cast(value.GetModifier())); - return result; - } -}; -} // namespace std - -namespace cru::ui::helper { -struct Shortcut { - // Just for debug. - String name; - ShortcutKeyBind key_bind; - // Return true if it consumes the shortcut. Or return false if it does not - // handle the shortcut. - std::function handler; -}; - -struct ShortcutInfo { - int id; - String name; - ShortcutKeyBind key_bind; - std::function handler; -}; - -class CRU_UI_API ShortcutHub : public Object { - public: - ShortcutHub() = default; - - CRU_DELETE_COPY(ShortcutHub) - CRU_DELETE_MOVE(ShortcutHub) - - ~ShortcutHub() override = default; - - int RegisterShortcut(String name, ShortcutKeyBind bind, - std::function handler) { - return RegisterShortcut({std::move(name), bind, std::move(handler)}); - } - - // Return an id used for unregistering. - int RegisterShortcut(Shortcut shortcut); - - void UnregisterShortcut(int id); - - std::vector GetAllShortcuts() const; - std::optional GetShortcut(int id) const; - const std::vector& GetShortcutByKeyBind( - const ShortcutKeyBind& key_bind) const; - - IEvent* FallbackKeyEvent() { return &fallback_event_; } - - void Install(controls::Control* control); - void Uninstall(); - - private: - void OnKeyDown(events::KeyEventArgs& event); - - private: - std::unordered_map> map_; - - std::vector empty_list_; - - int current_id_ = 1; - - Event fallback_event_; - - EventRevokerListGuard event_guard_; -}; -} // namespace cru::ui::helper diff --git a/include/cru/ui/host/LayoutPaintCycler.h b/include/cru/ui/host/LayoutPaintCycler.h new file mode 100644 index 00000000..e95ed81d --- /dev/null +++ b/include/cru/ui/host/LayoutPaintCycler.h @@ -0,0 +1,40 @@ +#pragma once +#include "../Base.h" + +#include "cru/platform/gui/TimerHelper.h" +#include "cru/platform/gui/UiApplication.h" + +#include + +namespace cru::ui::host { +class CRU_UI_API LayoutPaintCycler { + public: + explicit LayoutPaintCycler(WindowHost* host); + + CRU_DELETE_COPY(LayoutPaintCycler) + CRU_DELETE_MOVE(LayoutPaintCycler) + + ~LayoutPaintCycler(); + + public: + void InvalidateLayout(); + void InvalidatePaint(); + + bool IsLayoutDirty() { return layout_dirty_; } + + private: + void OnCycle(); + + private: + WindowHost* host_; + + platform::gui::TimerAutoCanceler timer_canceler_; + + bool layout_dirty_ = true; + bool paint_dirty_ = true; + + std::chrono::steady_clock::time_point last_cycle_time_; + std::chrono::steady_clock::duration cycle_threshold_ = + std::chrono::milliseconds(1000) / 144; +}; +} // namespace cru::ui::host diff --git a/include/cru/ui/host/LayoutPaintCycler.hpp b/include/cru/ui/host/LayoutPaintCycler.hpp deleted file mode 100644 index 1d44ad06..00000000 --- a/include/cru/ui/host/LayoutPaintCycler.hpp +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once -#include "../Base.hpp" - -#include "cru/platform/gui/TimerHelper.hpp" -#include "cru/platform/gui/UiApplication.hpp" - -#include - -namespace cru::ui::host { -class CRU_UI_API LayoutPaintCycler { - public: - explicit LayoutPaintCycler(WindowHost* host); - - CRU_DELETE_COPY(LayoutPaintCycler) - CRU_DELETE_MOVE(LayoutPaintCycler) - - ~LayoutPaintCycler(); - - public: - void InvalidateLayout(); - void InvalidatePaint(); - - bool IsLayoutDirty() { return layout_dirty_; } - - private: - void OnCycle(); - - private: - WindowHost* host_; - - platform::gui::TimerAutoCanceler timer_canceler_; - - bool layout_dirty_ = true; - bool paint_dirty_ = true; - - std::chrono::steady_clock::time_point last_cycle_time_; - std::chrono::steady_clock::duration cycle_threshold_ = - std::chrono::milliseconds(1000) / 144; -}; -} // namespace cru::ui::host diff --git a/include/cru/ui/host/WindowHost.h b/include/cru/ui/host/WindowHost.h new file mode 100644 index 00000000..caa50397 --- /dev/null +++ b/include/cru/ui/host/WindowHost.h @@ -0,0 +1,164 @@ +#pragma once +#include "../Base.h" + +#include "../render/Base.h" +#include "cru/common/Event.h" +#include "cru/platform/gui/Cursor.h" +#include "cru/platform/gui/UiApplication.h" +#include "cru/platform/gui/Window.h" + +#include +#include +#include + +namespace cru::ui::host { +class LayoutPaintCycler; + +struct AfterLayoutEventArgs {}; + +// The bridge between control tree and native window. +class CRU_UI_API WindowHost : public Object { + CRU_DEFINE_CLASS_LOG_TAG(u"cru::ui::host::WindowHost") + + public: + explicit WindowHost(controls::Control* root_control); + + CRU_DELETE_COPY(WindowHost) + CRU_DELETE_MOVE(WindowHost) + + ~WindowHost() override; + + public: + platform::gui::INativeWindow* GetNativeWindow() { return native_window_; } + + // Mark the layout as invalid, and arrange a re-layout later. + // This method could be called more than one times in a message cycle. But + // layout only takes place once. + void InvalidateLayout(); + + // Mark the paint as invalid, and arrange a re-paint later. + // This method could be called more than one times in a message cycle. But + // paint only takes place once. + void InvalidatePaint(); + + IEvent* AfterLayoutEvent() { + return &after_layout_event_; + } + + void Relayout(); + void RelayoutWithSize(const Size& available_size = Size::Infinate(), + bool set_window_size_to_fit_content = false); + + void Repaint(); + + // Is layout is invalid, wait for relayout and then run the action. Otherwist + // run it right now. + void RunAfterLayoutStable(std::function action); + + // If true, preferred size of root render object is set to window size when + // measure. Default is true. + bool IsLayoutPreferToFillWindow() const; + void SetLayoutPreferToFillWindow(bool value); + + // Get current control that mouse hovers on. This ignores the mouse-capture + // control. Even when mouse is captured by another control, this function + // return the control under cursor. You can use `GetMouseCaptureControl` to + // get more info. + controls::Control* GetMouseHoverControl() const { + return mouse_hover_control_; + } + + //*************** region: focus *************** + + controls::Control* GetFocusControl(); + + void SetFocusControl(controls::Control* control); + + //*************** region: focus *************** + + // Pass nullptr to release capture. If mouse is already capture by a control, + // this capture will fail and return false. If control is identical to the + // capturing control, capture is not changed and this function will return + // true. + // + // When capturing control changes, + // appropriate event will be sent. If mouse is not on the capturing control + // and capture is released, mouse enter event will be sent to the mouse-hover + // control. If mouse is not on the capturing control and capture is set, mouse + // leave event will be sent to the mouse-hover control. + bool CaptureMouseFor(controls::Control* control); + + // Return null if not captured. + controls::Control* GetMouseCaptureControl(); + + controls::Control* HitTest(const Point& point); + + void UpdateCursor(); + + IEvent* NativeWindowChangeEvent() { + return &native_window_change_event_; + } + + std::shared_ptr GetOverrideCursor(); + void SetOverrideCursor(std::shared_ptr cursor); + + private: + gsl::not_null CreateNativeWindow(); + + //*************** region: native messages *************** + void OnNativeDestroy(platform::gui::INativeWindow* window, std::nullptr_t); + void OnNativePaint(platform::gui::INativeWindow* window, std::nullptr_t); + void OnNativeResize(platform::gui::INativeWindow* window, const Size& size); + + void OnNativeFocus(platform::gui::INativeWindow* window, + cru::platform::gui::FocusChangeType focus); + + void OnNativeMouseEnterLeave(platform::gui::INativeWindow* window, + cru::platform::gui::MouseEnterLeaveType enter); + void OnNativeMouseMove(platform::gui::INativeWindow* window, + const Point& point); + void OnNativeMouseDown(platform::gui::INativeWindow* window, + const platform::gui::NativeMouseButtonEventArgs& args); + void OnNativeMouseUp(platform::gui::INativeWindow* window, + const platform::gui::NativeMouseButtonEventArgs& args); + void OnNativeMouseWheel(platform::gui::INativeWindow* window, + const platform::gui::NativeMouseWheelEventArgs& args); + + void OnNativeKeyDown(platform::gui::INativeWindow* window, + const platform::gui::NativeKeyEventArgs& args); + void OnNativeKeyUp(platform::gui::INativeWindow* window, + const platform::gui::NativeKeyEventArgs& args); + + //*************** region: event dispatcher helper *************** + + void DispatchMouseHoverControlChangeEvent(controls::Control* old_control, + controls::Control* new_control, + const Point& point, bool no_leave, + bool no_enter); + + private: + controls::Control* root_control_ = nullptr; + render::RenderObject* root_render_object_ = nullptr; + + platform::gui::INativeWindow* native_window_ = nullptr; + + std::unique_ptr layout_paint_cycler_; + + Event after_layout_event_; + std::vector > after_layout_stable_action_; + + std::vector event_revoker_guards_; + + controls::Control* mouse_hover_control_ = nullptr; + + controls::Control* focus_control_; + + controls::Control* mouse_captured_control_ = nullptr; + + bool layout_prefer_to_fill_window_ = false; + + Event native_window_change_event_; + + std::shared_ptr override_cursor_; +}; +} // namespace cru::ui::host diff --git a/include/cru/ui/host/WindowHost.hpp b/include/cru/ui/host/WindowHost.hpp deleted file mode 100644 index 7832174c..00000000 --- a/include/cru/ui/host/WindowHost.hpp +++ /dev/null @@ -1,164 +0,0 @@ -#pragma once -#include "../Base.hpp" - -#include "../render/Base.hpp" -#include "cru/common/Event.hpp" -#include "cru/platform/gui/Cursor.hpp" -#include "cru/platform/gui/UiApplication.hpp" -#include "cru/platform/gui/Window.hpp" - -#include -#include -#include - -namespace cru::ui::host { -class LayoutPaintCycler; - -struct AfterLayoutEventArgs {}; - -// The bridge between control tree and native window. -class CRU_UI_API WindowHost : public Object { - CRU_DEFINE_CLASS_LOG_TAG(u"cru::ui::host::WindowHost") - - public: - explicit WindowHost(controls::Control* root_control); - - CRU_DELETE_COPY(WindowHost) - CRU_DELETE_MOVE(WindowHost) - - ~WindowHost() override; - - public: - platform::gui::INativeWindow* GetNativeWindow() { return native_window_; } - - // Mark the layout as invalid, and arrange a re-layout later. - // This method could be called more than one times in a message cycle. But - // layout only takes place once. - void InvalidateLayout(); - - // Mark the paint as invalid, and arrange a re-paint later. - // This method could be called more than one times in a message cycle. But - // paint only takes place once. - void InvalidatePaint(); - - IEvent* AfterLayoutEvent() { - return &after_layout_event_; - } - - void Relayout(); - void RelayoutWithSize(const Size& available_size = Size::Infinate(), - bool set_window_size_to_fit_content = false); - - void Repaint(); - - // Is layout is invalid, wait for relayout and then run the action. Otherwist - // run it right now. - void RunAfterLayoutStable(std::function action); - - // If true, preferred size of root render object is set to window size when - // measure. Default is true. - bool IsLayoutPreferToFillWindow() const; - void SetLayoutPreferToFillWindow(bool value); - - // Get current control that mouse hovers on. This ignores the mouse-capture - // control. Even when mouse is captured by another control, this function - // return the control under cursor. You can use `GetMouseCaptureControl` to - // get more info. - controls::Control* GetMouseHoverControl() const { - return mouse_hover_control_; - } - - //*************** region: focus *************** - - controls::Control* GetFocusControl(); - - void SetFocusControl(controls::Control* control); - - //*************** region: focus *************** - - // Pass nullptr to release capture. If mouse is already capture by a control, - // this capture will fail and return false. If control is identical to the - // capturing control, capture is not changed and this function will return - // true. - // - // When capturing control changes, - // appropriate event will be sent. If mouse is not on the capturing control - // and capture is released, mouse enter event will be sent to the mouse-hover - // control. If mouse is not on the capturing control and capture is set, mouse - // leave event will be sent to the mouse-hover control. - bool CaptureMouseFor(controls::Control* control); - - // Return null if not captured. - controls::Control* GetMouseCaptureControl(); - - controls::Control* HitTest(const Point& point); - - void UpdateCursor(); - - IEvent* NativeWindowChangeEvent() { - return &native_window_change_event_; - } - - std::shared_ptr GetOverrideCursor(); - void SetOverrideCursor(std::shared_ptr cursor); - - private: - gsl::not_null CreateNativeWindow(); - - //*************** region: native messages *************** - void OnNativeDestroy(platform::gui::INativeWindow* window, std::nullptr_t); - void OnNativePaint(platform::gui::INativeWindow* window, std::nullptr_t); - void OnNativeResize(platform::gui::INativeWindow* window, const Size& size); - - void OnNativeFocus(platform::gui::INativeWindow* window, - cru::platform::gui::FocusChangeType focus); - - void OnNativeMouseEnterLeave(platform::gui::INativeWindow* window, - cru::platform::gui::MouseEnterLeaveType enter); - void OnNativeMouseMove(platform::gui::INativeWindow* window, - const Point& point); - void OnNativeMouseDown(platform::gui::INativeWindow* window, - const platform::gui::NativeMouseButtonEventArgs& args); - void OnNativeMouseUp(platform::gui::INativeWindow* window, - const platform::gui::NativeMouseButtonEventArgs& args); - void OnNativeMouseWheel(platform::gui::INativeWindow* window, - const platform::gui::NativeMouseWheelEventArgs& args); - - void OnNativeKeyDown(platform::gui::INativeWindow* window, - const platform::gui::NativeKeyEventArgs& args); - void OnNativeKeyUp(platform::gui::INativeWindow* window, - const platform::gui::NativeKeyEventArgs& args); - - //*************** region: event dispatcher helper *************** - - void DispatchMouseHoverControlChangeEvent(controls::Control* old_control, - controls::Control* new_control, - const Point& point, bool no_leave, - bool no_enter); - - private: - controls::Control* root_control_ = nullptr; - render::RenderObject* root_render_object_ = nullptr; - - platform::gui::INativeWindow* native_window_ = nullptr; - - std::unique_ptr layout_paint_cycler_; - - Event after_layout_event_; - std::vector > after_layout_stable_action_; - - std::vector event_revoker_guards_; - - controls::Control* mouse_hover_control_ = nullptr; - - controls::Control* focus_control_; - - controls::Control* mouse_captured_control_ = nullptr; - - bool layout_prefer_to_fill_window_ = false; - - Event native_window_change_event_; - - std::shared_ptr override_cursor_; -}; -} // namespace cru::ui::host diff --git a/include/cru/ui/mapper/BorderStyleMapper.h b/include/cru/ui/mapper/BorderStyleMapper.h new file mode 100644 index 00000000..d585639f --- /dev/null +++ b/include/cru/ui/mapper/BorderStyleMapper.h @@ -0,0 +1,20 @@ +#pragma once +#include "Mapper.h" +#include "cru/ui/style/ApplyBorderStyleInfo.h" +#include "cru/xml/XmlNode.h" + +namespace cru::ui::mapper { +class CRU_UI_API BorderStyleMapper + : public BasicMapper { + public: + CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(BorderStyleMapper) + + public: + bool SupportMapFromXml() override { return true; } + bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; + + protected: + ui::style::ApplyBorderStyleInfo DoMapFromXml( + xml::XmlElementNode* node) override; +}; +} // namespace cru::ui::mapper diff --git a/include/cru/ui/mapper/BorderStyleMapper.hpp b/include/cru/ui/mapper/BorderStyleMapper.hpp deleted file mode 100644 index 7a574f38..00000000 --- a/include/cru/ui/mapper/BorderStyleMapper.hpp +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once -#include "Mapper.hpp" -#include "cru/ui/style/ApplyBorderStyleInfo.hpp" -#include "cru/xml/XmlNode.hpp" - -namespace cru::ui::mapper { -class CRU_UI_API BorderStyleMapper - : public BasicMapper { - public: - CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(BorderStyleMapper) - - public: - bool SupportMapFromXml() override { return true; } - bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; - - protected: - ui::style::ApplyBorderStyleInfo DoMapFromXml( - xml::XmlElementNode* node) override; -}; -} // namespace cru::ui::mapper diff --git a/include/cru/ui/mapper/BrushMapper.h b/include/cru/ui/mapper/BrushMapper.h new file mode 100644 index 00000000..be70e89e --- /dev/null +++ b/include/cru/ui/mapper/BrushMapper.h @@ -0,0 +1,20 @@ +#pragma once +#include "Mapper.h" +#include "cru/common/Base.h" +#include "cru/platform/graphics/Brush.h" +#include "cru/xml/XmlNode.h" + +namespace cru::ui::mapper { +class BrushMapper : public BasicRefMapper { + public: + CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(BrushMapper) + + public: + bool SupportMapFromXml() override { return true; } + bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; + + protected: + std::shared_ptr DoMapFromXml( + xml::XmlElementNode* node) override; +}; +} // namespace cru::ui::mapper diff --git a/include/cru/ui/mapper/BrushMapper.hpp b/include/cru/ui/mapper/BrushMapper.hpp deleted file mode 100644 index b2021076..00000000 --- a/include/cru/ui/mapper/BrushMapper.hpp +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once -#include "Mapper.hpp" -#include "cru/common/Base.hpp" -#include "cru/platform/graphics/Brush.hpp" -#include "cru/xml/XmlNode.hpp" - -namespace cru::ui::mapper { -class BrushMapper : public BasicRefMapper { - public: - CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(BrushMapper) - - public: - bool SupportMapFromXml() override { return true; } - bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; - - protected: - std::shared_ptr DoMapFromXml( - xml::XmlElementNode* node) override; -}; -} // namespace cru::ui::mapper diff --git a/include/cru/ui/mapper/ColorMapper.h b/include/cru/ui/mapper/ColorMapper.h new file mode 100644 index 00000000..785f1ca9 --- /dev/null +++ b/include/cru/ui/mapper/ColorMapper.h @@ -0,0 +1,19 @@ +#pragma once +#include "Mapper.h" +#include "cru/xml/XmlNode.h" + +namespace cru::ui::mapper { +class CRU_UI_API ColorMapper : public BasicMapper { + public: + CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(ColorMapper) + + public: + bool SupportMapFromString() override { return true; } + bool SupportMapFromXml() override { return true; } + bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; + + protected: + Color DoMapFromString(String str) override; + Color DoMapFromXml(xml::XmlElementNode* node) override; +}; +} // namespace cru::ui::mapper diff --git a/include/cru/ui/mapper/ColorMapper.hpp b/include/cru/ui/mapper/ColorMapper.hpp deleted file mode 100644 index d96ca74b..00000000 --- a/include/cru/ui/mapper/ColorMapper.hpp +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once -#include "Mapper.hpp" -#include "cru/xml/XmlNode.hpp" - -namespace cru::ui::mapper { -class CRU_UI_API ColorMapper : public BasicMapper { - public: - CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(ColorMapper) - - public: - bool SupportMapFromString() override { return true; } - bool SupportMapFromXml() override { return true; } - bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; - - protected: - Color DoMapFromString(String str) override; - Color DoMapFromXml(xml::XmlElementNode* node) override; -}; -} // namespace cru::ui::mapper diff --git a/include/cru/ui/mapper/CornerRadiusMapper.h b/include/cru/ui/mapper/CornerRadiusMapper.h new file mode 100644 index 00000000..b8998a1b --- /dev/null +++ b/include/cru/ui/mapper/CornerRadiusMapper.h @@ -0,0 +1,17 @@ +#pragma once + +#include "Mapper.h" + +namespace cru::ui::mapper { +class CRU_UI_API CornerRadiusMapper : public BasicMapper { + public: + CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(CornerRadiusMapper) + + public: + bool SupportMapFromXml() override { return true; } + bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; + + protected: + CornerRadius DoMapFromXml(xml::XmlElementNode* node) override; +}; +} // namespace cru::ui::mapper diff --git a/include/cru/ui/mapper/CornerRadiusMapper.hpp b/include/cru/ui/mapper/CornerRadiusMapper.hpp deleted file mode 100644 index 4d83f2de..00000000 --- a/include/cru/ui/mapper/CornerRadiusMapper.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include "Mapper.hpp" - -namespace cru::ui::mapper { -class CRU_UI_API CornerRadiusMapper : public BasicMapper { - public: - CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(CornerRadiusMapper) - - public: - bool SupportMapFromXml() override { return true; } - bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; - - protected: - CornerRadius DoMapFromXml(xml::XmlElementNode* node) override; -}; -} // namespace cru::ui::mapper diff --git a/include/cru/ui/mapper/CursorMapper.h b/include/cru/ui/mapper/CursorMapper.h new file mode 100644 index 00000000..d98a798c --- /dev/null +++ b/include/cru/ui/mapper/CursorMapper.h @@ -0,0 +1,21 @@ +#pragma once +#include "Mapper.h" +#include "cru/platform/gui/Cursor.h" +#include "cru/xml/XmlNode.h" + +namespace cru::ui::mapper { +class CRU_UI_API CursorMapper : public BasicRefMapper { + public: + CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(CursorMapper); + + public: + bool SupportMapFromString() override { return true; } + bool SupportMapFromXml() override { return true; } + bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; + + protected: + std::shared_ptr DoMapFromString(String str) override; + std::shared_ptr DoMapFromXml( + xml::XmlElementNode* node) override; +}; +} // namespace cru::ui::mapper diff --git a/include/cru/ui/mapper/CursorMapper.hpp b/include/cru/ui/mapper/CursorMapper.hpp deleted file mode 100644 index 6cb61d87..00000000 --- a/include/cru/ui/mapper/CursorMapper.hpp +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once -#include "Mapper.hpp" -#include "cru/platform/gui/Cursor.hpp" -#include "cru/xml/XmlNode.hpp" - -namespace cru::ui::mapper { -class CRU_UI_API CursorMapper : public BasicRefMapper { - public: - CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(CursorMapper); - - public: - bool SupportMapFromString() override { return true; } - bool SupportMapFromXml() override { return true; } - bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; - - protected: - std::shared_ptr DoMapFromString(String str) override; - std::shared_ptr DoMapFromXml( - xml::XmlElementNode* node) override; -}; -} // namespace cru::ui::mapper diff --git a/include/cru/ui/mapper/FontMapper.h b/include/cru/ui/mapper/FontMapper.h new file mode 100644 index 00000000..545b94d8 --- /dev/null +++ b/include/cru/ui/mapper/FontMapper.h @@ -0,0 +1,19 @@ +#pragma once +#include "Mapper.h" +#include "cru/common/Base.h" +#include "cru/platform/graphics/Font.h" + +namespace cru::ui::mapper { +class FontMapper : public BasicRefMapper { + public: + CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(FontMapper) + + public: + bool SupportMapFromXml() override { return true; } + bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; + + protected: + std::shared_ptr DoMapFromXml( + xml::XmlElementNode* node) override; +}; +} // namespace cru::ui::mapper diff --git a/include/cru/ui/mapper/FontMapper.hpp b/include/cru/ui/mapper/FontMapper.hpp deleted file mode 100644 index ad8c43b5..00000000 --- a/include/cru/ui/mapper/FontMapper.hpp +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once -#include "Mapper.hpp" -#include "cru/common/Base.hpp" -#include "cru/platform/graphics/Font.hpp" - -namespace cru::ui::mapper { -class FontMapper : public BasicRefMapper { - public: - CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(FontMapper) - - public: - bool SupportMapFromXml() override { return true; } - bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; - - protected: - std::shared_ptr DoMapFromXml( - xml::XmlElementNode* node) override; -}; -} // namespace cru::ui::mapper diff --git a/include/cru/ui/mapper/Mapper.h b/include/cru/ui/mapper/Mapper.h new file mode 100644 index 00000000..fa7f8cef --- /dev/null +++ b/include/cru/ui/mapper/Mapper.h @@ -0,0 +1,92 @@ +#pragma once +#include "../Base.h" + +#include "cru/common/ClonablePtr.h" +#include "cru/common/Exception.h" +#include "cru/xml/XmlNode.h" + +#include +#include +#include +#include + +namespace cru::ui::mapper { +template +class BasicMapper; + +class CRU_UI_API MapperBase : public Object { + public: + explicit MapperBase(std::type_index type_index); + + CRU_DELETE_COPY(MapperBase) + CRU_DELETE_MOVE(MapperBase) + + ~MapperBase() override = default; + + public: + std::type_index GetTypeIndex() const { return type_index_; } + + template + BasicMapper* StaticCast() { + return static_cast*>(this); + } + + template + BasicMapper* DynamicCast() { + return dynamic_cast*>(this); + } + + virtual bool SupportMapFromString() { return false; } + virtual bool SupportMapFromXml() { return false; } + virtual bool XmlElementIsOfThisType(xml::XmlElementNode* node) { + return false; + } + + private: + std::type_index type_index_; +}; + +template +class CRU_UI_API BasicMapper : public MapperBase { + public: + static_assert(std::is_default_constructible_v, + "T must be default constructible."); + + BasicMapper() : MapperBase(typeid(T)) {} + + CRU_DELETE_COPY(BasicMapper) + CRU_DELETE_MOVE(BasicMapper) + + ~BasicMapper() override = default; + + virtual T MapFromString(String str) { + if (!SupportMapFromString()) { + throw Exception(u"This mapper does not support map from string."); + } + + return DoMapFromString(str); + } + + T MapFromXml(xml::XmlElementNode* node) { + if (!SupportMapFromXml()) { + throw Exception(u"This mapper does not support map from xml."); + } + + if (!XmlElementIsOfThisType(node)) { + throw Exception(u"This xml element is not of mapping type."); + } + + return DoMapFromXml(node); + } + + protected: + virtual T DoMapFromString(String str) { return {}; } + virtual T DoMapFromXml(xml::XmlElementNode* node) { return {}; } +}; + +template +using BasicRefMapper = BasicMapper>; + +template +using BasicPtrMapper = BasicMapper>; +} // namespace cru::ui::mapper diff --git a/include/cru/ui/mapper/Mapper.hpp b/include/cru/ui/mapper/Mapper.hpp deleted file mode 100644 index c52bec70..00000000 --- a/include/cru/ui/mapper/Mapper.hpp +++ /dev/null @@ -1,92 +0,0 @@ -#pragma once -#include "../Base.hpp" - -#include "cru/common/ClonablePtr.hpp" -#include "cru/common/Exception.hpp" -#include "cru/xml/XmlNode.hpp" - -#include -#include -#include -#include - -namespace cru::ui::mapper { -template -class BasicMapper; - -class CRU_UI_API MapperBase : public Object { - public: - explicit MapperBase(std::type_index type_index); - - CRU_DELETE_COPY(MapperBase) - CRU_DELETE_MOVE(MapperBase) - - ~MapperBase() override = default; - - public: - std::type_index GetTypeIndex() const { return type_index_; } - - template - BasicMapper* StaticCast() { - return static_cast*>(this); - } - - template - BasicMapper* DynamicCast() { - return dynamic_cast*>(this); - } - - virtual bool SupportMapFromString() { return false; } - virtual bool SupportMapFromXml() { return false; } - virtual bool XmlElementIsOfThisType(xml::XmlElementNode* node) { - return false; - } - - private: - std::type_index type_index_; -}; - -template -class CRU_UI_API BasicMapper : public MapperBase { - public: - static_assert(std::is_default_constructible_v, - "T must be default constructible."); - - BasicMapper() : MapperBase(typeid(T)) {} - - CRU_DELETE_COPY(BasicMapper) - CRU_DELETE_MOVE(BasicMapper) - - ~BasicMapper() override = default; - - virtual T MapFromString(String str) { - if (!SupportMapFromString()) { - throw Exception(u"This mapper does not support map from string."); - } - - return DoMapFromString(str); - } - - T MapFromXml(xml::XmlElementNode* node) { - if (!SupportMapFromXml()) { - throw Exception(u"This mapper does not support map from xml."); - } - - if (!XmlElementIsOfThisType(node)) { - throw Exception(u"This xml element is not of mapping type."); - } - - return DoMapFromXml(node); - } - - protected: - virtual T DoMapFromString(String str) { return {}; } - virtual T DoMapFromXml(xml::XmlElementNode* node) { return {}; } -}; - -template -using BasicRefMapper = BasicMapper>; - -template -using BasicPtrMapper = BasicMapper>; -} // namespace cru::ui::mapper diff --git a/include/cru/ui/mapper/MapperRegistry.h b/include/cru/ui/mapper/MapperRegistry.h new file mode 100644 index 00000000..29d26156 --- /dev/null +++ b/include/cru/ui/mapper/MapperRegistry.h @@ -0,0 +1,58 @@ +#pragma once + +#include "Mapper.h" + +#include +#include + +namespace cru::ui::mapper { +class CRU_UI_API MapperRegistry { + public: + static MapperRegistry* GetInstance(); + + MapperRegistry(); + + CRU_DELETE_COPY(MapperRegistry) + CRU_DELETE_MOVE(MapperRegistry) + + ~MapperRegistry(); + + const std::vector& GetAllMappers() const { return mapper_list_; } + + template + BasicMapper* GetMapper() const { + for (auto mapper : mapper_list_) { + if (mapper->GetTypeIndex() == typeid(T)) { + return static_cast*>(mapper); + } + } + return nullptr; + } + + template + BasicRefMapper* GetRefMapper() const { + return GetMapper>(); + } + + template + BasicPtrMapper* GetPtrMapper() const { + return GetMapper>(); + } + + template + std::vector GetMappersByInterface() const { + std::vector result; + for (auto mapper : mapper_list_) { + auto m = dynamic_cast(mapper); + if (m) result.push_back(m); + } + return result; + } + + void RegisterMapper(MapperBase* mapper); + void UnregisterMapper(MapperBase* mapper); + + private: + std::vector mapper_list_; +}; +} // namespace cru::ui::mapper diff --git a/include/cru/ui/mapper/MapperRegistry.hpp b/include/cru/ui/mapper/MapperRegistry.hpp deleted file mode 100644 index 1c02be49..00000000 --- a/include/cru/ui/mapper/MapperRegistry.hpp +++ /dev/null @@ -1,58 +0,0 @@ -#pragma once - -#include "Mapper.hpp" - -#include -#include - -namespace cru::ui::mapper { -class CRU_UI_API MapperRegistry { - public: - static MapperRegistry* GetInstance(); - - MapperRegistry(); - - CRU_DELETE_COPY(MapperRegistry) - CRU_DELETE_MOVE(MapperRegistry) - - ~MapperRegistry(); - - const std::vector& GetAllMappers() const { return mapper_list_; } - - template - BasicMapper* GetMapper() const { - for (auto mapper : mapper_list_) { - if (mapper->GetTypeIndex() == typeid(T)) { - return static_cast*>(mapper); - } - } - return nullptr; - } - - template - BasicRefMapper* GetRefMapper() const { - return GetMapper>(); - } - - template - BasicPtrMapper* GetPtrMapper() const { - return GetMapper>(); - } - - template - std::vector GetMappersByInterface() const { - std::vector result; - for (auto mapper : mapper_list_) { - auto m = dynamic_cast(mapper); - if (m) result.push_back(m); - } - return result; - } - - void RegisterMapper(MapperBase* mapper); - void UnregisterMapper(MapperBase* mapper); - - private: - std::vector mapper_list_; -}; -} // namespace cru::ui::mapper diff --git a/include/cru/ui/mapper/PointMapper.h b/include/cru/ui/mapper/PointMapper.h new file mode 100644 index 00000000..83dd135b --- /dev/null +++ b/include/cru/ui/mapper/PointMapper.h @@ -0,0 +1,18 @@ +#pragma once +#include "Mapper.h" + +namespace cru::ui::mapper { +class CRU_UI_API PointMapper : public BasicMapper { + public: + CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(PointMapper) + + public: + bool SupportMapFromString() override { return true; } + bool SupportMapFromXml() override { return true; } + bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; + + protected: + Point DoMapFromString(String str) override; + Point DoMapFromXml(xml::XmlElementNode* node) override; +}; +} // namespace cru::ui::mapper diff --git a/include/cru/ui/mapper/PointMapper.hpp b/include/cru/ui/mapper/PointMapper.hpp deleted file mode 100644 index b1837fe1..00000000 --- a/include/cru/ui/mapper/PointMapper.hpp +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once -#include "Mapper.hpp" - -namespace cru::ui::mapper { -class CRU_UI_API PointMapper : public BasicMapper { - public: - CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(PointMapper) - - public: - bool SupportMapFromString() override { return true; } - bool SupportMapFromXml() override { return true; } - bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; - - protected: - Point DoMapFromString(String str) override; - Point DoMapFromXml(xml::XmlElementNode* node) override; -}; -} // namespace cru::ui::mapper diff --git a/include/cru/ui/mapper/SizeMapper.h b/include/cru/ui/mapper/SizeMapper.h new file mode 100644 index 00000000..6cf3eb9a --- /dev/null +++ b/include/cru/ui/mapper/SizeMapper.h @@ -0,0 +1,18 @@ +#pragma once +#include "Mapper.h" + +namespace cru::ui::mapper { +class CRU_UI_API SizeMapper : public BasicMapper { + public: + CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(SizeMapper) + + public: + bool SupportMapFromString() override { return true; } + bool SupportMapFromXml() override { return true; } + bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; + + protected: + Size DoMapFromString(String str) override; + Size DoMapFromXml(xml::XmlElementNode* node) override; +}; +} // namespace cru::ui::mapper diff --git a/include/cru/ui/mapper/SizeMapper.hpp b/include/cru/ui/mapper/SizeMapper.hpp deleted file mode 100644 index 6ab4b97d..00000000 --- a/include/cru/ui/mapper/SizeMapper.hpp +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once -#include "Mapper.hpp" - -namespace cru::ui::mapper { -class CRU_UI_API SizeMapper : public BasicMapper { - public: - CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(SizeMapper) - - public: - bool SupportMapFromString() override { return true; } - bool SupportMapFromXml() override { return true; } - bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; - - protected: - Size DoMapFromString(String str) override; - Size DoMapFromXml(xml::XmlElementNode* node) override; -}; -} // namespace cru::ui::mapper diff --git a/include/cru/ui/mapper/ThicknessMapper.h b/include/cru/ui/mapper/ThicknessMapper.h new file mode 100644 index 00000000..10f6c87e --- /dev/null +++ b/include/cru/ui/mapper/ThicknessMapper.h @@ -0,0 +1,22 @@ +#pragma once +#include "Mapper.h" + +#include "../Base.h" +#include "cru/common/Base.h" +#include "cru/xml/XmlNode.h" + +namespace cru::ui::mapper { +class CRU_UI_API ThicknessMapper : public BasicMapper { + public: + CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(ThicknessMapper) + + public: + bool SupportMapFromString() override { return true; } + bool SupportMapFromXml() override { return true; } + bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; + + protected: + Thickness DoMapFromString(String str) override; + Thickness DoMapFromXml(xml::XmlElementNode* node) override; +}; +} // namespace cru::ui::mapper diff --git a/include/cru/ui/mapper/ThicknessMapper.hpp b/include/cru/ui/mapper/ThicknessMapper.hpp deleted file mode 100644 index 4ffbeb9b..00000000 --- a/include/cru/ui/mapper/ThicknessMapper.hpp +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once -#include "Mapper.hpp" - -#include "../Base.hpp" -#include "cru/common/Base.hpp" -#include "cru/xml/XmlNode.hpp" - -namespace cru::ui::mapper { -class CRU_UI_API ThicknessMapper : public BasicMapper { - public: - CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(ThicknessMapper) - - public: - bool SupportMapFromString() override { return true; } - bool SupportMapFromXml() override { return true; } - bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; - - protected: - Thickness DoMapFromString(String str) override; - Thickness DoMapFromXml(xml::XmlElementNode* node) override; -}; -} // namespace cru::ui::mapper diff --git a/include/cru/ui/mapper/style/AndConditionMapper.h b/include/cru/ui/mapper/style/AndConditionMapper.h new file mode 100644 index 00000000..142637a7 --- /dev/null +++ b/include/cru/ui/mapper/style/AndConditionMapper.h @@ -0,0 +1,25 @@ +#pragma once +#include "../Mapper.h" +#include "IConditionMapper.h" + +namespace cru::ui::mapper::style { +class CRU_UI_API AndConditionMapper + : public BasicPtrMapper, + public virtual IConditionMapper { + public: + CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(AndConditionMapper) + + public: + bool SupportMapFromXml() override { return true; } + bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; + + ClonablePtr MapConditionFromXml( + xml::XmlElementNode* node) override { + return MapFromXml(node); + } + + protected: + ClonablePtr DoMapFromXml( + xml::XmlElementNode* node) override; +}; +} // namespace cru::ui::mapper::style diff --git a/include/cru/ui/mapper/style/AndConditionMapper.hpp b/include/cru/ui/mapper/style/AndConditionMapper.hpp deleted file mode 100644 index 747e496d..00000000 --- a/include/cru/ui/mapper/style/AndConditionMapper.hpp +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once -#include "../Mapper.hpp" -#include "IConditionMapper.hpp" - -namespace cru::ui::mapper::style { -class CRU_UI_API AndConditionMapper - : public BasicPtrMapper, - public virtual IConditionMapper { - public: - CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(AndConditionMapper) - - public: - bool SupportMapFromXml() override { return true; } - bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; - - ClonablePtr MapConditionFromXml( - xml::XmlElementNode* node) override { - return MapFromXml(node); - } - - protected: - ClonablePtr DoMapFromXml( - xml::XmlElementNode* node) override; -}; -} // namespace cru::ui::mapper::style diff --git a/include/cru/ui/mapper/style/BorderStylerMapper.h b/include/cru/ui/mapper/style/BorderStylerMapper.h new file mode 100644 index 00000000..da09989b --- /dev/null +++ b/include/cru/ui/mapper/style/BorderStylerMapper.h @@ -0,0 +1,28 @@ +#pragma once +#include "../Mapper.h" +#include "cru/common/ClonablePtr.h" +#include "cru/ui/mapper/style/IStylerMapper.h" +#include "cru/ui/style/Styler.h" +#include "cru/xml/XmlNode.h" + +namespace cru::ui::mapper::style { +class CRU_UI_API BorderStylerMapper + : public BasicPtrMapper, + public virtual IStylerMapper { + public: + CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(BorderStylerMapper) + + public: + bool SupportMapFromXml() override { return true; } + bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; + + ClonablePtr MapStylerFromXml( + xml::XmlElementNode* node) override { + return MapFromXml(node); + } + + protected: + ClonablePtr DoMapFromXml( + xml::XmlElementNode* node) override; +}; +} // namespace cru::ui::mapper::style diff --git a/include/cru/ui/mapper/style/BorderStylerMapper.hpp b/include/cru/ui/mapper/style/BorderStylerMapper.hpp deleted file mode 100644 index 2f48550c..00000000 --- a/include/cru/ui/mapper/style/BorderStylerMapper.hpp +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once -#include "../Mapper.hpp" -#include "cru/common/ClonablePtr.hpp" -#include "cru/ui/mapper/style/IStylerMapper.hpp" -#include "cru/ui/style/Styler.hpp" -#include "cru/xml/XmlNode.hpp" - -namespace cru::ui::mapper::style { -class CRU_UI_API BorderStylerMapper - : public BasicPtrMapper, - public virtual IStylerMapper { - public: - CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(BorderStylerMapper) - - public: - bool SupportMapFromXml() override { return true; } - bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; - - ClonablePtr MapStylerFromXml( - xml::XmlElementNode* node) override { - return MapFromXml(node); - } - - protected: - ClonablePtr DoMapFromXml( - xml::XmlElementNode* node) override; -}; -} // namespace cru::ui::mapper::style diff --git a/include/cru/ui/mapper/style/ClickStateConditionMapper.h b/include/cru/ui/mapper/style/ClickStateConditionMapper.h new file mode 100644 index 00000000..0c25646c --- /dev/null +++ b/include/cru/ui/mapper/style/ClickStateConditionMapper.h @@ -0,0 +1,26 @@ +#pragma once +#include "../Mapper.h" +#include "IConditionMapper.h" +#include "cru/ui/style/Condition.h" + +namespace cru::ui::mapper::style { +class CRU_UI_API ClickStateConditionMapper + : public BasicPtrMapper, + public virtual IConditionMapper { + public: + CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(ClickStateConditionMapper) + + public: + bool SupportMapFromXml() override { return true; } + bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; + + ClonablePtr MapConditionFromXml( + xml::XmlElementNode* node) override { + return MapFromXml(node); + } + + public: + ClonablePtr DoMapFromXml( + xml::XmlElementNode* node) override; +}; +} // namespace cru::ui::mapper::style diff --git a/include/cru/ui/mapper/style/ClickStateConditionMapper.hpp b/include/cru/ui/mapper/style/ClickStateConditionMapper.hpp deleted file mode 100644 index 63577641..00000000 --- a/include/cru/ui/mapper/style/ClickStateConditionMapper.hpp +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once -#include "../Mapper.hpp" -#include "IConditionMapper.hpp" -#include "cru/ui/style/Condition.hpp" - -namespace cru::ui::mapper::style { -class CRU_UI_API ClickStateConditionMapper - : public BasicPtrMapper, - public virtual IConditionMapper { - public: - CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(ClickStateConditionMapper) - - public: - bool SupportMapFromXml() override { return true; } - bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; - - ClonablePtr MapConditionFromXml( - xml::XmlElementNode* node) override { - return MapFromXml(node); - } - - public: - ClonablePtr DoMapFromXml( - xml::XmlElementNode* node) override; -}; -} // namespace cru::ui::mapper::style diff --git a/include/cru/ui/mapper/style/CursorStylerMapper.h b/include/cru/ui/mapper/style/CursorStylerMapper.h new file mode 100644 index 00000000..37c3ade9 --- /dev/null +++ b/include/cru/ui/mapper/style/CursorStylerMapper.h @@ -0,0 +1,26 @@ +#pragma once +#include "../Mapper.h" +#include "cru/ui/mapper/style/IStylerMapper.h" +#include "cru/ui/style/Styler.h" + +namespace cru::ui::mapper::style { +class CRU_UI_API CursorStylerMapper + : public BasicPtrMapper, + public virtual IStylerMapper { + public: + CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(CursorStylerMapper) + + public: + bool SupportMapFromXml() override { return true; } + bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; + + ClonablePtr MapStylerFromXml( + xml::XmlElementNode* node) override { + return MapFromXml(node); + } + + protected: + ClonablePtr DoMapFromXml( + xml::XmlElementNode* node) override; +}; +} // namespace cru::ui::mapper::style diff --git a/include/cru/ui/mapper/style/CursorStylerMapper.hpp b/include/cru/ui/mapper/style/CursorStylerMapper.hpp deleted file mode 100644 index 2b0c62a8..00000000 --- a/include/cru/ui/mapper/style/CursorStylerMapper.hpp +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once -#include "../Mapper.hpp" -#include "cru/ui/mapper/style/IStylerMapper.hpp" -#include "cru/ui/style/Styler.hpp" - -namespace cru::ui::mapper::style { -class CRU_UI_API CursorStylerMapper - : public BasicPtrMapper, - public virtual IStylerMapper { - public: - CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(CursorStylerMapper) - - public: - bool SupportMapFromXml() override { return true; } - bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; - - ClonablePtr MapStylerFromXml( - xml::XmlElementNode* node) override { - return MapFromXml(node); - } - - protected: - ClonablePtr DoMapFromXml( - xml::XmlElementNode* node) override; -}; -} // namespace cru::ui::mapper::style diff --git a/include/cru/ui/mapper/style/FocusConditionMapper.h b/include/cru/ui/mapper/style/FocusConditionMapper.h new file mode 100644 index 00000000..bc7264d1 --- /dev/null +++ b/include/cru/ui/mapper/style/FocusConditionMapper.h @@ -0,0 +1,26 @@ +#pragma once +#include "../Mapper.h" +#include "IConditionMapper.h" +#include "cru/ui/style/Condition.h" + +namespace cru::ui::mapper::style { +class CRU_UI_API FocusConditionMapper + : public BasicPtrMapper, + public virtual IConditionMapper { + public: + CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(FocusConditionMapper) + + public: + bool SupportMapFromXml() override { return true; } + bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; + + ClonablePtr MapConditionFromXml( + xml::XmlElementNode* node) override { + return MapFromXml(node); + } + + protected: + ClonablePtr DoMapFromXml( + xml::XmlElementNode* node) override; +}; +} // namespace cru::ui::mapper::style diff --git a/include/cru/ui/mapper/style/FocusConditionMapper.hpp b/include/cru/ui/mapper/style/FocusConditionMapper.hpp deleted file mode 100644 index 00809850..00000000 --- a/include/cru/ui/mapper/style/FocusConditionMapper.hpp +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once -#include "../Mapper.hpp" -#include "IConditionMapper.hpp" -#include "cru/ui/style/Condition.hpp" - -namespace cru::ui::mapper::style { -class CRU_UI_API FocusConditionMapper - : public BasicPtrMapper, - public virtual IConditionMapper { - public: - CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(FocusConditionMapper) - - public: - bool SupportMapFromXml() override { return true; } - bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; - - ClonablePtr MapConditionFromXml( - xml::XmlElementNode* node) override { - return MapFromXml(node); - } - - protected: - ClonablePtr DoMapFromXml( - xml::XmlElementNode* node) override; -}; -} // namespace cru::ui::mapper::style diff --git a/include/cru/ui/mapper/style/HoverConditionMapper.h b/include/cru/ui/mapper/style/HoverConditionMapper.h new file mode 100644 index 00000000..c897f672 --- /dev/null +++ b/include/cru/ui/mapper/style/HoverConditionMapper.h @@ -0,0 +1,26 @@ +#pragma once +#include "../Mapper.h" +#include "IConditionMapper.h" +#include "cru/ui/style/Condition.h" + +namespace cru::ui::mapper::style { +class CRU_UI_API HoverConditionMapper + : public BasicPtrMapper, + public virtual IConditionMapper { + public: + CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(HoverConditionMapper) + + public: + bool SupportMapFromXml() override { return true; } + bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; + + ClonablePtr MapConditionFromXml( + xml::XmlElementNode* node) override { + return MapFromXml(node); + } + + protected: + ClonablePtr DoMapFromXml( + xml::XmlElementNode* node) override; +}; +} // namespace cru::ui::mapper::style diff --git a/include/cru/ui/mapper/style/HoverConditionMapper.hpp b/include/cru/ui/mapper/style/HoverConditionMapper.hpp deleted file mode 100644 index bbd840f0..00000000 --- a/include/cru/ui/mapper/style/HoverConditionMapper.hpp +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once -#include "../Mapper.hpp" -#include "IConditionMapper.hpp" -#include "cru/ui/style/Condition.hpp" - -namespace cru::ui::mapper::style { -class CRU_UI_API HoverConditionMapper - : public BasicPtrMapper, - public virtual IConditionMapper { - public: - CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(HoverConditionMapper) - - public: - bool SupportMapFromXml() override { return true; } - bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; - - ClonablePtr MapConditionFromXml( - xml::XmlElementNode* node) override { - return MapFromXml(node); - } - - protected: - ClonablePtr DoMapFromXml( - xml::XmlElementNode* node) override; -}; -} // namespace cru::ui::mapper::style diff --git a/include/cru/ui/mapper/style/IConditionMapper.h b/include/cru/ui/mapper/style/IConditionMapper.h new file mode 100644 index 00000000..7610dc8d --- /dev/null +++ b/include/cru/ui/mapper/style/IConditionMapper.h @@ -0,0 +1,17 @@ +#pragma once +#include "../../Base.h" +#include "cru/common/ClonablePtr.h" +#include "cru/ui/mapper/Mapper.h" +#include "cru/ui/style/Condition.h" +#include "cru/xml/XmlNode.h" + +namespace cru::ui::mapper::style { +struct CRU_UI_API IConditionMapper : virtual Interface { + bool XmlElementIsOfThisType(xml::XmlElementNode* node) { + return dynamic_cast(this)->XmlElementIsOfThisType(node); + } + + virtual ClonablePtr MapConditionFromXml( + xml::XmlElementNode* node) = 0; +}; +} // namespace cru::ui::mapper::style diff --git a/include/cru/ui/mapper/style/IConditionMapper.hpp b/include/cru/ui/mapper/style/IConditionMapper.hpp deleted file mode 100644 index 3fa7d10a..00000000 --- a/include/cru/ui/mapper/style/IConditionMapper.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once -#include "../../Base.hpp" -#include "cru/common/ClonablePtr.hpp" -#include "cru/ui/mapper/Mapper.hpp" -#include "cru/ui/style/Condition.hpp" -#include "cru/xml/XmlNode.hpp" - -namespace cru::ui::mapper::style { -struct CRU_UI_API IConditionMapper : virtual Interface { - bool XmlElementIsOfThisType(xml::XmlElementNode* node) { - return dynamic_cast(this)->XmlElementIsOfThisType(node); - } - - virtual ClonablePtr MapConditionFromXml( - xml::XmlElementNode* node) = 0; -}; -} // namespace cru::ui::mapper::style diff --git a/include/cru/ui/mapper/style/IStylerMapper.h b/include/cru/ui/mapper/style/IStylerMapper.h new file mode 100644 index 00000000..4c2ecd16 --- /dev/null +++ b/include/cru/ui/mapper/style/IStylerMapper.h @@ -0,0 +1,17 @@ +#pragma once +#include "../../Base.h" +#include "cru/common/ClonablePtr.h" +#include "cru/ui/mapper/Mapper.h" +#include "cru/ui/style/Styler.h" +#include "cru/xml/XmlNode.h" + +namespace cru::ui::mapper::style { +struct CRU_UI_API IStylerMapper : virtual Interface { + bool XmlElementIsOfThisType(xml::XmlElementNode* node) { + return dynamic_cast(this)->XmlElementIsOfThisType(node); + } + + virtual ClonablePtr MapStylerFromXml( + xml::XmlElementNode* node) = 0; +}; +} // namespace cru::ui::mapper::style diff --git a/include/cru/ui/mapper/style/IStylerMapper.hpp b/include/cru/ui/mapper/style/IStylerMapper.hpp deleted file mode 100644 index d00f39be..00000000 --- a/include/cru/ui/mapper/style/IStylerMapper.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once -#include "../../Base.hpp" -#include "cru/common/ClonablePtr.hpp" -#include "cru/ui/mapper/Mapper.hpp" -#include "cru/ui/style/Styler.hpp" -#include "cru/xml/XmlNode.hpp" - -namespace cru::ui::mapper::style { -struct CRU_UI_API IStylerMapper : virtual Interface { - bool XmlElementIsOfThisType(xml::XmlElementNode* node) { - return dynamic_cast(this)->XmlElementIsOfThisType(node); - } - - virtual ClonablePtr MapStylerFromXml( - xml::XmlElementNode* node) = 0; -}; -} // namespace cru::ui::mapper::style diff --git a/include/cru/ui/mapper/style/NoConditionMapper.h b/include/cru/ui/mapper/style/NoConditionMapper.h new file mode 100644 index 00000000..bc7ce623 --- /dev/null +++ b/include/cru/ui/mapper/style/NoConditionMapper.h @@ -0,0 +1,29 @@ +#pragma once +#include "../Mapper.h" +#include "IConditionMapper.h" +#include "cru/common/Base.h" +#include "cru/common/ClonablePtr.h" +#include "cru/ui/style/Condition.h" +#include "cru/xml/XmlNode.h" + +namespace cru::ui::mapper::style { +class CRU_UI_API NoConditionMapper + : public BasicPtrMapper, + public virtual IConditionMapper { + public: + CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(NoConditionMapper) + + public: + bool SupportMapFromXml() override { return true; } + bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; + + ClonablePtr MapConditionFromXml( + xml::XmlElementNode* node) override { + return MapFromXml(node); + } + + protected: + ClonablePtr DoMapFromXml( + xml::XmlElementNode* node) override; +}; +} // namespace cru::ui::mapper::style diff --git a/include/cru/ui/mapper/style/NoConditionMapper.hpp b/include/cru/ui/mapper/style/NoConditionMapper.hpp deleted file mode 100644 index ed3a9699..00000000 --- a/include/cru/ui/mapper/style/NoConditionMapper.hpp +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once -#include "../Mapper.hpp" -#include "IConditionMapper.hpp" -#include "cru/common/Base.hpp" -#include "cru/common/ClonablePtr.hpp" -#include "cru/ui/style/Condition.hpp" -#include "cru/xml/XmlNode.hpp" - -namespace cru::ui::mapper::style { -class CRU_UI_API NoConditionMapper - : public BasicPtrMapper, - public virtual IConditionMapper { - public: - CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(NoConditionMapper) - - public: - bool SupportMapFromXml() override { return true; } - bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; - - ClonablePtr MapConditionFromXml( - xml::XmlElementNode* node) override { - return MapFromXml(node); - } - - protected: - ClonablePtr DoMapFromXml( - xml::XmlElementNode* node) override; -}; -} // namespace cru::ui::mapper::style diff --git a/include/cru/ui/mapper/style/OrConditionMapper.h b/include/cru/ui/mapper/style/OrConditionMapper.h new file mode 100644 index 00000000..e93484b4 --- /dev/null +++ b/include/cru/ui/mapper/style/OrConditionMapper.h @@ -0,0 +1,25 @@ +#pragma once +#include "../Mapper.h" +#include "IConditionMapper.h" + +namespace cru::ui::mapper::style { +class CRU_UI_API OrConditionMapper + : public BasicPtrMapper, + public virtual IConditionMapper { + public: + CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(OrConditionMapper) + + public: + bool SupportMapFromXml() override { return true; } + bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; + + ClonablePtr MapConditionFromXml( + xml::XmlElementNode* node) override { + return MapFromXml(node); + } + + protected: + ClonablePtr DoMapFromXml( + xml::XmlElementNode* node) override; +}; +} // namespace cru::ui::mapper::style diff --git a/include/cru/ui/mapper/style/OrConditionMapper.hpp b/include/cru/ui/mapper/style/OrConditionMapper.hpp deleted file mode 100644 index 21a402c2..00000000 --- a/include/cru/ui/mapper/style/OrConditionMapper.hpp +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once -#include "../Mapper.hpp" -#include "IConditionMapper.hpp" - -namespace cru::ui::mapper::style { -class CRU_UI_API OrConditionMapper - : public BasicPtrMapper, - public virtual IConditionMapper { - public: - CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(OrConditionMapper) - - public: - bool SupportMapFromXml() override { return true; } - bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; - - ClonablePtr MapConditionFromXml( - xml::XmlElementNode* node) override { - return MapFromXml(node); - } - - protected: - ClonablePtr DoMapFromXml( - xml::XmlElementNode* node) override; -}; -} // namespace cru::ui::mapper::style diff --git a/include/cru/ui/mapper/style/StyleRuleMapper.h b/include/cru/ui/mapper/style/StyleRuleMapper.h new file mode 100644 index 00000000..cba4872c --- /dev/null +++ b/include/cru/ui/mapper/style/StyleRuleMapper.h @@ -0,0 +1,20 @@ +#pragma once +#include "../Mapper.h" +#include "cru/common/ClonablePtr.h" +#include "cru/ui/style/StyleRule.h" +#include "cru/xml/XmlNode.h" + +namespace cru::ui::mapper::style { +class CRU_UI_API StyleRuleMapper : public BasicPtrMapper { + public: + CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(StyleRuleMapper) + + public: + bool SupportMapFromXml() override { return true; } + bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; + + protected: + ClonablePtr DoMapFromXml( + xml::XmlElementNode* node) override; +}; +} // namespace cru::ui::mapper::style diff --git a/include/cru/ui/mapper/style/StyleRuleMapper.hpp b/include/cru/ui/mapper/style/StyleRuleMapper.hpp deleted file mode 100644 index ce5ccf00..00000000 --- a/include/cru/ui/mapper/style/StyleRuleMapper.hpp +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once -#include "../Mapper.hpp" -#include "cru/common/ClonablePtr.hpp" -#include "cru/ui/style/StyleRule.hpp" -#include "cru/xml/XmlNode.hpp" - -namespace cru::ui::mapper::style { -class CRU_UI_API StyleRuleMapper : public BasicPtrMapper { - public: - CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(StyleRuleMapper) - - public: - bool SupportMapFromXml() override { return true; } - bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; - - protected: - ClonablePtr DoMapFromXml( - xml::XmlElementNode* node) override; -}; -} // namespace cru::ui::mapper::style diff --git a/include/cru/ui/mapper/style/StyleRuleSetMapper.h b/include/cru/ui/mapper/style/StyleRuleSetMapper.h new file mode 100644 index 00000000..95f745e5 --- /dev/null +++ b/include/cru/ui/mapper/style/StyleRuleSetMapper.h @@ -0,0 +1,21 @@ +#pragma once +#include +#include "../../style/StyleRuleSet.h" +#include "../Mapper.h" + +namespace cru::ui::mapper::style { +class CRU_UI_API StyleRuleSetMapper + : public BasicRefMapper { + public: + CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(StyleRuleSetMapper) + + public: + bool SupportMapFromXml() override { return true; } + bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; + + protected: + std::shared_ptr DoMapFromXml( + xml::XmlElementNode* node) override; +}; +; +} // namespace cru::ui::mapper::style diff --git a/include/cru/ui/mapper/style/StyleRuleSetMapper.hpp b/include/cru/ui/mapper/style/StyleRuleSetMapper.hpp deleted file mode 100644 index 66b95f4a..00000000 --- a/include/cru/ui/mapper/style/StyleRuleSetMapper.hpp +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once -#include -#include "../../style/StyleRuleSet.hpp" -#include "../Mapper.hpp" - -namespace cru::ui::mapper::style { -class CRU_UI_API StyleRuleSetMapper - : public BasicRefMapper { - public: - CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(StyleRuleSetMapper) - - public: - bool SupportMapFromXml() override { return true; } - bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; - - protected: - std::shared_ptr DoMapFromXml( - xml::XmlElementNode* node) override; -}; -; -} // namespace cru::ui::mapper::style diff --git a/include/cru/ui/render/Base.h b/include/cru/ui/render/Base.h new file mode 100644 index 00000000..fca67086 --- /dev/null +++ b/include/cru/ui/render/Base.h @@ -0,0 +1,12 @@ +#pragma once +#include "../Base.h" + +namespace cru::ui::render { +class RenderObject; +class BorderRenderObject; +class CanvasRenderObject; +class FlexLayoutRenderObject; +class ScrollRenderObject; +class StackLayoutRenderObject; +class TextRenderObject; +} // namespace cru::ui::render diff --git a/include/cru/ui/render/Base.hpp b/include/cru/ui/render/Base.hpp deleted file mode 100644 index ac67349e..00000000 --- a/include/cru/ui/render/Base.hpp +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once -#include "../Base.hpp" - -namespace cru::ui::render { -class RenderObject; -class BorderRenderObject; -class CanvasRenderObject; -class FlexLayoutRenderObject; -class ScrollRenderObject; -class StackLayoutRenderObject; -class TextRenderObject; -} // namespace cru::ui::render diff --git a/include/cru/ui/render/BorderRenderObject.h b/include/cru/ui/render/BorderRenderObject.h new file mode 100644 index 00000000..d75a979f --- /dev/null +++ b/include/cru/ui/render/BorderRenderObject.h @@ -0,0 +1,109 @@ +#pragma once +#include +#include "../style/ApplyBorderStyleInfo.h" +#include "RenderObject.h" +#include "cru/platform/GraphicsBase.h" +#include "cru/ui/Base.h" + +namespace cru::ui::render { +class CRU_UI_API BorderRenderObject : public RenderObject { + CRU_DEFINE_CLASS_LOG_TAG(u"cru::ui::render::BorderRenderObject") + + public: + BorderRenderObject(); + BorderRenderObject(const BorderRenderObject& other) = delete; + BorderRenderObject(BorderRenderObject&& other) = delete; + BorderRenderObject& operator=(const BorderRenderObject& other) = delete; + BorderRenderObject& operator=(BorderRenderObject&& other) = delete; + ~BorderRenderObject() override; + + bool IsBorderEnabled() const { return is_border_enabled_; } + void SetBorderEnabled(bool enabled) { is_border_enabled_ = enabled; } + + std::shared_ptr GetBorderBrush() { + return border_brush_; + } + + void SetBorderBrush(std::shared_ptr brush) { + if (brush == border_brush_) return; + border_brush_ = std::move(brush); + InvalidatePaint(); + } + + Thickness GetBorderThickness() const { return border_thickness_; } + + void SetBorderThickness(const Thickness thickness) { + if (thickness == border_thickness_) return; + border_thickness_ = thickness; + InvalidateLayout(); + } + + CornerRadius GetBorderRadius() { return border_radius_; } + + void SetBorderRadius(const CornerRadius radius) { + if (radius == border_radius_) return; + border_radius_ = radius; + RecreateGeometry(); + } + + std::shared_ptr GetForegroundBrush() { + return foreground_brush_; + } + + void SetForegroundBrush(std::shared_ptr brush) { + if (brush == foreground_brush_) return; + foreground_brush_ = std::move(brush); + InvalidatePaint(); + } + + std::shared_ptr GetBackgroundBrush() { + return background_brush_; + } + + void SetBackgroundBrush(std::shared_ptr brush) { + if (brush == background_brush_) return; + background_brush_ = std::move(brush); + InvalidatePaint(); + } + + void ApplyBorderStyle(const style::ApplyBorderStyleInfo& style); + + RenderObject* HitTest(const Point& point) override; + + Thickness GetOuterSpaceThickness() const override; + Rect GetPaddingRect() const override; + Rect GetContentRect() const override; + + std::u16string_view GetName() const override { return u"BorderRenderObject"; } + + protected: + void OnDrawCore(platform::graphics::IPainter* painter) override; + + Size OnMeasureContent(const MeasureRequirement& requirement, + const MeasureSize& preferred_size) override; + void OnLayoutContent(const Rect& content_rect) override; + + void OnAfterLayout() override; + + private: + void RecreateGeometry(); + + private: + bool is_border_enabled_ = false; + + std::shared_ptr border_brush_; + Thickness border_thickness_; + CornerRadius border_radius_; + + std::shared_ptr foreground_brush_; + std::shared_ptr background_brush_; + + // The ring. Used for painting. + std::unique_ptr geometry_; + // Area including inner area of the border. Used for painting foreground and + // background. + std::unique_ptr border_inner_geometry_; + // Area including border ring and inner area. Used for hit test. + std::unique_ptr border_outer_geometry_; +}; +} // namespace cru::ui::render diff --git a/include/cru/ui/render/BorderRenderObject.hpp b/include/cru/ui/render/BorderRenderObject.hpp deleted file mode 100644 index bf4b27a1..00000000 --- a/include/cru/ui/render/BorderRenderObject.hpp +++ /dev/null @@ -1,109 +0,0 @@ -#pragma once -#include -#include "../style/ApplyBorderStyleInfo.hpp" -#include "RenderObject.hpp" -#include "cru/platform/GraphicsBase.hpp" -#include "cru/ui/Base.hpp" - -namespace cru::ui::render { -class CRU_UI_API BorderRenderObject : public RenderObject { - CRU_DEFINE_CLASS_LOG_TAG(u"cru::ui::render::BorderRenderObject") - - public: - BorderRenderObject(); - BorderRenderObject(const BorderRenderObject& other) = delete; - BorderRenderObject(BorderRenderObject&& other) = delete; - BorderRenderObject& operator=(const BorderRenderObject& other) = delete; - BorderRenderObject& operator=(BorderRenderObject&& other) = delete; - ~BorderRenderObject() override; - - bool IsBorderEnabled() const { return is_border_enabled_; } - void SetBorderEnabled(bool enabled) { is_border_enabled_ = enabled; } - - std::shared_ptr GetBorderBrush() { - return border_brush_; - } - - void SetBorderBrush(std::shared_ptr brush) { - if (brush == border_brush_) return; - border_brush_ = std::move(brush); - InvalidatePaint(); - } - - Thickness GetBorderThickness() const { return border_thickness_; } - - void SetBorderThickness(const Thickness thickness) { - if (thickness == border_thickness_) return; - border_thickness_ = thickness; - InvalidateLayout(); - } - - CornerRadius GetBorderRadius() { return border_radius_; } - - void SetBorderRadius(const CornerRadius radius) { - if (radius == border_radius_) return; - border_radius_ = radius; - RecreateGeometry(); - } - - std::shared_ptr GetForegroundBrush() { - return foreground_brush_; - } - - void SetForegroundBrush(std::shared_ptr brush) { - if (brush == foreground_brush_) return; - foreground_brush_ = std::move(brush); - InvalidatePaint(); - } - - std::shared_ptr GetBackgroundBrush() { - return background_brush_; - } - - void SetBackgroundBrush(std::shared_ptr brush) { - if (brush == background_brush_) return; - background_brush_ = std::move(brush); - InvalidatePaint(); - } - - void ApplyBorderStyle(const style::ApplyBorderStyleInfo& style); - - RenderObject* HitTest(const Point& point) override; - - Thickness GetOuterSpaceThickness() const override; - Rect GetPaddingRect() const override; - Rect GetContentRect() const override; - - std::u16string_view GetName() const override { return u"BorderRenderObject"; } - - protected: - void OnDrawCore(platform::graphics::IPainter* painter) override; - - Size OnMeasureContent(const MeasureRequirement& requirement, - const MeasureSize& preferred_size) override; - void OnLayoutContent(const Rect& content_rect) override; - - void OnAfterLayout() override; - - private: - void RecreateGeometry(); - - private: - bool is_border_enabled_ = false; - - std::shared_ptr border_brush_; - Thickness border_thickness_; - CornerRadius border_radius_; - - std::shared_ptr foreground_brush_; - std::shared_ptr background_brush_; - - // The ring. Used for painting. - std::unique_ptr geometry_; - // Area including inner area of the border. Used for painting foreground and - // background. - std::unique_ptr border_inner_geometry_; - // Area including border ring and inner area. Used for hit test. - std::unique_ptr border_outer_geometry_; -}; -} // namespace cru::ui::render diff --git a/include/cru/ui/render/CanvasRenderObject.h b/include/cru/ui/render/CanvasRenderObject.h new file mode 100644 index 00000000..ca55ebc6 --- /dev/null +++ b/include/cru/ui/render/CanvasRenderObject.h @@ -0,0 +1,36 @@ +#pragma once +#include "RenderObject.h" + +namespace cru::ui::render { +// Layout logic: +// If no preferred size is set. Then (100, 100) is used and then coerced to +// required range. +class CRU_UI_API CanvasRenderObject : public RenderObject { + public: + CanvasRenderObject(); + + CRU_DELETE_COPY(CanvasRenderObject) + CRU_DELETE_MOVE(CanvasRenderObject) + + ~CanvasRenderObject(); + + public: + RenderObject* HitTest(const Point& point) override; + + Size GetDesiredSize() const { return desired_size_; } + + IEvent* PaintEvent() { return &paint_event_; } + + protected: + void OnDrawContent(platform::graphics::IPainter* painter) override; + + Size OnMeasureContent(const MeasureRequirement& requirement, + const MeasureSize& preferred_size) override; + void OnLayoutContent(const Rect& content_rect) override; + + private: + Size desired_size_{}; + + Event paint_event_; +}; +} // namespace cru::ui::render diff --git a/include/cru/ui/render/CanvasRenderObject.hpp b/include/cru/ui/render/CanvasRenderObject.hpp deleted file mode 100644 index 68400271..00000000 --- a/include/cru/ui/render/CanvasRenderObject.hpp +++ /dev/null @@ -1,36 +0,0 @@ -#pragma once -#include "RenderObject.hpp" - -namespace cru::ui::render { -// Layout logic: -// If no preferred size is set. Then (100, 100) is used and then coerced to -// required range. -class CRU_UI_API CanvasRenderObject : public RenderObject { - public: - CanvasRenderObject(); - - CRU_DELETE_COPY(CanvasRenderObject) - CRU_DELETE_MOVE(CanvasRenderObject) - - ~CanvasRenderObject(); - - public: - RenderObject* HitTest(const Point& point) override; - - Size GetDesiredSize() const { return desired_size_; } - - IEvent* PaintEvent() { return &paint_event_; } - - protected: - void OnDrawContent(platform::graphics::IPainter* painter) override; - - Size OnMeasureContent(const MeasureRequirement& requirement, - const MeasureSize& preferred_size) override; - void OnLayoutContent(const Rect& content_rect) override; - - private: - Size desired_size_{}; - - Event paint_event_; -}; -} // namespace cru::ui::render diff --git a/include/cru/ui/render/FlexLayoutRenderObject.h b/include/cru/ui/render/FlexLayoutRenderObject.h new file mode 100644 index 00000000..3a8348f6 --- /dev/null +++ b/include/cru/ui/render/FlexLayoutRenderObject.h @@ -0,0 +1,120 @@ +#pragma once +#include "LayoutRenderObject.h" + +#include + +namespace cru::ui::render { +// Measure Logic (v0.1): +// Cross axis measure logic is the same as stack layout. +// +// 1. Layout all children with unspecified(infinate) max main axis length. +// +// 2. Add up main axis length of children to get total main length. +// +// 2.1. If preferred main axis length of parent is specified, then compare +// total length with it. If bigger, shrink is performed. If smaller, expand is +// performed. And preferred main axis length is used as target length. +// +// 2.2. If preferred main axis length of parent is not specified. +// +// 2.2.1. If max main axis length is specified and total length is bigger +// than max main axis length, shrink is performed and max main axis length is +// used as target length. +// +// 2.2.2. Or if max main axis length is specified and total length is smaller +// than max main axis length and any child has a positive expand factor, then +// expand is performed and max main axis length is used as target length. +// +// 2.2.3. Or if min main axis length is specified and total length is smaller +// than min main axis length, expand is performed and min main axis length is +// used as target length. +// +// 3. If shrink or expand is needed, then +// +// 3.1. Shrink: +// +// 3.1.1. Find out all shrink_factor > 0 children to form a adjusting list. +// +// 3.1.2. Use total main length minus target length to get the total shrink +// length. Add up all shrink_factor in adjusting list to get total shrink +// factor. +// +// 3.1.3. Iterate all children in adjusting list: +// +// 3.1.3.1. Calculate its own shrink length as +// shrink_factor / total_shrink_factor * total_shrink_length . +// Use current main axis length of child minus shrink length to get new +// measure length. +// +// 3.1.3.2. If min main axis length of the child is specified and new +// measure length is less than it, then min main axis length is used as new +// measure length. +// +// 3.1.3.3. Or if new measure length is less than 0, then it is coerced to +// 0. +// +// 3.1.3.4. Call measure with max and preferred main axis length set to new +// measure length. Cross axis length requirement is the same as step 1. +// +// 3.1.3.5. After measure, if it has min main axis length specified and +// actual main axis length is equal to it or its actual main axis length is +// 0, then remove it from adjusting list. +// +// 3.1.4. Add up main axis length of children to update total main length. If +// total main length is less than or equal to target length, goto step 4. +// +// 3.1.4. If adjusting list is not empty, go to step 3.1.2. +// +// 3.2. Expand: +// The same as shrink after you exchange related things except some minor +// things like do not do special things on 0. +// +// 4. If final total main axis length exceeeds the max main axis length (if +// specified), then report an error. And result main axis length is the coerced +// length. If final total main axis length is smaller than min main axis length +// (if specified), then coerce the length to the min value but not report error +// and just fill the rest space with blank. +// +class CRU_UI_API FlexLayoutRenderObject : public LayoutRenderObject { + CRU_DEFINE_CLASS_LOG_TAG(u"cru::ui::render::FlexLayoutRenderObject") + + public: + FlexLayoutRenderObject() = default; + FlexLayoutRenderObject(const FlexLayoutRenderObject& other) = delete; + FlexLayoutRenderObject& operator=(const FlexLayoutRenderObject& other) = + delete; + FlexLayoutRenderObject(FlexLayoutRenderObject&& other) = delete; + FlexLayoutRenderObject& operator=(FlexLayoutRenderObject&& other) = delete; + ~FlexLayoutRenderObject() override = default; + + std::u16string_view GetName() const override; + + FlexDirection GetFlexDirection() const { return direction_; } + void SetFlexDirection(FlexDirection direction) { + direction_ = direction; + InvalidateLayout(); + } + + FlexMainAlignment GetContentMainAlign() const { return content_main_align_; } + void SetContentMainAlign(FlexMainAlignment align) { + content_main_align_ = align; + InvalidateLayout(); + } + + FlexCrossAlignment GetItemCrossAlign() const { return item_cross_align_; } + void SetItemCrossAlign(FlexCrossAlignment align) { + item_cross_align_ = align; + InvalidateLayout(); + } + + protected: + Size OnMeasureContent(const MeasureRequirement& requirement, + const MeasureSize& preferred_size) override; + void OnLayoutContent(const Rect& content_rect) override; + + private: + FlexDirection direction_ = FlexDirection::Horizontal; + FlexMainAlignment content_main_align_ = FlexMainAlignment::Start; + FlexCrossAlignment item_cross_align_ = FlexCrossAlignment::Center; +}; +} // namespace cru::ui::render diff --git a/include/cru/ui/render/FlexLayoutRenderObject.hpp b/include/cru/ui/render/FlexLayoutRenderObject.hpp deleted file mode 100644 index c6fb7211..00000000 --- a/include/cru/ui/render/FlexLayoutRenderObject.hpp +++ /dev/null @@ -1,120 +0,0 @@ -#pragma once -#include "LayoutRenderObject.hpp" - -#include - -namespace cru::ui::render { -// Measure Logic (v0.1): -// Cross axis measure logic is the same as stack layout. -// -// 1. Layout all children with unspecified(infinate) max main axis length. -// -// 2. Add up main axis length of children to get total main length. -// -// 2.1. If preferred main axis length of parent is specified, then compare -// total length with it. If bigger, shrink is performed. If smaller, expand is -// performed. And preferred main axis length is used as target length. -// -// 2.2. If preferred main axis length of parent is not specified. -// -// 2.2.1. If max main axis length is specified and total length is bigger -// than max main axis length, shrink is performed and max main axis length is -// used as target length. -// -// 2.2.2. Or if max main axis length is specified and total length is smaller -// than max main axis length and any child has a positive expand factor, then -// expand is performed and max main axis length is used as target length. -// -// 2.2.3. Or if min main axis length is specified and total length is smaller -// than min main axis length, expand is performed and min main axis length is -// used as target length. -// -// 3. If shrink or expand is needed, then -// -// 3.1. Shrink: -// -// 3.1.1. Find out all shrink_factor > 0 children to form a adjusting list. -// -// 3.1.2. Use total main length minus target length to get the total shrink -// length. Add up all shrink_factor in adjusting list to get total shrink -// factor. -// -// 3.1.3. Iterate all children in adjusting list: -// -// 3.1.3.1. Calculate its own shrink length as -// shrink_factor / total_shrink_factor * total_shrink_length . -// Use current main axis length of child minus shrink length to get new -// measure length. -// -// 3.1.3.2. If min main axis length of the child is specified and new -// measure length is less than it, then min main axis length is used as new -// measure length. -// -// 3.1.3.3. Or if new measure length is less than 0, then it is coerced to -// 0. -// -// 3.1.3.4. Call measure with max and preferred main axis length set to new -// measure length. Cross axis length requirement is the same as step 1. -// -// 3.1.3.5. After measure, if it has min main axis length specified and -// actual main axis length is equal to it or its actual main axis length is -// 0, then remove it from adjusting list. -// -// 3.1.4. Add up main axis length of children to update total main length. If -// total main length is less than or equal to target length, goto step 4. -// -// 3.1.4. If adjusting list is not empty, go to step 3.1.2. -// -// 3.2. Expand: -// The same as shrink after you exchange related things except some minor -// things like do not do special things on 0. -// -// 4. If final total main axis length exceeeds the max main axis length (if -// specified), then report an error. And result main axis length is the coerced -// length. If final total main axis length is smaller than min main axis length -// (if specified), then coerce the length to the min value but not report error -// and just fill the rest space with blank. -// -class CRU_UI_API FlexLayoutRenderObject : public LayoutRenderObject { - CRU_DEFINE_CLASS_LOG_TAG(u"cru::ui::render::FlexLayoutRenderObject") - - public: - FlexLayoutRenderObject() = default; - FlexLayoutRenderObject(const FlexLayoutRenderObject& other) = delete; - FlexLayoutRenderObject& operator=(const FlexLayoutRenderObject& other) = - delete; - FlexLayoutRenderObject(FlexLayoutRenderObject&& other) = delete; - FlexLayoutRenderObject& operator=(FlexLayoutRenderObject&& other) = delete; - ~FlexLayoutRenderObject() override = default; - - std::u16string_view GetName() const override; - - FlexDirection GetFlexDirection() const { return direction_; } - void SetFlexDirection(FlexDirection direction) { - direction_ = direction; - InvalidateLayout(); - } - - FlexMainAlignment GetContentMainAlign() const { return content_main_align_; } - void SetContentMainAlign(FlexMainAlignment align) { - content_main_align_ = align; - InvalidateLayout(); - } - - FlexCrossAlignment GetItemCrossAlign() const { return item_cross_align_; } - void SetItemCrossAlign(FlexCrossAlignment align) { - item_cross_align_ = align; - InvalidateLayout(); - } - - protected: - Size OnMeasureContent(const MeasureRequirement& requirement, - const MeasureSize& preferred_size) override; - void OnLayoutContent(const Rect& content_rect) override; - - private: - FlexDirection direction_ = FlexDirection::Horizontal; - FlexMainAlignment content_main_align_ = FlexMainAlignment::Start; - FlexCrossAlignment item_cross_align_ = FlexCrossAlignment::Center; -}; -} // namespace cru::ui::render diff --git a/include/cru/ui/render/LayoutHelper.h b/include/cru/ui/render/LayoutHelper.h new file mode 100644 index 00000000..c2377066 --- /dev/null +++ b/include/cru/ui/render/LayoutHelper.h @@ -0,0 +1,14 @@ +#pragma once +#include "Base.h" + +#include "MeasureRequirement.h" + +namespace cru::ui::render { +float CalculateAnchorByAlignment(Alignment alignment, float start_point, + float content_length, float child_length); + +MeasureLength StackLayoutCalculateChildMaxLength( + MeasureLength parent_preferred_size, MeasureLength parent_max_size, + MeasureLength child_min_size, std::u16string_view log_tag, + std::u16string_view exceeds_message); +} // namespace cru::ui::render diff --git a/include/cru/ui/render/LayoutHelper.hpp b/include/cru/ui/render/LayoutHelper.hpp deleted file mode 100644 index 518dc5a3..00000000 --- a/include/cru/ui/render/LayoutHelper.hpp +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once -#include "Base.hpp" - -#include "MeasureRequirement.hpp" - -namespace cru::ui::render { -float CalculateAnchorByAlignment(Alignment alignment, float start_point, - float content_length, float child_length); - -MeasureLength StackLayoutCalculateChildMaxLength( - MeasureLength parent_preferred_size, MeasureLength parent_max_size, - MeasureLength child_min_size, std::u16string_view log_tag, - std::u16string_view exceeds_message); -} // namespace cru::ui::render diff --git a/include/cru/ui/render/LayoutRenderObject.h b/include/cru/ui/render/LayoutRenderObject.h new file mode 100644 index 00000000..42a3aa55 --- /dev/null +++ b/include/cru/ui/render/LayoutRenderObject.h @@ -0,0 +1,86 @@ +#pragma once +#include "RenderObject.h" + +#include "cru/platform/graphics/util/Painter.h" + +namespace cru::ui::render { +template +class CRU_UI_API LayoutRenderObject : public RenderObject { + public: + using ChildLayoutData = TChildLayoutData; + + protected: + LayoutRenderObject() : RenderObject(ChildMode::Multiple) {} + + public: + CRU_DELETE_COPY(LayoutRenderObject) + CRU_DELETE_MOVE(LayoutRenderObject) + + ~LayoutRenderObject() override = default; + + const std::vector& GetChildLayoutDataList() const { + return this->child_layout_data_; + } + + void SetChildLayoutData(Index position, ChildLayoutData data) { + Expects(position >= 0 && + position < static_cast(this->child_layout_data_.size())); + this->child_layout_data_[position] = std::move(data); + this->InvalidateLayout(); + } + + const ChildLayoutData& GetChildLayoutData(Index position) const { + Expects(position >= 0 && + position < static_cast(this->child_layout_data_.size())); + return this->child_layout_data_[position]; + } + + RenderObject* HitTest(const Point& point) override; + + protected: + void OnAddChild(RenderObject* new_child, Index position) override; + void OnRemoveChild(RenderObject* removed_child, Index position) override; + + private: + std::vector child_layout_data_{}; +}; + +template +RenderObject* LayoutRenderObject::HitTest( + const Point& point) { + const auto& children = GetChildren(); + for (auto i = children.crbegin(); i != children.crend(); ++i) { + auto offset = (*i)->GetOffset(); + Point p{point.x - offset.x, point.y - offset.y}; + const auto result = (*i)->HitTest(p); + if (result != nullptr) { + return result; + } + } + + const auto margin = GetMargin(); + const auto size = GetSize(); + return Rect{margin.left, margin.top, + std::max(size.width - margin.GetHorizontalTotal(), 0.0f), + std::max(size.height - margin.GetVerticalTotal(), 0.0f)} + .IsPointInside(point) + ? this + : nullptr; +} // namespace cru::ui::render + +template +void LayoutRenderObject::OnAddChild(RenderObject* new_child, + const Index position) { + CRU_UNUSED(new_child) + + child_layout_data_.emplace(child_layout_data_.cbegin() + position); +} + +template +void LayoutRenderObject::OnRemoveChild( + RenderObject* removed_child, const Index position) { + CRU_UNUSED(removed_child) + + child_layout_data_.erase(child_layout_data_.cbegin() + position); +} +} // namespace cru::ui::render diff --git a/include/cru/ui/render/LayoutRenderObject.hpp b/include/cru/ui/render/LayoutRenderObject.hpp deleted file mode 100644 index 424a5831..00000000 --- a/include/cru/ui/render/LayoutRenderObject.hpp +++ /dev/null @@ -1,86 +0,0 @@ -#pragma once -#include "RenderObject.hpp" - -#include "cru/platform/graphics/util/Painter.hpp" - -namespace cru::ui::render { -template -class CRU_UI_API LayoutRenderObject : public RenderObject { - public: - using ChildLayoutData = TChildLayoutData; - - protected: - LayoutRenderObject() : RenderObject(ChildMode::Multiple) {} - - public: - CRU_DELETE_COPY(LayoutRenderObject) - CRU_DELETE_MOVE(LayoutRenderObject) - - ~LayoutRenderObject() override = default; - - const std::vector& GetChildLayoutDataList() const { - return this->child_layout_data_; - } - - void SetChildLayoutData(Index position, ChildLayoutData data) { - Expects(position >= 0 && - position < static_cast(this->child_layout_data_.size())); - this->child_layout_data_[position] = std::move(data); - this->InvalidateLayout(); - } - - const ChildLayoutData& GetChildLayoutData(Index position) const { - Expects(position >= 0 && - position < static_cast(this->child_layout_data_.size())); - return this->child_layout_data_[position]; - } - - RenderObject* HitTest(const Point& point) override; - - protected: - void OnAddChild(RenderObject* new_child, Index position) override; - void OnRemoveChild(RenderObject* removed_child, Index position) override; - - private: - std::vector child_layout_data_{}; -}; - -template -RenderObject* LayoutRenderObject::HitTest( - const Point& point) { - const auto& children = GetChildren(); - for (auto i = children.crbegin(); i != children.crend(); ++i) { - auto offset = (*i)->GetOffset(); - Point p{point.x - offset.x, point.y - offset.y}; - const auto result = (*i)->HitTest(p); - if (result != nullptr) { - return result; - } - } - - const auto margin = GetMargin(); - const auto size = GetSize(); - return Rect{margin.left, margin.top, - std::max(size.width - margin.GetHorizontalTotal(), 0.0f), - std::max(size.height - margin.GetVerticalTotal(), 0.0f)} - .IsPointInside(point) - ? this - : nullptr; -} // namespace cru::ui::render - -template -void LayoutRenderObject::OnAddChild(RenderObject* new_child, - const Index position) { - CRU_UNUSED(new_child) - - child_layout_data_.emplace(child_layout_data_.cbegin() + position); -} - -template -void LayoutRenderObject::OnRemoveChild( - RenderObject* removed_child, const Index position) { - CRU_UNUSED(removed_child) - - child_layout_data_.erase(child_layout_data_.cbegin() + position); -} -} // namespace cru::ui::render diff --git a/include/cru/ui/render/MeasureRequirement.h b/include/cru/ui/render/MeasureRequirement.h new file mode 100644 index 00000000..c740385b --- /dev/null +++ b/include/cru/ui/render/MeasureRequirement.h @@ -0,0 +1,251 @@ +#pragma once +#include "Base.h" + +#include "cru/common/String.h" + +#include +#include +#include + +namespace cru::ui::render { +constexpr Size Min(const Size& left, const Size& right) { + return Size{std::min(left.width, right.width), + std::min(left.height, right.height)}; +} + +constexpr Size Max(const Size& left, const Size& right) { + return Size{std::max(left.width, right.width), + std::max(left.height, right.height)}; +} + +class MeasureLength final { + public: + struct tag_not_specify_t {}; + constexpr static tag_not_specify_t tag_not_specify{}; + + constexpr MeasureLength() : MeasureLength(tag_not_specify) {} + constexpr MeasureLength(tag_not_specify_t) : length_(-1) {} + constexpr MeasureLength(float length) : length_(length) { + Expects(length >= 0); + } + + MeasureLength(const MeasureLength& other) = default; + constexpr MeasureLength& operator=(const MeasureLength& other) = default; + constexpr MeasureLength& operator=(float length) { + Expects(length >= 0); + length_ = length; + return *this; + } + + ~MeasureLength() = default; + + constexpr static MeasureLength NotSpecified() { + return MeasureLength{tag_not_specify}; + } + + constexpr bool IsSpecified() const { return length_ >= 0.0f; } + + // What not specified means depends on situation. + constexpr bool IsNotSpecified() const { return length_ < 0.0f; } + + constexpr float GetLengthOr(float value) const { + return length_ < 0 ? value : length_; + } + + // If not specify max value of float is returned. + constexpr float GetLengthOrMax() const { + return GetLengthOr(std::numeric_limits::max()); + } + + // If not specify 0 is returned. + constexpr float GetLengthOr0() const { return GetLengthOr(0.f); } + + // If not specify, return value is undefined. + constexpr float GetLengthOrUndefined() const { return length_; } + + // Not operator overload because this semantics is not very clear. + constexpr MeasureLength Plus(float length) const { + if (IsSpecified()) + return length_ + length; + else + return NotSpecified(); + } + + // Not operator overload because this semantics is not very clear. + constexpr MeasureLength Minus(float length) const { + if (IsSpecified()) + return std::max(length_ - length, 0.f); + else + return NotSpecified(); + } + + constexpr bool operator==(MeasureLength other) const { + return (length_ < 0 && other.length_ < 0) || length_ == other.length_; + } + + constexpr bool operator!=(MeasureLength other) const { + return !operator==(other); + } + + constexpr static MeasureLength Min(MeasureLength left, MeasureLength right) { + if (left.IsNotSpecified()) { + if (right.IsNotSpecified()) + return NotSpecified(); + else + return right; + } else { + if (right.IsNotSpecified()) + return left; + else + return std::min(left.length_, right.length_); + } + } + + constexpr static MeasureLength Max(MeasureLength left, MeasureLength right) { + if (left.IsNotSpecified()) { + if (right.IsNotSpecified()) + return NotSpecified(); + else + return right; + } else { + if (left.IsNotSpecified()) + return left; + else + return std::max(left.length_, right.length_); + } + } + + String ToDebugString() const { + return IsSpecified() ? ToString(GetLengthOrUndefined()) : u"UNSPECIFIED"; + } + + private: + // -1 for not specify + float length_; +}; + +struct MeasureSize { + MeasureLength width; + MeasureLength height; + + constexpr MeasureSize() = default; + constexpr MeasureSize(MeasureLength width, MeasureLength height) + : width(width), height(height) {} + constexpr MeasureSize(const Size& size) + : width(size.width), height(size.height) {} + constexpr MeasureSize(const MeasureSize& other) = default; + + MeasureSize& operator=(const MeasureSize& other) = default; + constexpr MeasureSize& operator=(const Size& other) { + width = other.width; + height = other.height; + return *this; + } + + constexpr Size GetSizeOrMax() const { + return Size{width.GetLengthOrMax(), height.GetLengthOrMax()}; + } + + constexpr Size GetSizeOr0() const { + return Size{width.GetLengthOr0(), height.GetLengthOr0()}; + } + + constexpr MeasureSize Plus(const Size& size) const { + return MeasureSize{width.Plus(size.width), height.Plus(size.height)}; + } + + constexpr MeasureSize Minus(const Size& size) const { + return MeasureSize{width.Minus(size.width), height.Minus(size.height)}; + } + + constexpr MeasureSize OverrideBy(const MeasureSize& size) const { + return MeasureSize{ + size.width.IsSpecified() ? size.width.GetLengthOrUndefined() + : this->width, + size.height.IsSpecified() ? size.height.GetLengthOrUndefined() + : this->height, + }; + } + + String ToDebugString() const { + return Format(u"({}, {})", width.ToDebugString(), height.ToDebugString()); + } + + constexpr static MeasureSize NotSpecified() { + return MeasureSize{MeasureLength::NotSpecified(), + MeasureLength::NotSpecified()}; + } + + constexpr static MeasureSize Min(const MeasureSize& left, + const MeasureSize& right) { + return MeasureSize{MeasureLength::Min(left.width, right.width), + MeasureLength::Min(left.height, right.height)}; + } + + constexpr static MeasureSize Max(const MeasureSize& left, + const MeasureSize& right) { + return MeasureSize{MeasureLength::Max(left.width, right.width), + MeasureLength::Max(left.height, right.height)}; + } +}; + +struct MeasureRequirement { + MeasureSize max; + MeasureSize min; + + constexpr MeasureRequirement() = default; + constexpr MeasureRequirement(const MeasureSize& max, const MeasureSize& min) + : max(max), min(min) {} + + constexpr bool Satisfy(const Size& size) const { + auto normalized = Normalize(); + return normalized.max.width.GetLengthOrMax() >= size.width && + normalized.max.height.GetLengthOrMax() >= size.height && + normalized.min.width.GetLengthOr0() <= size.width && + normalized.min.height.GetLengthOr0() <= size.height; + } + + constexpr MeasureRequirement Normalize() const { + MeasureRequirement result = *this; + if (result.min.width.GetLengthOr0() > result.max.width.GetLengthOrMax()) + result.min.width = result.max.width; + + if (result.min.height.GetLengthOr0() > result.max.height.GetLengthOrMax()) + result.min.height = result.max.height; + return result; + } + + constexpr Size Coerce(const Size& size) const { + // This order guarentees result is the same as normalized. + return Min(Max(size, min.GetSizeOr0()), max.GetSizeOrMax()); + } + + constexpr MeasureSize Coerce(const MeasureSize& size) const { + MeasureSize result = size; + if (result.width.IsSpecified()) + // This order guarentees result is the same as normalized. + result.width = std::min(std::max(min.width.GetLengthOr0(), + result.width.GetLengthOrUndefined()), + max.width.GetLengthOrMax()); + + if (result.height.IsSpecified()) + // This order guarentees result is the same as normalized. + result.height = std::min(std::max(min.height.GetLengthOr0(), + result.height.GetLengthOrUndefined()), + max.height.GetLengthOrMax()); + + return result; + } + + String ToDebugString() const { + return Format(u"{{min: {}, max: {}}}", min.ToDebugString(), + max.ToDebugString()); + } + + constexpr static MeasureRequirement Merge(const MeasureRequirement& left, + const MeasureRequirement& right) { + return MeasureRequirement{MeasureSize::Min(left.max, right.max), + MeasureSize::Max(left.min, right.min)}; + } +}; +} // namespace cru::ui::render diff --git a/include/cru/ui/render/MeasureRequirement.hpp b/include/cru/ui/render/MeasureRequirement.hpp deleted file mode 100644 index 90d02a02..00000000 --- a/include/cru/ui/render/MeasureRequirement.hpp +++ /dev/null @@ -1,251 +0,0 @@ -#pragma once -#include "Base.hpp" - -#include "cru/common/String.hpp" - -#include -#include -#include - -namespace cru::ui::render { -constexpr Size Min(const Size& left, const Size& right) { - return Size{std::min(left.width, right.width), - std::min(left.height, right.height)}; -} - -constexpr Size Max(const Size& left, const Size& right) { - return Size{std::max(left.width, right.width), - std::max(left.height, right.height)}; -} - -class MeasureLength final { - public: - struct tag_not_specify_t {}; - constexpr static tag_not_specify_t tag_not_specify{}; - - constexpr MeasureLength() : MeasureLength(tag_not_specify) {} - constexpr MeasureLength(tag_not_specify_t) : length_(-1) {} - constexpr MeasureLength(float length) : length_(length) { - Expects(length >= 0); - } - - MeasureLength(const MeasureLength& other) = default; - constexpr MeasureLength& operator=(const MeasureLength& other) = default; - constexpr MeasureLength& operator=(float length) { - Expects(length >= 0); - length_ = length; - return *this; - } - - ~MeasureLength() = default; - - constexpr static MeasureLength NotSpecified() { - return MeasureLength{tag_not_specify}; - } - - constexpr bool IsSpecified() const { return length_ >= 0.0f; } - - // What not specified means depends on situation. - constexpr bool IsNotSpecified() const { return length_ < 0.0f; } - - constexpr float GetLengthOr(float value) const { - return length_ < 0 ? value : length_; - } - - // If not specify max value of float is returned. - constexpr float GetLengthOrMax() const { - return GetLengthOr(std::numeric_limits::max()); - } - - // If not specify 0 is returned. - constexpr float GetLengthOr0() const { return GetLengthOr(0.f); } - - // If not specify, return value is undefined. - constexpr float GetLengthOrUndefined() const { return length_; } - - // Not operator overload because this semantics is not very clear. - constexpr MeasureLength Plus(float length) const { - if (IsSpecified()) - return length_ + length; - else - return NotSpecified(); - } - - // Not operator overload because this semantics is not very clear. - constexpr MeasureLength Minus(float length) const { - if (IsSpecified()) - return std::max(length_ - length, 0.f); - else - return NotSpecified(); - } - - constexpr bool operator==(MeasureLength other) const { - return (length_ < 0 && other.length_ < 0) || length_ == other.length_; - } - - constexpr bool operator!=(MeasureLength other) const { - return !operator==(other); - } - - constexpr static MeasureLength Min(MeasureLength left, MeasureLength right) { - if (left.IsNotSpecified()) { - if (right.IsNotSpecified()) - return NotSpecified(); - else - return right; - } else { - if (right.IsNotSpecified()) - return left; - else - return std::min(left.length_, right.length_); - } - } - - constexpr static MeasureLength Max(MeasureLength left, MeasureLength right) { - if (left.IsNotSpecified()) { - if (right.IsNotSpecified()) - return NotSpecified(); - else - return right; - } else { - if (left.IsNotSpecified()) - return left; - else - return std::max(left.length_, right.length_); - } - } - - String ToDebugString() const { - return IsSpecified() ? ToString(GetLengthOrUndefined()) : u"UNSPECIFIED"; - } - - private: - // -1 for not specify - float length_; -}; - -struct MeasureSize { - MeasureLength width; - MeasureLength height; - - constexpr MeasureSize() = default; - constexpr MeasureSize(MeasureLength width, MeasureLength height) - : width(width), height(height) {} - constexpr MeasureSize(const Size& size) - : width(size.width), height(size.height) {} - constexpr MeasureSize(const MeasureSize& other) = default; - - MeasureSize& operator=(const MeasureSize& other) = default; - constexpr MeasureSize& operator=(const Size& other) { - width = other.width; - height = other.height; - return *this; - } - - constexpr Size GetSizeOrMax() const { - return Size{width.GetLengthOrMax(), height.GetLengthOrMax()}; - } - - constexpr Size GetSizeOr0() const { - return Size{width.GetLengthOr0(), height.GetLengthOr0()}; - } - - constexpr MeasureSize Plus(const Size& size) const { - return MeasureSize{width.Plus(size.width), height.Plus(size.height)}; - } - - constexpr MeasureSize Minus(const Size& size) const { - return MeasureSize{width.Minus(size.width), height.Minus(size.height)}; - } - - constexpr MeasureSize OverrideBy(const MeasureSize& size) const { - return MeasureSize{ - size.width.IsSpecified() ? size.width.GetLengthOrUndefined() - : this->width, - size.height.IsSpecified() ? size.height.GetLengthOrUndefined() - : this->height, - }; - } - - String ToDebugString() const { - return Format(u"({}, {})", width.ToDebugString(), height.ToDebugString()); - } - - constexpr static MeasureSize NotSpecified() { - return MeasureSize{MeasureLength::NotSpecified(), - MeasureLength::NotSpecified()}; - } - - constexpr static MeasureSize Min(const MeasureSize& left, - const MeasureSize& right) { - return MeasureSize{MeasureLength::Min(left.width, right.width), - MeasureLength::Min(left.height, right.height)}; - } - - constexpr static MeasureSize Max(const MeasureSize& left, - const MeasureSize& right) { - return MeasureSize{MeasureLength::Max(left.width, right.width), - MeasureLength::Max(left.height, right.height)}; - } -}; - -struct MeasureRequirement { - MeasureSize max; - MeasureSize min; - - constexpr MeasureRequirement() = default; - constexpr MeasureRequirement(const MeasureSize& max, const MeasureSize& min) - : max(max), min(min) {} - - constexpr bool Satisfy(const Size& size) const { - auto normalized = Normalize(); - return normalized.max.width.GetLengthOrMax() >= size.width && - normalized.max.height.GetLengthOrMax() >= size.height && - normalized.min.width.GetLengthOr0() <= size.width && - normalized.min.height.GetLengthOr0() <= size.height; - } - - constexpr MeasureRequirement Normalize() const { - MeasureRequirement result = *this; - if (result.min.width.GetLengthOr0() > result.max.width.GetLengthOrMax()) - result.min.width = result.max.width; - - if (result.min.height.GetLengthOr0() > result.max.height.GetLengthOrMax()) - result.min.height = result.max.height; - return result; - } - - constexpr Size Coerce(const Size& size) const { - // This order guarentees result is the same as normalized. - return Min(Max(size, min.GetSizeOr0()), max.GetSizeOrMax()); - } - - constexpr MeasureSize Coerce(const MeasureSize& size) const { - MeasureSize result = size; - if (result.width.IsSpecified()) - // This order guarentees result is the same as normalized. - result.width = std::min(std::max(min.width.GetLengthOr0(), - result.width.GetLengthOrUndefined()), - max.width.GetLengthOrMax()); - - if (result.height.IsSpecified()) - // This order guarentees result is the same as normalized. - result.height = std::min(std::max(min.height.GetLengthOr0(), - result.height.GetLengthOrUndefined()), - max.height.GetLengthOrMax()); - - return result; - } - - String ToDebugString() const { - return Format(u"{{min: {}, max: {}}}", min.ToDebugString(), - max.ToDebugString()); - } - - constexpr static MeasureRequirement Merge(const MeasureRequirement& left, - const MeasureRequirement& right) { - return MeasureRequirement{MeasureSize::Min(left.max, right.max), - MeasureSize::Max(left.min, right.min)}; - } -}; -} // namespace cru::ui::render diff --git a/include/cru/ui/render/RenderObject.h b/include/cru/ui/render/RenderObject.h new file mode 100644 index 00000000..1e47e7fc --- /dev/null +++ b/include/cru/ui/render/RenderObject.h @@ -0,0 +1,240 @@ +#pragma once +#include "Base.h" + +#include "MeasureRequirement.h" +#include "cru/common/Base.h" +#include "cru/common/Event.h" +#include "cru/ui/Base.h" + +#include +#include +#include + +namespace cru::ui::render { +// 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. +// +// About layout: +// Render object should be able to deal with arbitrary size as the result of +// measure and layout. +// +// Parent may pass calculated preferred size down. But the preferred size set on +// child itself takes precedence. +// +// Each render object should obey the measure requirement to set size and report +// a warning when that requirement can't be satisfied with probably bigger size +// of children than that of itself and optional visual effect to indicate that. +// +// If size of chilren are less than min size requirement, then render object +// should try to fill the rest area. If size of children are more than max size +// requirement, then render object should display a warning of the layout +// problem and use the max size of itself with children exceeding its bound. +// (Or better you could use some visual effect to indicate that.) +// +// To write a custom RenderObject, override following methods: +// public: +// void Draw(platform::graphics::IPainter* painter) override; +// RenderObject* HitTest(const Point& point) override; +// protected: +// Size OnMeasureContent(const MeasureRequirement& requirement) override; +// void OnLayoutContent(const Rect& content_rect) override; +class CRU_UI_API RenderObject : public Object { + friend host::WindowHost; + + CRU_DEFINE_CLASS_LOG_TAG(u"cru::ui::render::RenderObject") + + protected: + enum class ChildMode { + None, + Single, + Multiple, + }; + + RenderObject() = default; + RenderObject(ChildMode child_mode) : RenderObject() { + SetChildMode(child_mode); + } + + public: + RenderObject(const RenderObject& other) = delete; + RenderObject(RenderObject&& other) = delete; + RenderObject& operator=(const RenderObject& other) = delete; + RenderObject& operator=(RenderObject&& other) = delete; + ~RenderObject() override = default; + + controls::Control* GetAttachedControl() const { return control_; } + void SetAttachedControl(controls::Control* new_control); + + host::WindowHost* GetWindowHost() const { return window_host_; } + + RenderObject* GetParent() const { return parent_; } + + const std::vector& GetChildren() const { return children_; } + Index GetChildCount() const { return static_cast(children_.size()); } + void AddChild(RenderObject* render_object, Index position); + void RemoveChild(Index position); + + RenderObject* GetFirstChild() const; + void TraverseDescendants(const std::function& action); + + // Offset from parent's lefttop to lefttop of this render object. Margin is + // accounted for. + Point GetOffset() const { return offset_; } + Size GetSize() const { return size_; } + Point GetTotalOffset() const; + Point FromRootToContent(const Point& point) const; + + Thickness GetMargin() const { return margin_; } + void SetMargin(const Thickness& margin) { + margin_ = margin; + InvalidateLayout(); + } + + Thickness GetPadding() const { return padding_; } + void SetPadding(const Thickness& padding) { + padding_ = padding; + InvalidateLayout(); + } + + MeasureSize GetPreferredSize() const { return preferred_size_; } + void SetPreferredSize(const MeasureSize& preferred_size) { + preferred_size_ = preferred_size; + InvalidateLayout(); + } + + MeasureSize GetMinSize() const { return custom_measure_requirement_.min; } + void SetMinSize(const MeasureSize& min_size) { + custom_measure_requirement_.min = min_size; + InvalidateLayout(); + } + + MeasureSize GetMaxSize() const { return custom_measure_requirement_.max; } + void SetMaxSize(const MeasureSize& max_size) { + custom_measure_requirement_.max = max_size; + InvalidateLayout(); + } + + MeasureRequirement GetCustomMeasureRequirement() const { + return custom_measure_requirement_; + } + + // This method will merge requirement passed by argument and requirement of + // the render object using MeasureRequirement::Merge and then call + // MeasureRequirement::Normalize on it. And it will use preferred size of the + // render object to override the one passed by argument. Then pass the two to + // OnMeasureCore and use the return value of it to set the size of this render + // object. This can be called multiple times on children during measure to + // adjust for better size. + void Measure(const MeasureRequirement& requirement, + const MeasureSize& preferred_size); + // This will set offset of this render object and call OnLayoutCore. + void Layout(const Point& offset); + + virtual Thickness GetOuterSpaceThickness() const; + virtual Rect GetPaddingRect() const; + virtual Rect GetContentRect() const; + + void Draw(platform::graphics::IPainter* painter); + + // Param point must be relative the lefttop of render object including margin. + // Add offset before pass point to children. + virtual RenderObject* HitTest(const Point& point) = 0; + + IEvent* AttachToHostEvent() { + return &attach_to_host_event_; + } + IEvent* DetachFromHostEvent() { + return &detach_from_host_event_; + } + + public: + void InvalidateLayout(); + void InvalidatePaint(); + + public: + virtual std::u16string_view GetName() const; + std::u16string GetDebugPathInTree() const; + + protected: + void SetChildMode(ChildMode mode) { child_mode_ = mode; } + + protected: + RenderObject* GetSingleChild() const; + + virtual void OnParentChanged(RenderObject* old_parent, + RenderObject* new_parent); + + // default is to invalidate both layout and paint + virtual void OnAddChild(RenderObject* new_child, Index position); + // default is to invalidate both layout and paint + virtual void OnRemoveChild(RenderObject* removed_child, Index position); + + // Draw all children with offset. + void DefaultDrawChildren(platform::graphics::IPainter* painter); + + // Draw all children with translation of content rect lefttop. + void DefaultDrawContent(platform::graphics::IPainter* painter); + + // Call DefaultDrawContent. Then call DefaultDrawChildren. + virtual void OnDrawCore(platform::graphics::IPainter* painter); + + virtual void OnDrawContent(platform::graphics::IPainter* painter); + + // Size measure including margin and padding. Please reduce margin and padding + // or other custom things and pass the result content measure requirement and + // preferred size to OnMeasureContent. Return value must not be negative and + // must obey requirement. + // Note: Implementation should coerce the preferred size into the requirement + // when pass them to OnMeasureContent. + virtual Size OnMeasureCore(const MeasureRequirement& requirement, + const MeasureSize& preferred_size); + + // Please reduce margin and padding or other custom things and pass the result + // content rect to OnLayoutContent. + virtual void OnLayoutCore(); + + // Override this function to measure content and children(Call Measure on + // them). Do not consider margin or padding in this method because they are + // already considered in OnMeasureCore. Returned size must obey requirement. + // Caller should guarantee preferred_size is corerced into required range. + virtual Size OnMeasureContent(const MeasureRequirement& requirement, + const MeasureSize& preferred_size) = 0; + + // Layout all content and children(Call Layout on them). + // Lefttop of content_rect should be added when calculated children's offset. + virtual void OnLayoutContent(const Rect& content_rect) = 0; + + virtual void OnAttachedControlChanged(controls::Control* control) { + CRU_UNUSED(control) + } + + virtual void OnAfterLayout(); + + private: + void SetParent(RenderObject* new_parent); + + void SetWindowHostRecursive(host::WindowHost* host); + + private: + controls::Control* control_ = nullptr; + host::WindowHost* window_host_ = nullptr; + + RenderObject* parent_ = nullptr; + std::vector children_{}; + + ChildMode child_mode_ = ChildMode::None; + + Point offset_{}; + Size size_{}; + + MeasureSize preferred_size_; + MeasureRequirement custom_measure_requirement_; + + Thickness margin_{}; + Thickness padding_{}; + + Event attach_to_host_event_; + Event detach_from_host_event_; +}; +} // namespace cru::ui::render diff --git a/include/cru/ui/render/RenderObject.hpp b/include/cru/ui/render/RenderObject.hpp deleted file mode 100644 index bac97640..00000000 --- a/include/cru/ui/render/RenderObject.hpp +++ /dev/null @@ -1,240 +0,0 @@ -#pragma once -#include "Base.hpp" - -#include "MeasureRequirement.hpp" -#include "cru/common/Base.hpp" -#include "cru/common/Event.hpp" -#include "cru/ui/Base.hpp" - -#include -#include -#include - -namespace cru::ui::render { -// 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. -// -// About layout: -// Render object should be able to deal with arbitrary size as the result of -// measure and layout. -// -// Parent may pass calculated preferred size down. But the preferred size set on -// child itself takes precedence. -// -// Each render object should obey the measure requirement to set size and report -// a warning when that requirement can't be satisfied with probably bigger size -// of children than that of itself and optional visual effect to indicate that. -// -// If size of chilren are less than min size requirement, then render object -// should try to fill the rest area. If size of children are more than max size -// requirement, then render object should display a warning of the layout -// problem and use the max size of itself with children exceeding its bound. -// (Or better you could use some visual effect to indicate that.) -// -// To write a custom RenderObject, override following methods: -// public: -// void Draw(platform::graphics::IPainter* painter) override; -// RenderObject* HitTest(const Point& point) override; -// protected: -// Size OnMeasureContent(const MeasureRequirement& requirement) override; -// void OnLayoutContent(const Rect& content_rect) override; -class CRU_UI_API RenderObject : public Object { - friend host::WindowHost; - - CRU_DEFINE_CLASS_LOG_TAG(u"cru::ui::render::RenderObject") - - protected: - enum class ChildMode { - None, - Single, - Multiple, - }; - - RenderObject() = default; - RenderObject(ChildMode child_mode) : RenderObject() { - SetChildMode(child_mode); - } - - public: - RenderObject(const RenderObject& other) = delete; - RenderObject(RenderObject&& other) = delete; - RenderObject& operator=(const RenderObject& other) = delete; - RenderObject& operator=(RenderObject&& other) = delete; - ~RenderObject() override = default; - - controls::Control* GetAttachedControl() const { return control_; } - void SetAttachedControl(controls::Control* new_control); - - host::WindowHost* GetWindowHost() const { return window_host_; } - - RenderObject* GetParent() const { return parent_; } - - const std::vector& GetChildren() const { return children_; } - Index GetChildCount() const { return static_cast(children_.size()); } - void AddChild(RenderObject* render_object, Index position); - void RemoveChild(Index position); - - RenderObject* GetFirstChild() const; - void TraverseDescendants(const std::function& action); - - // Offset from parent's lefttop to lefttop of this render object. Margin is - // accounted for. - Point GetOffset() const { return offset_; } - Size GetSize() const { return size_; } - Point GetTotalOffset() const; - Point FromRootToContent(const Point& point) const; - - Thickness GetMargin() const { return margin_; } - void SetMargin(const Thickness& margin) { - margin_ = margin; - InvalidateLayout(); - } - - Thickness GetPadding() const { return padding_; } - void SetPadding(const Thickness& padding) { - padding_ = padding; - InvalidateLayout(); - } - - MeasureSize GetPreferredSize() const { return preferred_size_; } - void SetPreferredSize(const MeasureSize& preferred_size) { - preferred_size_ = preferred_size; - InvalidateLayout(); - } - - MeasureSize GetMinSize() const { return custom_measure_requirement_.min; } - void SetMinSize(const MeasureSize& min_size) { - custom_measure_requirement_.min = min_size; - InvalidateLayout(); - } - - MeasureSize GetMaxSize() const { return custom_measure_requirement_.max; } - void SetMaxSize(const MeasureSize& max_size) { - custom_measure_requirement_.max = max_size; - InvalidateLayout(); - } - - MeasureRequirement GetCustomMeasureRequirement() const { - return custom_measure_requirement_; - } - - // This method will merge requirement passed by argument and requirement of - // the render object using MeasureRequirement::Merge and then call - // MeasureRequirement::Normalize on it. And it will use preferred size of the - // render object to override the one passed by argument. Then pass the two to - // OnMeasureCore and use the return value of it to set the size of this render - // object. This can be called multiple times on children during measure to - // adjust for better size. - void Measure(const MeasureRequirement& requirement, - const MeasureSize& preferred_size); - // This will set offset of this render object and call OnLayoutCore. - void Layout(const Point& offset); - - virtual Thickness GetOuterSpaceThickness() const; - virtual Rect GetPaddingRect() const; - virtual Rect GetContentRect() const; - - void Draw(platform::graphics::IPainter* painter); - - // Param point must be relative the lefttop of render object including margin. - // Add offset before pass point to children. - virtual RenderObject* HitTest(const Point& point) = 0; - - IEvent* AttachToHostEvent() { - return &attach_to_host_event_; - } - IEvent* DetachFromHostEvent() { - return &detach_from_host_event_; - } - - public: - void InvalidateLayout(); - void InvalidatePaint(); - - public: - virtual std::u16string_view GetName() const; - std::u16string GetDebugPathInTree() const; - - protected: - void SetChildMode(ChildMode mode) { child_mode_ = mode; } - - protected: - RenderObject* GetSingleChild() const; - - virtual void OnParentChanged(RenderObject* old_parent, - RenderObject* new_parent); - - // default is to invalidate both layout and paint - virtual void OnAddChild(RenderObject* new_child, Index position); - // default is to invalidate both layout and paint - virtual void OnRemoveChild(RenderObject* removed_child, Index position); - - // Draw all children with offset. - void DefaultDrawChildren(platform::graphics::IPainter* painter); - - // Draw all children with translation of content rect lefttop. - void DefaultDrawContent(platform::graphics::IPainter* painter); - - // Call DefaultDrawContent. Then call DefaultDrawChildren. - virtual void OnDrawCore(platform::graphics::IPainter* painter); - - virtual void OnDrawContent(platform::graphics::IPainter* painter); - - // Size measure including margin and padding. Please reduce margin and padding - // or other custom things and pass the result content measure requirement and - // preferred size to OnMeasureContent. Return value must not be negative and - // must obey requirement. - // Note: Implementation should coerce the preferred size into the requirement - // when pass them to OnMeasureContent. - virtual Size OnMeasureCore(const MeasureRequirement& requirement, - const MeasureSize& preferred_size); - - // Please reduce margin and padding or other custom things and pass the result - // content rect to OnLayoutContent. - virtual void OnLayoutCore(); - - // Override this function to measure content and children(Call Measure on - // them). Do not consider margin or padding in this method because they are - // already considered in OnMeasureCore. Returned size must obey requirement. - // Caller should guarantee preferred_size is corerced into required range. - virtual Size OnMeasureContent(const MeasureRequirement& requirement, - const MeasureSize& preferred_size) = 0; - - // Layout all content and children(Call Layout on them). - // Lefttop of content_rect should be added when calculated children's offset. - virtual void OnLayoutContent(const Rect& content_rect) = 0; - - virtual void OnAttachedControlChanged(controls::Control* control) { - CRU_UNUSED(control) - } - - virtual void OnAfterLayout(); - - private: - void SetParent(RenderObject* new_parent); - - void SetWindowHostRecursive(host::WindowHost* host); - - private: - controls::Control* control_ = nullptr; - host::WindowHost* window_host_ = nullptr; - - RenderObject* parent_ = nullptr; - std::vector children_{}; - - ChildMode child_mode_ = ChildMode::None; - - Point offset_{}; - Size size_{}; - - MeasureSize preferred_size_; - MeasureRequirement custom_measure_requirement_; - - Thickness margin_{}; - Thickness padding_{}; - - Event attach_to_host_event_; - Event detach_from_host_event_; -}; -} // namespace cru::ui::render diff --git a/include/cru/ui/render/ScrollBar.h b/include/cru/ui/render/ScrollBar.h new file mode 100644 index 00000000..84d4375b --- /dev/null +++ b/include/cru/ui/render/ScrollBar.h @@ -0,0 +1,255 @@ +#pragma once +#include "Base.h" +#include "cru/common/Base.h" +#include "cru/common/Event.h" +#include "cru/platform/graphics/Base.h" +#include "cru/platform/graphics/Brush.h" +#include "cru/platform/graphics/Geometry.h" +#include "cru/platform/graphics/Painter.h" +#include "cru/platform/gui/Cursor.h" +#include "cru/platform/gui/TimerHelper.h" +#include "cru/platform/gui/UiApplication.h" +#include "cru/ui/Base.h" +#include "cru/ui/controls/Control.h" +#include "cru/ui/helper/ClickDetector.h" + +#include +#include +#include +#include + +namespace cru::ui::render { +class ScrollRenderObject; + +enum class ScrollKind { Absolute, Relative, Page, Line }; + +struct Scroll { + Direction direction; + ScrollKind kind; + // For absolute, the new scroll position. Otherwise, offset. + float value; +}; + +enum class ScrollBarAreaKind { + UpArrow, // Line up + DownArrow, // Line down + UpSlot, // Page up + DownSlot, // Page down + Thumb +}; + +enum class ScrollBarBrushUsageKind { Arrow, ArrowBackground, Slot, Thumb }; +enum class ScrollBarBrushStateKind { Normal, Hover, Press, Disable }; + +String CRU_UI_API GenerateScrollBarThemeColorKey(ScrollBarBrushUsageKind usage, + ScrollBarBrushStateKind state); + +class CRU_UI_API ScrollBar : public Object { + public: + ScrollBar(gsl::not_null render_object, + Direction direction); + + CRU_DELETE_COPY(ScrollBar) + CRU_DELETE_MOVE(ScrollBar) + + ~ScrollBar() override; + + public: + Direction GetDirection() const { return direction_; } + + bool IsEnabled() const { return is_enabled_; } + void SetEnabled(bool value); + + bool IsExpanded() const { return is_expanded_; } + void SetExpanded(bool value); + + void Draw(platform::graphics::IPainter* painter); + + IEvent* ScrollAttemptEvent() { return &scroll_attempt_event_; } + + void InstallHandlers(controls::Control* control); + void UninstallHandlers() { InstallHandlers(nullptr); } + + gsl::not_null> + GetCollapsedThumbBrush(); + // Brush could be nullptr to use the theme brush. + void SetCollapsedThumbBrush( + std::shared_ptr brush); + gsl::not_null> GetBrush( + ScrollBarBrushUsageKind usage, ScrollBarBrushStateKind state); + // Brush could be nullptr to use the theme brush. + void SetBrush(ScrollBarBrushUsageKind usage, ScrollBarBrushStateKind state, + std::shared_ptr brush); + + protected: + void OnDraw(platform::graphics::IPainter* painter, bool expand); + + virtual void DrawUpArrow( + platform::graphics::IPainter* painter, const Rect& area, + gsl::not_null arrow_brush, + gsl::not_null background_brush) = 0; + virtual void DrawDownArrow( + platform::graphics::IPainter* painter, const Rect& area, + gsl::not_null arrow_brush, + gsl::not_null background_brush) = 0; + + std::optional ExpandedHitTest(const Point& point); + + virtual bool IsShowBar() = 0; + + virtual std::optional GetExpandedAreaRect( + ScrollBarAreaKind area_kind) = 0; + virtual std::optional GetCollapsedTriggerExpandAreaRect() = 0; + virtual std::optional GetCollapsedThumbRect() = 0; + + virtual float CalculateNewScrollPosition(const Rect& thumb_original_rect, + const Point& mouse_offset) = 0; + + virtual bool CanScrollUp() = 0; + virtual bool CanScrollDown() = 0; + + private: + void SetCursor(); + void RestoreCursor(); + + void BeginAutoCollapseTimer(); + void StopAutoCollapseTimer(); + + void OnMouseLeave(); + + ScrollBarBrushStateKind GetState(ScrollBarAreaKind area); + + protected: + gsl::not_null render_object_; + + std::unique_ptr arrow_geometry_; + + private: + Direction direction_; + + bool is_enabled_ = true; + + bool is_expanded_ = false; + + std::shared_ptr collapsed_thumb_brush_; + std::unordered_map< + ScrollBarBrushUsageKind, + std::unordered_map>> + brushes_; + + Rect move_thumb_thumb_original_rect_; + std::optional move_thumb_start_; + + std::optional mouse_hover_; + std::optional mouse_press_; + + EventRevokerListGuard event_guard_; + + Event scroll_attempt_event_; + + bool cursor_overrided_ = false; + + platform::gui::TimerAutoCanceler auto_collapse_timer_canceler_; +}; + +class CRU_UI_API HorizontalScrollBar : public ScrollBar { + public: + explicit HorizontalScrollBar( + gsl::not_null render_object); + + CRU_DELETE_COPY(HorizontalScrollBar) + CRU_DELETE_MOVE(HorizontalScrollBar) + + ~HorizontalScrollBar() override = default; + + protected: + void DrawUpArrow( + platform::graphics::IPainter* painter, const Rect& area, + gsl::not_null arrow_brush, + gsl::not_null background_brush) override; + void DrawDownArrow( + platform::graphics::IPainter* painter, const Rect& area, + gsl::not_null arrow_brush, + gsl::not_null background_brush) override; + + bool IsShowBar() override; + + std::optional GetExpandedAreaRect(ScrollBarAreaKind area_kind) override; + std::optional GetCollapsedTriggerExpandAreaRect() override; + std::optional GetCollapsedThumbRect() override; + + float CalculateNewScrollPosition(const Rect& thumb_original_rect, + const Point& mouse_offset) override; + + bool CanScrollUp() override; + bool CanScrollDown() override; +}; + +class CRU_UI_API VerticalScrollBar : public ScrollBar { + public: + explicit VerticalScrollBar(gsl::not_null render_object); + + CRU_DELETE_COPY(VerticalScrollBar) + CRU_DELETE_MOVE(VerticalScrollBar) + + ~VerticalScrollBar() override = default; + + protected: + void DrawUpArrow( + platform::graphics::IPainter* painter, const Rect& area, + gsl::not_null arrow_brush, + gsl::not_null background_brush) override; + void DrawDownArrow( + platform::graphics::IPainter* painter, const Rect& area, + gsl::not_null arrow_brush, + gsl::not_null background_brush) override; + + bool IsShowBar() override; + + std::optional GetExpandedAreaRect(ScrollBarAreaKind area_kind) override; + std::optional GetCollapsedTriggerExpandAreaRect() override; + std::optional GetCollapsedThumbRect() override; + + float CalculateNewScrollPosition(const Rect& thumb_original_rect, + const Point& mouse_offset) override; + + bool CanScrollUp() override; + bool CanScrollDown() override; +}; + +// A delegate to draw scrollbar and register related events. +class CRU_UI_API ScrollBarDelegate : public Object { + public: + explicit ScrollBarDelegate(gsl::not_null render_object); + + CRU_DELETE_COPY(ScrollBarDelegate) + CRU_DELETE_MOVE(ScrollBarDelegate) + + ~ScrollBarDelegate() override = default; + + public: + bool IsHorizontalBarEnabled() const { return horizontal_bar_.IsEnabled(); } + void SetHorizontalBarEnabled(bool value) { + horizontal_bar_.SetEnabled(value); + } + + bool IsVerticalBarEnabled() const { return horizontal_bar_.IsEnabled(); } + void SetVerticalBarEnabled(bool value) { horizontal_bar_.SetEnabled(value); } + + IEvent* ScrollAttemptEvent() { return &scroll_attempt_event_; } + + void DrawScrollBar(platform::graphics::IPainter* painter); + + void InstallHandlers(controls::Control* control); + void UninstallHandlers() { InstallHandlers(nullptr); } + + private: + gsl::not_null render_object_; + + HorizontalScrollBar horizontal_bar_; + VerticalScrollBar vertical_bar_; + + Event scroll_attempt_event_; +}; +} // namespace cru::ui::render diff --git a/include/cru/ui/render/ScrollBar.hpp b/include/cru/ui/render/ScrollBar.hpp deleted file mode 100644 index f1007d4d..00000000 --- a/include/cru/ui/render/ScrollBar.hpp +++ /dev/null @@ -1,255 +0,0 @@ -#pragma once -#include "Base.hpp" -#include "cru/common/Base.hpp" -#include "cru/common/Event.hpp" -#include "cru/platform/graphics/Base.hpp" -#include "cru/platform/graphics/Brush.hpp" -#include "cru/platform/graphics/Geometry.hpp" -#include "cru/platform/graphics/Painter.hpp" -#include "cru/platform/gui/Cursor.hpp" -#include "cru/platform/gui/TimerHelper.hpp" -#include "cru/platform/gui/UiApplication.hpp" -#include "cru/ui/Base.hpp" -#include "cru/ui/controls/Control.hpp" -#include "cru/ui/helper/ClickDetector.hpp" - -#include -#include -#include -#include - -namespace cru::ui::render { -class ScrollRenderObject; - -enum class ScrollKind { Absolute, Relative, Page, Line }; - -struct Scroll { - Direction direction; - ScrollKind kind; - // For absolute, the new scroll position. Otherwise, offset. - float value; -}; - -enum class ScrollBarAreaKind { - UpArrow, // Line up - DownArrow, // Line down - UpSlot, // Page up - DownSlot, // Page down - Thumb -}; - -enum class ScrollBarBrushUsageKind { Arrow, ArrowBackground, Slot, Thumb }; -enum class ScrollBarBrushStateKind { Normal, Hover, Press, Disable }; - -String CRU_UI_API GenerateScrollBarThemeColorKey(ScrollBarBrushUsageKind usage, - ScrollBarBrushStateKind state); - -class CRU_UI_API ScrollBar : public Object { - public: - ScrollBar(gsl::not_null render_object, - Direction direction); - - CRU_DELETE_COPY(ScrollBar) - CRU_DELETE_MOVE(ScrollBar) - - ~ScrollBar() override; - - public: - Direction GetDirection() const { return direction_; } - - bool IsEnabled() const { return is_enabled_; } - void SetEnabled(bool value); - - bool IsExpanded() const { return is_expanded_; } - void SetExpanded(bool value); - - void Draw(platform::graphics::IPainter* painter); - - IEvent* ScrollAttemptEvent() { return &scroll_attempt_event_; } - - void InstallHandlers(controls::Control* control); - void UninstallHandlers() { InstallHandlers(nullptr); } - - gsl::not_null> - GetCollapsedThumbBrush(); - // Brush could be nullptr to use the theme brush. - void SetCollapsedThumbBrush( - std::shared_ptr brush); - gsl::not_null> GetBrush( - ScrollBarBrushUsageKind usage, ScrollBarBrushStateKind state); - // Brush could be nullptr to use the theme brush. - void SetBrush(ScrollBarBrushUsageKind usage, ScrollBarBrushStateKind state, - std::shared_ptr brush); - - protected: - void OnDraw(platform::graphics::IPainter* painter, bool expand); - - virtual void DrawUpArrow( - platform::graphics::IPainter* painter, const Rect& area, - gsl::not_null arrow_brush, - gsl::not_null background_brush) = 0; - virtual void DrawDownArrow( - platform::graphics::IPainter* painter, const Rect& area, - gsl::not_null arrow_brush, - gsl::not_null background_brush) = 0; - - std::optional ExpandedHitTest(const Point& point); - - virtual bool IsShowBar() = 0; - - virtual std::optional GetExpandedAreaRect( - ScrollBarAreaKind area_kind) = 0; - virtual std::optional GetCollapsedTriggerExpandAreaRect() = 0; - virtual std::optional GetCollapsedThumbRect() = 0; - - virtual float CalculateNewScrollPosition(const Rect& thumb_original_rect, - const Point& mouse_offset) = 0; - - virtual bool CanScrollUp() = 0; - virtual bool CanScrollDown() = 0; - - private: - void SetCursor(); - void RestoreCursor(); - - void BeginAutoCollapseTimer(); - void StopAutoCollapseTimer(); - - void OnMouseLeave(); - - ScrollBarBrushStateKind GetState(ScrollBarAreaKind area); - - protected: - gsl::not_null render_object_; - - std::unique_ptr arrow_geometry_; - - private: - Direction direction_; - - bool is_enabled_ = true; - - bool is_expanded_ = false; - - std::shared_ptr collapsed_thumb_brush_; - std::unordered_map< - ScrollBarBrushUsageKind, - std::unordered_map>> - brushes_; - - Rect move_thumb_thumb_original_rect_; - std::optional move_thumb_start_; - - std::optional mouse_hover_; - std::optional mouse_press_; - - EventRevokerListGuard event_guard_; - - Event scroll_attempt_event_; - - bool cursor_overrided_ = false; - - platform::gui::TimerAutoCanceler auto_collapse_timer_canceler_; -}; - -class CRU_UI_API HorizontalScrollBar : public ScrollBar { - public: - explicit HorizontalScrollBar( - gsl::not_null render_object); - - CRU_DELETE_COPY(HorizontalScrollBar) - CRU_DELETE_MOVE(HorizontalScrollBar) - - ~HorizontalScrollBar() override = default; - - protected: - void DrawUpArrow( - platform::graphics::IPainter* painter, const Rect& area, - gsl::not_null arrow_brush, - gsl::not_null background_brush) override; - void DrawDownArrow( - platform::graphics::IPainter* painter, const Rect& area, - gsl::not_null arrow_brush, - gsl::not_null background_brush) override; - - bool IsShowBar() override; - - std::optional GetExpandedAreaRect(ScrollBarAreaKind area_kind) override; - std::optional GetCollapsedTriggerExpandAreaRect() override; - std::optional GetCollapsedThumbRect() override; - - float CalculateNewScrollPosition(const Rect& thumb_original_rect, - const Point& mouse_offset) override; - - bool CanScrollUp() override; - bool CanScrollDown() override; -}; - -class CRU_UI_API VerticalScrollBar : public ScrollBar { - public: - explicit VerticalScrollBar(gsl::not_null render_object); - - CRU_DELETE_COPY(VerticalScrollBar) - CRU_DELETE_MOVE(VerticalScrollBar) - - ~VerticalScrollBar() override = default; - - protected: - void DrawUpArrow( - platform::graphics::IPainter* painter, const Rect& area, - gsl::not_null arrow_brush, - gsl::not_null background_brush) override; - void DrawDownArrow( - platform::graphics::IPainter* painter, const Rect& area, - gsl::not_null arrow_brush, - gsl::not_null background_brush) override; - - bool IsShowBar() override; - - std::optional GetExpandedAreaRect(ScrollBarAreaKind area_kind) override; - std::optional GetCollapsedTriggerExpandAreaRect() override; - std::optional GetCollapsedThumbRect() override; - - float CalculateNewScrollPosition(const Rect& thumb_original_rect, - const Point& mouse_offset) override; - - bool CanScrollUp() override; - bool CanScrollDown() override; -}; - -// A delegate to draw scrollbar and register related events. -class CRU_UI_API ScrollBarDelegate : public Object { - public: - explicit ScrollBarDelegate(gsl::not_null render_object); - - CRU_DELETE_COPY(ScrollBarDelegate) - CRU_DELETE_MOVE(ScrollBarDelegate) - - ~ScrollBarDelegate() override = default; - - public: - bool IsHorizontalBarEnabled() const { return horizontal_bar_.IsEnabled(); } - void SetHorizontalBarEnabled(bool value) { - horizontal_bar_.SetEnabled(value); - } - - bool IsVerticalBarEnabled() const { return horizontal_bar_.IsEnabled(); } - void SetVerticalBarEnabled(bool value) { horizontal_bar_.SetEnabled(value); } - - IEvent* ScrollAttemptEvent() { return &scroll_attempt_event_; } - - void DrawScrollBar(platform::graphics::IPainter* painter); - - void InstallHandlers(controls::Control* control); - void UninstallHandlers() { InstallHandlers(nullptr); } - - private: - gsl::not_null render_object_; - - HorizontalScrollBar horizontal_bar_; - VerticalScrollBar vertical_bar_; - - Event scroll_attempt_event_; -}; -} // namespace cru::ui::render diff --git a/include/cru/ui/render/ScrollRenderObject.h b/include/cru/ui/render/ScrollRenderObject.h new file mode 100644 index 00000000..bb282953 --- /dev/null +++ b/include/cru/ui/render/ScrollRenderObject.h @@ -0,0 +1,98 @@ +#pragma once +#include "RenderObject.h" + +#include "cru/common/Event.h" +#include "cru/platform/graphics/util/Painter.h" +#include "cru/ui/Base.h" +#include "cru/ui/render/ScrollBar.h" + +#include +#include + +namespace cru::ui::render { +// Measure logic: +// Measure child with unspecified min and max size. +// If parent's preferred size is specified, then it is used as measure result. +// Or child's size is coerced into requirement and then used as result. +// If no child, then use the preferred size if set or min size if set or 0. +// Layout logic: +// If child is smaller than content area, layout at lefttop. +// Or layout by scroll state. +class CRU_UI_API ScrollRenderObject : public RenderObject { + public: + ScrollRenderObject(); + + CRU_DELETE_COPY(ScrollRenderObject) + CRU_DELETE_MOVE(ScrollRenderObject) + + ~ScrollRenderObject() override = default; + + RenderObject* HitTest(const Point& point) override; + + // Return the coerced scroll offset. + Point GetScrollOffset(); + float GetScrollOffset(Direction direction) { + return direction == Direction::Horizontal ? GetScrollOffset().x + : GetScrollOffset().y; + } + void SetScrollOffset(const Point& offset); + void SetScrollOffset(std::optional x, std::optional y); + void SetScrollOffset(Direction direction, std::optional value) { + if (direction == Direction::Horizontal) { + SetScrollOffset(value, std::nullopt); + } else { + SetScrollOffset(std::nullopt, value); + } + } + + void ApplyScroll(const Scroll& scroll); + + Point GetRawScrollOffset() const { return scroll_offset_; } + + // Return the viewable area rect. + // Lefttop is scroll offset. Size is content size. + // If size exceeds view area, left and top is more important when calculate + // new scroll offset. + Rect GetViewRect() { + return Rect{GetScrollOffset(), GetContentRect().GetSize()}; + } + + // Rect lefttop relative to content rect. + // Param margin is just for convenience and it will just add to the rect. + void ScrollToContain(const Rect& rect, const Thickness& margin = Thickness{}); + + std::u16string_view GetName() const override { return u"ScrollRenderObject"; } + + bool IsMouseWheelScrollEnabled() const { return is_mouse_wheel_enabled_; } + void SetMouseWheelScrollEnabled(bool enable); + + bool HorizontalCanScrollUp(); + bool HorizontalCanScrollDown(); + bool VerticalCanScrollUp(); + bool VerticalCanScrollDown(); + + protected: + void OnDrawCore(platform::graphics::IPainter* painter) override; + + // Logic: + // If available size is bigger than child's preferred size, then child's + // preferred size is taken. + // If not, all available size is taken while forming a scroll area. + Size OnMeasureContent(const MeasureRequirement& requirement, + const MeasureSize& preferred_size) override; + void OnLayoutContent(const Rect& content_rect) override; + + void OnAttachedControlChanged(controls::Control* control) override; + + void InstallMouseWheelHandler(controls::Control* control); + + private: + Point scroll_offset_; + + std::unique_ptr scroll_bar_delegate_; + + bool is_mouse_wheel_enabled_ = true; + + EventRevokerListGuard guard_; +}; +} // namespace cru::ui::render diff --git a/include/cru/ui/render/ScrollRenderObject.hpp b/include/cru/ui/render/ScrollRenderObject.hpp deleted file mode 100644 index 19814c51..00000000 --- a/include/cru/ui/render/ScrollRenderObject.hpp +++ /dev/null @@ -1,98 +0,0 @@ -#pragma once -#include "RenderObject.hpp" - -#include "cru/common/Event.hpp" -#include "cru/platform/graphics/util/Painter.hpp" -#include "cru/ui/Base.hpp" -#include "cru/ui/render/ScrollBar.hpp" - -#include -#include - -namespace cru::ui::render { -// Measure logic: -// Measure child with unspecified min and max size. -// If parent's preferred size is specified, then it is used as measure result. -// Or child's size is coerced into requirement and then used as result. -// If no child, then use the preferred size if set or min size if set or 0. -// Layout logic: -// If child is smaller than content area, layout at lefttop. -// Or layout by scroll state. -class CRU_UI_API ScrollRenderObject : public RenderObject { - public: - ScrollRenderObject(); - - CRU_DELETE_COPY(ScrollRenderObject) - CRU_DELETE_MOVE(ScrollRenderObject) - - ~ScrollRenderObject() override = default; - - RenderObject* HitTest(const Point& point) override; - - // Return the coerced scroll offset. - Point GetScrollOffset(); - float GetScrollOffset(Direction direction) { - return direction == Direction::Horizontal ? GetScrollOffset().x - : GetScrollOffset().y; - } - void SetScrollOffset(const Point& offset); - void SetScrollOffset(std::optional x, std::optional y); - void SetScrollOffset(Direction direction, std::optional value) { - if (direction == Direction::Horizontal) { - SetScrollOffset(value, std::nullopt); - } else { - SetScrollOffset(std::nullopt, value); - } - } - - void ApplyScroll(const Scroll& scroll); - - Point GetRawScrollOffset() const { return scroll_offset_; } - - // Return the viewable area rect. - // Lefttop is scroll offset. Size is content size. - // If size exceeds view area, left and top is more important when calculate - // new scroll offset. - Rect GetViewRect() { - return Rect{GetScrollOffset(), GetContentRect().GetSize()}; - } - - // Rect lefttop relative to content rect. - // Param margin is just for convenience and it will just add to the rect. - void ScrollToContain(const Rect& rect, const Thickness& margin = Thickness{}); - - std::u16string_view GetName() const override { return u"ScrollRenderObject"; } - - bool IsMouseWheelScrollEnabled() const { return is_mouse_wheel_enabled_; } - void SetMouseWheelScrollEnabled(bool enable); - - bool HorizontalCanScrollUp(); - bool HorizontalCanScrollDown(); - bool VerticalCanScrollUp(); - bool VerticalCanScrollDown(); - - protected: - void OnDrawCore(platform::graphics::IPainter* painter) override; - - // Logic: - // If available size is bigger than child's preferred size, then child's - // preferred size is taken. - // If not, all available size is taken while forming a scroll area. - Size OnMeasureContent(const MeasureRequirement& requirement, - const MeasureSize& preferred_size) override; - void OnLayoutContent(const Rect& content_rect) override; - - void OnAttachedControlChanged(controls::Control* control) override; - - void InstallMouseWheelHandler(controls::Control* control); - - private: - Point scroll_offset_; - - std::unique_ptr scroll_bar_delegate_; - - bool is_mouse_wheel_enabled_ = true; - - EventRevokerListGuard guard_; -}; -} // namespace cru::ui::render diff --git a/include/cru/ui/render/StackLayoutRenderObject.h b/include/cru/ui/render/StackLayoutRenderObject.h new file mode 100644 index 00000000..e141d16e --- /dev/null +++ b/include/cru/ui/render/StackLayoutRenderObject.h @@ -0,0 +1,57 @@ +#pragma once +#include "LayoutRenderObject.h" +#include "cru/ui/Base.h" + +namespace cru::ui::render { +// Measure Logic: +// Following rules are applied both horizontally and vertically. +// +// 1. Measure each children with min size not specified and max size as +// following rules: +// +// 1.1. If parent's preferred size is set and it is not less than child's min +// size, then it is used as child's max size. +// +// 1.2. Or if parent's max size is set, then it is used as child's max size. +// If it is less than child's min size, then log an warning about that. +// +// 2. Result size is children's max size. +// +// 3. If preferred size is specified and result size is smaller than it, coerce +// result size to preferred size. +// +// 4. If result size is smaller than min size (if specified), coerce result size +// to min size. +class CRU_UI_API StackLayoutRenderObject + : public LayoutRenderObject { + CRU_DEFINE_CLASS_LOG_TAG(u"cru::ui::render:StackLayoutRenderObject") + + public: + StackLayoutRenderObject() = default; + CRU_DELETE_COPY(StackLayoutRenderObject) + CRU_DELETE_MOVE(StackLayoutRenderObject) + ~StackLayoutRenderObject() = default; + + std::u16string_view GetName() const override { + return u"StackLayoutRenderObject"; + } + + Alignment GetDefaultHorizontalAlignment() const { + return default_vertical_alignment_; + } + void SetDefaultHorizontalAlignment(Alignment alignment); + Alignment GetDefaultVerticalAlignment() { + return default_horizontal_alignment_; + } + void SetDefaultVertialAlignment(Alignment alignment); + + protected: + Size OnMeasureContent(const MeasureRequirement& requirement, + const MeasureSize& preferred_size) override; + void OnLayoutContent(const Rect& content_rect) override; + + private: + Alignment default_horizontal_alignment_ = Alignment::Start; + Alignment default_vertical_alignment_ = Alignment::Start; +}; +} // namespace cru::ui::render diff --git a/include/cru/ui/render/StackLayoutRenderObject.hpp b/include/cru/ui/render/StackLayoutRenderObject.hpp deleted file mode 100644 index 2f832e55..00000000 --- a/include/cru/ui/render/StackLayoutRenderObject.hpp +++ /dev/null @@ -1,57 +0,0 @@ -#pragma once -#include "LayoutRenderObject.hpp" -#include "cru/ui/Base.hpp" - -namespace cru::ui::render { -// Measure Logic: -// Following rules are applied both horizontally and vertically. -// -// 1. Measure each children with min size not specified and max size as -// following rules: -// -// 1.1. If parent's preferred size is set and it is not less than child's min -// size, then it is used as child's max size. -// -// 1.2. Or if parent's max size is set, then it is used as child's max size. -// If it is less than child's min size, then log an warning about that. -// -// 2. Result size is children's max size. -// -// 3. If preferred size is specified and result size is smaller than it, coerce -// result size to preferred size. -// -// 4. If result size is smaller than min size (if specified), coerce result size -// to min size. -class CRU_UI_API StackLayoutRenderObject - : public LayoutRenderObject { - CRU_DEFINE_CLASS_LOG_TAG(u"cru::ui::render:StackLayoutRenderObject") - - public: - StackLayoutRenderObject() = default; - CRU_DELETE_COPY(StackLayoutRenderObject) - CRU_DELETE_MOVE(StackLayoutRenderObject) - ~StackLayoutRenderObject() = default; - - std::u16string_view GetName() const override { - return u"StackLayoutRenderObject"; - } - - Alignment GetDefaultHorizontalAlignment() const { - return default_vertical_alignment_; - } - void SetDefaultHorizontalAlignment(Alignment alignment); - Alignment GetDefaultVerticalAlignment() { - return default_horizontal_alignment_; - } - void SetDefaultVertialAlignment(Alignment alignment); - - protected: - Size OnMeasureContent(const MeasureRequirement& requirement, - const MeasureSize& preferred_size) override; - void OnLayoutContent(const Rect& content_rect) override; - - private: - Alignment default_horizontal_alignment_ = Alignment::Start; - Alignment default_vertical_alignment_ = Alignment::Start; -}; -} // namespace cru::ui::render diff --git a/include/cru/ui/render/TextRenderObject.h b/include/cru/ui/render/TextRenderObject.h new file mode 100644 index 00000000..3b5f581a --- /dev/null +++ b/include/cru/ui/render/TextRenderObject.h @@ -0,0 +1,121 @@ +#pragma once +#include "RenderObject.h" + +#include + +namespace cru::ui::render { +// Layout logic: +// 1. If preferred width is set then it is taken to do a text measure. If not +// set, then max width is taken to do that. +// 2. If the actual width of text after measure exceeds the required width, +// coerced value is returned and an error is reported. If preferred width is set +// and actual width is smaller than that, then preferred width is used. If +// preferred width is not set, then the same thing is applied to min width. +// 3. If actual height of text is bigger than max height, an error is reported +// and coerced value is returned. Or height is lifted up to be at least +// preferred size if set or min height. +// +// If the result layout box is bigger than actual text box, then text is center +// aligned. +class CRU_UI_API TextRenderObject : public RenderObject { + CRU_DEFINE_CLASS_LOG_TAG(u"cru::ui::render::TextRenderObject") + + public: + constexpr static float default_caret_width = 2; + + public: + TextRenderObject(std::shared_ptr brush, + std::shared_ptr font, + std::shared_ptr selection_brush, + std::shared_ptr caret_brush); + TextRenderObject(const TextRenderObject& other) = delete; + TextRenderObject(TextRenderObject&& other) = delete; + TextRenderObject& operator=(const TextRenderObject& other) = delete; + TextRenderObject& operator=(TextRenderObject&& other) = delete; + ~TextRenderObject() override; + + String GetText() const; + void SetText(String new_text); + + void SetBrush(std::shared_ptr new_brush); + + std::shared_ptr GetFont() const; + void SetFont(std::shared_ptr font); + + bool IsEditMode(); + void SetEditMode(bool enable); + + Index GetLineCount(); + Index GetLineIndexFromCharIndex(Index char_index); + float GetLineHeight(Index line_index); + std::vector TextRangeRect(const TextRange& text_range); + Rect TextSinglePoint(gsl::index position, bool trailing); + platform::graphics::TextHitTestResult TextHitTest(const Point& point); + + std::optional GetSelectionRange() const { + return selection_range_; + } + void SetSelectionRange(std::optional new_range); + + std::shared_ptr GetSelectionBrush() const { + return selection_brush_; + } + void SetSelectionBrush(std::shared_ptr new_brush); + + bool IsDrawCaret() const { return draw_caret_; } + void SetDrawCaret(bool draw_caret); + void ToggleDrawCaret() { SetDrawCaret(!IsDrawCaret()); } + + // Caret position can be any value. When it is negative, 0 is used. When it + // exceeds the size of the string, the size of the string is used. + gsl::index GetCaretPosition() const { return caret_position_; } + void SetCaretPosition(gsl::index position); + + // Lefttop relative to content lefttop. + Rect GetCaretRectInContent(); + // Lefttop relative to render object lefttop. + Rect GetCaretRect(); + + std::shared_ptr GetCaretBrush() const { + return caret_brush_; + } + void GetCaretBrush(std::shared_ptr brush); + + float GetCaretWidth() const { return caret_width_; } + void SetCaretWidth(float width); + + bool IsMeasureIncludingTrailingSpace() const { + return is_measure_including_trailing_space_; + } + void SetMeasureIncludingTrailingSpace(bool including); + + RenderObject* HitTest(const Point& point) override; + + std::u16string_view GetName() const override { return u"TextRenderObject"; } + + protected: + void OnDrawContent(platform::graphics::IPainter* painter) override; + + // See remarks of this class. + Size OnMeasureContent(const MeasureRequirement& requirement, + const MeasureSize& preferred_size) override; + void OnLayoutContent(const Rect& content_rect) override; + + void OnAfterLayout() override; + + private: + std::shared_ptr brush_; + std::shared_ptr font_; + std::unique_ptr text_layout_; + + std::optional selection_range_ = std::nullopt; + std::shared_ptr selection_brush_; + + bool draw_caret_ = false; + gsl::index caret_position_ = 0; + std::shared_ptr caret_brush_; + float caret_width_ = default_caret_width; + + bool is_measure_including_trailing_space_ = false; +}; +} // namespace cru::ui::render diff --git a/include/cru/ui/render/TextRenderObject.hpp b/include/cru/ui/render/TextRenderObject.hpp deleted file mode 100644 index 601bd0a8..00000000 --- a/include/cru/ui/render/TextRenderObject.hpp +++ /dev/null @@ -1,121 +0,0 @@ -#pragma once -#include "RenderObject.hpp" - -#include - -namespace cru::ui::render { -// Layout logic: -// 1. If preferred width is set then it is taken to do a text measure. If not -// set, then max width is taken to do that. -// 2. If the actual width of text after measure exceeds the required width, -// coerced value is returned and an error is reported. If preferred width is set -// and actual width is smaller than that, then preferred width is used. If -// preferred width is not set, then the same thing is applied to min width. -// 3. If actual height of text is bigger than max height, an error is reported -// and coerced value is returned. Or height is lifted up to be at least -// preferred size if set or min height. -// -// If the result layout box is bigger than actual text box, then text is center -// aligned. -class CRU_UI_API TextRenderObject : public RenderObject { - CRU_DEFINE_CLASS_LOG_TAG(u"cru::ui::render::TextRenderObject") - - public: - constexpr static float default_caret_width = 2; - - public: - TextRenderObject(std::shared_ptr brush, - std::shared_ptr font, - std::shared_ptr selection_brush, - std::shared_ptr caret_brush); - TextRenderObject(const TextRenderObject& other) = delete; - TextRenderObject(TextRenderObject&& other) = delete; - TextRenderObject& operator=(const TextRenderObject& other) = delete; - TextRenderObject& operator=(TextRenderObject&& other) = delete; - ~TextRenderObject() override; - - String GetText() const; - void SetText(String new_text); - - void SetBrush(std::shared_ptr new_brush); - - std::shared_ptr GetFont() const; - void SetFont(std::shared_ptr font); - - bool IsEditMode(); - void SetEditMode(bool enable); - - Index GetLineCount(); - Index GetLineIndexFromCharIndex(Index char_index); - float GetLineHeight(Index line_index); - std::vector TextRangeRect(const TextRange& text_range); - Rect TextSinglePoint(gsl::index position, bool trailing); - platform::graphics::TextHitTestResult TextHitTest(const Point& point); - - std::optional GetSelectionRange() const { - return selection_range_; - } - void SetSelectionRange(std::optional new_range); - - std::shared_ptr GetSelectionBrush() const { - return selection_brush_; - } - void SetSelectionBrush(std::shared_ptr new_brush); - - bool IsDrawCaret() const { return draw_caret_; } - void SetDrawCaret(bool draw_caret); - void ToggleDrawCaret() { SetDrawCaret(!IsDrawCaret()); } - - // Caret position can be any value. When it is negative, 0 is used. When it - // exceeds the size of the string, the size of the string is used. - gsl::index GetCaretPosition() const { return caret_position_; } - void SetCaretPosition(gsl::index position); - - // Lefttop relative to content lefttop. - Rect GetCaretRectInContent(); - // Lefttop relative to render object lefttop. - Rect GetCaretRect(); - - std::shared_ptr GetCaretBrush() const { - return caret_brush_; - } - void GetCaretBrush(std::shared_ptr brush); - - float GetCaretWidth() const { return caret_width_; } - void SetCaretWidth(float width); - - bool IsMeasureIncludingTrailingSpace() const { - return is_measure_including_trailing_space_; - } - void SetMeasureIncludingTrailingSpace(bool including); - - RenderObject* HitTest(const Point& point) override; - - std::u16string_view GetName() const override { return u"TextRenderObject"; } - - protected: - void OnDrawContent(platform::graphics::IPainter* painter) override; - - // See remarks of this class. - Size OnMeasureContent(const MeasureRequirement& requirement, - const MeasureSize& preferred_size) override; - void OnLayoutContent(const Rect& content_rect) override; - - void OnAfterLayout() override; - - private: - std::shared_ptr brush_; - std::shared_ptr font_; - std::unique_ptr text_layout_; - - std::optional selection_range_ = std::nullopt; - std::shared_ptr selection_brush_; - - bool draw_caret_ = false; - gsl::index caret_position_ = 0; - std::shared_ptr caret_brush_; - float caret_width_ = default_caret_width; - - bool is_measure_including_trailing_space_ = false; -}; -} // namespace cru::ui::render diff --git a/include/cru/ui/style/ApplyBorderStyleInfo.h b/include/cru/ui/style/ApplyBorderStyleInfo.h new file mode 100644 index 00000000..2e6753fc --- /dev/null +++ b/include/cru/ui/style/ApplyBorderStyleInfo.h @@ -0,0 +1,30 @@ +#pragma once +#include "../Base.h" + +#include + +namespace cru::ui::style { +struct ApplyBorderStyleInfo { + ApplyBorderStyleInfo() = default; + + explicit ApplyBorderStyleInfo( + std::optional> border_brush, + std::optional border_thickness = std::nullopt, + std::optional border_radius = std::nullopt, + std::optional> + foreground_brush = std::nullopt, + std::optional> + background_brush = std::nullopt) + : border_brush(std::move(border_brush)), + border_thickness(std::move(border_thickness)), + border_radius(std::move(border_radius)), + foreground_brush(std::move(foreground_brush)), + background_brush(std::move(background_brush)) {} + + std::optional> border_brush; + std::optional border_thickness; + std::optional border_radius; + std::optional> foreground_brush; + std::optional> background_brush; +}; +} // namespace cru::ui::style diff --git a/include/cru/ui/style/ApplyBorderStyleInfo.hpp b/include/cru/ui/style/ApplyBorderStyleInfo.hpp deleted file mode 100644 index 7ae5b2c5..00000000 --- a/include/cru/ui/style/ApplyBorderStyleInfo.hpp +++ /dev/null @@ -1,30 +0,0 @@ -#pragma once -#include "../Base.hpp" - -#include - -namespace cru::ui::style { -struct ApplyBorderStyleInfo { - ApplyBorderStyleInfo() = default; - - explicit ApplyBorderStyleInfo( - std::optional> border_brush, - std::optional border_thickness = std::nullopt, - std::optional border_radius = std::nullopt, - std::optional> - foreground_brush = std::nullopt, - std::optional> - background_brush = std::nullopt) - : border_brush(std::move(border_brush)), - border_thickness(std::move(border_thickness)), - border_radius(std::move(border_radius)), - foreground_brush(std::move(foreground_brush)), - background_brush(std::move(background_brush)) {} - - std::optional> border_brush; - std::optional border_thickness; - std::optional border_radius; - std::optional> foreground_brush; - std::optional> background_brush; -}; -} // namespace cru::ui::style diff --git a/include/cru/ui/style/Condition.h b/include/cru/ui/style/Condition.h new file mode 100644 index 00000000..221a7428 --- /dev/null +++ b/include/cru/ui/style/Condition.h @@ -0,0 +1,133 @@ +#pragma once +#include "../Base.h" +#include "cru/common/Base.h" +#include "cru/common/ClonablePtr.h" +#include "cru/common/Event.h" +#include "cru/ui/controls/IClickableControl.h" +#include "cru/ui/helper/ClickDetector.h" + +#include +#include +#include +#include + +namespace cru::ui::style { +class Condition : public Object { + public: + virtual std::vector ChangeOn( + controls::Control* control) const = 0; + virtual bool Judge(controls::Control* control) const = 0; + + virtual Condition* Clone() const = 0; +}; + +class NoCondition : public Condition { + public: + static ClonablePtr Create() { + return ClonablePtr(new NoCondition); + }; + + std::vector ChangeOn(controls::Control*) const override { + return {}; + } + + bool Judge(controls::Control*) const override { return true; } + + NoCondition* Clone() const override { return new NoCondition; } +}; + +class CompoundCondition : public Condition { + public: + explicit CompoundCondition(std::vector> conditions); + + std::vector ChangeOn(controls::Control* control) const override; + + protected: + std::vector> conditions_; +}; + +class AndCondition : public CompoundCondition { + public: + static ClonablePtr Create( + std::vector> conditions) { + return ClonablePtr(new AndCondition(std::move(conditions))); + } + + using CompoundCondition::CompoundCondition; + + bool Judge(controls::Control* control) const override; + + AndCondition* Clone() const override { return new AndCondition(conditions_); } +}; + +class OrCondition : public CompoundCondition { + public: + static ClonablePtr Create( + std::vector> conditions) { + return ClonablePtr(new OrCondition(std::move(conditions))); + } + + using CompoundCondition::CompoundCondition; + + bool Judge(controls::Control* control) const override; + + OrCondition* Clone() const override { return new OrCondition(conditions_); } +}; + +class FocusCondition : public Condition { + public: + static ClonablePtr Create(bool has_focus) { + return ClonablePtr(new FocusCondition(has_focus)); + } + + explicit FocusCondition(bool has_focus); + + std::vector ChangeOn(controls::Control* control) const override; + bool Judge(controls::Control* control) const override; + + FocusCondition* Clone() const override { + return new FocusCondition(has_focus_); + } + + private: + bool has_focus_; +}; + +class HoverCondition : public Condition { + public: + static ClonablePtr Create(bool hover) { + return ClonablePtr(new HoverCondition(hover)); + } + + explicit HoverCondition(bool hover) : hover_(hover) {} + + std::vector ChangeOn(controls::Control* control) const override; + bool Judge(controls::Control* control) const override; + + HoverCondition* Clone() const override { return new HoverCondition(hover_); } + + private: + bool hover_; +}; + +class ClickStateCondition : public Condition { + public: + static ClonablePtr Create( + helper::ClickState click_state) { + return ClonablePtr( + new ClickStateCondition(click_state)); + } + + explicit ClickStateCondition(helper::ClickState click_state); + + std::vector ChangeOn(controls::Control* control) const override; + bool Judge(controls::Control* control) const override; + + ClickStateCondition* Clone() const override { + return new ClickStateCondition(click_state_); + } + + private: + helper::ClickState click_state_; +}; +} // namespace cru::ui::style diff --git a/include/cru/ui/style/Condition.hpp b/include/cru/ui/style/Condition.hpp deleted file mode 100644 index 89da81f6..00000000 --- a/include/cru/ui/style/Condition.hpp +++ /dev/null @@ -1,133 +0,0 @@ -#pragma once -#include "../Base.hpp" -#include "cru/common/Base.hpp" -#include "cru/common/ClonablePtr.hpp" -#include "cru/common/Event.hpp" -#include "cru/ui/controls/IClickableControl.hpp" -#include "cru/ui/helper/ClickDetector.hpp" - -#include -#include -#include -#include - -namespace cru::ui::style { -class Condition : public Object { - public: - virtual std::vector ChangeOn( - controls::Control* control) const = 0; - virtual bool Judge(controls::Control* control) const = 0; - - virtual Condition* Clone() const = 0; -}; - -class NoCondition : public Condition { - public: - static ClonablePtr Create() { - return ClonablePtr(new NoCondition); - }; - - std::vector ChangeOn(controls::Control*) const override { - return {}; - } - - bool Judge(controls::Control*) const override { return true; } - - NoCondition* Clone() const override { return new NoCondition; } -}; - -class CompoundCondition : public Condition { - public: - explicit CompoundCondition(std::vector> conditions); - - std::vector ChangeOn(controls::Control* control) const override; - - protected: - std::vector> conditions_; -}; - -class AndCondition : public CompoundCondition { - public: - static ClonablePtr Create( - std::vector> conditions) { - return ClonablePtr(new AndCondition(std::move(conditions))); - } - - using CompoundCondition::CompoundCondition; - - bool Judge(controls::Control* control) const override; - - AndCondition* Clone() const override { return new AndCondition(conditions_); } -}; - -class OrCondition : public CompoundCondition { - public: - static ClonablePtr Create( - std::vector> conditions) { - return ClonablePtr(new OrCondition(std::move(conditions))); - } - - using CompoundCondition::CompoundCondition; - - bool Judge(controls::Control* control) const override; - - OrCondition* Clone() const override { return new OrCondition(conditions_); } -}; - -class FocusCondition : public Condition { - public: - static ClonablePtr Create(bool has_focus) { - return ClonablePtr(new FocusCondition(has_focus)); - } - - explicit FocusCondition(bool has_focus); - - std::vector ChangeOn(controls::Control* control) const override; - bool Judge(controls::Control* control) const override; - - FocusCondition* Clone() const override { - return new FocusCondition(has_focus_); - } - - private: - bool has_focus_; -}; - -class HoverCondition : public Condition { - public: - static ClonablePtr Create(bool hover) { - return ClonablePtr(new HoverCondition(hover)); - } - - explicit HoverCondition(bool hover) : hover_(hover) {} - - std::vector ChangeOn(controls::Control* control) const override; - bool Judge(controls::Control* control) const override; - - HoverCondition* Clone() const override { return new HoverCondition(hover_); } - - private: - bool hover_; -}; - -class ClickStateCondition : public Condition { - public: - static ClonablePtr Create( - helper::ClickState click_state) { - return ClonablePtr( - new ClickStateCondition(click_state)); - } - - explicit ClickStateCondition(helper::ClickState click_state); - - std::vector ChangeOn(controls::Control* control) const override; - bool Judge(controls::Control* control) const override; - - ClickStateCondition* Clone() const override { - return new ClickStateCondition(click_state_); - } - - private: - helper::ClickState click_state_; -}; -} // namespace cru::ui::style diff --git a/include/cru/ui/style/StyleRule.h b/include/cru/ui/style/StyleRule.h new file mode 100644 index 00000000..68b239f3 --- /dev/null +++ b/include/cru/ui/style/StyleRule.h @@ -0,0 +1,49 @@ +#pragma once +#include "../Base.h" +#include "Condition.h" +#include "Styler.h" +#include "cru/common/ClonablePtr.h" + +#include +#include + +namespace cru::ui::style { +class StyleRule : public Object { + public: + static ClonablePtr Create(ClonablePtr condition, + ClonablePtr styler, + String name = {}) { + return ClonablePtr(new StyleRule( + std::move(condition), std::move(styler), std::move(name))); + } + + StyleRule(ClonablePtr condition, ClonablePtr styler, + String name = {}); + + CRU_DEFAULT_COPY(StyleRule) + CRU_DEFAULT_MOVE(StyleRule) + + ~StyleRule() override = default; + + public: + String GetName() const { return name_; } + Condition* GetCondition() const { return condition_.get(); } + Styler* GetStyler() const { return styler_.get(); } + + StyleRule WithNewCondition(ClonablePtr condition, + String name = {}) const { + return StyleRule{std::move(condition), styler_, std::move(name)}; + } + + StyleRule WithNewStyler(ClonablePtr styler, String name = {}) const { + return StyleRule{condition_, std::move(styler), std::move(name)}; + } + + bool CheckAndApply(controls::Control* control) const; + + private: + ClonablePtr condition_; + ClonablePtr styler_; + String name_; +}; +} // namespace cru::ui::style diff --git a/include/cru/ui/style/StyleRule.hpp b/include/cru/ui/style/StyleRule.hpp deleted file mode 100644 index 743aa759..00000000 --- a/include/cru/ui/style/StyleRule.hpp +++ /dev/null @@ -1,49 +0,0 @@ -#pragma once -#include "../Base.hpp" -#include "Condition.hpp" -#include "Styler.hpp" -#include "cru/common/ClonablePtr.hpp" - -#include -#include - -namespace cru::ui::style { -class StyleRule : public Object { - public: - static ClonablePtr Create(ClonablePtr condition, - ClonablePtr styler, - String name = {}) { - return ClonablePtr(new StyleRule( - std::move(condition), std::move(styler), std::move(name))); - } - - StyleRule(ClonablePtr condition, ClonablePtr styler, - String name = {}); - - CRU_DEFAULT_COPY(StyleRule) - CRU_DEFAULT_MOVE(StyleRule) - - ~StyleRule() override = default; - - public: - String GetName() const { return name_; } - Condition* GetCondition() const { return condition_.get(); } - Styler* GetStyler() const { return styler_.get(); } - - StyleRule WithNewCondition(ClonablePtr condition, - String name = {}) const { - return StyleRule{std::move(condition), styler_, std::move(name)}; - } - - StyleRule WithNewStyler(ClonablePtr styler, String name = {}) const { - return StyleRule{condition_, std::move(styler), std::move(name)}; - } - - bool CheckAndApply(controls::Control* control) const; - - private: - ClonablePtr condition_; - ClonablePtr styler_; - String name_; -}; -} // namespace cru::ui::style diff --git a/include/cru/ui/style/StyleRuleSet.h b/include/cru/ui/style/StyleRuleSet.h new file mode 100644 index 00000000..34d0fad4 --- /dev/null +++ b/include/cru/ui/style/StyleRuleSet.h @@ -0,0 +1,80 @@ +#pragma once +#include "StyleRule.h" +#include "cru/common/Base.h" +#include "cru/common/Event.h" + +#include + +namespace cru::ui::style { +class StyleRuleSet : public Object { + public: + StyleRuleSet() = default; + explicit StyleRuleSet(std::shared_ptr parent); + + CRU_DELETE_COPY(StyleRuleSet) + CRU_DELETE_MOVE(StyleRuleSet) + + ~StyleRuleSet() override = default; + + public: + std::shared_ptr GetParent() const { return parent_; } + void SetParent(std::shared_ptr parent); + + gsl::index GetSize() const { return static_cast(rules_.size()); } + const std::vector& GetRules() const { return rules_; } + + void AddStyleRule(StyleRule rule) { + AddStyleRule(std::move(rule), GetSize()); + } + + void AddStyleRule(StyleRule rule, gsl::index index); + + void RemoveStyleRule(gsl::index index, gsl::index count = 1); + + void Clear() { RemoveStyleRule(0, GetSize()); } + + void Set(const StyleRuleSet& other, bool set_parent = false); + + const StyleRule& operator[](gsl::index index) const { return rules_[index]; } + + // Triggered whenever a change happened to this (rule add or remove, parent + // change ...). Subscribe to this and update style change listeners and style. + IEvent* ChangeEvent() { return &change_event_; } + + private: + void RaiseChangeEvent() { change_event_.Raise(nullptr); } + + private: + Event change_event_; + + std::shared_ptr parent_ = nullptr; + EventRevokerGuard parent_change_event_guard_; + + std::vector rules_; +}; + +class StyleRuleSetBind { + public: + StyleRuleSetBind(controls::Control* control, + std::shared_ptr ruleset); + + CRU_DELETE_COPY(StyleRuleSetBind) + CRU_DELETE_MOVE(StyleRuleSetBind) + + ~StyleRuleSetBind() = default; + + private: + void UpdateRuleSetChainCache(); + void UpdateChangeListener(); + void UpdateStyle(); + + private: + controls::Control* control_; + std::shared_ptr ruleset_; + + // child first, parent last. + std::vector ruleset_chain_cache_; + + EventRevokerListGuard guard_; +}; +} // namespace cru::ui::style diff --git a/include/cru/ui/style/StyleRuleSet.hpp b/include/cru/ui/style/StyleRuleSet.hpp deleted file mode 100644 index 32b02d78..00000000 --- a/include/cru/ui/style/StyleRuleSet.hpp +++ /dev/null @@ -1,80 +0,0 @@ -#pragma once -#include "StyleRule.hpp" -#include "cru/common/Base.hpp" -#include "cru/common/Event.hpp" - -#include - -namespace cru::ui::style { -class StyleRuleSet : public Object { - public: - StyleRuleSet() = default; - explicit StyleRuleSet(std::shared_ptr parent); - - CRU_DELETE_COPY(StyleRuleSet) - CRU_DELETE_MOVE(StyleRuleSet) - - ~StyleRuleSet() override = default; - - public: - std::shared_ptr GetParent() const { return parent_; } - void SetParent(std::shared_ptr parent); - - gsl::index GetSize() const { return static_cast(rules_.size()); } - const std::vector& GetRules() const { return rules_; } - - void AddStyleRule(StyleRule rule) { - AddStyleRule(std::move(rule), GetSize()); - } - - void AddStyleRule(StyleRule rule, gsl::index index); - - void RemoveStyleRule(gsl::index index, gsl::index count = 1); - - void Clear() { RemoveStyleRule(0, GetSize()); } - - void Set(const StyleRuleSet& other, bool set_parent = false); - - const StyleRule& operator[](gsl::index index) const { return rules_[index]; } - - // Triggered whenever a change happened to this (rule add or remove, parent - // change ...). Subscribe to this and update style change listeners and style. - IEvent* ChangeEvent() { return &change_event_; } - - private: - void RaiseChangeEvent() { change_event_.Raise(nullptr); } - - private: - Event change_event_; - - std::shared_ptr parent_ = nullptr; - EventRevokerGuard parent_change_event_guard_; - - std::vector rules_; -}; - -class StyleRuleSetBind { - public: - StyleRuleSetBind(controls::Control* control, - std::shared_ptr ruleset); - - CRU_DELETE_COPY(StyleRuleSetBind) - CRU_DELETE_MOVE(StyleRuleSetBind) - - ~StyleRuleSetBind() = default; - - private: - void UpdateRuleSetChainCache(); - void UpdateChangeListener(); - void UpdateStyle(); - - private: - controls::Control* control_; - std::shared_ptr ruleset_; - - // child first, parent last. - std::vector ruleset_chain_cache_; - - EventRevokerListGuard guard_; -}; -} // namespace cru::ui::style diff --git a/include/cru/ui/style/Styler.h b/include/cru/ui/style/Styler.h new file mode 100644 index 00000000..9bdec294 --- /dev/null +++ b/include/cru/ui/style/Styler.h @@ -0,0 +1,90 @@ +#pragma once +#include "../Base.h" +#include "ApplyBorderStyleInfo.h" +#include "cru/common/Base.h" +#include "cru/common/ClonablePtr.h" +#include "cru/platform/gui/Cursor.h" +#include "cru/ui/controls/Control.h" + +#include +#include + +namespace cru::ui::style { +class Styler : public Object { + public: + virtual void Apply(controls::Control* control) const = 0; + + virtual Styler* Clone() const = 0; +}; + +class CompoundStyler : public Styler { + public: + template + static ClonablePtr Create(ClonablePtr... s) { + return ClonablePtr( + new CompoundStyler(std::vector>{std::move(s)...})); + } + + static ClonablePtr Create( + std::vector> stylers) { + return ClonablePtr(new CompoundStyler(std::move(stylers))); + } + + explicit CompoundStyler(std::vector> stylers) + : stylers_(std::move(stylers)) {} + + void Apply(controls::Control* control) const override { + for (const auto& styler : stylers_) { + styler->Apply(control); + } + } + + virtual CompoundStyler* Clone() const override { + return new CompoundStyler(stylers_); + } + + private: + std::vector> stylers_; +}; + +class BorderStyler : public Styler { + public: + static ClonablePtr Create() { + return ClonablePtr(new BorderStyler()); + } + + static ClonablePtr Create(ApplyBorderStyleInfo style) { + return ClonablePtr(new BorderStyler(std::move(style))); + } + + BorderStyler() = default; + explicit BorderStyler(ApplyBorderStyleInfo style); + + void Apply(controls::Control* control) const override; + + BorderStyler* Clone() const override { return new BorderStyler(style_); } + + private: + ApplyBorderStyleInfo style_; +}; + +class CursorStyler : public Styler { + public: + static ClonablePtr Create( + std::shared_ptr cursor) { + return ClonablePtr(new CursorStyler(std::move(cursor))); + } + + static ClonablePtr Create(platform::gui::SystemCursorType type); + + explicit CursorStyler(std::shared_ptr cursor) + : cursor_(std::move(cursor)) {} + + void Apply(controls::Control* control) const override; + + CursorStyler* Clone() const override { return new CursorStyler(cursor_); } + + private: + std::shared_ptr cursor_; +}; +} // namespace cru::ui::style diff --git a/include/cru/ui/style/Styler.hpp b/include/cru/ui/style/Styler.hpp deleted file mode 100644 index 3bf0b027..00000000 --- a/include/cru/ui/style/Styler.hpp +++ /dev/null @@ -1,90 +0,0 @@ -#pragma once -#include "../Base.hpp" -#include "ApplyBorderStyleInfo.hpp" -#include "cru/common/Base.hpp" -#include "cru/common/ClonablePtr.hpp" -#include "cru/platform/gui/Cursor.hpp" -#include "cru/ui/controls/Control.hpp" - -#include -#include - -namespace cru::ui::style { -class Styler : public Object { - public: - virtual void Apply(controls::Control* control) const = 0; - - virtual Styler* Clone() const = 0; -}; - -class CompoundStyler : public Styler { - public: - template - static ClonablePtr Create(ClonablePtr... s) { - return ClonablePtr( - new CompoundStyler(std::vector>{std::move(s)...})); - } - - static ClonablePtr Create( - std::vector> stylers) { - return ClonablePtr(new CompoundStyler(std::move(stylers))); - } - - explicit CompoundStyler(std::vector> stylers) - : stylers_(std::move(stylers)) {} - - void Apply(controls::Control* control) const override { - for (const auto& styler : stylers_) { - styler->Apply(control); - } - } - - virtual CompoundStyler* Clone() const override { - return new CompoundStyler(stylers_); - } - - private: - std::vector> stylers_; -}; - -class BorderStyler : public Styler { - public: - static ClonablePtr Create() { - return ClonablePtr(new BorderStyler()); - } - - static ClonablePtr Create(ApplyBorderStyleInfo style) { - return ClonablePtr(new BorderStyler(std::move(style))); - } - - BorderStyler() = default; - explicit BorderStyler(ApplyBorderStyleInfo style); - - void Apply(controls::Control* control) const override; - - BorderStyler* Clone() const override { return new BorderStyler(style_); } - - private: - ApplyBorderStyleInfo style_; -}; - -class CursorStyler : public Styler { - public: - static ClonablePtr Create( - std::shared_ptr cursor) { - return ClonablePtr(new CursorStyler(std::move(cursor))); - } - - static ClonablePtr Create(platform::gui::SystemCursorType type); - - explicit CursorStyler(std::shared_ptr cursor) - : cursor_(std::move(cursor)) {} - - void Apply(controls::Control* control) const override; - - CursorStyler* Clone() const override { return new CursorStyler(cursor_); } - - private: - std::shared_ptr cursor_; -}; -} // namespace cru::ui::style -- cgit v1.2.3