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 [[deprecated( "use setShortcut( Shortcut, ShortcutAction )" )]]
55 void setShortcut( const ShortcutKey& key, const ShortcutCommand& command )
56 { setShortcut( { key, command.category }, { command.name, command.action, command.repeatable } ); }
57
58 using ShortcutList = std::vector<std::tuple<ShortcutKey, Category, std::string>>;
59
62 MRVIEWER_API const ShortcutList& getShortcutList() const;
63
64 enum class Reason
65 {
68 };
69
71 bool isEnabled() const { return enabled_; }
72 void enable( bool on ) { enabled_ = on; }
73
75 MRVIEWER_API virtual bool processShortcut( const ShortcutKey& key, Reason = Reason::KeyDown ) const;
76
77 MRVIEWER_API bool onKeyDown_( int key, int modifier ) override;
78 MRVIEWER_API bool onKeyRepeat_( int key, int modifier ) override;
79
81 MRVIEWER_API static const char* getModifierString( int mod );
83 MRVIEWER_API static std::string getKeyString( int key );
85 MRVIEWER_API static std::string getKeyFullString( const ShortcutKey& key, bool respectKey = true );
86
91 MRVIEWER_API static std::optional<int> parseKey( std::string_view name );
92
96 MRVIEWER_API static std::optional<int> parseModifier( std::string_view name );
97
100 MRVIEWER_API static std::optional<Category> parseCategory( std::string_view name );
101
104 MRVIEWER_API static std::optional<ShortcutKey> parseShortcutKey( std::string_view keys );
105
107 MRVIEWER_API std::optional<ShortcutKey> findShortcutByName( const std::string& name ) const;
108
110 MRVIEWER_API void clear();
111protected:
114 MRVIEWER_API static int mapKeyFromKeyAndMod( const ShortcutKey& key, bool respectKeyboard );
116 static ShortcutKey kayAndModFromMapKey( int mapKey ) { return { mapKey >> 6, mapKey % ( 1 << 6 ) }; }
117
120
121 bool enabled_{ true };
122
125
126 mutable std::optional<ShortcutList> listCache_;
127};
128
129}
Definition MRShortcutManager.h:22
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:116
HashMap< std::string, int > ShourtcutsBackMap
Definition MRShortcutManager.h:119
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 it
Reason
Definition MRShortcutManager.h:65
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
void setShortcut(const ShortcutKey &key, const ShortcutCommand &command)
deprecated: pass the category in (shortcut) and the rest in ShortcutAction
Definition MRShortcutManager.h:55
void enable(bool on)
Definition MRShortcutManager.h:72
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;
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)
std::vector< std::tuple< ShortcutKey, Category, std::string > > ShortcutList
Definition MRShortcutManager.h:58
static std::string getKeyFullString(const ShortcutKey &key, bool respectKey=true)
make string from all modifiers and with/without key and returns it
bool onKeyDown_(int key, int modifier) override
MR::ShortcutKey ShortcutKey
Definition MRShortcutManager.h:26
static std::optional< Category > parseCategory(std::string_view name)
HashMap< int, ShortcutCommand > ShourtcutsMap
Definition MRShortcutManager.h:118
bool onKeyRepeat_(int key, int modifier) override
ShourtcutsBackMap backMap_
Definition MRShortcutManager.h:124
ShourtcutsMap map_
Definition MRShortcutManager.h:123
bool isEnabled() const
processShortcut does nothing if not enabled
Definition MRShortcutManager.h:71
ShortcutCategory
Definition MRShortcutKey.h:20
virtual ~ShortcutManager()=default
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:126
bool enabled_
Definition MRShortcutManager.h:121
phmap::flat_hash_map< K, V, Hash, Eq > HashMap
Definition MRMeshFwd.h:606
@ KeyRepeat
the user just pressed the keys
Definition MRShortcutManager.h:67
@ KeyDown
Definition MRShortcutManager.h:66
only for bindings generation
Definition MRCameraOrientationPlugin.h:8
Definition MRViewerEventsListener.h:32
Definition MRShortcutKey.h:12
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:32