From 7320f082c4bce5939cfa7c9a23765249dac55d07 Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 13 Mar 2022 18:51:19 +0800 Subject: ... --- include/cru/ui/model/IListChangeNotify.h | 47 ++++++++++++++++++++++++++++++++ include/cru/ui/style/StyleRuleSet.h | 12 +++++++- 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 include/cru/ui/model/IListChangeNotify.h (limited to 'include') diff --git a/include/cru/ui/model/IListChangeNotify.h b/include/cru/ui/model/IListChangeNotify.h new file mode 100644 index 00000000..a9fcd65b --- /dev/null +++ b/include/cru/ui/model/IListChangeNotify.h @@ -0,0 +1,47 @@ +#pragma once +#include "../Base.h" +#include "cru/common/Base.h" +#include "cru/common/Event.h" + +namespace cru::ui::model { +enum ListChangeType { + kItemAdd, + kItemRemove, + kItemSet, + kItemMove, + kClear, +}; + +struct CRU_UI_API ListChange { + ListChangeType type; + union { + Index position; + }; + union { + Index count; + Index new_position; + }; + + constexpr static ListChange ItemAdd(Index index, Index count = 1) { + return {ListChangeType::kItemAdd, index, count}; + } + + constexpr static ListChange ItemRemove(Index index, Index count = 1) { + return {ListChangeType::kItemRemove, index, count}; + } + + constexpr static ListChange ItemSet(Index index, Index count = 1) { + return {ListChangeType::kItemSet, index, count}; + } + + constexpr static ListChange ItemMove(Index position, Index new_position) { + return {ListChangeType::kItemMove, position, new_position}; + } + + constexpr static ListChange Clear() { return {ListChangeType::kClear}; } +}; + +struct CRU_UI_API IListChangeNotify : virtual Interface { + virtual IEvent* ListChangeEvent() = 0; +}; +} // namespace cru::ui::model diff --git a/include/cru/ui/style/StyleRuleSet.h b/include/cru/ui/style/StyleRuleSet.h index 8bf9c9ff..8f3495bc 100644 --- a/include/cru/ui/style/StyleRuleSet.h +++ b/include/cru/ui/style/StyleRuleSet.h @@ -2,6 +2,7 @@ #include "StyleRule.h" #include "cru/common/Base.h" #include "cru/common/Event.h" +#include "cru/ui/model/IListChangeNotify.h" #include @@ -12,7 +13,7 @@ namespace cru::ui::style { * mutable and has reference semantics. Change of it will be notified by * StyleRuleSet::ChangeEvent. */ -class CRU_UI_API StyleRuleSet : public Object { +class CRU_UI_API StyleRuleSet : public Object, public model::IListChangeNotify { public: StyleRuleSet() = default; explicit StyleRuleSet(std::shared_ptr parent); @@ -50,11 +51,20 @@ class CRU_UI_API StyleRuleSet : public Object { // change ...). Subscribe to this and update style change listeners and style. IEvent* ChangeEvent() { return &change_event_; } + IEvent* ListChangeEvent() override { + return &list_change_event_; + } + private: void RaiseChangeEvent() { change_event_.Raise(nullptr); } + void RaiseChangeEvent(model::ListChange list_change) { + list_change_event_.Raise(list_change); + change_event_.Raise(nullptr); + } private: Event change_event_; + Event list_change_event_; std::shared_ptr parent_ = nullptr; EventRevokerGuard parent_change_event_guard_; -- cgit v1.2.3