| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- #include "lumacs/window.hpp"
- namespace lumacs {
- Window::Window(std::shared_ptr<Buffer> buffer)
- : buffer_(std::move(buffer))
- {
- }
- void Window::set_buffer(std::shared_ptr<Buffer> buffer) {
- buffer_ = std::move(buffer);
- cursor_ = {0, 0};
- viewport_.scroll_offset = 0;
- }
- void Window::set_cursor(Position pos) {
- cursor_ = buffer_->clamp_position(pos);
- adjust_scroll();
- }
- void Window::move_up() {
- if (cursor_.line > 0) {
- cursor_.line--;
- clamp_cursor();
- adjust_scroll();
- }
- }
- void Window::move_down() {
- if (cursor_.line < buffer_->line_count() - 1) {
- cursor_.line++;
- clamp_cursor();
- adjust_scroll();
- }
- }
- void Window::move_left() {
- if (cursor_.column > 0) {
- cursor_.column--;
- } else if (cursor_.line > 0) {
- cursor_.line--;
- cursor_.column = buffer_->line(cursor_.line).size();
- adjust_scroll();
- }
- }
- void Window::move_right() {
- const auto& line = buffer_->line(cursor_.line);
- if (cursor_.column < line.size()) {
- cursor_.column++;
- } else if (cursor_.line < buffer_->line_count() - 1) {
- cursor_.line++;
- cursor_.column = 0;
- adjust_scroll();
- }
- }
- void Window::move_to_line_start() {
- cursor_.column = 0;
- }
- void Window::move_to_line_end() {
- cursor_.column = buffer_->line(cursor_.line).size();
- }
- void Window::set_viewport_size(int width, int height) {
- viewport_.width = std::max(1, width);
- viewport_.height = std::max(1, height);
- adjust_scroll();
- }
- void Window::adjust_scroll() {
- viewport_.height = std::max(10, viewport_.height);
- if (static_cast<int>(cursor_.line) >= viewport_.scroll_offset + viewport_.height - SCROLL_MARGIN) {
- viewport_.scroll_offset = static_cast<int>(cursor_.line) - viewport_.height + SCROLL_MARGIN + 1;
- }
- if (static_cast<int>(cursor_.line) < viewport_.scroll_offset + SCROLL_MARGIN) {
- viewport_.scroll_offset = (cursor_.line >= static_cast<size_t>(SCROLL_MARGIN))
- ? static_cast<int>(cursor_.line) - SCROLL_MARGIN
- : 0;
- }
- viewport_.scroll_offset = std::max(0, viewport_.scroll_offset);
- }
- std::pair<size_t, size_t> Window::visible_line_range() const {
- size_t start = viewport_.scroll_offset;
- size_t end = std::min(buffer_->line_count(),
- static_cast<size_t>(viewport_.scroll_offset + viewport_.height));
- return {start, end};
- }
- void Window::clamp_cursor() {
- cursor_ = buffer_->clamp_position(cursor_);
- }
- } // namespace lumacs
|