rgpui 1.3.0

GUI UI framework
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
//! 多光标多选区:主选区之外可再加若干选区,同写同删。
//!
//! 模型:`selected_range` 仍是主光标(既有代码零改动),`extra_selections`
//! 存其余光标(有序、互不重叠、不与主光标重叠,由 [`InputState::normalize_extras`]
//! 维护)。渲染、括号匹配、当前行等跟随主光标;编辑(键入/退格/删除/剪切/
//! 复制/粘贴/回车)扇出到所有光标,自后向前应用,偏移不失效。
//!
//! 范围(v1,文档注明即契约):
//! - 新增光标:`Alt+点击`、上下加光标动作;纯键盘移动/鼠标左键/Esc/全选坍缩为单光标,
//!   `Shift+方向` 只扩展主选区、其余保留;行操作先清额外光标再执行;
//! - 扇出编辑成组进撤销栈(一次撤销整体回退);
//! - 键入时自动闭合不参与(按原文插入);粘贴板多行不按光标分发(每处贴全文);
//! - 掩码/单行输入建不了额外光标。

use std::ops::Range;

use crate::{ClipboardItem, Context, Window};

use super::super::InputState;
use super::super::rope_ext::RopeExt as _;

impl InputState {
    /// 是否存在额外光标。
    pub fn has_multiple_cursors(&self) -> bool {
        !self.extra_selections.is_empty()
    }

    /// 额外光标(主光标 `selected_range` 之外)。
    pub fn extra_selections(&self) -> &[super::Selection] {
        &self.extra_selections
    }

    /// 清除额外光标,只留主光标。
    pub fn clear_extra_cursors(&mut self, cx: &mut Context<Self>) {
        if self.extra_selections.is_empty() {
            return;
        }
        self.extra_selections.clear();
        cx.notify();
    }

    /// 在主光标上方同列加一个光标(`AddCursorAbove`)。
    pub(crate) fn add_cursor_above(
        &mut self,
        _: &super::AddCursorAbove,
        _: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if !self.multicursor_allowed(cx) {
            return;
        }
        self.add_cursors(-1);
        cx.notify();
    }

    /// 在主光标下方同列加一个光标(`AddCursorBelow`)。
    pub(crate) fn add_cursor_below(
        &mut self,
        _: &super::AddCursorBelow,
        _: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if !self.multicursor_allowed(cx) {
            return;
        }
        self.add_cursors(1);
        cx.notify();
    }

    /// 多光标守卫:仅多行可编辑非掩码输入。
    fn multicursor_allowed(&self, cx: &mut Context<Self>) -> bool {
        if !self.mode.is_multi_line() || self.disabled || self.read_only || self.masked {
            cx.propagate();
            return false;
        }
        true
    }

    /// 给每个已有光标在上下 `delta_rows` 行的同列加一个光标。
    fn add_cursors(&mut self, delta_rows: isize) {
        let last_row = self.core.text.lines_len().saturating_sub(1);
        let mut offsets: Vec<usize> = std::iter::once(self.core.selected_range.end)
            .chain(self.extra_selections.iter().map(|sel| sel.end))
            .collect();
        for offset in offsets.drain(..) {
            let point = self.core.text.offset_to_point(offset);
            let column = offset.saturating_sub(self.core.text.line_start_offset(point.row));
            let target = point.row.saturating_add_signed(delta_rows).min(last_row);
            if target == point.row {
                continue;
            }
            let line_start = self.core.text.line_start_offset(target);
            let line_len = self.core.text.line_end_offset(target) - line_start;
            let at = self
                .core
                .text
                .floor_char_boundary(line_start + column.min(line_len));
            self.extra_selections.push(super::Selection::new(at, at));
        }
        self.normalize_extras();
    }

    /// 整理额外光标:排序、合并重叠、去掉与主光标重叠的(主光标保留)。
    pub(crate) fn normalize_extras(&mut self) {
        let primary: Range<usize> = self.core.selected_range.into();
        self.extra_selections.sort_by_key(|sel| sel.start);
        let mut merged: Vec<super::Selection> = Vec::new();
        for sel in self.extra_selections.drain(..) {
            // 与主光标重叠/相接:主光标保留,丢弃。
            if sel.start <= primary.end && primary.start <= sel.end {
                continue;
            }
            if let Some(last) = merged.last_mut() {
                if sel.start <= last.end {
                    last.end = last.end.max(sel.end);
                    continue;
                }
            }
            merged.push(sel);
        }
        self.extra_selections = merged;
    }

