keybinding.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #pragma once
  2. #include <string>
  3. #include <vector>
  4. #include <map>
  5. #include <memory>
  6. #include <chrono>
  7. #include <optional>
  8. #include "lumacs/command_system.hpp" // Include the full definition of CommandResult
  9. // Forward declaration for CommandSystem
  10. namespace lumacs {
  11. class CommandSystem;
  12. }
  13. namespace lumacs {
  14. /// @brief Represents the base key (without modifiers).
  15. enum class BaseKey : uint16_t {
  16. Unknown = 0,
  17. A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z,
  18. D0, D1, D2, D3, D4, D5, D6, D7, D8, D9, // Digits
  19. F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, // Function keys
  20. Space, Return, Tab, Escape, Backspace, Delete,
  21. ArrowUp, ArrowDown, ArrowLeft, ArrowRight,
  22. Home, End, PageUp, PageDown, Insert,
  23. Semicolon, Equal, Comma, Minus, Period, Slash, Backtick,
  24. LeftBracket, Backslash, RightBracket, Quote,
  25. NumLock, ScrollLock, PrintScreen, Pause,
  26. // Add more as needed
  27. };
  28. /// @brief Represents key modifiers as a bitmask.
  29. enum class Modifier : uint16_t {
  30. None = 0,
  31. Control = 1 << 0,
  32. Meta = 1 << 1, // Alt key, Option key
  33. Shift = 1 << 2,
  34. };
  35. /// @brief Represents a single key in a key sequence.
  36. struct Key {
  37. BaseKey base_key = BaseKey::Unknown;
  38. Modifier modifiers = Modifier::None;
  39. Key() = default;
  40. explicit Key(BaseKey bk, Modifier mods = Modifier::None) : base_key(bk), modifiers(mods) {}
  41. explicit Key(const std::string& key_name); // To be implemented in .cpp
  42. /// @brief Parse a key string like "C-x" or "M-S-a" into a Key structure.
  43. static Key parse(const std::string& key_str); // To be implemented in .cpp
  44. /// @brief Convert Key back to canonical string representation.
  45. std::string to_string() const; // To be implemented in .cpp
  46. bool operator==(const Key& other) const;
  47. bool operator<(const Key& other) const;
  48. };
  49. /// @brief Represents a sequence of keys (e.g., "C-x C-f").
  50. class KeySequence {
  51. public:
  52. KeySequence() = default;
  53. explicit KeySequence(const std::vector<Key>& keys);
  54. explicit KeySequence(const std::string& key_sequence_str);
  55. /// @brief Add a key to the sequence.
  56. void add_key(const Key& key);
  57. void add_key(const std::string& key_str);
  58. /// @brief Get the keys in this sequence.
  59. const std::vector<Key>& keys() const { return keys_; }
  60. /// @brief Check if this sequence is empty.
  61. bool empty() const { return keys_.empty(); }
  62. /// @brief Get the length of the sequence.
  63. size_t length() const { return keys_.size(); }
  64. /// @brief Check if this sequence is a prefix of another sequence.
  65. bool is_prefix_of(const KeySequence& other) const;
  66. /// @brief Check if this sequence has the given sequence as a prefix.
  67. bool has_prefix(const KeySequence& prefix) const;
  68. /// @brief Get string representation like "C-x 2".
  69. std::string to_string() const;
  70. /// @brief Clear the sequence.
  71. void clear();
  72. /// @brief Get a subsequence starting from index.
  73. KeySequence subsequence(size_t start_index) const;
  74. bool operator==(const KeySequence& other) const;
  75. bool operator<(const KeySequence& other) const;
  76. private:
  77. std::vector<Key> keys_;
  78. };
  79. /// @brief Represents a key binding with its associated function.
  80. struct KeyBinding {
  81. KeySequence sequence; ///< The key sequence that triggers this binding.
  82. std::string command_name; ///< The name of the command to execute.
  83. std::string description; ///< Human-readable description.
  84. KeyBinding() = default;
  85. KeyBinding(const KeySequence& seq, std::string cmd_name, std::string desc = "");
  86. KeyBinding(const std::string& key_str, std::string cmd_name, const std::string& description = "");
  87. };
  88. /// @brief Represents the result of processing a key in the binding manager.
  89. enum class KeyResult {
  90. Unbound, ///< No binding found, key not handled.
  91. Executed, ///< Binding found and executed successfully.
  92. Failed, ///< Binding found but execution failed.
  93. Partial, ///< Key is part of a multi-key sequence, waiting for more keys.
  94. Timeout ///< Partial sequence timed out.
  95. };
  96. /// @brief Detailed result of a key processing operation.
  97. struct KeyProcessingResult {
  98. KeyResult type;
  99. std::optional<CommandResult> command_result; // Relevant if type is Executed or Failed
  100. };
  101. /// @brief Central manager for all key bindings.
  102. ///
  103. /// Handles:
  104. /// - Registration of global key bindings.
  105. /// - Processing of key input (single and multi-key sequences).
  106. /// - Prefix key logic (e.g., C-x waiting for next key).
  107. class KeyBindingManager {
  108. public:
  109. KeyBindingManager(CommandSystem* command_system);
  110. ~KeyBindingManager() = default;
  111. /// @brief Bind a key sequence to a function.
  112. void bind(const KeySequence& sequence, std::string cmd_name, const std::string& description = "");
  113. void bind(const std::string& key_str, std::string cmd_name, const std::string& description = "");
  114. /// @brief Unbind a key sequence.
  115. void unbind(const KeySequence& sequence);
  116. void unbind(const std::string& key_str);
  117. /// @brief Process a single key press.
  118. /// @return Result indicating if key was bound, partial, etc.
  119. KeyProcessingResult process_key(const Key& key);
  120. KeyProcessingResult process_key(const std::string& key_str);
  121. /// @brief Check if a sequence has any bindings (exact or partial).
  122. bool has_binding(const KeySequence& sequence) const;
  123. /// @brief Check if a sequence has an exact binding.
  124. bool has_exact_binding(const KeySequence& sequence) const;
  125. /// @brief Check if a sequence is a prefix for other bindings.
  126. bool has_prefix_bindings(const KeySequence& sequence) const;
  127. /// @brief Clear current partial sequence (e.g., on timeout or escape).
  128. void clear_partial_sequence();
  129. /// @brief Get current partial sequence for display purposes.
  130. const KeySequence& current_partial_sequence() const { return current_sequence_; }
  131. /// @brief Check if we're currently building a multi-key sequence.
  132. bool is_building_sequence() const;
  133. /// @brief Get string representation of current sequence for display.
  134. std::string current_sequence_display() const;
  135. /// @brief Get all registered bindings (for debugging/help).
  136. std::vector<KeyBinding> get_all_bindings() const;
  137. /// @brief Set timeout for multi-key sequences.
  138. void set_sequence_timeout(std::chrono::milliseconds timeout);
  139. /// @brief Get timeout for multi-key sequences.
  140. [[nodiscard]] std::chrono::milliseconds get_sequence_timeout() const { return sequence_timeout_; }
  141. /// @brief Check if current partial sequence has timed out.
  142. bool has_sequence_timed_out() const;
  143. private:
  144. // Trie node structure
  145. struct TrieNode {
  146. std::optional<KeyBinding> binding; // Stores binding if this node represents the end of a sequence
  147. std::map<Key, std::unique_ptr<TrieNode>> children;
  148. int ref_count = 0; // Number of bindings passing through this node
  149. TrieNode() = default;
  150. // Prevent copying, allow moving
  151. TrieNode(const TrieNode&) = delete;
  152. TrieNode& operator=(const TrieNode&) = delete;
  153. TrieNode(TrieNode&&) noexcept = default;
  154. TrieNode& operator=(TrieNode&&) noexcept = default;
  155. };
  156. std::unique_ptr<TrieNode> root_; // Root of the trie
  157. KeySequence current_sequence_;
  158. std::chrono::steady_clock::time_point sequence_start_time_;
  159. std::chrono::milliseconds sequence_timeout_;
  160. CommandSystem* command_system_ = nullptr; // Dependency on CommandSystem
  161. // Helper function to find a node in the trie
  162. TrieNode* find_node(const KeySequence& sequence) const;
  163. // Helper to traverse trie and collect all bindings
  164. void collect_bindings(TrieNode* node, KeySequence current_prefix, std::vector<KeyBinding>& result) const;
  165. };
  166. } // namespace lumacs