Skip to main content

gpui_material_foundation/test/
events.rs

1use gpui::{
2    App, InputEvent, KeyDownEvent, KeyUpEvent, Keystroke, Modifiers, MouseButton, Pixels, Point,
3    Window,
4};
5
6use super::{TestWindowExt as _, change_modifiers, mouse_down, mouse_up, move_pointer};
7
8/// Step-level pointer, modifier, and keyboard dispatch helpers for tests that
9/// inspect intermediate gesture state.
10pub trait TestEventExt {
11    /// Dispatches a single mouse-down event at `position` and completes a frame.
12    fn pointer_down(&mut self, position: Point<Pixels>, button: MouseButton, cx: &mut App);
13
14    /// Dispatches a single pointer-move event at `position` with an explicit held
15    /// button state and completes a frame.
16    fn pointer_move(
17        &mut self,
18        position: Point<Pixels>,
19        pressed_button: Option<MouseButton>,
20        cx: &mut App,
21    );
22
23    /// Dispatches a single mouse-up event at `position` and completes a frame.
24    fn pointer_up(&mut self, position: Point<Pixels>, button: MouseButton, cx: &mut App);
25
26    /// Updates the window's active modifiers (preserving Caps Lock) and completes a frame.
27    fn change_modifiers(&mut self, modifiers: Modifiers, cx: &mut App);
28
29    /// Dispatches a key-down event (optionally with auto-repeat `is_held = true`)
30    /// combining any parsed modifiers with the window's held modifiers.
31    fn key_down(&mut self, key: &str, is_held: bool, cx: &mut App);
32
33    /// Dispatches a matching key-up event combining any parsed modifiers with the
34    /// window's held modifiers.
35    fn key_up(&mut self, key: &str, cx: &mut App);
36}
37
38fn parse_with_held_modifiers(window: &Window, key: &str, cx: &App) -> Keystroke {
39    let mut parsed = Keystroke::parse_with_os(key, cx.platform_info().os)
40        .unwrap_or_else(|error| panic!("invalid test keystroke: {error}"));
41    let held = window.modifiers();
42    parsed.modifiers.control |= held.control;
43    parsed.modifiers.alt |= held.alt;
44    parsed.modifiers.shift |= held.shift;
45    parsed.modifiers.platform |= held.platform;
46    parsed.modifiers.function |= held.function;
47    parsed
48}
49
50impl TestEventExt for Window {
51    fn pointer_down(&mut self, position: Point<Pixels>, button: MouseButton, cx: &mut App) {
52        self.render_frame(cx);
53        let modifiers = self.modifiers();
54        mouse_down(self, position, button, 1, modifiers, cx);
55    }
56
57    fn pointer_move(
58        &mut self,
59        position: Point<Pixels>,
60        pressed_button: Option<MouseButton>,
61        cx: &mut App,
62    ) {
63        self.render_frame(cx);
64        let modifiers = self.modifiers();
65        move_pointer(self, position, pressed_button, modifiers, cx);
66    }
67
68    fn pointer_up(&mut self, position: Point<Pixels>, button: MouseButton, cx: &mut App) {
69        self.render_frame(cx);
70        let modifiers = self.modifiers();
71        mouse_up(self, position, button, 1, modifiers, cx);
72    }
73
74    fn change_modifiers(&mut self, modifiers: Modifiers, cx: &mut App) {
75        self.render_frame(cx);
76        change_modifiers(self, modifiers, cx);
77    }
78
79    fn key_down(&mut self, key: &str, is_held: bool, cx: &mut App) {
80        self.render_frame(cx);
81        let keystroke = parse_with_held_modifiers(self, key, cx);
82        self.dispatch_event(
83            KeyDownEvent {
84                keystroke,
85                is_held,
86                prefer_character_input: false,
87            }
88            .to_platform_input(),
89            cx,
90        );
91        self.render_frame(cx);
92    }
93
94    fn key_up(&mut self, key: &str, cx: &mut App) {
95        self.render_frame(cx);
96        let keystroke = parse_with_held_modifiers(self, key, cx);
97        self.dispatch_event(KeyUpEvent { keystroke }.to_platform_input(), cx);
98        self.render_frame(cx);
99    }
100}