blinc_layout 0.5.0

Blinc layout engine - Flexbox layout powered by Taffy
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
//! Inline formatting operations — mark / unmark a selection range.
//!
//! These are pure functions over `&mut RichDocument` that walk every
//! `StyledLine` covered by a selection and rewrite its `spans` so each
//! span fully inside the selection has the requested attribute toggled
//! (or set), spans straddling the selection boundary are split, and
//! adjacent spans with identical attributes are merged.
//!
//! The toggle behaviour matches every other rich editor: if *every*
//! character in the selection already has the mark, applying the mark
//! removes it; otherwise the mark is added to whatever doesn't have it.
//!
//! All ops are no-ops when the selection is empty.

use blinc_core::Color;

use crate::styled_text::{StyledLine, TextSpan};

use super::cursor::{DocPosition, Selection};
use super::document::{char_to_byte, RichDocument};

/// Which attribute an op should rewrite.
#[derive(Clone, Debug)]
pub enum Mark {
    Bold,
    Italic,
    Underline,
    Strikethrough,
    Code,
    /// Set the color (does not "toggle" — `apply_mark_to_selection`
    /// always sets it).
    Color(Color),
    /// Set or clear the link target. `Some` sets, `None` clears.
    Link(Option<String>),
}

/// Apply (or toggle) `mark` over the entire `sel` range.
///
/// For boolean marks (Bold, Italic, …) this checks whether the entire
/// selection already has the mark — if so it clears it, otherwise it
/// sets it. For `Color` and `Link` it always sets the requested value.
///
/// Returns `true` if anything changed, so the editor can decide whether
/// to push an undo entry.
pub fn apply_mark_to_selection(doc: &mut RichDocument, sel: Selection, mark: Mark) -> bool {
    let (start, end) = sel.ordered();
    if start == end {
        return false;
    }

    // Decide whether this is a "set" or "clear" pass for boolean marks.
    let action = match &mark {
        Mark::Bold | Mark::Italic | Mark::Underline | Mark::Strikethrough | Mark::Code => {
            if selection_fully_has(doc, sel, &mark) {
                MarkAction::Clear
            } else {
                MarkAction::Set
            }
        }
        Mark::Color(_) | Mark::Link(_) => MarkAction::Set,
    };

    let mut changed = false;
    for_each_line_in_selection(doc, start, end, |line, byte_start, byte_end| {
        if rewrite_line_marks(line, byte_start, byte_end, &mark, action) {
            changed = true;
        }
    });
    changed
}

#[derive(Clone, Copy)]
enum MarkAction {
    Set,
    Clear,
}

/// Test whether every span fully covered by the selection already has
/// the boolean mark `mark`. Used to decide whether the toggle should
/// set or clear.
fn selection_fully_has(doc: &RichDocument, sel: Selection, mark: &Mark) -> bool {
    let (start, end) = sel.ordered();
    let mut any = false;
    let mut all = true;
    walk_selection_chars(doc, start, end, |span| {
        any = true;
        let has = match mark {
            Mark::Bold => span.bold,
            Mark::Italic => span.italic,
            Mark::Underline => span.underline,
            Mark::Strikethrough => span.strikethrough,
            Mark::Code => span.code,
            _ => true,
        };
        if !has {
            all = false;
        }
    });
    any && all
}

/// Walk every span byte that overlaps the selection (in document order),
/// invoking `visit` once per character.
fn walk_selection_chars<F: FnMut(&TextSpan)>(
    doc: &RichDocument,
    start: DocPosition,
    end: DocPosition,
    mut visit: F,
) {
    for_each_line_in_selection_ref(doc, start, end, |line, byte_start, byte_end| {
        for span in &line.spans {
            let s = span.start.max(byte_start);
            let e = span.end.min(byte_end);
            if s >= e {
                continue;
            }
            visit(span);
        }
    });
}

