MeshLib C++ Docs
Loading...
Searching...
No Matches
MRShortcutManager.h
Go to the documentation of this file.
1#pragma once
2#include "MRShortcutKey.h"
3#include "MRMesh/MRphmap.h"
5#include <string>
6#include <string_view>
7#include <functional>
8#include <optional>
9
10namespace MR
11{
14
15
21class MRVIEWER_CLASS ShortcutManager : public MultiListener<KeyDownListener, KeyRepeatListener>
22{
23public:
24 virtual ~ShortcutManager() = default;
25
28
31 {
32 std::string name;
33 std::function<void()> func;
34 bool repeatable = true;
35 };
36
39 {
41 std::string name;
42 std::function<void()> action;
43 bool repeatable = true;
44 };
45
46 inline static const std::string categoryNames[6] = { "Info", "Edit", "View", "Scene", "Objects", "Selection " };
47
51 MRVIEWER_API virtual void setShortcut( const Shortcut& shortcut, const ShortcutAction& action );
52
54 MRVIEWER_API void resetShortcut( const std::string& name );
55
57 [[deprecated( "use setShortcut( Shortcut, ShortcutAction )" )]]
58 void setShortcut( const ShortcutKey& key, const ShortcutCommand& command )
59 { setShortcut( { key, command.category }, { command.name, command.action, command.repeatable } ); }
60
62 using ShortcutList = std::vector<std::pair<Shortcut, std::string>>;
63
66 MRVIEWER_API const ShortcutList& getShortcutList() const;
67
68 enum class Reason
69 {
72 };
73
75 bool isEnabled() const { return enabled_; }
76 void enable( bool on ) { enabled_ = on; }
77
79 MRVIEWER_API virtual bool processShortcut( const ShortcutKey& key, Reason = Reason::KeyDown ) const;
80
81 MRVIEWER_API bool onKeyDown_( int key, int modifier ) override;
82 MRVIEWER_API bool onKeyRepeat_( int key, int modifier ) override;
83
85 MRVIEWER_API static const char* getModifierString( int mod );
87 MRVIEWER_API static std::string getKeyString( int key );
89 MRVIEWER_API static std::string getKeyFullString( const ShortcutKey& key, bool respectKey = true );
91 MRVIEWER_API static std::string getKeysFullString( const std::vector<ShortcutKey>& keys );
92
97 MRVIEWER_API static std::optional<int> parseKey( std::string_view name );
98
102 MRVIEWER_API static std::optional<int> parseModifier( std::string_view name );
103
106 MRVIEWER_API static std::optional<Category> parseCategory( std::string_view name );
107
110 MRVIEWER_API static std::optional<ShortcutKey> parseShortcutKey( std::string_view keys );
111
113 MRVIEWER_API std::optional<ShortcutKey> findShortcutByName( const std::string& name ) const;
114
116 MRVIEWER_API std::vector<ShortcutKey> findShortcutsByName( const std::string& name ) const;
117
119 MRVIEWER_API void clear();
120protected:
123 MRVIEWER_API static int mapKeyFromKeyAndMod( const ShortcutKey& key, bool respectKeyboard );
125 static ShortcutKey kayAndModFromMapKey( int mapKey )
126 { return { ( mapKey >> cModBits ) % ( 1 << cKeyBits ), mapKey % ( 1 << cModBits ), mapKey >> ( cModBits + cKeyBits ) }; }
127
128 static constexpr int cModBits = 6;
129 static constexpr int cKeyBits = 9;
130
132 int findHeldKey_( int key, int mod ) const;
133
136
137 bool enabled_{ true };
138
141
142 mutable std::optional<ShortcutList> listCache_;
143};
144
145}
Definition MRShortcutManager.h:22
static constexpr int cKeyBits
Definition MRShortcutManager.h:129
std::function< void()> func
name of action
Definition MRShortcutManager.h:33
static ShortcutKey kayAndModFromMapKey(int mapKey)
returns key with modifier (alt, ctrl, shift, etc.) from simple map key
Definition MRShortcutManager.h:125
virtual void setShortcut(const Shortcut &shortcut, const ShortcutAction &action)
static const char * getModifierString(int mod)
make string from strictly one modifier
std::optional< ShortcutKey > findShortcutByName(const std::string &name) const
if action with given name is present in shortcut list - returns its first key
Reason
Definition MRShortcutManager.h:69
const ShortcutList & getShortcutList() const
void clear()
clear all saved shortcuts
bool repeatable
Definition MRShortcutManager.h:34
static std::string getKeyString(int key)
make string from a key without modifiers, for arrow characters it uses icons font
static const std::string categoryNames[6]
Definition MRShortcutManager.h:46
int findHeldKey_(int key, int mod) const
the held key, down now and not reserved by a UI hotkey (reserveKeyEvent), of a chord with given key a...
static constexpr int cModBits
bits of the modifiers and of the key in a map key, the held key takes the rest
Definition MRShortcutManager.h:128
void setShortcut(const ShortcutKey &key, const ShortcutCommand &command)
deprecated: pass the category in (shortcut) and the rest in ShortcutAction
Definition MRShortcutManager.h:58
void enable(bool on)
Definition MRShortcutManager.h:76
virtual bool processShortcut(const ShortcutKey &key, Reason=Reason::KeyDown) const
if given key has action in shortcut map - process it and returns true, otherwise returns false;
static std::string getKeysFullString(const std::vector< ShortcutKey > &keys)
makes string from several keys separated by commas, e.g. "Ctrl+Shift+Z, Ctrl+Y"
std::function< void()> action
name of action
Definition MRShortcutManager.h:42
std::string name
Definition MRShortcutManager.h:41
static std::optional< int > parseKey(std::string_view name)
std::string name
Definition MRShortcutManager.h:32
static int mapKeyFromKeyAndMod(const ShortcutKey &key, bool respectKeyboard)
static std::string getKeyFullString(const ShortcutKey &key, bool respectKey=true)
make string from all modifiers, the held key and with/without key and returns it, e....
bool onKeyDown_(int key, int modifier) override
MR::ShortcutKey ShortcutKey
Definition MRShortcutManager.h:26
std::vector< std::pair< Shortcut, std::string > > ShortcutList
the shortcut of every action with all its keys, and the name of the action
Definition MRShortcutManager.h:62
phmap::flat_hash_map< K, V, Hash, Eq, Allocator< std::pair< const K, V > > > HashMap
Definition MRMeshFwd.h:632
static std::optional< Category > parseCategory(std::string_view name)
HashMap< int, ShortcutCommand > ShourtcutsMap
Definition MRShortcutManager.h:134
bool onKeyRepeat_(int key, int modifier) override
ShourtcutsBackMap backMap_
Definition MRShortcutManager.h:140
ShourtcutsMap map_
Definition MRShortcutManager.h:139
bool isEnabled() const
processShortcut does nothing if not enabled
Definition MRShortcutManager.h:75
ShortcutCategory
Definition MRShortcutKey.h:25
virtual ~ShortcutManager()=default
void resetShortcut(const std::string &name)
removes the action with given name and all its keys
static std::optional< ShortcutKey > parseShortcutKey(std::string_view keys)
Category category
Definition MRShortcutManager.h:40
MR::ShortcutCategory Category
Definition MRShortcutManager.h:27
bool repeatable
Definition MRShortcutManager.h:43
static std::optional< int > parseModifier(std::string_view name)
std::optional< ShortcutList > listCache_
Definition MRShortcutManager.h:142
bool enabled_
all keys of an action in their order
Definition MRShortcutManager.h:137
std::vector< ShortcutKey > findShortcutsByName(const std::string &name) const
returns all keys of the action with given name in their order, empty if the action is not present in ...
HashMap< std::string, std::vector< int > > ShourtcutsBackMap
Definition MRShortcutManager.h:135
@ KeyRepeat
the user just pressed the keys
Definition MRShortcutManager.h:71
@ KeyDown
Definition MRShortcutManager.h:70
Only strictly necessary for the bindings (to make MR::MeshIntersectionResult a complete type).
Definition MRCameraOrientationPlugin.h:8
Definition MRViewerEventsListener.h:32
Definition MRShortcutKey.h:14
what a shortcut does
Definition MRShortcutManager.h:31
the parameter of the deprecated setShortcut overload, also the internal storage of an action
Definition MRShortcutManager.h:39
a keyboard shortcut: the keys to press and the category it is listed under
Definition MRShortcutKey.h:37