1use super::*;
4
5impl AppRuntime {
6 pub fn ensure_mcp_cursor(&mut self) -> &mut strata_platform::McpCursorState {
8 if self.mcp_cursor.is_none() {
9 self.mcp_cursor = Some(strata_platform::McpCursorState::new(
10 self.viewport_width / 2.0,
11 self.viewport_height / 2.0,
12 ));
13 }
14 self.mcp_cursor.as_mut().unwrap()
15 }
16
17 pub fn mcp_move_pointer(&mut self, x: f32, y: f32, smooth: bool) -> bool {
22 let cursor_icon = Self::hit_test_cursor(&self.root_blueprint, &self.layout_root, x, y);
23 let cursor = self.ensure_mcp_cursor();
24 cursor.move_to(x, y, smooth);
25 cursor.cursor_icon = cursor_icon;
26
27 self.last_mouse_pos = (x, y);
28 let mut needs_rebuild = false;
29 let mut needs_redraw = false;
30
31 if self.dispatch_pointer_move(x, y) {
32 needs_rebuild = true;
33 }
34 if self.update_hover(x, y) {
35 if self.hover_is_paint_only() {
36 needs_redraw = true;
37 } else {
38 needs_rebuild = true;
39 }
40 }
41
42 needs_redraw || needs_rebuild || true
43 }
44
45 pub fn mcp_mouse_down(
47 &mut self,
48 button: strata_platform::MouseButton,
49 x: Option<f32>,
50 y: Option<f32>,
51 ) -> bool {
52 let (px, py) = match (x, y) {
53 (Some(px), Some(py)) => {
54 self.mcp_move_pointer(px, py, false);
55 (px, py)
56 }
57 _ => {
58 let c = self.ensure_mcp_cursor();
59 (c.current_x, c.current_y)
60 }
61 };
62
63 let cursor = self.ensure_mcp_cursor();
64 cursor.set_down(true);
65
66 let btn_idx = button.to_button_index();
67 let mut rebuild = false;
68
69 if button == strata_platform::MouseButton::Left {
70 if self.check_scrollbar_thumb_press(px, py) {
71 return true;
72 }
73 self.begin_window_drag_candidate(px, py);
74 if self.dispatch_pointer_down_with_modifiers(
75 px,
76 py,
77 strata_platform::Modifiers::default(),
78 ) {
79 rebuild = true;
80 }
81 if self.dispatch_mouse_click(px, py, btn_idx, 1) {
82 rebuild = true;
83 }
84 if self.drain_edit_commands() {
85 rebuild = true;
86 }
87 } else if button == strata_platform::MouseButton::Middle {
88 if self.dispatch_pointer_down(px, py) {
89 rebuild = true;
90 }
91 if self.dispatch_mouse_click(px, py, btn_idx, 1) {
92 rebuild = true;
93 }
94 } else if button == strata_platform::MouseButton::Right {
95 if self.dispatch_context_menu(px, py) {
96 rebuild = true;
97 }
98 if self.drain_edit_commands() {
99 rebuild = true;
100 }
101 }
102
103 rebuild
104 }
105
106 pub fn mcp_mouse_up(
108 &mut self,
109 _button: strata_platform::MouseButton,
110 x: Option<f32>,
111 y: Option<f32>,
112 ) -> bool {
113 let (px, py) = match (x, y) {
114 (Some(px), Some(py)) => {
115 self.mcp_move_pointer(px, py, false);
116 (px, py)
117 }
118 _ => {
119 let c = self.ensure_mcp_cursor();
120 (c.current_x, c.current_y)
121 }
122 };
123
124 let cursor = self.ensure_mcp_cursor();
125 cursor.set_down(false);
126
127 let mut redraw = false;
128 if self.scrollbar_drag.is_some() {
129 self.scrollbar_drag = None;
130 redraw = true;
131 }
132 self.pending_window_drag = None;
133 let rebuild = self.dispatch_pointer_up(px, py);
134
135 rebuild || redraw
136 }
137
138 pub fn mcp_mouse_click(
140 &mut self,
141 button: strata_platform::MouseButton,
142 x: Option<f32>,
143 y: Option<f32>,
144 count: usize,
145 ) -> bool {
146 let (px, py) = match (x, y) {
147 (Some(px), Some(py)) => {
148 self.mcp_move_pointer(px, py, false);
149 (px, py)
150 }
151 _ => {
152 let c = self.ensure_mcp_cursor();
153 (c.current_x, c.current_y)
154 }
155 };
156
157 let mut rebuild = false;
158 for _ in 0..count.max(1) {
159 let down = self.mcp_mouse_down(button, Some(px), Some(py));
160 let up = self.mcp_mouse_up(button, Some(px), Some(py));
161 if down || up {
162 rebuild = true;
163 }
164 }
165
166 rebuild
167 }
168
169 pub fn mcp_mouse_scroll(
171 &mut self,
172 delta_x: f32,
173 delta_y: f32,
174 x: Option<f32>,
175 y: Option<f32>,
176 ) -> bool {
177 let (px, py) = match (x, y) {
178 (Some(px), Some(py)) => {
179 self.mcp_move_pointer(px, py, false);
180 (px, py)
181 }
182 _ => {
183 let c = self.ensure_mcp_cursor();
184 (c.current_x, c.current_y)
185 }
186 };
187
188 self.ensure_mcp_cursor().touch_activity();
189 self.dispatch_smooth_scroll(px, py, delta_x, delta_y)
190 }
191
192 pub fn mcp_keyboard_type(&mut self, text: &str) -> bool {
194 self.ensure_mcp_cursor().touch_activity();
195 let mut any_handled = false;
196 for ch in text.chars() {
197 let ev = strata_platform::PlatformEvent::ReceivedCharacter(ch);
198 if self.handle_key_event(&ev) {
199 any_handled = true;
200 }
201 }
202 any_handled
203 }
204
205 pub fn mcp_keyboard_key(
207 &mut self,
208 key: strata_platform::KeyCode,
209 modifiers: strata_platform::Modifiers,
210 ) -> bool {
211 self.ensure_mcp_cursor().touch_activity();
212 let ev = strata_platform::PlatformEvent::KeyDown { key, modifiers };
213 let handled = self.handle_key_event(&ev);
214 let _ = self.handle_key_event(&strata_platform::PlatformEvent::KeyUp { key, modifiers });
215 handled
216 }
217
218 pub fn mcp_inspect_element(
220 &self,
221 x: f32,
222 y: f32,
223 ) -> (
224 String,
225 Vec<String>,
226 strata_layout::Rect,
227 String,
228 strata_platform::window::CursorIcon,
229 ) {
230 let cursor_icon = Self::hit_test_cursor(&self.root_blueprint, &self.layout_root, x, y);
231 if let Some(path) = Self::hit_test_node_path(&self.root_blueprint, &self.layout_root, x, y)
232 {
233 if let Some((bp, layout)) =
234 Self::find_node_by_path(&self.root_blueprint, &self.layout_root, &path)
235 {
236 let (tag_name, classes) = match bp {
237 NodeBlueprint::Element { tag, classes, .. } => {
238 (format!("{:?}", tag).to_ascii_lowercase(), classes.clone())
239 }
240 NodeBlueprint::Text(_) | NodeBlueprint::DynamicText(_) => {
241 ("#text".to_string(), strata_dom::ClassList::Empty)
242 }
243 _ => ("div".to_string(), strata_dom::ClassList::Empty),
244 };
245 let text = Self::extract_text_content(bp);
246 return (
247 tag_name,
248 classes.to_vec(),
249 layout.geometry.border_box,
250 text,
251 cursor_icon,
252 );
253 }
254 }
255
256 (
257 "body".to_string(),
258 Vec::new(),
259 strata_layout::Rect {
260 x: 0.0,
261 y: 0.0,
262 width: self.viewport_width,
263 height: self.viewport_height,
264 },
265 String::new(),
266 cursor_icon,
267 )
268 }
269}