global_shortcuts_generic.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #include "base/global_shortcuts.h"
  9. namespace base {
  10. using GlobalShortcutKeyGeneric = uint64;
  11. class GlobalShortcutValueGeneric final : public GlobalShortcutValue {
  12. public:
  13. GlobalShortcutValueGeneric(
  14. std::vector<GlobalShortcutKeyGeneric> descriptors);
  15. QString toDisplayString() override;
  16. QByteArray serialize() override;
  17. const std::vector<GlobalShortcutKeyGeneric> &descriptors() const {
  18. return _descriptors;
  19. }
  20. private:
  21. std::vector<GlobalShortcutKeyGeneric> _descriptors;
  22. };
  23. class GlobalShortcutManagerGeneric final
  24. : public GlobalShortcutManager
  25. , public QObject {
  26. public:
  27. GlobalShortcutManagerGeneric();
  28. ~GlobalShortcutManagerGeneric();
  29. void startRecording(
  30. Fn<void(GlobalShortcut)> progress,
  31. Fn<void(GlobalShortcut)> done) override;
  32. void stopRecording() override;
  33. void startWatching(
  34. GlobalShortcut shortcut,
  35. Fn<void(bool pressed)> callback) override;
  36. void stopWatching(GlobalShortcut shortcut) override;
  37. GlobalShortcut shortcutFromSerialized(QByteArray serialized) override;
  38. // Thread-safe.
  39. void schedule(GlobalShortcutKeyGeneric descriptor, bool down);
  40. private:
  41. struct Watch {
  42. GlobalShortcut shortcut;
  43. std::vector<GlobalShortcutKeyGeneric> sorted;
  44. Fn<void(bool pressed)> callback;
  45. };
  46. void process(GlobalShortcutKeyGeneric descriptor, bool down);
  47. void processRecording(GlobalShortcutKeyGeneric descriptor, bool down);
  48. void processRecordingPress(GlobalShortcutKeyGeneric descriptor);
  49. void processRecordingRelease(GlobalShortcutKeyGeneric descriptor);
  50. void finishRecording();
  51. Fn<void(GlobalShortcut)> _recordingProgress;
  52. Fn<void(GlobalShortcut)> _recordingDone;
  53. std::vector<GlobalShortcutKeyGeneric> _recordingDown;
  54. flat_set<GlobalShortcutKeyGeneric> _recordingUp;
  55. flat_set<GlobalShortcutKeyGeneric> _down;
  56. std::vector<Watch> _watchlist;
  57. std::vector<GlobalShortcut> _pressed;
  58. bool _recording = false;
  59. };
  60. } // namespace base