1// Aseprite
2// Copyright (C) 2020-2022 Igara Studio S.A.
3// Copyright (C) 2001-2018 David Capello
4//
5// This program is distributed under the terms of
6// the End-User License Agreement for Aseprite.
7
8#ifndef APP_UI_KEYBOARD_SHORTCUTS_H_INCLUDED
9#define APP_UI_KEYBOARD_SHORTCUTS_H_INCLUDED
10#pragma once
11
12#include "app/ui/key.h"
13#include "obs/signal.h"
14
15class TiXmlElement;
16
17namespace app {
18
19 class KeyboardShortcuts {
20 public:
21 typedef Keys::iterator iterator;
22 typedef Keys::const_iterator const_iterator;
23
24 static KeyboardShortcuts* instance();
25 static void destroyInstance();
26
27 KeyboardShortcuts();
28 KeyboardShortcuts(const KeyboardShortcuts&) = delete;
29 KeyboardShortcuts& operator=(const KeyboardShortcuts&) = delete;
30 ~KeyboardShortcuts();
31
32 iterator begin() { return m_keys.begin(); }
33 iterator end() { return m_keys.end(); }
34 const_iterator begin() const { return m_keys.begin(); }
35 const_iterator end() const { return m_keys.end(); }
36
37 // const Keys& keys() const { return m_keys; }
38 void setKeys(const KeyboardShortcuts& keys,
39 const bool cloneKeys);
40
41 void clear();
42 void importFile(TiXmlElement* rootElement, KeySource source);
43 void importFile(const std::string& filename, KeySource source);
44 void exportFile(const std::string& filename);
45 void reset();
46
47 KeyPtr command(const char* commandName,
48 const Params& params = Params(),
49 const KeyContext keyContext = KeyContext::Any);
50 KeyPtr tool(tools::Tool* tool);
51 KeyPtr quicktool(tools::Tool* tool);
52 KeyPtr action(const KeyAction action,
53 const KeyContext keyContext = KeyContext::Any);
54 KeyPtr wheelAction(const WheelAction action);
55 KeyPtr dragAction(const WheelAction action);
56
57 void disableAccel(const ui::Accelerator& accel,
58 const KeySource source,
59 const KeyContext keyContext,
60 const Key* newKey);
61
62 KeyContext getCurrentKeyContext();
63 bool getCommandFromKeyMessage(const ui::Message* msg, Command** command, Params* params);
64 tools::Tool* getCurrentQuicktool(tools::Tool* currentTool);
65 KeyAction getCurrentActionModifiers(KeyContext context);
66 WheelAction getWheelActionFromMouseMessage(const KeyContext context,
67 const ui::Message* msg);
68 Keys getDragActionsFromKeyMessage(const KeyContext context,
69 const ui::Message* msg);
70 bool hasMouseWheelCustomization() const;
71 void clearMouseWheelKeys();
72 void addMissingMouseWheelKeys();
73 void setDefaultMouseWheelKeys(const bool zoomWithWheel);
74
75 void addMissingKeysForCommands();
76
77 // Generated when the keyboard shortcuts are modified by the user.
78 // Useful to regenerate tooltips with shortcuts.
79 obs::signal<void()> UserChange;
80
81 private:
82 void exportKeys(TiXmlElement& parent, KeyType type);
83 void exportAccel(TiXmlElement& parent, const Key* key, const ui::Accelerator& accel, bool removed);
84
85 Keys m_keys;
86 };
87
88 std::string key_tooltip(const char* str, const Key* key);
89
90 inline std::string key_tooltip(const char* str,
91 const char* commandName,
92 const Params& params = Params(),
93 KeyContext keyContext = KeyContext::Any) {
94 return key_tooltip(
95 str, KeyboardShortcuts::instance()->command(
96 commandName, params, keyContext).get());
97 }
98
99 inline std::string key_tooltip(const char* str,
100 KeyAction keyAction,
101 KeyContext keyContext = KeyContext::Any) {
102 return key_tooltip(
103 str, KeyboardShortcuts::instance()->action(keyAction, keyContext).get());
104 }
105
106} // namespace app
107
108#endif
109