aboutsummaryrefslogtreecommitdiff
path: root/src/tui/document_view.hh
blob: 5f5e331ddc667f3a2cb5d55f3111c62f74ae88e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * Ricochet - A minimalist CLI web browser for fast, distraction-free navigation.
 * Copyright (C) 2026  Richard Qin
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#pragma once

#include "document_text.hh"
#include "layout.hh"

#include <chrono>

namespace ricochet::tui::detail {

enum class ViewMotion : std::uint8_t
{
  Left,
  Right,
  Up,
  Down,
  NextWord,
  PreviousWord,
  WordEnd,
  LineStart,
  LineEnd,
  DocumentStart,
  DocumentEnd,
  PageUp,
  PageDown,
  HalfPageUp,
  HalfPageDown,
  PanLeft,
  PanRight
};

enum class FocusResult : std::uint8_t
{
  Empty,
  Moved,
  Wrapped
};

// One tab's reading state. It owns the logical snapshot; screen rows are caches.
// Keyboard, mouse and host navigation all use the same layout/source mappings.
class DocumentView
{
public:
  DocumentView( Document document, ViewSnapshot snapshot, const TextService& text, ScreenSize viewport );

  const Document& document() const;
  const ViewSnapshot& snapshot() const;
  void resize( ScreenSize viewport );
  void move( ViewMotion motion );
  void press( CellPoint point, std::chrono::steady_clock::time_point now );
  // The session routes these only while dragging() owns mouse capture.
  void drag( CellPoint point );
  void release( CellPoint point );
  void stop_drag();
  bool dragging() const;
  bool tick( std::chrono::steady_clock::time_point now );
  void begin_selection();
  void cancel_selection();
  std::optional<std::string> selected_text() const;
  void wheel( int steps );
  bool locate( const DocumentPoint& point );
  std::optional<HitTarget> focused_target() const;
  // Browse/field-editor routing owns mode eligibility; this follows the shared order.
  FocusResult focus( bool backward );
  const Link* focused_link() const;
  void draw( Frame& frame, CellPoint origin ) const;

private:
  std::size_t last_top_row() const;
  void set_top_row( std::size_t row );
  void restore_viewport();
  void apply_scrolls( std::span<const HorizontalScroll> changes );
  void reveal_cursor();
  void finish_move( bool preserve_column );
  void move_rows( std::int64_t distance, bool page );
  void extend_selection();
  void drag_to_pointer();

  struct Drag
  {
    CellPoint press {};
    CellPoint pointer {};
    DocumentRange anchor {};
    bool selecting { false };
    std::chrono::steady_clock::time_point next_scroll {};
  };

  Document document_;
  ViewSnapshot snapshot_;
  const TextService& text_;
  DocumentText content_;
  ScreenSize viewport_;
  DocumentLayout layout_;
  std::size_t top_row_ { 0 };
  std::int64_t desired_column_ { 0 };
  bool follow_cursor_ { false };
  std::optional<Drag> drag_ {};
};

} // namespace ricochet::tui::detail