teksilo-platform 0.24.1

Platform integration for Teksilo — winit and AccessKit adapters, clipboard, OS theme, native file dialogs, drag-and-drop and menus.
Documentation
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: 2026 FernTech

//! What a window's native view answers the platform's text input system,
//! from the focused text widget's [`TextInputDocument`]: the half that needs
//! no platform.
//!
//! The document counts in characters and places things in window-logical
//! coordinates. The platform counts in UTF-16 code units, with a "not found"
//! location for "no range" (macOS's `NSNotFound`), and in its own screen
//! space. This module does the counting: ranges in and out, the conversions
//! through the document's
//! [`Utf16Index`](teksilo_core::text_input_document::Utf16Index), the
//! clamping, and the walk over a
//! selection's lines. The coordinates it hands back are still window-logical,
//! and the platform backend converts them to and from its screen space with
//! the platform's own calls, as winit converts its IME cursor area.
//!
//! Every function answers `None` where the document could not answer (busy,
//! no layout yet): the caller then answers as it would without a document.

#![cfg_attr(
    not(target_os = "macos"),
    allow(
        dead_code,
        reason = "the macOS view answers every question here; the Windows hook a few"
    )
)]

use std::ops::Range;

use teksilo_canvas::{Point, Rect};
use teksilo_core::text_input_document::TextInputDocument;

/// The "no range" location: macOS's `NSNotFound`, an `NSInteger`'s largest
/// value. Any location from it up reads as none.
pub(crate) const NOT_FOUND: usize = isize::MAX as usize;

/// A range as the platform counts it: a location and a length in UTF-16 code
/// units, the location [`NOT_FOUND`] for none. The shape of an `NSRange`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Utf16Range {
    pub(crate) location: usize,
    pub(crate) length: usize,
}

impl Utf16Range {
    /// No range.
    pub(crate) const NONE: Self = Self {
        location: NOT_FOUND,
        length: 0,
    };

    /// `range` as a location and a length; an end before the start is an
    /// empty range at the start.
    pub(crate) fn from_range(range: Range<usize>) -> Self {
        Self {
            location: range.start,
            length: range.end.saturating_sub(range.start),
        }
    }

    /// The range, `None` for none. An end past `usize::MAX` stops there.
    pub(crate) fn to_range(self) -> Option<Range<usize>> {
        (self.location < NOT_FOUND)
            .then(|| self.location..self.location.saturating_add(self.length))
    }
}

/// `writingToolsBehavior`'s `NSWritingToolsBehaviorNone`: Writing Tools
/// leaves the view alone.
pub(crate) const WRITING_TOOLS_NONE: isize = -1;
/// `writingToolsBehavior`'s `NSWritingToolsBehaviorLimited`: Writing Tools
/// works in its panel and replaces the text from there, never inline.
pub(crate) const WRITING_TOOLS_LIMITED: isize = 2;
/// `allowedWritingToolsResultOptions`' `NSWritingToolsResultPlainText`: the
/// text Writing Tools hands back carries no formatting.
pub(crate) const WRITING_TOOLS_PLAIN_TEXT: usize = 1;

/// The most lines [`union_rect_in_visible_selection`] walks. Far more than a
/// window shows: the walk starts at the first visible line.
pub(crate) const MAX_UNION_LINES: usize = 1024;

/// `selectedRange`: the selection, in UTF-16.
pub(crate) fn selected_range(document: &dyn TextInputDocument) -> Option<Utf16Range> {
    let index = document.utf16_index()?;
    let selection = document.selection()?;
    Some(Utf16Range::from_range(index.char_range_to_utf16(selection)))
}

