diff options
author | crupest <crupest@outlook.com> | 2022-02-08 16:53:51 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-02-08 16:53:51 +0800 |
commit | 74bb9cd27242b9320f99ff4d2b50c3051576cc14 (patch) | |
tree | 744bac5799c593d1d6f81e7b09581bea626f2cde /src/win/gui/TimerManager.h | |
parent | b90c398de829d1ba5329651d75bae82f5e4085fe (diff) | |
download | cru-74bb9cd27242b9320f99ff4d2b50c3051576cc14.tar.gz cru-74bb9cd27242b9320f99ff4d2b50c3051576cc14.tar.bz2 cru-74bb9cd27242b9320f99ff4d2b50c3051576cc14.zip |
...
Diffstat (limited to 'src/win/gui/TimerManager.h')
-rw-r--r-- | src/win/gui/TimerManager.h | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/win/gui/TimerManager.h b/src/win/gui/TimerManager.h new file mode 100644 index 00000000..c44bd5bd --- /dev/null +++ b/src/win/gui/TimerManager.h @@ -0,0 +1,61 @@ +#pragma once +#include "cru/common/Event.h" +#include "cru/win/WinPreConfig.h" + +#include "cru/common/Base.h" +#include "cru/win/gui/GodWindow.h" +#include "cru/win/gui/WindowNativeMessageEventArgs.h" + +#include <chrono> +#include <functional> +#include <optional> +#include <unordered_map> + +namespace cru::platform::gui::win { +enum class TimerType { Immediate, Timeout, Interval }; + +struct TimerInfo { + TimerInfo(long long id, TimerType type, int period, + std::function<void()> action, UINT_PTR native_timer_id = 0) + : id(id), + type(type), + period(period), + action(std::move(action)), + native_timer_id(native_timer_id) {} + + long long id; + TimerType type; + int period; // in milliseconds + std::function<void()> action; + UINT_PTR native_timer_id; +}; + +class TimerManager : public Object { + public: + TimerManager(GodWindow* god_window); + + CRU_DELETE_COPY(TimerManager) + CRU_DELETE_MOVE(TimerManager) + + ~TimerManager() override = default; + + // Period is in milliseconds. When type is immediate, it is not checked and + // used. + long long SetTimer(TimerType type, int period, std::function<void()> action); + void CancelTimer(long long id); + + private: + void HandleGodWindowMessage(WindowNativeMessageEventArgs& args); + + void CreateNativeTimer(TimerInfo* info); + void KillNativeTimer(TimerInfo* info); + + private: + GodWindow* god_window_; + + EventRevokerListGuard event_guard_; + + long long next_id_ = 1; + std::unordered_map<long long, TimerInfo> info_map_; +}; +} // namespace cru::platform::gui::win |