-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathActionsShortcutsManager.h
More file actions
135 lines (103 loc) · 6.07 KB
/
ActionsShortcutsManager.h
File metadata and controls
135 lines (103 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright (C) 2014-2026 Michael Kazakov. Subject to GNU General Public License version 3.
#pragma once
// ⇧ - NSShiftKeyMask
// % - fn <- NO
// ^ - NSControlKeyMask
// ⌥ - NSAlternateKeyMask
// ⌘ - NSCommandKeyMask
#include <Base/UnorderedUtil.h>
#include <Utility/ActionShortcut.h>
#include <Utility/ActionsShortcutsManager.h>
#include <absl/container/inlined_vector.h>
#include <vector>
#include <span>
#include <string_view>
#include <optional>
namespace nc::config {
class Config;
}
namespace nc::core {
// Implementation of the abstract interface ActionsShortcutsManager
class ActionsShortcutsManager : public nc::utility::ActionsShortcutsManager
{
public:
// Creates a new shortcut manager with the list of Action<->Tag mappings, Actions->DefaultShortcut mappings and the
// config to store the overides.
ActionsShortcutsManager(std::span<const std::pair<const char *, int>> _action_tags,
std::span<const std::pair<const char *, const char *>> _default_shortcuts,
nc::config::Config &_config);
ActionsShortcutsManager(const ActionsShortcutsManager &) = delete;
// Destructor.
~ActionsShortcutsManager() override;
// Returns a numeric tag that corresponds to the given action name.
[[nodiscard]] std::optional<int> TagFromAction(std::string_view _action) const noexcept override;
// Returns an action name of the given numeric tag.
[[nodiscard]] std::optional<std::string_view> ActionFromTag(int _tag) const noexcept override;
// Returns a shortcut assigned to the specified action.
// Returns std::nullopt such action cannot be found.
// Overrides have priority over the default shortcuts.
[[nodiscard]] std::optional<Shortcuts> ShortcutsFromAction(std::string_view _action) const noexcept override;
// Returns a shortcut assigned to the specified numeric action tag.
// Returns std::nullopt such action cannot be found.
// Overrides have priority over the default shortcuts.
[[nodiscard]] std::optional<Shortcuts> ShortcutsFromTag(int _tag) const noexcept override;
// Returns a default shortcut for an action specified by its numeric tag.
// Returns std::nullopt such action cannot be found.
[[nodiscard]] std::optional<Shortcuts> DefaultShortcutsFromTag(int _tag) const noexcept override;
// Returns an unordered list of numeric tags of actions that have the specified shortcut.
// An optional domain parameter can be specified to filter the output by only leaving actions that have the
// specified domain in their name.
[[nodiscard]] std::optional<ActionTags>
ActionTagsFromShortcut(Shortcut _sc, std::string_view _in_domain = {}) const noexcept override;
// Syntax sugar around ActionTagsFromShortCut(_sc, _in_domain) and find_first_of(_of_tags).
// Returns the first tag from the specified set.
// The order is not defined in case of ambiguities.
[[nodiscard]] std::optional<int>
FirstOfActionTagsFromShortcut(std::span<const int> _of_tags,
Shortcut _sc,
std::string_view _in_domain = {}) const noexcept override;
// Returns the list of actions alongside with their tags, preserving the order.
[[nodiscard]] std::vector<std::pair<std::string, int>> AllShortcuts() const override;
// Removes any hotkeys overrides.
void RevertToDefaults() override;
// Sets the custom shortkey for the specified action.
// Returns true if any change was done to the actions maps.
// If the _action doesn't exist or already has the same value, returns false.
// This function is effectively a syntax sugar for SetShortCutsOverride(_action, {&_sc, 1}).
bool SetShortcutOverride(std::string_view _action, Shortcut _sc) override;
// Sets the custom shortkeys for the specified action.
// Returns true if any change was done to the actions maps.
// If the _action doesn't exist or already has the same value, returns false.
bool SetShortcutsOverride(std::string_view _action, std::span<const Shortcut> _shortcuts) override;
private:
// An unordered list of numeric tags indicating which actions are using a shortcut.
// An inlined vector is used to avoid memory allocating for such tiny memory blocks.
using TagsUsingShortcut = absl::InlinedVector<int, 4>;
void ReadOverrideFromConfig();
void WriteOverridesToConfig() const;
// Clears the shortcuts usage map and builds it from the defaults and the overrides
void BuildShortcutUsageMap() noexcept;
// Adds the specified action tag to a list of actions that use the specified shortcut.
// The shortcut should not be empty.
void RegisterShortcutUsage(Shortcut _shortcut, int _tag) noexcept;
// Removes the specified actions tag from the list of action tags that use the specified shortcut.
void UnregisterShortcutUsage(Shortcut _shortcut, int _tag) noexcept;
// Returns a container without empty shortcuts, while preserving the original relative order of the remaining items.
// Duplicates are removed as well.
static Shortcuts SanitizedShortcuts(const Shortcuts &_shortcuts) noexcept;
// Maps a numeric action tag to its name.
ankerl::unordered_dense::map<int, std::string> m_TagToAction;
// Maps an action name to its numeric tag.
ankerl::unordered_dense::map<std::string, int, UnorderedStringHashEqual, UnorderedStringHashEqual> m_ActionToTag;
// Maps an action tag to the default ordered list of its shortcuts.
ankerl::unordered_dense::map<int, Shortcuts> m_ShortcutsDefaults;
// Maps an action tag to the overriden ordered list of its shortcuts.
ankerl::unordered_dense::map<int, Shortcuts> m_ShortcutsOverrides;
// Maps a shortcut to an unordered list of action tags that use it.
ankerl::unordered_dense::map<Shortcut, TagsUsingShortcut> m_ShortcutsUsage;
// Config instance used to read from and write to the shortcut overrides.
nc::config::Config &m_Config;
// A backup copy of the original input, preserving the order.
std::vector<std::pair<std::string, int>> m_OriginalOrderedActions;
};
} // namespace nc::core