EditingEngine.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. void move_up(double page_height_factor);
  53. void move_down(double page_height_factor);
  54. void get_selection_line_boundaries(size_t& first_line, size_t& last_line);
  55. void delete_line();
  56. void delete_char();
  57. virtual EngineType engine_type() const = 0;
  58. private:
  59. void move_selected_lines_up();
  60. void move_selected_lines_down();
  61. };
  62. }