/// Compute the `(byte_start, byte_end)` range to mutate inside `line`
/// for the given selection, then invoke `f`.
fn for_each_line_in_selection<F: FnMut(&mut StyledLine, usize, usize)>(
    doc: &mut RichDocument,
    start: DocPosition,
    end: DocPosition,
    mut f: F,
) {
    if start.block > end.block || (start.block == end.block && start > end) {
        return;
    }
    let block_count = doc.blocks.len();
    let mut block_idx = start.block;
    while block_idx <= end.block && block_idx < block_count {
        // Snapshot line count to avoid mutable borrow conflicts.
        let line_count = doc.blocks[block_idx].lines.len();
        let first_line = if block_idx == start.block {
            start.line
        } else {
            0
        };
        let last_line = if block_idx == end.block {
            end.line
        } else {
            line_count.saturating_sub(1)
        };
        for line_idx in first_line..=last_line.min(line_count.saturating_sub(1)) {
            // Compute the byte range for this line within the selection.
            let line_start_col = if block_idx == start.block && line_idx == start.line {
                start.col
            } else {
                0
            };
            let line_end_col = if block_idx == end.block && line_idx == end.line {
                end.col
            } else {
                doc.blocks[block_idx].lines[line_idx].text.chars().count()
            };
            let line = &mut doc.blocks[block_idx].lines[line_idx];
            let bs = char_to_byte(&line.text, line_start_col);
            let be = char_to_byte(&line.text, line_end_col);
            if bs >= be {
                continue;
            }
            f(line, bs, be);
        }
        block_idx += 1;
    }
}

/// Same as `for_each_line_in_selection` but with an immutable line
/// reference. Used by the "does the whole selection have this mark?"
/// query.
fn for_each_line_in_selection_ref<F: FnMut(&StyledLine, usize, usize)>(
    doc: &RichDocument,
    start: DocPosition,
    end: DocPosition,
    mut f: F,
) {
    if start.block > end.block || (start.block == end.block && start > end) {
        return;
    }
    let block_count = doc.blocks.len();
    let mut block_idx = start.block;
    while block_idx <= end.block && block_idx < block_count {
        let line_count = doc.blocks[block_idx].lines.len();
        let first_line = if block_idx == start.block {
            start.line
        } else {
            0
        };
        let last_line = if block_idx == end.block {
            end.line
        } else {
            line_count.saturating_sub(1)
        };
        for line_idx in first_line..=last_line.min(line_count.saturating_sub(1)) {
            let line_start_col = if block_idx == start.block && line_idx == start.line {
                start.col
            } else {
                0
            };
            let line_end_col = if block_idx == end.block && line_idx == end.line {
                end.col
            } else {
                doc.blocks[block_idx].lines[line_idx].text.chars().count()
            };
            let line = &doc.blocks[block_idx].lines[line_idx];
            let bs = char_to_byte(&line.text, line_start_col);
            let be = char_to_byte(&line.text, line_end_col);
            if bs >= be {
                continue;
            }
            f(line, bs, be);
        }
        block_idx += 1;
    }
}

/// Rewrite the spans of `line` so that all bytes in `[start, end)` carry
/// `mark` set or cleared per `action`. Spans straddling the boundaries
/// are split; adjacent spans with identical attributes are merged.
///
/// Returns `true` if any span attribute actually changed.
fn rewrite_line_marks(
    line: &mut StyledLine,
    start: usize,
    end: usize,
    mark: &Mark,
    action: MarkAction,
) -> bool {
    if start >= end || line.spans.is_empty() {
        return false;
    }
    let mut new_spans: Vec<TextSpan> = Vec::with_capacity(line.spans.len() + 4);
    let mut changed = false;
    for span in line.spans.drain(..) {
        // Split into up to three pieces: [span.start..start), [start..end),
        // [end..span.end), keeping only the slices that exist.
        let s = span.start;
        let e = span.end;
        if e <= start || s >= end {
            // Entirely outside the mark range — keep as-is.
            new_spans.push(span);
            continue;
        }
        // Left piece (untouched).
        if s < start {
            let mut left = span.clone();
            left.end = start;
            new_spans.push(left);
        }
        // Middle piece (the part covered by the mark range).
        let mid_start = s.max(start);
        let mid_end = e.min(end);
        let mut mid = span.clone();
        mid.start = mid_start;
        mid.end = mid_end;
        if apply_mark_to_span(&mut mid, mark, action) {
            changed = true;
        }
        new_spans.push(mid);
        // Right piece (untouched).
        if e > end {
            let mut right = span.clone();
            right.start = end;
            new_spans.push(right);
        }
    }

    // Merge adjacent spans that share identical attributes.
    let merged = merge_adjacent(new_spans);
    line.spans = merged;
    changed
}

