Skip to main content

gpui_material_foundation/test/
input.rs

1use std::ops::Range;
2
3use gpui::{AnyWindowHandle, AppContext, InputHandler, TestAppContext, UTF16Selection};
4
5use super::TestWindowExt as _;
6
7/// Bridges an explicit GPUI [`InputHandler`] to a test window so tests can
8/// exercise UTF-16 composition (`replace_and_mark_text_in_range`), preedit
9/// cancellation, and text commits without relying on an OS IME.
10pub struct TestInput {
11    handler: Box<dyn InputHandler>,
12    window: AnyWindowHandle,
13}
14
15impl TestInput {
16    /// Creates a composition bridge bound to `handler` and `window`.
17    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    /// Commits `text` through [`InputHandler::replace_text_in_range`] and completes a frame.
25    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    /// Updates marked/preedit text through [`InputHandler::replace_and_mark_text_in_range`]
34    /// using UTF-16 code-unit ranges and completes a frame.
35    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    /// Returns the document-relative UTF-16 marked range, if any.
51    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    /// Returns the current UTF-16 selection range and direction, if any.
59    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    /// Clears active preedit marking while keeping its text and completes a frame.
67    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}