/// `markedRange`: the composition, in UTF-16, while the platform's own view
/// holds marked text (`view_has_marked_text`); none otherwise, and none
/// while the view holds marked text the document has no composition for.
///
/// That last case is the moment after an input method commits inside a key
/// press: winit has sent the commit, which the field has taken, and keeps
/// its marked text until the key press is over. Its own answer then is its
/// marked text's range counted from its own start, `0..length`, which is not
/// where anything is in the document: measured on macOS 27 (round 10),
/// Kotoeri asked right after committing ニホン at the end of a heading, got
/// 0..3, and read "Ric", the document's first characters, as its marked
/// text. A native text view answers none after a commit. `None` (the view's
/// own answer stands) only while the document cannot answer.
pub(crate) fn marked_range(
    document: &dyn TextInputDocument,
    view_has_marked_text: bool,
) -> Option<Utf16Range> {
    if !view_has_marked_text {
        return Some(Utf16Range::NONE);
    }
    let index = document.utf16_index()?;
    Some(
        document
            .composition_range()
            .map_or(Utf16Range::NONE, |composition| {
                Utf16Range::from_range(index.char_range_to_utf16(composition))
            }),
    )
}

/// The answer to `attributedSubstringForProposedRange:actualRange:`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum Substring {
    /// No text: a secure field, or no range asked.
    Nil,
    /// `text`, which is the document's `actual` range: the proposed one,
    /// clamped to the document and widened to whole characters.
    Text { text: String, actual: Utf16Range },
}

/// `attributedSubstringForProposedRange:actualRange:`.
pub(crate) fn substring(
    document: &dyn TextInputDocument,
    proposed: Utf16Range,
) -> Option<Substring> {
    if document.is_secure() {
        return Some(Substring::Nil);
    }
    let Some(proposed) = proposed.to_range() else {
        return Some(Substring::Nil);
    };
    let index = document.utf16_index()?;
    let chars = index.utf16_range_to_chars(proposed);
    let text = document.text_in(chars.clone())?;
    Some(Substring::Text {
        text,
        actual: Utf16Range::from_range(index.char_range_to_utf16(chars)),
    })
}

/// `characterIndexForPoint:`, for a point already in window-logical
/// coordinates: the UTF-16 offset of the character there, [`NOT_FOUND`]
/// where the document has none (outside its text, a field that shows
/// nothing).
pub(crate) fn character_index(
    document: &dyn TextInputDocument,
    window_point: Point,
) -> Option<usize> {
    let index = document.utf16_index()?;
    Some(
        document
            .offset_at_point(window_point)
            .map_or(NOT_FOUND, |offset| index.char_to_utf16(offset)),
    )
}

/// `firstRectForCharacterRange:actualRange:`: where the first line of
/// `range` is drawn, window-logical, and the UTF-16 range of the characters
/// that line holds. No range asks for the selection's, which is where a
/// panel the input system opens (Emoji & Symbols) is anchored.
pub(crate) fn first_rect(
    document: &dyn TextInputDocument,
    range: Utf16Range,
) -> Option<(Rect, Utf16Range)> {
    let index = document.utf16_index()?;
    let chars = match range.to_range() {
        Some(range) => index.utf16_range_to_chars(range),
        None => document.selection()?,
    };
    let line = document.first_line_rect(chars)?;
    Some((
        line.rect,
        Utf16Range::from_range(index.char_range_to_utf16(line.covered)),
    ))
}

/// The character range a UTF-16 range the input system named stands for,
/// for an [`ImeCommitPayload`](super::ImeCommitPayload)'s replacement.
pub(crate) fn replacement_chars(
    document: &dyn TextInputDocument,
    range: Utf16Range,
) -> Option<Range<usize>> {
    let range = range.to_range()?;
    Some(document.utf16_index()?.utf16_range_to_chars(range))
}

/// `documentVisibleRect`: the part of the text that is on screen,
/// window-logical.
pub(crate) fn visible_rect(document: &dyn TextInputDocument) -> Option<Rect> {
    document.visible_text_rect()
}

