Skip to repository content409 lines · 12.0 KB · rust
tenant.openagents/omega
No repository description is available.
OpenAgents Git authority 2026-07-28T06:50:44.187Z Public web read
NIP-34 coordinate
30617:7649603503856e5148d571eac2766b288a8ff1e9e35d380337a1d2b0015b4f92:omegaMaintainersHidden in public view
References2 branches · 1 tag
Read-only clone
git clone https://openagents.com/git/tenant.openagents/omega.gitBrowse files
window.rs
1use crate::{
2 AnyWindowHandle, AtlasKey, AtlasTextureId, AtlasTile, Bounds, DevicePixels,
3 DispatchEventResult, GpuSpecs, Pixels, PlatformAtlas, PlatformDisplay,
4 PlatformHeadlessRenderer, PlatformInput, PlatformInputHandler, PlatformWindow, Point,
5 PromptButton, RequestFrameOptions, Scene, Size, TestPlatform, TileId, WindowAppearance,
6 WindowBackgroundAppearance, WindowBounds, WindowControlArea, WindowParams,
7};
8use collections::HashMap;
9use gpui_util::ResultExt as _;
10use image::RgbaImage;
11use parking_lot::Mutex;
12use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
13use std::{
14 rc::{Rc, Weak},
15 sync::{self, Arc},
16};
17
18pub(crate) struct TestWindowState {
19 pub(crate) bounds: Bounds<Pixels>,
20 pub(crate) handle: AnyWindowHandle,
21 display: Rc<dyn PlatformDisplay>,
22 pub(crate) title: Option<String>,
23 pub(crate) edited: bool,
24 pub(crate) document_path: Option<std::path::PathBuf>,
25 platform: Weak<TestPlatform>,
26 // TODO: Replace with `Rc`
27 sprite_atlas: Arc<dyn PlatformAtlas>,
28 renderer: Option<Box<dyn PlatformHeadlessRenderer>>,
29 pub(crate) should_close_handler: Option<Box<dyn FnMut() -> bool>>,
30 hit_test_window_control_callback: Option<Box<dyn FnMut() -> Option<WindowControlArea>>>,
31 input_callback: Option<Box<dyn FnMut(PlatformInput) -> DispatchEventResult>>,
32 active_status_change_callback: Option<Box<dyn FnMut(bool)>>,
33 hover_status_change_callback: Option<Box<dyn FnMut(bool)>>,
34 resize_callback: Option<Box<dyn FnMut(Size<Pixels>, f32)>>,
35 moved_callback: Option<Box<dyn FnMut()>>,
36 input_handler: Option<PlatformInputHandler>,
37 is_fullscreen: bool,
38}
39
40#[derive(Clone)]
41pub struct TestWindow(pub(crate) Rc<Mutex<TestWindowState>>);
42
43impl HasWindowHandle for TestWindow {
44 fn window_handle(
45 &self,
46 ) -> Result<raw_window_handle::WindowHandle<'_>, raw_window_handle::HandleError> {
47 unimplemented!("Test Windows are not backed by a real platform window")
48 }
49}
50
51impl HasDisplayHandle for TestWindow {
52 fn display_handle(
53 &self,
54 ) -> Result<raw_window_handle::DisplayHandle<'_>, raw_window_handle::HandleError> {
55 unimplemented!("Test Windows are not backed by a real platform window")
56 }
57}
58
59impl TestWindow {
60 pub(crate) fn new(
61 handle: AnyWindowHandle,
62 params: WindowParams,
63 platform: Weak<TestPlatform>,
64 display: Rc<dyn PlatformDisplay>,
65 renderer: Option<Box<dyn PlatformHeadlessRenderer>>,
66 ) -> Self {
67 let sprite_atlas: Arc<dyn PlatformAtlas> = match &renderer {
68 Some(r) => r.sprite_atlas(),
69 None => Arc::new(TestAtlas::new()),
70 };
71 Self(Rc::new(Mutex::new(TestWindowState {
72 bounds: params.bounds,
73 display,
74 platform,
75 handle,
76 sprite_atlas,
77 renderer,
78 title: Default::default(),
79 edited: false,
80 document_path: None,
81 should_close_handler: None,
82 hit_test_window_control_callback: None,
83 input_callback: None,
84 active_status_change_callback: None,
85 hover_status_change_callback: None,
86 resize_callback: None,
87 moved_callback: None,
88 input_handler: None,
89 is_fullscreen: false,
90 })))
91 }
92
93 pub fn simulate_resize(&mut self, size: Size<Pixels>) {
94 let scale_factor = self.scale_factor();
95 let mut lock = self.0.lock();
96 // Always update bounds, even if no callback is registered
97 lock.bounds.size = size;
98 let Some(mut callback) = lock.resize_callback.take() else {
99 return;
100 };
101 drop(lock);
102 callback(size, scale_factor);
103 self.0.lock().resize_callback = Some(callback);
104 }
105
106 pub(crate) fn simulate_active_status_change(&self, active: bool) {
107 let mut lock = self.0.lock();
108 let Some(mut callback) = lock.active_status_change_callback.take() else {
109 return;
110 };
111 drop(lock);
112 callback(active);
113 self.0.lock().active_status_change_callback = Some(callback);
114 }
115
116 pub fn simulate_input(&mut self, event: PlatformInput) -> bool {
117 let mut lock = self.0.lock();
118 let Some(mut callback) = lock.input_callback.take() else {
119 return false;
120 };
121 drop(lock);
122 let result = callback(event);
123 self.0.lock().input_callback = Some(callback);
124 !result.propagate
125 }
126}
127
128impl PlatformWindow for TestWindow {
129 fn bounds(&self) -> Bounds<Pixels> {
130 self.0.lock().bounds
131 }
132
133 fn window_bounds(&self) -> WindowBounds {
134 WindowBounds::Windowed(self.bounds())
135 }
136
137 fn is_maximized(&self) -> bool {
138 false
139 }
140
141 fn content_size(&self) -> Size<Pixels> {
142 self.bounds().size
143 }
144
145 fn resize(&mut self, size: Size<Pixels>) {
146 let mut lock = self.0.lock();
147 lock.bounds.size = size;
148 }
149
150 fn scale_factor(&self) -> f32 {
151 2.0
152 }
153
154 fn appearance(&self) -> WindowAppearance {
155 WindowAppearance::Light
156 }
157
158 fn display(&self) -> Option<std::rc::Rc<dyn crate::PlatformDisplay>> {
159 Some(self.0.lock().display.clone())
160 }
161
162 fn mouse_position(&self) -> Point<Pixels> {
163 Point::default()
164 }
165
166 fn modifiers(&self) -> crate::Modifiers {
167 crate::Modifiers::default()
168 }
169
170 fn capslock(&self) -> crate::Capslock {
171 crate::Capslock::default()
172 }
173
174 fn set_input_handler(&mut self, input_handler: PlatformInputHandler) {
175 self.0.lock().input_handler = Some(input_handler);
176 }
177
178 fn take_input_handler(&mut self) -> Option<PlatformInputHandler> {
179 self.0.lock().input_handler.take()
180 }
181
182 fn prompt(
183 &self,
184 _level: crate::PromptLevel,
185 msg: &str,
186 detail: Option<&str>,
187 answers: &[PromptButton],
188 ) -> Option<futures::channel::oneshot::Receiver<usize>> {
189 Some(
190 self.0
191 .lock()
192 .platform
193 .upgrade()
194 .expect("platform dropped")
195 .prompt(msg, detail, answers),
196 )
197 }
198
199 fn activate(&self) {
200 self.0
201 .lock()
202 .platform
203 .upgrade()
204 .unwrap()
205 .set_active_window(Some(self.clone()))
206 }
207
208 fn is_active(&self) -> bool {
209 false
210 }
211
212 fn is_hovered(&self) -> bool {
213 false
214 }
215
216 fn background_appearance(&self) -> WindowBackgroundAppearance {
217 WindowBackgroundAppearance::Opaque
218 }
219
220 fn is_subpixel_rendering_supported(&self) -> bool {
221 false
222 }
223
224 fn set_title(&mut self, title: &str) {
225 self.0.lock().title = Some(title.to_owned());
226 }
227
228 fn set_app_id(&mut self, _app_id: &str) {}
229
230 fn set_background_appearance(&self, _background: WindowBackgroundAppearance) {}
231
232 fn set_edited(&mut self, edited: bool) {
233 self.0.lock().edited = edited;
234 }
235
236 fn set_document_path(&self, path: Option<&std::path::Path>) {
237 self.0.lock().document_path = path.map(|p| p.to_path_buf());
238 }
239
240 fn show_character_palette(&self) {
241 unimplemented!()
242 }
243
244 fn minimize(&self) {
245 unimplemented!()
246 }
247
248 fn zoom(&self) {
249 unimplemented!()
250 }
251
252 fn toggle_fullscreen(&self) {
253 let mut lock = self.0.lock();
254 lock.is_fullscreen = !lock.is_fullscreen;
255 }
256
257 fn is_fullscreen(&self) -> bool {
258 self.0.lock().is_fullscreen
259 }
260
261 fn on_request_frame(&self, _callback: Box<dyn FnMut(RequestFrameOptions)>) {}
262
263 fn on_input(&self, callback: Box<dyn FnMut(crate::PlatformInput) -> DispatchEventResult>) {
264 self.0.lock().input_callback = Some(callback)
265 }
266
267 fn on_active_status_change(&self, callback: Box<dyn FnMut(bool)>) {
268 self.0.lock().active_status_change_callback = Some(callback)
269 }
270
271 fn on_hover_status_change(&self, callback: Box<dyn FnMut(bool)>) {
272 self.0.lock().hover_status_change_callback = Some(callback)
273 }
274
275 fn on_resize(&self, callback: Box<dyn FnMut(Size<Pixels>, f32)>) {
276 self.0.lock().resize_callback = Some(callback)
277 }
278
279 fn on_moved(&self, callback: Box<dyn FnMut()>) {
280 self.0.lock().moved_callback = Some(callback)
281 }
282
283 fn on_should_close(&self, callback: Box<dyn FnMut() -> bool>) {
284 self.0.lock().should_close_handler = Some(callback);
285 }
286
287 fn on_close(&self, _callback: Box<dyn FnOnce()>) {}
288
289 fn on_hit_test_window_control(&self, callback: Box<dyn FnMut() -> Option<WindowControlArea>>) {
290 self.0.lock().hit_test_window_control_callback = Some(callback);
291 }
292
293 fn on_appearance_changed(&self, _callback: Box<dyn FnMut()>) {}
294
295 fn draw(&self, scene: &Scene) {
296 let scale_factor = self.scale_factor();
297 let mut state = self.0.lock();
298 let device_size: Size<DevicePixels> = state.bounds.size.to_device_pixels(scale_factor);
299 if let Some(renderer) = &mut state.renderer {
300 renderer.render_scene(scene, device_size).warn_on_err();
301 }
302 }
303
304 fn sprite_atlas(&self) -> sync::Arc<dyn crate::PlatformAtlas> {
305 self.0.lock().sprite_atlas.clone()
306 }
307
308 #[cfg(any(test, feature = "test-support"))]
309 fn render_to_image(&self, scene: &Scene) -> anyhow::Result<RgbaImage> {
310 let scale_factor = self.scale_factor();
311 let mut state = self.0.lock();
312 let size = state.bounds.size;
313 if let Some(renderer) = &mut state.renderer {
314 let device_size: Size<DevicePixels> = size.to_device_pixels(scale_factor);
315 renderer.render_scene_to_image(scene, device_size)
316 } else {
317 anyhow::bail!("render_to_image not available: no HeadlessRenderer configured")
318 }
319 }
320
321 fn as_test(&mut self) -> Option<&mut TestWindow> {
322 Some(self)
323 }
324
325 #[cfg(target_os = "windows")]
326 fn get_raw_handle(&self) -> windows::Win32::Foundation::HWND {
327 unimplemented!()
328 }
329
330 fn show_window_menu(&self, _position: Point<Pixels>) {
331 unimplemented!()
332 }
333
334 fn start_window_move(&self) {
335 unimplemented!()
336 }
337
338 fn update_ime_position(&self, _bounds: Bounds<Pixels>) {}
339
340 fn gpu_specs(&self) -> Option<GpuSpecs> {
341 None
342 }
343}
344
345pub(crate) struct TestAtlasState {
346 next_id: u32,
347 tiles: HashMap<AtlasKey, AtlasTile>,
348}
349
350pub(crate) struct TestAtlas(Mutex<TestAtlasState>);
351
352impl TestAtlas {
353 pub fn new() -> Self {
354 TestAtlas(Mutex::new(TestAtlasState {
355 next_id: 0,
356 tiles: HashMap::default(),
357 }))
358 }
359}
360
361impl PlatformAtlas for TestAtlas {
362 fn get_or_insert_with<'a>(
363 &self,
364 key: &crate::AtlasKey,
365 build: &mut dyn FnMut() -> anyhow::Result<
366 Option<(Size<crate::DevicePixels>, std::borrow::Cow<'a, [u8]>)>,
367 >,
368 ) -> anyhow::Result<Option<crate::AtlasTile>> {
369 let mut state = self.0.lock();
370 if let Some(&tile) = state.tiles.get(key) {
371 return Ok(Some(tile));
372 }
373 drop(state);
374
375 let Some((size, _)) = build()? else {
376 return Ok(None);
377 };
378
379 let mut state = self.0.lock();
380 state.next_id += 1;
381 let texture_id = state.next_id;
382 state.next_id += 1;
383 let tile_id = state.next_id;
384
385 state.tiles.insert(
386 key.clone(),
387 crate::AtlasTile {
388 texture_id: AtlasTextureId {
389 index: texture_id,
390 kind: crate::AtlasTextureKind::Monochrome,
391 },
392 tile_id: TileId(tile_id),
393 padding: 0,
394 bounds: crate::Bounds {
395 origin: Point::default(),
396 size,
397 },
398 },
399 );
400
401 Ok(Some(state.tiles[key]))
402 }
403
404 fn remove(&self, key: &AtlasKey) {
405 let mut state = self.0.lock();
406 state.tiles.remove(key);
407 }
408}
409