aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-03-03 20:22:36 +0800
committercrupest <crupest@outlook.com>2020-03-03 20:22:36 +0800
commit47053829c322c43032244937cb63f9da178b852d (patch)
treed17b81cdf61b3a48eb978176cfb57dda841da9d1
parentb0946c0e6dc163fe981f230302a1976449150907 (diff)
downloadcru-47053829c322c43032244937cb63f9da178b852d.tar.gz
cru-47053829c322c43032244937cb63f9da178b852d.tar.bz2
cru-47053829c322c43032244937cb63f9da178b852d.zip
Write canvas render object layout logic.
-rw-r--r--include/cru/ui/render/canvas_render_object.hpp11
-rw-r--r--include/cru/ui/render/layout_utility.hpp6
-rw-r--r--src/ui/CMakeLists.txt2
-rw-r--r--src/ui/render/canvas_render_object.cpp8
-rw-r--r--src/ui/render/layout_utility.cpp15
5 files changed, 40 insertions, 2 deletions
diff --git a/include/cru/ui/render/canvas_render_object.hpp b/include/cru/ui/render/canvas_render_object.hpp
index 5fea8755..e3388014 100644
--- a/include/cru/ui/render/canvas_render_object.hpp
+++ b/include/cru/ui/render/canvas_render_object.hpp
@@ -21,6 +21,9 @@ class CanvasPaintEventArgs {
Rect paint_rect_;
};
+// The measure logic for `CanvasRenderObject` is that you set a desired size by
+// `SetDesiredSize` (not `SetPreferredSize`) and it will compare desired size
+// and available size and use the smaller one (by `Min`).
class CanvasRenderObject : public RenderObject {
public:
CanvasRenderObject();
@@ -35,6 +38,12 @@ class CanvasRenderObject : public RenderObject {
RenderObject* HitTest(const Point& point) override;
+ Size GetDesiredSize() const { return desired_size_; }
+
+ // Set the desired size. This is the content size excluding padding and
+ // margin.
+ void SetDesiredSize(const Size& new_size) { desired_size_ = new_size; }
+
IEvent<CanvasPaintEventArgs>* PaintEvent() { return &paint_event_; }
protected:
@@ -42,6 +51,8 @@ class CanvasRenderObject : public RenderObject {
void OnLayoutContent(const Rect& content_rect) override;
private:
+ Size desired_size_{};
+
Event<CanvasPaintEventArgs> paint_event_;
};
} // namespace cru::ui::render
diff --git a/include/cru/ui/render/layout_utility.hpp b/include/cru/ui/render/layout_utility.hpp
new file mode 100644
index 00000000..38191ad5
--- /dev/null
+++ b/include/cru/ui/render/layout_utility.hpp
@@ -0,0 +1,6 @@
+#include "../base.hpp"
+
+namespace cru::ui::render {
+ Size Min(const Size& left, const Size& right);
+ Size Max(const Size& left, const Size& right);
+}
diff --git a/src/ui/CMakeLists.txt b/src/ui/CMakeLists.txt
index cb34e590..2220740e 100644
--- a/src/ui/CMakeLists.txt
+++ b/src/ui/CMakeLists.txt
@@ -21,6 +21,7 @@ add_library(cru_ui STATIC
render/border_render_object.cpp
render/canvas_render_object.cpp
render/flex_layout_render_object.cpp
+ render/layout_utility.cpp
render/render_object.cpp
render/stack_layout_render_object.cpp
render/text_render_object.cpp
@@ -46,6 +47,7 @@ target_sources(cru_ui PUBLIC
${CRU_UI_INCLUDE_DIR}/render/canvas_render_object.hpp
${CRU_UI_INCLUDE_DIR}/render/flex_layout_render_object.hpp
${CRU_UI_INCLUDE_DIR}/render/layout_render_object.hpp
+ ${CRU_UI_INCLUDE_DIR}/render/layout_utility.hpp
${CRU_UI_INCLUDE_DIR}/render/render_object.hpp
${CRU_UI_INCLUDE_DIR}/render/stack_layout_render_object.hpp
${CRU_UI_INCLUDE_DIR}/render/text_render_object.hpp
diff --git a/src/ui/render/canvas_render_object.cpp b/src/ui/render/canvas_render_object.cpp
index 0a442317..0508e276 100644
--- a/src/ui/render/canvas_render_object.cpp
+++ b/src/ui/render/canvas_render_object.cpp
@@ -1,5 +1,7 @@
#include "cru/ui/render/canvas_render_object.hpp"
+#include "cru/ui/render/layout_utility.hpp"
+
namespace cru::ui::render {
CanvasRenderObject::CanvasRenderObject() : RenderObject(ChildMode::None) {}
@@ -17,8 +19,10 @@ RenderObject* CanvasRenderObject::HitTest(const Point& point) {
}
Size CanvasRenderObject::OnMeasureContent(const Size& available_size) {
- return Size{};
+ return Min(available_size, GetDesiredSize());
}
-void CanvasRenderObject::OnLayoutContent(const Rect& content_rect) {}
+void CanvasRenderObject::OnLayoutContent(const Rect& content_rect) {
+ CRU_UNUSED(content_rect)
+}
} // namespace cru::ui::render
diff --git a/src/ui/render/layout_utility.cpp b/src/ui/render/layout_utility.cpp
new file mode 100644
index 00000000..aae62d35
--- /dev/null
+++ b/src/ui/render/layout_utility.cpp
@@ -0,0 +1,15 @@
+#include "cru/ui/render/layout_utility.hpp"
+
+#include <algorithm>
+
+namespace cru::ui::render {
+Size Min(const Size& left, const Size& right) {
+ return Size{std::min(left.width, right.width),
+ std::min(left.height, right.height)};
+}
+
+Size Max(const Size& left, const Size& right) {
+ return Size{std::max(left.width, right.width),
+ std::max(left.height, right.height)};
+}
+} // namespace cru::ui::render