window.hpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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;
  11. };
  12. class Window {
  13. public:
  14. Window(std::shared_ptr<Buffer> buffer);
  15. void set_buffer(std::shared_ptr<Buffer> buffer);
  16. // Accessors
  17. std::shared_ptr<Buffer> buffer_ptr() const { return buffer_; }
  18. Buffer& buffer() { return *buffer_; }
  19. const Buffer& buffer() const { return *buffer_; }
  20. void set_cursor(Position pos);
  21. Position cursor() const { return cursor_; }
  22. // Movement
  23. void move_up();
  24. void move_down();
  25. void move_left();
  26. void move_right();
  27. void move_to_line_start();
  28. void move_to_line_end();
  29. // Viewport
  30. void set_viewport_size(int width, int height);
  31. const Viewport& viewport() const { return viewport_; }
  32. void adjust_scroll();
  33. std::pair<size_t, size_t> visible_line_range() const;
  34. private:
  35. void clamp_cursor();
  36. std::shared_ptr<Buffer> buffer_;
  37. Position cursor_;
  38. Viewport viewport_;
  39. static constexpr int SCROLL_MARGIN = 3;
  40. };
  41. } // namespace lumacs