aboutsummaryrefslogtreecommitdiff
path: root/include/cru
diff options
context:
space:
mode:
Diffstat (limited to 'include/cru')
-rw-r--r--include/cru/platform/native/UiApplication.hpp55
-rw-r--r--include/cru/ui/ShortcutHub.hpp20
2 files changed, 71 insertions, 4 deletions
diff --git a/include/cru/platform/native/UiApplication.hpp b/include/cru/platform/native/UiApplication.hpp
index 135e95c3..4c1b3456 100644
--- a/include/cru/platform/native/UiApplication.hpp
+++ b/include/cru/platform/native/UiApplication.hpp
@@ -1,6 +1,7 @@
#pragma once
#include "../Resource.hpp"
#include "Base.hpp"
+#include "cru/common/Base.hpp"
#include <chrono>
#include <functional>
@@ -53,6 +54,60 @@ struct IUiApplication : public virtual INativeResource {
virtual IInputMethodManager* GetInputMethodManager() = 0;
};
+class TimerAutoCanceler {
+ public:
+ TimerAutoCanceler() : id_(0) {}
+ explicit TimerAutoCanceler(long long id) : id_(id) {}
+
+ CRU_DELETE_COPY(TimerAutoCanceler)
+
+ TimerAutoCanceler(TimerAutoCanceler&& other) : id_(other.id_) {
+ other.id_ = 0;
+ }
+
+ TimerAutoCanceler& operator=(TimerAutoCanceler&& other) {
+ Reset(other.id_);
+ other.id_ = 0;
+ return *this;
+ }
+
+ ~TimerAutoCanceler() { Reset(); }
+
+ long long Release() {
+ auto temp = id_;
+ id_ = 0;
+ return temp;
+ }
+
+ void Reset(long long id = 0) {
+ if (id_ > 0) IUiApplication::GetInstance()->CancelTimer(id_);
+ id_ = id;
+ }
+
+ private:
+ long long id_;
+};
+
+class TimerListAutoCanceler {
+ public:
+ TimerListAutoCanceler() = default;
+ CRU_DELETE_COPY(TimerListAutoCanceler)
+ CRU_DEFAULT_MOVE(TimerListAutoCanceler)
+ ~TimerListAutoCanceler() = default;
+
+ TimerListAutoCanceler& operator+=(long long id) {
+ list_.push_back(TimerAutoCanceler(id));
+ return *this;
+ }
+
+ void Clear() { list_.clear(); }
+
+ bool IsEmpty() const { return list_.empty(); }
+
+ private:
+ std::vector<TimerAutoCanceler> list_;
+};
+
// Bootstrap from this.
std::unique_ptr<IUiApplication> CreateUiApplication();
} // namespace cru::platform::native
diff --git a/include/cru/ui/ShortcutHub.hpp b/include/cru/ui/ShortcutHub.hpp
index a1dfcb7d..5382f63e 100644
--- a/include/cru/ui/ShortcutHub.hpp
+++ b/include/cru/ui/ShortcutHub.hpp
@@ -12,6 +12,7 @@
#include <optional>
#include <string>
#include <string_view>
+#include <type_traits>
#include <unordered_map>
#include <vector>
@@ -71,6 +72,15 @@ struct hash<cru::ui::ShortcutKeyBind> {
} // namespace std
namespace cru::ui {
+struct Shortcut {
+ // Just for debug.
+ std::u16string name;
+ ShortcutKeyBind key_bind;
+ // Return true if it consumes the shortcut. Or return false if it does not
+ // handle the shortcut.
+ std::function<bool()> handler;
+};
+
struct ShortcutInfo {
int id;
std::u16string name;
@@ -87,11 +97,13 @@ class ShortcutHub : public Object {
~ShortcutHub() override = default;
- // Handler return true if it consumes the shortcut. Or return false if it does
- // not handle the shortcut. Name is just for debug. Return an id used for
- // unregistering.
int RegisterShortcut(std::u16string name, ShortcutKeyBind bind,
- std::function<bool()> handler);
+ std::function<bool()> handler) {
+ return RegisterShortcut({std::move(name), bind, std::move(handler)});
+ }
+
+ // Return an id used for unregistering.
+ int RegisterShortcut(Shortcut shortcut);
void UnregisterShortcut(int id);