From 4a0c86d94a06e72be0988062d49a19c05142434a Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 27 Feb 2021 22:16:28 +0800 Subject: ... --- include/cru/ui/render/ScrollBar.hpp | 144 +++++++++++++++++++++++++++ include/cru/ui/render/ScrollBarDelegate.hpp | 142 -------------------------- include/cru/ui/render/ScrollRenderObject.hpp | 2 +- 3 files changed, 145 insertions(+), 143 deletions(-) create mode 100644 include/cru/ui/render/ScrollBar.hpp delete mode 100644 include/cru/ui/render/ScrollBarDelegate.hpp (limited to 'include/cru/ui/render') diff --git a/include/cru/ui/render/ScrollBar.hpp b/include/cru/ui/render/ScrollBar.hpp new file mode 100644 index 00000000..a9be49a3 --- /dev/null +++ b/include/cru/ui/render/ScrollBar.hpp @@ -0,0 +1,144 @@ +#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/Painter.hpp" +#include "cru/platform/gui/UiApplication.hpp" +#include "cru/ui/Base.hpp" +#include "cru/ui/controls/Control.hpp" + +#include +#include +#include + +namespace cru::ui::render { +class ScrollRenderObject; + +enum class ScrollKind { Absolute, Page, Line }; + +struct Scroll { + Direction direction; + ScrollKind kind; + float offset; +}; + +enum class ScrollBarAreaKind { + UpArrow, // Line up + DownArrow, // Line down + UpThumb, // Page up + DownThumb, // Page down + Thumb +}; + +class ScrollBar : public Object { + public: + explicit ScrollBar(gsl::not_null render_object); + + CRU_DELETE_COPY(ScrollBar) + CRU_DELETE_MOVE(ScrollBar) + + ~ScrollBar() override = default; + + public: + bool IsEnabled() const { return is_enabled_; } + void SetEnabled(bool value); + + void Draw(platform::graphics::IPainter* painter); + + virtual std::optional HitTest(const Point& point) = 0; + + IEvent* ScrollAttemptEvent() { return &scroll_attempt_event_; } + + void InstallHandlers(controls::Control* control); + void UninstallHandlers() { InstallHandlers(nullptr); } + + gsl::not_null> + GetCollapseThumbBrush() const; + + protected: + virtual void OnDraw(platform::graphics::IPainter* painter, bool expand) = 0; + + protected: + gsl::not_null render_object_; + + private: + bool is_enabled_ = true; + + bool is_expanded_ = false; + + std::shared_ptr collapse_thumb_brush_; + + EventRevokerListGuard event_guard_; + + Event scroll_attempt_event_; +}; + +class HorizontalScrollBar : public ScrollBar { + public: + explicit HorizontalScrollBar( + gsl::not_null render_object); + + CRU_DELETE_COPY(HorizontalScrollBar) + CRU_DELETE_MOVE(HorizontalScrollBar) + + ~HorizontalScrollBar() override = default; + + public: + std::optional HitTest(const Point& point) override; + + protected: + void OnDraw(platform::graphics::IPainter* painter, bool expand) override; +}; + +class VerticalScrollBar : public ScrollBar { + public: + explicit VerticalScrollBar(gsl::not_null render_object); + + CRU_DELETE_COPY(VerticalScrollBar) + CRU_DELETE_MOVE(VerticalScrollBar) + + ~VerticalScrollBar() override = default; + + public: + std::optional HitTest(const Point& point) override; + + protected: + void OnDraw(platform::graphics::IPainter* painter, bool expand) override; +}; + +// A delegate to draw scrollbar and register related events. +class 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/ScrollBarDelegate.hpp b/include/cru/ui/render/ScrollBarDelegate.hpp deleted file mode 100644 index e5c63f6d..00000000 --- a/include/cru/ui/render/ScrollBarDelegate.hpp +++ /dev/null @@ -1,142 +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/Painter.hpp" -#include "cru/platform/gui/UiApplication.hpp" -#include "cru/ui/controls/Control.hpp" - -#include -#include -#include - -namespace cru::ui::render { -class ScrollRenderObject; - -enum class ScrollBarAreaKind { - UpArrow, // Line up - DownArrow, // Line down - UpThumb, // Page up - DownThumb, // Page down - Thumb -}; - -class ScrollBar : public Object { - public: - explicit ScrollBar(gsl::not_null render_object); - - CRU_DELETE_COPY(ScrollBar) - CRU_DELETE_MOVE(ScrollBar) - - ~ScrollBar() override = default; - - public: - bool IsEnabled() const { return is_enabled_; } - void SetEnabled(bool value); - - void Draw(platform::graphics::IPainter* painter); - - virtual std::optional HitTest(const Point& point) = 0; - - IEvent* ScrollAttemptEvent() { return &scroll_attempt_event_; } - - void InstallHandlers(controls::Control* control); - void UninstallHandlers() { InstallHandlers(nullptr); } - - gsl::not_null> - GetCollapseThumbBrush() const; - - protected: - virtual void OnDraw(platform::graphics::IPainter* painter, bool expand) = 0; - - protected: - gsl::not_null render_object_; - - private: - bool is_enabled_ = true; - - bool is_expanded_ = false; - - std::shared_ptr collapse_thumb_brush_; - - EventRevokerListGuard event_guard_; - - Event scroll_attempt_event_; -}; - -class HorizontalScrollBar : public ScrollBar { - public: - explicit HorizontalScrollBar( - gsl::not_null render_object); - - CRU_DELETE_COPY(HorizontalScrollBar) - CRU_DELETE_MOVE(HorizontalScrollBar) - - ~HorizontalScrollBar() override = default; - - public: - std::optional HitTest(const Point& point) override; - - protected: - void OnDraw(platform::graphics::IPainter* painter, bool expand) override; -}; - -class VerticalScrollBar : public ScrollBar { - public: - explicit VerticalScrollBar(gsl::not_null render_object); - - CRU_DELETE_COPY(VerticalScrollBar) - CRU_DELETE_MOVE(VerticalScrollBar) - - ~VerticalScrollBar() override = default; - - public: - std::optional HitTest(const Point& point) override; - - protected: - void OnDraw(platform::graphics::IPainter* painter, bool expand) override; -}; - -struct ScrollBarScrollAttemptArgs { - float x_offset; - float y_offset; -}; - -// A delegate to draw scrollbar and register related events. -class 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.hpp b/include/cru/ui/render/ScrollRenderObject.hpp index 5a431527..6a6ef198 100644 --- a/include/cru/ui/render/ScrollRenderObject.hpp +++ b/include/cru/ui/render/ScrollRenderObject.hpp @@ -2,7 +2,7 @@ #include "RenderObject.hpp" #include "cru/platform/graphics/util/Painter.hpp" -#include "cru/ui/render/ScrollBarDelegate.hpp" +#include "cru/ui/render/ScrollBar.hpp" #include #include -- cgit v1.2.3