EditingEngine.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (c) 2021-2022, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Noncopyable.h>
  8. #include <LibGUI/Event.h>
  9. #include <LibGUI/TextDocument.h>
  10. namespace GUI {
  11. enum CursorWidth {
  12. NARROW,
  13. WIDE
  14. };
  15. enum EngineType {
  16. Regular,
  17. Vim,
  18. };
  19. class EditingEngine {
  20. AK_MAKE_NONCOPYABLE(EditingEngine);
  21. AK_MAKE_NONMOVABLE(EditingEngine);
  22. public:
  23. virtual ~EditingEngine() = default;
  24. virtual CursorWidth cursor_width() const { return NARROW; }
  25. void attach(TextEditor& editor);
  26. void detach();
  27. TextEditor& editor()
  28. {
  29. VERIFY(!m_editor.is_null());
  30. return *m_editor.unsafe_ptr();
  31. }
  32. virtual bool on_key(KeyEvent const& event);
  33. bool is_regular() const { return engine_type() == EngineType::Regular; }
  34. bool is_vim() const { return engine_type() == EngineType::Vim; }
  35. protected:
  36. EditingEngine() = default;
  37. WeakPtr<TextEditor> m_editor;
  38. void move_one_left();
  39. void move_one_right();
  40. void move_one_up(KeyEvent const& event);
  41. void move_one_down(KeyEvent const& event);
  42. void move_to_previous_span();
  43. void move_to_next_span();
  44. void move_to_logical_line_beginning();
  45. void move_to_logical_line_end();
  46. void move_to_line_beginning();
  47. void move_to_line_end();
  48. void move_page_up();
  49. void move_page_down();
  50. void move_to_first_line();
  51. void move_to_last_line();
  52. TextPosition find_beginning_of_next_word();
  53. void move_to_beginning_of_next_word();
  54. TextPosition find_end_of_next_word();
  55. void move_to_end_of_next_word();
  56. TextPosition find_end_of_previous_word();
  57. void move_to_end_of_previous_word();
  58. TextPosition find_beginning_of_previous_word();
  59. void move_to_beginning_of_previous_word();
  60. void move_up(double page_height_factor);
  61. void move_down(double page_height_factor);
  62. void get_selection_line_boundaries(size_t& first_line, size_t& last_line);
  63. void delete_line();
  64. void delete_char();
  65. virtual EngineType engine_type() const = 0;
  66. private:
  67. void move_selected_lines_up();
  68. void move_selected_lines_down();
  69. };
  70. }