/// `unionRectInVisibleSelectedRange`: the union of the selection's lines,
/// each clipped to the visible text, window-logical; the caret's rectangle,
/// clipped, for an empty selection. `None` when none of it is visible.
///
/// The walk starts at the first visible line when the selection begins
/// above it (the lower of the offsets at the visible text's two top corners,
/// so a right-to-left line is taken whole), stops at the first line below
/// the visible text, and takes at most [`MAX_UNION_LINES`] lines.
pub(crate) fn union_rect_in_visible_selection(document: &dyn TextInputDocument) -> Option<Rect> {
    let visible = document.visible_text_rect()?;
    let selection = document.selection()?;
    if selection.is_empty() {
        let caret = document.first_line_rect(selection)?.rect;
        return intersection(caret, visible);
    }
    let first_visible = [visible.x + 0.5, visible.right() - 0.5]
        .into_iter()
        .filter_map(|x| document.offset_at_point(Point::new(x, visible.y + 0.5)))
        .min();
    let mut start = match first_visible {
        Some(offset) if offset > selection.start && offset < selection.end => offset,
        _ => selection.start,
    };
    let mut union: Option<Rect> = None;
    for _ in 0..MAX_UNION_LINES {
        if start >= selection.end {
            break;
        }
        let Some(line) = document.first_line_rect(start..selection.end) else {
            break;
        };
        if line.rect.y > visible.bottom() {
            break;
        }
        if let Some(clipped) = intersection(line.rect, visible) {
            union = Some(union.map_or(clipped, |union| bounding(union, clipped)));
        }
        if line.covered.end <= start {
            break;
        }
        start = line.covered.end;
    }
    union
}

/// TSF's `GetTextExt`: the bounding box of the lines of `range`
/// (characters), window-logical, clipped to the visible text, and whether
/// any of it was cut off; the caret's rectangle for an empty range, drawn
/// downstream at a soft line break (at the start of the next line, where a
/// caret placed there is drawn, not at the end of the line it ends). A range
/// none of which is visible is answered whole, as cut off, so a candidate
/// window still opens next to it. At most [`MAX_UNION_LINES`] lines are
/// walked. `None` when the document cannot place the range's first line.
#[cfg_attr(
    target_os = "macos",
    allow(
        dead_code,
        reason = "the Windows store asks it; the macOS view asks firstRect"
    )
)]
pub(crate) fn range_rect(
    document: &dyn TextInputDocument,
    range: Range<usize>,
) -> Option<(Rect, bool)> {
    let mut union: Option<Rect> = None;
    if range.is_empty() {
        let at = range.start;
        let caret = document.first_line_rect(at..at)?.rect;
        // The character after the caret, when it starts another visual
        // line: the caret is at a soft line break.
        let next = document
            .first_line_rect(at..at + 1)
            .filter(|next| next.covered.start == at && !next.covered.is_empty())
            .map(|next| next.rect)
            .filter(|next| (next.y - caret.y).abs() > 0.5);
        union = Some(match next {
            Some(next) => Rect::new(next.x, next.y, caret.width, next.height),
            None => caret,
        });
    } else {
        let mut start = range.start;
        for _ in 0..MAX_UNION_LINES {
            if start >= range.end {
                break;
            }
            let Some(line) = document.first_line_rect(start..range.end) else {
                break;
            };
            union = Some(union.map_or(line.rect, |union| bounding(union, line.rect)));
            if line.covered.end <= start {
                break;
            }
            start = line.covered.end;
        }
    }
    let union = union?;
    Some(match document.visible_text_rect() {
        // A box that only touches the visible text's edge is not visible.
        Some(visible) => match intersection(union, visible) {
            Some(clipped) if clipped.height > 0.0 || union.height <= 0.0 => {
                (clipped, clipped != union)
            }
            _ => (union, true),
        },
        None => (union, false),
    })
}

/// `isEditable`: always `true`, whatever has focus. AppKit gives a view that
/// answers `false` no input context, and winit's view reads its input context
/// when it is created and at every `keyDown:`, and panics without one
/// (measured on macOS 27: a window opened while no text field had focus).
/// Writing Tools, which asks before it replaces text, is kept off text it may
/// not replace by [`writing_tools_behavior`] instead.
pub(crate) fn is_editable() -> bool {
    true
}

