From 5729a5aa1b443e3e25f3e14dee29636d3b31a6f8 Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 18 Oct 2020 21:09:21 +0800 Subject: ... --- include/cru/common/Format.hpp | 23 +++++++++++++++++++ include/cru/common/Logger.hpp | 2 ++ include/cru/platform/GraphBase.hpp | 9 ++++++++ include/cru/ui/Base.hpp | 3 +++ include/cru/ui/UiManager.hpp | 4 ++++ include/cru/ui/render/FlexLayoutRenderObject.hpp | 4 ++++ include/cru/ui/render/MeasureRequirement.hpp | 28 ++++++++++++++++++++---- include/cru/ui/render/RenderObject.hpp | 7 ++++++ include/cru/ui/render/WindowRenderObject.hpp | 5 +++++ 9 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 include/cru/common/Format.hpp (limited to 'include') diff --git a/include/cru/common/Format.hpp b/include/cru/common/Format.hpp new file mode 100644 index 00000000..59f34036 --- /dev/null +++ b/include/cru/common/Format.hpp @@ -0,0 +1,23 @@ +#pragma once +#include "Base.hpp" + +#include "StringUtil.hpp" + +#include +#include +#include +#include +#include +#include + +namespace cru { +template >> +std::u16string ToUtf16String(T number) { + std::array buffer; + auto result = + std::to_chars(buffer.data(), buffer.data() + buffer.size(), number); + Ensures(result.ec == std::errc()); + std::string_view utf8_result(buffer.data(), result.ptr - buffer.data()); + return ToUtf16(utf8_result); +} +} // namespace cru diff --git a/include/cru/common/Logger.hpp b/include/cru/common/Logger.hpp index 4ea17c09..daf2e7d2 100644 --- a/include/cru/common/Logger.hpp +++ b/include/cru/common/Logger.hpp @@ -40,6 +40,7 @@ class Logger : public Object { std::list> sources_; }; +// TODO: Remove argument evaluation in Debug. template void Debug([[maybe_unused]] TArgs&&... args) { #ifdef CRU_DEBUG @@ -66,6 +67,7 @@ void Error(TArgs&&... args) { fmt::format(std::forward(args)...)); } +// TODO: Remove argument evaluation in Debug. template void TagDebug([[maybe_unused]] std::u16string_view tag, [[maybe_unused]] TArgs&&... args) { diff --git a/include/cru/platform/GraphBase.hpp b/include/cru/platform/GraphBase.hpp index 186ee9d0..571b36f7 100644 --- a/include/cru/platform/GraphBase.hpp +++ b/include/cru/platform/GraphBase.hpp @@ -1,9 +1,13 @@ #pragma once #include "cru/common/Base.hpp" +#include "cru/common/Format.hpp" + +#include #include #include #include +#include #include namespace cru::platform { @@ -46,6 +50,11 @@ struct Size final { std::numeric_limits::max()}; } + std::u16string ToDebugString() const { + return fmt::format(u"({}, {})", ToUtf16String(width), + ToUtf16String(height)); + } + float width = 0; float height = 0; }; diff --git a/include/cru/ui/Base.hpp b/include/cru/ui/Base.hpp index 6be359ab..0c0a4783 100644 --- a/include/cru/ui/Base.hpp +++ b/include/cru/ui/Base.hpp @@ -8,6 +8,9 @@ #include #include +// Change 0 to 1 to enable debug layout log. +#define CRUUI_DEBUG_LAYOUT 0 + namespace cru::ui { //-------------------- region: import -------------------- using cru::platform::Color; diff --git a/include/cru/ui/UiManager.hpp b/include/cru/ui/UiManager.hpp index e6facdbd..46f06ac2 100644 --- a/include/cru/ui/UiManager.hpp +++ b/include/cru/ui/UiManager.hpp @@ -3,8 +3,12 @@ #include "controls/Base.hpp" +#include +#include + namespace cru::ui { struct ThemeResources { + std::u16string default_font_family; std::shared_ptr default_font; std::shared_ptr text_brush; std::shared_ptr text_selection_brush; diff --git a/include/cru/ui/render/FlexLayoutRenderObject.hpp b/include/cru/ui/render/FlexLayoutRenderObject.hpp index ee29d1e4..a8154487 100644 --- a/include/cru/ui/render/FlexLayoutRenderObject.hpp +++ b/include/cru/ui/render/FlexLayoutRenderObject.hpp @@ -1,6 +1,8 @@ #pragma once #include "LayoutRenderObject.hpp" +#include + namespace cru::ui::render { // Measure Logic (v0.1): // Cross axis measure logic is the same as stack layout. @@ -85,6 +87,8 @@ class FlexLayoutRenderObject : public LayoutRenderObject { 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; diff --git a/include/cru/ui/render/MeasureRequirement.hpp b/include/cru/ui/render/MeasureRequirement.hpp index 2be159f8..6a0c6952 100644 --- a/include/cru/ui/render/MeasureRequirement.hpp +++ b/include/cru/ui/render/MeasureRequirement.hpp @@ -1,8 +1,12 @@ #pragma once #include "Base.hpp" +#include "cru/common/Format.hpp" + +#include #include #include +#include namespace cru::ui::render { constexpr Size Min(const Size& left, const Size& right) { @@ -112,6 +116,11 @@ class MeasureLength final { } } + std::u16string ToDebugString() const { + return IsSpecified() ? ToUtf16String(GetLengthOrUndefined()) + : u"UNSPECIFIED"; + } + private: // -1 for not specify float length_; @@ -160,6 +169,11 @@ struct MeasureSize { }; } + std::u16string ToDebugString() const { + return fmt::format(u"({}, {})", width.ToDebugString(), + height.ToDebugString()); + } + constexpr static MeasureSize NotSpecified() { return MeasureSize{MeasureLength::NotSpecified(), MeasureLength::NotSpecified()}; @@ -187,10 +201,11 @@ struct MeasureRequirement { : max(max), min(min) {} constexpr bool Satisfy(const Size& size) const { - return max.width.GetLengthOrMax() >= size.width && - max.height.GetLengthOrMax() >= size.height && - min.width.GetLengthOr0() <= size.width && - min.height.GetLengthOr0() <= size.height; + 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 { @@ -225,6 +240,11 @@ struct MeasureRequirement { return result; } + std::u16string ToDebugString() const { + return fmt::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), diff --git a/include/cru/ui/render/RenderObject.hpp b/include/cru/ui/render/RenderObject.hpp index f820f029..57251e3a 100644 --- a/include/cru/ui/render/RenderObject.hpp +++ b/include/cru/ui/render/RenderObject.hpp @@ -4,6 +4,9 @@ #include "MeasureRequirement.hpp" #include "cru/common/Event.hpp" +#include +#include + namespace cru::ui::render { // Render object will not destroy its children when destroyed. Control must @@ -133,6 +136,10 @@ class RenderObject : public Object { void InvalidateLayout(); void InvalidatePaint(); + public: + virtual std::u16string_view GetName() const; + std::u16string GetDebugPathInTree() const; + protected: void SetChildMode(ChildMode mode) { child_mode_ = mode; } diff --git a/include/cru/ui/render/WindowRenderObject.hpp b/include/cru/ui/render/WindowRenderObject.hpp index 4c254f42..d2ca5526 100644 --- a/include/cru/ui/render/WindowRenderObject.hpp +++ b/include/cru/ui/render/WindowRenderObject.hpp @@ -1,6 +1,8 @@ #pragma once #include "RenderObject.hpp" +#include + namespace cru::ui::render { class WindowRenderObject : public RenderObject { public: @@ -13,6 +15,9 @@ class WindowRenderObject : public RenderObject { RenderObject* HitTest(const Point& point) override; + public: + std::u16string_view GetName() const override; + protected: Size OnMeasureContent(const MeasureRequirement& requirement, const MeasureSize& preferred_size) override; -- cgit v1.2.3