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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
|
/*
* 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/>.
*/
#include "document_view.hh"
#include <algorithm>
#include <utility>
namespace ricochet::tui::detail {
namespace {
ScreenSize body_size( ScreenSize viewport )
{ return { std::max( 1, viewport.rows ), std::max( 1, viewport.columns ) }; }
std::size_t shifted_row( std::size_t row, std::int64_t distance, std::size_t last )
{
// Clamp the displacement before addition; restored relative offsets can be signed.
const auto origin = static_cast<std::int64_t>( row );
return static_cast<std::size_t>( origin
+ std::clamp( distance, -origin, static_cast<std::int64_t>( last ) - origin ) );
}
} // namespace
DocumentView::DocumentView( Document document, ViewSnapshot snapshot, const TextService& text, ScreenSize viewport )
: document_( std::move( document ) )
, snapshot_( std::move( snapshot ) )
, text_( text )
, content_( document_, text_ )
, viewport_( body_size( viewport ) )
, layout_( document_, text_, viewport_.columns, snapshot_.horizontal_scroll )
{
// A history snapshot can name a removed boundary. The new document starts at
// its first reading unit; it never manufactures a byte offset into that text.
if ( std::holds_alternative<DocumentStart>( snapshot_.cursor.point ) || !layout_.locate( snapshot_.cursor ) ) {
snapshot_.cursor = layout_.first();
}
desired_column_ = layout_.locate( snapshot_.cursor ).value().column;
restore_viewport();
if ( snapshot_.selection ) {
const auto& selection = *snapshot_.selection;
// History endpoints must still be layout boundaries in this exact document/draft.
if ( layout_.locate( { selection.anchor.start } ) && layout_.locate( { selection.anchor.end } )
&& layout_.locate( { selection.active.start } ) && layout_.locate( { selection.active.end } ) ) {
snapshot_.selection = content_.select( selection.anchor, selection.active );
} else {
snapshot_.selection.reset();
}
}
}
const Document& DocumentView::document() const
{ return document_; }
const ViewSnapshot& DocumentView::snapshot() const
{ return snapshot_; }
std::size_t DocumentView::last_top_row() const
{ return layout_.rows() - std::min( layout_.rows(), static_cast<std::size_t>( viewport_.rows ) ); }
void DocumentView::set_top_row( std::size_t row )
{
top_row_ = std::min( row, last_top_row() );
snapshot_.viewport_anchor = layout_.on_row( { top_row_, 0 } );
const auto anchor = layout_.locate( snapshot_.viewport_anchor ).value();
snapshot_.viewport_row_offset = static_cast<std::int64_t>( top_row_ ) - static_cast<std::int64_t>( anchor.row );
}
void DocumentView::restore_viewport()
{
auto anchor = layout_.locate( snapshot_.viewport_anchor );
if ( !anchor ) {
snapshot_.viewport_anchor = layout_.first();
snapshot_.viewport_row_offset = 0;
anchor = layout_.locate( snapshot_.viewport_anchor );
}
set_top_row( shifted_row( anchor->row, snapshot_.viewport_row_offset, last_top_row() ) );
}
void DocumentView::resize( ScreenSize viewport )
{
const auto next = body_size( viewport );
if ( next == viewport_ ) {
return;
}
stop_drag();
const bool reflow = next.columns != viewport_.columns;
viewport_ = next;
if ( reflow ) {
layout_ = DocumentLayout( document_, text_, viewport_.columns, snapshot_.horizontal_scroll );
desired_column_ = layout_.locate( snapshot_.cursor ).value().column;
}
restore_viewport();
// A wheel/pan action deliberately allows the cursor to stay outside the view.
if ( follow_cursor_ ) {
reveal_cursor();
}
}
void DocumentView::apply_scrolls( std::span<const HorizontalScroll> changes )
{
if ( changes.empty() ) {
return;
}
for ( const auto& change : changes ) {
const auto found = std::ranges::find( snapshot_.horizontal_scroll, change.block, &HorizontalScroll::block );
if ( found == snapshot_.horizontal_scroll.end() ) {
snapshot_.horizontal_scroll.push_back( change );
} else {
*found = change;
}
}
layout_ = DocumentLayout( document_, text_, viewport_.columns, snapshot_.horizontal_scroll );
restore_viewport();
}
void DocumentView::reveal_cursor()
{
apply_scrolls( layout_.reveal( snapshot_.cursor ) );
const auto point = layout_.locate( snapshot_.cursor ).value();
if ( point.row < top_row_ ) {
set_top_row( point.row );
} else if ( point.row - top_row_ >= static_cast<std::size_t>( viewport_.rows ) ) {
set_top_row( point.row - static_cast<std::size_t>( viewport_.rows - 1 ) );
}
}
void DocumentView::finish_move( bool preserve_column )
{
follow_cursor_ = true;
extend_selection();
reveal_cursor();
if ( !preserve_column ) {
desired_column_ = layout_.locate( snapshot_.cursor ).value().column;
}
}
void DocumentView::move_rows( std::int64_t distance, bool page )
{
const auto origin = layout_.locate( snapshot_.cursor ).value();
const auto row = shifted_row( origin.row, distance, layout_.rows() - 1 );
snapshot_.cursor = layout_.on_row( { row, desired_column_ } );
if ( page ) {
// Shift both by the same amount before revealing, retaining screen-relative
// cursor position wherever document boundaries permit it.
set_top_row( shifted_row( top_row_, distance, last_top_row() ) );
}
finish_move( true );
}
void DocumentView::move( ViewMotion motion )
{
stop_drag();
switch ( motion ) {
case ViewMotion::Left:
snapshot_.cursor = layout_.adjacent( snapshot_.cursor, -1 );
break;
case ViewMotion::Right:
snapshot_.cursor = layout_.adjacent( snapshot_.cursor, 1 );
break;
case ViewMotion::NextWord:
snapshot_.cursor = layout_.word( snapshot_.cursor, WordMotion::Next );
break;
case ViewMotion::PreviousWord:
snapshot_.cursor = layout_.word( snapshot_.cursor, WordMotion::Previous );
break;
case ViewMotion::WordEnd:
snapshot_.cursor = layout_.word( snapshot_.cursor, WordMotion::End );
break;
case ViewMotion::LineStart:
snapshot_.cursor = layout_.line_edge( snapshot_.cursor, false );
break;
case ViewMotion::LineEnd:
snapshot_.cursor = layout_.line_edge( snapshot_.cursor, true );
break;
case ViewMotion::DocumentStart:
snapshot_.cursor = layout_.first();
break;
case ViewMotion::DocumentEnd:
snapshot_.cursor = layout_.last();
break;
case ViewMotion::Up:
move_rows( -1, false );
return;
case ViewMotion::Down:
move_rows( 1, false );
return;
case ViewMotion::PageUp:
move_rows( -std::max( 1, viewport_.rows - 1 ), true );
return;
case ViewMotion::PageDown:
move_rows( std::max( 1, viewport_.rows - 1 ), true );
return;
case ViewMotion::HalfPageUp:
move_rows( -std::max( 1, viewport_.rows / 2 ), true );
return;
case ViewMotion::HalfPageDown:
move_rows( std::max( 1, viewport_.rows / 2 ), true );
return;
case ViewMotion::PanLeft:
case ViewMotion::PanRight: {
const auto changes = layout_.pan( snapshot_.cursor, motion == ViewMotion::PanLeft ? -1 : 1 );
if ( changes.empty() ) {
return;
}
const auto old_column = layout_.locate( snapshot_.cursor ).value().column;
apply_scrolls( changes );
// Panning changes screen coordinates, not the desired logical column for
// the next vertical move (including one remembered across a short line).
desired_column_ += layout_.locate( snapshot_.cursor ).value().column - old_column;
follow_cursor_ = false;
return;
}
}
finish_move( false );
}
void DocumentView::begin_selection()
{
stop_drag();
const auto unit = layout_.unit( snapshot_.cursor );
snapshot_.selection = content_.select( unit, unit );
}
void DocumentView::cancel_selection()
{
stop_drag();
snapshot_.selection.reset();
}
void DocumentView::extend_selection()
{
if ( snapshot_.selection ) {
snapshot_.selection = content_.select( snapshot_.selection->anchor, layout_.unit( snapshot_.cursor ) );
}
}
std::optional<std::string> DocumentView::selected_text() const
{
if ( !snapshot_.selection ) {
return std::nullopt;
}
return content_.extract( snapshot_.selection->range );
}
void DocumentView::press( CellPoint point, std::chrono::steady_clock::time_point now )
{
const auto hit = layout_.hit( { top_row_ + static_cast<std::size_t>( point.row ), point.column } );
// A completely clipped wide glyph has no visible cell to hit.
if ( !hit ) {
return;
}
snapshot_.cursor = hit->position;
cancel_selection();
desired_column_ = layout_.locate( snapshot_.cursor ).value().column;
follow_cursor_ = false;
drag_ = Drag { point, point, layout_.unit( snapshot_.cursor ), false, now + std::chrono::milliseconds( 100 ) };
}
void DocumentView::drag_to_pointer()
{
const auto row = std::clamp( drag_->pointer.row, 0, viewport_.rows - 1 );
const auto column = std::clamp( drag_->pointer.column, 0, viewport_.columns - 1 );
const auto hit = layout_.hit( { top_row_ + static_cast<std::size_t>( row ), column } );
if ( !hit ) {
return;
}
snapshot_.cursor = hit->position;
snapshot_.selection = content_.select( drag_->anchor, layout_.unit( snapshot_.cursor ) );
desired_column_ = layout_.locate( snapshot_.cursor ).value().column;
follow_cursor_ = false;
}
void DocumentView::drag( CellPoint point )
{
drag_->pointer = point;
// One cell is the terminal's movement threshold; wide halves still share a unit.
drag_->selecting = drag_->selecting || point.row != drag_->press.row || point.column != drag_->press.column;
if ( drag_->selecting ) {
drag_to_pointer();
}
}
void DocumentView::release( CellPoint point )
{
drag( point );
stop_drag();
}
void DocumentView::stop_drag()
{ drag_.reset(); }
bool DocumentView::dragging() const
{ return drag_.has_value(); }
bool DocumentView::tick( std::chrono::steady_clock::time_point now )
{
if ( !drag_ || !drag_->selecting || now < drag_->next_scroll ) {
return false;
}
drag_->next_scroll = now + std::chrono::milliseconds( 100 );
// With a one-row body both edges coincide; leaving it above/below disambiguates.
const int direction = drag_->pointer.row < 0 ? -1
: drag_->pointer.row >= viewport_.rows ? 1
: viewport_.rows == 1 ? 0
: drag_->pointer.row == 0 ? -1
: drag_->pointer.row == viewport_.rows - 1 ? 1
: 0;
const auto next = shifted_row( top_row_, direction, last_top_row() );
if ( next == top_row_ ) {
return false;
}
set_top_row( next );
drag_to_pointer();
return true;
}
void DocumentView::wheel( int steps )
{
set_top_row( shifted_row( top_row_, static_cast<std::int64_t>( steps ) * 3, last_top_row() ) );
follow_cursor_ = false;
if ( drag_ && drag_->selecting ) {
drag_to_pointer();
}
}
bool DocumentView::locate( const DocumentPoint& point )
{
const DisplayPosition requested { point, WrapAffinity::NextLine };
const auto location = layout_.locate( requested );
// Host jumps can refer to absent anchors, removed leaves or non-grapheme offsets.
if ( !location ) {
return false;
}
snapshot_.cursor = requested;
if ( std::holds_alternative<DocumentStart>( point ) ) {
snapshot_.cursor = layout_.first();
} else if ( const auto* object = std::get_if<ObjectPosition>( &point );
object && std::holds_alternative<BlockId>( object->object ) ) {
snapshot_.cursor = layout_.on_row( *location );
}
cancel_selection();
finish_move( false );
return true;
}
std::optional<HitTarget> DocumentView::focused_target() const
{ return layout_.target( snapshot_.cursor ); }
FocusResult DocumentView::focus( bool backward )
{
const auto next = layout_.focus( snapshot_.cursor, backward );
if ( !next ) {
return FocusResult::Empty;
}
stop_drag();
snapshot_.cursor = next->position;
finish_move( false );
return next->wrapped ? FocusResult::Wrapped : FocusResult::Moved;
}
const Link* DocumentView::focused_link() const
{
const auto target = focused_target();
if ( !target || !std::holds_alternative<LinkId>( *target ) ) {
return nullptr;
}
// Layout link targets originate from this same immutable document.
return &*std::ranges::find( document_.data().links, std::get<LinkId>( *target ), &Link::id );
}
void DocumentView::draw( Frame& frame, CellPoint origin ) const
{
const auto selection = snapshot_.selection ? std::optional( snapshot_.selection->range ) : std::nullopt;
layout_.append_frame(
frame, top_row_, origin, viewport_, content_, selection, snapshot_.cursor, focused_target() );
const auto cursor = layout_.cursor_cell( snapshot_.cursor );
if ( cursor && cursor->row >= top_row_ && cursor->row - top_row_ < static_cast<std::size_t>( viewport_.rows )
&& cursor->column >= 0 && cursor->column < viewport_.columns ) {
frame.cursor = CellPoint { origin.row + static_cast<int>( cursor->row - top_row_ ),
origin.column + static_cast<int>( cursor->column ) };
}
}
} // namespace ricochet::tui::detail
|