1#[cfg(any(feature = "inspector", debug_assertions))]
2use crate::Inspector;
3use crate::{
4 Action, AnyDrag, AnyElement, AnyImageCache, AnyTooltip, AnyView, App, AppContext, Arena, Asset,
5 AsyncWindowContext, AvailableSpace, Background, BorderStyle, Bounds, BoxShadow, Capslock,
6 Context, Corners, CursorStyle, Decorations, DevicePixels, DispatchActionListener,
7 DispatchNodeId, DispatchTree, DisplayId, Edges, Effect, Entity, EntityId, EventEmitter,
8 FileDropEvent, FontId, Global, GlobalElementId, GlyphId, GpuSpecs, Hsla, InputHandler, IsZero,
9 KeyBinding, KeyContext, KeyDownEvent, KeyEvent, Keystroke, KeystrokeEvent, LayoutId,
10 LineLayoutIndex, Modifiers, ModifiersChangedEvent, MonochromeSprite, MouseButton, MouseEvent,
11 MouseMoveEvent, MouseUpEvent, Path, Pixels, PlatformAtlas, PlatformDisplay, PlatformInput,
12 PlatformInputHandler, PlatformWindow, Point, PolychromeSprite, Priority, PromptButton,
13 PromptLevel, Quad, Render, RenderGlyphParams, RenderImage, RenderImageParams, RenderSvgParams,
14 Replay, ResizeEdge, SMOOTH_SVG_SCALE_FACTOR, SUBPIXEL_VARIANTS_X, ScaledPixels, Scene, Shadow,
15 SharedString, Size, StrikethroughStyle, Style, SubpixelSprite, SubscriberSet, Subscription,
16 SystemWindowTab, SystemWindowTabController, TabStopMap, TaffyLayoutEngine, Task,
17 TextRenderingMode, TextStyle, TextStyleRefinement, ThermalState, TransformationMatrix,
18 Underline, UnderlineStyle, WindowAppearance, WindowBackgroundAppearance, WindowBounds,
19 WindowControls, WindowDecorations, WindowOptions, WindowParams, WindowTextSystem, point,
20 prelude::*, px, rems, size, transparent_black,
21};
22use anyhow::{Context as _, Result, anyhow};
23use collections::{FxHashMap, FxHashSet};
24#[cfg(target_os = "macos")]
25use core_video::pixel_buffer::CVPixelBuffer;
26use derive_more::{Deref, DerefMut};
27use futures::FutureExt;
28use futures::channel::oneshot;
29use gpui_util::post_inc;
30use gpui_util::{ResultExt, measure};
31use itertools::FoldWhile::{Continue, Done};
32use itertools::Itertools;
33use parking_lot::RwLock;
34use raw_window_handle::{HandleError, HasDisplayHandle, HasWindowHandle};
35use refineable::Refineable;
36use scheduler::Instant;
37use slotmap::SlotMap;
38use smallvec::SmallVec;
39use std::{
40 any::{Any, TypeId},
41 borrow::Cow,
42 cell::{Cell, RefCell},
43 cmp,
44 fmt::{Debug, Display},
45 hash::{Hash, Hasher},
46 marker::PhantomData,
47 mem,
48 ops::{DerefMut, Range},
49 rc::Rc,
50 sync::{
51 Arc, Weak,
52 atomic::{AtomicUsize, Ordering::SeqCst},
53 },
54 time::Duration,
55};
56use uuid::Uuid;
57
58mod prompts;
59
60use crate::util::{atomic_incr_if_not_zero, round_half_toward_zero, round_half_toward_zero_f64};
61pub use prompts::*;
62
63/// Default window size used when no explicit size is provided.
64pub const DEFAULT_WINDOW_SIZE: Size<Pixels> = size(px(1536.), px(864.));
65
66/// A 6:5 aspect ratio minimum window size to be used for functional,
67/// additional-to-main-Zed windows, like the settings and rules library windows.
68pub const DEFAULT_ADDITIONAL_WINDOW_SIZE: Size<Pixels> = Size {
69 width: Pixels(900.),
70 height: Pixels(750.),
71};
72
73/// Represents the two different phases when dispatching events.
74#[derive(Default, Copy, Clone, Debug, Eq, PartialEq)]
75pub enum DispatchPhase {
76 /// After the capture phase comes the bubble phase, in which mouse event listeners are
77 /// invoked front to back and keyboard event listeners are invoked from the focused element
78 /// to the root of the element tree. This is the phase you'll most commonly want to use when
79 /// registering event listeners.
80 #[default]
81 Bubble,
82 /// During the initial capture phase, mouse event listeners are invoked back to front, and keyboard
83 /// listeners are invoked from the root of the tree downward toward the focused element. This phase
84 /// is used for special purposes such as clearing the "pressed" state for click events. If
85 /// you stop event propagation during this phase, you need to know what you're doing. Handlers
86 /// outside of the immediate region may rely on detecting non-local events during this phase.
87 Capture,
88}
89
90impl DispatchPhase {
91 /// Returns true if this represents the "bubble" phase.
92 #[inline]
93 pub fn bubble(self) -> bool {
94 self == DispatchPhase::Bubble
95 }
96
97 /// Returns true if this represents the "capture" phase.
98 #[inline]
99 pub fn capture(self) -> bool {
100 self == DispatchPhase::Capture
101 }
102}
103
104struct WindowInvalidatorInner {
105 pub dirty: bool,
106 pub draw_phase: DrawPhase,
107 pub dirty_views: FxHashSet<EntityId>,
108}
109
110#[derive(Clone)]
111pub(crate) struct WindowInvalidator {
112 inner: Rc<RefCell<WindowInvalidatorInner>>,
113}
114
115impl WindowInvalidator {
116 pub fn new() -> Self {
117 WindowInvalidator {
118 inner: Rc::new(RefCell::new(WindowInvalidatorInner {
119 dirty: true,
120 draw_phase: DrawPhase::None,
121 dirty_views: FxHashSet::default(),
122 })),
123 }
124 }
125
126 pub fn invalidate_view(&self, entity: EntityId, cx: &mut App) -> bool {
127 let mut inner = self.inner.borrow_mut();
128 inner.dirty_views.insert(entity);
129 if inner.draw_phase == DrawPhase::None {
130 inner.dirty = true;
131 cx.push_effect(Effect::Notify { emitter: entity });
132 true
133 } else {
134 false
135 }
136 }
137
138 pub fn is_dirty(&self) -> bool {
139 self.inner.borrow().dirty
140 }
141
142 pub fn set_dirty(&self, dirty: bool) {
143 self.inner.borrow_mut().dirty = dirty
144 }
145
146 pub fn set_phase(&self, phase: DrawPhase) {
147 self.inner.borrow_mut().draw_phase = phase
148 }
149
150 pub fn take_views(&self) -> FxHashSet<EntityId> {
151 mem::take(&mut self.inner.borrow_mut().dirty_views)
152 }
153
154 pub fn replace_views(&self, views: FxHashSet<EntityId>) {
155 self.inner.borrow_mut().dirty_views = views;
156 }
157
158 pub fn not_drawing(&self) -> bool {
159 self.inner.borrow().draw_phase == DrawPhase::None
160 }
161
162 #[track_caller]
163 pub fn debug_assert_paint(&self) {
164 debug_assert!(
165 matches!(self.inner.borrow().draw_phase, DrawPhase::Paint),
166 "this method can only be called during paint"
167 );
168 }
169
170 #[track_caller]
171 pub fn debug_assert_prepaint(&self) {
172 debug_assert!(
173 matches!(self.inner.borrow().draw_phase, DrawPhase::Prepaint),
174 "this method can only be called during request_layout, or prepaint"
175 );
176 }
177
178 #[track_caller]
179 pub fn debug_assert_paint_or_prepaint(&self) {
180 debug_assert!(
181 matches!(
182 self.inner.borrow().draw_phase,
183 DrawPhase::Paint | DrawPhase::Prepaint
184 ),
185 "this method can only be called during request_layout, prepaint, or paint"
186 );
187 }
188}
189
190type AnyObserver = Box<dyn FnMut(&mut Window, &mut App) -> bool + 'static>;
191
192pub(crate) type AnyWindowFocusListener =
193 Box<dyn FnMut(&WindowFocusEvent, &mut Window, &mut App) -> bool + 'static>;
194
195pub(crate) struct WindowFocusEvent {
196 pub(crate) previous_focus_path: SmallVec<[FocusId; 8]>,
197 pub(crate) current_focus_path: SmallVec<[FocusId; 8]>,
198}
199
200impl WindowFocusEvent {
201 pub fn is_focus_in(&self, focus_id: FocusId) -> bool {
202 !self.previous_focus_path.contains(&focus_id) && self.current_focus_path.contains(&focus_id)
203 }
204
205 pub fn is_focus_out(&self, focus_id: FocusId) -> bool {
206 self.previous_focus_path.contains(&focus_id) && !self.current_focus_path.contains(&focus_id)
207 }
208}
209
210/// This is provided when subscribing for `Context::on_focus_out` events.
211pub struct FocusOutEvent {
212 /// A weak focus handle representing what was blurred.
213 pub blurred: WeakFocusHandle,
214}
215
216slotmap::new_key_type! {
217 /// A globally unique identifier for a focusable element.
218 pub struct FocusId;
219}
220
221thread_local! {
222 /// Fallback arena used when no app-specific arena is active.
223 /// In production, each window draw sets CURRENT_ELEMENT_ARENA to the app's arena.
224 pub(crate) static ELEMENT_ARENA: RefCell<Arena> = RefCell::new(Arena::new(1024 * 1024));
225
226 /// Points to the current App's element arena during draw operations.
227 /// This allows multiple test Apps to have isolated arenas, preventing
228 /// cross-session corruption when the scheduler interleaves their tasks.
229 static CURRENT_ELEMENT_ARENA: Cell<Option<*const RefCell<Arena>>> = const { Cell::new(None) };
230}
231
232/// Allocates an element in the current arena. Uses the app-specific arena if one
233/// is active (during draw), otherwise falls back to the thread-local ELEMENT_ARENA.
234pub(crate) fn with_element_arena<R>(f: impl FnOnce(&mut Arena) -> R) -> R {
235 CURRENT_ELEMENT_ARENA.with(|current| {
236 if let Some(arena_ptr) = current.get() {
237 // SAFETY: The pointer is valid for the duration of the draw operation
238 // that set it, and we're being called during that same draw.
239 let arena_cell = unsafe { &*arena_ptr };
240 f(&mut arena_cell.borrow_mut())
241 } else {
242 ELEMENT_ARENA.with_borrow_mut(f)
243 }
244 })
245}
246
247/// RAII guard that sets CURRENT_ELEMENT_ARENA for the duration of a draw operation.
248/// When dropped, restores the previous arena (supporting nested draws).
249pub(crate) struct ElementArenaScope {
250 previous: Option<*const RefCell<Arena>>,
251}
252
253impl ElementArenaScope {
254 /// Enter a scope where element allocations use the given arena.
255 pub(crate) fn enter(arena: &RefCell<Arena>) -> Self {
256 let previous = CURRENT_ELEMENT_ARENA.with(|current| {
257 let prev = current.get();
258 current.set(Some(arena as *const RefCell<Arena>));
259 prev
260 });
261 Self { previous }
262 }
263}
264
265impl Drop for ElementArenaScope {
266 fn drop(&mut self) {
267 CURRENT_ELEMENT_ARENA.with(|current| {
268 current.set(self.previous);
269 });
270 }
271}
272
273/// Returned when the element arena has been used and so must be cleared before the next draw.
274#[must_use]
275pub struct ArenaClearNeeded {
276 arena: *const RefCell<Arena>,
277}
278
279impl ArenaClearNeeded {
280 /// Create a new ArenaClearNeeded that will clear the given arena.
281 pub(crate) fn new(arena: &RefCell<Arena>) -> Self {
282 Self {
283 arena: arena as *const RefCell<Arena>,
284 }
285 }
286
287 /// Clear the element arena.
288 pub fn clear(self) {
289 // SAFETY: The arena pointer is valid because ArenaClearNeeded is created
290 // at the end of draw() and must be cleared before the next draw.
291 let arena_cell = unsafe { &*self.arena };
292 arena_cell.borrow_mut().clear();
293 }
294}
295
296pub(crate) type FocusMap = RwLock<SlotMap<FocusId, FocusRef>>;
297pub(crate) struct FocusRef {
298 pub(crate) ref_count: AtomicUsize,
299 pub(crate) tab_index: isize,
300 pub(crate) tab_stop: bool,
301}
302
303impl FocusId {
304 /// Obtains whether the element associated with this handle is currently focused.
305 pub fn is_focused(&self, window: &Window) -> bool {
306 window.focus == Some(*self)
307 }
308
309 /// Obtains whether the element associated with this handle contains the focused
310 /// element or is itself focused.
311 pub fn contains_focused(&self, window: &Window, cx: &App) -> bool {
312 window
313 .focused(cx)
314 .is_some_and(|focused| self.contains(focused.id, window))
315 }
316
317 /// Obtains whether the element associated with this handle is contained within the
318 /// focused element or is itself focused.
319 pub fn within_focused(&self, window: &Window, cx: &App) -> bool {
320 let focused = window.focused(cx);
321 focused.is_some_and(|focused| focused.id.contains(*self, window))
322 }
323
324 /// Obtains whether this handle contains the given handle in the most recently rendered frame.
325 pub(crate) fn contains(&self, other: Self, window: &Window) -> bool {
326 window
327 .rendered_frame
328 .dispatch_tree
329 .focus_contains(*self, other)
330 }
331}
332
333/// A handle which can be used to track and manipulate the focused element in a window.
334pub struct FocusHandle {
335 pub(crate) id: FocusId,
336 handles: Arc<FocusMap>,
337 /// The index of this element in the tab order.
338 pub tab_index: isize,
339 /// Whether this element can be focused by tab navigation.
340 pub tab_stop: bool,
341}
342
343impl std::fmt::Debug for FocusHandle {
344 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
345 f.write_fmt(format_args!("FocusHandle({:?})", self.id))
346 }
347}
348
349impl FocusHandle {
350 pub(crate) fn new(handles: &Arc<FocusMap>) -> Self {
351 let id = handles.write().insert(FocusRef {
352 ref_count: AtomicUsize::new(1),
353 tab_index: 0,
354 tab_stop: false,
355 });
356
357 Self {
358 id,
359 tab_index: 0,
360 tab_stop: false,
361 handles: handles.clone(),
362 }
363 }
364
365 pub(crate) fn for_id(id: FocusId, handles: &Arc<FocusMap>) -> Option<Self> {
366 let lock = handles.read();
367 let focus = lock.get(id)?;
368 if atomic_incr_if_not_zero(&focus.ref_count) == 0 {
369 return None;
370 }
371 Some(Self {
372 id,
373 tab_index: focus.tab_index,
374 tab_stop: focus.tab_stop,
375 handles: handles.clone(),
376 })
377 }
378
379 /// Sets the tab index of the element associated with this handle.
380 pub fn tab_index(mut self, index: isize) -> Self {
381 self.tab_index = index;
382 if let Some(focus) = self.handles.write().get_mut(self.id) {
383 focus.tab_index = index;
384 }
385 self
386 }
387
388 /// Sets whether the element associated with this handle is a tab stop.
389 ///
390 /// When `false`, the element will not be included in the tab order.
391 pub fn tab_stop(mut self, tab_stop: bool) -> Self {
392 self.tab_stop = tab_stop;
393 if let Some(focus) = self.handles.write().get_mut(self.id) {
394 focus.tab_stop = tab_stop;
395 }
396 self
397 }
398
399 /// Converts this focus handle into a weak variant, which does not prevent it from being released.
400 pub fn downgrade(&self) -> WeakFocusHandle {
401 WeakFocusHandle {
402 id: self.id,
403 handles: Arc::downgrade(&self.handles),
404 }
405 }
406
407 /// Moves the focus to the element associated with this handle.
408 pub fn focus(&self, window: &mut Window, cx: &mut App) {
409 window.focus(self, cx)
410 }
411
412 /// Obtains whether the element associated with this handle is currently focused.
413 pub fn is_focused(&self, window: &Window) -> bool {
414 self.id.is_focused(window)
415 }
416
417 /// Obtains whether the element associated with this handle contains the focused
418 /// element or is itself focused.
419 pub fn contains_focused(&self, window: &Window, cx: &App) -> bool {
420 self.id.contains_focused(window, cx)
421 }
422
423 /// Obtains whether the element associated with this handle is contained within the
424 /// focused element or is itself focused.
425 pub fn within_focused(&self, window: &Window, cx: &mut App) -> bool {
426 self.id.within_focused(window, cx)
427 }
428
429 /// Obtains whether this handle contains the given handle in the most recently rendered frame.
430 pub fn contains(&self, other: &Self, window: &Window) -> bool {
431 self.id.contains(other.id, window)
432 }
433
434 /// Dispatch an action on the element that rendered this focus handle
435 pub fn dispatch_action(&self, action: &dyn Action, window: &mut Window, cx: &mut App) {
436 if let Some(node_id) = window
437 .rendered_frame
438 .dispatch_tree
439 .focusable_node_id(self.id)
440 {
441 window.dispatch_action_on_node(node_id, action, cx)
442 }
443 }
444}
445
446impl Clone for FocusHandle {
447 fn clone(&self) -> Self {
448 Self::for_id(self.id, &self.handles).unwrap()
449 }
450}
451
452impl PartialEq for FocusHandle {
453 fn eq(&self, other: &Self) -> bool {
454 self.id == other.id
455 }
456}
457
458impl Eq for FocusHandle {}
459
460impl Drop for FocusHandle {
461 fn drop(&mut self) {
462 self.handles
463 .read()
464 .get(self.id)
465 .unwrap()
466 .ref_count
467 .fetch_sub(1, SeqCst);
468 }
469}
470
471/// A weak reference to a focus handle.
472#[derive(Clone, Debug)]
473pub struct WeakFocusHandle {
474 pub(crate) id: FocusId,
475 pub(crate) handles: Weak<FocusMap>,
476}
477
478impl WeakFocusHandle {
479 /// Attempts to upgrade the [WeakFocusHandle] to a [FocusHandle].
480 pub fn upgrade(&self) -> Option<FocusHandle> {
481 let handles = self.handles.upgrade()?;
482 FocusHandle::for_id(self.id, &handles)
483 }
484}
485
486impl PartialEq for WeakFocusHandle {
487 fn eq(&self, other: &WeakFocusHandle) -> bool {
488 self.id == other.id
489 }
490}
491
492impl Eq for WeakFocusHandle {}
493
494impl PartialEq<FocusHandle> for WeakFocusHandle {
495 fn eq(&self, other: &FocusHandle) -> bool {
496 self.id == other.id
497 }
498}
499
500impl PartialEq<WeakFocusHandle> for FocusHandle {
501 fn eq(&self, other: &WeakFocusHandle) -> bool {
502 self.id == other.id
503 }
504}
505
506/// Focusable allows users of your view to easily
507/// focus it (using window.focus_view(cx, view))
508pub trait Focusable: 'static {
509 /// Returns the focus handle associated with this view.
510 fn focus_handle(&self, cx: &App) -> FocusHandle;
511}
512
513impl<V: Focusable> Focusable for Entity<V> {
514 fn focus_handle(&self, cx: &App) -> FocusHandle {
515 self.read(cx).focus_handle(cx)
516 }
517}
518
519/// ManagedView is a view (like a Modal, Popover, Menu, etc.)
520/// where the lifecycle of the view is handled by another view.
521pub trait ManagedView: Focusable + EventEmitter<DismissEvent> + Render {}
522
523impl<M: Focusable + EventEmitter<DismissEvent> + Render> ManagedView for M {}
524
525/// Emitted by implementers of [`ManagedView`] to indicate the view should be dismissed, such as when a view is presented as a modal.
526pub struct DismissEvent;
527
528type FrameCallback = Box<dyn FnOnce(&mut Window, &mut App)>;
529
530pub(crate) type AnyMouseListener =
531 Box<dyn FnMut(&dyn Any, DispatchPhase, &mut Window, &mut App) + 'static>;
532
533#[derive(Clone)]
534pub(crate) struct CursorStyleRequest {
535 pub(crate) hitbox_id: Option<HitboxId>,
536 pub(crate) style: CursorStyle,
537}
538
539#[derive(Default, Eq, PartialEq)]
540pub(crate) struct HitTest {
541 pub(crate) ids: SmallVec<[HitboxId; 8]>,
542 pub(crate) hover_hitbox_count: usize,
543}
544
545/// A type of window control area that corresponds to the platform window.
546#[derive(Clone, Copy, Debug, Eq, PartialEq)]
547pub enum WindowControlArea {
548 /// An area that allows dragging of the platform window.
549 Drag,
550 /// An area that allows closing of the platform window.
551 Close,
552 /// An area that allows maximizing of the platform window.
553 Max,
554 /// An area that allows minimizing of the platform window.
555 Min,
556}
557
558/// An identifier for a [Hitbox] which also includes [HitboxBehavior].
559#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
560pub struct HitboxId(u64);
561
562impl HitboxId {
563 /// Checks if the hitbox with this ID is currently hovered. Except when handling
564 /// `ScrollWheelEvent`, this is typically what you want when determining whether to handle mouse
565 /// events or paint hover styles.
566 ///
567 /// See [`Hitbox::is_hovered`] for details.
568 pub fn is_hovered(self, window: &Window) -> bool {
569 let hit_test = &window.mouse_hit_test;
570 for id in hit_test.ids.iter().take(hit_test.hover_hitbox_count) {
571 if self == *id {
572 return true;
573 }
574 }
575 false
576 }
577
578 /// Checks if the hitbox with this ID contains the mouse and should handle scroll events.
579 /// Typically this should only be used when handling `ScrollWheelEvent`, and otherwise
580 /// `is_hovered` should be used. See the documentation of `Hitbox::is_hovered` for details about
581 /// this distinction.
582 pub fn should_handle_scroll(self, window: &Window) -> bool {
583 window.mouse_hit_test.ids.contains(&self)
584 }
585
586 fn next(mut self) -> HitboxId {
587 HitboxId(self.0.wrapping_add(1))
588 }
589}
590
591/// A rectangular region that potentially blocks hitboxes inserted prior.
592/// See [Window::insert_hitbox] for more details.
593#[derive(Clone, Debug, Deref)]
594pub struct Hitbox {
595 /// A unique identifier for the hitbox.
596 pub id: HitboxId,
597 /// The bounds of the hitbox.
598 #[deref]
599 pub bounds: Bounds<Pixels>,
600 /// The content mask when the hitbox was inserted.
601 pub content_mask: ContentMask<Pixels>,
602 /// Flags that specify hitbox behavior.
603 pub behavior: HitboxBehavior,
604}
605
606impl Hitbox {
607 /// Checks if the hitbox is currently hovered. Except when handling `ScrollWheelEvent`, this is
608 /// typically what you want when determining whether to handle mouse events or paint hover
609 /// styles.
610 ///
611 /// This can return `false` even when the hitbox contains the mouse, if a hitbox in front of
612 /// this sets `HitboxBehavior::BlockMouse` (`InteractiveElement::occlude`) or
613 /// `HitboxBehavior::BlockMouseExceptScroll` (`InteractiveElement::block_mouse_except_scroll`).
614 ///
615 /// Handling of `ScrollWheelEvent` should typically use `should_handle_scroll` instead.
616 /// Concretely, this is due to use-cases like overlays that cause the elements under to be
617 /// non-interactive while still allowing scrolling. More abstractly, this is because
618 /// `is_hovered` is about element interactions directly under the mouse - mouse moves, clicks,
619 /// hover styling, etc. In contrast, scrolling is about finding the current outer scrollable
620 /// container.
621 pub fn is_hovered(&self, window: &Window) -> bool {
622 self.id.is_hovered(window)
623 }
624
625 /// Checks if the hitbox contains the mouse and should handle scroll events. Typically this
626 /// should only be used when handling `ScrollWheelEvent`, and otherwise `is_hovered` should be
627 /// used. See the documentation of `Hitbox::is_hovered` for details about this distinction.
628 ///
629 /// This can return `false` even when the hitbox contains the mouse, if a hitbox in front of
630 /// this sets `HitboxBehavior::BlockMouse` (`InteractiveElement::occlude`).
631 pub fn should_handle_scroll(&self, window: &Window) -> bool {
632 self.id.should_handle_scroll(window)
633 }
634}
635
636/// How the hitbox affects mouse behavior.
637#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
638pub enum HitboxBehavior {
639 /// Normal hitbox mouse behavior, doesn't affect mouse handling for other hitboxes.
640 #[default]
641 Normal,
642
643 /// All hitboxes behind this hitbox will be ignored and so will have `hitbox.is_hovered() ==
644 /// false` and `hitbox.should_handle_scroll() == false`. Typically for elements this causes
645 /// skipping of all mouse events, hover styles, and tooltips. This flag is set by
646 /// [`InteractiveElement::occlude`].
647 ///
648 /// For mouse handlers that check those hitboxes, this behaves the same as registering a
649 /// bubble-phase handler for every mouse event type:
650 ///
651 /// ```ignore
652 /// window.on_mouse_event(move |_: &EveryMouseEventTypeHere, phase, window, cx| {
653 /// if phase == DispatchPhase::Capture && hitbox.is_hovered(window) {
654 /// cx.stop_propagation();
655 /// }
656 /// })
657 /// ```
658 ///
659 /// This has effects beyond event handling - any use of hitbox checking, such as hover
660 /// styles and tooltips. These other behaviors are the main point of this mechanism. An
661 /// alternative might be to not affect mouse event handling - but this would allow
662 /// inconsistent UI where clicks and moves interact with elements that are not considered to
663 /// be hovered.
664 BlockMouse,
665
666 /// All hitboxes behind this hitbox will have `hitbox.is_hovered() == false`, even when
667 /// `hitbox.should_handle_scroll() == true`. Typically for elements this causes all mouse
668 /// interaction except scroll events to be ignored - see the documentation of
669 /// [`Hitbox::is_hovered`] for details. This flag is set by
670 /// [`InteractiveElement::block_mouse_except_scroll`].
671 ///
672 /// For mouse handlers that check those hitboxes, this behaves the same as registering a
673 /// bubble-phase handler for every mouse event type **except** `ScrollWheelEvent`:
674 ///
675 /// ```ignore
676 /// window.on_mouse_event(move |_: &EveryMouseEventTypeExceptScroll, phase, window, cx| {
677 /// if phase == DispatchPhase::Bubble && hitbox.should_handle_scroll(window) {
678 /// cx.stop_propagation();
679 /// }
680 /// })
681 /// ```
682 ///
683 /// See the documentation of [`Hitbox::is_hovered`] for details of why `ScrollWheelEvent` is
684 /// handled differently than other mouse events. If also blocking these scroll events is
685 /// desired, then a `cx.stop_propagation()` handler like the one above can be used.
686 ///
687 /// This has effects beyond event handling - this affects any use of `is_hovered`, such as
688 /// hover styles and tooltips. These other behaviors are the main point of this mechanism.
689 /// An alternative might be to not affect mouse event handling - but this would allow
690 /// inconsistent UI where clicks and moves interact with elements that are not considered to
691 /// be hovered.
692 BlockMouseExceptScroll,
693}
694
695/// An identifier for a tooltip.
696#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
697pub struct TooltipId(usize);
698
699impl TooltipId {
700 /// Checks if the tooltip is currently hovered.
701 pub fn is_hovered(&self, window: &Window) -> bool {
702 window
703 .tooltip_bounds
704 .as_ref()
705 .is_some_and(|tooltip_bounds| {
706 tooltip_bounds.id == *self
707 && tooltip_bounds.bounds.contains(&window.mouse_position())
708 })
709 }
710}
711
712pub(crate) struct TooltipBounds {
713 id: TooltipId,
714 bounds: Bounds<Pixels>,
715}
716
717#[derive(Clone)]
718pub(crate) struct TooltipRequest {
719 id: TooltipId,
720 tooltip: AnyTooltip,
721}
722
723pub(crate) struct DeferredDraw {
724 current_view: EntityId,
725 priority: usize,
726 parent_node: DispatchNodeId,
727 element_id_stack: SmallVec<[ElementId; 32]>,
728 text_style_stack: Vec<TextStyleRefinement>,
729 content_mask: Option<ContentMask<Pixels>>,
730 rem_size: Pixels,
731 element: Option<AnyElement>,
732 absolute_offset: Point<Pixels>,
733 prepaint_range: Range<PrepaintStateIndex>,
734 paint_range: Range<PaintIndex>,
735}
736
737pub(crate) struct Frame {
738 pub(crate) focus: Option<FocusId>,
739 pub(crate) window_active: bool,
740 pub(crate) element_states: FxHashMap<(GlobalElementId, TypeId), ElementStateBox>,
741 accessed_element_states: Vec<(GlobalElementId, TypeId)>,
742 pub(crate) mouse_listeners: Vec<Option<AnyMouseListener>>,
743 pub(crate) dispatch_tree: DispatchTree,
744 pub(crate) scene: Scene,
745 pub(crate) hitboxes: Vec<Hitbox>,
746 pub(crate) window_control_hitboxes: Vec<(WindowControlArea, Hitbox)>,
747 pub(crate) deferred_draws: Vec<DeferredDraw>,
748 pub(crate) input_handlers: Vec<Option<PlatformInputHandler>>,
749 pub(crate) tooltip_requests: Vec<Option<TooltipRequest>>,
750 pub(crate) cursor_styles: Vec<CursorStyleRequest>,
751 #[cfg(any(test, feature = "test-support"))]
752 pub(crate) debug_bounds: FxHashMap<String, Bounds<Pixels>>,
753 #[cfg(any(feature = "inspector", debug_assertions))]
754 pub(crate) next_inspector_instance_ids: FxHashMap<Rc<crate::InspectorElementPath>, usize>,
755 #[cfg(any(feature = "inspector", debug_assertions))]
756 pub(crate) inspector_hitboxes: FxHashMap<HitboxId, crate::InspectorElementId>,
757 pub(crate) tab_stops: TabStopMap,
758}
759
760#[derive(Clone, Default)]
761pub(crate) struct PrepaintStateIndex {
762 hitboxes_index: usize,
763 tooltips_index: usize,
764 deferred_draws_index: usize,
765 dispatch_tree_index: usize,
766 accessed_element_states_index: usize,
767 line_layout_index: LineLayoutIndex,
768}
769
770#[derive(Clone, Default)]
771pub(crate) struct PaintIndex {
772 scene_index: usize,
773 mouse_listeners_index: usize,
774 input_handlers_index: usize,
775 cursor_styles_index: usize,
776 accessed_element_states_index: usize,
777 tab_handle_index: usize,
778 line_layout_index: LineLayoutIndex,
779}
780
781impl Frame {
782 pub(crate) fn new(dispatch_tree: DispatchTree) -> Self {
783 Frame {
784 focus: None,
785 window_active: false,
786 element_states: FxHashMap::default(),
787 accessed_element_states: Vec::new(),
788 mouse_listeners: Vec::new(),
789 dispatch_tree,
790 scene: Scene::default(),
791 hitboxes: Vec::new(),
792 window_control_hitboxes: Vec::new(),
793 deferred_draws: Vec::new(),
794 input_handlers: Vec::new(),
795 tooltip_requests: Vec::new(),
796 cursor_styles: Vec::new(),
797
798 #[cfg(any(test, feature = "test-support"))]
799 debug_bounds: FxHashMap::default(),
800
801 #[cfg(any(feature = "inspector", debug_assertions))]
802 next_inspector_instance_ids: FxHashMap::default(),
803
804 #[cfg(any(feature = "inspector", debug_assertions))]
805 inspector_hitboxes: FxHashMap::default(),
806 tab_stops: TabStopMap::default(),
807 }
808 }
809
810 pub(crate) fn clear(&mut self) {
811 self.element_states.clear();
812 self.accessed_element_states.clear();
813 self.mouse_listeners.clear();
814 self.dispatch_tree.clear();
815 self.scene.clear();
816 self.input_handlers.clear();
817 self.tooltip_requests.clear();
818 self.cursor_styles.clear();
819 self.hitboxes.clear();
820 self.window_control_hitboxes.clear();
821 self.deferred_draws.clear();
822 self.tab_stops.clear();
823 self.focus = None;
824
825 #[cfg(any(feature = "inspector", debug_assertions))]
826 {
827 self.next_inspector_instance_ids.clear();
828 self.inspector_hitboxes.clear();
829 }
830 }
831
832 pub(crate) fn cursor_style(&self, window: &Window) -> Option<CursorStyle> {
833 self.cursor_styles
834 .iter()
835 .rev()
836 .fold_while(None, |style, request| match request.hitbox_id {
837 None => Done(Some(request.style)),
838 Some(hitbox_id) => Continue(
839 style.or_else(|| hitbox_id.is_hovered(window).then_some(request.style)),
840 ),
841 })
842 .into_inner()
843 }
844
845 pub(crate) fn hit_test(&self, position: Point<Pixels>) -> HitTest {
846 let mut set_hover_hitbox_count = false;
847 let mut hit_test = HitTest::default();
848 for hitbox in self.hitboxes.iter().rev() {
849 let bounds = hitbox.bounds.intersect(&hitbox.content_mask.bounds);
850 if bounds.contains(&position) {
851 hit_test.ids.push(hitbox.id);
852 if !set_hover_hitbox_count
853 && hitbox.behavior == HitboxBehavior::BlockMouseExceptScroll
854 {
855 hit_test.hover_hitbox_count = hit_test.ids.len();
856 set_hover_hitbox_count = true;
857 }
858 if hitbox.behavior == HitboxBehavior::BlockMouse {
859 break;
860 }
861 }
862 }
863 if !set_hover_hitbox_count {
864 hit_test.hover_hitbox_count = hit_test.ids.len();
865 }
866 hit_test
867 }
868
869 pub(crate) fn focus_path(&self) -> SmallVec<[FocusId; 8]> {
870 self.focus
871 .map(|focus_id| self.dispatch_tree.focus_path(focus_id))
872 .unwrap_or_default()
873 }
874
875 pub(crate) fn finish(&mut self, prev_frame: &mut Self) {
876 for element_state_key in &self.accessed_element_states {
877 if let Some((element_state_key, element_state)) =
878 prev_frame.element_states.remove_entry(element_state_key)
879 {
880 self.element_states.insert(element_state_key, element_state);
881 }
882 }
883
884 self.scene.finish();
885 }
886}
887
888#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
889enum InputModality {
890 Mouse,
891 Keyboard,
892}
893
894/// Holds the state for a specific window.
895pub struct Window {
896 pub(crate) handle: AnyWindowHandle,
897 pub(crate) invalidator: WindowInvalidator,
898 pub(crate) removed: bool,
899 pub(crate) platform_window: Box<dyn PlatformWindow>,
900 display_id: Option<DisplayId>,
901 sprite_atlas: Arc<dyn PlatformAtlas>,
902 text_system: Arc<WindowTextSystem>,
903 text_rendering_mode: Rc<Cell<TextRenderingMode>>,
904 rem_size: Pixels,
905 /// The stack of override values for the window's rem size.
906 ///
907 /// This is used by `with_rem_size` to allow rendering an element tree with
908 /// a given rem size.
909 rem_size_override_stack: SmallVec<[Pixels; 8]>,
910 pub(crate) viewport_size: Size<Pixels>,
911 layout_engine: Option<TaffyLayoutEngine>,
912 pub(crate) root: Option<AnyView>,
913 pub(crate) element_id_stack: SmallVec<[ElementId; 32]>,
914 pub(crate) text_style_stack: Vec<TextStyleRefinement>,
915 pub(crate) rendered_entity_stack: Vec<EntityId>,
916 pub(crate) element_offset_stack: Vec<Point<Pixels>>,
917 pub(crate) element_opacity: f32,
918 pub(crate) content_mask_stack: Vec<ContentMask<Pixels>>,
919 pub(crate) requested_autoscroll: Option<Bounds<Pixels>>,
920 pub(crate) image_cache_stack: Vec<AnyImageCache>,
921 pub(crate) rendered_frame: Frame,
922 pub(crate) next_frame: Frame,
923 next_hitbox_id: HitboxId,
924 pub(crate) next_tooltip_id: TooltipId,
925 pub(crate) tooltip_bounds: Option<TooltipBounds>,
926 next_frame_callbacks: Rc<RefCell<Vec<FrameCallback>>>,
927 pub(crate) dirty_views: FxHashSet<EntityId>,
928 focus_listeners: SubscriberSet<(), AnyWindowFocusListener>,
929 pub(crate) focus_lost_listeners: SubscriberSet<(), AnyObserver>,
930 default_prevented: bool,
931 mouse_position: Point<Pixels>,
932 mouse_hit_test: HitTest,
933 modifiers: Modifiers,
934 capslock: Capslock,
935 scale_factor: f32,
936 pub(crate) bounds_observers: SubscriberSet<(), AnyObserver>,
937 appearance: WindowAppearance,
938 pub(crate) appearance_observers: SubscriberSet<(), AnyObserver>,
939 active: Rc<Cell<bool>>,
940 hovered: Rc<Cell<bool>>,
941 pub(crate) needs_present: Rc<Cell<bool>>,
942 /// Tracks recent input event timestamps to determine if input is arriving at a high rate.
943 /// Used to selectively enable VRR optimization only when input rate exceeds 60fps.
944 pub(crate) input_rate_tracker: Rc<RefCell<InputRateTracker>>,
945 last_input_modality: InputModality,
946 pub(crate) refreshing: bool,
947 pub(crate) activation_observers: SubscriberSet<(), AnyObserver>,
948 pub(crate) focus: Option<FocusId>,
949 focus_enabled: bool,
950 pending_input: Option<PendingInput>,
951 pending_modifier: ModifierState,
952 pub(crate) pending_input_observers: SubscriberSet<(), AnyObserver>,
953 prompt: Option<RenderablePromptHandle>,
954 pub(crate) client_inset: Option<Pixels>,
955 #[cfg(any(feature = "inspector", debug_assertions))]
956 inspector: Option<Entity<Inspector>>,
957}
958
959#[derive(Clone, Debug, Default)]
960struct ModifierState {
961 modifiers: Modifiers,
962 saw_keystroke: bool,
963}
964
965/// Tracks input event timestamps to determine if input is arriving at a high rate.
966/// Used for selective VRR (Variable Refresh Rate) optimization.
967#[derive(Clone, Debug)]
968pub(crate) struct InputRateTracker {
969 timestamps: Vec<Instant>,
970 window: Duration,
971 inputs_per_second: u32,
972 sustain_until: Instant,
973 sustain_duration: Duration,
974}
975
976impl Default for InputRateTracker {
977 fn default() -> Self {
978 Self {
979 timestamps: Vec::new(),
980 window: Duration::from_millis(100),
981 inputs_per_second: 60,
982 sustain_until: Instant::now(),
983 sustain_duration: Duration::from_secs(1),
984 }
985 }
986}
987
988impl InputRateTracker {
989 pub fn record_input(&mut self) {
990 let now = Instant::now();
991 self.timestamps.push(now);
992 self.prune_old_timestamps(now);
993
994 let min_events = self.inputs_per_second as u128 * self.window.as_millis() / 1000;
995 if self.timestamps.len() as u128 >= min_events {
996 self.sustain_until = now + self.sustain_duration;
997 }
998 }
999
1000 pub fn is_high_rate(&self) -> bool {
1001 Instant::now() < self.sustain_until
1002 }
1003
1004 fn prune_old_timestamps(&mut self, now: Instant) {
1005 self.timestamps
1006 .retain(|&t| now.duration_since(t) <= self.window);
1007 }
1008}
1009
1010#[derive(Clone, Copy, Debug, Eq, PartialEq)]
1011pub(crate) enum DrawPhase {
1012 None,
1013 Prepaint,
1014 Paint,
1015 Focus,
1016}
1017
1018#[derive(Default, Debug)]
1019struct PendingInput {
1020 keystrokes: SmallVec<[Keystroke; 1]>,
1021 focus: Option<FocusId>,
1022 timer: Option<Task<()>>,
1023 needs_timeout: bool,
1024}
1025
1026pub(crate) struct ElementStateBox {
1027 pub(crate) inner: Box<dyn Any>,
1028 #[cfg(debug_assertions)]
1029 pub(crate) type_name: &'static str,
1030}
1031
1032fn default_bounds(display_id: Option<DisplayId>, cx: &mut App) -> WindowBounds {
1033 // TODO, BUG: if you open a window with the currently active window
1034 // on the stack, this will erroneously fallback to `None`
1035 //
1036 // TODO these should be the initial window bounds not considering maximized/fullscreen
1037 let active_window_bounds = cx
1038 .active_window()
1039 .and_then(|w| w.update(cx, |_, window, _| window.window_bounds()).ok());
1040
1041 const CASCADE_OFFSET: f32 = 25.0;
1042
1043 let display = display_id
1044 .map(|id| cx.find_display(id))
1045 .unwrap_or_else(|| cx.primary_display());
1046
1047 let default_placement = || Bounds::new(point(px(0.), px(0.)), DEFAULT_WINDOW_SIZE);
1048
1049 // Use visible_bounds to exclude taskbar/dock areas
1050 let display_bounds = display
1051 .as_ref()
1052 .map(|d| d.visible_bounds())
1053 .unwrap_or_else(default_placement);
1054
1055 let (
1056 Bounds {
1057 origin: base_origin,
1058 size: base_size,
1059 },
1060 window_bounds_ctor,
1061 ): (_, fn(Bounds<Pixels>) -> WindowBounds) = match active_window_bounds {
1062 Some(bounds) => match bounds {
1063 WindowBounds::Windowed(bounds) => (bounds, WindowBounds::Windowed),
1064 WindowBounds::Maximized(bounds) => (bounds, WindowBounds::Maximized),
1065 WindowBounds::Fullscreen(bounds) => (bounds, WindowBounds::Fullscreen),
1066 },
1067 None => (
1068 display
1069 .as_ref()
1070 .map(|d| d.default_bounds())
1071 .unwrap_or_else(default_placement),
1072 WindowBounds::Windowed,
1073 ),
1074 };
1075
1076 let cascade_offset = point(px(CASCADE_OFFSET), px(CASCADE_OFFSET));
1077 let proposed_origin = base_origin + cascade_offset;
1078 let proposed_bounds = Bounds::new(proposed_origin, base_size);
1079
1080 let display_right = display_bounds.origin.x + display_bounds.size.width;
1081 let display_bottom = display_bounds.origin.y + display_bounds.size.height;
1082 let window_right = proposed_bounds.origin.x + proposed_bounds.size.width;
1083 let window_bottom = proposed_bounds.origin.y + proposed_bounds.size.height;
1084
1085 let fits_horizontally = window_right <= display_right;
1086 let fits_vertically = window_bottom <= display_bottom;
1087
1088 let final_origin = match (fits_horizontally, fits_vertically) {
1089 (true, true) => proposed_origin,
1090 (false, true) => point(display_bounds.origin.x, base_origin.y),
1091 (true, false) => point(base_origin.x, display_bounds.origin.y),
1092 (false, false) => display_bounds.origin,
1093 };
1094 window_bounds_ctor(Bounds::new(final_origin, base_size))
1095}
1096
1097impl Window {
1098 pub(crate) fn new(
1099 handle: AnyWindowHandle,
1100 options: WindowOptions,
1101 cx: &mut App,
1102 ) -> Result<Self> {
1103 let WindowOptions {
1104 window_bounds,
1105 titlebar,
1106 focus,
1107 show,
1108 kind,
1109 is_movable,
1110 is_resizable,
1111 is_minimizable,
1112 display_id,
1113 window_background,
1114 app_id,
1115 window_min_size,
1116 window_decorations,
1117 #[cfg_attr(not(target_os = "macos"), allow(unused_variables))]
1118 tabbing_identifier,
1119 } = options;
1120
1121 let window_bounds = window_bounds.unwrap_or_else(|| default_bounds(display_id, cx));
1122 let mut platform_window = cx.platform.open_window(
1123 handle,
1124 WindowParams {
1125 bounds: window_bounds.get_bounds(),
1126 titlebar,
1127 kind,
1128 is_movable,
1129 is_resizable,
1130 is_minimizable,
1131 focus,
1132 show,
1133 display_id,
1134 window_min_size,
1135 #[cfg(target_os = "macos")]
1136 tabbing_identifier,
1137 },
1138 )?;
1139
1140 let tab_bar_visible = platform_window.tab_bar_visible();
1141 SystemWindowTabController::init_visible(cx, tab_bar_visible);
1142 if let Some(tabs) = platform_window.tabbed_windows() {
1143 SystemWindowTabController::add_tab(cx, handle.window_id(), tabs);
1144 }
1145
1146 let display_id = platform_window.display().map(|display| display.id());
1147 let sprite_atlas = platform_window.sprite_atlas();
1148 let mouse_position = platform_window.mouse_position();
1149 let modifiers = platform_window.modifiers();
1150 let capslock = platform_window.capslock();
1151 let content_size = platform_window.content_size();
1152 let scale_factor = platform_window.scale_factor();
1153 let appearance = platform_window.appearance();
1154 let text_system = Arc::new(WindowTextSystem::new(cx.text_system().clone()));
1155 let invalidator = WindowInvalidator::new();
1156 let active = Rc::new(Cell::new(platform_window.is_active()));
1157 let hovered = Rc::new(Cell::new(platform_window.is_hovered()));
1158 let needs_present = Rc::new(Cell::new(false));
1159 let next_frame_callbacks: Rc<RefCell<Vec<FrameCallback>>> = Default::default();
1160 let input_rate_tracker = Rc::new(RefCell::new(InputRateTracker::default()));
1161 let last_frame_time = Rc::new(Cell::new(None));
1162
1163 platform_window
1164 .request_decorations(window_decorations.unwrap_or(WindowDecorations::Server));
1165 platform_window.set_background_appearance(window_background);
1166
1167 match window_bounds {
1168 WindowBounds::Fullscreen(_) => platform_window.toggle_fullscreen(),
1169 WindowBounds::Maximized(_) => platform_window.zoom(),
1170 WindowBounds::Windowed(_) => {}
1171 }
1172
1173 platform_window.on_close(Box::new({
1174 let window_id = handle.window_id();
1175 let mut cx = cx.to_async();
1176 move || {
1177 let _ = handle.update(&mut cx, |_, window, _| window.remove_window());
1178 let _ = cx.update(|cx| {
1179 SystemWindowTabController::remove_tab(cx, window_id);
1180 });
1181 }
1182 }));
1183 platform_window.on_request_frame(Box::new({
1184 let mut cx = cx.to_async();
1185 let invalidator = invalidator.clone();
1186 let active = active.clone();
1187 let needs_present = needs_present.clone();
1188 let next_frame_callbacks = next_frame_callbacks.clone();
1189 let input_rate_tracker = input_rate_tracker.clone();
1190 move |request_frame_options| {
1191 let thermal_state = handle
1192 .update(&mut cx, |_, _, cx| cx.thermal_state())
1193 .log_err();
1194
1195 if thermal_state == Some(ThermalState::Serious)
1196 || thermal_state == Some(ThermalState::Critical)
1197 {
1198 let now = Instant::now();
1199 let last_frame_time = last_frame_time.replace(Some(now));
1200
1201 if let Some(last_frame) = last_frame_time
1202 && now.duration_since(last_frame) < Duration::from_micros(16667)
1203 {
1204 return;
1205 }
1206 }
1207
1208 let next_frame_callbacks = next_frame_callbacks.take();
1209 if !next_frame_callbacks.is_empty() {
1210 handle
1211 .update(&mut cx, |_, window, cx| {
1212 for callback in next_frame_callbacks {
1213 callback(window, cx);
1214 }
1215 })
1216 .log_err();
1217 }
1218
1219 // Keep presenting if input was recently arriving at a high rate (>= 60fps).
1220 // Once high-rate input is detected, we sustain presentation for 1 second
1221 // to prevent display underclocking during active input.
1222 let needs_present = request_frame_options.require_presentation
1223 || needs_present.get()
1224 || (active.get() && input_rate_tracker.borrow_mut().is_high_rate());
1225
1226 if invalidator.is_dirty() || request_frame_options.force_render {
1227 measure("frame duration", || {
1228 handle
1229 .update(&mut cx, |_, window, cx| {
1230 let arena_clear_needed = window.draw(cx);
1231 window.present();
1232 arena_clear_needed.clear();
1233 })
1234 .log_err();
1235 })
1236 } else if needs_present {
1237 handle
1238 .update(&mut cx, |_, window, _| window.present())
1239 .log_err();
1240 }
1241
1242 handle
1243 .update(&mut cx, |_, window, _| {
1244 window.complete_frame();
1245 })
1246 .log_err();
1247 }
1248 }));
1249 platform_window.on_resize(Box::new({
1250 let mut cx = cx.to_async();
1251 move |_, _| {
1252 handle
1253 .update(&mut cx, |_, window, cx| window.bounds_changed(cx))
1254 .log_err();
1255 }
1256 }));
1257 platform_window.on_moved(Box::new({
1258 let mut cx = cx.to_async();
1259 move || {
1260 handle
1261 .update(&mut cx, |_, window, cx| window.bounds_changed(cx))
1262 .log_err();
1263 }
1264 }));
1265 platform_window.on_appearance_changed(Box::new({
1266 let mut cx = cx.to_async();
1267 move || {
1268 handle
1269 .update(&mut cx, |_, window, cx| window.appearance_changed(cx))
1270 .log_err();
1271 }
1272 }));
1273 platform_window.on_active_status_change(Box::new({
1274 let mut cx = cx.to_async();
1275 move |active| {
1276 handle
1277 .update(&mut cx, |_, window, cx| {
1278 window.active.set(active);
1279 window.modifiers = window.platform_window.modifiers();
1280 window.capslock = window.platform_window.capslock();
1281 window
1282 .activation_observers
1283 .clone()
1284 .retain(&(), |callback| callback(window, cx));
1285
1286 window.bounds_changed(cx);
1287 window.refresh();
1288
1289 SystemWindowTabController::update_last_active(cx, window.handle.id);
1290 })
1291 .log_err();
1292 }
1293 }));
1294 platform_window.on_hover_status_change(Box::new({
1295 let mut cx = cx.to_async();
1296 move |active| {
1297 handle
1298 .update(&mut cx, |_, window, _| {
1299 window.hovered.set(active);
1300 window.refresh();
1301 })
1302 .log_err();
1303 }
1304 }));
1305 platform_window.on_input({
1306 let mut cx = cx.to_async();
1307 Box::new(move |event| {
1308 handle
1309 .update(&mut cx, |_, window, cx| window.dispatch_event(event, cx))
1310 .log_err()
1311 .unwrap_or(DispatchEventResult::default())
1312 })
1313 });
1314 platform_window.on_hit_test_window_control({
1315 let mut cx = cx.to_async();
1316 Box::new(move || {
1317 handle
1318 .update(&mut cx, |_, window, _cx| {
1319 for (area, hitbox) in &window.rendered_frame.window_control_hitboxes {
1320 if window.mouse_hit_test.ids.contains(&hitbox.id) {
1321 return Some(*area);
1322 }
1323 }
1324 None
1325 })
1326 .log_err()
1327 .unwrap_or(None)
1328 })
1329 });
1330 platform_window.on_move_tab_to_new_window({
1331 let mut cx = cx.to_async();
1332 Box::new(move || {
1333 handle
1334 .update(&mut cx, |_, _window, cx| {
1335 SystemWindowTabController::move_tab_to_new_window(cx, handle.window_id());
1336 })
1337 .log_err();
1338 })
1339 });
1340 platform_window.on_merge_all_windows({
1341 let mut cx = cx.to_async();
1342 Box::new(move || {
1343 handle
1344 .update(&mut cx, |_, _window, cx| {
1345 SystemWindowTabController::merge_all_windows(cx, handle.window_id());
1346 })
1347 .log_err();
1348 })
1349 });
1350 platform_window.on_select_next_tab({
1351 let mut cx = cx.to_async();
1352 Box::new(move || {
1353 handle
1354 .update(&mut cx, |_, _window, cx| {
1355 SystemWindowTabController::select_next_tab(cx, handle.window_id());
1356 })
1357 .log_err();
1358 })
1359 });
1360 platform_window.on_select_previous_tab({
1361 let mut cx = cx.to_async();
1362 Box::new(move || {
1363 handle
1364 .update(&mut cx, |_, _window, cx| {
1365 SystemWindowTabController::select_previous_tab(cx, handle.window_id())
1366 })
1367 .log_err();
1368 })
1369 });
1370 platform_window.on_toggle_tab_bar({
1371 let mut cx = cx.to_async();
1372 Box::new(move || {
1373 handle
1374 .update(&mut cx, |_, window, cx| {
1375 let tab_bar_visible = window.platform_window.tab_bar_visible();
1376 SystemWindowTabController::set_visible(cx, tab_bar_visible);
1377 })
1378 .log_err();
1379 })
1380 });
1381
1382 if let Some(app_id) = app_id {
1383 platform_window.set_app_id(&app_id);
1384 }
1385
1386 platform_window.map_window().unwrap();
1387
1388 Ok(Window {
1389 handle,
1390 invalidator,
1391 removed: false,
1392 platform_window,
1393 display_id,
1394 sprite_atlas,
1395 text_system,
1396 text_rendering_mode: cx.text_rendering_mode.clone(),
1397 rem_size: px(16.),
1398 rem_size_override_stack: SmallVec::new(),
1399 viewport_size: content_size,
1400 layout_engine: Some(TaffyLayoutEngine::new()),
1401 root: None,
1402 element_id_stack: SmallVec::default(),
1403 text_style_stack: Vec::new(),
1404 rendered_entity_stack: Vec::new(),
1405 element_offset_stack: Vec::new(),
1406 content_mask_stack: Vec::new(),
1407 element_opacity: 1.0,
1408 requested_autoscroll: None,
1409 rendered_frame: Frame::new(DispatchTree::new(cx.keymap.clone(), cx.actions.clone())),
1410 next_frame: Frame::new(DispatchTree::new(cx.keymap.clone(), cx.actions.clone())),
1411 next_frame_callbacks,
1412 next_hitbox_id: HitboxId(0),
1413 next_tooltip_id: TooltipId::default(),
1414 tooltip_bounds: None,
1415 dirty_views: FxHashSet::default(),
1416 focus_listeners: SubscriberSet::new(),
1417 focus_lost_listeners: SubscriberSet::new(),
1418 default_prevented: true,
1419 mouse_position,
1420 mouse_hit_test: HitTest::default(),
1421 modifiers,
1422 capslock,
1423 scale_factor,
1424 bounds_observers: SubscriberSet::new(),
1425 appearance,
1426 appearance_observers: SubscriberSet::new(),
1427 active,
1428 hovered,
1429 needs_present,
1430 input_rate_tracker,
1431 last_input_modality: InputModality::Mouse,
1432 refreshing: false,
1433 activation_observers: SubscriberSet::new(),
1434 focus: None,
1435 focus_enabled: true,
1436 pending_input: None,
1437 pending_modifier: ModifierState::default(),
1438 pending_input_observers: SubscriberSet::new(),
1439 prompt: None,
1440 client_inset: None,
1441 image_cache_stack: Vec::new(),
1442 #[cfg(any(feature = "inspector", debug_assertions))]
1443 inspector: None,
1444 })
1445 }
1446
1447 pub(crate) fn new_focus_listener(
1448 &self,
1449 value: AnyWindowFocusListener,
1450 ) -> (Subscription, impl FnOnce() + use<>) {
1451 self.focus_listeners.insert((), value)
1452 }
1453}
1454
1455#[derive(Clone, Debug, Default, PartialEq, Eq)]
1456#[expect(missing_docs)]
1457pub struct DispatchEventResult {
1458 pub propagate: bool,
1459 pub default_prevented: bool,
1460}
1461
1462/// Indicates which region of the window is visible. Content falling outside of this mask will not be
1463/// rendered. Currently, only rectangular content masks are supported, but we give the mask its own type
1464/// to leave room to support more complex shapes in the future.
1465#[derive(Clone, Debug, Default, PartialEq, Eq)]
1466#[repr(C)]
1467pub struct ContentMask<P: Clone + Debug + Default + PartialEq> {
1468 /// The bounds
1469 pub bounds: Bounds<P>,
1470}
1471
1472impl ContentMask<Pixels> {
1473 /// Scale the content mask's pixel units by the given scaling factor.
1474 pub fn scale(&self, factor: f32) -> ContentMask<ScaledPixels> {
1475 ContentMask {
1476 bounds: self.bounds.scale(factor),
1477 }
1478 }
1479
1480 /// Intersect the content mask with the given content mask.
1481 pub fn intersect(&self, other: &Self) -> Self {
1482 let bounds = self.bounds.intersect(&other.bounds);
1483 ContentMask { bounds }
1484 }
1485}
1486
1487impl Window {
1488 fn mark_view_dirty(&mut self, view_id: EntityId) {
1489 // Mark ancestor views as dirty. If already in the `dirty_views` set, then all its ancestors
1490 // should already be dirty.
1491 for view_id in self
1492 .rendered_frame
1493 .dispatch_tree
1494 .view_path_reversed(view_id)
1495 {
1496 if !self.dirty_views.insert(view_id) {
1497 break;
1498 }
1499 }
1500 }
1501
1502 /// Registers a callback to be invoked when the window appearance changes.
1503 pub fn observe_window_appearance(
1504 &self,
1505 mut callback: impl FnMut(&mut Window, &mut App) + 'static,
1506 ) -> Subscription {
1507 let (subscription, activate) = self.appearance_observers.insert(
1508 (),
1509 Box::new(move |window, cx| {
1510 callback(window, cx);
1511 true
1512 }),
1513 );
1514 activate();
1515 subscription
1516 }
1517
1518 /// Replaces the root entity of the window with a new one.
1519 pub fn replace_root<E>(
1520 &mut self,
1521 cx: &mut App,
1522 build_view: impl FnOnce(&mut Window, &mut Context<E>) -> E,
1523 ) -> Entity<E>
1524 where
1525 E: 'static + Render,
1526 {
1527 let view = cx.new(|cx| build_view(self, cx));
1528 self.root = Some(view.clone().into());
1529 self.refresh();
1530 view
1531 }
1532
1533 /// Returns the root entity of the window, if it has one.
1534 pub fn root<E>(&self) -> Option<Option<Entity<E>>>
1535 where
1536 E: 'static + Render,
1537 {
1538 self.root
1539 .as_ref()
1540 .map(|view| view.clone().downcast::<E>().ok())
1541 }
1542
1543 /// Obtain a handle to the window that belongs to this context.
1544 pub fn window_handle(&self) -> AnyWindowHandle {
1545 self.handle
1546 }
1547
1548 /// Mark the window as dirty, scheduling it to be redrawn on the next frame.
1549 pub fn refresh(&mut self) {
1550 if self.invalidator.not_drawing() {
1551 self.refreshing = true;
1552 self.invalidator.set_dirty(true);
1553 }
1554 }
1555
1556 /// Close this window.
1557 pub fn remove_window(&mut self) {
1558 self.removed = true;
1559 }
1560
1561 /// Obtain the currently focused [`FocusHandle`]. If no elements are focused, returns `None`.
1562 pub fn focused(&self, cx: &App) -> Option<FocusHandle> {
1563 self.focus
1564 .and_then(|id| FocusHandle::for_id(id, &cx.focus_handles))
1565 }
1566
1567 /// Move focus to the element associated with the given [`FocusHandle`].
1568 pub fn focus(&mut self, handle: &FocusHandle, cx: &mut App) {
1569 if !self.focus_enabled || self.focus == Some(handle.id) {
1570 return;
1571 }
1572
1573 self.focus = Some(handle.id);
1574 self.clear_pending_keystrokes();
1575
1576 // Avoid re-entrant entity updates by deferring observer notifications to the end of the
1577 // current effect cycle, and only for this window.
1578 let window_handle = self.handle;
1579 cx.defer(move |cx| {
1580 window_handle
1581 .update(cx, |_, window, cx| {
1582 window.pending_input_changed(cx);
1583 })
1584 .ok();
1585 });
1586
1587 self.refresh();
1588 }
1589
1590 /// Remove focus from all elements within this context's window.
1591 pub fn blur(&mut self) {
1592 if !self.focus_enabled {
1593 return;
1594 }
1595
1596 self.focus = None;
1597 self.refresh();
1598 }
1599
1600 /// Blur the window and don't allow anything in it to be focused again.
1601 pub fn disable_focus(&mut self) {
1602 self.blur();
1603 self.focus_enabled = false;
1604 }
1605
1606 /// Move focus to next tab stop.
1607 pub fn focus_next(&mut self, cx: &mut App) {
1608 if !self.focus_enabled {
1609 return;
1610 }
1611
1612 if let Some(handle) = self.rendered_frame.tab_stops.next(self.focus.as_ref()) {
1613 self.focus(&handle, cx)
1614 }
1615 }
1616
1617 /// Move focus to previous tab stop.
1618 pub fn focus_prev(&mut self, cx: &mut App) {
1619 if !self.focus_enabled {
1620 return;
1621 }
1622
1623 if let Some(handle) = self.rendered_frame.tab_stops.prev(self.focus.as_ref()) {
1624 self.focus(&handle, cx)
1625 }
1626 }
1627
1628 /// Accessor for the text system.
1629 pub fn text_system(&self) -> &Arc<WindowTextSystem> {
1630 &self.text_system
1631 }
1632
1633 /// The current text style. Which is composed of all the style refinements provided to `with_text_style`.
1634 pub fn text_style(&self) -> TextStyle {
1635 let mut style = TextStyle::default();
1636 for refinement in &self.text_style_stack {
1637 style.refine(refinement);
1638 }
1639 style
1640 }
1641
1642 /// Check if the platform window is maximized.
1643 ///
1644 /// On some platforms (namely Windows) this is different than the bounds being the size of the display
1645 pub fn is_maximized(&self) -> bool {
1646 self.platform_window.is_maximized()
1647 }
1648
1649 /// request a certain window decoration (Wayland)
1650 pub fn request_decorations(&self, decorations: WindowDecorations) {
1651 self.platform_window.request_decorations(decorations);
1652 }
1653
1654 /// Start a window resize operation (Wayland)
1655 pub fn start_window_resize(&self, edge: ResizeEdge) {
1656 self.platform_window.start_window_resize(edge);
1657 }
1658
1659 /// Return the `WindowBounds` to indicate that how a window should be opened
1660 /// after it has been closed
1661 pub fn window_bounds(&self) -> WindowBounds {
1662 self.platform_window.window_bounds()
1663 }
1664
1665 /// Return the `WindowBounds` excluding insets (Wayland and X11)
1666 pub fn inner_window_bounds(&self) -> WindowBounds {
1667 self.platform_window.inner_window_bounds()
1668 }
1669
1670 /// Dispatch the given action on the currently focused element.
1671 pub fn dispatch_action(&mut self, action: Box<dyn Action>, cx: &mut App) {
1672 let focus_id = self.focused(cx).map(|handle| handle.id);
1673
1674 let window = self.handle;
1675 cx.defer(move |cx| {
1676 window
1677 .update(cx, |_, window, cx| {
1678 let node_id = window.focus_node_id_in_rendered_frame(focus_id);
1679 window.dispatch_action_on_node(node_id, action.as_ref(), cx);
1680 })
1681 .log_err();
1682 })
1683 }
1684
1685 pub(crate) fn dispatch_keystroke_observers(
1686 &mut self,
1687 event: &dyn Any,
1688 action: Option<Box<dyn Action>>,
1689 context_stack: Vec<KeyContext>,
1690 cx: &mut App,
1691 ) {
1692 let Some(key_down_event) = event.downcast_ref::<KeyDownEvent>() else {
1693 return;
1694 };
1695
1696 cx.keystroke_observers.clone().retain(&(), move |callback| {
1697 (callback)(
1698 &KeystrokeEvent {
1699 keystroke: key_down_event.keystroke.clone(),
1700 action: action.as_ref().map(|action| action.boxed_clone()),
1701 context_stack: context_stack.clone(),
1702 },
1703 self,
1704 cx,
1705 )
1706 });
1707 }
1708
1709 pub(crate) fn dispatch_keystroke_interceptors(
1710 &mut self,
1711 event: &dyn Any,
1712 context_stack: Vec<KeyContext>,
1713 cx: &mut App,
1714 ) {
1715 let Some(key_down_event) = event.downcast_ref::<KeyDownEvent>() else {
1716 return;
1717 };
1718
1719 cx.keystroke_interceptors
1720 .clone()
1721 .retain(&(), move |callback| {
1722 (callback)(
1723 &KeystrokeEvent {
1724 keystroke: key_down_event.keystroke.clone(),
1725 action: None,
1726 context_stack: context_stack.clone(),
1727 },
1728 self,
1729 cx,
1730 )
1731 });
1732 }
1733
1734 /// Schedules the given function to be run at the end of the current effect cycle, allowing entities
1735 /// that are currently on the stack to be returned to the app.
1736 pub fn defer(&self, cx: &mut App, f: impl FnOnce(&mut Window, &mut App) + 'static) {
1737 let handle = self.handle;
1738 cx.defer(move |cx| {
1739 handle.update(cx, |_, window, cx| f(window, cx)).ok();
1740 });
1741 }
1742
1743 /// Subscribe to events emitted by a entity.
1744 /// The entity to which you're subscribing must implement the [`EventEmitter`] trait.
1745 /// The callback will be invoked a handle to the emitting entity, the event, and a window context for the current window.
1746 pub fn observe<T: 'static>(
1747 &mut self,
1748 observed: &Entity<T>,
1749 cx: &mut App,
1750 mut on_notify: impl FnMut(Entity<T>, &mut Window, &mut App) + 'static,
1751 ) -> Subscription {
1752 let entity_id = observed.entity_id();
1753 let observed = observed.downgrade();
1754 let window_handle = self.handle;
1755 cx.new_observer(
1756 entity_id,
1757 Box::new(move |cx| {
1758 window_handle
1759 .update(cx, |_, window, cx| {
1760 if let Some(handle) = observed.upgrade() {
1761 on_notify(handle, window, cx);
1762 true
1763 } else {
1764 false
1765 }
1766 })
1767 .unwrap_or(false)
1768 }),
1769 )
1770 }
1771
1772 /// Subscribe to events emitted by a entity.
1773 /// The entity to which you're subscribing must implement the [`EventEmitter`] trait.
1774 /// The callback will be invoked a handle to the emitting entity, the event, and a window context for the current window.
1775 pub fn subscribe<Emitter, Evt>(
1776 &mut self,
1777 entity: &Entity<Emitter>,
1778 cx: &mut App,
1779 mut on_event: impl FnMut(Entity<Emitter>, &Evt, &mut Window, &mut App) + 'static,
1780 ) -> Subscription
1781 where
1782 Emitter: EventEmitter<Evt>,
1783 Evt: 'static,
1784 {
1785 let entity_id = entity.entity_id();
1786 let handle = entity.downgrade();
1787 let window_handle = self.handle;
1788 cx.new_subscription(
1789 entity_id,
1790 (
1791 TypeId::of::<Evt>(),
1792 Box::new(move |event, cx| {
1793 window_handle
1794 .update(cx, |_, window, cx| {
1795 if let Some(entity) = handle.upgrade() {
1796 let event = event.downcast_ref().expect("invalid event type");
1797 on_event(entity, event, window, cx);
1798 true
1799 } else {
1800 false
1801 }
1802 })
1803 .unwrap_or(false)
1804 }),
1805 ),
1806 )
1807 }
1808
1809 /// Register a callback to be invoked when the given `Entity` is released.
1810 pub fn observe_release<T>(
1811 &self,
1812 entity: &Entity<T>,
1813 cx: &mut App,
1814 mut on_release: impl FnOnce(&mut T, &mut Window, &mut App) + 'static,
1815 ) -> Subscription
1816 where
1817 T: 'static,
1818 {
1819 let entity_id = entity.entity_id();
1820 let window_handle = self.handle;
1821 let (subscription, activate) = cx.release_listeners.insert(
1822 entity_id,
1823 Box::new(move |entity, cx| {
1824 let entity = entity.downcast_mut().expect("invalid entity type");
1825 let _ = window_handle.update(cx, |_, window, cx| on_release(entity, window, cx));
1826 }),
1827 );
1828 activate();
1829 subscription
1830 }
1831
1832 /// Creates an [`AsyncWindowContext`], which has a static lifetime and can be held across
1833 /// await points in async code.
1834 pub fn to_async(&self, cx: &App) -> AsyncWindowContext {
1835 AsyncWindowContext::new_context(cx.to_async(), self.handle)
1836 }
1837
1838 /// Schedule the given closure to be run directly after the current frame is rendered.
1839 pub fn on_next_frame(&self, callback: impl FnOnce(&mut Window, &mut App) + 'static) {
1840 RefCell::borrow_mut(&self.next_frame_callbacks).push(Box::new(callback));
1841 }
1842
1843 /// Schedule a frame to be drawn on the next animation frame.
1844 ///
1845 /// This is useful for elements that need to animate continuously, such as a video player or an animated GIF.
1846 /// It will cause the window to redraw on the next frame, even if no other changes have occurred.
1847 ///
1848 /// If called from within a view, it will notify that view on the next frame. Otherwise, it will refresh the entire window.
1849 pub fn request_animation_frame(&self) {
1850 let entity = self.current_view();
1851 self.on_next_frame(move |_, cx| cx.notify(entity));
1852 }
1853
1854 /// Spawn the future returned by the given closure on the application thread pool.
1855 /// The closure is provided a handle to the current window and an `AsyncWindowContext` for
1856 /// use within your future.
1857 #[track_caller]
1858 pub fn spawn<AsyncFn, R>(&self, cx: &App, f: AsyncFn) -> Task<R>
1859 where
1860 R: 'static,
1861 AsyncFn: AsyncFnOnce(&mut AsyncWindowContext) -> R + 'static,
1862 {
1863 let handle = self.handle;
1864 cx.spawn(async move |app| {
1865 let mut async_window_cx = AsyncWindowContext::new_context(app.clone(), handle);
1866 f(&mut async_window_cx).await
1867 })
1868 }
1869
1870 /// Spawn the future returned by the given closure on the application thread
1871 /// pool, with the given priority. The closure is provided a handle to the
1872 /// current window and an `AsyncWindowContext` for use within your future.
1873 #[track_caller]
1874 pub fn spawn_with_priority<AsyncFn, R>(
1875 &self,
1876 priority: Priority,
1877 cx: &App,
1878 f: AsyncFn,
1879 ) -> Task<R>
1880 where
1881 R: 'static,
1882 AsyncFn: AsyncFnOnce(&mut AsyncWindowContext) -> R + 'static,
1883 {
1884 let handle = self.handle;
1885 cx.spawn_with_priority(priority, async move |app| {
1886 let mut async_window_cx = AsyncWindowContext::new_context(app.clone(), handle);
1887 f(&mut async_window_cx).await
1888 })
1889 }
1890
1891 fn bounds_changed(&mut self, cx: &mut App) {
1892 self.scale_factor = self.platform_window.scale_factor();
1893 self.viewport_size = self.platform_window.content_size();
1894 self.display_id = self.platform_window.display().map(|display| display.id());
1895
1896 self.refresh();
1897
1898 self.bounds_observers
1899 .clone()
1900 .retain(&(), |callback| callback(self, cx));
1901 }
1902
1903 /// Returns the bounds of the current window in the global coordinate space, which could span across multiple displays.
1904 pub fn bounds(&self) -> Bounds<Pixels> {
1905 self.platform_window.bounds()
1906 }
1907
1908 /// Renders the current frame's scene to a texture and returns the pixel data as an RGBA image.
1909 /// This does not present the frame to screen - useful for visual testing where we want
1910 /// to capture what would be rendered without displaying it or requiring the window to be visible.
1911 #[cfg(any(test, feature = "test-support"))]
1912 pub fn render_to_image(&self) -> anyhow::Result<image::RgbaImage> {
1913 self.platform_window
1914 .render_to_image(&self.rendered_frame.scene)
1915 }
1916
1917 /// Set the content size of the window.
1918 pub fn resize(&mut self, size: Size<Pixels>) {
1919 self.platform_window.resize(size);
1920 }
1921
1922 /// Returns whether or not the window is currently fullscreen
1923 pub fn is_fullscreen(&self) -> bool {
1924 self.platform_window.is_fullscreen()
1925 }
1926
1927 pub(crate) fn appearance_changed(&mut self, cx: &mut App) {
1928 self.appearance = self.platform_window.appearance();
1929
1930 self.appearance_observers
1931 .clone()
1932 .retain(&(), |callback| callback(self, cx));
1933 }
1934
1935 /// Returns the appearance of the current window.
1936 pub fn appearance(&self) -> WindowAppearance {
1937 self.appearance
1938 }
1939
1940 /// Returns the size of the drawable area within the window.
1941 pub fn viewport_size(&self) -> Size<Pixels> {
1942 self.viewport_size
1943 }
1944
1945 /// Returns whether this window is focused by the operating system (receiving key events).
1946 pub fn is_window_active(&self) -> bool {
1947 self.active.get()
1948 }
1949
1950 /// Returns whether this window is considered to be the window
1951 /// that currently owns the mouse cursor.
1952 /// On mac, this is equivalent to `is_window_active`.
1953 pub fn is_window_hovered(&self) -> bool {
1954 if cfg!(any(
1955 target_os = "windows",
1956 target_os = "linux",
1957 target_os = "freebsd"
1958 )) {
1959 self.hovered.get()
1960 } else {
1961 self.is_window_active()
1962 }
1963 }
1964
1965 /// Toggle zoom on the window.
1966 pub fn zoom_window(&self) {
1967 self.platform_window.zoom();
1968 }
1969
1970 /// Opens the native title bar context menu, useful when implementing client side decorations (Wayland and X11)
1971 pub fn show_window_menu(&self, position: Point<Pixels>) {
1972 self.platform_window.show_window_menu(position)
1973 }
1974
1975 /// Handle window movement for Linux and macOS.
1976 /// Tells the compositor to take control of window movement (Wayland and X11)
1977 ///
1978 /// Events may not be received during a move operation.
1979 pub fn start_window_move(&self) {
1980 self.platform_window.start_window_move()
1981 }
1982
1983 /// When using client side decorations, set this to the width of the invisible decorations (Wayland and X11)
1984 pub fn set_client_inset(&mut self, inset: Pixels) {
1985 self.client_inset = Some(inset);
1986 self.platform_window.set_client_inset(inset);
1987 }
1988
1989 /// Returns the client_inset value by [`Self::set_client_inset`].
1990 pub fn client_inset(&self) -> Option<Pixels> {
1991 self.client_inset
1992 }
1993
1994 /// Returns whether the title bar window controls need to be rendered by the application (Wayland and X11)
1995 pub fn window_decorations(&self) -> Decorations {
1996 self.platform_window.window_decorations()
1997 }
1998
1999 /// Returns which window controls are currently visible (Wayland)
2000 pub fn window_controls(&self) -> WindowControls {
2001 self.platform_window.window_controls()
2002 }
2003
2004 /// Updates the window's title at the platform level.
2005 pub fn set_window_title(&mut self, title: &str) {
2006 self.platform_window.set_title(title);
2007 }
2008
2009 /// Sets the application identifier.
2010 pub fn set_app_id(&mut self, app_id: &str) {
2011 self.platform_window.set_app_id(app_id);
2012 }
2013
2014 /// Sets the window background appearance.
2015 pub fn set_background_appearance(&self, background_appearance: WindowBackgroundAppearance) {
2016 self.platform_window
2017 .set_background_appearance(background_appearance);
2018 }
2019
2020 /// Mark the window as dirty at the platform level.
2021 pub fn set_window_edited(&mut self, edited: bool) {
2022 self.platform_window.set_edited(edited);
2023 }
2024
2025 /// Determine the display on which the window is visible.
2026 pub fn display(&self, cx: &App) -> Option<Rc<dyn PlatformDisplay>> {
2027 cx.platform
2028 .displays()
2029 .into_iter()
2030 .find(|display| Some(display.id()) == self.display_id)
2031 }
2032
2033 /// Show the platform character palette.
2034 pub fn show_character_palette(&self) {
2035 self.platform_window.show_character_palette();
2036 }
2037
2038 /// The scale factor of the display associated with the window. For example, it could
2039 /// return 2.0 for a "retina" display, indicating that each logical pixel should actually
2040 /// be rendered as two pixels on screen.
2041 pub fn scale_factor(&self) -> f32 {
2042 self.scale_factor
2043 }
2044
2045 /// The size of an em for the base font of the application. Adjusting this value allows the
2046 /// UI to scale, just like zooming a web page.
2047 pub fn rem_size(&self) -> Pixels {
2048 self.rem_size_override_stack
2049 .last()
2050 .copied()
2051 .unwrap_or(self.rem_size)
2052 }
2053
2054 /// Sets the size of an em for the base font of the application. Adjusting this value allows the
2055 /// UI to scale, just like zooming a web page.
2056 pub fn set_rem_size(&mut self, rem_size: impl Into<Pixels>) {
2057 self.rem_size = rem_size.into();
2058 }
2059
2060 /// Acquire a globally unique identifier for the given ElementId.
2061 /// Only valid for the duration of the provided closure.
2062 pub fn with_global_id<R>(
2063 &mut self,
2064 element_id: ElementId,
2065 f: impl FnOnce(&GlobalElementId, &mut Self) -> R,
2066 ) -> R {
2067 self.with_id(element_id, |this| {
2068 let global_id = GlobalElementId(Arc::from(&*this.element_id_stack));
2069
2070 f(&global_id, this)
2071 })
2072 }
2073
2074 /// Calls the provided closure with the element ID pushed on the stack.
2075 #[inline]
2076 pub fn with_id<R>(
2077 &mut self,
2078 element_id: impl Into<ElementId>,
2079 f: impl FnOnce(&mut Self) -> R,
2080 ) -> R {
2081 self.element_id_stack.push(element_id.into());
2082 let result = f(self);
2083 self.element_id_stack.pop();
2084 result
2085 }
2086
2087 /// Executes the provided function with the specified rem size.
2088 ///
2089 /// This method must only be called as part of element drawing.
2090 // This function is called in a highly recursive manner in editor
2091 // prepainting, make sure its inlined to reduce the stack burden
2092 #[inline]
2093 pub fn with_rem_size<F, R>(&mut self, rem_size: Option<impl Into<Pixels>>, f: F) -> R
2094 where
2095 F: FnOnce(&mut Self) -> R,
2096 {
2097 self.invalidator.debug_assert_paint_or_prepaint();
2098
2099 if let Some(rem_size) = rem_size {
2100 self.rem_size_override_stack.push(rem_size.into());
2101 let result = f(self);
2102 self.rem_size_override_stack.pop();
2103 result
2104 } else {
2105 f(self)
2106 }
2107 }
2108
2109 /// The line height associated with the current text style.
2110 pub fn line_height(&self) -> Pixels {
2111 self.round_to_nearest_device_pixel(self.text_style().line_height_in_pixels(self.rem_size()))
2112 }
2113
2114 /// Rounds a logical size or coordinate to the nearest device pixel for this window.
2115 /// Uses round-half-toward-zero to match the layout engine's rounding policy.
2116 #[inline]
2117 pub fn round_to_nearest_device_pixel(&self, value: Pixels) -> Pixels {
2118 let scale_factor = self.scale_factor();
2119 px(round_half_toward_zero(value.0 * scale_factor) / scale_factor)
2120 }
2121
2122 /// f64 variant of [`Self::round_to_nearest_device_pixel`] for scroll-offset
2123 /// arithmetic that must preserve f64 precision.
2124 #[inline]
2125 pub fn round_f64_to_nearest_device_pixel(&self, value: f64) -> f64 {
2126 let scale_factor = f64::from(self.scale_factor());
2127 round_half_toward_zero_f64(value * scale_factor) / scale_factor
2128 }
2129
2130 #[inline]
2131 fn round_point_to_device_pixels(&self, position: Point<Pixels>) -> Point<Pixels> {
2132 point(
2133 self.round_to_nearest_device_pixel(position.x),
2134 self.round_to_nearest_device_pixel(position.y),
2135 )
2136 }
2137
2138 #[inline]
2139 fn bounds_from_device_edges(
2140 &self,
2141 left: f32,
2142 top: f32,
2143 right: f32,
2144 bottom: f32,
2145 ) -> Bounds<ScaledPixels> {
2146 let right = right.max(left);
2147 let bottom = bottom.max(top);
2148 Bounds::from_corners(
2149 point(ScaledPixels(left), ScaledPixels(top)),
2150 point(ScaledPixels(right), ScaledPixels(bottom)),
2151 )
2152 }
2153
2154 /// Converts logical-pixel bounds to device-pixel bounds by rounding origin
2155 /// and size independently. This preserves consistent sizes: elements with
2156 /// the same logical size always produce the same device-pixel size.
2157 ///
2158 /// Gap-free abutment between layout siblings is handled upstream by
2159 /// Taffy's cumulative edge-based rounding post-pass. For elements
2160 /// positioned by Taffy, the values arriving here are already on the
2161 /// device-pixel grid and this rounding is effectively a no-op.
2162 #[inline]
2163 fn round_bounds_to_device_pixels(&self, bounds: Bounds<Pixels>) -> Bounds<ScaledPixels> {
2164 let scale_factor = self.scale_factor();
2165 Bounds {
2166 origin: point(
2167 ScaledPixels(round_half_toward_zero(bounds.origin.x.0 * scale_factor)),
2168 ScaledPixels(round_half_toward_zero(bounds.origin.y.0 * scale_factor)),
2169 ),
2170 size: size(
2171 self.round_length_to_device_pixels(bounds.size.width),
2172 self.round_length_to_device_pixels(bounds.size.height),
2173 ),
2174 }
2175 }
2176
2177 #[inline]
2178 fn outset_bounds_to_device_pixels(&self, bounds: Bounds<Pixels>) -> Bounds<ScaledPixels> {
2179 let scale_factor = self.scale_factor();
2180 let left = (bounds.left().0 * scale_factor).floor();
2181 let top = (bounds.top().0 * scale_factor).floor();
2182 let right = (bounds.right().0 * scale_factor).ceil();
2183 let bottom = (bounds.bottom().0 * scale_factor).ceil();
2184 self.bounds_from_device_edges(left, top, right, bottom)
2185 }
2186
2187 #[inline]
2188 fn outset_content_mask_to_device_pixels(
2189 &self,
2190 content_mask: ContentMask<Pixels>,
2191 ) -> ContentMask<ScaledPixels> {
2192 ContentMask {
2193 bounds: self.outset_bounds_to_device_pixels(content_mask.bounds),
2194 }
2195 }
2196
2197 #[inline]
2198 fn round_edges_to_device_pixels(&self, edges: Edges<Pixels>) -> Edges<ScaledPixels> {
2199 Edges {
2200 top: self.round_nonzero_length_to_device_pixels(edges.top),
2201 right: self.round_nonzero_length_to_device_pixels(edges.right),
2202 bottom: self.round_nonzero_length_to_device_pixels(edges.bottom),
2203 left: self.round_nonzero_length_to_device_pixels(edges.left),
2204 }
2205 }
2206
2207 #[inline]
2208 fn round_length_to_device_pixels(&self, value: Pixels) -> ScaledPixels {
2209 let scaled = (value.0 * self.scale_factor()).max(0.0);
2210 ScaledPixels(round_half_toward_zero(scaled))
2211 }
2212
2213 #[inline]
2214 fn round_nonzero_length_to_device_pixels(&self, value: Pixels) -> ScaledPixels {
2215 let rounded = self.round_length_to_device_pixels(value).0;
2216 if value.is_zero() {
2217 ScaledPixels(0.0)
2218 } else {
2219 ScaledPixels(rounded.max(1.0))
2220 }
2221 }
2222
2223 /// Call to prevent the default action of an event. Currently only used to prevent
2224 /// parent elements from becoming focused on mouse down.
2225 pub fn prevent_default(&mut self) {
2226 self.default_prevented = true;
2227 }
2228
2229 /// Obtain whether default has been prevented for the event currently being dispatched.
2230 pub fn default_prevented(&self) -> bool {
2231 self.default_prevented
2232 }
2233
2234 /// Determine whether the given action is available along the dispatch path to the currently focused element.
2235 pub fn is_action_available(&self, action: &dyn Action, cx: &App) -> bool {
2236 let node_id =
2237 self.focus_node_id_in_rendered_frame(self.focused(cx).map(|handle| handle.id));
2238 self.rendered_frame
2239 .dispatch_tree
2240 .is_action_available(action, node_id)
2241 }
2242
2243 /// Determine whether the given action is available along the dispatch path to the given focus_handle.
2244 pub fn is_action_available_in(&self, action: &dyn Action, focus_handle: &FocusHandle) -> bool {
2245 let node_id = self.focus_node_id_in_rendered_frame(Some(focus_handle.id));
2246 self.rendered_frame
2247 .dispatch_tree
2248 .is_action_available(action, node_id)
2249 }
2250
2251 /// The position of the mouse relative to the window.
2252 pub fn mouse_position(&self) -> Point<Pixels> {
2253 self.mouse_position
2254 }
2255
2256 /// The current state of the keyboard's modifiers
2257 pub fn modifiers(&self) -> Modifiers {
2258 self.modifiers
2259 }
2260
2261 /// Returns true if the last input event was keyboard-based (key press, tab navigation, etc.)
2262 /// This is used for focus-visible styling to show focus indicators only for keyboard navigation.
2263 pub fn last_input_was_keyboard(&self) -> bool {
2264 self.last_input_modality == InputModality::Keyboard
2265 }
2266
2267 /// The current state of the keyboard's capslock
2268 pub fn capslock(&self) -> Capslock {
2269 self.capslock
2270 }
2271
2272 fn complete_frame(&self) {
2273 self.platform_window.completed_frame();
2274 }
2275
2276 /// Produces a new frame and assigns it to `rendered_frame`. To actually show
2277 /// the contents of the new [`Scene`], use [`Self::present`].
2278 #[profiling::function]
2279 pub fn draw(&mut self, cx: &mut App) -> ArenaClearNeeded {
2280 // Set up the per-App arena for element allocation during this draw.
2281 // This ensures that multiple test Apps have isolated arenas.
2282 let _arena_scope = ElementArenaScope::enter(&cx.element_arena);
2283
2284 self.invalidate_entities();
2285 cx.entities.clear_accessed();
2286 debug_assert!(self.rendered_entity_stack.is_empty());
2287 self.invalidator.set_dirty(false);
2288 self.requested_autoscroll = None;
2289
2290 // Restore the previously-used input handler.
2291 if let Some(input_handler) = self.platform_window.take_input_handler() {
2292 self.rendered_frame.input_handlers.push(Some(input_handler));
2293 }
2294 if !cx.mode.skip_drawing() {
2295 self.draw_roots(cx);
2296 }
2297 self.dirty_views.clear();
2298 self.next_frame.window_active = self.active.get();
2299
2300 // Register requested input handler with the platform window.
2301 if let Some(input_handler) = self.next_frame.input_handlers.pop() {
2302 self.platform_window
2303 .set_input_handler(input_handler.unwrap());
2304 }
2305
2306 self.layout_engine.as_mut().unwrap().clear();
2307 self.text_system().finish_frame();
2308 self.next_frame.finish(&mut self.rendered_frame);
2309
2310 self.invalidator.set_phase(DrawPhase::Focus);
2311 let previous_focus_path = self.rendered_frame.focus_path();
2312 let previous_window_active = self.rendered_frame.window_active;
2313 mem::swap(&mut self.rendered_frame, &mut self.next_frame);
2314 self.next_frame.clear();
2315 let current_focus_path = self.rendered_frame.focus_path();
2316 let current_window_active = self.rendered_frame.window_active;
2317
2318 if previous_focus_path != current_focus_path
2319 || previous_window_active != current_window_active
2320 {
2321 if !previous_focus_path.is_empty() && current_focus_path.is_empty() {
2322 self.focus_lost_listeners
2323 .clone()
2324 .retain(&(), |listener| listener(self, cx));
2325 }
2326
2327 let event = WindowFocusEvent {
2328 previous_focus_path: if previous_window_active {
2329 previous_focus_path
2330 } else {
2331 Default::default()
2332 },
2333 current_focus_path: if current_window_active {
2334 current_focus_path
2335 } else {
2336 Default::default()
2337 },
2338 };
2339 self.focus_listeners
2340 .clone()
2341 .retain(&(), |listener| listener(&event, self, cx));
2342 }
2343
2344 debug_assert!(self.rendered_entity_stack.is_empty());
2345 self.record_entities_accessed(cx);
2346 self.reset_cursor_style(cx);
2347 self.refreshing = false;
2348 self.invalidator.set_phase(DrawPhase::None);
2349 self.needs_present.set(true);
2350
2351 ArenaClearNeeded::new(&cx.element_arena)
2352 }
2353
2354 fn record_entities_accessed(&mut self, cx: &mut App) {
2355 let mut entities_ref = cx.entities.accessed_entities.get_mut();
2356 let mut entities = mem::take(entities_ref.deref_mut());
2357 let handle = self.handle;
2358 cx.record_entities_accessed(
2359 handle,
2360 // Try moving window invalidator into the Window
2361 self.invalidator.clone(),
2362 &entities,
2363 );
2364 let mut entities_ref = cx.entities.accessed_entities.get_mut();
2365 mem::swap(&mut entities, entities_ref.deref_mut());
2366 }
2367
2368 fn invalidate_entities(&mut self) {
2369 let mut views = self.invalidator.take_views();
2370 for entity in views.drain() {
2371 self.mark_view_dirty(entity);
2372 }
2373 self.invalidator.replace_views(views);
2374 }
2375
2376 #[profiling::function]
2377 fn present(&self) {
2378 self.platform_window.draw(&self.rendered_frame.scene);
2379 self.needs_present.set(false);
2380 profiling::finish_frame!();
2381 }
2382
2383 fn draw_roots(&mut self, cx: &mut App) {
2384 self.invalidator.set_phase(DrawPhase::Prepaint);
2385 self.tooltip_bounds.take();
2386
2387 let _inspector_width: Pixels = rems(30.0).to_pixels(self.rem_size());
2388 let root_size = {
2389 #[cfg(any(feature = "inspector", debug_assertions))]
2390 {
2391 if self.inspector.is_some() {
2392 let mut size = self.viewport_size;
2393 size.width = (size.width - _inspector_width).max(px(0.0));
2394 size
2395 } else {
2396 self.viewport_size
2397 }
2398 }
2399 #[cfg(not(any(feature = "inspector", debug_assertions)))]
2400 {
2401 self.viewport_size
2402 }
2403 };
2404
2405 // Layout all root elements.
2406 let mut root_element = self.root.as_ref().unwrap().clone().into_any();
2407 root_element.prepaint_as_root(Point::default(), root_size.into(), self, cx);
2408
2409 #[cfg(any(feature = "inspector", debug_assertions))]
2410 let inspector_element = self.prepaint_inspector(_inspector_width, cx);
2411
2412 let mut sorted_deferred_draws =
2413 (0..self.next_frame.deferred_draws.len()).collect::<SmallVec<[_; 8]>>();
2414 sorted_deferred_draws.sort_by_key(|ix| self.next_frame.deferred_draws[*ix].priority);
2415 self.prepaint_deferred_draws(&sorted_deferred_draws, cx);
2416
2417 let mut prompt_element = None;
2418 let mut active_drag_element = None;
2419 let mut tooltip_element = None;
2420 if let Some(prompt) = self.prompt.take() {
2421 let mut element = prompt.view.any_view().into_any();
2422 element.prepaint_as_root(Point::default(), root_size.into(), self, cx);
2423 prompt_element = Some(element);
2424 self.prompt = Some(prompt);
2425 } else if let Some(active_drag) = cx.active_drag.take() {
2426 let mut element = active_drag.view.clone().into_any();
2427 let offset = self.mouse_position() - active_drag.cursor_offset;
2428 element.prepaint_as_root(offset, AvailableSpace::min_size(), self, cx);
2429 active_drag_element = Some(element);
2430 cx.active_drag = Some(active_drag);
2431 } else {
2432 tooltip_element = self.prepaint_tooltip(cx);
2433 }
2434
2435 self.mouse_hit_test = self.next_frame.hit_test(self.mouse_position);
2436
2437 // Now actually paint the elements.
2438 self.invalidator.set_phase(DrawPhase::Paint);
2439 root_element.paint(self, cx);
2440
2441 #[cfg(any(feature = "inspector", debug_assertions))]
2442 self.paint_inspector(inspector_element, cx);
2443
2444 self.paint_deferred_draws(&sorted_deferred_draws, cx);
2445
2446 if let Some(mut prompt_element) = prompt_element {
2447 prompt_element.paint(self, cx);
2448 } else if let Some(mut drag_element) = active_drag_element {
2449 drag_element.paint(self, cx);
2450 } else if let Some(mut tooltip_element) = tooltip_element {
2451 tooltip_element.paint(self, cx);
2452 }
2453
2454 #[cfg(any(feature = "inspector", debug_assertions))]
2455 self.paint_inspector_hitbox(cx);
2456 }
2457
2458 fn prepaint_tooltip(&mut self, cx: &mut App) -> Option<AnyElement> {
2459 // Use indexing instead of iteration to avoid borrowing self for the duration of the loop.
2460 for tooltip_request_index in (0..self.next_frame.tooltip_requests.len()).rev() {
2461 let Some(Some(tooltip_request)) = self
2462 .next_frame
2463 .tooltip_requests
2464 .get(tooltip_request_index)
2465 .cloned()
2466 else {
2467 log::error!("Unexpectedly absent TooltipRequest");
2468 continue;
2469 };
2470 let mut element = tooltip_request.tooltip.view.clone().into_any();
2471 let mouse_position = tooltip_request.tooltip.mouse_position;
2472 let tooltip_size = element.layout_as_root(AvailableSpace::min_size(), self, cx);
2473
2474 let mut tooltip_bounds =
2475 Bounds::new(mouse_position + point(px(1.), px(1.)), tooltip_size);
2476 let window_bounds = Bounds {
2477 origin: Point::default(),
2478 size: self.viewport_size(),
2479 };
2480
2481 if tooltip_bounds.right() > window_bounds.right() {
2482 let new_x = mouse_position.x - tooltip_bounds.size.width - px(1.);
2483 if new_x >= Pixels::ZERO {
2484 tooltip_bounds.origin.x = new_x;
2485 } else {
2486 tooltip_bounds.origin.x = cmp::max(
2487 Pixels::ZERO,
2488 tooltip_bounds.origin.x - tooltip_bounds.right() - window_bounds.right(),
2489 );
2490 }
2491 }
2492
2493 if tooltip_bounds.bottom() > window_bounds.bottom() {
2494 let new_y = mouse_position.y - tooltip_bounds.size.height - px(1.);
2495 if new_y >= Pixels::ZERO {
2496 tooltip_bounds.origin.y = new_y;
2497 } else {
2498 tooltip_bounds.origin.y = cmp::max(
2499 Pixels::ZERO,
2500 tooltip_bounds.origin.y - tooltip_bounds.bottom() - window_bounds.bottom(),
2501 );
2502 }
2503 }
2504
2505 // It's possible for an element to have an active tooltip while not being painted (e.g.
2506 // via the `visible_on_hover` method). Since mouse listeners are not active in this
2507 // case, instead update the tooltip's visibility here.
2508 let is_visible =
2509 (tooltip_request.tooltip.check_visible_and_update)(tooltip_bounds, self, cx);
2510 if !is_visible {
2511 continue;
2512 }
2513
2514 self.with_absolute_element_offset(tooltip_bounds.origin, |window| {
2515 element.prepaint(window, cx)
2516 });
2517
2518 self.tooltip_bounds = Some(TooltipBounds {
2519 id: tooltip_request.id,
2520 bounds: tooltip_bounds,
2521 });
2522 return Some(element);
2523 }
2524 None
2525 }
2526
2527 fn prepaint_deferred_draws(&mut self, deferred_draw_indices: &[usize], cx: &mut App) {
2528 assert_eq!(self.element_id_stack.len(), 0);
2529
2530 let mut deferred_draws = mem::take(&mut self.next_frame.deferred_draws);
2531 for deferred_draw_ix in deferred_draw_indices {
2532 let deferred_draw = &mut deferred_draws[*deferred_draw_ix];
2533 self.element_id_stack
2534 .clone_from(&deferred_draw.element_id_stack);
2535 self.text_style_stack
2536 .clone_from(&deferred_draw.text_style_stack);
2537 self.next_frame
2538 .dispatch_tree
2539 .set_active_node(deferred_draw.parent_node);
2540
2541 let prepaint_start = self.prepaint_index();
2542 let content_mask = deferred_draw.content_mask.clone();
2543 if let Some(element) = deferred_draw.element.as_mut() {
2544 self.with_rendered_view(deferred_draw.current_view, |window| {
2545 window.with_content_mask(content_mask, |window| {
2546 window.with_rem_size(Some(deferred_draw.rem_size), |window| {
2547 window.with_absolute_element_offset(
2548 deferred_draw.absolute_offset,
2549 |window| {
2550 element.prepaint(window, cx);
2551 },
2552 );
2553 });
2554 });
2555 })
2556 } else {
2557 self.reuse_prepaint(deferred_draw.prepaint_range.clone());
2558 }
2559 let prepaint_end = self.prepaint_index();
2560 deferred_draw.prepaint_range = prepaint_start..prepaint_end;
2561 }
2562 assert_eq!(
2563 self.next_frame.deferred_draws.len(),
2564 0,
2565 "cannot call defer_draw during deferred drawing"
2566 );
2567 self.next_frame.deferred_draws = deferred_draws;
2568 self.element_id_stack.clear();
2569 self.text_style_stack.clear();
2570 }
2571
2572 fn paint_deferred_draws(&mut self, deferred_draw_indices: &[usize], cx: &mut App) {
2573 assert_eq!(self.element_id_stack.len(), 0);
2574
2575 let mut deferred_draws = mem::take(&mut self.next_frame.deferred_draws);
2576 for deferred_draw_ix in deferred_draw_indices {
2577 let mut deferred_draw = &mut deferred_draws[*deferred_draw_ix];
2578 self.element_id_stack
2579 .clone_from(&deferred_draw.element_id_stack);
2580 self.next_frame
2581 .dispatch_tree
2582 .set_active_node(deferred_draw.parent_node);
2583
2584 let paint_start = self.paint_index();
2585 let content_mask = deferred_draw.content_mask.clone();
2586 if let Some(element) = deferred_draw.element.as_mut() {
2587 self.with_rendered_view(deferred_draw.current_view, |window| {
2588 window.with_content_mask(content_mask, |window| {
2589 window.with_rem_size(Some(deferred_draw.rem_size), |window| {
2590 element.paint(window, cx);
2591 });
2592 })
2593 })
2594 } else {
2595 self.reuse_paint(deferred_draw.paint_range.clone());
2596 }
2597 let paint_end = self.paint_index();
2598 deferred_draw.paint_range = paint_start..paint_end;
2599 }
2600 self.next_frame.deferred_draws = deferred_draws;
2601 self.element_id_stack.clear();
2602 }
2603
2604 pub(crate) fn prepaint_index(&self) -> PrepaintStateIndex {
2605 PrepaintStateIndex {
2606 hitboxes_index: self.next_frame.hitboxes.len(),
2607 tooltips_index: self.next_frame.tooltip_requests.len(),
2608 deferred_draws_index: self.next_frame.deferred_draws.len(),
2609 dispatch_tree_index: self.next_frame.dispatch_tree.len(),
2610 accessed_element_states_index: self.next_frame.accessed_element_states.len(),
2611 line_layout_index: self.text_system.layout_index(),
2612 }
2613 }
2614
2615 pub(crate) fn reuse_prepaint(&mut self, range: Range<PrepaintStateIndex>) {
2616 self.next_frame.hitboxes.extend(
2617 self.rendered_frame.hitboxes[range.start.hitboxes_index..range.end.hitboxes_index]
2618 .iter()
2619 .cloned(),
2620 );
2621 self.next_frame.tooltip_requests.extend(
2622 self.rendered_frame.tooltip_requests
2623 [range.start.tooltips_index..range.end.tooltips_index]
2624 .iter_mut()
2625 .map(|request| request.take()),
2626 );
2627 self.next_frame.accessed_element_states.extend(
2628 self.rendered_frame.accessed_element_states[range.start.accessed_element_states_index
2629 ..range.end.accessed_element_states_index]
2630 .iter()
2631 .map(|(id, type_id)| (id.clone(), *type_id)),
2632 );
2633 self.text_system
2634 .reuse_layouts(range.start.line_layout_index..range.end.line_layout_index);
2635
2636 let reused_subtree = self.next_frame.dispatch_tree.reuse_subtree(
2637 range.start.dispatch_tree_index..range.end.dispatch_tree_index,
2638 &mut self.rendered_frame.dispatch_tree,
2639 self.focus,
2640 );
2641
2642 if reused_subtree.contains_focus() {
2643 self.next_frame.focus = self.focus;
2644 }
2645
2646 self.next_frame.deferred_draws.extend(
2647 self.rendered_frame.deferred_draws
2648 [range.start.deferred_draws_index..range.end.deferred_draws_index]
2649 .iter()
2650 .map(|deferred_draw| DeferredDraw {
2651 current_view: deferred_draw.current_view,
2652 parent_node: reused_subtree.refresh_node_id(deferred_draw.parent_node),
2653 element_id_stack: deferred_draw.element_id_stack.clone(),
2654 text_style_stack: deferred_draw.text_style_stack.clone(),
2655 content_mask: deferred_draw.content_mask.clone(),
2656 rem_size: deferred_draw.rem_size,
2657 priority: deferred_draw.priority,
2658 element: None,
2659 absolute_offset: deferred_draw.absolute_offset,
2660 prepaint_range: deferred_draw.prepaint_range.clone(),
2661 paint_range: deferred_draw.paint_range.clone(),
2662 }),
2663 );
2664 }
2665
2666 pub(crate) fn paint_index(&self) -> PaintIndex {
2667 PaintIndex {
2668 scene_index: self.next_frame.scene.len(),
2669 mouse_listeners_index: self.next_frame.mouse_listeners.len(),
2670 input_handlers_index: self.next_frame.input_handlers.len(),
2671 cursor_styles_index: self.next_frame.cursor_styles.len(),
2672 accessed_element_states_index: self.next_frame.accessed_element_states.len(),
2673 tab_handle_index: self.next_frame.tab_stops.paint_index(),
2674 line_layout_index: self.text_system.layout_index(),
2675 }
2676 }
2677
2678 pub(crate) fn reuse_paint(&mut self, range: Range<PaintIndex>) {
2679 self.next_frame.cursor_styles.extend(
2680 self.rendered_frame.cursor_styles
2681 [range.start.cursor_styles_index..range.end.cursor_styles_index]
2682 .iter()
2683 .cloned(),
2684 );
2685 self.next_frame.input_handlers.extend(
2686 self.rendered_frame.input_handlers
2687 [range.start.input_handlers_index..range.end.input_handlers_index]
2688 .iter_mut()
2689 .map(|handler| handler.take()),
2690 );
2691 self.next_frame.mouse_listeners.extend(
2692 self.rendered_frame.mouse_listeners
2693 [range.start.mouse_listeners_index..range.end.mouse_listeners_index]
2694 .iter_mut()
2695 .map(|listener| listener.take()),
2696 );
2697 self.next_frame.accessed_element_states.extend(
2698 self.rendered_frame.accessed_element_states[range.start.accessed_element_states_index
2699 ..range.end.accessed_element_states_index]
2700 .iter()
2701 .map(|(id, type_id)| (id.clone(), *type_id)),
2702 );
2703 self.next_frame.tab_stops.replay(
2704 &self.rendered_frame.tab_stops.insertion_history
2705 [range.start.tab_handle_index..range.end.tab_handle_index],
2706 );
2707
2708 self.text_system
2709 .reuse_layouts(range.start.line_layout_index..range.end.line_layout_index);
2710 self.next_frame.scene.replay(
2711 range.start.scene_index..range.end.scene_index,
2712 &self.rendered_frame.scene,
2713 );
2714 }
2715
2716 /// Push a text style onto the stack, and call a function with that style active.
2717 /// Use [`Window::text_style`] to get the current, combined text style. This method
2718 /// should only be called as part of element drawing.
2719 // This function is called in a highly recursive manner in editor
2720 // prepainting, make sure its inlined to reduce the stack burden
2721 #[inline]
2722 pub fn with_text_style<F, R>(&mut self, style: Option<TextStyleRefinement>, f: F) -> R
2723 where
2724 F: FnOnce(&mut Self) -> R,
2725 {
2726 self.invalidator.debug_assert_paint_or_prepaint();
2727 if let Some(style) = style {
2728 self.text_style_stack.push(style);
2729 let result = f(self);
2730 self.text_style_stack.pop();
2731 result
2732 } else {
2733 f(self)
2734 }
2735 }
2736
2737 /// Updates the cursor style at the platform level. This method should only be called
2738 /// during the paint phase of element drawing.
2739 pub fn set_cursor_style(&mut self, style: CursorStyle, hitbox: &Hitbox) {
2740 self.invalidator.debug_assert_paint();
2741 self.next_frame.cursor_styles.push(CursorStyleRequest {
2742 hitbox_id: Some(hitbox.id),
2743 style,
2744 });
2745 }
2746
2747 /// Updates the cursor style for the entire window at the platform level. A cursor
2748 /// style using this method will have precedence over any cursor style set using
2749 /// `set_cursor_style`. This method should only be called during the paint
2750 /// phase of element drawing.
2751 pub fn set_window_cursor_style(&mut self, style: CursorStyle) {
2752 self.invalidator.debug_assert_paint();
2753 self.next_frame.cursor_styles.push(CursorStyleRequest {
2754 hitbox_id: None,
2755 style,
2756 })
2757 }
2758
2759 /// Sets a tooltip to be rendered for the upcoming frame. This method should only be called
2760 /// during the paint phase of element drawing.
2761 pub fn set_tooltip(&mut self, tooltip: AnyTooltip) -> TooltipId {
2762 self.invalidator.debug_assert_prepaint();
2763 let id = TooltipId(post_inc(&mut self.next_tooltip_id.0));
2764 self.next_frame
2765 .tooltip_requests
2766 .push(Some(TooltipRequest { id, tooltip }));
2767 id
2768 }
2769
2770 /// Invoke the given function with the given content mask after intersecting it
2771 /// with the current mask. This method should only be called during element drawing.
2772 // This function is called in a highly recursive manner in editor
2773 // prepainting, make sure its inlined to reduce the stack burden
2774 #[inline]
2775 pub fn with_content_mask<R>(
2776 &mut self,
2777 mask: Option<ContentMask<Pixels>>,
2778 f: impl FnOnce(&mut Self) -> R,
2779 ) -> R {
2780 self.invalidator.debug_assert_paint_or_prepaint();
2781 if let Some(mask) = mask {
2782 let mask = mask.intersect(&self.content_mask());
2783 self.content_mask_stack.push(mask);
2784 let result = f(self);
2785 self.content_mask_stack.pop();
2786 result
2787 } else {
2788 f(self)
2789 }
2790 }
2791
2792 /// Updates the global element offset relative to the current offset. This is used to implement
2793 /// scrolling. This method should only be called during the prepaint phase of element drawing.
2794 pub fn with_element_offset<R>(
2795 &mut self,
2796 offset: Point<Pixels>,
2797 f: impl FnOnce(&mut Self) -> R,
2798 ) -> R {
2799 self.invalidator.debug_assert_prepaint();
2800
2801 if offset.is_zero() {
2802 return f(self);
2803 };
2804
2805 let abs_offset = self.round_point_to_device_pixels(self.element_offset() + offset);
2806 self.with_absolute_element_offset(abs_offset, f)
2807 }
2808
2809 /// Updates the global element offset based on the given offset. This is used to implement
2810 /// drag handles and other manual painting of elements. This method should only be called during
2811 /// the prepaint phase of element drawing.
2812 pub fn with_absolute_element_offset<R>(
2813 &mut self,
2814 offset: Point<Pixels>,
2815 f: impl FnOnce(&mut Self) -> R,
2816 ) -> R {
2817 self.invalidator.debug_assert_prepaint();
2818 self.element_offset_stack
2819 .push(self.round_point_to_device_pixels(offset));
2820 let result = f(self);
2821 self.element_offset_stack.pop();
2822 result
2823 }
2824
2825 pub(crate) fn with_element_opacity<R>(
2826 &mut self,
2827 opacity: Option<f32>,
2828 f: impl FnOnce(&mut Self) -> R,
2829 ) -> R {
2830 self.invalidator.debug_assert_paint_or_prepaint();
2831
2832 let Some(opacity) = opacity else {
2833 return f(self);
2834 };
2835
2836 let previous_opacity = self.element_opacity;
2837 self.element_opacity = previous_opacity * opacity;
2838 let result = f(self);
2839 self.element_opacity = previous_opacity;
2840 result
2841 }
2842
2843 /// Perform prepaint on child elements in a "retryable" manner, so that any side effects
2844 /// of prepaints can be discarded before prepainting again. This is used to support autoscroll
2845 /// where we need to prepaint children to detect the autoscroll bounds, then adjust the
2846 /// element offset and prepaint again. See [`crate::List`] for an example. This method should only be
2847 /// called during the prepaint phase of element drawing.
2848 pub fn transact<T, U>(&mut self, f: impl FnOnce(&mut Self) -> Result<T, U>) -> Result<T, U> {
2849 self.invalidator.debug_assert_prepaint();
2850 let index = self.prepaint_index();
2851 let result = f(self);
2852 if result.is_err() {
2853 self.next_frame.hitboxes.truncate(index.hitboxes_index);
2854 self.next_frame
2855 .tooltip_requests
2856 .truncate(index.tooltips_index);
2857 self.next_frame
2858 .deferred_draws
2859 .truncate(index.deferred_draws_index);
2860 self.next_frame
2861 .dispatch_tree
2862 .truncate(index.dispatch_tree_index);
2863 self.next_frame
2864 .accessed_element_states
2865 .truncate(index.accessed_element_states_index);
2866 self.text_system.truncate_layouts(index.line_layout_index);
2867 }
2868 result
2869 }
2870
2871 /// When you call this method during [`Element::prepaint`], containing elements will attempt to
2872 /// scroll to cause the specified bounds to become visible. When they decide to autoscroll, they will call
2873 /// [`Element::prepaint`] again with a new set of bounds. See [`crate::List`] for an example of an element
2874 /// that supports this method being called on the elements it contains. This method should only be
2875 /// called during the prepaint phase of element drawing.
2876 pub fn request_autoscroll(&mut self, bounds: Bounds<Pixels>) {
2877 self.invalidator.debug_assert_prepaint();
2878 self.requested_autoscroll = Some(bounds);
2879 }
2880
2881 /// This method can be called from a containing element such as [`crate::List`] to support the autoscroll behavior
2882 /// described in [`Self::request_autoscroll`].
2883 pub fn take_autoscroll(&mut self) -> Option<Bounds<Pixels>> {
2884 self.invalidator.debug_assert_prepaint();
2885 self.requested_autoscroll.take()
2886 }
2887
2888 /// Asynchronously load an asset, if the asset hasn't finished loading this will return None.
2889 /// Your view will be re-drawn once the asset has finished loading.
2890 ///
2891 /// Note that the multiple calls to this method will only result in one `Asset::load` call at a
2892 /// time.
2893 pub fn use_asset<A: Asset>(&mut self, source: &A::Source, cx: &mut App) -> Option<A::Output> {
2894 let (task, is_first) = cx.fetch_asset::<A>(source);
2895 task.clone().now_or_never().or_else(|| {
2896 if is_first {
2897 let entity_id = self.current_view();
2898 self.spawn(cx, {
2899 let task = task.clone();
2900 async move |cx| {
2901 task.await;
2902
2903 cx.on_next_frame(move |_, cx| {
2904 cx.notify(entity_id);
2905 });
2906 }
2907 })
2908 .detach();
2909 }
2910
2911 None
2912 })
2913 }
2914
2915 /// Asynchronously load an asset, if the asset hasn't finished loading or doesn't exist this will return None.
2916 /// Your view will not be re-drawn once the asset has finished loading.
2917 ///
2918 /// Note that the multiple calls to this method will only result in one `Asset::load` call at a
2919 /// time.
2920 pub fn get_asset<A: Asset>(&mut self, source: &A::Source, cx: &mut App) -> Option<A::Output> {
2921 let (task, _) = cx.fetch_asset::<A>(source);
2922 task.now_or_never()
2923 }
2924 /// Obtain the current element offset. This method should only be called during the
2925 /// prepaint phase of element drawing.
2926 pub fn element_offset(&self) -> Point<Pixels> {
2927 self.invalidator.debug_assert_prepaint();
2928 self.element_offset_stack
2929 .last()
2930 .copied()
2931 .unwrap_or_default()
2932 }
2933
2934 /// Obtain the current element opacity. This method should only be called during the
2935 /// prepaint phase of element drawing.
2936 #[inline]
2937 pub(crate) fn element_opacity(&self) -> f32 {
2938 self.invalidator.debug_assert_paint_or_prepaint();
2939 self.element_opacity
2940 }
2941
2942 /// Obtain the current content mask. This method should only be called during element drawing.
2943 pub fn content_mask(&self) -> ContentMask<Pixels> {
2944 self.invalidator.debug_assert_paint_or_prepaint();
2945 self.content_mask_stack
2946 .last()
2947 .cloned()
2948 .unwrap_or_else(|| ContentMask {
2949 bounds: Bounds {
2950 origin: Point::default(),
2951 size: self.viewport_size,
2952 },
2953 })
2954 }
2955
2956 /// Provide elements in the called function with a new namespace in which their identifiers must be unique.
2957 /// This can be used within a custom element to distinguish multiple sets of child elements.
2958 pub fn with_element_namespace<R>(
2959 &mut self,
2960 element_id: impl Into<ElementId>,
2961 f: impl FnOnce(&mut Self) -> R,
2962 ) -> R {
2963 self.element_id_stack.push(element_id.into());
2964 let result = f(self);
2965 self.element_id_stack.pop();
2966 result
2967 }
2968
2969 /// Use a piece of state that exists as long this element is being rendered in consecutive frames.
2970 pub fn use_keyed_state<S: 'static>(
2971 &mut self,
2972 key: impl Into<ElementId>,
2973 cx: &mut App,
2974 init: impl FnOnce(&mut Self, &mut Context<S>) -> S,
2975 ) -> Entity<S> {
2976 let current_view = self.current_view();
2977 self.with_global_id(key.into(), |global_id, window| {
2978 window.with_element_state(global_id, |state: Option<Entity<S>>, window| {
2979 if let Some(state) = state {
2980 (state.clone(), state)
2981 } else {
2982 let new_state = cx.new(|cx| init(window, cx));
2983 cx.observe(&new_state, move |_, cx| {
2984 cx.notify(current_view);
2985 })
2986 .detach();
2987 (new_state.clone(), new_state)
2988 }
2989 })
2990 })
2991 }
2992
2993 /// Use a piece of state that exists as long this element is being rendered in consecutive frames, without needing to specify a key
2994 ///
2995 /// NOTE: This method uses the location of the caller to generate an ID for this state.
2996 /// If this is not sufficient to identify your state (e.g. you're rendering a list item),
2997 /// you can provide a custom ElementID using the `use_keyed_state` method.
2998 #[track_caller]
2999 pub fn use_state<S: 'static>(
3000 &mut self,
3001 cx: &mut App,
3002 init: impl FnOnce(&mut Self, &mut Context<S>) -> S,
3003 ) -> Entity<S> {
3004 self.use_keyed_state(
3005 ElementId::CodeLocation(*core::panic::Location::caller()),
3006 cx,
3007 init,
3008 )
3009 }
3010
3011 /// Updates or initializes state for an element with the given id that lives across multiple
3012 /// frames. If an element with this ID existed in the rendered frame, its state will be passed
3013 /// to the given closure. The state returned by the closure will be stored so it can be referenced
3014 /// when drawing the next frame. This method should only be called as part of element drawing.
3015 pub fn with_element_state<S, R>(
3016 &mut self,
3017 global_id: &GlobalElementId,
3018 f: impl FnOnce(Option<S>, &mut Self) -> (R, S),
3019 ) -> R
3020 where
3021 S: 'static,
3022 {
3023 self.invalidator.debug_assert_paint_or_prepaint();
3024
3025 let key = (global_id.clone(), TypeId::of::<S>());
3026 self.next_frame.accessed_element_states.push(key.clone());
3027
3028 if let Some(any) = self
3029 .next_frame
3030 .element_states
3031 .remove(&key)
3032 .or_else(|| self.rendered_frame.element_states.remove(&key))
3033 {
3034 let ElementStateBox {
3035 inner,
3036 #[cfg(debug_assertions)]
3037 type_name,
3038 } = any;
3039 // Using the extra inner option to avoid needing to reallocate a new box.
3040 let mut state_box = inner
3041 .downcast::<Option<S>>()
3042 .map_err(|_| {
3043 #[cfg(debug_assertions)]
3044 {
3045 anyhow::anyhow!(
3046 "invalid element state type for id, requested {:?}, actual: {:?}",
3047 std::any::type_name::<S>(),
3048 type_name
3049 )
3050 }
3051
3052 #[cfg(not(debug_assertions))]
3053 {
3054 anyhow::anyhow!(
3055 "invalid element state type for id, requested {:?}",
3056 std::any::type_name::<S>(),
3057 )
3058 }
3059 })
3060 .unwrap();
3061
3062 let state = state_box.take().expect(
3063 "reentrant call to with_element_state for the same state type and element id",
3064 );
3065 let (result, state) = f(Some(state), self);
3066 state_box.replace(state);
3067 self.next_frame.element_states.insert(
3068 key,
3069 ElementStateBox {
3070 inner: state_box,
3071 #[cfg(debug_assertions)]
3072 type_name,
3073 },
3074 );
3075 result
3076 } else {
3077 let (result, state) = f(None, self);
3078 self.next_frame.element_states.insert(
3079 key,
3080 ElementStateBox {
3081 inner: Box::new(Some(state)),
3082 #[cfg(debug_assertions)]
3083 type_name: std::any::type_name::<S>(),
3084 },
3085 );
3086 result
3087 }
3088 }
3089
3090 /// A variant of `with_element_state` that allows the element's id to be optional. This is a convenience
3091 /// method for elements where the element id may or may not be assigned. Prefer using `with_element_state`
3092 /// when the element is guaranteed to have an id.
3093 ///
3094 /// The first option means 'no ID provided'
3095 /// The second option means 'not yet initialized'
3096 pub fn with_optional_element_state<S, R>(
3097 &mut self,
3098 global_id: Option<&GlobalElementId>,
3099 f: impl FnOnce(Option<Option<S>>, &mut Self) -> (R, Option<S>),
3100 ) -> R
3101 where
3102 S: 'static,
3103 {
3104 self.invalidator.debug_assert_paint_or_prepaint();
3105
3106 if let Some(global_id) = global_id {
3107 self.with_element_state(global_id, |state, cx| {
3108 let (result, state) = f(Some(state), cx);
3109 let state =
3110 state.expect("you must return some state when you pass some element id");
3111 (result, state)
3112 })
3113 } else {
3114 let (result, state) = f(None, self);
3115 debug_assert!(
3116 state.is_none(),
3117 "you must not return an element state when passing None for the global id"
3118 );
3119 result
3120 }
3121 }
3122
3123 /// Executes the given closure within the context of a tab group.
3124 #[inline]
3125 pub fn with_tab_group<R>(&mut self, index: Option<isize>, f: impl FnOnce(&mut Self) -> R) -> R {
3126 if let Some(index) = index {
3127 self.next_frame.tab_stops.begin_group(index);
3128 let result = f(self);
3129 self.next_frame.tab_stops.end_group();
3130 result
3131 } else {
3132 f(self)
3133 }
3134 }
3135
3136 /// Defers the drawing of the given element, scheduling it to be painted on top of the currently-drawn tree
3137 /// at a later time. The `priority` parameter determines the drawing order relative to other deferred elements,
3138 /// with higher values being drawn on top.
3139 ///
3140 /// When `content_mask` is provided, the deferred element will be clipped to that region during
3141 /// both prepaint and paint. When `None`, no additional clipping is applied.
3142 ///
3143 /// This method should only be called as part of the prepaint phase of element drawing.
3144 pub fn defer_draw(
3145 &mut self,
3146 element: AnyElement,
3147 absolute_offset: Point<Pixels>,
3148 priority: usize,
3149 content_mask: Option<ContentMask<Pixels>>,
3150 ) {
3151 self.invalidator.debug_assert_prepaint();
3152 let parent_node = self.next_frame.dispatch_tree.active_node_id().unwrap();
3153 self.next_frame.deferred_draws.push(DeferredDraw {
3154 current_view: self.current_view(),
3155 parent_node,
3156 element_id_stack: self.element_id_stack.clone(),
3157 text_style_stack: self.text_style_stack.clone(),
3158 content_mask,
3159 rem_size: self.rem_size(),
3160 priority,
3161 element: Some(element),
3162 absolute_offset,
3163 prepaint_range: PrepaintStateIndex::default()..PrepaintStateIndex::default(),
3164 paint_range: PaintIndex::default()..PaintIndex::default(),
3165 });
3166 }
3167
3168 /// Creates a new painting layer for the specified bounds. A "layer" is a batch
3169 /// of geometry that are non-overlapping and have the same draw order. This is typically used
3170 /// for performance reasons.
3171 ///
3172 /// This method should only be called as part of the paint phase of element drawing.
3173 pub fn paint_layer<R>(&mut self, bounds: Bounds<Pixels>, f: impl FnOnce(&mut Self) -> R) -> R {
3174 self.invalidator.debug_assert_paint();
3175
3176 let content_mask = self.content_mask();
3177 let clipped_bounds = bounds.intersect(&content_mask.bounds);
3178 if !clipped_bounds.is_empty() {
3179 self.next_frame
3180 .scene
3181 .push_layer(self.outset_bounds_to_device_pixels(clipped_bounds));
3182 }
3183
3184 let result = f(self);
3185
3186 if !clipped_bounds.is_empty() {
3187 self.next_frame.scene.pop_layer();
3188 }
3189
3190 result
3191 }
3192
3193 /// Paint one or more drop shadows into the scene for the next frame at the current z-index.
3194 ///
3195 /// This method should only be called as part of the paint phase of element drawing.
3196 pub fn paint_shadows(
3197 &mut self,
3198 bounds: Bounds<Pixels>,
3199 corner_radii: Corners<Pixels>,
3200 shadows: &[BoxShadow],
3201 ) {
3202 self.invalidator.debug_assert_paint();
3203
3204 let content_mask = self.content_mask();
3205 let scale_factor = self.scale_factor();
3206 let opacity = self.element_opacity();
3207 for shadow in shadows {
3208 let shadow_bounds = (bounds + shadow.offset).dilate(shadow.spread_radius);
3209 self.next_frame.scene.insert_primitive(Shadow {
3210 order: 0,
3211 blur_radius: shadow.blur_radius.scale(scale_factor),
3212 bounds: self.outset_bounds_to_device_pixels(shadow_bounds),
3213 content_mask: self.outset_content_mask_to_device_pixels(content_mask.clone()),
3214 corner_radii: corner_radii.scale(scale_factor),
3215 color: shadow.color.opacity(opacity),
3216 });
3217 }
3218 }
3219
3220 /// Paint one or more quads into the scene for the next frame at the current stacking context.
3221 /// Quads are colored rectangular regions with an optional background, border, and corner radius.
3222 /// see [`fill`], [`outline`], and [`quad`] to construct this type.
3223 ///
3224 /// This method should only be called as part of the paint phase of element drawing.
3225 ///
3226 /// Note that the `quad.corner_radii` are allowed to exceed the bounds, creating sharp corners
3227 /// where the circular arcs meet. This will not display well when combined with dashed borders.
3228 /// Use `Corners::clamp_radii_for_quad_size` if the radii should fit within the bounds.
3229 pub fn paint_quad(&mut self, quad: PaintQuad) {
3230 self.invalidator.debug_assert_paint();
3231
3232 let content_mask = self.content_mask();
3233 let opacity = self.element_opacity();
3234 self.next_frame.scene.insert_primitive(Quad {
3235 order: 0,
3236 bounds: self.round_bounds_to_device_pixels(quad.bounds),
3237 content_mask: self.outset_content_mask_to_device_pixels(content_mask),
3238 background: quad.background.opacity(opacity),
3239 border_color: quad.border_color.opacity(opacity),
3240 corner_radii: quad.corner_radii.scale(self.scale_factor()),
3241 border_widths: self.round_edges_to_device_pixels(quad.border_widths),
3242 border_style: quad.border_style,
3243 });
3244 }
3245
3246 /// Paint the given `Path` into the scene for the next frame at the current z-index.
3247 ///
3248 /// This method should only be called as part of the paint phase of element drawing.
3249 pub fn paint_path(&mut self, mut path: Path<Pixels>, color: impl Into<Background>) {
3250 self.invalidator.debug_assert_paint();
3251
3252 let scale_factor = self.scale_factor();
3253 let content_mask = self.content_mask();
3254 let opacity = self.element_opacity();
3255 path.content_mask = content_mask;
3256 let color: Background = color.into();
3257 path.color = color.opacity(opacity);
3258 self.next_frame
3259 .scene
3260 .insert_primitive(path.scale(scale_factor));
3261 }
3262
3263 /// Paint an underline into the scene for the next frame at the current z-index.
3264 ///
3265 /// This method should only be called as part of the paint phase of element drawing.
3266 pub fn paint_underline(
3267 &mut self,
3268 origin: Point<Pixels>,
3269 width: Pixels,
3270 style: &UnderlineStyle,
3271 ) {
3272 self.invalidator.debug_assert_paint();
3273
3274 let scale_factor = self.scale_factor();
3275 let height = if style.wavy {
3276 style.thickness * 3.
3277 } else {
3278 style.thickness
3279 };
3280 let bounds = Bounds {
3281 origin: point(
3282 ScaledPixels(round_half_toward_zero(origin.x.0 * scale_factor)),
3283 ScaledPixels(round_half_toward_zero(origin.y.0 * scale_factor)),
3284 ),
3285 size: size(
3286 self.round_nonzero_length_to_device_pixels(width),
3287 self.round_nonzero_length_to_device_pixels(height),
3288 ),
3289 };
3290 let content_mask = self.content_mask();
3291 let element_opacity = self.element_opacity();
3292
3293 self.next_frame.scene.insert_primitive(Underline {
3294 order: 0,
3295 pad: 0,
3296 bounds,
3297 content_mask: self.outset_content_mask_to_device_pixels(content_mask),
3298 color: style.color.unwrap_or_default().opacity(element_opacity),
3299 thickness: self.round_nonzero_length_to_device_pixels(style.thickness),
3300 wavy: if style.wavy { 1 } else { 0 },
3301 });
3302 }
3303
3304 /// Paint a strikethrough into the scene for the next frame at the current z-index.
3305 ///
3306 /// This method should only be called as part of the paint phase of element drawing.
3307 pub fn paint_strikethrough(
3308 &mut self,
3309 origin: Point<Pixels>,
3310 width: Pixels,
3311 style: &StrikethroughStyle,
3312 ) {
3313 self.invalidator.debug_assert_paint();
3314
3315 let scale_factor = self.scale_factor();
3316 let height = style.thickness;
3317 let bounds = Bounds {
3318 origin: point(
3319 ScaledPixels(round_half_toward_zero(origin.x.0 * scale_factor)),
3320 ScaledPixels(round_half_toward_zero(origin.y.0 * scale_factor)),
3321 ),
3322 size: size(
3323 self.round_nonzero_length_to_device_pixels(width),
3324 self.round_nonzero_length_to_device_pixels(height),
3325 ),
3326 };
3327 let content_mask = self.content_mask();
3328 let opacity = self.element_opacity();
3329
3330 self.next_frame.scene.insert_primitive(Underline {
3331 order: 0,
3332 pad: 0,
3333 bounds,
3334 content_mask: self.outset_content_mask_to_device_pixels(content_mask),
3335 thickness: self.round_nonzero_length_to_device_pixels(style.thickness),
3336 color: style.color.unwrap_or_default().opacity(opacity),
3337 wavy: 0,
3338 });
3339 }
3340
3341 /// Paints a monochrome (non-emoji) glyph into the scene for the next frame at the current z-index.
3342 ///
3343 /// The y component of the origin is the baseline of the glyph.
3344 /// You should generally prefer to use the [`ShapedLine::paint`](crate::ShapedLine::paint) or
3345 /// [`WrappedLine::paint`](crate::WrappedLine::paint) methods in the [`TextSystem`](crate::TextSystem).
3346 /// This method is only useful if you need to paint a single glyph that has already been shaped.
3347 ///
3348 /// This method should only be called as part of the paint phase of element drawing.
3349 pub fn paint_glyph(
3350 &mut self,
3351 origin: Point<Pixels>,
3352 font_id: FontId,
3353 glyph_id: GlyphId,
3354 font_size: Pixels,
3355 color: Hsla,
3356 ) -> Result<()> {
3357 self.invalidator.debug_assert_paint();
3358
3359 let element_opacity = self.element_opacity();
3360 let scale_factor = self.scale_factor();
3361 let glyph_origin = origin.scale(scale_factor);
3362
3363 let subpixel_variant = Point {
3364 x: (glyph_origin.x.0.fract() * SUBPIXEL_VARIANTS_X as f32).floor() as u8,
3365 // Keep vertical glyph rasterization stable while scrolling. Y-position is
3366 // snapped at quad placement time, so varying Y subpixel glyph variants only
3367 // introduces frame-to-frame wobble.
3368 y: 0,
3369 };
3370 let subpixel_rendering = self.should_use_subpixel_rendering(font_id, font_size);
3371 let params = RenderGlyphParams {
3372 font_id,
3373 glyph_id,
3374 font_size,
3375 subpixel_variant,
3376 scale_factor,
3377 is_emoji: false,
3378 subpixel_rendering,
3379 };
3380
3381 let raster_bounds = self.text_system().raster_bounds(¶ms)?;
3382 if !raster_bounds.is_zero() {
3383 let tile = self
3384 .sprite_atlas
3385 .get_or_insert_with(¶ms.clone().into(), &mut || {
3386 let (size, bytes) = self.text_system().rasterize_glyph(¶ms)?;
3387 Ok(Some((size, Cow::Owned(bytes))))
3388 })?
3389 .expect("Callback above only errors or returns Some");
3390 let bounds = Bounds {
3391 // X uses floor because subpixel_variant.x captures the fractional
3392 // part — the glyph raster already contains the sub-pixel shift.
3393 // Y uses round because there is no Y subpixel variant (y: 0),
3394 // so rounding gives the most accurate integer placement.
3395 origin: point(glyph_origin.x.floor(), ScaledPixels(round_half_toward_zero(glyph_origin.y.0)))
3396 + raster_bounds.origin.map(Into::into),
3397 size: tile.bounds.size.map(Into::into),
3398 };
3399 let content_mask = self.outset_content_mask_to_device_pixels(self.content_mask());
3400
3401 if subpixel_rendering {
3402 self.next_frame.scene.insert_primitive(SubpixelSprite {
3403 order: 0,
3404 pad: 0,
3405 bounds,
3406 content_mask,
3407 color: color.opacity(element_opacity),
3408 tile,
3409 transformation: TransformationMatrix::unit(),
3410 });
3411 } else {
3412 self.next_frame.scene.insert_primitive(MonochromeSprite {
3413 order: 0,
3414 pad: 0,
3415 bounds,
3416 content_mask,
3417 color: color.opacity(element_opacity),
3418 tile,
3419 transformation: TransformationMatrix::unit(),
3420 });
3421 }
3422 }
3423 Ok(())
3424 }
3425
3426 fn should_use_subpixel_rendering(&self, font_id: FontId, font_size: Pixels) -> bool {
3427 if self.platform_window.background_appearance() != WindowBackgroundAppearance::Opaque {
3428 return false;
3429 }
3430
3431 if !self.platform_window.is_subpixel_rendering_supported() {
3432 return false;
3433 }
3434
3435 let mode = match self.text_rendering_mode.get() {
3436 TextRenderingMode::PlatformDefault => self
3437 .text_system()
3438 .recommended_rendering_mode(font_id, font_size),
3439 mode => mode,
3440 };
3441
3442 mode == TextRenderingMode::Subpixel
3443 }
3444
3445 /// Paints an emoji glyph into the scene for the next frame at the current z-index.
3446 ///
3447 /// The y component of the origin is the baseline of the glyph.
3448 /// You should generally prefer to use the [`ShapedLine::paint`](crate::ShapedLine::paint) or
3449 /// [`WrappedLine::paint`](crate::WrappedLine::paint) methods in the [`TextSystem`](crate::TextSystem).
3450 /// This method is only useful if you need to paint a single emoji that has already been shaped.
3451 ///
3452 /// This method should only be called as part of the paint phase of element drawing.
3453 pub fn paint_emoji(
3454 &mut self,
3455 origin: Point<Pixels>,
3456 font_id: FontId,
3457 glyph_id: GlyphId,
3458 font_size: Pixels,
3459 ) -> Result<()> {
3460 self.invalidator.debug_assert_paint();
3461
3462 let scale_factor = self.scale_factor();
3463 let glyph_origin = origin.scale(scale_factor);
3464 let params = RenderGlyphParams {
3465 font_id,
3466 glyph_id,
3467 font_size,
3468 // We don't render emojis with subpixel variants.
3469 subpixel_variant: Default::default(),
3470 scale_factor,
3471 is_emoji: true,
3472 subpixel_rendering: false,
3473 };
3474
3475 let raster_bounds = self.text_system().raster_bounds(¶ms)?;
3476 if !raster_bounds.is_zero() {
3477 let tile = self
3478 .sprite_atlas
3479 .get_or_insert_with(¶ms.clone().into(), &mut || {
3480 let (size, bytes) = self.text_system().rasterize_glyph(¶ms)?;
3481 Ok(Some((size, Cow::Owned(bytes))))
3482 })?
3483 .expect("Callback above only errors or returns Some");
3484
3485 let bounds = Bounds {
3486 // See comment in paint_glyph: floor for X (subpixel variant),
3487 // round for Y (no subpixel variant).
3488 origin: point(glyph_origin.x.floor(), ScaledPixels(round_half_toward_zero(glyph_origin.y.0)))
3489 + raster_bounds.origin.map(Into::into),
3490 size: tile.bounds.size.map(Into::into),
3491 };
3492 let content_mask = self.outset_content_mask_to_device_pixels(self.content_mask());
3493 let opacity = self.element_opacity();
3494
3495 self.next_frame.scene.insert_primitive(PolychromeSprite {
3496 order: 0,
3497 pad: 0,
3498 grayscale: false,
3499 bounds,
3500 corner_radii: Default::default(),
3501 content_mask,
3502 tile,
3503 opacity,
3504 });
3505 }
3506 Ok(())
3507 }
3508
3509 /// Paint a monochrome SVG into the scene for the next frame at the current stacking context.
3510 ///
3511 /// This method should only be called as part of the paint phase of element drawing.
3512 pub fn paint_svg(
3513 &mut self,
3514 bounds: Bounds<Pixels>,
3515 path: SharedString,
3516 mut data: Option<&[u8]>,
3517 transformation: TransformationMatrix,
3518 color: Hsla,
3519 cx: &App,
3520 ) -> Result<()> {
3521 self.invalidator.debug_assert_paint();
3522
3523 let element_opacity = self.element_opacity();
3524
3525 let bounds = self.round_bounds_to_device_pixels(bounds);
3526 let params = RenderSvgParams {
3527 path,
3528 size: bounds.size.map(|pixels| {
3529 DevicePixels::from((pixels.0 * SMOOTH_SVG_SCALE_FACTOR).ceil() as i32)
3530 }),
3531 };
3532
3533 let Some(tile) =
3534 self.sprite_atlas
3535 .get_or_insert_with(¶ms.clone().into(), &mut || {
3536 let Some((size, bytes)) = cx.svg_renderer.render_alpha_mask(¶ms, data)?
3537 else {
3538 return Ok(None);
3539 };
3540 Ok(Some((size, Cow::Owned(bytes))))
3541 })?
3542 else {
3543 return Ok(());
3544 };
3545 let content_mask = self.outset_content_mask_to_device_pixels(self.content_mask());
3546 let svg_bounds = Bounds {
3547 origin: bounds.center()
3548 - Point::new(
3549 ScaledPixels(tile.bounds.size.width.0 as f32 / SMOOTH_SVG_SCALE_FACTOR / 2.),
3550 ScaledPixels(tile.bounds.size.height.0 as f32 / SMOOTH_SVG_SCALE_FACTOR / 2.),
3551 ),
3552 size: tile
3553 .bounds
3554 .size
3555 .map(|value| ScaledPixels(value.0 as f32 / SMOOTH_SVG_SCALE_FACTOR)),
3556 };
3557
3558 self.next_frame.scene.insert_primitive(MonochromeSprite {
3559 order: 0,
3560 pad: 0,
3561 bounds: svg_bounds
3562 .map_origin(|v| ScaledPixels(round_half_toward_zero(v.0)))
3563 .map_size(|size| size.ceil()),
3564 content_mask,
3565 color: color.opacity(element_opacity),
3566 tile,
3567 transformation,
3568 });
3569
3570 Ok(())
3571 }
3572
3573 /// Paint an image into the scene for the next frame at the current z-index.
3574 /// This method will panic if the frame_index is not valid
3575 ///
3576 /// This method should only be called as part of the paint phase of element drawing.
3577 pub fn paint_image(
3578 &mut self,
3579 bounds: Bounds<Pixels>,
3580 corner_radii: Corners<Pixels>,
3581 data: Arc<RenderImage>,
3582 frame_index: usize,
3583 grayscale: bool,
3584 ) -> Result<()> {
3585 self.invalidator.debug_assert_paint();
3586
3587 let bounds = self.outset_bounds_to_device_pixels(bounds);
3588 let params = RenderImageParams {
3589 image_id: data.id,
3590 frame_index,
3591 };
3592
3593 let tile = self
3594 .sprite_atlas
3595 .get_or_insert_with(¶ms.into(), &mut || {
3596 Ok(Some((
3597 data.size(frame_index),
3598 Cow::Borrowed(
3599 data.as_bytes(frame_index)
3600 .expect("It's the caller's job to pass a valid frame index"),
3601 ),
3602 )))
3603 })?
3604 .expect("Callback above only returns Some");
3605 let content_mask = self.outset_content_mask_to_device_pixels(self.content_mask());
3606 let corner_radii = corner_radii.scale(self.scale_factor());
3607 let opacity = self.element_opacity();
3608
3609 self.next_frame.scene.insert_primitive(PolychromeSprite {
3610 order: 0,
3611 pad: 0,
3612 grayscale,
3613 bounds,
3614 content_mask,
3615 corner_radii,
3616 tile,
3617 opacity,
3618 });
3619 Ok(())
3620 }
3621
3622 /// Paint a surface into the scene for the next frame at the current z-index.
3623 ///
3624 /// This method should only be called as part of the paint phase of element drawing.
3625 #[cfg(target_os = "macos")]
3626 pub fn paint_surface(&mut self, bounds: Bounds<Pixels>, image_buffer: CVPixelBuffer) {
3627 use crate::PaintSurface;
3628
3629 self.invalidator.debug_assert_paint();
3630
3631 let bounds = self.round_bounds_to_device_pixels(bounds);
3632 let content_mask = self.outset_content_mask_to_device_pixels(self.content_mask());
3633 self.next_frame.scene.insert_primitive(PaintSurface {
3634 order: 0,
3635 bounds,
3636 content_mask,
3637 image_buffer,
3638 });
3639 }
3640
3641 /// Removes an image from the sprite atlas.
3642 pub fn drop_image(&mut self, data: Arc<RenderImage>) -> Result<()> {
3643 for frame_index in 0..data.frame_count() {
3644 let params = RenderImageParams {
3645 image_id: data.id,
3646 frame_index,
3647 };
3648
3649 self.sprite_atlas.remove(¶ms.clone().into());
3650 }
3651
3652 Ok(())
3653 }
3654
3655 /// Add a node to the layout tree for the current frame. Takes the `Style` of the element for which
3656 /// layout is being requested, along with the layout ids of any children. This method is called during
3657 /// calls to the [`Element::request_layout`] trait method and enables any element to participate in layout.
3658 ///
3659 /// This method should only be called as part of the request_layout or prepaint phase of element drawing.
3660 #[must_use]
3661 pub fn request_layout(
3662 &mut self,
3663 style: Style,
3664 children: impl IntoIterator<Item = LayoutId>,
3665 cx: &mut App,
3666 ) -> LayoutId {
3667 self.invalidator.debug_assert_prepaint();
3668
3669 cx.layout_id_buffer.clear();
3670 cx.layout_id_buffer.extend(children);
3671 let rem_size = self.rem_size();
3672 let scale_factor = self.scale_factor();
3673
3674 self.layout_engine.as_mut().unwrap().request_layout(
3675 style,
3676 rem_size,
3677 scale_factor,
3678 &cx.layout_id_buffer,
3679 )
3680 }
3681
3682 /// Add a node to the layout tree for the current frame. Instead of taking a `Style` and children,
3683 /// this variant takes a function that is invoked during layout so you can use arbitrary logic to
3684 /// determine the element's size. One place this is used internally is when measuring text.
3685 ///
3686 /// The given closure is invoked at layout time with the known dimensions and available space and
3687 /// returns a `Size`.
3688 ///
3689 /// This method should only be called as part of the request_layout or prepaint phase of element drawing.
3690 pub fn request_measured_layout<F>(&mut self, style: Style, measure: F) -> LayoutId
3691 where
3692 F: Fn(Size<Option<Pixels>>, Size<AvailableSpace>, &mut Window, &mut App) -> Size<Pixels>
3693 + 'static,
3694 {
3695 self.invalidator.debug_assert_prepaint();
3696
3697 let rem_size = self.rem_size();
3698 let scale_factor = self.scale_factor();
3699 self.layout_engine
3700 .as_mut()
3701 .unwrap()
3702 .request_measured_layout(style, rem_size, scale_factor, measure)
3703 }
3704
3705 /// Compute the layout for the given id within the given available space.
3706 /// This method is called for its side effect, typically by the framework prior to painting.
3707 /// After calling it, you can request the bounds of the given layout node id or any descendant.
3708 ///
3709 /// This method should only be called as part of the prepaint phase of element drawing.
3710 pub fn compute_layout(
3711 &mut self,
3712 layout_id: LayoutId,
3713 available_space: Size<AvailableSpace>,
3714 cx: &mut App,
3715 ) {
3716 self.invalidator.debug_assert_prepaint();
3717
3718 let mut layout_engine = self.layout_engine.take().unwrap();
3719 layout_engine.compute_layout(layout_id, available_space, self, cx);
3720 self.layout_engine = Some(layout_engine);
3721 }
3722
3723 /// Obtain the bounds computed for the given LayoutId relative to the window. This method will usually be invoked by
3724 /// GPUI itself automatically in order to pass your element its `Bounds` automatically.
3725 ///
3726 /// This method should only be called as part of element drawing.
3727 pub fn layout_bounds(&mut self, layout_id: LayoutId) -> Bounds<Pixels> {
3728 self.invalidator.debug_assert_prepaint();
3729
3730 let scale_factor = self.scale_factor();
3731 let mut bounds = self
3732 .layout_engine
3733 .as_mut()
3734 .unwrap()
3735 .layout_bounds(layout_id, scale_factor)
3736 .map(Into::into);
3737 bounds.origin += self.element_offset();
3738 bounds
3739 }
3740
3741 /// This method should be called during `prepaint`. You can use
3742 /// the returned [Hitbox] during `paint` or in an event handler
3743 /// to determine whether the inserted hitbox was the topmost.
3744 ///
3745 /// This method should only be called as part of the prepaint phase of element drawing.
3746 pub fn insert_hitbox(&mut self, bounds: Bounds<Pixels>, behavior: HitboxBehavior) -> Hitbox {
3747 self.invalidator.debug_assert_prepaint();
3748
3749 let content_mask = self.content_mask();
3750 let mut id = self.next_hitbox_id;
3751 self.next_hitbox_id = self.next_hitbox_id.next();
3752 let hitbox = Hitbox {
3753 id,
3754 bounds,
3755 content_mask,
3756 behavior,
3757 };
3758 self.next_frame.hitboxes.push(hitbox.clone());
3759 hitbox
3760 }
3761
3762 /// Set a hitbox which will act as a control area of the platform window.
3763 ///
3764 /// This method should only be called as part of the paint phase of element drawing.
3765 pub fn insert_window_control_hitbox(&mut self, area: WindowControlArea, hitbox: Hitbox) {
3766 self.invalidator.debug_assert_paint();
3767 self.next_frame.window_control_hitboxes.push((area, hitbox));
3768 }
3769
3770 /// Sets the key context for the current element. This context will be used to translate
3771 /// keybindings into actions.
3772 ///
3773 /// This method should only be called as part of the paint phase of element drawing.
3774 pub fn set_key_context(&mut self, context: KeyContext) {
3775 self.invalidator.debug_assert_paint();
3776 self.next_frame.dispatch_tree.set_key_context(context);
3777 }
3778
3779 /// Sets the focus handle for the current element. This handle will be used to manage focus state
3780 /// and keyboard event dispatch for the element.
3781 ///
3782 /// This method should only be called as part of the prepaint phase of element drawing.
3783 pub fn set_focus_handle(&mut self, focus_handle: &FocusHandle, _: &App) {
3784 self.invalidator.debug_assert_prepaint();
3785 if focus_handle.is_focused(self) {
3786 self.next_frame.focus = Some(focus_handle.id);
3787 }
3788 self.next_frame.dispatch_tree.set_focus_id(focus_handle.id);
3789 }
3790
3791 /// Sets the view id for the current element, which will be used to manage view caching.
3792 ///
3793 /// This method should only be called as part of element prepaint. We plan on removing this
3794 /// method eventually when we solve some issues that require us to construct editor elements
3795 /// directly instead of always using editors via views.
3796 pub fn set_view_id(&mut self, view_id: EntityId) {
3797 self.invalidator.debug_assert_prepaint();
3798 self.next_frame.dispatch_tree.set_view_id(view_id);
3799 }
3800
3801 /// Get the entity ID for the currently rendering view
3802 pub fn current_view(&self) -> EntityId {
3803 self.invalidator.debug_assert_paint_or_prepaint();
3804 self.rendered_entity_stack.last().copied().unwrap()
3805 }
3806
3807 #[inline]
3808 pub(crate) fn with_rendered_view<R>(
3809 &mut self,
3810 id: EntityId,
3811 f: impl FnOnce(&mut Self) -> R,
3812 ) -> R {
3813 self.rendered_entity_stack.push(id);
3814 let result = f(self);
3815 self.rendered_entity_stack.pop();
3816 result
3817 }
3818
3819 /// Executes the provided function with the specified image cache.
3820 pub fn with_image_cache<F, R>(&mut self, image_cache: Option<AnyImageCache>, f: F) -> R
3821 where
3822 F: FnOnce(&mut Self) -> R,
3823 {
3824 if let Some(image_cache) = image_cache {
3825 self.image_cache_stack.push(image_cache);
3826 let result = f(self);
3827 self.image_cache_stack.pop();
3828 result
3829 } else {
3830 f(self)
3831 }
3832 }
3833
3834 /// Sets an input handler, such as [`ElementInputHandler`][element_input_handler], which interfaces with the
3835 /// platform to receive textual input with proper integration with concerns such
3836 /// as IME interactions. This handler will be active for the upcoming frame until the following frame is
3837 /// rendered.
3838 ///
3839 /// This method should only be called as part of the paint phase of element drawing.
3840 ///
3841 /// [element_input_handler]: crate::ElementInputHandler
3842 pub fn handle_input(
3843 &mut self,
3844 focus_handle: &FocusHandle,
3845 input_handler: impl InputHandler,
3846 cx: &App,
3847 ) {
3848 self.invalidator.debug_assert_paint();
3849
3850 if focus_handle.is_focused(self) {
3851 let cx = self.to_async(cx);
3852 self.next_frame
3853 .input_handlers
3854 .push(Some(PlatformInputHandler::new(cx, Box::new(input_handler))));
3855 }
3856 }
3857
3858 /// Register a mouse event listener on the window for the next frame. The type of event
3859 /// is determined by the first parameter of the given listener. When the next frame is rendered
3860 /// the listener will be cleared.
3861 ///
3862 /// This method should only be called as part of the paint phase of element drawing.
3863 pub fn on_mouse_event<Event: MouseEvent>(
3864 &mut self,
3865 mut listener: impl FnMut(&Event, DispatchPhase, &mut Window, &mut App) + 'static,
3866 ) {
3867 self.invalidator.debug_assert_paint();
3868
3869 self.next_frame.mouse_listeners.push(Some(Box::new(
3870 move |event: &dyn Any, phase: DispatchPhase, window: &mut Window, cx: &mut App| {
3871 if let Some(event) = event.downcast_ref() {
3872 listener(event, phase, window, cx)
3873 }
3874 },
3875 )));
3876 }
3877
3878 /// Register a key event listener on this node for the next frame. The type of event
3879 /// is determined by the first parameter of the given listener. When the next frame is rendered
3880 /// the listener will be cleared.
3881 ///
3882 /// This is a fairly low-level method, so prefer using event handlers on elements unless you have
3883 /// a specific need to register a listener yourself.
3884 ///
3885 /// This method should only be called as part of the paint phase of element drawing.
3886 pub fn on_key_event<Event: KeyEvent>(
3887 &mut self,
3888 listener: impl Fn(&Event, DispatchPhase, &mut Window, &mut App) + 'static,
3889 ) {
3890 self.invalidator.debug_assert_paint();
3891
3892 self.next_frame.dispatch_tree.on_key_event(Rc::new(
3893 move |event: &dyn Any, phase, window: &mut Window, cx: &mut App| {
3894 if let Some(event) = event.downcast_ref::<Event>() {
3895 listener(event, phase, window, cx)
3896 }
3897 },
3898 ));
3899 }
3900
3901 /// Register a modifiers changed event listener on the window for the next frame.
3902 ///
3903 /// This is a fairly low-level method, so prefer using event handlers on elements unless you have
3904 /// a specific need to register a global listener.
3905 ///
3906 /// This method should only be called as part of the paint phase of element drawing.
3907 pub fn on_modifiers_changed(
3908 &mut self,
3909 listener: impl Fn(&ModifiersChangedEvent, &mut Window, &mut App) + 'static,
3910 ) {
3911 self.invalidator.debug_assert_paint();
3912
3913 self.next_frame.dispatch_tree.on_modifiers_changed(Rc::new(
3914 move |event: &ModifiersChangedEvent, window: &mut Window, cx: &mut App| {
3915 listener(event, window, cx)
3916 },
3917 ));
3918 }
3919
3920 /// Register a listener to be called when the given focus handle or one of its descendants receives focus.
3921 /// This does not fire if the given focus handle - or one of its descendants - was previously focused.
3922 /// Returns a subscription and persists until the subscription is dropped.
3923 pub fn on_focus_in(
3924 &mut self,
3925 handle: &FocusHandle,
3926 cx: &mut App,
3927 mut listener: impl FnMut(&mut Window, &mut App) + 'static,
3928 ) -> Subscription {
3929 let focus_id = handle.id;
3930 let (subscription, activate) =
3931 self.new_focus_listener(Box::new(move |event, window, cx| {
3932 if event.is_focus_in(focus_id) {
3933 listener(window, cx);
3934 }
3935 true
3936 }));
3937 cx.defer(move |_| activate());
3938 subscription
3939 }
3940
3941 /// Register a listener to be called when the given focus handle or one of its descendants loses focus.
3942 /// Returns a subscription and persists until the subscription is dropped.
3943 pub fn on_focus_out(
3944 &mut self,
3945 handle: &FocusHandle,
3946 cx: &mut App,
3947 mut listener: impl FnMut(FocusOutEvent, &mut Window, &mut App) + 'static,
3948 ) -> Subscription {
3949 let focus_id = handle.id;
3950 let (subscription, activate) =
3951 self.new_focus_listener(Box::new(move |event, window, cx| {
3952 if let Some(blurred_id) = event.previous_focus_path.last().copied()
3953 && event.is_focus_out(focus_id)
3954 {
3955 let event = FocusOutEvent {
3956 blurred: WeakFocusHandle {
3957 id: blurred_id,
3958 handles: Arc::downgrade(&cx.focus_handles),
3959 },
3960 };
3961 listener(event, window, cx)
3962 }
3963 true
3964 }));
3965 cx.defer(move |_| activate());
3966 subscription
3967 }
3968
3969 fn reset_cursor_style(&self, cx: &mut App) {
3970 // Set the cursor only if we're the active window.
3971 if self.is_window_hovered() {
3972 let style = self
3973 .rendered_frame
3974 .cursor_style(self)
3975 .unwrap_or(CursorStyle::Arrow);
3976 cx.platform.set_cursor_style(style);
3977 }
3978 }
3979
3980 /// Dispatch a given keystroke as though the user had typed it.
3981 /// You can create a keystroke with Keystroke::parse("").
3982 pub fn dispatch_keystroke(&mut self, keystroke: Keystroke, cx: &mut App) -> bool {
3983 let keystroke = keystroke.with_simulated_ime();
3984 let result = self.dispatch_event(
3985 PlatformInput::KeyDown(KeyDownEvent {
3986 keystroke: keystroke.clone(),
3987 is_held: false,
3988 prefer_character_input: false,
3989 }),
3990 cx,
3991 );
3992 if !result.propagate {
3993 return true;
3994 }
3995
3996 if let Some(input) = keystroke.key_char
3997 && let Some(mut input_handler) = self.platform_window.take_input_handler()
3998 {
3999 input_handler.dispatch_input(&input, self, cx);
4000 self.platform_window.set_input_handler(input_handler);
4001 return true;
4002 }
4003
4004 false
4005 }
4006
4007 /// Return a key binding string for an action, to display in the UI. Uses the highest precedence
4008 /// binding for the action (last binding added to the keymap).
4009 pub fn keystroke_text_for(&self, action: &dyn Action) -> String {
4010 self.highest_precedence_binding_for_action(action)
4011 .map(|binding| {
4012 binding
4013 .keystrokes()
4014 .iter()
4015 .map(ToString::to_string)
4016 .collect::<Vec<_>>()
4017 .join(" ")
4018 })
4019 .unwrap_or_else(|| action.name().to_string())
4020 }
4021
4022 /// Dispatch a mouse or keyboard event on the window.
4023 #[profiling::function]
4024 pub fn dispatch_event(&mut self, event: PlatformInput, cx: &mut App) -> DispatchEventResult {
4025 // Track whether this input was keyboard-based for focus-visible styling
4026 self.last_input_modality = match &event {
4027 PlatformInput::KeyDown(_) | PlatformInput::ModifiersChanged(_) => {
4028 InputModality::Keyboard
4029 }
4030 PlatformInput::MouseDown(e) if e.is_focusing() => InputModality::Mouse,
4031 _ => self.last_input_modality,
4032 };
4033
4034 // Handlers may set this to false by calling `stop_propagation`.
4035 cx.propagate_event = true;
4036 // Handlers may set this to true by calling `prevent_default`.
4037 self.default_prevented = false;
4038
4039 let event = match event {
4040 // Track the mouse position with our own state, since accessing the platform
4041 // API for the mouse position can only occur on the main thread.
4042 PlatformInput::MouseMove(mouse_move) => {
4043 self.mouse_position = mouse_move.position;
4044 self.modifiers = mouse_move.modifiers;
4045 PlatformInput::MouseMove(mouse_move)
4046 }
4047 PlatformInput::MouseDown(mouse_down) => {
4048 self.mouse_position = mouse_down.position;
4049 self.modifiers = mouse_down.modifiers;
4050 PlatformInput::MouseDown(mouse_down)
4051 }
4052 PlatformInput::MouseUp(mouse_up) => {
4053 self.mouse_position = mouse_up.position;
4054 self.modifiers = mouse_up.modifiers;
4055 PlatformInput::MouseUp(mouse_up)
4056 }
4057 PlatformInput::MousePressure(mouse_pressure) => {
4058 PlatformInput::MousePressure(mouse_pressure)
4059 }
4060 PlatformInput::MouseExited(mouse_exited) => {
4061 self.modifiers = mouse_exited.modifiers;
4062 PlatformInput::MouseExited(mouse_exited)
4063 }
4064 PlatformInput::ModifiersChanged(modifiers_changed) => {
4065 self.modifiers = modifiers_changed.modifiers;
4066 self.capslock = modifiers_changed.capslock;
4067 PlatformInput::ModifiersChanged(modifiers_changed)
4068 }
4069 PlatformInput::ScrollWheel(scroll_wheel) => {
4070 self.mouse_position = scroll_wheel.position;
4071 self.modifiers = scroll_wheel.modifiers;
4072 PlatformInput::ScrollWheel(scroll_wheel)
4073 }
4074 // Translate dragging and dropping of external files from the operating system
4075 // to internal drag and drop events.
4076 PlatformInput::FileDrop(file_drop) => match file_drop {
4077 FileDropEvent::Entered { position, paths } => {
4078 self.mouse_position = position;
4079 if cx.active_drag.is_none() {
4080 cx.active_drag = Some(AnyDrag {
4081 value: Arc::new(paths.clone()),
4082 view: cx.new(|_| paths).into(),
4083 cursor_offset: position,
4084 cursor_style: None,
4085 });
4086 }
4087 PlatformInput::MouseMove(MouseMoveEvent {
4088 position,
4089 pressed_button: Some(MouseButton::Left),
4090 modifiers: Modifiers::default(),
4091 })
4092 }
4093 FileDropEvent::Pending { position } => {
4094 self.mouse_position = position;
4095 PlatformInput::MouseMove(MouseMoveEvent {
4096 position,
4097 pressed_button: Some(MouseButton::Left),
4098 modifiers: Modifiers::default(),
4099 })
4100 }
4101 FileDropEvent::Submit { position } => {
4102 cx.activate(true);
4103 self.mouse_position = position;
4104 PlatformInput::MouseUp(MouseUpEvent {
4105 button: MouseButton::Left,
4106 position,
4107 modifiers: Modifiers::default(),
4108 click_count: 1,
4109 })
4110 }
4111 FileDropEvent::Exited => {
4112 cx.active_drag.take();
4113 PlatformInput::FileDrop(FileDropEvent::Exited)
4114 }
4115 },
4116 PlatformInput::KeyDown(_) | PlatformInput::KeyUp(_) => event,
4117 };
4118
4119 if let Some(any_mouse_event) = event.mouse_event() {
4120 self.dispatch_mouse_event(any_mouse_event, cx);
4121 } else if let Some(any_key_event) = event.keyboard_event() {
4122 self.dispatch_key_event(any_key_event, cx);
4123 }
4124
4125 if self.invalidator.is_dirty() {
4126 self.input_rate_tracker.borrow_mut().record_input();
4127 }
4128
4129 DispatchEventResult {
4130 propagate: cx.propagate_event,
4131 default_prevented: self.default_prevented,
4132 }
4133 }
4134
4135 fn dispatch_mouse_event(&mut self, event: &dyn Any, cx: &mut App) {
4136 let hit_test = self.rendered_frame.hit_test(self.mouse_position());
4137 if hit_test != self.mouse_hit_test {
4138 self.mouse_hit_test = hit_test;
4139 self.reset_cursor_style(cx);
4140 }
4141
4142 #[cfg(any(feature = "inspector", debug_assertions))]
4143 if self.is_inspector_picking(cx) {
4144 self.handle_inspector_mouse_event(event, cx);
4145 // When inspector is picking, all other mouse handling is skipped.
4146 return;
4147 }
4148
4149 let mut mouse_listeners = mem::take(&mut self.rendered_frame.mouse_listeners);
4150
4151 // Capture phase, events bubble from back to front. Handlers for this phase are used for
4152 // special purposes, such as detecting events outside of a given Bounds.
4153 for listener in &mut mouse_listeners {
4154 let listener = listener.as_mut().unwrap();
4155 listener(event, DispatchPhase::Capture, self, cx);
4156 if !cx.propagate_event {
4157 break;
4158 }
4159 }
4160
4161 // Bubble phase, where most normal handlers do their work.
4162 if cx.propagate_event {
4163 for listener in mouse_listeners.iter_mut().rev() {
4164 let listener = listener.as_mut().unwrap();
4165 listener(event, DispatchPhase::Bubble, self, cx);
4166 if !cx.propagate_event {
4167 break;
4168 }
4169 }
4170 }
4171
4172 self.rendered_frame.mouse_listeners = mouse_listeners;
4173
4174 if cx.has_active_drag() {
4175 if event.is::<MouseMoveEvent>() {
4176 // If this was a mouse move event, redraw the window so that the
4177 // active drag can follow the mouse cursor.
4178 self.refresh();
4179 } else if event.is::<MouseUpEvent>() {
4180 // If this was a mouse up event, cancel the active drag and redraw
4181 // the window.
4182 cx.active_drag = None;
4183 self.refresh();
4184 }
4185 }
4186 }
4187
4188 fn dispatch_key_event(&mut self, event: &dyn Any, cx: &mut App) {
4189 if self.invalidator.is_dirty() {
4190 self.draw(cx).clear();
4191 }
4192
4193 let node_id = self.focus_node_id_in_rendered_frame(self.focus);
4194 let dispatch_path = self.rendered_frame.dispatch_tree.dispatch_path(node_id);
4195
4196 let mut keystroke: Option<Keystroke> = None;
4197
4198 if let Some(event) = event.downcast_ref::<ModifiersChangedEvent>() {
4199 if event.modifiers.number_of_modifiers() == 0
4200 && self.pending_modifier.modifiers.number_of_modifiers() == 1
4201 && !self.pending_modifier.saw_keystroke
4202 {
4203 let key = match self.pending_modifier.modifiers {
4204 modifiers if modifiers.shift => Some("shift"),
4205 modifiers if modifiers.control => Some("control"),
4206 modifiers if modifiers.alt => Some("alt"),
4207 modifiers if modifiers.platform => Some("platform"),
4208 modifiers if modifiers.function => Some("function"),
4209 _ => None,
4210 };
4211 if let Some(key) = key {
4212 keystroke = Some(Keystroke {
4213 key: key.to_string(),
4214 key_char: None,
4215 modifiers: Modifiers::default(),
4216 });
4217 }
4218 }
4219
4220 if self.pending_modifier.modifiers.number_of_modifiers() == 0
4221 && event.modifiers.number_of_modifiers() == 1
4222 {
4223 self.pending_modifier.saw_keystroke = false
4224 }
4225 self.pending_modifier.modifiers = event.modifiers
4226 } else if let Some(key_down_event) = event.downcast_ref::<KeyDownEvent>() {
4227 self.pending_modifier.saw_keystroke = true;
4228 keystroke = Some(key_down_event.keystroke.clone());
4229 }
4230
4231 let Some(keystroke) = keystroke else {
4232 self.finish_dispatch_key_event(event, dispatch_path, self.context_stack(), cx);
4233 return;
4234 };
4235
4236 cx.propagate_event = true;
4237 self.dispatch_keystroke_interceptors(event, self.context_stack(), cx);
4238 if !cx.propagate_event {
4239 self.finish_dispatch_key_event(event, dispatch_path, self.context_stack(), cx);
4240 return;
4241 }
4242
4243 let mut currently_pending = self.pending_input.take().unwrap_or_default();
4244 if currently_pending.focus.is_some() && currently_pending.focus != self.focus {
4245 currently_pending = PendingInput::default();
4246 }
4247
4248 let match_result = self.rendered_frame.dispatch_tree.dispatch_key(
4249 currently_pending.keystrokes,
4250 keystroke,
4251 &dispatch_path,
4252 );
4253
4254 if !match_result.to_replay.is_empty() {
4255 self.replay_pending_input(match_result.to_replay, cx);
4256 cx.propagate_event = true;
4257 }
4258
4259 if !match_result.pending.is_empty() {
4260 currently_pending.timer.take();
4261 currently_pending.keystrokes = match_result.pending;
4262 currently_pending.focus = self.focus;
4263
4264 let text_input_requires_timeout = event
4265 .downcast_ref::<KeyDownEvent>()
4266 .filter(|key_down| key_down.keystroke.key_char.is_some())
4267 .and_then(|_| self.platform_window.take_input_handler())
4268 .map_or(false, |mut input_handler| {
4269 let accepts = input_handler.accepts_text_input(self, cx);
4270 self.platform_window.set_input_handler(input_handler);
4271 accepts
4272 });
4273
4274 currently_pending.needs_timeout |=
4275 match_result.pending_has_binding || text_input_requires_timeout;
4276
4277 if currently_pending.needs_timeout {
4278 currently_pending.timer = Some(self.spawn(cx, async move |cx| {
4279 cx.background_executor.timer(Duration::from_secs(1)).await;
4280 cx.update(move |window, cx| {
4281 let Some(currently_pending) = window
4282 .pending_input
4283 .take()
4284 .filter(|pending| pending.focus == window.focus)
4285 else {
4286 return;
4287 };
4288
4289 let node_id = window.focus_node_id_in_rendered_frame(window.focus);
4290 let dispatch_path =
4291 window.rendered_frame.dispatch_tree.dispatch_path(node_id);
4292
4293 let to_replay = window
4294 .rendered_frame
4295 .dispatch_tree
4296 .flush_dispatch(currently_pending.keystrokes, &dispatch_path);
4297
4298 window.pending_input_changed(cx);
4299 window.replay_pending_input(to_replay, cx)
4300 })
4301 .log_err();
4302 }));
4303 } else {
4304 currently_pending.timer = None;
4305 }
4306 self.pending_input = Some(currently_pending);
4307 self.pending_input_changed(cx);
4308 cx.propagate_event = false;
4309 return;
4310 }
4311
4312 let skip_bindings = event
4313 .downcast_ref::<KeyDownEvent>()
4314 .filter(|key_down_event| key_down_event.prefer_character_input)
4315 .map(|_| {
4316 self.platform_window
4317 .take_input_handler()
4318 .map_or(false, |mut input_handler| {
4319 let accepts = input_handler.accepts_text_input(self, cx);
4320 self.platform_window.set_input_handler(input_handler);
4321 // If modifiers are not excessive (e.g. AltGr), and the input handler is accepting text input,
4322 // we prefer the text input over bindings.
4323 accepts
4324 })
4325 })
4326 .unwrap_or(false);
4327
4328 if !skip_bindings {
4329 for binding in match_result.bindings {
4330 self.dispatch_action_on_node(node_id, binding.action.as_ref(), cx);
4331 if !cx.propagate_event {
4332 self.dispatch_keystroke_observers(
4333 event,
4334 Some(binding.action),
4335 match_result.context_stack,
4336 cx,
4337 );
4338 self.pending_input_changed(cx);
4339 return;
4340 }
4341 }
4342 }
4343
4344 self.finish_dispatch_key_event(event, dispatch_path, match_result.context_stack, cx);
4345 self.pending_input_changed(cx);
4346 }
4347
4348 fn finish_dispatch_key_event(
4349 &mut self,
4350 event: &dyn Any,
4351 dispatch_path: SmallVec<[DispatchNodeId; 32]>,
4352 context_stack: Vec<KeyContext>,
4353 cx: &mut App,
4354 ) {
4355 self.dispatch_key_down_up_event(event, &dispatch_path, cx);
4356 if !cx.propagate_event {
4357 return;
4358 }
4359
4360 self.dispatch_modifiers_changed_event(event, &dispatch_path, cx);
4361 if !cx.propagate_event {
4362 return;
4363 }
4364
4365 self.dispatch_keystroke_observers(event, None, context_stack, cx);
4366 }
4367
4368 pub(crate) fn pending_input_changed(&mut self, cx: &mut App) {
4369 self.pending_input_observers
4370 .clone()
4371 .retain(&(), |callback| callback(self, cx));
4372 }
4373
4374 fn dispatch_key_down_up_event(
4375 &mut self,
4376 event: &dyn Any,
4377 dispatch_path: &SmallVec<[DispatchNodeId; 32]>,
4378 cx: &mut App,
4379 ) {
4380 // Capture phase
4381 for node_id in dispatch_path {
4382 let node = self.rendered_frame.dispatch_tree.node(*node_id);
4383
4384 for key_listener in node.key_listeners.clone() {
4385 key_listener(event, DispatchPhase::Capture, self, cx);
4386 if !cx.propagate_event {
4387 return;
4388 }
4389 }
4390 }
4391
4392 // Bubble phase
4393 for node_id in dispatch_path.iter().rev() {
4394 // Handle low level key events
4395 let node = self.rendered_frame.dispatch_tree.node(*node_id);
4396 for key_listener in node.key_listeners.clone() {
4397 key_listener(event, DispatchPhase::Bubble, self, cx);
4398 if !cx.propagate_event {
4399 return;
4400 }
4401 }
4402 }
4403 }
4404
4405 fn dispatch_modifiers_changed_event(
4406 &mut self,
4407 event: &dyn Any,
4408 dispatch_path: &SmallVec<[DispatchNodeId; 32]>,
4409 cx: &mut App,
4410 ) {
4411 let Some(event) = event.downcast_ref::<ModifiersChangedEvent>() else {
4412 return;
4413 };
4414 for node_id in dispatch_path.iter().rev() {
4415 let node = self.rendered_frame.dispatch_tree.node(*node_id);
4416 for listener in node.modifiers_changed_listeners.clone() {
4417 listener(event, self, cx);
4418 if !cx.propagate_event {
4419 return;
4420 }
4421 }
4422 }
4423 }
4424
4425 /// Determine whether a potential multi-stroke key binding is in progress on this window.
4426 pub fn has_pending_keystrokes(&self) -> bool {
4427 self.pending_input.is_some()
4428 }
4429
4430 pub(crate) fn clear_pending_keystrokes(&mut self) {
4431 self.pending_input.take();
4432 }
4433
4434 /// Returns the currently pending input keystrokes that might result in a multi-stroke key binding.
4435 pub fn pending_input_keystrokes(&self) -> Option<&[Keystroke]> {
4436 self.pending_input
4437 .as_ref()
4438 .map(|pending_input| pending_input.keystrokes.as_slice())
4439 }
4440
4441 fn replay_pending_input(&mut self, replays: SmallVec<[Replay; 1]>, cx: &mut App) {
4442 let node_id = self.focus_node_id_in_rendered_frame(self.focus);
4443 let dispatch_path = self.rendered_frame.dispatch_tree.dispatch_path(node_id);
4444
4445 'replay: for replay in replays {
4446 let event = KeyDownEvent {
4447 keystroke: replay.keystroke.clone(),
4448 is_held: false,
4449 prefer_character_input: true,
4450 };
4451
4452 cx.propagate_event = true;
4453 for binding in replay.bindings {
4454 self.dispatch_action_on_node(node_id, binding.action.as_ref(), cx);
4455 if !cx.propagate_event {
4456 self.dispatch_keystroke_observers(
4457 &event,
4458 Some(binding.action),
4459 Vec::default(),
4460 cx,
4461 );
4462 continue 'replay;
4463 }
4464 }
4465
4466 self.dispatch_key_down_up_event(&event, &dispatch_path, cx);
4467 if !cx.propagate_event {
4468 continue 'replay;
4469 }
4470 if let Some(input) = replay.keystroke.key_char.as_ref().cloned()
4471 && let Some(mut input_handler) = self.platform_window.take_input_handler()
4472 {
4473 input_handler.dispatch_input(&input, self, cx);
4474 self.platform_window.set_input_handler(input_handler)
4475 }
4476 }
4477 }
4478
4479 fn focus_node_id_in_rendered_frame(&self, focus_id: Option<FocusId>) -> DispatchNodeId {
4480 focus_id
4481 .and_then(|focus_id| {
4482 self.rendered_frame
4483 .dispatch_tree
4484 .focusable_node_id(focus_id)
4485 })
4486 .unwrap_or_else(|| self.rendered_frame.dispatch_tree.root_node_id())
4487 }
4488
4489 fn dispatch_action_on_node(
4490 &mut self,
4491 node_id: DispatchNodeId,
4492 action: &dyn Action,
4493 cx: &mut App,
4494 ) {
4495 let dispatch_path = self.rendered_frame.dispatch_tree.dispatch_path(node_id);
4496
4497 // Capture phase for global actions.
4498 cx.propagate_event = true;
4499 if let Some(mut global_listeners) = cx
4500 .global_action_listeners
4501 .remove(&action.as_any().type_id())
4502 {
4503 for listener in &global_listeners {
4504 listener(action.as_any(), DispatchPhase::Capture, cx);
4505 if !cx.propagate_event {
4506 break;
4507 }
4508 }
4509
4510 global_listeners.extend(
4511 cx.global_action_listeners
4512 .remove(&action.as_any().type_id())
4513 .unwrap_or_default(),
4514 );
4515
4516 cx.global_action_listeners
4517 .insert(action.as_any().type_id(), global_listeners);
4518 }
4519
4520 if !cx.propagate_event {
4521 return;
4522 }
4523
4524 // Capture phase for window actions.
4525 for node_id in &dispatch_path {
4526 let node = self.rendered_frame.dispatch_tree.node(*node_id);
4527 for DispatchActionListener {
4528 action_type,
4529 listener,
4530 } in node.action_listeners.clone()
4531 {
4532 let any_action = action.as_any();
4533 if action_type == any_action.type_id() {
4534 listener(any_action, DispatchPhase::Capture, self, cx);
4535
4536 if !cx.propagate_event {
4537 return;
4538 }
4539 }
4540 }
4541 }
4542
4543 // Bubble phase for window actions.
4544 for node_id in dispatch_path.iter().rev() {
4545 let node = self.rendered_frame.dispatch_tree.node(*node_id);
4546 for DispatchActionListener {
4547 action_type,
4548 listener,
4549 } in node.action_listeners.clone()
4550 {
4551 let any_action = action.as_any();
4552 if action_type == any_action.type_id() {
4553 cx.propagate_event = false; // Actions stop propagation by default during the bubble phase
4554 listener(any_action, DispatchPhase::Bubble, self, cx);
4555
4556 if !cx.propagate_event {
4557 return;
4558 }
4559 }
4560 }
4561 }
4562
4563 // Bubble phase for global actions.
4564 if let Some(mut global_listeners) = cx
4565 .global_action_listeners
4566 .remove(&action.as_any().type_id())
4567 {
4568 for listener in global_listeners.iter().rev() {
4569 cx.propagate_event = false; // Actions stop propagation by default during the bubble phase
4570
4571 listener(action.as_any(), DispatchPhase::Bubble, cx);
4572 if !cx.propagate_event {
4573 break;
4574 }
4575 }
4576
4577 global_listeners.extend(
4578 cx.global_action_listeners
4579 .remove(&action.as_any().type_id())
4580 .unwrap_or_default(),
4581 );
4582
4583 cx.global_action_listeners
4584 .insert(action.as_any().type_id(), global_listeners);
4585 }
4586 }
4587
4588 /// Register the given handler to be invoked whenever the global of the given type
4589 /// is updated.
4590 pub fn observe_global<G: Global>(
4591 &mut self,
4592 cx: &mut App,
4593 f: impl Fn(&mut Window, &mut App) + 'static,
4594 ) -> Subscription {
4595 let window_handle = self.handle;
4596 let (subscription, activate) = cx.global_observers.insert(
4597 TypeId::of::<G>(),
4598 Box::new(move |cx| {
4599 window_handle
4600 .update(cx, |_, window, cx| f(window, cx))
4601 .is_ok()
4602 }),
4603 );
4604 cx.defer(move |_| activate());
4605 subscription
4606 }
4607
4608 /// Focus the current window and bring it to the foreground at the platform level.
4609 pub fn activate_window(&self) {
4610 self.platform_window.activate();
4611 }
4612
4613 /// Minimize the current window at the platform level.
4614 pub fn minimize_window(&self) {
4615 self.platform_window.minimize();
4616 }
4617
4618 /// Toggle full screen status on the current window at the platform level.
4619 pub fn toggle_fullscreen(&self) {
4620 self.platform_window.toggle_fullscreen();
4621 }
4622
4623 /// Updates the IME panel position suggestions for languages like japanese, chinese.
4624 pub fn invalidate_character_coordinates(&self) {
4625 self.on_next_frame(|window, cx| {
4626 if let Some(mut input_handler) = window.platform_window.take_input_handler() {
4627 if let Some(bounds) = input_handler.selected_bounds(window, cx) {
4628 window.platform_window.update_ime_position(bounds);
4629 }
4630 window.platform_window.set_input_handler(input_handler);
4631 }
4632 });
4633 }
4634
4635 /// Present a platform dialog.
4636 /// The provided message will be presented, along with buttons for each answer.
4637 /// When a button is clicked, the returned Receiver will receive the index of the clicked button.
4638 pub fn prompt<T>(
4639 &mut self,
4640 level: PromptLevel,
4641 message: &str,
4642 detail: Option<&str>,
4643 answers: &[T],
4644 cx: &mut App,
4645 ) -> oneshot::Receiver<usize>
4646 where
4647 T: Clone + Into<PromptButton>,
4648 {
4649 let prompt_builder = cx.prompt_builder.take();
4650 let Some(prompt_builder) = prompt_builder else {
4651 unreachable!("Re-entrant window prompting is not supported by GPUI");
4652 };
4653
4654 let answers = answers
4655 .iter()
4656 .map(|answer| answer.clone().into())
4657 .collect::<Vec<_>>();
4658
4659 let receiver = match &prompt_builder {
4660 PromptBuilder::Default => self
4661 .platform_window
4662 .prompt(level, message, detail, &answers)
4663 .unwrap_or_else(|| {
4664 self.build_custom_prompt(&prompt_builder, level, message, detail, &answers, cx)
4665 }),
4666 PromptBuilder::Custom(_) => {
4667 self.build_custom_prompt(&prompt_builder, level, message, detail, &answers, cx)
4668 }
4669 };
4670
4671 cx.prompt_builder = Some(prompt_builder);
4672
4673 receiver
4674 }
4675
4676 fn build_custom_prompt(
4677 &mut self,
4678 prompt_builder: &PromptBuilder,
4679 level: PromptLevel,
4680 message: &str,
4681 detail: Option<&str>,
4682 answers: &[PromptButton],
4683 cx: &mut App,
4684 ) -> oneshot::Receiver<usize> {
4685 let (sender, receiver) = oneshot::channel();
4686 let handle = PromptHandle::new(sender);
4687 let handle = (prompt_builder)(level, message, detail, answers, handle, self, cx);
4688 self.prompt = Some(handle);
4689 receiver
4690 }
4691
4692 /// Returns the current context stack.
4693 pub fn context_stack(&self) -> Vec<KeyContext> {
4694 let node_id = self.focus_node_id_in_rendered_frame(self.focus);
4695 let dispatch_tree = &self.rendered_frame.dispatch_tree;
4696 dispatch_tree
4697 .dispatch_path(node_id)
4698 .iter()
4699 .filter_map(move |&node_id| dispatch_tree.node(node_id).context.clone())
4700 .collect()
4701 }
4702
4703 /// Returns all available actions for the focused element.
4704 pub fn available_actions(&self, cx: &App) -> Vec<Box<dyn Action>> {
4705 let node_id = self.focus_node_id_in_rendered_frame(self.focus);
4706 let mut actions = self.rendered_frame.dispatch_tree.available_actions(node_id);
4707 for action_type in cx.global_action_listeners.keys() {
4708 if let Err(ix) = actions.binary_search_by_key(action_type, |a| a.as_any().type_id()) {
4709 let action = cx.actions.build_action_type(action_type).ok();
4710 if let Some(action) = action {
4711 actions.insert(ix, action);
4712 }
4713 }
4714 }
4715 actions
4716 }
4717
4718 /// Returns key bindings that invoke an action on the currently focused element. Bindings are
4719 /// returned in the order they were added. For display, the last binding should take precedence.
4720 pub fn bindings_for_action(&self, action: &dyn Action) -> Vec<KeyBinding> {
4721 self.rendered_frame
4722 .dispatch_tree
4723 .bindings_for_action(action, &self.rendered_frame.dispatch_tree.context_stack)
4724 }
4725
4726 /// Returns the highest precedence key binding that invokes an action on the currently focused
4727 /// element. This is more efficient than getting the last result of `bindings_for_action`.
4728 pub fn highest_precedence_binding_for_action(&self, action: &dyn Action) -> Option<KeyBinding> {
4729 self.rendered_frame
4730 .dispatch_tree
4731 .highest_precedence_binding_for_action(
4732 action,
4733 &self.rendered_frame.dispatch_tree.context_stack,
4734 )
4735 }
4736
4737 /// Returns the key bindings for an action in a context.
4738 pub fn bindings_for_action_in_context(
4739 &self,
4740 action: &dyn Action,
4741 context: KeyContext,
4742 ) -> Vec<KeyBinding> {
4743 let dispatch_tree = &self.rendered_frame.dispatch_tree;
4744 dispatch_tree.bindings_for_action(action, &[context])
4745 }
4746
4747 /// Returns the highest precedence key binding for an action in a context. This is more
4748 /// efficient than getting the last result of `bindings_for_action_in_context`.
4749 pub fn highest_precedence_binding_for_action_in_context(
4750 &self,
4751 action: &dyn Action,
4752 context: KeyContext,
4753 ) -> Option<KeyBinding> {
4754 let dispatch_tree = &self.rendered_frame.dispatch_tree;
4755 dispatch_tree.highest_precedence_binding_for_action(action, &[context])
4756 }
4757
4758 /// Returns any bindings that would invoke an action on the given focus handle if it were
4759 /// focused. Bindings are returned in the order they were added. For display, the last binding
4760 /// should take precedence.
4761 pub fn bindings_for_action_in(
4762 &self,
4763 action: &dyn Action,
4764 focus_handle: &FocusHandle,
4765 ) -> Vec<KeyBinding> {
4766 let dispatch_tree = &self.rendered_frame.dispatch_tree;
4767 let Some(context_stack) = self.context_stack_for_focus_handle(focus_handle) else {
4768 return vec![];
4769 };
4770 dispatch_tree.bindings_for_action(action, &context_stack)
4771 }
4772
4773 /// Returns the highest precedence key binding that would invoke an action on the given focus
4774 /// handle if it were focused. This is more efficient than getting the last result of
4775 /// `bindings_for_action_in`.
4776 pub fn highest_precedence_binding_for_action_in(
4777 &self,
4778 action: &dyn Action,
4779 focus_handle: &FocusHandle,
4780 ) -> Option<KeyBinding> {
4781 let dispatch_tree = &self.rendered_frame.dispatch_tree;
4782 let context_stack = self.context_stack_for_focus_handle(focus_handle)?;
4783 dispatch_tree.highest_precedence_binding_for_action(action, &context_stack)
4784 }
4785
4786 /// Find the bindings that can follow the current input sequence for the current context stack.
4787 pub fn possible_bindings_for_input(&self, input: &[Keystroke]) -> Vec<KeyBinding> {
4788 self.rendered_frame
4789 .dispatch_tree
4790 .possible_next_bindings_for_input(input, &self.context_stack())
4791 }
4792
4793 fn context_stack_for_focus_handle(
4794 &self,
4795 focus_handle: &FocusHandle,
4796 ) -> Option<Vec<KeyContext>> {
4797 let dispatch_tree = &self.rendered_frame.dispatch_tree;
4798 let node_id = dispatch_tree.focusable_node_id(focus_handle.id)?;
4799 let context_stack: Vec<_> = dispatch_tree
4800 .dispatch_path(node_id)
4801 .into_iter()
4802 .filter_map(|node_id| dispatch_tree.node(node_id).context.clone())
4803 .collect();
4804 Some(context_stack)
4805 }
4806
4807 /// Returns a generic event listener that invokes the given listener with the view and context associated with the given view handle.
4808 pub fn listener_for<T: 'static, E>(
4809 &self,
4810 view: &Entity<T>,
4811 f: impl Fn(&mut T, &E, &mut Window, &mut Context<T>) + 'static,
4812 ) -> impl Fn(&E, &mut Window, &mut App) + 'static {
4813 let view = view.downgrade();
4814 move |e: &E, window: &mut Window, cx: &mut App| {
4815 view.update(cx, |view, cx| f(view, e, window, cx)).ok();
4816 }
4817 }
4818
4819 /// Returns a generic handler that invokes the given handler with the view and context associated with the given view handle.
4820 pub fn handler_for<E: 'static, Callback: Fn(&mut E, &mut Window, &mut Context<E>) + 'static>(
4821 &self,
4822 entity: &Entity<E>,
4823 f: Callback,
4824 ) -> impl Fn(&mut Window, &mut App) + 'static {
4825 let entity = entity.downgrade();
4826 move |window: &mut Window, cx: &mut App| {
4827 entity.update(cx, |entity, cx| f(entity, window, cx)).ok();
4828 }
4829 }
4830
4831 /// Register a callback that can interrupt the closing of the current window based the returned boolean.
4832 /// If the callback returns false, the window won't be closed.
4833 pub fn on_window_should_close(
4834 &self,
4835 cx: &App,
4836 f: impl Fn(&mut Window, &mut App) -> bool + 'static,
4837 ) {
4838 let mut cx = self.to_async(cx);
4839 self.platform_window.on_should_close(Box::new(move || {
4840 cx.update(|window, cx| f(window, cx)).unwrap_or(true)
4841 }))
4842 }
4843
4844 /// Register an action listener on this node for the next frame. The type of action
4845 /// is determined by the first parameter of the given listener. When the next frame is rendered
4846 /// the listener will be cleared.
4847 ///
4848 /// This is a fairly low-level method, so prefer using action handlers on elements unless you have
4849 /// a specific need to register a listener yourself.
4850 ///
4851 /// This method should only be called as part of the paint phase of element drawing.
4852 pub fn on_action(
4853 &mut self,
4854 action_type: TypeId,
4855 listener: impl Fn(&dyn Any, DispatchPhase, &mut Window, &mut App) + 'static,
4856 ) {
4857 self.invalidator.debug_assert_paint();
4858
4859 self.next_frame
4860 .dispatch_tree
4861 .on_action(action_type, Rc::new(listener));
4862 }
4863
4864 /// Register a capturing action listener on this node for the next frame if the condition is true.
4865 /// The type of action is determined by the first parameter of the given listener. When the next
4866 /// frame is rendered the listener will be cleared.
4867 ///
4868 /// This is a fairly low-level method, so prefer using action handlers on elements unless you have
4869 /// a specific need to register a listener yourself.
4870 ///
4871 /// This method should only be called as part of the paint phase of element drawing.
4872 pub fn on_action_when(
4873 &mut self,
4874 condition: bool,
4875 action_type: TypeId,
4876 listener: impl Fn(&dyn Any, DispatchPhase, &mut Window, &mut App) + 'static,
4877 ) {
4878 self.invalidator.debug_assert_paint();
4879
4880 if condition {
4881 self.next_frame
4882 .dispatch_tree
4883 .on_action(action_type, Rc::new(listener));
4884 }
4885 }
4886
4887 /// Read information about the GPU backing this window.
4888 /// Currently returns None on Mac and Windows.
4889 pub fn gpu_specs(&self) -> Option<GpuSpecs> {
4890 self.platform_window.gpu_specs()
4891 }
4892
4893 /// Perform titlebar double-click action.
4894 /// This is macOS specific.
4895 pub fn titlebar_double_click(&self) {
4896 self.platform_window.titlebar_double_click();
4897 }
4898
4899 /// Gets the window's title at the platform level.
4900 /// This is macOS specific.
4901 pub fn window_title(&self) -> String {
4902 self.platform_window.get_title()
4903 }
4904
4905 /// Returns a list of all tabbed windows and their titles.
4906 /// This is macOS specific.
4907 pub fn tabbed_windows(&self) -> Option<Vec<SystemWindowTab>> {
4908 self.platform_window.tabbed_windows()
4909 }
4910
4911 /// Returns the tab bar visibility.
4912 /// This is macOS specific.
4913 pub fn tab_bar_visible(&self) -> bool {
4914 self.platform_window.tab_bar_visible()
4915 }
4916
4917 /// Merges all open windows into a single tabbed window.
4918 /// This is macOS specific.
4919 pub fn merge_all_windows(&self) {
4920 self.platform_window.merge_all_windows()
4921 }
4922
4923 /// Moves the tab to a new containing window.
4924 /// This is macOS specific.
4925 pub fn move_tab_to_new_window(&self) {
4926 self.platform_window.move_tab_to_new_window()
4927 }
4928
4929 /// Shows or hides the window tab overview.
4930 /// This is macOS specific.
4931 pub fn toggle_window_tab_overview(&self) {
4932 self.platform_window.toggle_window_tab_overview()
4933 }
4934
4935 /// Sets the tabbing identifier for the window.
4936 /// This is macOS specific.
4937 pub fn set_tabbing_identifier(&self, tabbing_identifier: Option<String>) {
4938 self.platform_window
4939 .set_tabbing_identifier(tabbing_identifier)
4940 }
4941
4942 /// Toggles the inspector mode on this window.
4943 #[cfg(any(feature = "inspector", debug_assertions))]
4944 pub fn toggle_inspector(&mut self, cx: &mut App) {
4945 self.inspector = match self.inspector {
4946 None => Some(cx.new(|_| Inspector::new())),
4947 Some(_) => None,
4948 };
4949 self.refresh();
4950 }
4951
4952 /// Returns true if the window is in inspector mode.
4953 pub fn is_inspector_picking(&self, _cx: &App) -> bool {
4954 #[cfg(any(feature = "inspector", debug_assertions))]
4955 {
4956 if let Some(inspector) = &self.inspector {
4957 return inspector.read(_cx).is_picking();
4958 }
4959 }
4960 false
4961 }
4962
4963 /// Executes the provided function with mutable access to an inspector state.
4964 #[cfg(any(feature = "inspector", debug_assertions))]
4965 pub fn with_inspector_state<T: 'static, R>(
4966 &mut self,
4967 _inspector_id: Option<&crate::InspectorElementId>,
4968 cx: &mut App,
4969 f: impl FnOnce(&mut Option<T>, &mut Self) -> R,
4970 ) -> R {
4971 if let Some(inspector_id) = _inspector_id
4972 && let Some(inspector) = &self.inspector
4973 {
4974 let inspector = inspector.clone();
4975 let active_element_id = inspector.read(cx).active_element_id();
4976 if Some(inspector_id) == active_element_id {
4977 return inspector.update(cx, |inspector, _cx| {
4978 inspector.with_active_element_state(self, f)
4979 });
4980 }
4981 }
4982 f(&mut None, self)
4983 }
4984
4985 #[cfg(any(feature = "inspector", debug_assertions))]
4986 pub(crate) fn build_inspector_element_id(
4987 &mut self,
4988 path: crate::InspectorElementPath,
4989 ) -> crate::InspectorElementId {
4990 self.invalidator.debug_assert_paint_or_prepaint();
4991 let path = Rc::new(path);
4992 let next_instance_id = self
4993 .next_frame
4994 .next_inspector_instance_ids
4995 .entry(path.clone())
4996 .or_insert(0);
4997 let instance_id = *next_instance_id;
4998 *next_instance_id += 1;
4999 crate::InspectorElementId { path, instance_id }
5000 }
5001
5002 #[cfg(any(feature = "inspector", debug_assertions))]
5003 fn prepaint_inspector(&mut self, inspector_width: Pixels, cx: &mut App) -> Option<AnyElement> {
5004 if let Some(inspector) = self.inspector.take() {
5005 let mut inspector_element = AnyView::from(inspector.clone()).into_any_element();
5006 inspector_element.prepaint_as_root(
5007 point(self.viewport_size.width - inspector_width, px(0.0)),
5008 size(inspector_width, self.viewport_size.height).into(),
5009 self,
5010 cx,
5011 );
5012 self.inspector = Some(inspector);
5013 Some(inspector_element)
5014 } else {
5015 None
5016 }
5017 }
5018
5019 #[cfg(any(feature = "inspector", debug_assertions))]
5020 fn paint_inspector(&mut self, mut inspector_element: Option<AnyElement>, cx: &mut App) {
5021 if let Some(mut inspector_element) = inspector_element {
5022 inspector_element.paint(self, cx);
5023 };
5024 }
5025
5026 /// Registers a hitbox that can be used for inspector picking mode, allowing users to select and
5027 /// inspect UI elements by clicking on them.
5028 #[cfg(any(feature = "inspector", debug_assertions))]
5029 pub fn insert_inspector_hitbox(
5030 &mut self,
5031 hitbox_id: HitboxId,
5032 inspector_id: Option<&crate::InspectorElementId>,
5033 cx: &App,
5034 ) {
5035 self.invalidator.debug_assert_paint_or_prepaint();
5036 if !self.is_inspector_picking(cx) {
5037 return;
5038 }
5039 if let Some(inspector_id) = inspector_id {
5040 self.next_frame
5041 .inspector_hitboxes
5042 .insert(hitbox_id, inspector_id.clone());
5043 }
5044 }
5045
5046 #[cfg(any(feature = "inspector", debug_assertions))]
5047 fn paint_inspector_hitbox(&mut self, cx: &App) {
5048 if let Some(inspector) = self.inspector.as_ref() {
5049 let inspector = inspector.read(cx);
5050 if let Some((hitbox_id, _)) = self.hovered_inspector_hitbox(inspector, &self.next_frame)
5051 && let Some(hitbox) = self
5052 .next_frame
5053 .hitboxes
5054 .iter()
5055 .find(|hitbox| hitbox.id == hitbox_id)
5056 {
5057 self.paint_quad(crate::fill(hitbox.bounds, crate::rgba(0x61afef4d)));
5058 }
5059 }
5060 }
5061
5062 #[cfg(any(feature = "inspector", debug_assertions))]
5063 fn handle_inspector_mouse_event(&mut self, event: &dyn Any, cx: &mut App) {
5064 let Some(inspector) = self.inspector.clone() else {
5065 return;
5066 };
5067 if event.downcast_ref::<MouseMoveEvent>().is_some() {
5068 inspector.update(cx, |inspector, _cx| {
5069 if let Some((_, inspector_id)) =
5070 self.hovered_inspector_hitbox(inspector, &self.rendered_frame)
5071 {
5072 inspector.hover(inspector_id, self);
5073 }
5074 });
5075 } else if event.downcast_ref::<crate::MouseDownEvent>().is_some() {
5076 inspector.update(cx, |inspector, _cx| {
5077 if let Some((_, inspector_id)) =
5078 self.hovered_inspector_hitbox(inspector, &self.rendered_frame)
5079 {
5080 inspector.select(inspector_id, self);
5081 }
5082 });
5083 } else if let Some(event) = event.downcast_ref::<crate::ScrollWheelEvent>() {
5084 // This should be kept in sync with SCROLL_LINES in x11 platform.
5085 const SCROLL_LINES: f32 = 3.0;
5086 const SCROLL_PIXELS_PER_LAYER: f32 = 36.0;
5087 let delta_y = event
5088 .delta
5089 .pixel_delta(px(SCROLL_PIXELS_PER_LAYER / SCROLL_LINES))
5090 .y;
5091 if let Some(inspector) = self.inspector.clone() {
5092 inspector.update(cx, |inspector, _cx| {
5093 if let Some(depth) = inspector.pick_depth.as_mut() {
5094 *depth += f32::from(delta_y) / SCROLL_PIXELS_PER_LAYER;
5095 let max_depth = self.mouse_hit_test.ids.len() as f32 - 0.5;
5096 if *depth < 0.0 {
5097 *depth = 0.0;
5098 } else if *depth > max_depth {
5099 *depth = max_depth;
5100 }
5101 if let Some((_, inspector_id)) =
5102 self.hovered_inspector_hitbox(inspector, &self.rendered_frame)
5103 {
5104 inspector.set_active_element_id(inspector_id, self);
5105 }
5106 }
5107 });
5108 }
5109 }
5110 }
5111
5112 #[cfg(any(feature = "inspector", debug_assertions))]
5113 fn hovered_inspector_hitbox(
5114 &self,
5115 inspector: &Inspector,
5116 frame: &Frame,
5117 ) -> Option<(HitboxId, crate::InspectorElementId)> {
5118 if let Some(pick_depth) = inspector.pick_depth {
5119 let depth = (pick_depth as i64).try_into().unwrap_or(0);
5120 let max_skipped = self.mouse_hit_test.ids.len().saturating_sub(1);
5121 let skip_count = (depth as usize).min(max_skipped);
5122 for hitbox_id in self.mouse_hit_test.ids.iter().skip(skip_count) {
5123 if let Some(inspector_id) = frame.inspector_hitboxes.get(hitbox_id) {
5124 return Some((*hitbox_id, inspector_id.clone()));
5125 }
5126 }
5127 }
5128 None
5129 }
5130
5131 /// For testing: set the current modifier keys state.
5132 /// This does not generate any events.
5133 #[cfg(any(test, feature = "test-support"))]
5134 pub fn set_modifiers(&mut self, modifiers: Modifiers) {
5135 self.modifiers = modifiers;
5136 }
5137
5138 /// For testing: simulate a mouse move event to the given position.
5139 /// This dispatches the event through the normal event handling path,
5140 /// which will trigger hover states and tooltips.
5141 #[cfg(any(test, feature = "test-support"))]
5142 pub fn simulate_mouse_move(&mut self, position: Point<Pixels>, cx: &mut App) {
5143 let event = PlatformInput::MouseMove(MouseMoveEvent {
5144 position,
5145 modifiers: self.modifiers,
5146 pressed_button: None,
5147 });
5148 let _ = self.dispatch_event(event, cx);
5149 }
5150}
5151
5152// #[derive(Clone, Copy, Eq, PartialEq, Hash)]
5153slotmap::new_key_type! {
5154 /// A unique identifier for a window.
5155 pub struct WindowId;
5156}
5157
5158impl WindowId {
5159 /// Converts this window ID to a `u64`.
5160 pub fn as_u64(&self) -> u64 {
5161 self.0.as_ffi()
5162 }
5163}
5164
5165impl From<u64> for WindowId {
5166 fn from(value: u64) -> Self {
5167 WindowId(slotmap::KeyData::from_ffi(value))
5168 }
5169}
5170
5171/// A handle to a window with a specific root view type.
5172/// Note that this does not keep the window alive on its own.
5173#[derive(Deref, DerefMut)]
5174pub struct WindowHandle<V> {
5175 #[deref]
5176 #[deref_mut]
5177 pub(crate) any_handle: AnyWindowHandle,
5178 state_type: PhantomData<fn(V) -> V>,
5179}
5180
5181impl<V> Debug for WindowHandle<V> {
5182 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
5183 f.debug_struct("WindowHandle")
5184 .field("any_handle", &self.any_handle.id.as_u64())
5185 .finish()
5186 }
5187}
5188
5189impl<V: 'static + Render> WindowHandle<V> {
5190 /// Creates a new handle from a window ID.
5191 /// This does not check if the root type of the window is `V`.
5192 pub fn new(id: WindowId) -> Self {
5193 WindowHandle {
5194 any_handle: AnyWindowHandle {
5195 id,
5196 state_type: TypeId::of::<V>(),
5197 },
5198 state_type: PhantomData,
5199 }
5200 }
5201
5202 /// Get the root view out of this window.
5203 ///
5204 /// This will fail if the window is closed or if the root view's type does not match `V`.
5205 #[cfg(any(test, feature = "test-support"))]
5206 pub fn root<C>(&self, cx: &mut C) -> Result<Entity<V>>
5207 where
5208 C: AppContext,
5209 {
5210 cx.update_window(self.any_handle, |root_view, _, _| {
5211 root_view
5212 .downcast::<V>()
5213 .map_err(|_| anyhow!("the type of the window's root view has changed"))
5214 })?
5215 }
5216
5217 /// Updates the root view of this window.
5218 ///
5219 /// This will fail if the window has been closed or if the root view's type does not match
5220 pub fn update<C, R>(
5221 &self,
5222 cx: &mut C,
5223 update: impl FnOnce(&mut V, &mut Window, &mut Context<V>) -> R,
5224 ) -> Result<R>
5225 where
5226 C: AppContext,
5227 {
5228 cx.update_window(self.any_handle, |root_view, window, cx| {
5229 let view = root_view
5230 .downcast::<V>()
5231 .map_err(|_| anyhow!("the type of the window's root view has changed"))?;
5232
5233 Ok(view.update(cx, |view, cx| update(view, window, cx)))
5234 })?
5235 }
5236
5237 /// Read the root view out of this window.
5238 ///
5239 /// This will fail if the window is closed or if the root view's type does not match `V`.
5240 pub fn read<'a>(&self, cx: &'a App) -> Result<&'a V> {
5241 let x = cx
5242 .windows
5243 .get(self.id)
5244 .and_then(|window| {
5245 window
5246 .as_deref()
5247 .and_then(|window| window.root.clone())
5248 .map(|root_view| root_view.downcast::<V>())
5249 })
5250 .context("window not found")?
5251 .map_err(|_| anyhow!("the type of the window's root view has changed"))?;
5252
5253 Ok(x.read(cx))
5254 }
5255
5256 /// Read the root view out of this window, with a callback
5257 ///
5258 /// This will fail if the window is closed or if the root view's type does not match `V`.
5259 pub fn read_with<C, R>(&self, cx: &C, read_with: impl FnOnce(&V, &App) -> R) -> Result<R>
5260 where
5261 C: AppContext,
5262 {
5263 cx.read_window(self, |root_view, cx| read_with(root_view.read(cx), cx))
5264 }
5265
5266 /// Read the root view pointer off of this window.
5267 ///
5268 /// This will fail if the window is closed or if the root view's type does not match `V`.
5269 pub fn entity<C>(&self, cx: &C) -> Result<Entity<V>>
5270 where
5271 C: AppContext,
5272 {
5273 cx.read_window(self, |root_view, _cx| root_view)
5274 }
5275
5276 /// Check if this window is 'active'.
5277 ///
5278 /// Will return `None` if the window is closed or currently
5279 /// borrowed.
5280 pub fn is_active(&self, cx: &mut App) -> Option<bool> {
5281 cx.update_window(self.any_handle, |_, window, _| window.is_window_active())
5282 .ok()
5283 }
5284}
5285
5286impl<V> Copy for WindowHandle<V> {}
5287
5288impl<V> Clone for WindowHandle<V> {
5289 fn clone(&self) -> Self {
5290 *self
5291 }
5292}
5293
5294impl<V> PartialEq for WindowHandle<V> {
5295 fn eq(&self, other: &Self) -> bool {
5296 self.any_handle == other.any_handle
5297 }
5298}
5299
5300impl<V> Eq for WindowHandle<V> {}
5301
5302impl<V> Hash for WindowHandle<V> {
5303 fn hash<H: Hasher>(&self, state: &mut H) {
5304 self.any_handle.hash(state);
5305 }
5306}
5307
5308impl<V: 'static> From<WindowHandle<V>> for AnyWindowHandle {
5309 fn from(val: WindowHandle<V>) -> Self {
5310 val.any_handle
5311 }
5312}
5313
5314/// A handle to a window with any root view type, which can be downcast to a window with a specific root view type.
5315#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
5316pub struct AnyWindowHandle {
5317 pub(crate) id: WindowId,
5318 state_type: TypeId,
5319}
5320
5321impl AnyWindowHandle {
5322 /// Get the ID of this window.
5323 pub fn window_id(&self) -> WindowId {
5324 self.id
5325 }
5326
5327 /// Attempt to convert this handle to a window handle with a specific root view type.
5328 /// If the types do not match, this will return `None`.
5329 pub fn downcast<T: 'static>(&self) -> Option<WindowHandle<T>> {
5330 if TypeId::of::<T>() == self.state_type {
5331 Some(WindowHandle {
5332 any_handle: *self,
5333 state_type: PhantomData,
5334 })
5335 } else {
5336 None
5337 }
5338 }
5339
5340 /// Updates the state of the root view of this window.
5341 ///
5342 /// This will fail if the window has been closed.
5343 pub fn update<C, R>(
5344 self,
5345 cx: &mut C,
5346 update: impl FnOnce(AnyView, &mut Window, &mut App) -> R,
5347 ) -> Result<R>
5348 where
5349 C: AppContext,
5350 {
5351 cx.update_window(self, update)
5352 }
5353
5354 /// Read the state of the root view of this window.
5355 ///
5356 /// This will fail if the window has been closed.
5357 pub fn read<T, C, R>(self, cx: &C, read: impl FnOnce(Entity<T>, &App) -> R) -> Result<R>
5358 where
5359 C: AppContext,
5360 T: 'static,
5361 {
5362 let view = self
5363 .downcast::<T>()
5364 .context("the type of the window's root view has changed")?;
5365
5366 cx.read_window(&view, read)
5367 }
5368}
5369
5370impl HasWindowHandle for Window {
5371 fn window_handle(&self) -> Result<raw_window_handle::WindowHandle<'_>, HandleError> {
5372 self.platform_window.window_handle()
5373 }
5374}
5375
5376impl HasDisplayHandle for Window {
5377 fn display_handle(
5378 &self,
5379 ) -> std::result::Result<raw_window_handle::DisplayHandle<'_>, HandleError> {
5380 self.platform_window.display_handle()
5381 }
5382}
5383
5384/// An identifier for an [`Element`].
5385///
5386/// Can be constructed with a string, a number, or both, as well
5387/// as other internal representations.
5388#[derive(Clone, Debug, Eq, PartialEq, Hash)]
5389pub enum ElementId {
5390 /// The ID of a View element
5391 View(EntityId),
5392 /// An integer ID.
5393 Integer(u64),
5394 /// A string based ID.
5395 Name(SharedString),
5396 /// A UUID.
5397 Uuid(Uuid),
5398 /// An ID that's equated with a focus handle.
5399 FocusHandle(FocusId),
5400 /// A combination of a name and an integer.
5401 NamedInteger(SharedString, u64),
5402 /// A path.
5403 Path(Arc<std::path::Path>),
5404 /// A code location.
5405 CodeLocation(core::panic::Location<'static>),
5406 /// A labeled child of an element.
5407 NamedChild(Arc<ElementId>, SharedString),
5408}
5409
5410impl ElementId {
5411 /// Constructs an `ElementId::NamedInteger` from a name and `usize`.
5412 pub fn named_usize(name: impl Into<SharedString>, integer: usize) -> ElementId {
5413 Self::NamedInteger(name.into(), integer as u64)
5414 }
5415}
5416
5417impl Display for ElementId {
5418 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
5419 match self {
5420 ElementId::View(entity_id) => write!(f, "view-{}", entity_id)?,
5421 ElementId::Integer(ix) => write!(f, "{}", ix)?,
5422 ElementId::Name(name) => write!(f, "{}", name)?,
5423 ElementId::FocusHandle(_) => write!(f, "FocusHandle")?,
5424 ElementId::NamedInteger(s, i) => write!(f, "{}-{}", s, i)?,
5425 ElementId::Uuid(uuid) => write!(f, "{}", uuid)?,
5426 ElementId::Path(path) => write!(f, "{}", path.display())?,
5427 ElementId::CodeLocation(location) => write!(f, "{}", location)?,
5428 ElementId::NamedChild(id, name) => write!(f, "{}-{}", id, name)?,
5429 }
5430
5431 Ok(())
5432 }
5433}
5434
5435impl TryInto<SharedString> for ElementId {
5436 type Error = anyhow::Error;
5437
5438 fn try_into(self) -> anyhow::Result<SharedString> {
5439 if let ElementId::Name(name) = self {
5440 Ok(name)
5441 } else {
5442 anyhow::bail!("element id is not string")
5443 }
5444 }
5445}
5446
5447impl From<usize> for ElementId {
5448 fn from(id: usize) -> Self {
5449 ElementId::Integer(id as u64)
5450 }
5451}
5452
5453impl From<i32> for ElementId {
5454 fn from(id: i32) -> Self {
5455 Self::Integer(id as u64)
5456 }
5457}
5458
5459impl From<SharedString> for ElementId {
5460 fn from(name: SharedString) -> Self {
5461 ElementId::Name(name)
5462 }
5463}
5464
5465impl From<String> for ElementId {
5466 fn from(name: String) -> Self {
5467 ElementId::Name(name.into())
5468 }
5469}
5470
5471impl From<Arc<str>> for ElementId {
5472 fn from(name: Arc<str>) -> Self {
5473 ElementId::Name(name.into())
5474 }
5475}
5476
5477impl From<Arc<std::path::Path>> for ElementId {
5478 fn from(path: Arc<std::path::Path>) -> Self {
5479 ElementId::Path(path)
5480 }
5481}
5482
5483impl From<&'static str> for ElementId {
5484 fn from(name: &'static str) -> Self {
5485 ElementId::Name(name.into())
5486 }
5487}
5488
5489impl<'a> From<&'a FocusHandle> for ElementId {
5490 fn from(handle: &'a FocusHandle) -> Self {
5491 ElementId::FocusHandle(handle.id)
5492 }
5493}
5494
5495impl From<(&'static str, EntityId)> for ElementId {
5496 fn from((name, id): (&'static str, EntityId)) -> Self {
5497 ElementId::NamedInteger(name.into(), id.as_u64())
5498 }
5499}
5500
5501impl From<(&'static str, usize)> for ElementId {
5502 fn from((name, id): (&'static str, usize)) -> Self {
5503 ElementId::NamedInteger(name.into(), id as u64)
5504 }
5505}
5506
5507impl From<(SharedString, usize)> for ElementId {
5508 fn from((name, id): (SharedString, usize)) -> Self {
5509 ElementId::NamedInteger(name, id as u64)
5510 }
5511}
5512
5513impl From<(&'static str, u64)> for ElementId {
5514 fn from((name, id): (&'static str, u64)) -> Self {
5515 ElementId::NamedInteger(name.into(), id)
5516 }
5517}
5518
5519impl From<Uuid> for ElementId {
5520 fn from(value: Uuid) -> Self {
5521 Self::Uuid(value)
5522 }
5523}
5524
5525impl From<(&'static str, u32)> for ElementId {
5526 fn from((name, id): (&'static str, u32)) -> Self {
5527 ElementId::NamedInteger(name.into(), id.into())
5528 }
5529}
5530
5531impl<T: Into<SharedString>> From<(ElementId, T)> for ElementId {
5532 fn from((id, name): (ElementId, T)) -> Self {
5533 ElementId::NamedChild(Arc::new(id), name.into())
5534 }
5535}
5536
5537impl From<&'static core::panic::Location<'static>> for ElementId {
5538 fn from(location: &'static core::panic::Location<'static>) -> Self {
5539 ElementId::CodeLocation(*location)
5540 }
5541}
5542
5543/// A rectangle to be rendered in the window at the given position and size.
5544/// Passed as an argument [`Window::paint_quad`].
5545#[derive(Clone)]
5546pub struct PaintQuad {
5547 /// The bounds of the quad within the window.
5548 pub bounds: Bounds<Pixels>,
5549 /// The radii of the quad's corners.
5550 pub corner_radii: Corners<Pixels>,
5551 /// The background color of the quad.
5552 pub background: Background,
5553 /// The widths of the quad's borders.
5554 pub border_widths: Edges<Pixels>,
5555 /// The color of the quad's borders.
5556 pub border_color: Hsla,
5557 /// The style of the quad's borders.
5558 pub border_style: BorderStyle,
5559}
5560
5561impl PaintQuad {
5562 /// Sets the corner radii of the quad.
5563 pub fn corner_radii(self, corner_radii: impl Into<Corners<Pixels>>) -> Self {
5564 PaintQuad {
5565 corner_radii: corner_radii.into(),
5566 ..self
5567 }
5568 }
5569
5570 /// Sets the border widths of the quad.
5571 pub fn border_widths(self, border_widths: impl Into<Edges<Pixels>>) -> Self {
5572 PaintQuad {
5573 border_widths: border_widths.into(),
5574 ..self
5575 }
5576 }
5577
5578 /// Sets the border color of the quad.
5579 pub fn border_color(self, border_color: impl Into<Hsla>) -> Self {
5580 PaintQuad {
5581 border_color: border_color.into(),
5582 ..self
5583 }
5584 }
5585
5586 /// Sets the background color of the quad.
5587 pub fn background(self, background: impl Into<Background>) -> Self {
5588 PaintQuad {
5589 background: background.into(),
5590 ..self
5591 }
5592 }
5593}
5594
5595/// Creates a quad with the given parameters.
5596pub fn quad(
5597 bounds: Bounds<Pixels>,
5598 corner_radii: impl Into<Corners<Pixels>>,
5599 background: impl Into<Background>,
5600 border_widths: impl Into<Edges<Pixels>>,
5601 border_color: impl Into<Hsla>,
5602 border_style: BorderStyle,
5603) -> PaintQuad {
5604 PaintQuad {
5605 bounds,
5606 corner_radii: corner_radii.into(),
5607 background: background.into(),
5608 border_widths: border_widths.into(),
5609 border_color: border_color.into(),
5610 border_style,
5611 }
5612}
5613
5614/// Creates a filled quad with the given bounds and background color.
5615pub fn fill(bounds: impl Into<Bounds<Pixels>>, background: impl Into<Background>) -> PaintQuad {
5616 PaintQuad {
5617 bounds: bounds.into(),
5618 corner_radii: (0.).into(),
5619 background: background.into(),
5620 border_widths: (0.).into(),
5621 border_color: transparent_black(),
5622 border_style: BorderStyle::default(),
5623 }
5624}
5625
5626/// Creates a rectangle outline with the given bounds, border color, and a 1px border width
5627pub fn outline(
5628 bounds: impl Into<Bounds<Pixels>>,
5629 border_color: impl Into<Hsla>,
5630 border_style: BorderStyle,
5631) -> PaintQuad {
5632 PaintQuad {
5633 bounds: bounds.into(),
5634 corner_radii: (0.).into(),
5635 background: transparent_black().into(),
5636 border_widths: (1.).into(),
5637 border_color: border_color.into(),
5638 border_style,
5639 }
5640}