/// `writingToolsBehavior`: [`WRITING_TOOLS_LIMITED`] where the focused
/// widget takes text and lets Writing Tools at it, [`WRITING_TOOLS_NONE`]
/// otherwise and with no document.
pub(crate) fn writing_tools_behavior(document: Option<&dyn TextInputDocument>) -> isize {
    if document.is_some_and(|document| document.is_editable() && document.writing_tools_allowed()) {
        WRITING_TOOLS_LIMITED
    } else {
        WRITING_TOOLS_NONE
    }
}

/// Where `a` and `b` overlap, edges included, so a caret one point wide on
/// the visible text's edge stays; `None` when they do not touch.
pub(crate) fn intersection(a: Rect, b: Rect) -> Option<Rect> {
    let left = a.x.max(b.x);
    let top = a.y.max(b.y);
    let right = a.right().min(b.right());
    let bottom = a.bottom().min(b.bottom());
    (right >= left && bottom >= top).then(|| Rect::new(left, top, right - left, bottom - top))
}

/// The smallest rectangle holding `a` and `b`.
pub(crate) fn bounding(a: Rect, b: Rect) -> Rect {
    let left = a.x.min(b.x);
    let top = a.y.min(b.y);
    let right = a.right().max(b.right());
    let bottom = a.bottom().max(b.bottom());
    Rect::new(left, top, right - left, bottom - top)
}

#[cfg(test)]
mod tests {
    use std::cell::Cell;
    use std::rc::Rc;

    use teksilo_core::text_input_document::{FirstLineRect, Utf16Index};

    use super::*;

    /// A document laid out in a fixed grid: every character 10 wide, lines
    /// of `per_line` characters 20 tall, the text scrolled up by `scroll_y`
    /// in a visible area at the window's origin, `width` by `height`.
    struct Grid {
        text: String,
        selection: Range<usize>,
        composition: Option<Range<usize>>,
        per_line: usize,
        scroll_y: f32,
        width: f32,
        height: f32,
        secure: bool,
        editable: bool,
        writing_tools: bool,
        busy: bool,
        /// A caret at a line's end is placed at the end of that line
        /// (upstream), as a wrapping editor places it.
        upstream: bool,
        lines_asked: Cell<usize>,
    }

    impl Grid {
        fn new(text: &str) -> Self {
            Self {
                text: text.to_owned(),
                selection: 0..0,
                composition: None,
                per_line: 10,
                scroll_y: 0.0,
                width: 100.0,
                height: 60.0,
                secure: false,
                editable: true,
                writing_tools: true,
                busy: false,
                upstream: false,
                lines_asked: Cell::new(0),
            }
        }

        fn len(&self) -> usize {
            self.text.chars().count()
        }

        fn place(&self, offset: usize) -> (f32, f32) {
            let line = offset / self.per_line;
            let column = offset % self.per_line;
            (column as f32 * 10.0, line as f32 * 20.0 - self.scroll_y)
        }
    }