    /// 所有光标范围(含主光标),按起始偏移降序(自后向前编辑用)。
    fn cursors_back_to_front(&self) -> Vec<Range<usize>> {
        let mut all: Vec<Range<usize>> = std::iter::once(self.core.selected_range.into())
            .chain(self.extra_selections.iter().map(|sel| (*sel).into()))
            .collect();
        all.sort_by_key(|range| std::cmp::Reverse(range.start));
        all
    }

    /// 自后向前应用一批互不重叠的替换,并跟随一组光标位置。
    ///
    /// 返回与 `points` 同顺序的新位置。规则:编辑区之后的光标按增量平移,
    /// 落在编辑区内的收敛到新区末尾,恰在插入点的跟到插入末尾。
    fn apply_edits_and_track(
        &mut self,
        edits: &mut Vec<(Range<usize>, String)>,
        points: &mut Vec<usize>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        edits.sort_by_key(|(range, _)| std::cmp::Reverse(range.start));
        let before = self.core.history.undos().len();
        for (range, text) in edits.iter() {
            let old_len = range.len();
            let new_len = text.len();
            self.replace_text_in_range_silent(Some(self.range_to_utf16(range)), text, window, cx);
            for point in points.iter_mut() {
                if *point >= range.end {
                    *point = point.saturating_add(new_len).saturating_sub(old_len);
                } else if *point > range.start {
                    *point = range.start + new_len;
                } else if *point == range.start && new_len > 0 {
                    *point = range.start + new_len;
                }
            }
        }
        self.regroup_history(before);
    }

    /// 收敛多光标编辑结果:主光标身份保持,extras 保持原顺序。
    fn collapse_cursors(&mut self, mut points: Vec<usize>, cx: &mut Context<Self>) {
        let mut points_iter = points.drain(..);
        let primary = points_iter.next().unwrap_or(0);
        self.core.selected_range = super::Selection::new(primary, primary);
        self.extra_selections = points_iter
            .map(|at| super::Selection::new(at, at))
            .collect();
        self.core.selection_reversed = false;
        cx.notify();
    }

    /// 在所有光标处插入同一文本(键入/粘贴扇出),每处光标落插入末尾。
    pub(crate) fn multi_insert(&mut self, text: &str, window: &mut Window, cx: &mut Context<Self>) {
        if text.is_empty() {
            return;
        }
        let mut edits: Vec<(Range<usize>, String)> = self
            .cursors_back_to_front()
            .into_iter()
            .map(|range| (range, text.to_string()))
            .collect();
        let mut points: Vec<usize> = std::iter::once(self.core.selected_range.start)
            .chain(self.extra_selections.iter().map(|sel| sel.start))
            .collect();
        self.apply_edits_and_track(&mut edits, &mut points, window, cx);
        self.collapse_cursors(points, cx);
    }

    /// 在所有光标处删除(退格/删除扇出):有选区删选区,否则删一个边界单位。
    pub(crate) fn multi_delete(
        &mut self,
        backward: bool,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let cursors = self.cursors_back_to_front();
        // 先算出所有待删范围(边界计算依赖原文本,一次算完再动手)。
        let mut ranges = Vec::new();
        for range in &cursors {
            let delete = if !range.is_empty() {
                Some(range.clone())
            } else if backward {
                let start = self.previous_boundary(range.start);
                (start < range.start).then(|| start..range.start)
            } else {
                let end = self.next_boundary(range.end);
                (end > range.end).then(|| range.end..end)
            };
            if let Some(delete) = delete {
                ranges.push(delete);
            }
        }
        if ranges.is_empty() {
            return;
        }
        let mut edits: Vec<(Range<usize>, String)> = ranges
            .into_iter()
            .map(|range| (range, String::new()))
            .collect();
        let mut points: Vec<usize> = std::iter::once(self.core.selected_range.start)
            .chain(self.extra_selections.iter().map(|sel| sel.start))
            .collect();
        self.apply_edits_and_track(&mut edits, &mut points, window, cx);
        self.collapse_cursors(points, cx);
    }

