BlueGit

ict rusty-teams

Files

rusty-teams / vendor / gpui-base / src / text / inline_virtual_tests.rs

inline_virtual_tests.rs

eef6c0a4 History
inline_virtual_tests.rs 6.1 KiB Download Raw
1use std::sync::{
2 Arc,
3 atomic::{AtomicUsize, Ordering},
4};
5
6use gpui::{
7 AppContext as _, Context, Entity, IntoElement, Modifiers, MouseButton, ParentElement as _,
8 Pixels, Render, Styled as _, TestAppContext, VisualTestContext, Window, div, point, px, rems,
9};
10
11use super::{InlineElement, MarkdownNode, SelectionFormat, TextView, TextViewState};
12
13const BLOCKS: usize = 24;
14
15struct VirtualInlineRoot {
16 view: Entity<TextViewState>,
17 prepared_size: Arc<AtomicUsize>,
18 font_scale: f32,
19 source_format: bool,
20}
21
22impl Render for VirtualInlineRoot {
23 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
24 let prepared = self.prepared_size.clone();
25 div().w(px(400.)).h(px(200.)).overflow_hidden().text_size(rems(self.font_scale))
26 .child(crate::TextSelectionLayer)
27 .child(TextView::new(&self.view).scrollable(true)
28 .selection_format(if self.source_format { SelectionFormat::Source } else { SelectionFormat::Plain })
29
30 .plugin(crate::text::markdown_ext::TestInlinePlugin::new("math").parse_with(|node, _| {
31 let markdown::mdast::Node::InlineMath(math) = node else { return None };
32 Some(MarkdownNode::new("math", ()).text(format!("{}²", math.value)))
33 }).render_with(move |_, context, _, _| {
34 let extent = context.font_size() * (prepared.load(Ordering::Relaxed) as f32 / 16.);
35 let image = Arc::new(gpui::Image::from_bytes(gpui::ImageFormat::Svg,
36 b"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"40\" height=\"40\"><path d=\"M0 0L40 40\" stroke=\"black\"/></svg>".to_vec()));
37 Some(InlineElement::new(gpui::img(image).w(extent).h(extent)).with_baseline(extent * 0.75))
38 })))
39 }
40}
41
42fn document() -> String {
43 (0..BLOCKS)
44 .map(|i| format!("第{i}段 $x$ $y$ English"))
45 .collect::<Vec<_>>()
46 .join("\n\n")
47}
48
49fn settle(cx: &mut VisualTestContext) {
50 cx.run_until_parked();
51 cx.update(|window, cx| window.draw(cx).clear(cx));
52 cx.run_until_parked();
53}
54
55fn total_height(root: &Entity<VirtualInlineRoot>, cx: &mut VisualTestContext) -> Pixels {
56 root.read_with(cx, |root, cx| {
57 let list = root.view.read(cx).list_state();
58 assert_eq!(list.item_count(), BLOCKS);
59 list.max_offset_for_scrollbar().y + list.viewport_bounds().size.height
60 })
61}
62
63#[gpui::test]
64fn inline_virtual_resource_growth_remeasures_all_blocks_and_preserves_copy(
65 cx: &mut TestAppContext,
66) {
67 cx.update(crate::init);
68 let prepared = Arc::new(AtomicUsize::new(16));
69 let (root, cx) = cx.add_window_view(|_, cx| VirtualInlineRoot {
70 view: cx.new(|cx| TextViewState::markdown(&document(), cx)),
71 prepared_size: prepared.clone(),
72 font_scale: 1.,
73 source_format: false,
74 });
75 settle(cx);
76 let before = total_height(&root, cx);
77 let bounds = root.read_with(cx, |root, cx| {
78 root.view.read(cx).selection_adapter.text_bounds()
79 });
80 let first = bounds.first().unwrap();
81 let last = bounds.last().unwrap();
82 let start = point(first.left() + px(0.1), first.top() + first.size.height / 2.);
83 let end = point(last.right() - px(0.1), last.top() + last.size.height / 2.);
84 cx.simulate_mouse_down(start, MouseButton::Left, Modifiers::default());
85 settle(cx);
86 cx.simulate_mouse_move(end, MouseButton::Left, Modifiers::default());
87 settle(cx);
88 root.update(cx, |root, cx| {
89 root.view.read(cx).list_state().scroll_to_end();
90 cx.notify();
91 });
92 settle(cx);
93 let bounds = root.read_with(cx, |root, cx| {
94 root.view.read(cx).selection_adapter.text_bounds()
95 });
96 let last = bounds.last().unwrap();
97 let end = point(last.right() - px(0.1), last.top() + last.size.height / 2.);
98 cx.simulate_mouse_move(end, MouseButton::Left, Modifiers::default());
99 settle(cx);
100 cx.simulate_mouse_up(end, MouseButton::Left, Modifiers::default());
101 settle(cx);
102 let copy_before = root.read_with(cx, |root, cx| {
103 root.view.read(cx).selected_text_in(Some(0..=BLOCKS - 1))
104 });
105 assert!(
106 copy_before.contains("第12段 x² y² English"),
107 "offscreen covered blocks must use their logical text"
108 );
109 prepared.store(96, Ordering::Relaxed);
110 root.update(cx, |root, cx| {
111 root.view
112 .update(cx, |state, cx| state.invalidate_inline_layout(cx))
113 });
114 settle(cx);
115 let after = total_height(&root, cx);
116 assert!(after > before * 2., "before={before:?}, after={after:?}");
117 assert_eq!(
118 root.read_with(cx, |root, cx| root
119 .view
120 .read(cx)
121 .selected_text_in(Some(0..=BLOCKS - 1))),
122 copy_before
123 );
124 root.update(cx, |root, cx| {
125 root.source_format = true;
126 cx.notify();
127 });
128 settle(cx);
129 let source = root.read_with(cx, |root, cx| {
130 root.view.read(cx).selected_text_in(Some(0..=BLOCKS - 1))
131 });
132 assert!(source.contains("第12段 $x$ $y$ English"));
133}
134
135#[gpui::test]
136fn inline_virtual_font_and_rem_zoom_remeasure_offscreen_blocks(cx: &mut TestAppContext) {
137 cx.update(crate::init);
138 let (root, cx) = cx.add_window_view(|_, cx| VirtualInlineRoot {
139 view: cx.new(|cx| TextViewState::markdown(&document(), cx)),
140 prepared_size: Arc::new(AtomicUsize::new(40)),
141 font_scale: 1.,
142 source_format: false,
143 });
144 settle(cx);
145 let normal = total_height(&root, cx);
146 let mut zoomed = normal;
147 for scale in [1.5, 2.] {
148 root.update(cx, |root, cx| {
149 root.font_scale = scale;
150 cx.notify();
151 });
152 settle(cx);
153 zoomed = total_height(&root, cx);
154 assert!(
155 zoomed > normal * (scale * 0.85),
156 "scale={scale}, normal={normal:?}, zoomed={zoomed:?}"
157 );
158 }
159 cx.update(|window, cx| {
160 window.set_rem_size(window.rem_size() * 1.5);
161 window.refresh();
162 cx.notify(root.entity_id());
163 });
164 settle(cx);
165 let rem_zoomed = total_height(&root, cx);
166 assert!(
167 rem_zoomed > zoomed * 1.3,
168 "zoomed={zoomed:?}, rem_zoomed={rem_zoomed:?}"
169 );
170}