gpui_material_foundation/test/
input.rs1use std::ops::Range;
2
3use gpui::{AnyWindowHandle, AppContext, InputHandler, TestAppContext, UTF16Selection};
4
5use super::TestWindowExt as _;
6
7pub struct TestInput {
11 handler: Box<dyn InputHandler>,
12 window: AnyWindowHandle,
13}
14
15impl TestInput {
16 pub fn new(handler: impl InputHandler + 'static, window: impl Into<AnyWindowHandle>) -> Self {
18 Self {
19 handler: Box::new(handler),
20 window: window.into(),
21 }
22 }
23
24 pub fn commit_text(&mut self, text: &str, cx: &mut TestAppContext) {
26 cx.update_window(self.window, |_, window, cx| {
27 self.handler.replace_text_in_range(None, text, window, cx);
28 window.render_frame(cx);
29 })
30 .expect("test window closed while committing text");
31 }
32
33 pub fn compose_text(
36 &mut self,
37 replacement: Option<Range<usize>>,
38 text: &str,
39 selection: Option<Range<usize>>,
40 cx: &mut TestAppContext,
41 ) {
42 cx.update_window(self.window, |_, window, cx| {
43 self.handler
44 .replace_and_mark_text_in_range(replacement, text, selection, window, cx);
45 window.render_frame(cx);
46 })
47 .expect("test window closed while composing text");
48 }
49
50 pub fn marked_text_range(&mut self, cx: &mut TestAppContext) -> Option<Range<usize>> {
52 cx.update_window(self.window, |_, window, cx| {
53 self.handler.marked_text_range(window, cx)
54 })
55 .expect("test window closed while querying marked range")
56 }
57
58 pub fn selected_text_range(&mut self, cx: &mut TestAppContext) -> Option<UTF16Selection> {
60 cx.update_window(self.window, |_, window, cx| {
61 self.handler.selected_text_range(false, window, cx)
62 })
63 .expect("test window closed while querying selected range")
64 }
65
66 pub fn unmark_text(&mut self, cx: &mut TestAppContext) {
68 cx.update_window(self.window, |_, window, cx| {
69 self.handler.unmark_text(window, cx);
70 window.render_frame(cx);
71 })
72 .expect("test window closed while unmarking text");
73 }
74}