window.hpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #pragma once
  2. #include "lumacs/buffer.hpp"
  3. #include <memory>
  4. #include <utility>
  5. #include <algorithm>
  6. namespace lumacs {
  7. struct Viewport {
  8. int width = 0;
  9. int height = 0;
  10. int scroll_offset = 0; // Vertical scroll (line offset)
  11. int horizontal_offset = 0; // Horizontal scroll (column offset)
  12. };
  13. class Window {
  14. public:
  15. Window(std::shared_ptr<Buffer> buffer);
  16. void set_buffer(std::shared_ptr<Buffer> buffer);
  17. // Accessors
  18. std::shared_ptr<Buffer> buffer_ptr() const { return buffer_; }
  19. Buffer& buffer() { return *buffer_; }
  20. const Buffer& buffer() const { return *buffer_; }
  21. void set_cursor(Position pos);
  22. Position cursor() const { return cursor_; }
  23. // Movement
  24. void move_up();
  25. void move_down();
  26. void move_left();
  27. void move_right();
  28. void move_to_line_start();
  29. void move_to_line_end();
  30. // Viewport
  31. void set_viewport_size(int width, int height);
  32. const Viewport& viewport() const { return viewport_; }
  33. void adjust_scroll();
  34. // Explicit scrolling (moves view, and cursor if necessary)
  35. void scroll_lines(int lines); // + for down, - for up
  36. std::pair<size_t, size_t> visible_line_range() const;
  37. private:
  38. void clamp_cursor();
  39. std::shared_ptr<Buffer> buffer_;
  40. Position cursor_;
  41. Viewport viewport_;
  42. static constexpr int SCROLL_MARGIN = 3;
  43. };
  44. } // namespace lumacs