|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <cstdint> |
| 4 | +#include <string> |
| 5 | + |
| 6 | +namespace nativeapi { |
| 7 | + |
| 8 | +/** |
| 9 | + * @brief Enumeration of keyboard modifier keys. |
| 10 | + * |
| 11 | + * Defines the various modifier keys that can be combined with regular keys |
| 12 | + * to create keyboard shortcuts and accelerators. Modifiers can be combined |
| 13 | + * using bitwise OR operations. |
| 14 | + */ |
| 15 | +enum class ModifierKey : uint32_t { |
| 16 | + /** |
| 17 | + * No modifier keys pressed. |
| 18 | + */ |
| 19 | + None = 0, |
| 20 | + |
| 21 | + /** |
| 22 | + * Shift key modifier. |
| 23 | + */ |
| 24 | + Shift = 1 << 0, |
| 25 | + |
| 26 | + /** |
| 27 | + * Control key modifier (Ctrl on Windows/Linux). |
| 28 | + */ |
| 29 | + Ctrl = 1 << 1, |
| 30 | + |
| 31 | + /** |
| 32 | + * Alt key modifier (Option on macOS). |
| 33 | + */ |
| 34 | + Alt = 1 << 2, |
| 35 | + |
| 36 | + /** |
| 37 | + * Meta key modifier (Windows key on Windows, Command key on macOS, Super on Linux). |
| 38 | + */ |
| 39 | + Meta = 1 << 3, |
| 40 | + |
| 41 | + /** |
| 42 | + * Function key modifier (Fn key, typically on laptops). |
| 43 | + */ |
| 44 | + Fn = 1 << 4, |
| 45 | + |
| 46 | + /** |
| 47 | + * Caps Lock state indicator. |
| 48 | + */ |
| 49 | + CapsLock = 1 << 5, |
| 50 | + |
| 51 | + /** |
| 52 | + * Num Lock state indicator. |
| 53 | + */ |
| 54 | + NumLock = 1 << 6, |
| 55 | + |
| 56 | + /** |
| 57 | + * Scroll Lock state indicator. |
| 58 | + */ |
| 59 | + ScrollLock = 1 << 7 |
| 60 | +}; |
| 61 | + |
| 62 | +/** |
| 63 | + * @brief Bitwise OR operator for combining ModifierKey values. |
| 64 | + * |
| 65 | + * Allows combining multiple modifier keys using the | operator. |
| 66 | + * |
| 67 | + * @param a First modifier key |
| 68 | + * @param b Second modifier key |
| 69 | + * @return Combined modifier keys |
| 70 | + * |
| 71 | + * @example |
| 72 | + * ```cpp |
| 73 | + * auto modifiers = ModifierKey::Ctrl | ModifierKey::Shift; |
| 74 | + * ``` |
| 75 | + */ |
| 76 | +inline ModifierKey operator|(ModifierKey a, ModifierKey b) { |
| 77 | + return static_cast<ModifierKey>(static_cast<uint32_t>(a) | static_cast<uint32_t>(b)); |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * @brief Bitwise AND operator for checking ModifierKey values. |
| 82 | + * |
| 83 | + * Allows checking if specific modifier keys are present using the & operator. |
| 84 | + * |
| 85 | + * @param a First modifier key |
| 86 | + * @param b Second modifier key |
| 87 | + * @return Intersection of modifier keys |
| 88 | + * |
| 89 | + * @example |
| 90 | + * ```cpp |
| 91 | + * if ((modifiers & ModifierKey::Ctrl) != ModifierKey::None) { |
| 92 | + * // Ctrl is pressed |
| 93 | + * } |
| 94 | + * ``` |
| 95 | + */ |
| 96 | +inline ModifierKey operator&(ModifierKey a, ModifierKey b) { |
| 97 | + return static_cast<ModifierKey>(static_cast<uint32_t>(a) & static_cast<uint32_t>(b)); |
| 98 | +} |
| 99 | + |
| 100 | +/** |
| 101 | + * @brief Bitwise OR assignment operator for ModifierKey values. |
| 102 | + * |
| 103 | + * Allows accumulating modifier keys using the |= operator. |
| 104 | + * |
| 105 | + * @param a Modifier key to modify |
| 106 | + * @param b Modifier key to add |
| 107 | + * @return Reference to the modified modifier key |
| 108 | + * |
| 109 | + * @example |
| 110 | + * ```cpp |
| 111 | + * ModifierKey modifiers = ModifierKey::Ctrl; |
| 112 | + * modifiers |= ModifierKey::Shift; |
| 113 | + * ``` |
| 114 | + */ |
| 115 | +inline ModifierKey& operator|=(ModifierKey& a, ModifierKey b) { |
| 116 | + a = a | b; |
| 117 | + return a; |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * @brief Keyboard accelerator for menu items and shortcuts. |
| 122 | + * |
| 123 | + * Represents a keyboard shortcut that can trigger a menu item or action. |
| 124 | + * Supports modifier keys and regular keys in a platform-independent way. |
| 125 | + */ |
| 126 | +struct KeyboardAccelerator { |
| 127 | + /** |
| 128 | + * Combination of modifier flags. |
| 129 | + */ |
| 130 | + ModifierKey modifiers = ModifierKey::None; |
| 131 | + |
| 132 | + /** |
| 133 | + * The main key code (e.g., 'A', 'F1', etc.). |
| 134 | + */ |
| 135 | + std::string key; |
| 136 | + |
| 137 | + /** |
| 138 | + * Constructor for creating keyboard accelerators. |
| 139 | + * |
| 140 | + * @param key The main key (e.g., "A", "F1", "Enter") |
| 141 | + * @param modifiers Combination of modifier flags |
| 142 | + * |
| 143 | + * @example |
| 144 | + * ```cpp |
| 145 | + * // Ctrl+S |
| 146 | + * KeyboardAccelerator save_accel("S", ModifierKey::Ctrl); |
| 147 | + * |
| 148 | + * // Ctrl+Shift+N |
| 149 | + * KeyboardAccelerator new_accel("N", |
| 150 | + * ModifierKey::Ctrl | ModifierKey::Shift); |
| 151 | + * ``` |
| 152 | + */ |
| 153 | + KeyboardAccelerator(const std::string& key = "", ModifierKey modifiers = ModifierKey::None) |
| 154 | + : key(key), modifiers(modifiers) {} |
| 155 | + |
| 156 | + /** |
| 157 | + * Get a human-readable string representation of the accelerator. |
| 158 | + * |
| 159 | + * @return String representation like "Ctrl+S" or "Alt+F4" |
| 160 | + */ |
| 161 | + std::string ToString() const; |
| 162 | + |
| 163 | + /** |
| 164 | + * Check if this accelerator is empty (no key specified). |
| 165 | + * |
| 166 | + * @return true if no key is specified, false otherwise |
| 167 | + */ |
| 168 | + bool IsEmpty() const { return key.empty(); } |
| 169 | + |
| 170 | + /** |
| 171 | + * Equality comparison operator. |
| 172 | + * |
| 173 | + * @param other The other accelerator to compare with |
| 174 | + * @return true if both accelerators are equal, false otherwise |
| 175 | + */ |
| 176 | + bool operator==(const KeyboardAccelerator& other) const { |
| 177 | + return modifiers == other.modifiers && key == other.key; |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Inequality comparison operator. |
| 182 | + * |
| 183 | + * @param other The other accelerator to compare with |
| 184 | + * @return true if accelerators are different, false otherwise |
| 185 | + */ |
| 186 | + bool operator!=(const KeyboardAccelerator& other) const { return !(*this == other); } |
| 187 | +}; |
| 188 | + |
| 189 | +} // namespace nativeapi |
0 commit comments