    /// 多光标复制:各选区文本换行拼接;全塌缩时复制各光标所在整行。
    pub(crate) fn copy_cursors(&mut self, cx: &mut Context<Self>) {
        let mut selections = self.cursors_back_to_front();
        selections.sort_by_key(|range| range.start);
        let all_collapsed = selections.iter().all(|range| range.is_empty());
        let parts: Vec<String> = if all_collapsed {
            selections
                .iter()
                .map(|range| {
                    let row = self.core.text.offset_to_point(range.start).row;
                    self.core
                        .text
                        .slice(
                            self.core.text.line_start_offset(row)
                                ..self.core.text.line_end_offset(row),
                        )
                        .to_string()
                })
                .collect()
        } else {
            selections
                .iter()
                .map(|range| self.core.text.slice(range.clone()).to_string())
                .collect()
        };
        cx.write_to_clipboard(ClipboardItem::new_string(parts.join("\n")));
    }

    /// 多光标剪切:复制后删除所有光标范围(空光标删整行)。
    pub(crate) fn cut_cursors(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        self.copy_cursors(cx);
        let mut selections = self.cursors_back_to_front();
        selections.sort_by_key(|range| range.start);
        let all_collapsed = selections.iter().all(|range| range.is_empty());
        let mut ranges: Vec<Range<usize>> = if all_collapsed {
            selections
                .iter()
                .map(|range| {
                    let row = self.core.text.offset_to_point(range.start).row;
                    self.block_range(row, row)
                })
                .collect()
        } else {
            selections
        };
        // 自后向前删。
        ranges.sort_by_key(|range| std::cmp::Reverse(range.start));
        let mut edits: Vec<(Range<usize>, String)> = ranges
            .into_iter()
            .map(|range| (range, String::new()))
            .collect();
        let mut points: Vec<usize> = std::iter::once(self.core.selected_range.start)
            .chain(self.extra_selections.iter().map(|sel| sel.start))
            .collect();
        self.apply_edits_and_track(&mut edits, &mut points, window, cx);
        // 光标落删除起点(钳制到新文本内)。
        let len = self.core.text.len();
        let points: Vec<usize> = points.into_iter().map(|at| at.min(len)).collect();
        self.collapse_cursors(points, cx);
    }

    /// 多光标回车:每处换行并延续该行缩进(电缩进拆行不参与,落插入末尾)。
    pub(crate) fn enter_cursors(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        // 先算好每处的插入文本(依赖原文本,一次算完再动手)。
        let mut edits = Vec::new();
        for range in self.cursors_back_to_front() {
            let start = range.start.min(self.core.text.len());
            let line_start = self
                .core
                .text
                .line_start_offset(self.core.text.offset_to_point(start).row);
            let indent: String = self
                .core
                .text
                .slice(line_start..start)
                .chars()
                .take_while(|c| *c == ' ' || *c == '\t')
                .collect();
            edits.push((range, format!("\n{indent}")));
        }
        let mut points: Vec<usize> = std::iter::once(self.core.selected_range.start)
            .chain(self.extra_selections.iter().map(|sel| sel.start))
            .collect();
        self.apply_edits_and_track(&mut edits, &mut points, window, cx);
        self.pause_blink_cursor(cx);
        self.collapse_cursors(points, cx);
    }