/// Apply `mark` to a single span according to `action`. Returns `true`
/// if any field actually changed.
fn apply_mark_to_span(span: &mut TextSpan, mark: &Mark, action: MarkAction) -> bool {
    let set = matches!(action, MarkAction::Set);
    match mark {
        Mark::Bold => {
            if span.bold != set {
                span.bold = set;
                return true;
            }
        }
        Mark::Italic => {
            if span.italic != set {
                span.italic = set;
                return true;
            }
        }
        Mark::Underline => {
            if span.underline != set {
                span.underline = set;
                return true;
            }
        }
        Mark::Strikethrough => {
            if span.strikethrough != set {
                span.strikethrough = set;
                return true;
            }
        }
        Mark::Code => {
            if span.code != set {
                span.code = set;
                return true;
            }
        }
        Mark::Color(c) => {
            if !color_eq(span.color, *c) {
                span.color = *c;
                return true;
            }
        }
        Mark::Link(url) => {
            if span.link_url != *url {
                span.link_url = url.clone();
                // Mirror standard editor behaviour: links carry an
                // underline. Clearing a link doesn't strip the
                // underline (the user can do that explicitly).
                if url.is_some() {
                    span.underline = true;
                }
                return true;
            }
        }
    }
    false
}

fn color_eq(a: Color, b: Color) -> bool {
    (a.r - b.r).abs() < 1e-3
        && (a.g - b.g).abs() < 1e-3
        && (a.b - b.b).abs() < 1e-3
        && (a.a - b.a).abs() < 1e-3
}

/// Merge consecutive spans with identical attributes into single runs.
fn merge_adjacent(spans: Vec<TextSpan>) -> Vec<TextSpan> {
    let mut out: Vec<TextSpan> = Vec::with_capacity(spans.len());
    for span in spans {
        if span.start >= span.end {
            continue;
        }
        if let Some(last) = out.last_mut() {
            if last.end == span.start && spans_format_match(last, &span) {
                last.end = span.end;
                continue;
            }
        }
        out.push(span);
    }
    out
}