    impl TextInputDocument for Grid {
        fn char_count(&self) -> Option<usize> {
            (!self.busy).then(|| self.len())
        }
        fn text_in(&self, range: Range<usize>) -> Option<String> {
            if self.busy || self.secure {
                return None;
            }
            let end = range.end.min(self.len());
            let start = range.start.min(end);
            Some(self.text.chars().skip(start).take(end - start).collect())
        }
        fn selection(&self) -> Option<Range<usize>> {
            (!self.busy).then(|| self.selection.clone())
        }
        fn caret(&self) -> Option<usize> {
            (!self.busy).then_some(self.selection.end)
        }
        fn composition_range(&self) -> Option<Range<usize>> {
            if self.busy {
                None
            } else {
                self.composition.clone()
            }
        }
        fn first_line_rect(&self, range: Range<usize>) -> Option<FirstLineRect> {
            if self.busy {
                return None;
            }
            self.lines_asked.set(self.lines_asked.get() + 1);
            let end = range.end.min(self.len());
            let start = range.start.min(end);
            let (x, y) = self.place(start);
            if start == end && self.upstream && start > 0 && start.is_multiple_of(self.per_line) {
                let (x, y) = self.place(start - 1);
                return Some(FirstLineRect {
                    rect: Rect::new(x + 10.0, y, 1.0, 20.0),
                    covered: start..start,
                });
            }
            if start == end {
                return Some(FirstLineRect {
                    rect: Rect::new(x, y, 1.0, 20.0),
                    covered: start..start,
                });
            }
            let line_end = (start / self.per_line + 1) * self.per_line;
            let covered_end = end.min(line_end);
            let width = (covered_end - start) as f32 * 10.0;
            Some(FirstLineRect {
                rect: Rect::new(x, y, width, 20.0),
                covered: start..covered_end,
            })
        }
        fn offset_at_point(&self, point: Point) -> Option<usize> {
            if self.busy || !self.visible_text_rect()?.contains(point) {
                return None;
            }
            let line = ((point.y + self.scroll_y) / 20.0).floor() as usize;
            let column = ((point.x / 10.0).round() as usize).min(self.per_line);
            Some((line * self.per_line + column).min(self.len()))
        }
        fn visible_text_rect(&self) -> Option<Rect> {
            (!self.busy).then(|| Rect::new(0.0, 0.0, self.width, self.height))
        }
        fn is_editable(&self) -> bool {
            !self.busy && self.editable
        }
        fn is_secure(&self) -> bool {
            self.busy || self.secure
        }
        fn writing_tools_allowed(&self) -> bool {
            !self.busy && !self.secure && self.writing_tools
        }
        fn utf16_index(&self) -> Option<Rc<Utf16Index>> {
            (!self.busy).then(|| Rc::new(Utf16Index::new(&self.text)))
        }
    }

    /// "a😀b😀c": characters 0..5, UTF-16 units 0..7.
    const EMOJI: &str = "a\u{1F600}b\u{1F600}c";

    fn range(location: usize, length: usize) -> Utf16Range {
        Utf16Range { location, length }
    }