    /// 把 `before` 之后新增的历史条目并为同一版本(一次撤销整体回退)。
    fn regroup_history(&mut self, before: usize) {
        let pushed = self.core.history.undos().len().saturating_sub(before);
        self.core.history.regroup_last(pushed);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::AppContext as _;
    use crate::input_ui::{AddCursorBelow, Undo};
    use crate::{Entity, Window};

    /// 持有多行输入框状态的测试宿主视图。
    struct Probe {
        state: Entity<InputState>,
    }

    impl crate::Render for Probe {
        fn render(
            &mut self,
            _window: &mut Window,
            _cx: &mut Context<Self>,
        ) -> impl crate::IntoElement {
            crate::div()
        }
    }

    /// 渲染输入框的宿主视图(渲染冒烟用)。
    struct InputProbe {
        state: Entity<InputState>,
    }

    impl crate::Render for InputProbe {
        fn render(
            &mut self,
            window: &mut Window,
            cx: &mut Context<Self>,
        ) -> impl crate::IntoElement {
            use crate::{IntoElement as _, RenderOnce as _};
            crate::input_ui::Input::new(&self.state)
                .render(window, cx)
                .into_element()
        }
    }

    #[rgpui::test]
    fn add_cursors_below_and_type_everywhere(cx: &mut crate::TestAppContext) {
        cx.update(crate::input_ui::init);
        cx.update(crate::theme::init);
        let (probe, cx) = cx.add_window_view(|window, cx| {
            let state = cx.new(|cx| InputState::new(window, cx).multi_line(true));
            // `set_value` 清空历史,避免 1 秒分组窗口把建仓文本也并入撤销组。
            state.update(cx, |state, cx| state.set_value("a\nb\nc", window, cx));
            Probe { state }
        });
        let state = probe.read_with(cx, |probe, _| probe.state.clone());
        cx.update(|_, cx| {
            state.update(cx, |state, cx| state.set_selected_range(0..0, cx));
        });
        // 加两次:第 1、2 行各添一个,主光标不动。
        cx.update(|window, cx| {
            state.update(cx, |state, cx| {
                state.add_cursor_below(&AddCursorBelow, window, cx)
            });
            state.update(cx, |state, cx| {
                state.add_cursor_below(&AddCursorBelow, window, cx)
            });
        });
        assert!(state.read_with(cx, |state, _| state.has_multiple_cursors()));
        let extras: Vec<(usize, usize)> = state.read_with(cx, |state, _| {
            state
                .extra_selections()
                .iter()
                .map(|sel| (sel.start, sel.end))
                .collect()
        });
        assert_eq!(extras, vec![(2, 2), (4, 4)]);
        // 键入扇出到三处。
        cx.update(|window, cx| {
            state.update(cx, |state, cx| {
                crate::EntityInputHandler::replace_text_in_range(state, None, "x", window, cx);
            });
        });
        assert_eq!(
            state.read_with(cx, |state, _| state.value().to_string()),
            "xa\nxb\nxc".to_string()
        );
        // 一次撤销整体回退(扇出编辑已并组)。
        cx.update(|window, cx| {
            state.update(cx, |state, cx| state.undo(&Undo, window, cx));
        });
        assert_eq!(
            state.read_with(cx, |state, _| state.value().to_string()),
            "a\nb\nc".to_string()
        );
    }

    #[rgpui::test]
    fn escape_and_navigation_collapse_to_single(cx: &mut crate::TestAppContext) {
        cx.update(crate::input_ui::init);
        cx.update(crate::theme::init);
        let (probe, cx) = cx.add_window_view(|window, cx| {
            let state = cx.new(|cx| InputState::new(window, cx).multi_line(true));
            state.update(cx, |state, cx| state.replace("a\nb\nc", window, cx));
            Probe { state }
        });
        let state = probe.read_with(cx, |probe, _| probe.state.clone());
        cx.update(|_, cx| {
            state.update(cx, |state, cx| state.set_selected_range(0..0, cx));
        });
        cx.update(|window, cx| {
            state.update(cx, |state, cx| {
                state.add_cursor_below(&AddCursorBelow, window, cx)
            });
        });
        assert!(state.read_with(cx, |state, _| state.has_multiple_cursors()));
        // Esc 先坍缩多光标。
        cx.update(|window, cx| {
            state.update(cx, |state, cx| {
                state.escape(&crate::input_ui::Escape, window, cx)
            });
        });
        assert!(!state.read_with(cx, |state, _| state.has_multiple_cursors()));
        // 再加一个,方向键移动坍缩。
        cx.update(|window, cx| {
            state.update(cx, |state, cx| {
                state.add_cursor_below(&AddCursorBelow, window, cx)
            });
        });
        cx.update(|window, cx| {
            state.update(cx, |state, cx| {
                state.right(&crate::input_ui::MoveRight, window, cx)
            });
        });
        assert!(!state.read_with(cx, |state, _| state.has_multiple_cursors()));
    }

    #[rgpui::test]
    fn extra_cursors_render_without_panic(cx: &mut crate::TestAppContext) {
        cx.update(crate::input_ui::init);
        cx.update(crate::theme::init);
        let (probe, cx) = cx.add_window_view(|window, cx| InputProbe {
            state: cx.new(|cx| {
                let mut state = InputState::new(window, cx).multi_line(true);
                state.replace("a\nb\nc", window, cx);
                state
            }),
        });
        let state = probe.read_with(cx, |probe, _| probe.state.clone());
        cx.update(|_, cx| {
            state.update(cx, |state, cx| state.set_selected_range(0..3, cx));
        });
        cx.update(|window, cx| {
            state.update(cx, |state, cx| {
                state.add_cursor_below(&AddCursorBelow, window, cx)
            });
        });
        // 主选区 + 额外选区 + 额外光标一次 paint,不 panic 即过。
        cx.update(|window, cx| {
            _ = window.draw(cx);
        });
        assert!(state.read_with(cx, |state, _| state.has_multiple_cursors()));
    }
}