1use crate::{
2 px, size, AnyBox, AnyView, AppContext, AsyncWindowContext, AvailableSpace, BorrowAppContext,
3 Bounds, BoxShadow, Context, Corners, DevicePixels, DisplayId, Edges, Effect, Element, EntityId,
4 EventEmitter, FocusEvent, FocusListener, FontId, GlobalElementId, GlyphId, Handle, Hsla,
5 ImageData, InputEvent, IsZero, KeyDownEvent, KeyDownListener, KeyUpEvent, KeyUpListener,
6 LayoutId, MainThread, MainThreadOnly, MonochromeSprite, MouseMoveEvent, Path, Pixels, Platform,
7 PlatformAtlas, PlatformWindow, Point, PolychromeSprite, Quad, Reference, RenderGlyphParams,
8 RenderImageParams, RenderSvgParams, ScaledPixels, SceneBuilder, Shadow, SharedString, Size,
9 Style, Subscription, TaffyLayoutEngine, Task, Underline, UnderlineStyle, WeakHandle,
10 WindowOptions, SUBPIXEL_VARIANTS,
11};
12use anyhow::Result;
13use collections::HashMap;
14use derive_more::{Deref, DerefMut};
15use smallvec::SmallVec;
16use std::{
17 any::{Any, TypeId},
18 borrow::Cow,
19 fmt::Debug,
20 future::Future,
21 marker::PhantomData,
22 mem,
23 sync::Arc,
24};
25use util::{post_inc, ResultExt};
26
27#[derive(Deref, DerefMut, Ord, PartialOrd, Eq, PartialEq, Clone, Default)]
28pub struct StackingOrder(pub(crate) SmallVec<[u32; 16]>);
29
30#[derive(Default, Copy, Clone, Debug, Eq, PartialEq)]
31pub enum DispatchPhase {
32 /// After the capture phase comes the bubble phase, in which event handlers are
33 /// invoked front to back. This is the phase you'll usually want to use for event handlers.
34 #[default]
35 Bubble,
36 /// During the initial capture phase, event handlers are invoked back to front. This phase
37 /// is used for special purposes such as clearing the "pressed" state for click events. If
38 /// you stop event propagation during this phase, you need to know what you're doing. Handlers
39 /// outside of the immediate region may rely on detecting non-local events during this phase.
40 Capture,
41}
42
43type AnyMouseEventListener =
44 Box<dyn Fn(&dyn Any, DispatchPhase, &mut WindowContext) + Send + Sync + 'static>;
45type AnyKeyboardEventListener =
46 Box<dyn Fn(&dyn Any, DispatchPhase, &mut WindowContext) + Send + Sync + 'static>;
47type AnyFocusListener = Box<dyn Fn(&FocusEvent, &mut WindowContext) + Send + Sync + 'static>;
48type AnyKeyDownListener =
49 Box<dyn Fn(&KeyDownEvent, DispatchPhase, &mut WindowContext) + Send + Sync + 'static>;
50type AnyKeyUpListener =
51 Box<dyn Fn(&KeyUpEvent, DispatchPhase, &mut WindowContext) + Send + Sync + 'static>;
52
53#[derive(Copy, Clone, PartialEq, Eq, Hash)]
54pub struct FocusId(usize);
55
56#[derive(Clone, PartialEq, Eq)]
57pub struct FocusHandle {
58 pub(crate) id: FocusId,
59}
60
61impl FocusHandle {
62 pub(crate) fn new(id: FocusId) -> Self {
63 Self { id }
64 }
65
66 pub fn is_focused(&self, cx: &WindowContext) -> bool {
67 cx.window.focus == Some(self.id)
68 }
69
70 pub fn contains_focused(&self, cx: &WindowContext) -> bool {
71 cx.focused()
72 .map_or(false, |focused| self.contains(&focused, cx))
73 }
74
75 pub fn within_focused(&self, cx: &WindowContext) -> bool {
76 let focused = cx.focused();
77 focused.map_or(false, |focused| focused.contains(self, cx))
78 }
79
80 pub(crate) fn contains(&self, other: &Self, cx: &WindowContext) -> bool {
81 let mut ancestor = Some(other.id);
82 while let Some(ancestor_id) = ancestor {
83 if self.id == ancestor_id {
84 return true;
85 } else {
86 ancestor = cx.window.focus_parents_by_child.get(&ancestor_id).copied();
87 }
88 }
89 false
90 }
91}
92
93pub struct Window {
94 handle: AnyWindowHandle,
95 platform_window: MainThreadOnly<Box<dyn PlatformWindow>>,
96 display_id: DisplayId,
97 sprite_atlas: Arc<dyn PlatformAtlas>,
98 rem_size: Pixels,
99 content_size: Size<Pixels>,
100 layout_engine: TaffyLayoutEngine,
101 pub(crate) root_view: Option<AnyView>,
102 pub(crate) element_id_stack: GlobalElementId,
103 prev_element_states: HashMap<GlobalElementId, AnyBox>,
104 element_states: HashMap<GlobalElementId, AnyBox>,
105 z_index_stack: StackingOrder,
106 content_mask_stack: Vec<ContentMask<Pixels>>,
107 mouse_listeners: HashMap<TypeId, Vec<(StackingOrder, AnyMouseEventListener)>>,
108 keyboard_listeners: HashMap<TypeId, Vec<AnyKeyboardEventListener>>,
109 focus_stack: Vec<FocusStackFrame>,
110 focus_parents_by_child: HashMap<FocusId, FocusId>,
111 pub(crate) focus_listeners: Vec<AnyFocusListener>,
112 propagate_event: bool,
113 mouse_position: Point<Pixels>,
114 scale_factor: f32,
115 pub(crate) scene_builder: SceneBuilder,
116 pub(crate) dirty: bool,
117 pub(crate) last_blur: Option<Option<FocusId>>,
118 pub(crate) focus: Option<FocusId>,
119 next_focus_id: FocusId,
120}
121
122impl Window {
123 pub fn new(
124 handle: AnyWindowHandle,
125 options: WindowOptions,
126 cx: &mut MainThread<AppContext>,
127 ) -> Self {
128 let platform_window = cx.platform().open_window(handle, options);
129 let display_id = platform_window.display().id();
130 let sprite_atlas = platform_window.sprite_atlas();
131 let mouse_position = platform_window.mouse_position();
132 let content_size = platform_window.content_size();
133 let scale_factor = platform_window.scale_factor();
134 platform_window.on_resize(Box::new({
135 let cx = cx.to_async();
136 move |content_size, scale_factor| {
137 cx.update_window(handle, |cx| {
138 cx.window.scale_factor = scale_factor;
139 cx.window.scene_builder = SceneBuilder::new();
140 cx.window.content_size = content_size;
141 cx.window.display_id = cx
142 .window
143 .platform_window
144 .borrow_on_main_thread()
145 .display()
146 .id();
147 cx.window.dirty = true;
148 })
149 .log_err();
150 }
151 }));
152
153 platform_window.on_input({
154 let cx = cx.to_async();
155 Box::new(move |event| {
156 cx.update_window(handle, |cx| cx.dispatch_event(event))
157 .log_err()
158 .unwrap_or(true)
159 })
160 });
161
162 let platform_window = MainThreadOnly::new(Arc::new(platform_window), cx.executor.clone());
163
164 Window {
165 handle,
166 platform_window,
167 display_id,
168 sprite_atlas,
169 rem_size: px(16.),
170 content_size,
171 layout_engine: TaffyLayoutEngine::new(),
172 root_view: None,
173 element_id_stack: GlobalElementId::default(),
174 prev_element_states: HashMap::default(),
175 element_states: HashMap::default(),
176 z_index_stack: StackingOrder(SmallVec::new()),
177 content_mask_stack: Vec::new(),
178 mouse_listeners: HashMap::default(),
179 keyboard_listeners: HashMap::default(),
180 focus_stack: Vec::new(),
181 focus_parents_by_child: HashMap::default(),
182 focus_listeners: Vec::new(),
183 propagate_event: true,
184 mouse_position,
185 scale_factor,
186 scene_builder: SceneBuilder::new(),
187 dirty: true,
188 last_blur: None,
189 focus: None,
190 next_focus_id: FocusId(0),
191 }
192 }
193}
194
195#[derive(Clone, Debug, Default, PartialEq, Eq)]
196#[repr(C)]
197pub struct ContentMask<P: Clone + Default + Debug> {
198 pub bounds: Bounds<P>,
199}
200
201impl ContentMask<Pixels> {
202 pub fn scale(&self, factor: f32) -> ContentMask<ScaledPixels> {
203 ContentMask {
204 bounds: self.bounds.scale(factor),
205 }
206 }
207
208 pub fn intersect(&self, other: &Self) -> Self {
209 let bounds = self.bounds.intersect(&other.bounds);
210 ContentMask { bounds }
211 }
212}
213
214struct FocusStackFrame {
215 handle: FocusHandle,
216 key_down_listeners: SmallVec<[AnyKeyDownListener; 2]>,
217 key_up_listeners: SmallVec<[AnyKeyUpListener; 2]>,
218}
219
220pub struct WindowContext<'a, 'w> {
221 app: Reference<'a, AppContext>,
222 pub(crate) window: Reference<'w, Window>,
223}
224
225impl<'a, 'w> WindowContext<'a, 'w> {
226 pub(crate) fn mutable(app: &'a mut AppContext, window: &'w mut Window) -> Self {
227 Self {
228 app: Reference::Mutable(app),
229 window: Reference::Mutable(window),
230 }
231 }
232
233 pub fn notify(&mut self) {
234 self.window.dirty = true;
235 }
236
237 pub fn focus_handle(&mut self) -> FocusHandle {
238 let id = FocusId(post_inc(&mut self.window.next_focus_id.0));
239 FocusHandle { id }
240 }
241
242 pub fn focused(&self) -> Option<FocusHandle> {
243 self.window.focus.map(|id| FocusHandle::new(id))
244 }
245
246 pub fn focus(&mut self, handle: &FocusHandle) {
247 if self.window.last_blur.is_none() {
248 self.window.last_blur = Some(self.window.focus);
249 }
250
251 let window_id = self.window.handle.id;
252 self.window.focus = Some(handle.id);
253 self.push_effect(Effect::FocusChanged {
254 window_id,
255 focused: Some(handle.id),
256 });
257 self.notify();
258 }
259
260 pub fn blur(&mut self) {
261 if self.window.last_blur.is_none() {
262 self.window.last_blur = Some(self.window.focus);
263 }
264
265 let window_id = self.window.handle.id;
266 self.window.focus = None;
267 self.push_effect(Effect::FocusChanged {
268 window_id,
269 focused: None,
270 });
271 self.notify();
272 }
273
274 pub fn run_on_main<R>(
275 &mut self,
276 f: impl FnOnce(&mut MainThread<WindowContext<'_, '_>>) -> R + Send + 'static,
277 ) -> Task<Result<R>>
278 where
279 R: Send + 'static,
280 {
281 if self.executor.is_main_thread() {
282 Task::ready(Ok(f(unsafe {
283 mem::transmute::<&mut Self, &mut MainThread<Self>>(self)
284 })))
285 } else {
286 let id = self.window.handle.id;
287 self.app.run_on_main(move |cx| cx.update_window(id, f))
288 }
289 }
290
291 pub fn to_async(&self) -> AsyncWindowContext {
292 AsyncWindowContext::new(self.app.to_async(), self.window.handle)
293 }
294
295 pub fn on_next_frame(&mut self, f: impl FnOnce(&mut WindowContext) + Send + 'static) {
296 let f = Box::new(f);
297 let display_id = self.window.display_id;
298 self.run_on_main(move |cx| {
299 if let Some(callbacks) = cx.next_frame_callbacks.get_mut(&display_id) {
300 callbacks.push(f);
301 // If there was already a callback, it means that we already scheduled a frame.
302 if callbacks.len() > 1 {
303 return;
304 }
305 } else {
306 let async_cx = cx.to_async();
307 cx.next_frame_callbacks.insert(display_id, vec![f]);
308 cx.platform().set_display_link_output_callback(
309 display_id,
310 Box::new(move |_current_time, _output_time| {
311 let _ = async_cx.update(|cx| {
312 let callbacks = cx
313 .next_frame_callbacks
314 .get_mut(&display_id)
315 .unwrap()
316 .drain(..)
317 .collect::<Vec<_>>();
318 for callback in callbacks {
319 callback(cx);
320 }
321
322 cx.run_on_main(move |cx| {
323 if cx.next_frame_callbacks.get(&display_id).unwrap().is_empty() {
324 cx.platform().stop_display_link(display_id);
325 }
326 })
327 .detach();
328 });
329 }),
330 );
331 }
332
333 cx.platform().start_display_link(display_id);
334 })
335 .detach();
336 }
337
338 pub fn spawn<Fut, R>(
339 &mut self,
340 f: impl FnOnce(AnyWindowHandle, AsyncWindowContext) -> Fut + Send + 'static,
341 ) -> Task<R>
342 where
343 R: Send + 'static,
344 Fut: Future<Output = R> + Send + 'static,
345 {
346 let window = self.window.handle;
347 self.app.spawn(move |app| {
348 let cx = AsyncWindowContext::new(app, window);
349 let future = f(window, cx);
350 async move { future.await }
351 })
352 }
353
354 pub fn request_layout(
355 &mut self,
356 style: &Style,
357 children: impl IntoIterator<Item = LayoutId>,
358 ) -> LayoutId {
359 self.app.layout_id_buffer.clear();
360 self.app.layout_id_buffer.extend(children.into_iter());
361 let rem_size = self.rem_size();
362
363 self.window
364 .layout_engine
365 .request_layout(style, rem_size, &self.app.layout_id_buffer)
366 }
367
368 pub fn request_measured_layout<
369 F: Fn(Size<Option<Pixels>>, Size<AvailableSpace>) -> Size<Pixels> + Send + Sync + 'static,
370 >(
371 &mut self,
372 style: Style,
373 rem_size: Pixels,
374 measure: F,
375 ) -> LayoutId {
376 self.window
377 .layout_engine
378 .request_measured_layout(style, rem_size, measure)
379 }
380
381 pub fn layout_bounds(&mut self, layout_id: LayoutId) -> Bounds<Pixels> {
382 self.window
383 .layout_engine
384 .layout_bounds(layout_id)
385 .map(Into::into)
386 }
387
388 pub fn scale_factor(&self) -> f32 {
389 self.window.scale_factor
390 }
391
392 pub fn rem_size(&self) -> Pixels {
393 self.window.rem_size
394 }
395
396 pub fn stop_propagation(&mut self) {
397 self.window.propagate_event = false;
398 }
399
400 pub fn on_mouse_event<Event: 'static>(
401 &mut self,
402 handler: impl Fn(&Event, DispatchPhase, &mut WindowContext) + Send + Sync + 'static,
403 ) {
404 let order = self.window.z_index_stack.clone();
405 self.window
406 .mouse_listeners
407 .entry(TypeId::of::<Event>())
408 .or_default()
409 .push((
410 order,
411 Box::new(move |event: &dyn Any, phase, cx| {
412 handler(event.downcast_ref().unwrap(), phase, cx)
413 }),
414 ))
415 }
416
417 pub fn on_keyboard_event<Event: 'static>(
418 &mut self,
419 handler: impl Fn(&Event, DispatchPhase, &mut WindowContext) + Send + Sync + 'static,
420 ) {
421 self.window
422 .keyboard_listeners
423 .entry(TypeId::of::<Event>())
424 .or_default()
425 .push(Box::new(move |event: &dyn Any, phase, cx| {
426 handler(event.downcast_ref().unwrap(), phase, cx)
427 }))
428 }
429
430 pub fn mouse_position(&self) -> Point<Pixels> {
431 self.window.mouse_position
432 }
433
434 pub fn stack<R>(&mut self, order: u32, f: impl FnOnce(&mut Self) -> R) -> R {
435 self.window.z_index_stack.push(order);
436 let result = f(self);
437 self.window.z_index_stack.pop();
438 result
439 }
440
441 pub fn paint_shadows(
442 &mut self,
443 bounds: Bounds<Pixels>,
444 corner_radii: Corners<Pixels>,
445 shadows: &[BoxShadow],
446 ) {
447 let scale_factor = self.scale_factor();
448 let content_mask = self.content_mask();
449 let window = &mut *self.window;
450 for shadow in shadows {
451 let mut shadow_bounds = bounds;
452 shadow_bounds.origin += shadow.offset;
453 shadow_bounds.dilate(shadow.spread_radius);
454 window.scene_builder.insert(
455 &window.z_index_stack,
456 Shadow {
457 order: 0,
458 bounds: shadow_bounds.scale(scale_factor),
459 content_mask: content_mask.scale(scale_factor),
460 corner_radii: corner_radii.scale(scale_factor),
461 color: shadow.color,
462 blur_radius: shadow.blur_radius.scale(scale_factor),
463 },
464 );
465 }
466 }
467
468 pub fn paint_quad(
469 &mut self,
470 bounds: Bounds<Pixels>,
471 corner_radii: Corners<Pixels>,
472 background: impl Into<Hsla>,
473 border_widths: Edges<Pixels>,
474 border_color: impl Into<Hsla>,
475 ) {
476 let scale_factor = self.scale_factor();
477 let content_mask = self.content_mask();
478
479 let window = &mut *self.window;
480 window.scene_builder.insert(
481 &window.z_index_stack,
482 Quad {
483 order: 0,
484 bounds: bounds.scale(scale_factor),
485 content_mask: content_mask.scale(scale_factor),
486 background: background.into(),
487 border_color: border_color.into(),
488 corner_radii: corner_radii.scale(scale_factor),
489 border_widths: border_widths.scale(scale_factor),
490 },
491 );
492 }
493
494 pub fn paint_path(&mut self, mut path: Path<Pixels>, color: impl Into<Hsla>) {
495 let scale_factor = self.scale_factor();
496 let content_mask = self.content_mask();
497 path.content_mask = content_mask;
498 path.color = color.into();
499 let window = &mut *self.window;
500 window
501 .scene_builder
502 .insert(&window.z_index_stack, path.scale(scale_factor));
503 }
504
505 pub fn paint_underline(
506 &mut self,
507 origin: Point<Pixels>,
508 width: Pixels,
509 style: &UnderlineStyle,
510 ) -> Result<()> {
511 let scale_factor = self.scale_factor();
512 let height = if style.wavy {
513 style.thickness * 3.
514 } else {
515 style.thickness
516 };
517 let bounds = Bounds {
518 origin,
519 size: size(width, height),
520 };
521 let content_mask = self.content_mask();
522 let window = &mut *self.window;
523 window.scene_builder.insert(
524 &window.z_index_stack,
525 Underline {
526 order: 0,
527 bounds: bounds.scale(scale_factor),
528 content_mask: content_mask.scale(scale_factor),
529 thickness: style.thickness.scale(scale_factor),
530 color: style.color.unwrap_or_default(),
531 wavy: style.wavy,
532 },
533 );
534 Ok(())
535 }
536
537 pub fn paint_glyph(
538 &mut self,
539 origin: Point<Pixels>,
540 font_id: FontId,
541 glyph_id: GlyphId,
542 font_size: Pixels,
543 color: Hsla,
544 ) -> Result<()> {
545 let scale_factor = self.scale_factor();
546 let glyph_origin = origin.scale(scale_factor);
547 let subpixel_variant = Point {
548 x: (glyph_origin.x.0.fract() * SUBPIXEL_VARIANTS as f32).floor() as u8,
549 y: (glyph_origin.y.0.fract() * SUBPIXEL_VARIANTS as f32).floor() as u8,
550 };
551 let params = RenderGlyphParams {
552 font_id,
553 glyph_id,
554 font_size,
555 subpixel_variant,
556 scale_factor,
557 is_emoji: false,
558 };
559
560 let raster_bounds = self.text_system().raster_bounds(¶ms)?;
561 if !raster_bounds.is_zero() {
562 let tile =
563 self.window
564 .sprite_atlas
565 .get_or_insert_with(¶ms.clone().into(), &mut || {
566 let (size, bytes) = self.text_system().rasterize_glyph(¶ms)?;
567 Ok((size, Cow::Owned(bytes)))
568 })?;
569 let bounds = Bounds {
570 origin: glyph_origin.map(|px| px.floor()) + raster_bounds.origin.map(Into::into),
571 size: tile.bounds.size.map(Into::into),
572 };
573 let content_mask = self.content_mask().scale(scale_factor);
574 let window = &mut *self.window;
575 window.scene_builder.insert(
576 &window.z_index_stack,
577 MonochromeSprite {
578 order: 0,
579 bounds,
580 content_mask,
581 color,
582 tile,
583 },
584 );
585 }
586 Ok(())
587 }
588
589 pub fn paint_emoji(
590 &mut self,
591 origin: Point<Pixels>,
592 font_id: FontId,
593 glyph_id: GlyphId,
594 font_size: Pixels,
595 ) -> Result<()> {
596 let scale_factor = self.scale_factor();
597 let glyph_origin = origin.scale(scale_factor);
598 let params = RenderGlyphParams {
599 font_id,
600 glyph_id,
601 font_size,
602 // We don't render emojis with subpixel variants.
603 subpixel_variant: Default::default(),
604 scale_factor,
605 is_emoji: true,
606 };
607
608 let raster_bounds = self.text_system().raster_bounds(¶ms)?;
609 if !raster_bounds.is_zero() {
610 let tile =
611 self.window
612 .sprite_atlas
613 .get_or_insert_with(¶ms.clone().into(), &mut || {
614 let (size, bytes) = self.text_system().rasterize_glyph(¶ms)?;
615 Ok((size, Cow::Owned(bytes)))
616 })?;
617 let bounds = Bounds {
618 origin: glyph_origin.map(|px| px.floor()) + raster_bounds.origin.map(Into::into),
619 size: tile.bounds.size.map(Into::into),
620 };
621 let content_mask = self.content_mask().scale(scale_factor);
622 let window = &mut *self.window;
623
624 window.scene_builder.insert(
625 &window.z_index_stack,
626 PolychromeSprite {
627 order: 0,
628 bounds,
629 corner_radii: Default::default(),
630 content_mask,
631 tile,
632 grayscale: false,
633 },
634 );
635 }
636 Ok(())
637 }
638
639 pub fn paint_svg(
640 &mut self,
641 bounds: Bounds<Pixels>,
642 path: SharedString,
643 color: Hsla,
644 ) -> Result<()> {
645 let scale_factor = self.scale_factor();
646 let bounds = bounds.scale(scale_factor);
647 // Render the SVG at twice the size to get a higher quality result.
648 let params = RenderSvgParams {
649 path,
650 size: bounds
651 .size
652 .map(|pixels| DevicePixels::from((pixels.0 * 2.).ceil() as i32)),
653 };
654
655 let tile =
656 self.window
657 .sprite_atlas
658 .get_or_insert_with(¶ms.clone().into(), &mut || {
659 let bytes = self.svg_renderer.render(¶ms)?;
660 Ok((params.size, Cow::Owned(bytes)))
661 })?;
662 let content_mask = self.content_mask().scale(scale_factor);
663
664 let window = &mut *self.window;
665 window.scene_builder.insert(
666 &window.z_index_stack,
667 MonochromeSprite {
668 order: 0,
669 bounds,
670 content_mask,
671 color,
672 tile,
673 },
674 );
675
676 Ok(())
677 }
678
679 pub fn paint_image(
680 &mut self,
681 bounds: Bounds<Pixels>,
682 corner_radii: Corners<Pixels>,
683 data: Arc<ImageData>,
684 grayscale: bool,
685 ) -> Result<()> {
686 let scale_factor = self.scale_factor();
687 let bounds = bounds.scale(scale_factor);
688 let params = RenderImageParams { image_id: data.id };
689
690 let tile = self
691 .window
692 .sprite_atlas
693 .get_or_insert_with(¶ms.clone().into(), &mut || {
694 Ok((data.size(), Cow::Borrowed(data.as_bytes())))
695 })?;
696 let content_mask = self.content_mask().scale(scale_factor);
697 let corner_radii = corner_radii.scale(scale_factor);
698
699 let window = &mut *self.window;
700 window.scene_builder.insert(
701 &window.z_index_stack,
702 PolychromeSprite {
703 order: 0,
704 bounds,
705 content_mask,
706 corner_radii,
707 tile,
708 grayscale,
709 },
710 );
711 Ok(())
712 }
713
714 pub(crate) fn draw(&mut self) {
715 let unit_entity = self.unit_entity.clone();
716 self.update_entity(&unit_entity, |view, cx| {
717 cx.start_frame();
718
719 let mut root_view = cx.window.root_view.take().unwrap();
720
721 if let Some(element_id) = root_view.id() {
722 cx.with_element_state(element_id, |element_state, cx| {
723 let element_state = draw_with_element_state(&mut root_view, element_state, cx);
724 ((), element_state)
725 });
726 } else {
727 draw_with_element_state(&mut root_view, None, cx);
728 };
729
730 cx.window.root_view = Some(root_view);
731 let scene = cx.window.scene_builder.build();
732 cx.end_frame();
733
734 cx.run_on_main(view, |_, cx| {
735 cx.window
736 .platform_window
737 .borrow_on_main_thread()
738 .draw(scene);
739 cx.window.dirty = false;
740 })
741 .detach();
742 });
743
744 fn draw_with_element_state(
745 root_view: &mut AnyView,
746 element_state: Option<AnyBox>,
747 cx: &mut ViewContext<()>,
748 ) -> AnyBox {
749 let mut element_state = root_view.initialize(&mut (), element_state, cx);
750 let layout_id = root_view.layout(&mut (), &mut element_state, cx);
751 let available_space = cx.window.content_size.map(Into::into);
752 cx.window
753 .layout_engine
754 .compute_layout(layout_id, available_space);
755 let bounds = cx.window.layout_engine.layout_bounds(layout_id);
756 root_view.paint(bounds, &mut (), &mut element_state, cx);
757 element_state
758 }
759 }
760
761 fn start_frame(&mut self) {
762 // Make the current element states the previous, and then clear the current.
763 // The empty element states map will be populated for any element states we
764 // reference during the upcoming frame.
765 let window = &mut *self.window;
766 mem::swap(&mut window.element_states, &mut window.prev_element_states);
767 window.element_states.clear();
768
769 // Clear mouse event listeners, because elements add new element listeners
770 // when the upcoming frame is painted.
771 window.mouse_listeners.values_mut().for_each(Vec::clear);
772
773 // Clear focus state, because we determine what is focused when the new elements
774 // in the upcoming frame are initialized.
775 window.focus_listeners.clear();
776 window.keyboard_listeners.values_mut().for_each(Vec::clear);
777 window.focus_parents_by_child.clear();
778 }
779
780 fn end_frame(&mut self) {
781 self.text_system().end_frame();
782 }
783
784 fn dispatch_event(&mut self, event: InputEvent) -> bool {
785 if let Some(any_mouse_event) = event.mouse_event() {
786 if let Some(MouseMoveEvent { position, .. }) = any_mouse_event.downcast_ref() {
787 self.window.mouse_position = *position;
788 }
789
790 if let Some(mut handlers) = self
791 .window
792 .mouse_listeners
793 .remove(&any_mouse_event.type_id())
794 {
795 // Because handlers may add other handlers, we sort every time.
796 handlers.sort_by(|(a, _), (b, _)| a.cmp(b));
797
798 // Handlers may set this to false by calling `stop_propagation`
799 self.window.propagate_event = true;
800
801 // Capture phase, events bubble from back to front. Handlers for this phase are used for
802 // special purposes, such as detecting events outside of a given Bounds.
803 for (_, handler) in &handlers {
804 handler(any_mouse_event, DispatchPhase::Capture, self);
805 if !self.window.propagate_event {
806 break;
807 }
808 }
809
810 // Bubble phase, where most normal handlers do their work.
811 if self.window.propagate_event {
812 for (_, handler) in handlers.iter().rev() {
813 handler(any_mouse_event, DispatchPhase::Bubble, self);
814 if !self.window.propagate_event {
815 break;
816 }
817 }
818 }
819
820 // Just in case any handlers added new handlers, which is weird, but possible.
821 handlers.extend(
822 self.window
823 .mouse_listeners
824 .get_mut(&any_mouse_event.type_id())
825 .into_iter()
826 .flat_map(|handlers| handlers.drain(..)),
827 );
828 self.window
829 .mouse_listeners
830 .insert(any_mouse_event.type_id(), handlers);
831 }
832 } else if let Some(any_keyboard_event) = event.keyboard_event() {
833 if let Some(mut handlers) = self
834 .window
835 .keyboard_listeners
836 .remove(&any_keyboard_event.type_id())
837 {
838 for handler in &handlers {
839 handler(any_keyboard_event, DispatchPhase::Capture, self);
840 if !self.window.propagate_event {
841 break;
842 }
843 }
844
845 if self.window.propagate_event {
846 for handler in handlers.iter().rev() {
847 handler(any_keyboard_event, DispatchPhase::Bubble, self);
848 if !self.window.propagate_event {
849 break;
850 }
851 }
852 }
853
854 handlers.extend(
855 self.window
856 .keyboard_listeners
857 .get_mut(&any_keyboard_event.type_id())
858 .into_iter()
859 .flat_map(|handlers| handlers.drain(..)),
860 );
861 self.window
862 .keyboard_listeners
863 .insert(any_keyboard_event.type_id(), handlers);
864 }
865 }
866
867 true
868 }
869}
870
871impl<'a, 'w> MainThread<WindowContext<'a, 'w>> {
872 fn platform(&self) -> &dyn Platform {
873 self.platform.borrow_on_main_thread()
874 }
875}
876
877impl Context for WindowContext<'_, '_> {
878 type EntityContext<'a, 'w, T: 'static + Send + Sync> = ViewContext<'a, 'w, T>;
879 type Result<T> = T;
880
881 fn entity<T: Send + Sync + 'static>(
882 &mut self,
883 build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T,
884 ) -> Handle<T> {
885 let slot = self.app.entities.reserve();
886 let entity = build_entity(&mut ViewContext::mutable(
887 &mut *self.app,
888 &mut self.window,
889 slot.id,
890 ));
891 self.entities.insert(slot, entity)
892 }
893
894 fn update_entity<T: Send + Sync + 'static, R>(
895 &mut self,
896 handle: &Handle<T>,
897 update: impl FnOnce(&mut T, &mut Self::EntityContext<'_, '_, T>) -> R,
898 ) -> R {
899 let mut entity = self.entities.lease(handle);
900 let result = update(
901 &mut *entity,
902 &mut ViewContext::mutable(&mut *self.app, &mut *self.window, handle.id),
903 );
904 self.entities.end_lease(entity);
905 result
906 }
907}
908
909impl<'a, 'w> std::ops::Deref for WindowContext<'a, 'w> {
910 type Target = AppContext;
911
912 fn deref(&self) -> &Self::Target {
913 &self.app
914 }
915}
916
917impl<'a, 'w> std::ops::DerefMut for WindowContext<'a, 'w> {
918 fn deref_mut(&mut self) -> &mut Self::Target {
919 &mut self.app
920 }
921}
922
923impl BorrowAppContext for WindowContext<'_, '_> {
924 fn app_mut(&mut self) -> &mut AppContext {
925 &mut *self.app
926 }
927}
928
929pub trait BorrowWindow: BorrowAppContext {
930 fn window(&self) -> &Window;
931 fn window_mut(&mut self) -> &mut Window;
932
933 fn with_element_id<R>(
934 &mut self,
935 id: impl Into<ElementId>,
936 f: impl FnOnce(&mut Self) -> R,
937 ) -> R {
938 self.window_mut().element_id_stack.push(id.into());
939 let result = f(self);
940 self.window_mut().element_id_stack.pop();
941 result
942 }
943
944 fn with_content_mask<R>(
945 &mut self,
946 mask: ContentMask<Pixels>,
947 f: impl FnOnce(&mut Self) -> R,
948 ) -> R {
949 let mask = mask.intersect(&self.content_mask());
950 self.window_mut().content_mask_stack.push(mask);
951 let result = f(self);
952 self.window_mut().content_mask_stack.pop();
953 result
954 }
955
956 fn with_element_state<S: 'static + Send + Sync, R>(
957 &mut self,
958 id: ElementId,
959 f: impl FnOnce(Option<S>, &mut Self) -> (R, S),
960 ) -> R {
961 self.with_element_id(id, |cx| {
962 let global_id = cx.window_mut().element_id_stack.clone();
963 if let Some(any) = cx
964 .window_mut()
965 .element_states
966 .remove(&global_id)
967 .or_else(|| cx.window_mut().prev_element_states.remove(&global_id))
968 {
969 // Using the extra inner option to avoid needing to reallocate a new box.
970 let mut state_box = any
971 .downcast::<Option<S>>()
972 .expect("invalid element state type for id");
973 let state = state_box
974 .take()
975 .expect("element state is already on the stack");
976 let (result, state) = f(Some(state), cx);
977 state_box.replace(state);
978 cx.window_mut().element_states.insert(global_id, state_box);
979 result
980 } else {
981 let (result, state) = f(None, cx);
982 cx.window_mut()
983 .element_states
984 .insert(global_id, Box::new(Some(state)));
985 result
986 }
987 })
988 }
989
990 fn content_mask(&self) -> ContentMask<Pixels> {
991 self.window()
992 .content_mask_stack
993 .last()
994 .cloned()
995 .unwrap_or_else(|| ContentMask {
996 bounds: Bounds {
997 origin: Point::default(),
998 size: self.window().content_size,
999 },
1000 })
1001 }
1002
1003 fn rem_size(&self) -> Pixels {
1004 self.window().rem_size
1005 }
1006}
1007
1008impl BorrowWindow for WindowContext<'_, '_> {
1009 fn window(&self) -> &Window {
1010 &*self.window
1011 }
1012
1013 fn window_mut(&mut self) -> &mut Window {
1014 &mut *self.window
1015 }
1016}
1017
1018pub struct ViewContext<'a, 'w, S> {
1019 window_cx: WindowContext<'a, 'w>,
1020 entity_type: PhantomData<S>,
1021 entity_id: EntityId,
1022}
1023
1024impl<S> BorrowAppContext for ViewContext<'_, '_, S> {
1025 fn app_mut(&mut self) -> &mut AppContext {
1026 &mut *self.window_cx.app
1027 }
1028}
1029
1030impl<S> BorrowWindow for ViewContext<'_, '_, S> {
1031 fn window(&self) -> &Window {
1032 &self.window_cx.window
1033 }
1034
1035 fn window_mut(&mut self) -> &mut Window {
1036 &mut *self.window_cx.window
1037 }
1038}
1039
1040impl<'a, 'w, V: Send + Sync + 'static> ViewContext<'a, 'w, V> {
1041 fn mutable(app: &'a mut AppContext, window: &'w mut Window, entity_id: EntityId) -> Self {
1042 Self {
1043 window_cx: WindowContext::mutable(app, window),
1044 entity_id,
1045 entity_type: PhantomData,
1046 }
1047 }
1048
1049 pub fn handle(&self) -> WeakHandle<V> {
1050 self.entities.weak_handle(self.entity_id)
1051 }
1052
1053 pub fn stack<R>(&mut self, order: u32, f: impl FnOnce(&mut Self) -> R) -> R {
1054 self.window.z_index_stack.push(order);
1055 let result = f(self);
1056 self.window.z_index_stack.pop();
1057 result
1058 }
1059
1060 pub fn on_next_frame(&mut self, f: impl FnOnce(&mut V, &mut ViewContext<V>) + Send + 'static) {
1061 let entity = self.handle();
1062 self.window_cx.on_next_frame(move |cx| {
1063 entity.update(cx, f).ok();
1064 });
1065 }
1066
1067 pub fn observe<E: Send + Sync + 'static>(
1068 &mut self,
1069 handle: &Handle<E>,
1070 on_notify: impl Fn(&mut V, Handle<E>, &mut ViewContext<'_, '_, V>) + Send + Sync + 'static,
1071 ) -> Subscription {
1072 let this = self.handle();
1073 let handle = handle.downgrade();
1074 let window_handle = self.window.handle;
1075 self.app.observers.insert(
1076 handle.id,
1077 Box::new(move |cx| {
1078 cx.update_window(window_handle.id, |cx| {
1079 if let Some(handle) = handle.upgrade(cx) {
1080 this.update(cx, |this, cx| on_notify(this, handle, cx))
1081 .is_ok()
1082 } else {
1083 false
1084 }
1085 })
1086 .unwrap_or(false)
1087 }),
1088 )
1089 }
1090
1091 pub fn subscribe<E: EventEmitter + Send + Sync + 'static>(
1092 &mut self,
1093 handle: &Handle<E>,
1094 on_event: impl Fn(&mut V, Handle<E>, &E::Event, &mut ViewContext<'_, '_, V>)
1095 + Send
1096 + Sync
1097 + 'static,
1098 ) -> Subscription {
1099 let this = self.handle();
1100 let handle = handle.downgrade();
1101 let window_handle = self.window.handle;
1102 self.app.event_handlers.insert(
1103 handle.id,
1104 Box::new(move |event, cx| {
1105 cx.update_window(window_handle.id, |cx| {
1106 if let Some(handle) = handle.upgrade(cx) {
1107 let event = event.downcast_ref().expect("invalid event type");
1108 this.update(cx, |this, cx| on_event(this, handle, event, cx))
1109 .is_ok()
1110 } else {
1111 false
1112 }
1113 })
1114 .unwrap_or(false)
1115 }),
1116 )
1117 }
1118
1119 pub fn on_release(
1120 &mut self,
1121 on_release: impl Fn(&mut V, &mut WindowContext) + Send + Sync + 'static,
1122 ) -> Subscription {
1123 let window_handle = self.window.handle;
1124 self.app.release_handlers.insert(
1125 self.entity_id,
1126 Box::new(move |this, cx| {
1127 let this = this.downcast_mut().expect("invalid entity type");
1128 // todo!("are we okay with silently swallowing the error?")
1129 let _ = cx.update_window(window_handle.id, |cx| on_release(this, cx));
1130 }),
1131 )
1132 }
1133
1134 pub fn observe_release<E: Send + Sync + 'static>(
1135 &mut self,
1136 handle: &Handle<E>,
1137 on_release: impl Fn(&mut V, &mut E, &mut ViewContext<'_, '_, V>) + Send + Sync + 'static,
1138 ) -> Subscription {
1139 let this = self.handle();
1140 let window_handle = self.window.handle;
1141 self.app.release_handlers.insert(
1142 handle.id,
1143 Box::new(move |entity, cx| {
1144 let entity = entity.downcast_mut().expect("invalid entity type");
1145 // todo!("are we okay with silently swallowing the error?")
1146 let _ = cx.update_window(window_handle.id, |cx| {
1147 this.update(cx, |this, cx| on_release(this, entity, cx))
1148 });
1149 }),
1150 )
1151 }
1152
1153 pub fn notify(&mut self) {
1154 self.window_cx.notify();
1155 self.window_cx.app.push_effect(Effect::Notify {
1156 emitter: self.entity_id,
1157 });
1158 }
1159
1160 pub fn with_focus<R>(
1161 &mut self,
1162 focus_handle: Option<FocusHandle>,
1163 key_down: impl IntoIterator<Item = KeyDownListener<V>>,
1164 key_up: impl IntoIterator<Item = KeyUpListener<V>>,
1165 focus: impl IntoIterator<Item = FocusListener<V>>,
1166 f: impl FnOnce(&mut Self) -> R,
1167 ) -> R {
1168 let Some(focus_handle) = focus_handle else {
1169 return f(self);
1170 };
1171
1172 let handle = self.handle();
1173 let window = &mut *self.window;
1174
1175 for listener in focus {
1176 let handle = handle.clone();
1177 window.focus_listeners.push(Box::new(move |event, cx| {
1178 handle
1179 .update(cx, |view, cx| listener(view, event, cx))
1180 .log_err();
1181 }));
1182 }
1183
1184 let mut focus_stack = mem::take(&mut window.focus_stack);
1185 if let Some(parent_frame) = focus_stack.last() {
1186 window
1187 .focus_parents_by_child
1188 .insert(focus_handle.id, parent_frame.handle.id);
1189 }
1190
1191 let mut frame = FocusStackFrame {
1192 handle: focus_handle.clone(),
1193 key_down_listeners: SmallVec::new(),
1194 key_up_listeners: SmallVec::new(),
1195 };
1196
1197 for listener in key_down {
1198 let handle = handle.clone();
1199 frame
1200 .key_down_listeners
1201 .push(Box::new(move |event, phase, cx| {
1202 handle
1203 .update(cx, |view, cx| listener(view, event, phase, cx))
1204 .log_err();
1205 }));
1206 }
1207 for listener in key_up {
1208 let handle = handle.clone();
1209 frame
1210 .key_up_listeners
1211 .push(Box::new(move |event, phase, cx| {
1212 handle
1213 .update(cx, |view, cx| listener(view, event, phase, cx))
1214 .log_err();
1215 }));
1216 }
1217 focus_stack.push(frame);
1218
1219 if Some(focus_handle.id) == window.focus {
1220 for focus_frame in &mut focus_stack {
1221 for listener in focus_frame.key_down_listeners.drain(..) {
1222 self.window_cx.on_keyboard_event(listener);
1223 }
1224 for listener in focus_frame.key_up_listeners.drain(..) {
1225 self.window_cx.on_keyboard_event(listener);
1226 }
1227 }
1228 }
1229
1230 self.window.focus_stack = focus_stack;
1231 let result = f(self);
1232 self.window.focus_stack.pop();
1233 result
1234 }
1235
1236 pub fn run_on_main<R>(
1237 &mut self,
1238 view: &mut V,
1239 f: impl FnOnce(&mut V, &mut MainThread<ViewContext<'_, '_, V>>) -> R + Send + 'static,
1240 ) -> Task<Result<R>>
1241 where
1242 R: Send + 'static,
1243 {
1244 if self.executor.is_main_thread() {
1245 let cx = unsafe { mem::transmute::<&mut Self, &mut MainThread<Self>>(self) };
1246 Task::ready(Ok(f(view, cx)))
1247 } else {
1248 let handle = self.handle().upgrade(self).unwrap();
1249 self.window_cx.run_on_main(move |cx| handle.update(cx, f))
1250 }
1251 }
1252
1253 pub fn spawn<Fut, R>(
1254 &mut self,
1255 f: impl FnOnce(WeakHandle<V>, AsyncWindowContext) -> Fut + Send + 'static,
1256 ) -> Task<R>
1257 where
1258 R: Send + 'static,
1259 Fut: Future<Output = R> + Send + 'static,
1260 {
1261 let handle = self.handle();
1262 self.window_cx.spawn(move |_, cx| {
1263 let result = f(handle, cx);
1264 async move { result.await }
1265 })
1266 }
1267
1268 pub fn on_mouse_event<Event: 'static>(
1269 &mut self,
1270 handler: impl Fn(&mut V, &Event, DispatchPhase, &mut ViewContext<V>) + Send + Sync + 'static,
1271 ) {
1272 let handle = self.handle().upgrade(self).unwrap();
1273 self.window_cx.on_mouse_event(move |event, phase, cx| {
1274 handle.update(cx, |view, cx| {
1275 handler(view, event, phase, cx);
1276 })
1277 });
1278 }
1279
1280 pub fn on_keyboard_event<Event: 'static>(
1281 &mut self,
1282 handler: impl Fn(&mut V, &Event, DispatchPhase, &mut ViewContext<V>) + Send + Sync + 'static,
1283 ) {
1284 let handle = self.handle().upgrade(self).unwrap();
1285 self.window_cx.on_keyboard_event(move |event, phase, cx| {
1286 handle.update(cx, |view, cx| {
1287 handler(view, event, phase, cx);
1288 })
1289 });
1290 }
1291}
1292
1293impl<'a, 'w, S: EventEmitter + Send + Sync + 'static> ViewContext<'a, 'w, S> {
1294 pub fn emit(&mut self, event: S::Event) {
1295 self.window_cx.app.push_effect(Effect::Emit {
1296 emitter: self.entity_id,
1297 event: Box::new(event),
1298 });
1299 }
1300}
1301
1302impl<'a, 'w, S> Context for ViewContext<'a, 'w, S> {
1303 type EntityContext<'b, 'c, U: 'static + Send + Sync> = ViewContext<'b, 'c, U>;
1304 type Result<U> = U;
1305
1306 fn entity<T2: Send + Sync + 'static>(
1307 &mut self,
1308 build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T2>) -> T2,
1309 ) -> Handle<T2> {
1310 self.window_cx.entity(build_entity)
1311 }
1312
1313 fn update_entity<U: Send + Sync + 'static, R>(
1314 &mut self,
1315 handle: &Handle<U>,
1316 update: impl FnOnce(&mut U, &mut Self::EntityContext<'_, '_, U>) -> R,
1317 ) -> R {
1318 self.window_cx.update_entity(handle, update)
1319 }
1320}
1321
1322impl<'a, 'w, S: 'static> std::ops::Deref for ViewContext<'a, 'w, S> {
1323 type Target = WindowContext<'a, 'w>;
1324
1325 fn deref(&self) -> &Self::Target {
1326 &self.window_cx
1327 }
1328}
1329
1330impl<'a, 'w, S: 'static> std::ops::DerefMut for ViewContext<'a, 'w, S> {
1331 fn deref_mut(&mut self) -> &mut Self::Target {
1332 &mut self.window_cx
1333 }
1334}
1335
1336// #[derive(Clone, Copy, Eq, PartialEq, Hash)]
1337slotmap::new_key_type! { pub struct WindowId; }
1338
1339#[derive(PartialEq, Eq)]
1340pub struct WindowHandle<S> {
1341 id: WindowId,
1342 state_type: PhantomData<S>,
1343}
1344
1345impl<S> Copy for WindowHandle<S> {}
1346
1347impl<S> Clone for WindowHandle<S> {
1348 fn clone(&self) -> Self {
1349 WindowHandle {
1350 id: self.id,
1351 state_type: PhantomData,
1352 }
1353 }
1354}
1355
1356impl<S> WindowHandle<S> {
1357 pub fn new(id: WindowId) -> Self {
1358 WindowHandle {
1359 id,
1360 state_type: PhantomData,
1361 }
1362 }
1363}
1364
1365impl<S: 'static> Into<AnyWindowHandle> for WindowHandle<S> {
1366 fn into(self) -> AnyWindowHandle {
1367 AnyWindowHandle {
1368 id: self.id,
1369 state_type: TypeId::of::<S>(),
1370 }
1371 }
1372}
1373
1374#[derive(Copy, Clone, PartialEq, Eq)]
1375pub struct AnyWindowHandle {
1376 pub(crate) id: WindowId,
1377 state_type: TypeId,
1378}
1379
1380#[cfg(any(test, feature = "test"))]
1381impl From<SmallVec<[u32; 16]>> for StackingOrder {
1382 fn from(small_vec: SmallVec<[u32; 16]>) -> Self {
1383 StackingOrder(small_vec)
1384 }
1385}
1386
1387#[derive(Clone, Debug, Eq, PartialEq, Hash)]
1388pub enum ElementId {
1389 View(EntityId),
1390 Number(usize),
1391 Name(SharedString),
1392}
1393
1394impl From<EntityId> for ElementId {
1395 fn from(id: EntityId) -> Self {
1396 ElementId::View(id)
1397 }
1398}
1399
1400impl From<usize> for ElementId {
1401 fn from(id: usize) -> Self {
1402 ElementId::Number(id)
1403 }
1404}
1405
1406impl From<i32> for ElementId {
1407 fn from(id: i32) -> Self {
1408 Self::Number(id as usize)
1409 }
1410}
1411
1412impl From<SharedString> for ElementId {
1413 fn from(name: SharedString) -> Self {
1414 ElementId::Name(name)
1415 }
1416}
1417
1418impl From<&'static str> for ElementId {
1419 fn from(name: &'static str) -> Self {
1420 ElementId::Name(name.into())
1421 }
1422}