window.rs

   1use crate::{
   2    px, size, Action, AnyBox, AnyDrag, AnyHandle, AnyView, AppContext, AsyncWindowContext,
   3    AvailableSpace, Bounds, BoxShadow, Context, Corners, DevicePixels, DispatchContext, DisplayId,
   4    Edges, Effect, Element, EntityId, EventEmitter, ExternalPaths, FileDropEvent, FocusEvent,
   5    FontId, GlobalElementId, GlyphId, Handle, Hsla, ImageData, InputEvent, IsZero, KeyListener,
   6    KeyMatch, KeyMatcher, Keystroke, LayoutId, MainThread, MainThreadOnly, Modifiers,
   7    MonochromeSprite, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, Path, Pixels,
   8    PlatformAtlas, PlatformWindow, Point, PolychromeSprite, Quad, Reference, RenderGlyphParams,
   9    RenderImageParams, RenderSvgParams, ScaledPixels, SceneBuilder, Shadow, SharedString, Size,
  10    Style, Subscription, TaffyLayoutEngine, Task, Underline, UnderlineStyle, WeakHandle,
  11    WindowOptions, SUBPIXEL_VARIANTS,
  12};
  13use anyhow::Result;
  14use collections::HashMap;
  15use derive_more::{Deref, DerefMut};
  16use parking_lot::RwLock;
  17use slotmap::SlotMap;
  18use smallvec::SmallVec;
  19use std::{
  20    any::{Any, TypeId},
  21    borrow::{Borrow, BorrowMut, Cow},
  22    fmt::Debug,
  23    future::Future,
  24    marker::PhantomData,
  25    mem,
  26    sync::{
  27        atomic::{AtomicUsize, Ordering::SeqCst},
  28        Arc,
  29    },
  30};
  31use util::ResultExt;
  32
  33#[derive(Deref, DerefMut, Ord, PartialOrd, Eq, PartialEq, Clone, Default)]
  34pub struct StackingOrder(pub(crate) SmallVec<[u32; 16]>);
  35
  36#[derive(Default, Copy, Clone, Debug, Eq, PartialEq)]
  37pub enum DispatchPhase {
  38    /// After the capture phase comes the bubble phase, in which event handlers are
  39    /// invoked front to back. This is the phase you'll usually want to use for event handlers.
  40    #[default]
  41    Bubble,
  42    /// During the initial capture phase, event handlers are invoked back to front. This phase
  43    /// is used for special purposes such as clearing the "pressed" state for click events. If
  44    /// you stop event propagation during this phase, you need to know what you're doing. Handlers
  45    /// outside of the immediate region may rely on detecting non-local events during this phase.
  46    Capture,
  47}
  48
  49type AnyListener = Arc<dyn Fn(&dyn Any, DispatchPhase, &mut WindowContext) + Send + Sync + 'static>;
  50type AnyKeyListener = Arc<
  51    dyn Fn(
  52            &dyn Any,
  53            &[&DispatchContext],
  54            DispatchPhase,
  55            &mut WindowContext,
  56        ) -> Option<Box<dyn Action>>
  57        + Send
  58        + Sync
  59        + 'static,
  60>;
  61type AnyFocusListener = Arc<dyn Fn(&FocusEvent, &mut WindowContext) + Send + Sync + 'static>;
  62
  63slotmap::new_key_type! { pub struct FocusId; }
  64
  65pub struct FocusHandle {
  66    pub(crate) id: FocusId,
  67    handles: Arc<RwLock<SlotMap<FocusId, AtomicUsize>>>,
  68}
  69
  70impl FocusHandle {
  71    pub(crate) fn new(handles: &Arc<RwLock<SlotMap<FocusId, AtomicUsize>>>) -> Self {
  72        let id = handles.write().insert(AtomicUsize::new(1));
  73        Self {
  74            id,
  75            handles: handles.clone(),
  76        }
  77    }
  78
  79    pub(crate) fn for_id(
  80        id: FocusId,
  81        handles: &Arc<RwLock<SlotMap<FocusId, AtomicUsize>>>,
  82    ) -> Option<Self> {
  83        let lock = handles.read();
  84        let ref_count = lock.get(id)?;
  85        if ref_count.load(SeqCst) == 0 {
  86            None
  87        } else {
  88            ref_count.fetch_add(1, SeqCst);
  89            Some(Self {
  90                id,
  91                handles: handles.clone(),
  92            })
  93        }
  94    }
  95
  96    pub fn is_focused(&self, cx: &WindowContext) -> bool {
  97        cx.window.focus == Some(self.id)
  98    }
  99
 100    pub fn contains_focused(&self, cx: &WindowContext) -> bool {
 101        cx.focused()
 102            .map_or(false, |focused| self.contains(&focused, cx))
 103    }
 104
 105    pub fn within_focused(&self, cx: &WindowContext) -> bool {
 106        let focused = cx.focused();
 107        focused.map_or(false, |focused| focused.contains(self, cx))
 108    }
 109
 110    pub(crate) fn contains(&self, other: &Self, cx: &WindowContext) -> bool {
 111        let mut ancestor = Some(other.id);
 112        while let Some(ancestor_id) = ancestor {
 113            if self.id == ancestor_id {
 114                return true;
 115            } else {
 116                ancestor = cx.window.focus_parents_by_child.get(&ancestor_id).copied();
 117            }
 118        }
 119        false
 120    }
 121}
 122
 123impl Clone for FocusHandle {
 124    fn clone(&self) -> Self {
 125        Self::for_id(self.id, &self.handles).unwrap()
 126    }
 127}
 128
 129impl PartialEq for FocusHandle {
 130    fn eq(&self, other: &Self) -> bool {
 131        self.id == other.id
 132    }
 133}
 134
 135impl Eq for FocusHandle {}
 136
 137impl Drop for FocusHandle {
 138    fn drop(&mut self) {
 139        self.handles
 140            .read()
 141            .get(self.id)
 142            .unwrap()
 143            .fetch_sub(1, SeqCst);
 144    }
 145}
 146
 147pub struct Window {
 148    pub(crate) handle: AnyWindowHandle,
 149    platform_window: MainThreadOnly<Box<dyn PlatformWindow>>,
 150    display_id: DisplayId,
 151    sprite_atlas: Arc<dyn PlatformAtlas>,
 152    rem_size: Pixels,
 153    content_size: Size<Pixels>,
 154    layout_engine: TaffyLayoutEngine,
 155    pub(crate) root_view: Option<AnyView>,
 156    pub(crate) element_id_stack: GlobalElementId,
 157    prev_frame_element_states: HashMap<GlobalElementId, AnyBox>,
 158    element_states: HashMap<GlobalElementId, AnyBox>,
 159    prev_frame_key_matchers: HashMap<GlobalElementId, KeyMatcher>,
 160    key_matchers: HashMap<GlobalElementId, KeyMatcher>,
 161    z_index_stack: StackingOrder,
 162    content_mask_stack: Vec<ContentMask<Pixels>>,
 163    element_offset_stack: Vec<Point<Pixels>>,
 164    mouse_listeners: HashMap<TypeId, Vec<(StackingOrder, AnyListener)>>,
 165    key_dispatch_stack: Vec<KeyDispatchStackFrame>,
 166    freeze_key_dispatch_stack: bool,
 167    focus_stack: Vec<FocusId>,
 168    focus_parents_by_child: HashMap<FocusId, FocusId>,
 169    pub(crate) focus_listeners: Vec<AnyFocusListener>,
 170    pub(crate) focus_handles: Arc<RwLock<SlotMap<FocusId, AtomicUsize>>>,
 171    default_prevented: bool,
 172    mouse_position: Point<Pixels>,
 173    scale_factor: f32,
 174    pub(crate) scene_builder: SceneBuilder,
 175    pub(crate) dirty: bool,
 176    pub(crate) last_blur: Option<Option<FocusId>>,
 177    pub(crate) focus: Option<FocusId>,
 178}
 179
 180impl Window {
 181    pub(crate) fn new(
 182        handle: AnyWindowHandle,
 183        options: WindowOptions,
 184        cx: &mut MainThread<AppContext>,
 185    ) -> Self {
 186        let platform_window = cx.platform().open_window(handle, options);
 187        let display_id = platform_window.display().id();
 188        let sprite_atlas = platform_window.sprite_atlas();
 189        let mouse_position = platform_window.mouse_position();
 190        let content_size = platform_window.content_size();
 191        let scale_factor = platform_window.scale_factor();
 192        platform_window.on_resize(Box::new({
 193            let cx = cx.to_async();
 194            move |content_size, scale_factor| {
 195                cx.update_window(handle, |cx| {
 196                    cx.window.scale_factor = scale_factor;
 197                    cx.window.scene_builder = SceneBuilder::new();
 198                    cx.window.content_size = content_size;
 199                    cx.window.display_id = cx
 200                        .window
 201                        .platform_window
 202                        .borrow_on_main_thread()
 203                        .display()
 204                        .id();
 205                    cx.window.dirty = true;
 206                })
 207                .log_err();
 208            }
 209        }));
 210
 211        platform_window.on_input({
 212            let cx = cx.to_async();
 213            Box::new(move |event| {
 214                cx.update_window(handle, |cx| cx.dispatch_event(event))
 215                    .log_err()
 216                    .unwrap_or(true)
 217            })
 218        });
 219
 220        let platform_window = MainThreadOnly::new(Arc::new(platform_window), cx.executor.clone());
 221
 222        Window {
 223            handle,
 224            platform_window,
 225            display_id,
 226            sprite_atlas,
 227            rem_size: px(16.),
 228            content_size,
 229            layout_engine: TaffyLayoutEngine::new(),
 230            root_view: None,
 231            element_id_stack: GlobalElementId::default(),
 232            prev_frame_element_states: HashMap::default(),
 233            element_states: HashMap::default(),
 234            prev_frame_key_matchers: HashMap::default(),
 235            key_matchers: HashMap::default(),
 236            z_index_stack: StackingOrder(SmallVec::new()),
 237            content_mask_stack: Vec::new(),
 238            element_offset_stack: Vec::new(),
 239            mouse_listeners: HashMap::default(),
 240            key_dispatch_stack: Vec::new(),
 241            freeze_key_dispatch_stack: false,
 242            focus_stack: Vec::new(),
 243            focus_parents_by_child: HashMap::default(),
 244            focus_listeners: Vec::new(),
 245            focus_handles: Arc::new(RwLock::new(SlotMap::with_key())),
 246            default_prevented: true,
 247            mouse_position,
 248            scale_factor,
 249            scene_builder: SceneBuilder::new(),
 250            dirty: true,
 251            last_blur: None,
 252            focus: None,
 253        }
 254    }
 255}
 256
 257enum KeyDispatchStackFrame {
 258    Listener {
 259        event_type: TypeId,
 260        listener: AnyKeyListener,
 261    },
 262    Context(DispatchContext),
 263}
 264
 265#[derive(Clone, Debug, Default, PartialEq, Eq)]
 266#[repr(C)]
 267pub struct ContentMask<P: Clone + Default + Debug> {
 268    pub bounds: Bounds<P>,
 269}
 270
 271impl ContentMask<Pixels> {
 272    pub fn scale(&self, factor: f32) -> ContentMask<ScaledPixels> {
 273        ContentMask {
 274            bounds: self.bounds.scale(factor),
 275        }
 276    }
 277
 278    pub fn intersect(&self, other: &Self) -> Self {
 279        let bounds = self.bounds.intersect(&other.bounds);
 280        ContentMask { bounds }
 281    }
 282}
 283
 284pub struct WindowContext<'a, 'w> {
 285    app: Reference<'a, AppContext>,
 286    pub(crate) window: Reference<'w, Window>,
 287}
 288
 289impl<'a, 'w> WindowContext<'a, 'w> {
 290    pub(crate) fn immutable(app: &'a AppContext, window: &'w Window) -> Self {
 291        Self {
 292            app: Reference::Immutable(app),
 293            window: Reference::Immutable(window),
 294        }
 295    }
 296
 297    pub(crate) fn mutable(app: &'a mut AppContext, window: &'w mut Window) -> Self {
 298        Self {
 299            app: Reference::Mutable(app),
 300            window: Reference::Mutable(window),
 301        }
 302    }
 303
 304    pub fn window_handle(&self) -> AnyWindowHandle {
 305        self.window.handle
 306    }
 307
 308    pub fn root_view(&self) -> Option<AnyHandle> {
 309        Some(self.window.root_view.as_ref()?.entity_handle())
 310    }
 311
 312    pub fn notify(&mut self) {
 313        self.window.dirty = true;
 314    }
 315
 316    pub fn focus_handle(&mut self) -> FocusHandle {
 317        FocusHandle::new(&self.window.focus_handles)
 318    }
 319
 320    pub fn focused(&self) -> Option<FocusHandle> {
 321        self.window
 322            .focus
 323            .and_then(|id| FocusHandle::for_id(id, &self.window.focus_handles))
 324    }
 325
 326    pub fn focus(&mut self, handle: &FocusHandle) {
 327        if self.window.last_blur.is_none() {
 328            self.window.last_blur = Some(self.window.focus);
 329        }
 330
 331        self.window.focus = Some(handle.id);
 332        self.app.push_effect(Effect::FocusChanged {
 333            window_handle: self.window.handle,
 334            focused: Some(handle.id),
 335        });
 336        self.notify();
 337    }
 338
 339    pub fn blur(&mut self) {
 340        if self.window.last_blur.is_none() {
 341            self.window.last_blur = Some(self.window.focus);
 342        }
 343
 344        self.window.focus = None;
 345        self.app.push_effect(Effect::FocusChanged {
 346            window_handle: self.window.handle,
 347            focused: None,
 348        });
 349        self.notify();
 350    }
 351
 352    pub fn run_on_main<R>(
 353        &mut self,
 354        f: impl FnOnce(&mut MainThread<WindowContext<'_, '_>>) -> R + Send + 'static,
 355    ) -> Task<Result<R>>
 356    where
 357        R: Send + 'static,
 358    {
 359        if self.executor.is_main_thread() {
 360            Task::ready(Ok(f(unsafe {
 361                mem::transmute::<&mut Self, &mut MainThread<Self>>(self)
 362            })))
 363        } else {
 364            let handle = self.window.handle;
 365            self.app.run_on_main(move |cx| cx.update_window(handle, f))
 366        }
 367    }
 368
 369    pub fn to_async(&self) -> AsyncWindowContext {
 370        AsyncWindowContext::new(self.app.to_async(), self.window.handle)
 371    }
 372
 373    pub fn on_next_frame(&mut self, f: impl FnOnce(&mut WindowContext) + Send + 'static) {
 374        let f = Box::new(f);
 375        let display_id = self.window.display_id;
 376        self.run_on_main(move |cx| {
 377            if let Some(callbacks) = cx.next_frame_callbacks.get_mut(&display_id) {
 378                callbacks.push(f);
 379                // If there was already a callback, it means that we already scheduled a frame.
 380                if callbacks.len() > 1 {
 381                    return;
 382                }
 383            } else {
 384                let async_cx = cx.to_async();
 385                cx.next_frame_callbacks.insert(display_id, vec![f]);
 386                cx.platform().set_display_link_output_callback(
 387                    display_id,
 388                    Box::new(move |_current_time, _output_time| {
 389                        let _ = async_cx.update(|cx| {
 390                            let callbacks = cx
 391                                .next_frame_callbacks
 392                                .get_mut(&display_id)
 393                                .unwrap()
 394                                .drain(..)
 395                                .collect::<Vec<_>>();
 396                            for callback in callbacks {
 397                                callback(cx);
 398                            }
 399
 400                            cx.run_on_main(move |cx| {
 401                                if cx.next_frame_callbacks.get(&display_id).unwrap().is_empty() {
 402                                    cx.platform().stop_display_link(display_id);
 403                                }
 404                            })
 405                            .detach();
 406                        });
 407                    }),
 408                );
 409            }
 410
 411            cx.platform().start_display_link(display_id);
 412        })
 413        .detach();
 414    }
 415
 416    pub fn spawn<Fut, R>(
 417        &mut self,
 418        f: impl FnOnce(AnyWindowHandle, AsyncWindowContext) -> Fut + Send + 'static,
 419    ) -> Task<R>
 420    where
 421        R: Send + 'static,
 422        Fut: Future<Output = R> + Send + 'static,
 423    {
 424        let window = self.window.handle;
 425        self.app.spawn(move |app| {
 426            let cx = AsyncWindowContext::new(app, window);
 427            let future = f(window, cx);
 428            async move { future.await }
 429        })
 430    }
 431
 432    pub fn update_global<G, R>(&mut self, f: impl FnOnce(&mut G, &mut Self) -> R) -> R
 433    where
 434        G: 'static,
 435    {
 436        let mut global = self.app.lease_global::<G>();
 437        let result = f(&mut global, self);
 438        self.app.end_global_lease(global);
 439        result
 440    }
 441
 442    pub fn request_layout(
 443        &mut self,
 444        style: &Style,
 445        children: impl IntoIterator<Item = LayoutId>,
 446    ) -> LayoutId {
 447        self.app.layout_id_buffer.clear();
 448        self.app.layout_id_buffer.extend(children.into_iter());
 449        let rem_size = self.rem_size();
 450
 451        self.window
 452            .layout_engine
 453            .request_layout(style, rem_size, &self.app.layout_id_buffer)
 454    }
 455
 456    pub fn request_measured_layout<
 457        F: Fn(Size<Option<Pixels>>, Size<AvailableSpace>) -> Size<Pixels> + Send + Sync + 'static,
 458    >(
 459        &mut self,
 460        style: Style,
 461        rem_size: Pixels,
 462        measure: F,
 463    ) -> LayoutId {
 464        self.window
 465            .layout_engine
 466            .request_measured_layout(style, rem_size, measure)
 467    }
 468
 469    pub fn layout_bounds(&mut self, layout_id: LayoutId) -> Bounds<Pixels> {
 470        let mut bounds = self
 471            .window
 472            .layout_engine
 473            .layout_bounds(layout_id)
 474            .map(Into::into);
 475        bounds.origin += self.element_offset();
 476        bounds
 477    }
 478
 479    pub fn scale_factor(&self) -> f32 {
 480        self.window.scale_factor
 481    }
 482
 483    pub fn rem_size(&self) -> Pixels {
 484        self.window.rem_size
 485    }
 486
 487    pub fn line_height(&self) -> Pixels {
 488        let rem_size = self.rem_size();
 489        let text_style = self.text_style();
 490        text_style
 491            .line_height
 492            .to_pixels(text_style.font_size.into(), rem_size)
 493    }
 494
 495    pub fn prevent_default(&mut self) {
 496        self.window.default_prevented = true;
 497    }
 498
 499    pub fn default_prevented(&self) -> bool {
 500        self.window.default_prevented
 501    }
 502
 503    pub fn on_mouse_event<Event: 'static>(
 504        &mut self,
 505        handler: impl Fn(&Event, DispatchPhase, &mut WindowContext) + Send + Sync + 'static,
 506    ) {
 507        let order = self.window.z_index_stack.clone();
 508        self.window
 509            .mouse_listeners
 510            .entry(TypeId::of::<Event>())
 511            .or_default()
 512            .push((
 513                order,
 514                Arc::new(move |event: &dyn Any, phase, cx| {
 515                    handler(event.downcast_ref().unwrap(), phase, cx)
 516                }),
 517            ))
 518    }
 519
 520    pub fn mouse_position(&self) -> Point<Pixels> {
 521        self.window.mouse_position
 522    }
 523
 524    pub fn stack<R>(&mut self, order: u32, f: impl FnOnce(&mut Self) -> R) -> R {
 525        self.window.z_index_stack.push(order);
 526        let result = f(self);
 527        self.window.z_index_stack.pop();
 528        result
 529    }
 530
 531    pub fn paint_shadows(
 532        &mut self,
 533        bounds: Bounds<Pixels>,
 534        corner_radii: Corners<Pixels>,
 535        shadows: &[BoxShadow],
 536    ) {
 537        let scale_factor = self.scale_factor();
 538        let content_mask = self.content_mask();
 539        let window = &mut *self.window;
 540        for shadow in shadows {
 541            let mut shadow_bounds = bounds;
 542            shadow_bounds.origin += shadow.offset;
 543            shadow_bounds.dilate(shadow.spread_radius);
 544            window.scene_builder.insert(
 545                &window.z_index_stack,
 546                Shadow {
 547                    order: 0,
 548                    bounds: shadow_bounds.scale(scale_factor),
 549                    content_mask: content_mask.scale(scale_factor),
 550                    corner_radii: corner_radii.scale(scale_factor),
 551                    color: shadow.color,
 552                    blur_radius: shadow.blur_radius.scale(scale_factor),
 553                },
 554            );
 555        }
 556    }
 557
 558    pub fn paint_quad(
 559        &mut self,
 560        bounds: Bounds<Pixels>,
 561        corner_radii: Corners<Pixels>,
 562        background: impl Into<Hsla>,
 563        border_widths: Edges<Pixels>,
 564        border_color: impl Into<Hsla>,
 565    ) {
 566        let scale_factor = self.scale_factor();
 567        let content_mask = self.content_mask();
 568
 569        let window = &mut *self.window;
 570        window.scene_builder.insert(
 571            &window.z_index_stack,
 572            Quad {
 573                order: 0,
 574                bounds: bounds.scale(scale_factor),
 575                content_mask: content_mask.scale(scale_factor),
 576                background: background.into(),
 577                border_color: border_color.into(),
 578                corner_radii: corner_radii.scale(scale_factor),
 579                border_widths: border_widths.scale(scale_factor),
 580            },
 581        );
 582    }
 583
 584    pub fn paint_path(&mut self, mut path: Path<Pixels>, color: impl Into<Hsla>) {
 585        let scale_factor = self.scale_factor();
 586        let content_mask = self.content_mask();
 587        path.content_mask = content_mask;
 588        path.color = color.into();
 589        let window = &mut *self.window;
 590        window
 591            .scene_builder
 592            .insert(&window.z_index_stack, path.scale(scale_factor));
 593    }
 594
 595    pub fn paint_underline(
 596        &mut self,
 597        origin: Point<Pixels>,
 598        width: Pixels,
 599        style: &UnderlineStyle,
 600    ) -> Result<()> {
 601        let scale_factor = self.scale_factor();
 602        let height = if style.wavy {
 603            style.thickness * 3.
 604        } else {
 605            style.thickness
 606        };
 607        let bounds = Bounds {
 608            origin,
 609            size: size(width, height),
 610        };
 611        let content_mask = self.content_mask();
 612        let window = &mut *self.window;
 613        window.scene_builder.insert(
 614            &window.z_index_stack,
 615            Underline {
 616                order: 0,
 617                bounds: bounds.scale(scale_factor),
 618                content_mask: content_mask.scale(scale_factor),
 619                thickness: style.thickness.scale(scale_factor),
 620                color: style.color.unwrap_or_default(),
 621                wavy: style.wavy,
 622            },
 623        );
 624        Ok(())
 625    }
 626
 627    pub fn paint_glyph(
 628        &mut self,
 629        origin: Point<Pixels>,
 630        font_id: FontId,
 631        glyph_id: GlyphId,
 632        font_size: Pixels,
 633        color: Hsla,
 634    ) -> Result<()> {
 635        let scale_factor = self.scale_factor();
 636        let glyph_origin = origin.scale(scale_factor);
 637        let subpixel_variant = Point {
 638            x: (glyph_origin.x.0.fract() * SUBPIXEL_VARIANTS as f32).floor() as u8,
 639            y: (glyph_origin.y.0.fract() * SUBPIXEL_VARIANTS as f32).floor() as u8,
 640        };
 641        let params = RenderGlyphParams {
 642            font_id,
 643            glyph_id,
 644            font_size,
 645            subpixel_variant,
 646            scale_factor,
 647            is_emoji: false,
 648        };
 649
 650        let raster_bounds = self.text_system().raster_bounds(&params)?;
 651        if !raster_bounds.is_zero() {
 652            let tile =
 653                self.window
 654                    .sprite_atlas
 655                    .get_or_insert_with(&params.clone().into(), &mut || {
 656                        let (size, bytes) = self.text_system().rasterize_glyph(&params)?;
 657                        Ok((size, Cow::Owned(bytes)))
 658                    })?;
 659            let bounds = Bounds {
 660                origin: glyph_origin.map(|px| px.floor()) + raster_bounds.origin.map(Into::into),
 661                size: tile.bounds.size.map(Into::into),
 662            };
 663            let content_mask = self.content_mask().scale(scale_factor);
 664            let window = &mut *self.window;
 665            window.scene_builder.insert(
 666                &window.z_index_stack,
 667                MonochromeSprite {
 668                    order: 0,
 669                    bounds,
 670                    content_mask,
 671                    color,
 672                    tile,
 673                },
 674            );
 675        }
 676        Ok(())
 677    }
 678
 679    pub fn paint_emoji(
 680        &mut self,
 681        origin: Point<Pixels>,
 682        font_id: FontId,
 683        glyph_id: GlyphId,
 684        font_size: Pixels,
 685    ) -> Result<()> {
 686        let scale_factor = self.scale_factor();
 687        let glyph_origin = origin.scale(scale_factor);
 688        let params = RenderGlyphParams {
 689            font_id,
 690            glyph_id,
 691            font_size,
 692            // We don't render emojis with subpixel variants.
 693            subpixel_variant: Default::default(),
 694            scale_factor,
 695            is_emoji: true,
 696        };
 697
 698        let raster_bounds = self.text_system().raster_bounds(&params)?;
 699        if !raster_bounds.is_zero() {
 700            let tile =
 701                self.window
 702                    .sprite_atlas
 703                    .get_or_insert_with(&params.clone().into(), &mut || {
 704                        let (size, bytes) = self.text_system().rasterize_glyph(&params)?;
 705                        Ok((size, Cow::Owned(bytes)))
 706                    })?;
 707            let bounds = Bounds {
 708                origin: glyph_origin.map(|px| px.floor()) + raster_bounds.origin.map(Into::into),
 709                size: tile.bounds.size.map(Into::into),
 710            };
 711            let content_mask = self.content_mask().scale(scale_factor);
 712            let window = &mut *self.window;
 713
 714            window.scene_builder.insert(
 715                &window.z_index_stack,
 716                PolychromeSprite {
 717                    order: 0,
 718                    bounds,
 719                    corner_radii: Default::default(),
 720                    content_mask,
 721                    tile,
 722                    grayscale: false,
 723                },
 724            );
 725        }
 726        Ok(())
 727    }
 728
 729    pub fn paint_svg(
 730        &mut self,
 731        bounds: Bounds<Pixels>,
 732        path: SharedString,
 733        color: Hsla,
 734    ) -> Result<()> {
 735        let scale_factor = self.scale_factor();
 736        let bounds = bounds.scale(scale_factor);
 737        // Render the SVG at twice the size to get a higher quality result.
 738        let params = RenderSvgParams {
 739            path,
 740            size: bounds
 741                .size
 742                .map(|pixels| DevicePixels::from((pixels.0 * 2.).ceil() as i32)),
 743        };
 744
 745        let tile =
 746            self.window
 747                .sprite_atlas
 748                .get_or_insert_with(&params.clone().into(), &mut || {
 749                    let bytes = self.svg_renderer.render(&params)?;
 750                    Ok((params.size, Cow::Owned(bytes)))
 751                })?;
 752        let content_mask = self.content_mask().scale(scale_factor);
 753
 754        let window = &mut *self.window;
 755        window.scene_builder.insert(
 756            &window.z_index_stack,
 757            MonochromeSprite {
 758                order: 0,
 759                bounds,
 760                content_mask,
 761                color,
 762                tile,
 763            },
 764        );
 765
 766        Ok(())
 767    }
 768
 769    pub fn paint_image(
 770        &mut self,
 771        bounds: Bounds<Pixels>,
 772        corner_radii: Corners<Pixels>,
 773        data: Arc<ImageData>,
 774        grayscale: bool,
 775    ) -> Result<()> {
 776        let scale_factor = self.scale_factor();
 777        let bounds = bounds.scale(scale_factor);
 778        let params = RenderImageParams { image_id: data.id };
 779
 780        let tile = self
 781            .window
 782            .sprite_atlas
 783            .get_or_insert_with(&params.clone().into(), &mut || {
 784                Ok((data.size(), Cow::Borrowed(data.as_bytes())))
 785            })?;
 786        let content_mask = self.content_mask().scale(scale_factor);
 787        let corner_radii = corner_radii.scale(scale_factor);
 788
 789        let window = &mut *self.window;
 790        window.scene_builder.insert(
 791            &window.z_index_stack,
 792            PolychromeSprite {
 793                order: 0,
 794                bounds,
 795                content_mask,
 796                corner_radii,
 797                tile,
 798                grayscale,
 799            },
 800        );
 801        Ok(())
 802    }
 803
 804    pub(crate) fn draw(&mut self) {
 805        let unit_entity = self.unit_entity.clone();
 806        self.update_entity(&unit_entity, |view, cx| {
 807            cx.start_frame();
 808
 809            let mut root_view = cx.window.root_view.take().unwrap();
 810
 811            cx.stack(0, |cx| {
 812                let available_space = cx.window.content_size.map(Into::into);
 813                draw_any_view(&mut root_view, available_space, cx);
 814            });
 815
 816            if let Some(mut active_drag) = cx.active_drag.take() {
 817                cx.stack(1, |cx| {
 818                    let offset = cx.mouse_position() - active_drag.cursor_offset;
 819                    cx.with_element_offset(Some(offset), |cx| {
 820                        let available_space =
 821                            size(AvailableSpace::MinContent, AvailableSpace::MinContent);
 822                        if let Some(drag_handle_view) = &mut active_drag.drag_handle_view {
 823                            draw_any_view(drag_handle_view, available_space, cx);
 824                        }
 825                        cx.active_drag = Some(active_drag);
 826                    });
 827                });
 828            }
 829
 830            cx.window.root_view = Some(root_view);
 831            let scene = cx.window.scene_builder.build();
 832
 833            cx.run_on_main(view, |_, cx| {
 834                cx.window
 835                    .platform_window
 836                    .borrow_on_main_thread()
 837                    .draw(scene);
 838                cx.window.dirty = false;
 839            })
 840            .detach();
 841        });
 842
 843        fn draw_any_view(
 844            view: &mut AnyView,
 845            available_space: Size<AvailableSpace>,
 846            cx: &mut ViewContext<()>,
 847        ) {
 848            cx.with_optional_element_state(view.id(), |element_state, cx| {
 849                let mut element_state = view.initialize(&mut (), element_state, cx);
 850                let layout_id = view.layout(&mut (), &mut element_state, cx);
 851                cx.window
 852                    .layout_engine
 853                    .compute_layout(layout_id, available_space);
 854                let bounds = cx.window.layout_engine.layout_bounds(layout_id);
 855                view.paint(bounds, &mut (), &mut element_state, cx);
 856                ((), element_state)
 857            });
 858        }
 859    }
 860
 861    fn start_frame(&mut self) {
 862        self.text_system().start_frame();
 863
 864        let window = &mut *self.window;
 865
 866        // Move the current frame element states to the previous frame.
 867        // The new empty element states map will be populated for any element states we
 868        // reference during the upcoming frame.
 869        mem::swap(
 870            &mut window.element_states,
 871            &mut window.prev_frame_element_states,
 872        );
 873        window.element_states.clear();
 874
 875        // Make the current key matchers the previous, and then clear the current.
 876        // An empty key matcher map will be created for every identified element in the
 877        // upcoming frame.
 878        mem::swap(
 879            &mut window.key_matchers,
 880            &mut window.prev_frame_key_matchers,
 881        );
 882        window.key_matchers.clear();
 883
 884        // Clear mouse event listeners, because elements add new element listeners
 885        // when the upcoming frame is painted.
 886        window.mouse_listeners.values_mut().for_each(Vec::clear);
 887
 888        // Clear focus state, because we determine what is focused when the new elements
 889        // in the upcoming frame are initialized.
 890        window.focus_listeners.clear();
 891        window.key_dispatch_stack.clear();
 892        window.focus_parents_by_child.clear();
 893        window.freeze_key_dispatch_stack = false;
 894    }
 895
 896    fn dispatch_event(&mut self, event: InputEvent) -> bool {
 897        let event = match event {
 898            InputEvent::MouseMove(mouse_move) => {
 899                self.window.mouse_position = mouse_move.position;
 900                InputEvent::MouseMove(mouse_move)
 901            }
 902            InputEvent::FileDrop(file_drop) => match file_drop {
 903                FileDropEvent::Entered { position, files } => {
 904                    self.active_drag.get_or_insert_with(|| AnyDrag {
 905                        drag_handle_view: None,
 906                        cursor_offset: position,
 907                        state: Box::new(files),
 908                        state_type: TypeId::of::<ExternalPaths>(),
 909                    });
 910                    InputEvent::MouseDown(MouseDownEvent {
 911                        position,
 912                        button: MouseButton::Left,
 913                        click_count: 1,
 914                        modifiers: Modifiers::default(),
 915                    })
 916                }
 917                FileDropEvent::Pending { position } => InputEvent::MouseMove(MouseMoveEvent {
 918                    position,
 919                    pressed_button: Some(MouseButton::Left),
 920                    modifiers: Modifiers::default(),
 921                }),
 922                FileDropEvent::Submit { position } => InputEvent::MouseUp(MouseUpEvent {
 923                    button: MouseButton::Left,
 924                    position,
 925                    modifiers: Modifiers::default(),
 926                    click_count: 1,
 927                }),
 928                FileDropEvent::Exited => InputEvent::MouseUp(MouseUpEvent {
 929                    button: MouseButton::Left,
 930                    position: Point::default(),
 931                    modifiers: Modifiers::default(),
 932                    click_count: 1,
 933                }),
 934            },
 935            _ => event,
 936        };
 937
 938        if let Some(any_mouse_event) = event.mouse_event() {
 939            // Handlers may set this to false by calling `stop_propagation`
 940            self.app.propagate_event = true;
 941            self.window.default_prevented = false;
 942
 943            if let Some(mut handlers) = self
 944                .window
 945                .mouse_listeners
 946                .remove(&any_mouse_event.type_id())
 947            {
 948                // Because handlers may add other handlers, we sort every time.
 949                handlers.sort_by(|(a, _), (b, _)| a.cmp(b));
 950
 951                // Capture phase, events bubble from back to front. Handlers for this phase are used for
 952                // special purposes, such as detecting events outside of a given Bounds.
 953                for (_, handler) in &handlers {
 954                    handler(any_mouse_event, DispatchPhase::Capture, self);
 955                    if !self.app.propagate_event {
 956                        break;
 957                    }
 958                }
 959
 960                // Bubble phase, where most normal handlers do their work.
 961                if self.app.propagate_event {
 962                    for (_, handler) in handlers.iter().rev() {
 963                        handler(any_mouse_event, DispatchPhase::Bubble, self);
 964                        if !self.app.propagate_event {
 965                            break;
 966                        }
 967                    }
 968                }
 969
 970                if self.app.propagate_event
 971                    && any_mouse_event.downcast_ref::<MouseUpEvent>().is_some()
 972                {
 973                    self.active_drag = None;
 974                }
 975
 976                // Just in case any handlers added new handlers, which is weird, but possible.
 977                handlers.extend(
 978                    self.window
 979                        .mouse_listeners
 980                        .get_mut(&any_mouse_event.type_id())
 981                        .into_iter()
 982                        .flat_map(|handlers| handlers.drain(..)),
 983                );
 984                self.window
 985                    .mouse_listeners
 986                    .insert(any_mouse_event.type_id(), handlers);
 987            }
 988        } else if let Some(any_key_event) = event.keyboard_event() {
 989            let key_dispatch_stack = mem::take(&mut self.window.key_dispatch_stack);
 990            let key_event_type = any_key_event.type_id();
 991            let mut context_stack = SmallVec::<[&DispatchContext; 16]>::new();
 992
 993            for (ix, frame) in key_dispatch_stack.iter().enumerate() {
 994                match frame {
 995                    KeyDispatchStackFrame::Listener {
 996                        event_type,
 997                        listener,
 998                    } => {
 999                        if key_event_type == *event_type {
1000                            if let Some(action) = listener(
1001                                any_key_event,
1002                                &context_stack,
1003                                DispatchPhase::Capture,
1004                                self,
1005                            ) {
1006                                self.dispatch_action(action, &key_dispatch_stack[..ix]);
1007                            }
1008                            if !self.app.propagate_event {
1009                                break;
1010                            }
1011                        }
1012                    }
1013                    KeyDispatchStackFrame::Context(context) => {
1014                        context_stack.push(&context);
1015                    }
1016                }
1017            }
1018
1019            if self.app.propagate_event {
1020                for (ix, frame) in key_dispatch_stack.iter().enumerate().rev() {
1021                    match frame {
1022                        KeyDispatchStackFrame::Listener {
1023                            event_type,
1024                            listener,
1025                        } => {
1026                            if key_event_type == *event_type {
1027                                if let Some(action) = listener(
1028                                    any_key_event,
1029                                    &context_stack,
1030                                    DispatchPhase::Bubble,
1031                                    self,
1032                                ) {
1033                                    self.dispatch_action(action, &key_dispatch_stack[..ix]);
1034                                }
1035
1036                                if !self.app.propagate_event {
1037                                    break;
1038                                }
1039                            }
1040                        }
1041                        KeyDispatchStackFrame::Context(_) => {
1042                            context_stack.pop();
1043                        }
1044                    }
1045                }
1046            }
1047
1048            drop(context_stack);
1049            self.window.key_dispatch_stack = key_dispatch_stack;
1050        }
1051
1052        true
1053    }
1054
1055    pub fn match_keystroke(
1056        &mut self,
1057        element_id: &GlobalElementId,
1058        keystroke: &Keystroke,
1059        context_stack: &[&DispatchContext],
1060    ) -> KeyMatch {
1061        let key_match = self
1062            .window
1063            .key_matchers
1064            .get_mut(element_id)
1065            .unwrap()
1066            .match_keystroke(keystroke, context_stack);
1067
1068        if key_match.is_some() {
1069            for matcher in self.window.key_matchers.values_mut() {
1070                matcher.clear_pending();
1071            }
1072        }
1073
1074        key_match
1075    }
1076
1077    pub fn observe_global<G: 'static>(
1078        &mut self,
1079        f: impl Fn(&mut WindowContext<'_, '_>) + Send + Sync + 'static,
1080    ) -> Subscription {
1081        let window_handle = self.window.handle;
1082        self.global_observers.insert(
1083            TypeId::of::<G>(),
1084            Box::new(move |cx| cx.update_window(window_handle, |cx| f(cx)).is_ok()),
1085        )
1086    }
1087
1088    fn dispatch_action(
1089        &mut self,
1090        action: Box<dyn Action>,
1091        dispatch_stack: &[KeyDispatchStackFrame],
1092    ) {
1093        let action_type = action.as_any().type_id();
1094
1095        if let Some(mut global_listeners) = self.app.global_action_listeners.remove(&action_type) {
1096            for listener in &global_listeners {
1097                listener(action.as_ref(), DispatchPhase::Capture, self);
1098                if !self.app.propagate_event {
1099                    break;
1100                }
1101            }
1102            global_listeners.extend(
1103                self.global_action_listeners
1104                    .remove(&action_type)
1105                    .unwrap_or_default(),
1106            );
1107            self.global_action_listeners
1108                .insert(action_type, global_listeners);
1109        }
1110
1111        if self.app.propagate_event {
1112            for stack_frame in dispatch_stack {
1113                if let KeyDispatchStackFrame::Listener {
1114                    event_type,
1115                    listener,
1116                } = stack_frame
1117                {
1118                    if action_type == *event_type {
1119                        listener(action.as_any(), &[], DispatchPhase::Capture, self);
1120                        if !self.app.propagate_event {
1121                            break;
1122                        }
1123                    }
1124                }
1125            }
1126        }
1127
1128        if self.app.propagate_event {
1129            for stack_frame in dispatch_stack.iter().rev() {
1130                if let KeyDispatchStackFrame::Listener {
1131                    event_type,
1132                    listener,
1133                } = stack_frame
1134                {
1135                    if action_type == *event_type {
1136                        listener(action.as_any(), &[], DispatchPhase::Bubble, self);
1137                        if !self.app.propagate_event {
1138                            break;
1139                        }
1140                    }
1141                }
1142            }
1143        }
1144
1145        if self.app.propagate_event {
1146            if let Some(mut global_listeners) =
1147                self.app.global_action_listeners.remove(&action_type)
1148            {
1149                for listener in global_listeners.iter().rev() {
1150                    listener(action.as_ref(), DispatchPhase::Bubble, self);
1151                    if !self.app.propagate_event {
1152                        break;
1153                    }
1154                }
1155                global_listeners.extend(
1156                    self.global_action_listeners
1157                        .remove(&action_type)
1158                        .unwrap_or_default(),
1159                );
1160                self.global_action_listeners
1161                    .insert(action_type, global_listeners);
1162            }
1163        }
1164    }
1165}
1166
1167impl<'a, 'w> MainThread<WindowContext<'a, 'w>> {
1168    fn platform_window(&self) -> &dyn PlatformWindow {
1169        self.window.platform_window.borrow_on_main_thread().as_ref()
1170    }
1171
1172    pub fn activate_window(&self) {
1173        self.platform_window().activate();
1174    }
1175}
1176
1177impl Context for WindowContext<'_, '_> {
1178    type EntityContext<'a, 'w, T> = ViewContext<'a, 'w, T>;
1179    type Result<T> = T;
1180
1181    fn entity<T>(
1182        &mut self,
1183        build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T,
1184    ) -> Handle<T>
1185    where
1186        T: Any + Send + Sync,
1187    {
1188        let slot = self.app.entities.reserve();
1189        let entity = build_entity(&mut ViewContext::mutable(
1190            &mut *self.app,
1191            &mut self.window,
1192            slot.downgrade(),
1193        ));
1194        self.entities.insert(slot, entity)
1195    }
1196
1197    fn update_entity<T: 'static, R>(
1198        &mut self,
1199        handle: &Handle<T>,
1200        update: impl FnOnce(&mut T, &mut Self::EntityContext<'_, '_, T>) -> R,
1201    ) -> R {
1202        let mut entity = self.entities.lease(handle);
1203        let result = update(
1204            &mut *entity,
1205            &mut ViewContext::mutable(&mut *self.app, &mut *self.window, handle.downgrade()),
1206        );
1207        self.entities.end_lease(entity);
1208        result
1209    }
1210}
1211
1212impl<'a, 'w> std::ops::Deref for WindowContext<'a, 'w> {
1213    type Target = AppContext;
1214
1215    fn deref(&self) -> &Self::Target {
1216        &self.app
1217    }
1218}
1219
1220impl<'a, 'w> std::ops::DerefMut for WindowContext<'a, 'w> {
1221    fn deref_mut(&mut self) -> &mut Self::Target {
1222        &mut self.app
1223    }
1224}
1225
1226impl<'a, 'w> Borrow<AppContext> for WindowContext<'a, 'w> {
1227    fn borrow(&self) -> &AppContext {
1228        &self.app
1229    }
1230}
1231
1232impl<'a, 'w> BorrowMut<AppContext> for WindowContext<'a, 'w> {
1233    fn borrow_mut(&mut self) -> &mut AppContext {
1234        &mut self.app
1235    }
1236}
1237
1238pub trait BorrowWindow: BorrowMut<Window> + BorrowMut<AppContext> {
1239    fn app_mut(&mut self) -> &mut AppContext {
1240        self.borrow_mut()
1241    }
1242
1243    fn window(&self) -> &Window {
1244        self.borrow()
1245    }
1246
1247    fn window_mut(&mut self) -> &mut Window {
1248        self.borrow_mut()
1249    }
1250
1251    fn with_element_id<R>(
1252        &mut self,
1253        id: impl Into<ElementId>,
1254        f: impl FnOnce(GlobalElementId, &mut Self) -> R,
1255    ) -> R {
1256        let keymap = self.app_mut().keymap.clone();
1257        let window = self.window_mut();
1258        window.element_id_stack.push(id.into());
1259        let global_id = window.element_id_stack.clone();
1260
1261        if window.key_matchers.get(&global_id).is_none() {
1262            window.key_matchers.insert(
1263                global_id.clone(),
1264                window
1265                    .prev_frame_key_matchers
1266                    .remove(&global_id)
1267                    .unwrap_or_else(|| KeyMatcher::new(keymap)),
1268            );
1269        }
1270
1271        let result = f(global_id, self);
1272        let window: &mut Window = self.borrow_mut();
1273        window.element_id_stack.pop();
1274        result
1275    }
1276
1277    fn with_content_mask<R>(
1278        &mut self,
1279        mask: ContentMask<Pixels>,
1280        f: impl FnOnce(&mut Self) -> R,
1281    ) -> R {
1282        let mask = mask.intersect(&self.content_mask());
1283        self.window_mut().content_mask_stack.push(mask);
1284        let result = f(self);
1285        self.window_mut().content_mask_stack.pop();
1286        result
1287    }
1288
1289    fn with_element_offset<R>(
1290        &mut self,
1291        offset: Option<Point<Pixels>>,
1292        f: impl FnOnce(&mut Self) -> R,
1293    ) -> R {
1294        let Some(offset) = offset else {
1295            return f(self);
1296        };
1297
1298        let offset = self.element_offset() + offset;
1299        self.window_mut().element_offset_stack.push(offset);
1300        let result = f(self);
1301        self.window_mut().element_offset_stack.pop();
1302        result
1303    }
1304
1305    fn element_offset(&self) -> Point<Pixels> {
1306        self.window()
1307            .element_offset_stack
1308            .last()
1309            .copied()
1310            .unwrap_or_default()
1311    }
1312
1313    fn with_element_state<S, R>(
1314        &mut self,
1315        id: ElementId,
1316        f: impl FnOnce(Option<S>, &mut Self) -> (R, S),
1317    ) -> R
1318    where
1319        S: Any + Send + Sync,
1320    {
1321        self.with_element_id(id, |global_id, cx| {
1322            if let Some(any) = cx
1323                .window_mut()
1324                .element_states
1325                .remove(&global_id)
1326                .or_else(|| cx.window_mut().prev_frame_element_states.remove(&global_id))
1327            {
1328                // Using the extra inner option to avoid needing to reallocate a new box.
1329                let mut state_box = any
1330                    .downcast::<Option<S>>()
1331                    .expect("invalid element state type for id");
1332                let state = state_box
1333                    .take()
1334                    .expect("element state is already on the stack");
1335                let (result, state) = f(Some(state), cx);
1336                state_box.replace(state);
1337                cx.window_mut().element_states.insert(global_id, state_box);
1338                result
1339            } else {
1340                let (result, state) = f(None, cx);
1341                cx.window_mut()
1342                    .element_states
1343                    .insert(global_id, Box::new(Some(state)));
1344                result
1345            }
1346        })
1347    }
1348
1349    fn with_optional_element_state<S, R>(
1350        &mut self,
1351        element_id: Option<ElementId>,
1352        f: impl FnOnce(Option<S>, &mut Self) -> (R, S),
1353    ) -> R
1354    where
1355        S: Any + Send + Sync,
1356    {
1357        if let Some(element_id) = element_id {
1358            self.with_element_state(element_id, f)
1359        } else {
1360            f(None, self).0
1361        }
1362    }
1363
1364    fn content_mask(&self) -> ContentMask<Pixels> {
1365        self.window()
1366            .content_mask_stack
1367            .last()
1368            .cloned()
1369            .unwrap_or_else(|| ContentMask {
1370                bounds: Bounds {
1371                    origin: Point::default(),
1372                    size: self.window().content_size,
1373                },
1374            })
1375    }
1376
1377    fn rem_size(&self) -> Pixels {
1378        self.window().rem_size
1379    }
1380}
1381
1382impl Borrow<Window> for WindowContext<'_, '_> {
1383    fn borrow(&self) -> &Window {
1384        &self.window
1385    }
1386}
1387
1388impl BorrowMut<Window> for WindowContext<'_, '_> {
1389    fn borrow_mut(&mut self) -> &mut Window {
1390        &mut self.window
1391    }
1392}
1393
1394impl<T> BorrowWindow for T where T: BorrowMut<AppContext> + BorrowMut<Window> {}
1395
1396pub struct ViewContext<'a, 'w, V> {
1397    window_cx: WindowContext<'a, 'w>,
1398    view_state: WeakHandle<V>,
1399}
1400
1401impl<V> Borrow<AppContext> for ViewContext<'_, '_, V> {
1402    fn borrow(&self) -> &AppContext {
1403        &*self.window_cx.app
1404    }
1405}
1406
1407impl<V> BorrowMut<AppContext> for ViewContext<'_, '_, V> {
1408    fn borrow_mut(&mut self) -> &mut AppContext {
1409        &mut *self.window_cx.app
1410    }
1411}
1412
1413impl<V> Borrow<Window> for ViewContext<'_, '_, V> {
1414    fn borrow(&self) -> &Window {
1415        &*self.window_cx.window
1416    }
1417}
1418
1419impl<V> BorrowMut<Window> for ViewContext<'_, '_, V> {
1420    fn borrow_mut(&mut self) -> &mut Window {
1421        &mut *self.window_cx.window
1422    }
1423}
1424
1425impl<'a, 'w, V: 'static> ViewContext<'a, 'w, V> {
1426    fn mutable(app: &'a mut AppContext, window: &'w mut Window, view_state: WeakHandle<V>) -> Self {
1427        Self {
1428            window_cx: WindowContext::mutable(app, window),
1429            view_state,
1430        }
1431    }
1432
1433    pub fn handle(&self) -> WeakHandle<V> {
1434        self.view_state.clone()
1435    }
1436
1437    pub fn stack<R>(&mut self, order: u32, f: impl FnOnce(&mut Self) -> R) -> R {
1438        self.window.z_index_stack.push(order);
1439        let result = f(self);
1440        self.window.z_index_stack.pop();
1441        result
1442    }
1443
1444    pub fn on_next_frame(&mut self, f: impl FnOnce(&mut V, &mut ViewContext<V>) + Send + 'static)
1445    where
1446        V: Any + Send + Sync,
1447    {
1448        let entity = self.handle();
1449        self.window_cx.on_next_frame(move |cx| {
1450            entity.update(cx, f).ok();
1451        });
1452    }
1453
1454    pub fn observe<E>(
1455        &mut self,
1456        handle: &Handle<E>,
1457        mut on_notify: impl FnMut(&mut V, Handle<E>, &mut ViewContext<'_, '_, V>)
1458            + Send
1459            + Sync
1460            + 'static,
1461    ) -> Subscription
1462    where
1463        E: 'static,
1464        V: Any + Send + Sync,
1465    {
1466        let this = self.handle();
1467        let handle = handle.downgrade();
1468        let window_handle = self.window.handle;
1469        self.app.observers.insert(
1470            handle.entity_id,
1471            Box::new(move |cx| {
1472                cx.update_window(window_handle, |cx| {
1473                    if let Some(handle) = handle.upgrade() {
1474                        this.update(cx, |this, cx| on_notify(this, handle, cx))
1475                            .is_ok()
1476                    } else {
1477                        false
1478                    }
1479                })
1480                .unwrap_or(false)
1481            }),
1482        )
1483    }
1484
1485    pub fn subscribe<E: EventEmitter>(
1486        &mut self,
1487        handle: &Handle<E>,
1488        mut on_event: impl FnMut(&mut V, Handle<E>, &E::Event, &mut ViewContext<'_, '_, V>)
1489            + Send
1490            + Sync
1491            + 'static,
1492    ) -> Subscription {
1493        let this = self.handle();
1494        let handle = handle.downgrade();
1495        let window_handle = self.window.handle;
1496        self.app.event_listeners.insert(
1497            handle.entity_id,
1498            Box::new(move |event, cx| {
1499                cx.update_window(window_handle, |cx| {
1500                    if let Some(handle) = handle.upgrade() {
1501                        let event = event.downcast_ref().expect("invalid event type");
1502                        this.update(cx, |this, cx| on_event(this, handle, event, cx))
1503                            .is_ok()
1504                    } else {
1505                        false
1506                    }
1507                })
1508                .unwrap_or(false)
1509            }),
1510        )
1511    }
1512
1513    pub fn on_release(
1514        &mut self,
1515        mut on_release: impl FnMut(&mut V, &mut WindowContext) + Send + Sync + 'static,
1516    ) -> Subscription {
1517        let window_handle = self.window.handle;
1518        self.app.release_listeners.insert(
1519            self.view_state.entity_id,
1520            Box::new(move |this, cx| {
1521                let this = this.downcast_mut().expect("invalid entity type");
1522                // todo!("are we okay with silently swallowing the error?")
1523                let _ = cx.update_window(window_handle, |cx| on_release(this, cx));
1524            }),
1525        )
1526    }
1527
1528    pub fn observe_release<T: 'static>(
1529        &mut self,
1530        handle: &Handle<T>,
1531        mut on_release: impl FnMut(&mut V, &mut T, &mut ViewContext<'_, '_, V>) + Send + Sync + 'static,
1532    ) -> Subscription {
1533        let this = self.handle();
1534        let window_handle = self.window.handle;
1535        self.app.observe_release(handle, move |entity, cx| {
1536            let _ = cx.update_window(window_handle, |cx| {
1537                this.update(cx, |this, cx| on_release(this, entity, cx))
1538            });
1539        })
1540    }
1541
1542    pub fn notify(&mut self) {
1543        self.window_cx.notify();
1544        self.window_cx.app.push_effect(Effect::Notify {
1545            emitter: self.view_state.entity_id,
1546        });
1547    }
1548
1549    pub fn on_focus_changed(
1550        &mut self,
1551        listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + Send + Sync + 'static,
1552    ) {
1553        let handle = self.handle();
1554        self.window.focus_listeners.push(Arc::new(move |event, cx| {
1555            handle
1556                .update(cx, |view, cx| listener(view, event, cx))
1557                .log_err();
1558        }));
1559    }
1560
1561    pub fn with_key_listeners<R>(
1562        &mut self,
1563        key_listeners: &[(TypeId, KeyListener<V>)],
1564        f: impl FnOnce(&mut Self) -> R,
1565    ) -> R {
1566        if !self.window.freeze_key_dispatch_stack {
1567            for (event_type, listener) in key_listeners.iter().cloned() {
1568                let handle = self.handle();
1569                let listener = Arc::new(
1570                    move |event: &dyn Any,
1571                          context_stack: &[&DispatchContext],
1572                          phase: DispatchPhase,
1573                          cx: &mut WindowContext<'_, '_>| {
1574                        handle
1575                            .update(cx, |view, cx| {
1576                                listener(view, event, context_stack, phase, cx)
1577                            })
1578                            .log_err()
1579                            .flatten()
1580                    },
1581                );
1582                self.window
1583                    .key_dispatch_stack
1584                    .push(KeyDispatchStackFrame::Listener {
1585                        event_type,
1586                        listener,
1587                    });
1588            }
1589        }
1590
1591        let result = f(self);
1592
1593        if !self.window.freeze_key_dispatch_stack {
1594            let prev_len = self.window.key_dispatch_stack.len() - key_listeners.len();
1595            self.window.key_dispatch_stack.truncate(prev_len);
1596        }
1597
1598        result
1599    }
1600
1601    pub fn with_key_dispatch_context<R>(
1602        &mut self,
1603        context: DispatchContext,
1604        f: impl FnOnce(&mut Self) -> R,
1605    ) -> R {
1606        if context.is_empty() {
1607            return f(self);
1608        }
1609
1610        if !self.window.freeze_key_dispatch_stack {
1611            self.window
1612                .key_dispatch_stack
1613                .push(KeyDispatchStackFrame::Context(context));
1614        }
1615
1616        let result = f(self);
1617
1618        if !self.window.freeze_key_dispatch_stack {
1619            self.window.key_dispatch_stack.pop();
1620        }
1621
1622        result
1623    }
1624
1625    pub fn with_focus<R>(
1626        &mut self,
1627        focus_handle: FocusHandle,
1628        f: impl FnOnce(&mut Self) -> R,
1629    ) -> R {
1630        if let Some(parent_focus_id) = self.window.focus_stack.last().copied() {
1631            self.window
1632                .focus_parents_by_child
1633                .insert(focus_handle.id, parent_focus_id);
1634        }
1635        self.window.focus_stack.push(focus_handle.id);
1636
1637        if Some(focus_handle.id) == self.window.focus {
1638            self.window.freeze_key_dispatch_stack = true;
1639        }
1640
1641        let result = f(self);
1642
1643        self.window.focus_stack.pop();
1644        result
1645    }
1646
1647    pub fn run_on_main<R>(
1648        &mut self,
1649        view: &mut V,
1650        f: impl FnOnce(&mut V, &mut MainThread<ViewContext<'_, '_, V>>) -> R + Send + 'static,
1651    ) -> Task<Result<R>>
1652    where
1653        R: Send + 'static,
1654    {
1655        if self.executor.is_main_thread() {
1656            let cx = unsafe { mem::transmute::<&mut Self, &mut MainThread<Self>>(self) };
1657            Task::ready(Ok(f(view, cx)))
1658        } else {
1659            let handle = self.handle().upgrade().unwrap();
1660            self.window_cx.run_on_main(move |cx| handle.update(cx, f))
1661        }
1662    }
1663
1664    pub fn spawn<Fut, R>(
1665        &mut self,
1666        f: impl FnOnce(WeakHandle<V>, AsyncWindowContext) -> Fut + Send + 'static,
1667    ) -> Task<R>
1668    where
1669        R: Send + 'static,
1670        Fut: Future<Output = R> + Send + 'static,
1671    {
1672        let handle = self.handle();
1673        self.window_cx.spawn(move |_, cx| {
1674            let result = f(handle, cx);
1675            async move { result.await }
1676        })
1677    }
1678
1679    pub fn update_global<G, R>(&mut self, f: impl FnOnce(&mut G, &mut Self) -> R) -> R
1680    where
1681        G: 'static + Send + Sync,
1682    {
1683        let mut global = self.app.lease_global::<G>();
1684        let result = f(&mut global, self);
1685        self.app.end_global_lease(global);
1686        result
1687    }
1688
1689    pub fn observe_global<G: 'static>(
1690        &mut self,
1691        f: impl Fn(&mut V, &mut ViewContext<'_, '_, V>) + Send + Sync + 'static,
1692    ) -> Subscription {
1693        let window_handle = self.window.handle;
1694        let handle = self.handle();
1695        self.global_observers.insert(
1696            TypeId::of::<G>(),
1697            Box::new(move |cx| {
1698                cx.update_window(window_handle, |cx| {
1699                    handle.update(cx, |view, cx| f(view, cx)).is_ok()
1700                })
1701                .unwrap_or(false)
1702            }),
1703        )
1704    }
1705
1706    pub fn on_mouse_event<Event: 'static>(
1707        &mut self,
1708        handler: impl Fn(&mut V, &Event, DispatchPhase, &mut ViewContext<V>) + Send + Sync + 'static,
1709    ) {
1710        let handle = self.handle().upgrade().unwrap();
1711        self.window_cx.on_mouse_event(move |event, phase, cx| {
1712            handle.update(cx, |view, cx| {
1713                handler(view, event, phase, cx);
1714            })
1715        });
1716    }
1717}
1718
1719impl<'a, 'w, V> ViewContext<'a, 'w, V>
1720where
1721    V: EventEmitter,
1722    V::Event: Any + Send + Sync,
1723{
1724    pub fn emit(&mut self, event: V::Event) {
1725        let emitter = self.view_state.entity_id;
1726        self.app.push_effect(Effect::Emit {
1727            emitter,
1728            event: Box::new(event),
1729        });
1730    }
1731}
1732
1733impl<'a, 'w, V> Context for ViewContext<'a, 'w, V> {
1734    type EntityContext<'b, 'c, U> = ViewContext<'b, 'c, U>;
1735    type Result<U> = U;
1736
1737    fn entity<T>(
1738        &mut self,
1739        build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T,
1740    ) -> Handle<T>
1741    where
1742        T: 'static + Send + Sync,
1743    {
1744        self.window_cx.entity(build_entity)
1745    }
1746
1747    fn update_entity<T: 'static, R>(
1748        &mut self,
1749        handle: &Handle<T>,
1750        update: impl FnOnce(&mut T, &mut Self::EntityContext<'_, '_, T>) -> R,
1751    ) -> R {
1752        self.window_cx.update_entity(handle, update)
1753    }
1754}
1755
1756impl<'a, 'w, V> std::ops::Deref for ViewContext<'a, 'w, V> {
1757    type Target = WindowContext<'a, 'w>;
1758
1759    fn deref(&self) -> &Self::Target {
1760        &self.window_cx
1761    }
1762}
1763
1764impl<'a, 'w, V> std::ops::DerefMut for ViewContext<'a, 'w, V> {
1765    fn deref_mut(&mut self) -> &mut Self::Target {
1766        &mut self.window_cx
1767    }
1768}
1769
1770// #[derive(Clone, Copy, Eq, PartialEq, Hash)]
1771slotmap::new_key_type! { pub struct WindowId; }
1772
1773impl WindowId {
1774    pub fn as_u64(&self) -> u64 {
1775        self.0.as_ffi()
1776    }
1777}
1778
1779#[derive(PartialEq, Eq)]
1780pub struct WindowHandle<V> {
1781    id: WindowId,
1782    state_type: PhantomData<V>,
1783}
1784
1785impl<S> Copy for WindowHandle<S> {}
1786
1787impl<S> Clone for WindowHandle<S> {
1788    fn clone(&self) -> Self {
1789        WindowHandle {
1790            id: self.id,
1791            state_type: PhantomData,
1792        }
1793    }
1794}
1795
1796impl<S> WindowHandle<S> {
1797    pub fn new(id: WindowId) -> Self {
1798        WindowHandle {
1799            id,
1800            state_type: PhantomData,
1801        }
1802    }
1803}
1804
1805impl<S: 'static> Into<AnyWindowHandle> for WindowHandle<S> {
1806    fn into(self) -> AnyWindowHandle {
1807        AnyWindowHandle {
1808            id: self.id,
1809            state_type: TypeId::of::<S>(),
1810        }
1811    }
1812}
1813
1814#[derive(Copy, Clone, PartialEq, Eq)]
1815pub struct AnyWindowHandle {
1816    pub(crate) id: WindowId,
1817    state_type: TypeId,
1818}
1819
1820impl AnyWindowHandle {
1821    pub fn window_id(&self) -> WindowId {
1822        self.id
1823    }
1824}
1825
1826#[cfg(any(test, feature = "test-support"))]
1827impl From<SmallVec<[u32; 16]>> for StackingOrder {
1828    fn from(small_vec: SmallVec<[u32; 16]>) -> Self {
1829        StackingOrder(small_vec)
1830    }
1831}
1832
1833#[derive(Clone, Debug, Eq, PartialEq, Hash)]
1834pub enum ElementId {
1835    View(EntityId),
1836    Number(usize),
1837    Name(SharedString),
1838    FocusHandle(FocusId),
1839}
1840
1841impl From<EntityId> for ElementId {
1842    fn from(id: EntityId) -> Self {
1843        ElementId::View(id)
1844    }
1845}
1846
1847impl From<usize> for ElementId {
1848    fn from(id: usize) -> Self {
1849        ElementId::Number(id)
1850    }
1851}
1852
1853impl From<i32> for ElementId {
1854    fn from(id: i32) -> Self {
1855        Self::Number(id as usize)
1856    }
1857}
1858
1859impl From<SharedString> for ElementId {
1860    fn from(name: SharedString) -> Self {
1861        ElementId::Name(name)
1862    }
1863}
1864
1865impl From<&'static str> for ElementId {
1866    fn from(name: &'static str) -> Self {
1867        ElementId::Name(name.into())
1868    }
1869}
1870
1871impl<'a> From<&'a FocusHandle> for ElementId {
1872    fn from(handle: &'a FocusHandle) -> Self {
1873        ElementId::FocusHandle(handle.id)
1874    }
1875}