From 46ff47d2f47a66372ca0a8a09dd08c4fb04004f3 Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 17 Oct 2020 15:57:53 +0800 Subject: Refactor timer. --- include/cru/common/Event.hpp | 29 +++++++++++++++++++++++++-- include/cru/platform/native/UiApplication.hpp | 10 ++++----- include/cru/win/native/GodWindow.hpp | 9 +++++++++ include/cru/win/native/UiApplication.hpp | 2 +- 4 files changed, 42 insertions(+), 8 deletions(-) (limited to 'include') diff --git a/include/cru/common/Event.hpp b/include/cru/common/Event.hpp index 377ca7f3..6417bc78 100644 --- a/include/cru/common/Event.hpp +++ b/include/cru/common/Event.hpp @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -183,6 +184,7 @@ struct EventRevokerDestroyer { }; } // namespace details +// A guard class for event revoker. Automatically revoke it when destroyed. class EventRevokerGuard { public: EventRevokerGuard() = default; @@ -201,7 +203,7 @@ class EventRevokerGuard { return *revoker_; } - void Release() { revoker_.release(); } + EventRevoker Release() { return std::move(*revoker_.release()); } void Reset(EventRevoker&& revoker) { revoker_.reset(new EventRevoker(std::move(revoker))); @@ -209,5 +211,28 @@ class EventRevokerGuard { private: std::unique_ptr revoker_; -}; // namespace cru +}; + +class EventRevokerListGuard { + public: + EventRevokerListGuard() = default; + EventRevokerListGuard(const EventRevokerListGuard& other) = delete; + EventRevokerListGuard(EventRevokerListGuard&& other) = default; + EventRevokerListGuard& operator=(const EventRevokerListGuard& other) = delete; + EventRevokerListGuard& operator=(EventRevokerListGuard&& other) = default; + ~EventRevokerListGuard() = default; + + public: + void Add(EventRevoker&& revoker) { + event_revoker_guard_list_.push_back(EventRevokerGuard(std::move(revoker))); + } + + EventRevokerListGuard& operator+=(EventRevoker&& revoker) { + this->Add(std::move(revoker)); + return *this; + } + + private: + std::vector event_revoker_guard_list_; +}; } // namespace cru diff --git a/include/cru/platform/native/UiApplication.hpp b/include/cru/platform/native/UiApplication.hpp index 1aa4df57..135e95c3 100644 --- a/include/cru/platform/native/UiApplication.hpp +++ b/include/cru/platform/native/UiApplication.hpp @@ -31,16 +31,16 @@ struct IUiApplication : public virtual INativeResource { virtual void AddOnQuitHandler(std::function handler) = 0; - virtual void InvokeLater(std::function action) = 0; - // Timer id should always be positive and never the same. So it's ok to use - // negative value to represent no timer. + // Timer id should always be positive (not 0) and never the same. So it's ok + // to use negative value (or 0) to represent no timer. + virtual long long SetImmediate(std::function action) = 0; virtual long long SetTimeout(std::chrono::milliseconds milliseconds, std::function action) = 0; virtual long long SetInterval(std::chrono::milliseconds milliseconds, std::function action) = 0; // Implementation should guarantee calls on timer id already canceled have no - // effects and do not crash. Also canceling negative id should always result - // in no-op. + // effects and do not crash. Also canceling negative id or 0 should always + // result in no-op. virtual void CancelTimer(long long id) = 0; virtual std::vector GetAllWindow() = 0; diff --git a/include/cru/win/native/GodWindow.hpp b/include/cru/win/native/GodWindow.hpp index 8b20e01f..93d1acad 100644 --- a/include/cru/win/native/GodWindow.hpp +++ b/include/cru/win/native/GodWindow.hpp @@ -1,6 +1,9 @@ #pragma once #include "Base.hpp" +#include "WindowNativeMessageEventArgs.hpp" +#include "cru/common/Event.hpp" + #include namespace cru::platform::native::win { @@ -20,10 +23,16 @@ class GodWindow : public Object { bool HandleGodWindowMessage(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param, LRESULT* result); + IEvent* MessageEvent() { + return &message_event_; + } + private: WinUiApplication* application_; std::unique_ptr god_window_class_; HWND hwnd_; + + Event message_event_; }; } // namespace cru::platform::native::win diff --git a/include/cru/win/native/UiApplication.hpp b/include/cru/win/native/UiApplication.hpp index cbc08af7..328a6b84 100644 --- a/include/cru/win/native/UiApplication.hpp +++ b/include/cru/win/native/UiApplication.hpp @@ -32,7 +32,7 @@ class WinUiApplication : public WinNativeResource, void AddOnQuitHandler(std::function handler) override; - void InvokeLater(std::function action) override; + long long SetImmediate(std::function action) override; long long SetTimeout(std::chrono::milliseconds milliseconds, std::function action) override; long long SetInterval(std::chrono::milliseconds milliseconds, -- cgit v1.2.3