KeyCallbackMachine.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <LibLine/Editor.h>
  8. namespace {
  9. constexpr u32 ctrl(char c) { return c & 0x3f; }
  10. }
  11. namespace Line {
  12. void KeyCallbackMachine::register_key_input_callback(Vector<Key> keys, Function<bool(Editor&)> callback)
  13. {
  14. m_key_callbacks.set(keys, make<KeyCallback>(move(callback)));
  15. }
  16. void KeyCallbackMachine::key_pressed(Editor& editor, Key key)
  17. {
  18. dbgln_if(CALLBACK_MACHINE_DEBUG, "Key<{}, {}> pressed, seq_length={}, {} things in the matching vector", key.key, key.modifiers, m_sequence_length, m_current_matching_keys.size());
  19. if (m_sequence_length == 0) {
  20. VERIFY(m_current_matching_keys.is_empty());
  21. for (auto& it : m_key_callbacks) {
  22. if (it.key.first() == key)
  23. m_current_matching_keys.append(it.key);
  24. }
  25. if (m_current_matching_keys.is_empty()) {
  26. m_should_process_this_key = true;
  27. return;
  28. }
  29. }
  30. ++m_sequence_length;
  31. Vector<Vector<Key>> old_matching_keys;
  32. swap(m_current_matching_keys, old_matching_keys);
  33. for (auto& okey : old_matching_keys) {
  34. if (okey.size() < m_sequence_length)
  35. continue;
  36. if (okey[m_sequence_length - 1] == key)
  37. m_current_matching_keys.append(okey);
  38. }
  39. if (m_current_matching_keys.is_empty()) {
  40. // Insert any keys that were captured
  41. if (!old_matching_keys.is_empty()) {
  42. auto& keys = old_matching_keys.first();
  43. for (size_t i = 0; i < m_sequence_length - 1; ++i)
  44. editor.insert(keys[i].key);
  45. }
  46. m_sequence_length = 0;
  47. m_should_process_this_key = true;
  48. return;
  49. }
  50. if constexpr (CALLBACK_MACHINE_DEBUG) {
  51. dbgln("seq_length={}, matching vector:", m_sequence_length);
  52. for (auto& key : m_current_matching_keys) {
  53. for (auto& k : key)
  54. dbgln(" {}, {}", k.key, k.modifiers);
  55. dbgln("");
  56. }
  57. }
  58. m_should_process_this_key = false;
  59. for (auto& key : m_current_matching_keys) {
  60. if (key.size() == m_sequence_length) {
  61. m_should_process_this_key = m_key_callbacks.get(key).value()->callback(editor);
  62. m_sequence_length = 0;
  63. m_current_matching_keys.clear();
  64. return;
  65. }
  66. }
  67. }
  68. void KeyCallbackMachine::interrupted(Editor& editor)
  69. {
  70. m_sequence_length = 0;
  71. m_current_matching_keys.clear();
  72. if (auto callback = m_key_callbacks.get({ ctrl('C') }); callback.has_value())
  73. m_should_process_this_key = callback.value()->callback(editor);
  74. else
  75. m_should_process_this_key = true;
  76. }
  77. }