aboutsummaryrefslogtreecommitdiff
path: root/src/platform/gui/win/TimerManager.cpp
diff options
context:
space:
mode:
authorYuqian Yang <crupest@crupest.life>2025-11-16 14:09:50 +0800
committerYuqian Yang <crupest@crupest.life>2025-11-16 14:09:50 +0800
commit07d662aafc25b145eb20e4123ebc82b5736cdeb7 (patch)
tree0b94baddd3235623beac4997ac09a503a00645e7 /src/platform/gui/win/TimerManager.cpp
parentfaf73c4af74bdae1abf394a33b573149b98ec2b1 (diff)
downloadcru-07d662aafc25b145eb20e4123ebc82b5736cdeb7.tar.gz
cru-07d662aafc25b145eb20e4123ebc82b5736cdeb7.tar.bz2
cru-07d662aafc25b145eb20e4123ebc82b5736cdeb7.zip
New timer impl on Windows. Add Event test. Fix delete later.
Diffstat (limited to 'src/platform/gui/win/TimerManager.cpp')
-rw-r--r--src/platform/gui/win/TimerManager.cpp95
1 files changed, 0 insertions, 95 deletions
diff --git a/src/platform/gui/win/TimerManager.cpp b/src/platform/gui/win/TimerManager.cpp
deleted file mode 100644
index 794e714c..00000000
--- a/src/platform/gui/win/TimerManager.cpp
+++ /dev/null
@@ -1,95 +0,0 @@
-#include "TimerManager.h"
-
-#include <functional>
-
-namespace cru::platform::gui::win {
-constexpr int kSetImmediateWindowMessageId = WM_USER + 2000;
-
-TimerManager::TimerManager(GodWindow* god_window) {
- god_window_ = god_window;
- event_guard_ += god_window->MessageEvent()->AddHandler(std::bind(
- &TimerManager::HandleGodWindowMessage, this, std::placeholders::_1));
-}
-
-long long TimerManager::SetTimer(TimerType type, int period,
- std::function<void()> action) {
- auto id = next_id_++;
- TimerInfo timer_info{id, type, type == TimerType::Immediate ? 0 : period,
- std::move(action)};
- if (type == TimerType::Immediate) {
- if (!::PostMessageW(god_window_->GetHandle(), kSetImmediateWindowMessageId,
- static_cast<UINT_PTR>(id), 0)) {
- throw Win32Error(
- ::GetLastError(),
- "Failed to post window message to god window for set immediate.");
- }
- } else {
- CreateNativeTimer(&timer_info);
- }
-
- info_map_.emplace(id, std::move(timer_info));
- return id;
-}
-
-void TimerManager::CancelTimer(long long id) {
- if (id <= 0) return;
- auto find_result = this->info_map_.find(id);
- if (find_result != info_map_.cend()) {
- auto& info = find_result->second;
- KillNativeTimer(&info);
- this->info_map_.erase(find_result);
- }
-}
-
-void TimerManager::CreateNativeTimer(TimerInfo* info) {
- info->native_timer_id = static_cast<UINT_PTR>(info->id);
- ::SetTimer(god_window_->GetHandle(), info->native_timer_id, info->period,
- nullptr);
-}
-
-void TimerManager::KillNativeTimer(TimerInfo* info) {
- if (info->native_timer_id == 0) return;
- ::KillTimer(god_window_->GetHandle(), info->native_timer_id);
- info->native_timer_id = 0;
-}
-
-void TimerManager::HandleGodWindowMessage(WindowNativeMessageEventArgs& args) {
- const auto& message = args.GetWindowMessage();
-
- switch (message.msg) {
- case kSetImmediateWindowMessageId: {
- auto find_result =
- this->info_map_.find(static_cast<long long>(message.w_param));
- if (find_result != info_map_.cend()) {
- auto& info = find_result->second;
- info.action();
- info_map_.erase(find_result);
- }
- args.SetResult(0);
- args.SetHandled(true);
- return;
- }
- case WM_TIMER: {
- auto find_result =
- this->info_map_.find(static_cast<long long>(message.w_param));
- if (find_result != info_map_.cend()) {
- auto& info = find_result->second;
- if (info.type == TimerType::Interval) {
- info.action();
- args.SetResult(0);
- args.SetHandled(true);
- } else if (info.type == TimerType::Timeout) {
- info.action();
- KillNativeTimer(&info);
- info_map_.erase(find_result);
- args.SetResult(0);
- args.SetHandled(true);
- }
- }
- return;
- }
- default:
- return;
- }
-}
-} // namespace cru::platform::gui::win