fn spans_format_match(a: &TextSpan, b: &TextSpan) -> bool {
    a.bold == b.bold
        && a.italic == b.italic
        && a.underline == b.underline
        && a.strikethrough == b.strikethrough
        && a.code == b.code
        && color_eq(a.color, b.color)
        && a.link_url == b.link_url
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::widgets::rich_text_editor::document::Block;
    use blinc_core::Color;

    fn doc_one_para(text: &str) -> RichDocument {
        RichDocument::from_blocks(vec![Block::paragraph(text, Color::WHITE)])
    }

    fn sel(b1: usize, l1: usize, c1: usize, b2: usize, l2: usize, c2: usize) -> Selection {
        Selection {
            anchor: DocPosition::new(b1, l1, c1),
            head: DocPosition::new(b2, l2, c2),
        }
    }

    #[test]
    fn empty_selection_is_no_op() {
        let mut d = doc_one_para("hello");
        let pos = DocPosition::new(0, 0, 2);
        let changed = apply_mark_to_selection(
            &mut d,
            Selection {
                anchor: pos,
                head: pos,
            },
            Mark::Bold,
        );
        assert!(!changed);
    }

    #[test]
    fn bold_first_word() {
        let mut d = doc_one_para("hello world");
        apply_mark_to_selection(&mut d, sel(0, 0, 0, 0, 0, 5), Mark::Bold);
        let line = &d.blocks[0].lines[0];
        // Expect a bold span over [0, 5) and a non-bold span over [5, 11).
        let bold = line.spans.iter().find(|s| s.bold).unwrap();
        assert_eq!(bold.start, 0);
        assert_eq!(bold.end, 5);
        assert!(line
            .spans
            .iter()
            .any(|s| !s.bold && s.start == 5 && s.end == 11));
    }

    #[test]
    fn toggle_bold_clears_when_already_bold() {
        let mut d = doc_one_para("hello");
        apply_mark_to_selection(&mut d, sel(0, 0, 0, 0, 0, 5), Mark::Bold);
        // Now the whole word is bold; toggling it should clear it.
        let changed = apply_mark_to_selection(&mut d, sel(0, 0, 0, 0, 0, 5), Mark::Bold);
        assert!(changed);
        assert!(d.blocks[0].lines[0].spans.iter().all(|s| !s.bold));
    }

    #[test]
    fn italic_middle_of_word_splits_spans() {
        let mut d = doc_one_para("abcdef");
        apply_mark_to_selection(&mut d, sel(0, 0, 2, 0, 0, 4), Mark::Italic);
        let spans = &d.blocks[0].lines[0].spans;
        // Expect three spans: [0..2) plain, [2..4) italic, [4..6) plain.
        let italic_count = spans.iter().filter(|s| s.italic).count();
        assert_eq!(italic_count, 1);
        let i = spans.iter().find(|s| s.italic).unwrap();
        assert_eq!(i.start, 2);
        assert_eq!(i.end, 4);
    }

    #[test]
    fn merge_adjacent_after_toggle() {
        let mut d = doc_one_para("ab");
        // Bold each char individually.
        apply_mark_to_selection(&mut d, sel(0, 0, 0, 0, 0, 1), Mark::Bold);
        apply_mark_to_selection(&mut d, sel(0, 0, 1, 0, 0, 2), Mark::Bold);
        // Spans should have merged to a single bold span over [0, 2).
        let spans = &d.blocks[0].lines[0].spans;
        let bold_runs: Vec<_> = spans.iter().filter(|s| s.bold).collect();
        assert_eq!(bold_runs.len(), 1);
        assert_eq!(bold_runs[0].start, 0);
        assert_eq!(bold_runs[0].end, 2);
    }

    #[test]
    fn color_change_overrides_existing_color() {
        let mut d = doc_one_para("hello");
        let red = Color::rgba(1.0, 0.0, 0.0, 1.0);
        apply_mark_to_selection(&mut d, sel(0, 0, 0, 0, 0, 5), Mark::Color(red));
        let line = &d.blocks[0].lines[0];
        for span in &line.spans {
            if span.start < 5 {
                assert!(
                    color_eq(span.color, red),
                    "expected red, got {:?}",
                    span.color
                );
            }
        }
    }

    #[test]
    fn link_sets_url_and_underline() {
        let mut d = doc_one_para("click");
        apply_mark_to_selection(
            &mut d,
            sel(0, 0, 0, 0, 0, 5),
            Mark::Link(Some("https://example.com".to_string())),
        );
        let line = &d.blocks[0].lines[0];
        let linked = line
            .spans
            .iter()
            .find(|s| s.link_url.as_deref() == Some("https://example.com"))
            .expect("link span exists");
        assert!(linked.underline);
        assert_eq!(linked.start, 0);
        assert_eq!(linked.end, 5);
    }

    #[test]
    fn link_clear_removes_url() {
        let mut d = doc_one_para("click");
        apply_mark_to_selection(
            &mut d,
            sel(0, 0, 0, 0, 0, 5),
            Mark::Link(Some("https://example.com".to_string())),
        );
        apply_mark_to_selection(&mut d, sel(0, 0, 0, 0, 0, 5), Mark::Link(None));
        let line = &d.blocks[0].lines[0];
        assert!(line.spans.iter().all(|s| s.link_url.is_none()));
    }

    #[test]
    fn multi_line_selection_marks_all_lines() {
        // Two-block doc, select across the boundary.
        let mut d = RichDocument::from_blocks(vec![
            Block::paragraph("foo bar", Color::WHITE),
            Block::paragraph("baz qux", Color::WHITE),
        ]);
        apply_mark_to_selection(&mut d, sel(0, 0, 4, 1, 0, 3), Mark::Bold);
        // First block's [4..7) should be bold.
        let line0 = &d.blocks[0].lines[0];
        let bold0 = line0
            .spans
            .iter()
            .find(|s| s.bold && s.start == 4)
            .expect("bold run on first line");
        assert_eq!(bold0.end, 7);
        // Second block's [0..3) should be bold.
        let line1 = &d.blocks[1].lines[0];
        let bold1 = line1
            .spans
            .iter()
            .find(|s| s.bold && s.start == 0)
            .expect("bold run on second line");
        assert_eq!(bold1.end, 3);
    }
}