    #[test]
    fn a_range_converts_both_ways_and_none_is_none() {
        assert_eq!(Utf16Range::from_range(2..5), range(2, 3));
        #[expect(
            clippy::reversed_empty_ranges,
            reason = "a reversed range must collapse, not underflow"
        )]
        let reversed = 5..2;
        assert_eq!(Utf16Range::from_range(reversed), range(5, 0));
        assert_eq!(range(2, 3).to_range(), Some(2..5));
        assert_eq!(Utf16Range::NONE.to_range(), None);
        assert_eq!(range(NOT_FOUND, 4).to_range(), None);
        assert_eq!(range(usize::MAX, 0).to_range(), None);
        assert_eq!(
            range(1, usize::MAX).to_range(),
            Some(1..usize::MAX),
            "the end stops at the top"
        );
    }

    #[test]
    fn the_selection_is_answered_in_utf16() {
        let mut document = Grid::new(EMOJI);
        document.selection = 2..4;
        assert_eq!(selected_range(&document), Some(range(3, 3)));
        document.selection = 5..5;
        assert_eq!(selected_range(&document), Some(range(7, 0)));
        document.busy = true;
        assert_eq!(selected_range(&document), None, "busy: the view answers");
    }

    #[test]
    fn the_marked_range_follows_the_views_own_marked_text() {
        let mut document = Grid::new(EMOJI);
        document.composition = Some(1..3);
        assert_eq!(marked_range(&document, true), Some(range(1, 3)));
        assert_eq!(
            marked_range(&document, false),
            Some(Utf16Range::NONE),
            "no marked text in the view: none, whatever the document says"
        );
        document.composition = None;
        assert_eq!(
            marked_range(&document, true),
            Some(Utf16Range::NONE),
            "marked text the document does not know (winit's, after a commit \
             in a key press): none, not the view's range from its own start"
        );
    }

    #[test]
    fn a_substring_is_widened_to_whole_characters_and_clamped() {
        let document = Grid::new(EMOJI);
        // Units 2..4 are the first emoji's second half and "b".
        assert_eq!(
            substring(&document, range(2, 2)),
            Some(Substring::Text {
                text: "\u{1F600}b".to_owned(),
                actual: range(1, 3),
            })
        );
        assert_eq!(
            substring(&document, range(5, 100)),
            Some(Substring::Text {
                text: "\u{1F600}c".to_owned(),
                actual: range(4, 3),
            })
        );
        assert_eq!(
            substring(&document, range(50, 3)),
            Some(Substring::Text {
                text: String::new(),
                actual: range(7, 0),
            })
        );
        assert_eq!(substring(&document, Utf16Range::NONE), Some(Substring::Nil));
    }

    #[test]
    fn a_secure_field_gives_no_text() {
        let mut document = Grid::new("secret");
        document.secure = true;
        assert_eq!(substring(&document, range(0, 6)), Some(Substring::Nil));
    }

    #[test]
    fn the_character_under_a_point_is_a_utf16_offset_or_not_found() {
        let document = Grid::new(EMOJI);
        // Column 3 is character 3, the second emoji, UTF-16 offset 4.
        assert_eq!(character_index(&document, Point::new(30.0, 5.0)), Some(4));
        assert_eq!(
            character_index(&document, Point::new(500.0, 5.0)),
            Some(NOT_FOUND),
            "outside the text"
        );
    }

    #[test]
    fn the_first_rect_covers_the_first_line_and_says_how_far() {
        let mut document = Grid::new("0123456789abcdefghij");
        assert_eq!(
            first_rect(&document, range(8, 6)),
            Some((Rect::new(80.0, 0.0, 20.0, 20.0), range(8, 2)))
        );
        document.selection = 12..15;
        assert_eq!(
            first_rect(&document, Utf16Range::NONE),
            Some((Rect::new(20.0, 20.0, 30.0, 20.0), range(12, 3))),
            "no range: the selection's"
        );
    }

    #[test]
    fn a_replacement_range_becomes_characters() {
        let document = Grid::new(EMOJI);
        assert_eq!(replacement_chars(&document, range(3, 3)), Some(2..4));
        assert_eq!(replacement_chars(&document, Utf16Range::NONE), None);
    }

    #[test]
    fn a_ranges_box_takes_in_its_lines_and_says_when_it_was_cut() {
        let mut document = Grid::new(&"x".repeat(40));
        // Lines of 10 characters, 20 tall, 100 by 60 visible.
        assert_eq!(
            range_rect(&document, 3..5),
            Some((Rect::new(30.0, 0.0, 20.0, 20.0), false))
        );
        assert_eq!(
            range_rect(&document, 5..25),
            Some((Rect::new(0.0, 0.0, 100.0, 60.0), false)),
            "three lines, the first from its middle"
        );
        assert_eq!(
            range_rect(&document, 7..7),
            Some((Rect::new(70.0, 0.0, 1.0, 20.0), false)),
            "a caret"
        );
        assert_eq!(
            range_rect(&document, 25..35),
            Some((Rect::new(0.0, 40.0, 100.0, 20.0), true)),
            "the fourth line is below the visible text"
        );
        assert_eq!(
            range_rect(&document, 31..33),
            Some((Rect::new(10.0, 60.0, 20.0, 20.0), true)),
            "nothing visible: the whole box, cut off"
        );
        document.busy = true;
        assert_eq!(range_rect(&document, 3..5), None);
    }

    /// A caret at a soft line break is answered at the start of the next
    /// line, where a caret placed there is drawn; one at the end of the text
    /// stays where it is.
    #[test]
    fn a_caret_at_a_soft_line_break_is_answered_downstream() {
        let mut document = Grid::new(&"x".repeat(25));
        document.upstream = true;
        assert_eq!(
            range_rect(&document, 10..10),
            Some((Rect::new(0.0, 20.0, 1.0, 20.0), false)),
            "the start of the second line, not the end of the first"
        );
        assert_eq!(
            range_rect(&document, 5..5),
            Some((Rect::new(50.0, 0.0, 1.0, 20.0), false)),
            "inside a line: the caret"
        );
        assert_eq!(
            range_rect(&document, 25..25),
            Some((Rect::new(50.0, 40.0, 1.0, 20.0), false)),
            "at the end of the text: the caret"
        );
    }

    #[test]
    fn the_union_of_a_selection_is_clipped_to_the_visible_text() {
        // Three lines of ten, the visible text two lines and a half tall.
        let mut document = Grid::new(&"x".repeat(30));
        document.height = 50.0;
        document.selection = 5..25;
        assert_eq!(
            union_rect_in_visible_selection(&document),
            Some(Rect::new(0.0, 0.0, 100.0, 50.0)),
            "lines 0 (from column 5), 1 and 2 (to column 5), clipped at the bottom"
        );
        document.selection = 3..6;
        assert_eq!(
            union_rect_in_visible_selection(&document),
            Some(Rect::new(30.0, 0.0, 30.0, 20.0))
        );
    }

    #[test]
    fn an_empty_selection_answers_the_caret() {
        let mut document = Grid::new("abc");
        document.selection = 2..2;
        assert_eq!(
            union_rect_in_visible_selection(&document),
            Some(Rect::new(20.0, 0.0, 1.0, 20.0))
        );
        document.scroll_y = 200.0;
        assert_eq!(
            union_rect_in_visible_selection(&document),
            None,
            "a caret scrolled out of view is not visible"
        );
    }

    #[test]
    fn a_long_selection_is_walked_from_the_first_visible_line_only() {
        // 1000 lines, scrolled to line 900: three lines visible.
        let mut document = Grid::new(&"x".repeat(10_000));
        document.scroll_y = 900.0 * 20.0;
        document.selection = 0..10_000;
        assert_eq!(
            union_rect_in_visible_selection(&document),
            Some(Rect::new(0.0, 0.0, 100.0, 60.0))
        );
        assert!(
            document.lines_asked.get() <= 5,
            "asked {} lines, not the 900 above the view",
            document.lines_asked.get()
        );
    }

    #[test]
    fn writing_tools_need_a_document_that_takes_text_and_allows_them() {
        let mut document = Grid::new("abc");
        assert!(
            is_editable(),
            "never false: the view would lose its input context"
        );
        assert_eq!(
            writing_tools_behavior(Some(&document)),
            WRITING_TOOLS_LIMITED
        );
        assert_eq!(writing_tools_behavior(None), WRITING_TOOLS_NONE);
        document.writing_tools = false;
        assert_eq!(writing_tools_behavior(Some(&document)), WRITING_TOOLS_NONE);
        document.writing_tools = true;
        document.secure = true;
        assert_eq!(writing_tools_behavior(Some(&document)), WRITING_TOOLS_NONE);
        document.secure = false;
        document.editable = false;
        assert_eq!(
            writing_tools_behavior(Some(&document)),
            WRITING_TOOLS_NONE,
            "read-only text"
        );
    }

    #[test]
    fn a_busy_document_answers_nothing() {
        let mut document = Grid::new(EMOJI);
        document.busy = true;
        assert!(document.utf16_index().is_none());
        assert_eq!(selected_range(&document), None);
        assert_eq!(marked_range(&document, true), None);
        assert_eq!(
            substring(&document, range(0, 2)),
            Some(Substring::Nil),
            "busy reads as secure"
        );
        assert_eq!(character_index(&document, Point::new(5.0, 5.0)), None);
        assert_eq!(first_rect(&document, range(0, 1)), None);
        assert_eq!(union_rect_in_visible_selection(&document), None);
        assert_eq!(writing_tools_behavior(Some(&document)), WRITING_TOOLS_NONE);
    }

    #[test]
    fn rectangles_meet_and_join() {
        let a = Rect::new(0.0, 0.0, 10.0, 10.0);
        let b = Rect::new(10.0, 5.0, 10.0, 10.0);
        assert_eq!(
            intersection(a, b),
            Some(Rect::new(10.0, 5.0, 0.0, 5.0)),
            "an edge counts"
        );
        assert_eq!(intersection(a, Rect::new(11.0, 0.0, 1.0, 1.0)), None);
        assert_eq!(bounding(a, b), Rect::new(0.0, 0.0, 20.0, 15.0));
    }
}