1 // This file is part of Desktop App Toolkit,
2 // a set of libraries for developing nice desktop applications.
3 //
4 // For license and copyright information please follow this link:
5 // https://github.com/desktop-app/legal/blob/master/LEGAL
6 //
7 #pragma once
8 
9 #include "base/global_shortcuts.h"
10 
11 namespace base {
12 
13 using GlobalShortcutKeyGeneric = uint64;
14 
15 class GlobalShortcutValueGeneric final : public GlobalShortcutValue {
16 public:
17 	GlobalShortcutValueGeneric(
18 		std::vector<GlobalShortcutKeyGeneric> descriptors);
19 
20 	QString toDisplayString() override;
21 	QByteArray serialize() override;
22 
descriptors()23 	const std::vector<GlobalShortcutKeyGeneric> &descriptors() const {
24 		return _descriptors;
25 	}
26 
27 private:
28 	std::vector<GlobalShortcutKeyGeneric> _descriptors;
29 
30 };
31 
32 class GlobalShortcutManagerGeneric final
33 	: public GlobalShortcutManager
34 	, public QObject {
35 public:
36 	GlobalShortcutManagerGeneric();
37 	~GlobalShortcutManagerGeneric();
38 
39 	void startRecording(
40 		Fn<void(GlobalShortcut)> progress,
41 		Fn<void(GlobalShortcut)> done) override;
42 	void stopRecording() override;
43 	void startWatching(
44 		GlobalShortcut shortcut,
45 		Fn<void(bool pressed)> callback) override;
46 	void stopWatching(GlobalShortcut shortcut) override;
47 
48 	GlobalShortcut shortcutFromSerialized(QByteArray serialized) override;
49 
50 	// Thread-safe.
51 	void schedule(GlobalShortcutKeyGeneric descriptor, bool down);
52 
53 private:
54 	struct Watch {
55 		GlobalShortcut shortcut;
56 		std::vector<GlobalShortcutKeyGeneric> sorted;
57 		Fn<void(bool pressed)> callback;
58 	};
59 	void process(GlobalShortcutKeyGeneric descriptor, bool down);
60 	void processRecording(GlobalShortcutKeyGeneric descriptor, bool down);
61 	void processRecordingPress(GlobalShortcutKeyGeneric descriptor);
62 	void processRecordingRelease(GlobalShortcutKeyGeneric descriptor);
63 	void finishRecording();
64 
65 	Fn<void(GlobalShortcut)> _recordingProgress;
66 	Fn<void(GlobalShortcut)> _recordingDone;
67 	std::vector<GlobalShortcutKeyGeneric> _recordingDown;
68 	flat_set<GlobalShortcutKeyGeneric> _recordingUp;
69 	flat_set<GlobalShortcutKeyGeneric> _down;
70 	std::vector<Watch> _watchlist;
71 	std::vector<GlobalShortcut> _pressed;
72 
73 	bool _recording = false;
74 
75 };
76 
77 } // namespace base
78