1use crate::schema::{EventPayload, Theme};
18use std::cell::{Cell, RefCell};
19use std::rc::Rc;
20
21pub use crate::schema::{LastWindowClosed, WindowMode};
22
23#[derive(Clone, Copy, Debug, Eq, PartialEq)]
25pub enum WindowDragPhase {
26 Move,
28 End,
30 Cancel,
32}
33
34impl WindowDragPhase {
35 fn from_wire(value: u32) -> Option<Self> {
36 match value {
37 crate::protocol::WINDOW_DRAG_MOVE => Some(Self::Move),
38 crate::protocol::WINDOW_DRAG_END => Some(Self::End),
39 crate::protocol::WINDOW_DRAG_CANCEL => Some(Self::Cancel),
40 _ => None,
41 }
42 }
43}
44
45#[derive(Clone, Copy, Debug, Eq, PartialEq)]
47pub enum WindowDragOverKind {
48 Enter,
49 Move,
50 Leave,
51 Drop,
53}
54
55impl WindowDragOverKind {
56 fn from_wire(value: u32) -> Option<Self> {
57 match value {
58 crate::protocol::WINDOW_DRAG_OVER_ENTER => Some(Self::Enter),
59 crate::protocol::WINDOW_DRAG_OVER_MOVE => Some(Self::Move),
60 crate::protocol::WINDOW_DRAG_OVER_LEAVE => Some(Self::Leave),
61 crate::protocol::WINDOW_DRAG_OVER_DROP => Some(Self::Drop),
62 _ => None,
63 }
64 }
65}
66
67#[derive(Clone, Copy, Debug, PartialEq)]
69pub enum WindowEvent {
70 CloseRequested,
73 FocusChanged { focused: bool },
75 Moved {
77 x: f32,
78 y: f32,
79 width: f32,
80 height: f32,
81 },
82 Drag {
85 phase: WindowDragPhase,
86 screen_x: f32,
87 screen_y: f32,
88 over_window: u32,
89 },
90 DragOver {
93 kind: WindowDragOverKind,
94 window: u32,
95 x: f32,
96 y: f32,
97 },
98 Unavailable,
100 Closed,
102}
103
104impl WindowEvent {
105 pub(crate) fn from_payload(payload: &EventPayload<'_>) -> Option<Self> {
107 Some(match *payload {
108 EventPayload::WindowCloseRequested => Self::CloseRequested,
109 EventPayload::WindowFocusChanged { focused } => Self::FocusChanged { focused },
110 EventPayload::WindowMoved {
111 x,
112 y,
113 width,
114 height,
115 } => Self::Moved {
116 x,
117 y,
118 width,
119 height,
120 },
121 EventPayload::WindowDrag {
122 phase,
123 screen_x,
124 screen_y,
125 over_window,
126 } => Self::Drag {
127 phase: WindowDragPhase::from_wire(phase)?,
128 screen_x,
129 screen_y,
130 over_window,
131 },
132 EventPayload::WindowDragOver { kind, window, x, y } => Self::DragOver {
133 kind: WindowDragOverKind::from_wire(kind)?,
134 window,
135 x,
136 y,
137 },
138 EventPayload::WindowUnavailable => Self::Unavailable,
139 EventPayload::WindowClosed => Self::Closed,
140 _ => return None,
141 })
142 }
143}
144
145#[derive(Clone, Copy, Debug, PartialEq)]
147pub struct WindowFrame {
148 pub x: f32,
149 pub y: f32,
150 pub width: f32,
151 pub height: f32,
152}
153
154#[derive(Clone, Copy, Debug, PartialEq)]
156pub struct WindowDragEvent {
157 pub phase: WindowDragPhase,
158 pub screen_x: f32,
160 pub screen_y: f32,
161 pub over_window: u32,
163}
164
165#[derive(Clone, Copy, Debug, PartialEq)]
167pub struct WindowDragOver {
168 pub kind: WindowDragOverKind,
169 pub window: u32,
171 pub x: f32,
173 pub y: f32,
174}
175
176#[derive(Clone, Copy, Debug, PartialEq)]
179pub struct DragFromPointer {
180 pub pointer_id: u32,
181 pub anchor_x: f32,
183 pub anchor_y: f32,
184}
185
186impl DragFromPointer {
187 pub(crate) fn to_bytes(self) -> Vec<u8> {
189 let mut bytes = Vec::with_capacity(12);
190 bytes.extend_from_slice(&self.pointer_id.to_le_bytes());
191 bytes.extend_from_slice(&self.anchor_x.to_bits().to_le_bytes());
192 bytes.extend_from_slice(&self.anchor_y.to_bits().to_le_bytes());
193 bytes
194 }
195}
196
197#[derive(Clone, Debug, Eq, PartialEq)]
200pub struct WindowRequest {
201 pub key: String,
203 pub request: u32,
205}
206
207pub(crate) enum Command {
209 Focus(u32),
210 Theme(u32, Theme),
211 ZoomLevel(u32, i8),
212}
213
214type RequestHandler = Rc<dyn Fn(WindowRequest)>;
215type ReopenHandler = Rc<dyn Fn()>;
216
217thread_local! {
218 static DISPATCH_WINDOW: Cell<u32> = const { Cell::new(0) };
221 static COMMANDS: RefCell<Vec<Command>> = const { RefCell::new(Vec::new()) };
222 static APPLIED_THEMES: RefCell<Vec<(u32, Theme)>> = const { RefCell::new(Vec::new()) };
224 static REQUEST_HANDLERS: RefCell<Vec<(u64, RequestHandler)>> =
225 const { RefCell::new(Vec::new()) };
226 static REOPEN_HANDLERS: RefCell<Vec<(u64, ReopenHandler)>> =
227 const { RefCell::new(Vec::new()) };
228 static NEXT_ID: Cell<u64> = const { Cell::new(1) };
229}
230
231pub(crate) struct DispatchWindow {
233 previous: u32,
234}
235
236impl DispatchWindow {
237 pub(crate) fn enter(window: u32) -> Self {
238 let previous = DISPATCH_WINDOW.with(|current| current.replace(window));
239 Self { previous }
240 }
241}
242
243impl Drop for DispatchWindow {
244 fn drop(&mut self) {
245 let _ = DISPATCH_WINDOW.try_with(|current| current.set(self.previous));
246 }
247}
248
249pub(crate) fn current_window_id() -> u32 {
254 let composing = crate::runtime::with_composer_if_idle(|composer| {
255 composer.composing().then(|| composer.enclosing_window())
256 })
257 .flatten();
258 composing.unwrap_or_else(|| DISPATCH_WINDOW.with(Cell::get))
259}
260
261#[derive(Clone, Copy, Debug, Eq, PartialEq)]
266pub struct WindowHandle {
267 node: u32,
268}
269
270pub fn current_window() -> Option<WindowHandle> {
273 match current_window_id() {
274 0 => None,
275 node => Some(WindowHandle { node }),
276 }
277}
278
279impl WindowHandle {
280 pub const fn id(&self) -> u32 {
282 self.node
283 }
284
285 pub fn focus(&self) {
288 COMMANDS.with_borrow_mut(|commands| commands.push(Command::Focus(self.node)));
289 crate::boundary::request_frame_from_worker();
290 }
291
292 pub fn set_theme(&self, theme: Theme) {
294 apply_theme(self.node, theme);
295 }
296
297 pub fn size(&self) -> crate::window::WindowSize {
300 crate::window::window_size_of(self.node)
301 }
302
303 pub fn zoom(&self) -> crate::zoom::Zoom {
305 crate::zoom::zoom_of(self.node)
306 }
307
308 pub fn density(&self) -> f32 {
310 crate::density::density_of(self.node)
311 }
312
313 pub fn set_zoom_level(&self, level: i8) {
315 queue_zoom_level(self.node, crate::zoom::clamp_level(level));
316 }
317}
318
319pub(crate) fn apply_theme(window: u32, theme: Theme) {
321 let changed = APPLIED_THEMES.with_borrow_mut(|applied| {
322 match applied.iter_mut().find(|(id, _)| *id == window) {
323 Some(entry) => {
324 if entry.1 == theme {
325 return false;
326 }
327 entry.1 = theme;
328 true
329 }
330 None => {
331 applied.push((window, theme));
332 true
333 }
334 }
335 });
336 if !changed {
337 return;
338 }
339 COMMANDS.with_borrow_mut(|commands| {
340 commands.retain(|command| !matches!(command, Command::Theme(id, _) if *id == window));
341 commands.push(Command::Theme(window, theme));
342 });
343 crate::boundary::request_frame_from_worker();
344}
345
346pub(crate) fn queue_zoom_level(window: u32, level: i8) {
348 COMMANDS.with_borrow_mut(|commands| {
349 commands.retain(|command| !matches!(command, Command::ZoomLevel(id, _) if *id == window));
350 commands.push(Command::ZoomLevel(window, level));
351 });
352 crate::boundary::request_frame_from_worker();
353}
354
355pub(crate) fn pending_zoom_level(window: u32) -> Option<i8> {
357 COMMANDS.with_borrow(|commands| {
358 commands.iter().find_map(|command| match command {
359 Command::ZoomLevel(id, level) if *id == window => Some(*level),
360 _ => None,
361 })
362 })
363}
364
365pub(crate) fn drain_commands(mut each: impl FnMut(Command)) {
367 let commands = COMMANDS.with_borrow_mut(std::mem::take);
368 for command in commands {
369 each(command);
370 }
371}
372
373pub(crate) fn forget_window(window: u32) {
375 crate::window::forget_window(window);
376 crate::zoom::forget_window(window);
377 crate::density::forget_window(window);
378 APPLIED_THEMES.with_borrow_mut(|applied| applied.retain(|(id, _)| *id != window));
379 COMMANDS.with_borrow_mut(|commands| {
380 commands.retain(|command| match command {
381 Command::Focus(id) | Command::Theme(id, _) | Command::ZoomLevel(id, _) => *id != window,
382 });
383 });
384}
385
386#[doc(hidden)]
390pub fn window_state_len() -> usize {
391 crate::window::window_state_len()
392 + crate::zoom::window_state_len()
393 + crate::density::window_state_len()
394 + APPLIED_THEMES.with_borrow(Vec::len)
395 + COMMANDS.with_borrow(Vec::len)
396}
397
398#[doc(hidden)]
400pub fn reset_windows() {
401 DISPATCH_WINDOW.with(|current| current.set(0));
402 COMMANDS.with_borrow_mut(Vec::clear);
403 APPLIED_THEMES.with_borrow_mut(Vec::clear);
404 REQUEST_HANDLERS.with_borrow_mut(Vec::clear);
405 REOPEN_HANDLERS.with_borrow_mut(Vec::clear);
406}
407
408fn next_id() -> u64 {
409 NEXT_ID.with(|next| {
410 let id = next.get();
411 next.set(id + 1);
412 id
413 })
414}
415
416pub struct WindowRequestSubscription {
419 id: u64,
420}
421
422impl Drop for WindowRequestSubscription {
423 fn drop(&mut self) {
424 REQUEST_HANDLERS.with_borrow_mut(|handlers| handlers.retain(|(id, _)| *id != self.id));
425 }
426}
427
428pub fn on_window_request(handler: Rc<dyn Fn(WindowRequest)>) -> WindowRequestSubscription {
433 let id = next_id();
434 REQUEST_HANDLERS.with_borrow_mut(|handlers| handlers.push((id, handler)));
435 WindowRequestSubscription { id }
436}
437
438pub(crate) fn deliver_request(request: &WindowRequest) -> bool {
440 let handlers: Vec<Rc<dyn Fn(WindowRequest)>> = REQUEST_HANDLERS.with_borrow(|handlers| {
442 handlers
443 .iter()
444 .map(|(_, handler)| handler.clone())
445 .collect()
446 });
447 for handler in &handlers {
448 handler(request.clone());
449 }
450 !handlers.is_empty()
451}
452
453pub struct ApplicationReopenSubscription {
456 id: u64,
457}
458
459impl Drop for ApplicationReopenSubscription {
460 fn drop(&mut self) {
461 REOPEN_HANDLERS.with_borrow_mut(|handlers| handlers.retain(|(id, _)| *id != self.id));
462 }
463}
464
465pub fn on_application_reopen_handler(handler: Rc<dyn Fn()>) -> ApplicationReopenSubscription {
468 let id = next_id();
469 REOPEN_HANDLERS.with_borrow_mut(|handlers| handlers.push((id, handler)));
470 ApplicationReopenSubscription { id }
471}
472
473pub(crate) fn deliver_reopen() -> bool {
475 let handlers: Vec<Rc<dyn Fn()>> = REOPEN_HANDLERS.with_borrow(|handlers| {
476 handlers
477 .iter()
478 .map(|(_, handler)| handler.clone())
479 .collect()
480 });
481 for handler in &handlers {
482 handler();
483 }
484 !handlers.is_empty()
485}