window.rs

   1use crate::{
   2    elements::AnyRootElement,
   3    fonts::{TextStyle, TextStyleRefinement},
   4    geometry::{rect::RectF, Size},
   5    json::ToJson,
   6    keymap_matcher::{Binding, KeymapContext, Keystroke, MatchResult},
   7    platform::{
   8        self, Appearance, CursorStyle, Event, KeyDownEvent, KeyUpEvent, ModifiersChangedEvent,
   9        MouseButton, MouseMovedEvent, PromptLevel, WindowBounds,
  10    },
  11    scene::{
  12        CursorRegion, EventHandler, MouseClick, MouseClickOut, MouseDown, MouseDownOut, MouseDrag,
  13        MouseEvent, MouseHover, MouseMove, MouseMoveOut, MouseScrollWheel, MouseUp, MouseUpOut,
  14        Scene,
  15    },
  16    text_layout::TextLayoutCache,
  17    util::post_inc,
  18    Action, AnyView, AnyViewHandle, AnyWindowHandle, AppContext, BorrowAppContext,
  19    BorrowWindowContext, Effect, Element, Entity, Handle, MouseRegion, MouseRegionId, SceneBuilder,
  20    Subscription, View, ViewContext, ViewHandle, WindowInvalidation,
  21};
  22use anyhow::{anyhow, bail, Result};
  23use collections::{HashMap, HashSet};
  24use pathfinder_geometry::vector::{vec2f, Vector2F};
  25use postage::oneshot;
  26use serde_json::json;
  27use smallvec::SmallVec;
  28use sqlez::{
  29    bindable::{Bind, Column, StaticColumnCount},
  30    statement::Statement,
  31};
  32use std::{
  33    any::{type_name, Any, TypeId},
  34    mem,
  35    ops::{Deref, DerefMut, Range, Sub},
  36};
  37use taffy::{
  38    tree::{Measurable, MeasureFunc},
  39    Taffy,
  40};
  41use util::ResultExt;
  42use uuid::Uuid;
  43
  44use super::{Reference, ViewMetadata};
  45
  46pub struct Window {
  47    layout_engines: Vec<LayoutEngine>,
  48    pub(crate) root_view: Option<AnyViewHandle>,
  49    pub(crate) focused_view_id: Option<usize>,
  50    pub(crate) parents: HashMap<usize, usize>,
  51    pub(crate) is_active: bool,
  52    pub(crate) is_fullscreen: bool,
  53    inspector_enabled: bool,
  54    pub(crate) invalidation: Option<WindowInvalidation>,
  55    pub(crate) platform_window: Box<dyn platform::Window>,
  56    pub(crate) rendered_views: HashMap<usize, Box<dyn AnyRootElement>>,
  57    scene: SceneBuilder,
  58    pub(crate) text_style_stack: Vec<TextStyle>,
  59    pub(crate) theme_stack: Vec<Box<dyn Any>>,
  60    pub(crate) new_parents: HashMap<usize, usize>,
  61    pub(crate) views_to_notify_if_ancestors_change: HashMap<usize, SmallVec<[usize; 2]>>,
  62    titlebar_height: f32,
  63    appearance: Appearance,
  64    cursor_regions: Vec<CursorRegion>,
  65    mouse_regions: Vec<(MouseRegion, usize)>,
  66    event_handlers: Vec<EventHandler>,
  67    last_mouse_moved_event: Option<Event>,
  68    last_mouse_position: Vector2F,
  69    pressed_buttons: HashSet<MouseButton>,
  70    pub(crate) hovered_region_ids: Vec<MouseRegionId>,
  71    pub(crate) clicked_region_ids: Vec<MouseRegionId>,
  72    pub(crate) clicked_region: Option<(MouseRegionId, MouseButton)>,
  73    text_layout_cache: TextLayoutCache,
  74    refreshing: bool,
  75}
  76
  77impl Window {
  78    pub fn new<V, F>(
  79        handle: AnyWindowHandle,
  80        platform_window: Box<dyn platform::Window>,
  81        cx: &mut AppContext,
  82        build_view: F,
  83    ) -> Self
  84    where
  85        V: View,
  86        F: FnOnce(&mut ViewContext<V>) -> V,
  87    {
  88        let titlebar_height = platform_window.titlebar_height();
  89        let appearance = platform_window.appearance();
  90        let mut window = Self {
  91            layout_engines: Vec::new(),
  92            root_view: None,
  93            focused_view_id: None,
  94            parents: Default::default(),
  95            is_active: false,
  96            invalidation: None,
  97            is_fullscreen: false,
  98            inspector_enabled: false,
  99            platform_window,
 100            rendered_views: Default::default(),
 101            scene: SceneBuilder::new(),
 102            text_style_stack: Vec::new(),
 103            theme_stack: Vec::new(),
 104            new_parents: HashMap::default(),
 105            views_to_notify_if_ancestors_change: HashMap::default(),
 106            cursor_regions: Default::default(),
 107            mouse_regions: Default::default(),
 108            event_handlers: Default::default(),
 109            text_layout_cache: TextLayoutCache::new(cx.font_system.clone()),
 110            last_mouse_moved_event: None,
 111            last_mouse_position: Vector2F::zero(),
 112            pressed_buttons: Default::default(),
 113            hovered_region_ids: Default::default(),
 114            clicked_region_ids: Default::default(),
 115            clicked_region: None,
 116            titlebar_height,
 117            appearance,
 118            refreshing: false,
 119        };
 120
 121        let mut window_context = WindowContext::mutable(cx, &mut window, handle);
 122        let root_view = window_context.add_view(|cx| build_view(cx));
 123        if let Some(invalidation) = window_context.window.invalidation.take() {
 124            window_context.invalidate(invalidation, appearance);
 125        }
 126        window.focused_view_id = Some(root_view.id());
 127        window.root_view = Some(root_view.into_any());
 128        window
 129    }
 130
 131    pub fn root_view(&self) -> &AnyViewHandle {
 132        &self
 133            .root_view
 134            .as_ref()
 135            .expect("root_view called during window construction")
 136    }
 137
 138    pub fn take_event_handlers(&mut self) -> Vec<EventHandler> {
 139        mem::take(&mut self.event_handlers)
 140    }
 141}
 142
 143pub struct WindowContext<'a> {
 144    pub(crate) app_context: Reference<'a, AppContext>,
 145    pub(crate) window: Reference<'a, Window>,
 146    pub(crate) window_handle: AnyWindowHandle,
 147    pub(crate) removed: bool,
 148}
 149
 150impl Deref for WindowContext<'_> {
 151    type Target = AppContext;
 152
 153    fn deref(&self) -> &Self::Target {
 154        &self.app_context
 155    }
 156}
 157
 158impl DerefMut for WindowContext<'_> {
 159    fn deref_mut(&mut self) -> &mut Self::Target {
 160        &mut self.app_context
 161    }
 162}
 163
 164impl BorrowAppContext for WindowContext<'_> {
 165    fn read_with<T, F: FnOnce(&AppContext) -> T>(&self, f: F) -> T {
 166        self.app_context.read_with(f)
 167    }
 168
 169    fn update<T, F: FnOnce(&mut AppContext) -> T>(&mut self, f: F) -> T {
 170        self.app_context.update(f)
 171    }
 172}
 173
 174impl BorrowWindowContext for WindowContext<'_> {
 175    type Result<T> = T;
 176
 177    fn read_window<T, F: FnOnce(&WindowContext) -> T>(&self, handle: AnyWindowHandle, f: F) -> T {
 178        if self.window_handle == handle {
 179            f(self)
 180        } else {
 181            panic!("read_with called with id of window that does not belong to this context")
 182        }
 183    }
 184
 185    fn read_window_optional<T, F>(&self, window: AnyWindowHandle, f: F) -> Option<T>
 186    where
 187        F: FnOnce(&WindowContext) -> Option<T>,
 188    {
 189        BorrowWindowContext::read_window(self, window, f)
 190    }
 191
 192    fn update_window<T, F: FnOnce(&mut WindowContext) -> T>(
 193        &mut self,
 194        handle: AnyWindowHandle,
 195        f: F,
 196    ) -> T {
 197        if self.window_handle == handle {
 198            f(self)
 199        } else {
 200            panic!("update called with id of window that does not belong to this context")
 201        }
 202    }
 203
 204    fn update_window_optional<T, F>(&mut self, handle: AnyWindowHandle, f: F) -> Option<T>
 205    where
 206        F: FnOnce(&mut WindowContext) -> Option<T>,
 207    {
 208        BorrowWindowContext::update_window(self, handle, f)
 209    }
 210}
 211
 212impl<'a> WindowContext<'a> {
 213    pub fn mutable(
 214        app_context: &'a mut AppContext,
 215        window: &'a mut Window,
 216        handle: AnyWindowHandle,
 217    ) -> Self {
 218        Self {
 219            app_context: Reference::Mutable(app_context),
 220            window: Reference::Mutable(window),
 221            window_handle: handle,
 222            removed: false,
 223        }
 224    }
 225
 226    pub fn immutable(
 227        app_context: &'a AppContext,
 228        window: &'a Window,
 229        handle: AnyWindowHandle,
 230    ) -> Self {
 231        Self {
 232            app_context: Reference::Immutable(app_context),
 233            window: Reference::Immutable(window),
 234            window_handle: handle,
 235            removed: false,
 236        }
 237    }
 238
 239    pub fn repaint(&mut self) {
 240        let window = self.window();
 241        self.pending_effects
 242            .push_back(Effect::RepaintWindow { window });
 243    }
 244
 245    pub fn scene(&mut self) -> &mut SceneBuilder {
 246        &mut self.window.scene
 247    }
 248
 249    pub fn enable_inspector(&mut self) {
 250        self.window.inspector_enabled = true;
 251    }
 252
 253    pub fn is_inspector_enabled(&self) -> bool {
 254        self.window.inspector_enabled
 255    }
 256
 257    pub fn is_mouse_down(&self, button: MouseButton) -> bool {
 258        self.window.pressed_buttons.contains(&button)
 259    }
 260
 261    pub fn rem_size(&self) -> f32 {
 262        16.
 263    }
 264
 265    pub fn layout_engine(&mut self) -> Option<&mut LayoutEngine> {
 266        self.window.layout_engines.last_mut()
 267    }
 268
 269    pub fn push_layout_engine(&mut self, engine: LayoutEngine) {
 270        self.window.layout_engines.push(engine);
 271    }
 272
 273    pub fn pop_layout_engine(&mut self) -> Option<LayoutEngine> {
 274        self.window.layout_engines.pop()
 275    }
 276
 277    pub fn remove_window(&mut self) {
 278        self.removed = true;
 279    }
 280
 281    pub fn window(&self) -> AnyWindowHandle {
 282        self.window_handle
 283    }
 284
 285    pub fn app_context(&mut self) -> &mut AppContext {
 286        &mut self.app_context
 287    }
 288
 289    pub fn root_view(&self) -> &AnyViewHandle {
 290        self.window.root_view()
 291    }
 292
 293    pub fn window_size(&self) -> Vector2F {
 294        self.window.platform_window.content_size()
 295    }
 296
 297    pub fn mouse_position(&self) -> Vector2F {
 298        self.window.platform_window.mouse_position()
 299    }
 300
 301    pub fn refreshing(&self) -> bool {
 302        self.window.refreshing
 303    }
 304
 305    pub fn text_layout_cache(&self) -> &TextLayoutCache {
 306        &self.window.text_layout_cache
 307    }
 308
 309    pub(crate) fn update_any_view<F, T>(&mut self, view_id: usize, f: F) -> Option<T>
 310    where
 311        F: FnOnce(&mut dyn AnyView, &mut Self) -> T,
 312    {
 313        let handle = self.window_handle;
 314        let mut view = self.views.remove(&(handle, view_id))?;
 315        let result = f(view.as_mut(), self);
 316        self.views.insert((handle, view_id), view);
 317        Some(result)
 318    }
 319
 320    pub(crate) fn update_view<V: 'static, S>(
 321        &mut self,
 322        handle: &ViewHandle<V>,
 323        update: &mut dyn FnMut(&mut V, &mut ViewContext<V>) -> S,
 324    ) -> S {
 325        self.update_any_view(handle.view_id, |view, cx| {
 326            let mut cx = ViewContext::mutable(cx, handle.view_id);
 327            update(
 328                view.as_any_mut()
 329                    .downcast_mut()
 330                    .expect("downcast is type safe"),
 331                &mut cx,
 332            )
 333        })
 334        .expect("view is already on the stack")
 335    }
 336
 337    pub fn defer(&mut self, callback: impl 'static + FnOnce(&mut WindowContext)) {
 338        let handle = self.window_handle;
 339        self.app_context.defer(move |cx| {
 340            cx.update_window(handle, |cx| callback(cx));
 341        })
 342    }
 343
 344    pub fn update_global<T, F, U>(&mut self, update: F) -> U
 345    where
 346        T: 'static,
 347        F: FnOnce(&mut T, &mut Self) -> U,
 348    {
 349        AppContext::update_global_internal(self, |global, cx| update(global, cx))
 350    }
 351
 352    pub fn update_default_global<T, F, U>(&mut self, update: F) -> U
 353    where
 354        T: 'static + Default,
 355        F: FnOnce(&mut T, &mut Self) -> U,
 356    {
 357        AppContext::update_default_global_internal(self, |global, cx| update(global, cx))
 358    }
 359
 360    pub fn subscribe<E, H, F>(&mut self, handle: &H, mut callback: F) -> Subscription
 361    where
 362        E: Entity,
 363        E::Event: 'static,
 364        H: Handle<E>,
 365        F: 'static + FnMut(H, &E::Event, &mut WindowContext),
 366    {
 367        self.subscribe_internal(handle, move |emitter, event, cx| {
 368            callback(emitter, event, cx);
 369            true
 370        })
 371    }
 372
 373    pub fn subscribe_internal<E, H, F>(&mut self, handle: &H, mut callback: F) -> Subscription
 374    where
 375        E: Entity,
 376        E::Event: 'static,
 377        H: Handle<E>,
 378        F: 'static + FnMut(H, &E::Event, &mut WindowContext) -> bool,
 379    {
 380        let window_handle = self.window_handle;
 381        self.app_context
 382            .subscribe_internal(handle, move |emitter, event, cx| {
 383                cx.update_window(window_handle, |cx| callback(emitter, event, cx))
 384                    .unwrap_or(false)
 385            })
 386    }
 387
 388    pub(crate) fn observe_window_activation<F>(&mut self, callback: F) -> Subscription
 389    where
 390        F: 'static + FnMut(bool, &mut WindowContext) -> bool,
 391    {
 392        let handle = self.window_handle;
 393        let subscription_id = post_inc(&mut self.next_subscription_id);
 394        self.pending_effects
 395            .push_back(Effect::WindowActivationObservation {
 396                window: handle,
 397                subscription_id,
 398                callback: Box::new(callback),
 399            });
 400        Subscription::WindowActivationObservation(
 401            self.window_activation_observations
 402                .subscribe(handle, subscription_id),
 403        )
 404    }
 405
 406    pub(crate) fn observe_fullscreen<F>(&mut self, callback: F) -> Subscription
 407    where
 408        F: 'static + FnMut(bool, &mut WindowContext) -> bool,
 409    {
 410        let window = self.window_handle;
 411        let subscription_id = post_inc(&mut self.next_subscription_id);
 412        self.pending_effects
 413            .push_back(Effect::WindowFullscreenObservation {
 414                window,
 415                subscription_id,
 416                callback: Box::new(callback),
 417            });
 418        Subscription::WindowActivationObservation(
 419            self.window_activation_observations
 420                .subscribe(window, subscription_id),
 421        )
 422    }
 423
 424    pub(crate) fn observe_window_bounds<F>(&mut self, callback: F) -> Subscription
 425    where
 426        F: 'static + FnMut(WindowBounds, Uuid, &mut WindowContext) -> bool,
 427    {
 428        let window = self.window_handle;
 429        let subscription_id = post_inc(&mut self.next_subscription_id);
 430        self.pending_effects
 431            .push_back(Effect::WindowBoundsObservation {
 432                window,
 433                subscription_id,
 434                callback: Box::new(callback),
 435            });
 436        Subscription::WindowBoundsObservation(
 437            self.window_bounds_observations
 438                .subscribe(window, subscription_id),
 439        )
 440    }
 441
 442    pub fn observe_keystrokes<F>(&mut self, callback: F) -> Subscription
 443    where
 444        F: 'static
 445            + FnMut(&Keystroke, &MatchResult, Option<&Box<dyn Action>>, &mut WindowContext) -> bool,
 446    {
 447        let window = self.window_handle;
 448        let subscription_id = post_inc(&mut self.next_subscription_id);
 449        self.keystroke_observations
 450            .add_callback(window, subscription_id, Box::new(callback));
 451        Subscription::KeystrokeObservation(
 452            self.keystroke_observations
 453                .subscribe(window, subscription_id),
 454        )
 455    }
 456
 457    pub(crate) fn available_actions(
 458        &self,
 459        view_id: usize,
 460    ) -> Vec<(&'static str, Box<dyn Action>, SmallVec<[Binding; 1]>)> {
 461        let handle = self.window_handle;
 462        let mut contexts = Vec::new();
 463        let mut handler_depths_by_action_id = HashMap::<TypeId, usize>::default();
 464        for (depth, view_id) in self.ancestors(view_id).enumerate() {
 465            if let Some(view_metadata) = self.views_metadata.get(&(handle, view_id)) {
 466                contexts.push(view_metadata.keymap_context.clone());
 467                if let Some(actions) = self.actions.get(&view_metadata.type_id) {
 468                    handler_depths_by_action_id
 469                        .extend(actions.keys().copied().map(|action_id| (action_id, depth)));
 470                }
 471            } else {
 472                log::error!(
 473                    "view {} not found when computing available actions",
 474                    view_id
 475                );
 476            }
 477        }
 478
 479        handler_depths_by_action_id.extend(
 480            self.global_actions
 481                .keys()
 482                .copied()
 483                .map(|action_id| (action_id, contexts.len())),
 484        );
 485
 486        self.action_deserializers
 487            .iter()
 488            .filter_map(move |(name, (action_id, deserialize))| {
 489                if let Some(action_depth) = handler_depths_by_action_id.get(action_id).copied() {
 490                    let action = deserialize(serde_json::Value::Object(Default::default())).ok()?;
 491                    let bindings = self
 492                        .keystroke_matcher
 493                        .bindings_for_action(*action_id)
 494                        .filter(|b| {
 495                            action.eq(b.action())
 496                                && (0..=action_depth)
 497                                    .any(|depth| b.match_context(&contexts[depth..]))
 498                        })
 499                        .cloned()
 500                        .collect();
 501                    Some((*name, action, bindings))
 502                } else {
 503                    None
 504                }
 505            })
 506            .collect()
 507    }
 508
 509    pub(crate) fn dispatch_keystroke(&mut self, keystroke: &Keystroke) -> bool {
 510        let handle = self.window_handle;
 511        if let Some(focused_view_id) = self.focused_view_id() {
 512            let dispatch_path = self
 513                .ancestors(focused_view_id)
 514                .filter_map(|view_id| {
 515                    self.views_metadata
 516                        .get(&(handle, view_id))
 517                        .map(|view| (view_id, view.keymap_context.clone()))
 518                })
 519                .collect();
 520
 521            let match_result = self
 522                .keystroke_matcher
 523                .push_keystroke(keystroke.clone(), dispatch_path);
 524            let mut handled_by = None;
 525
 526            let keystroke_handled = match &match_result {
 527                MatchResult::None => false,
 528                MatchResult::Pending => true,
 529                MatchResult::Matches(matches) => {
 530                    for (view_id, action) in matches {
 531                        if self.dispatch_action(Some(*view_id), action.as_ref()) {
 532                            self.keystroke_matcher.clear_pending();
 533                            handled_by = Some(action.boxed_clone());
 534                            break;
 535                        }
 536                    }
 537                    handled_by.is_some()
 538                }
 539            };
 540
 541            self.keystroke(handle, keystroke.clone(), handled_by, match_result.clone());
 542            keystroke_handled
 543        } else {
 544            self.keystroke(handle, keystroke.clone(), None, MatchResult::None);
 545            false
 546        }
 547    }
 548
 549    pub(crate) fn dispatch_event(&mut self, event: Event, event_reused: bool) -> bool {
 550        if !event_reused {
 551            self.dispatch_event_2(&event);
 552        }
 553
 554        let mut mouse_events = SmallVec::<[_; 2]>::new();
 555        let mut notified_views: HashSet<usize> = Default::default();
 556        let handle = self.window_handle;
 557
 558        // 1. Handle platform event. Keyboard events get dispatched immediately, while mouse events
 559        //    get mapped into the mouse-specific MouseEvent type.
 560        //  -> These are usually small: [Mouse Down] or [Mouse up, Click] or [Mouse Moved, Mouse Dragged?]
 561        //  -> Also updates mouse-related state
 562        match &event {
 563            Event::KeyDown(e) => return self.dispatch_key_down(e),
 564
 565            Event::KeyUp(e) => return self.dispatch_key_up(e),
 566
 567            Event::ModifiersChanged(e) => return self.dispatch_modifiers_changed(e),
 568
 569            Event::MouseDown(e) => {
 570                // Click events are weird because they can be fired after a drag event.
 571                // MDN says that browsers handle this by starting from 'the most
 572                // specific ancestor element that contained both [positions]'
 573                // So we need to store the overlapping regions on mouse down.
 574
 575                // If there is already region being clicked, don't replace it.
 576                if self.window.clicked_region.is_none() {
 577                    self.window.clicked_region_ids = self
 578                        .window
 579                        .mouse_regions
 580                        .iter()
 581                        .filter_map(|(region, _)| {
 582                            if region.bounds.contains_point(e.position) {
 583                                Some(region.id())
 584                            } else {
 585                                None
 586                            }
 587                        })
 588                        .collect();
 589
 590                    let mut highest_z_index = 0;
 591                    let mut clicked_region_id = None;
 592                    for (region, z_index) in self.window.mouse_regions.iter() {
 593                        if region.bounds.contains_point(e.position) && *z_index >= highest_z_index {
 594                            highest_z_index = *z_index;
 595                            clicked_region_id = Some(region.id());
 596                        }
 597                    }
 598
 599                    self.window.clicked_region =
 600                        clicked_region_id.map(|region_id| (region_id, e.button));
 601                }
 602
 603                mouse_events.push(MouseEvent::Down(MouseDown {
 604                    region: Default::default(),
 605                    platform_event: e.clone(),
 606                }));
 607                mouse_events.push(MouseEvent::DownOut(MouseDownOut {
 608                    region: Default::default(),
 609                    platform_event: e.clone(),
 610                }));
 611            }
 612
 613            Event::MouseUp(e) => {
 614                mouse_events.push(MouseEvent::Up(MouseUp {
 615                    region: Default::default(),
 616                    platform_event: e.clone(),
 617                }));
 618
 619                // Synthesize one last drag event to end the drag
 620                mouse_events.push(MouseEvent::Drag(MouseDrag {
 621                    region: Default::default(),
 622                    prev_mouse_position: self.window.last_mouse_position,
 623                    platform_event: MouseMovedEvent {
 624                        position: e.position,
 625                        pressed_button: Some(e.button),
 626                        modifiers: e.modifiers,
 627                    },
 628                    end: true,
 629                }));
 630
 631                mouse_events.push(MouseEvent::UpOut(MouseUpOut {
 632                    region: Default::default(),
 633                    platform_event: e.clone(),
 634                }));
 635                mouse_events.push(MouseEvent::Click(MouseClick {
 636                    region: Default::default(),
 637                    platform_event: e.clone(),
 638                }));
 639                mouse_events.push(MouseEvent::ClickOut(MouseClickOut {
 640                    region: Default::default(),
 641                    platform_event: e.clone(),
 642                }));
 643            }
 644
 645            Event::MouseMoved(
 646                e @ MouseMovedEvent {
 647                    position,
 648                    pressed_button,
 649                    ..
 650                },
 651            ) => {
 652                let mut style_to_assign = CursorStyle::Arrow;
 653                for region in self.window.cursor_regions.iter().rev() {
 654                    if region.bounds.contains_point(*position) {
 655                        style_to_assign = region.style;
 656                        break;
 657                    }
 658                }
 659
 660                if pressed_button.is_none()
 661                    && self
 662                        .window
 663                        .platform_window
 664                        .is_topmost_for_position(*position)
 665                {
 666                    self.platform().set_cursor_style(style_to_assign);
 667                }
 668
 669                if !event_reused {
 670                    if pressed_button.is_some() {
 671                        mouse_events.push(MouseEvent::Drag(MouseDrag {
 672                            region: Default::default(),
 673                            prev_mouse_position: self.window.last_mouse_position,
 674                            platform_event: e.clone(),
 675                            end: false,
 676                        }));
 677                    } else if let Some((_, clicked_button)) = self.window.clicked_region {
 678                        mouse_events.push(MouseEvent::Drag(MouseDrag {
 679                            region: Default::default(),
 680                            prev_mouse_position: self.window.last_mouse_position,
 681                            platform_event: e.clone(),
 682                            end: true,
 683                        }));
 684
 685                        // Mouse up event happened outside the current window. Simulate mouse up button event
 686                        let button_event = e.to_button_event(clicked_button);
 687                        mouse_events.push(MouseEvent::Up(MouseUp {
 688                            region: Default::default(),
 689                            platform_event: button_event.clone(),
 690                        }));
 691                        mouse_events.push(MouseEvent::UpOut(MouseUpOut {
 692                            region: Default::default(),
 693                            platform_event: button_event.clone(),
 694                        }));
 695                        mouse_events.push(MouseEvent::Click(MouseClick {
 696                            region: Default::default(),
 697                            platform_event: button_event.clone(),
 698                        }));
 699                    }
 700
 701                    mouse_events.push(MouseEvent::Move(MouseMove {
 702                        region: Default::default(),
 703                        platform_event: e.clone(),
 704                    }));
 705                }
 706
 707                mouse_events.push(MouseEvent::Hover(MouseHover {
 708                    region: Default::default(),
 709                    platform_event: e.clone(),
 710                    started: false,
 711                }));
 712                mouse_events.push(MouseEvent::MoveOut(MouseMoveOut {
 713                    region: Default::default(),
 714                }));
 715
 716                self.window.last_mouse_moved_event = Some(event.clone());
 717            }
 718
 719            Event::MouseExited(event) => {
 720                // When the platform sends a MouseExited event, synthesize
 721                // a MouseMoved event whose position is outside the window's
 722                // bounds so that hover and cursor state can be updated.
 723                return self.dispatch_event(
 724                    Event::MouseMoved(MouseMovedEvent {
 725                        position: event.position,
 726                        pressed_button: event.pressed_button,
 727                        modifiers: event.modifiers,
 728                    }),
 729                    event_reused,
 730                );
 731            }
 732
 733            Event::ScrollWheel(e) => mouse_events.push(MouseEvent::ScrollWheel(MouseScrollWheel {
 734                region: Default::default(),
 735                platform_event: e.clone(),
 736            })),
 737        }
 738
 739        if let Some(position) = event.position() {
 740            self.window.last_mouse_position = position;
 741        }
 742
 743        // 2. Dispatch mouse events on regions
 744        let mut any_event_handled = false;
 745        for mut mouse_event in mouse_events {
 746            let mut valid_regions = Vec::new();
 747
 748            // GPUI elements are arranged by z_index but sibling elements can register overlapping
 749            // mouse regions. As such, hover events are only fired on overlapping elements which
 750            // are at the same z-index as the topmost element which overlaps with the mouse.
 751            match &mouse_event {
 752                MouseEvent::Hover(_) => {
 753                    let mut highest_z_index = None;
 754                    let mouse_position = self.mouse_position();
 755                    let window = &mut *self.window;
 756                    let prev_hovered_regions = mem::take(&mut window.hovered_region_ids);
 757                    for (region, z_index) in window.mouse_regions.iter().rev() {
 758                        // Allow mouse regions to appear transparent to hovers
 759                        if !region.hoverable {
 760                            continue;
 761                        }
 762
 763                        let contains_mouse = region.bounds.contains_point(mouse_position);
 764
 765                        if contains_mouse && highest_z_index.is_none() {
 766                            highest_z_index = Some(z_index);
 767                        }
 768
 769                        // This unwrap relies on short circuiting boolean expressions
 770                        // The right side of the && is only executed when contains_mouse
 771                        // is true, and we know above that when contains_mouse is true
 772                        // highest_z_index is set.
 773                        if contains_mouse && z_index == highest_z_index.unwrap() {
 774                            //Ensure that hover entrance events aren't sent twice
 775                            if let Err(ix) = window.hovered_region_ids.binary_search(&region.id()) {
 776                                window.hovered_region_ids.insert(ix, region.id());
 777                            }
 778                            // window.hovered_region_ids.insert(region.id());
 779                            if !prev_hovered_regions.contains(&region.id()) {
 780                                valid_regions.push(region.clone());
 781                                if region.notify_on_hover {
 782                                    notified_views.insert(region.id().view_id());
 783                                }
 784                            }
 785                        } else {
 786                            // Ensure that hover exit events aren't sent twice
 787                            if prev_hovered_regions.contains(&region.id()) {
 788                                valid_regions.push(region.clone());
 789                                if region.notify_on_hover {
 790                                    notified_views.insert(region.id().view_id());
 791                                }
 792                            }
 793                        }
 794                    }
 795                }
 796
 797                MouseEvent::Down(_) | MouseEvent::Up(_) => {
 798                    for (region, _) in self.window.mouse_regions.iter().rev() {
 799                        if region.bounds.contains_point(self.mouse_position()) {
 800                            valid_regions.push(region.clone());
 801                            if region.notify_on_click {
 802                                notified_views.insert(region.id().view_id());
 803                            }
 804                        }
 805                    }
 806                }
 807
 808                MouseEvent::Click(e) => {
 809                    // Only raise click events if the released button is the same as the one stored
 810                    if self
 811                        .window
 812                        .clicked_region
 813                        .map(|(_, clicked_button)| clicked_button == e.button)
 814                        .unwrap_or(false)
 815                    {
 816                        // Clear clicked regions and clicked button
 817                        let clicked_region_ids = std::mem::replace(
 818                            &mut self.window.clicked_region_ids,
 819                            Default::default(),
 820                        );
 821                        self.window.clicked_region = None;
 822
 823                        // Find regions which still overlap with the mouse since the last MouseDown happened
 824                        for (mouse_region, _) in self.window.mouse_regions.iter().rev() {
 825                            if clicked_region_ids.contains(&mouse_region.id()) {
 826                                if mouse_region.bounds.contains_point(self.mouse_position()) {
 827                                    valid_regions.push(mouse_region.clone());
 828                                } else {
 829                                    // Let the view know that it hasn't been clicked anymore
 830                                    if mouse_region.notify_on_click {
 831                                        notified_views.insert(mouse_region.id().view_id());
 832                                    }
 833                                }
 834                            }
 835                        }
 836                    }
 837                }
 838
 839                MouseEvent::Drag(_) => {
 840                    for (mouse_region, _) in self.window.mouse_regions.iter().rev() {
 841                        if self.window.clicked_region_ids.contains(&mouse_region.id()) {
 842                            valid_regions.push(mouse_region.clone());
 843                        }
 844                    }
 845                }
 846
 847                MouseEvent::MoveOut(_)
 848                | MouseEvent::UpOut(_)
 849                | MouseEvent::DownOut(_)
 850                | MouseEvent::ClickOut(_) => {
 851                    for (mouse_region, _) in self.window.mouse_regions.iter().rev() {
 852                        // NOT contains
 853                        if !mouse_region.bounds.contains_point(self.mouse_position()) {
 854                            valid_regions.push(mouse_region.clone());
 855                        }
 856                    }
 857                }
 858
 859                _ => {
 860                    for (mouse_region, _) in self.window.mouse_regions.iter().rev() {
 861                        // Contains
 862                        if mouse_region.bounds.contains_point(self.mouse_position()) {
 863                            valid_regions.push(mouse_region.clone());
 864                        }
 865                    }
 866                }
 867            }
 868
 869            //3. Fire region events
 870            let hovered_region_ids = self.window.hovered_region_ids.clone();
 871            for valid_region in valid_regions.into_iter() {
 872                let mut handled = false;
 873                mouse_event.set_region(valid_region.bounds);
 874                if let MouseEvent::Hover(e) = &mut mouse_event {
 875                    e.started = hovered_region_ids.contains(&valid_region.id())
 876                }
 877                // Handle Down events if the MouseRegion has a Click or Drag handler. This makes the api more intuitive as you would
 878                // not expect a MouseRegion to be transparent to Down events if it also has a Click handler.
 879                // This behavior can be overridden by adding a Down handler
 880                if let MouseEvent::Down(e) = &mouse_event {
 881                    let has_click = valid_region
 882                        .handlers
 883                        .contains(MouseEvent::click_disc(), Some(e.button));
 884                    let has_drag = valid_region
 885                        .handlers
 886                        .contains(MouseEvent::drag_disc(), Some(e.button));
 887                    let has_down = valid_region
 888                        .handlers
 889                        .contains(MouseEvent::down_disc(), Some(e.button));
 890                    if !has_down && (has_click || has_drag) {
 891                        handled = true;
 892                    }
 893                }
 894
 895                // `event_consumed` should only be true if there are any handlers for this event.
 896                let mut event_consumed = handled;
 897                if let Some(callbacks) = valid_region.handlers.get(&mouse_event.handler_key()) {
 898                    for callback in callbacks {
 899                        handled = true;
 900                        let view_id = valid_region.id().view_id();
 901                        self.update_any_view(view_id, |view, cx| {
 902                            handled = callback(mouse_event.clone(), view.as_any_mut(), cx, view_id);
 903                        });
 904                        event_consumed |= handled;
 905                        any_event_handled |= handled;
 906                    }
 907                }
 908
 909                any_event_handled |= handled;
 910
 911                // For bubbling events, if the event was handled, don't continue dispatching.
 912                // This only makes sense for local events which return false from is_capturable.
 913                if event_consumed && mouse_event.is_capturable() {
 914                    break;
 915                }
 916            }
 917        }
 918
 919        for view_id in notified_views {
 920            self.notify_view(handle, view_id);
 921        }
 922
 923        any_event_handled
 924    }
 925
 926    fn dispatch_event_2(&mut self, event: &Event) {
 927        match event {
 928            Event::MouseDown(event) => {
 929                self.window.pressed_buttons.insert(event.button);
 930            }
 931            Event::MouseUp(event) => {
 932                self.window.pressed_buttons.remove(&event.button);
 933            }
 934            _ => {}
 935        }
 936
 937        if let Some(mouse_event) = event.mouse_event() {
 938            let event_handlers = self.window.take_event_handlers();
 939            for event_handler in event_handlers.iter().rev() {
 940                if event_handler.event_type == mouse_event.type_id() {
 941                    if !(event_handler.handler)(mouse_event, self) {
 942                        break;
 943                    }
 944                }
 945            }
 946            self.window.event_handlers = event_handlers;
 947        }
 948    }
 949
 950    pub(crate) fn dispatch_key_down(&mut self, event: &KeyDownEvent) -> bool {
 951        let handle = self.window_handle;
 952        if let Some(focused_view_id) = self.window.focused_view_id {
 953            for view_id in self.ancestors(focused_view_id).collect::<Vec<_>>() {
 954                if let Some(mut view) = self.views.remove(&(handle, view_id)) {
 955                    let handled = view.key_down(event, self, view_id);
 956                    self.views.insert((handle, view_id), view);
 957                    if handled {
 958                        return true;
 959                    }
 960                } else {
 961                    log::error!("view {} does not exist", view_id)
 962                }
 963            }
 964        }
 965
 966        false
 967    }
 968
 969    pub(crate) fn dispatch_key_up(&mut self, event: &KeyUpEvent) -> bool {
 970        let handle = self.window_handle;
 971        if let Some(focused_view_id) = self.window.focused_view_id {
 972            for view_id in self.ancestors(focused_view_id).collect::<Vec<_>>() {
 973                if let Some(mut view) = self.views.remove(&(handle, view_id)) {
 974                    let handled = view.key_up(event, self, view_id);
 975                    self.views.insert((handle, view_id), view);
 976                    if handled {
 977                        return true;
 978                    }
 979                } else {
 980                    log::error!("view {} does not exist", view_id)
 981                }
 982            }
 983        }
 984
 985        false
 986    }
 987
 988    pub(crate) fn dispatch_modifiers_changed(&mut self, event: &ModifiersChangedEvent) -> bool {
 989        let handle = self.window_handle;
 990        if let Some(focused_view_id) = self.window.focused_view_id {
 991            for view_id in self.ancestors(focused_view_id).collect::<Vec<_>>() {
 992                if let Some(mut view) = self.views.remove(&(handle, view_id)) {
 993                    let handled = view.modifiers_changed(event, self, view_id);
 994                    self.views.insert((handle, view_id), view);
 995                    if handled {
 996                        return true;
 997                    }
 998                } else {
 999                    log::error!("view {} does not exist", view_id)
1000                }
1001            }
1002        }
1003
1004        false
1005    }
1006
1007    pub fn invalidate(&mut self, mut invalidation: WindowInvalidation, appearance: Appearance) {
1008        self.start_frame();
1009        self.window.appearance = appearance;
1010        for view_id in &invalidation.removed {
1011            invalidation.updated.remove(view_id);
1012            self.window.rendered_views.remove(view_id);
1013        }
1014        for view_id in &invalidation.updated {
1015            let titlebar_height = self.window.titlebar_height;
1016            let element = self
1017                .render_view(RenderParams {
1018                    view_id: *view_id,
1019                    titlebar_height,
1020                    refreshing: false,
1021                    appearance,
1022                })
1023                .unwrap();
1024            self.window.rendered_views.insert(*view_id, element);
1025        }
1026    }
1027
1028    pub fn render_view(&mut self, params: RenderParams) -> Result<Box<dyn AnyRootElement>> {
1029        let handle = self.window_handle;
1030        let view_id = params.view_id;
1031        let mut view = self
1032            .views
1033            .remove(&(handle, view_id))
1034            .ok_or_else(|| anyhow!("view not found"))?;
1035        let element = view.render(self, view_id);
1036        self.views.insert((handle, view_id), view);
1037        Ok(element)
1038    }
1039
1040    pub fn layout(&mut self, refreshing: bool) -> Result<HashMap<usize, usize>> {
1041        let window_size = self.window.platform_window.content_size();
1042        let root_view_id = self.window.root_view().id();
1043
1044        let mut rendered_root = self.window.rendered_views.remove(&root_view_id).unwrap();
1045
1046        self.window.refreshing = refreshing;
1047        rendered_root.layout(SizeConstraint::strict(window_size), self)?;
1048        self.window.refreshing = false;
1049
1050        let views_to_notify_if_ancestors_change =
1051            mem::take(&mut self.window.views_to_notify_if_ancestors_change);
1052        for (view_id, view_ids_to_notify) in views_to_notify_if_ancestors_change {
1053            let mut current_view_id = view_id;
1054            loop {
1055                let old_parent_id = self.window.parents.get(&current_view_id);
1056                let new_parent_id = self.window.new_parents.get(&current_view_id);
1057                if old_parent_id.is_none() && new_parent_id.is_none() {
1058                    break;
1059                } else if old_parent_id == new_parent_id {
1060                    current_view_id = *old_parent_id.unwrap();
1061                } else {
1062                    let handle = self.window_handle;
1063                    for view_id_to_notify in view_ids_to_notify {
1064                        self.notify_view(handle, view_id_to_notify);
1065                    }
1066                    break;
1067                }
1068            }
1069        }
1070
1071        let new_parents = mem::take(&mut self.window.new_parents);
1072        let old_parents = mem::replace(&mut self.window.parents, new_parents);
1073        self.window
1074            .rendered_views
1075            .insert(root_view_id, rendered_root);
1076        Ok(old_parents)
1077    }
1078
1079    pub fn paint(&mut self) -> Result<Scene> {
1080        let window_size = self.window.platform_window.content_size();
1081        let scale_factor = self.window.platform_window.scale_factor();
1082
1083        let root_view_id = self.window.root_view().id();
1084        let mut rendered_root = self.window.rendered_views.remove(&root_view_id).unwrap();
1085
1086        rendered_root.paint(
1087            Vector2F::zero(),
1088            RectF::from_points(Vector2F::zero(), window_size),
1089            self,
1090        )?;
1091        self.window
1092            .rendered_views
1093            .insert(root_view_id, rendered_root);
1094
1095        self.window.text_layout_cache.finish_frame();
1096        let mut scene = self.window.scene.build(scale_factor);
1097        self.window.cursor_regions = scene.cursor_regions();
1098        self.window.mouse_regions = scene.mouse_regions();
1099        self.window.event_handlers = scene.take_event_handlers();
1100
1101        if self.window_is_active() {
1102            if let Some(event) = self.window.last_mouse_moved_event.clone() {
1103                self.dispatch_event(event, true);
1104            }
1105        }
1106
1107        Ok(scene)
1108    }
1109
1110    pub fn root_element(&self) -> &Box<dyn AnyRootElement> {
1111        let view_id = self.window.root_view().id();
1112        self.window.rendered_views.get(&view_id).unwrap()
1113    }
1114
1115    pub fn rect_for_text_range(&self, range_utf16: Range<usize>) -> Option<RectF> {
1116        let focused_view_id = self.window.focused_view_id?;
1117        self.window
1118            .rendered_views
1119            .get(&focused_view_id)?
1120            .rect_for_text_range(range_utf16, self)
1121            .log_err()
1122            .flatten()
1123    }
1124
1125    pub fn set_window_title(&mut self, title: &str) {
1126        self.window.platform_window.set_title(title);
1127    }
1128
1129    pub fn set_window_edited(&mut self, edited: bool) {
1130        self.window.platform_window.set_edited(edited);
1131    }
1132
1133    pub fn is_topmost_window_for_position(&self, position: Vector2F) -> bool {
1134        self.window
1135            .platform_window
1136            .is_topmost_for_position(position)
1137    }
1138
1139    pub fn activate_window(&self) {
1140        self.window.platform_window.activate();
1141    }
1142
1143    pub fn window_is_active(&self) -> bool {
1144        self.window.is_active
1145    }
1146
1147    pub fn window_is_fullscreen(&self) -> bool {
1148        self.window.is_fullscreen
1149    }
1150
1151    pub fn dispatch_action(&mut self, view_id: Option<usize>, action: &dyn Action) -> bool {
1152        if let Some(view_id) = view_id {
1153            self.halt_action_dispatch = false;
1154            self.visit_dispatch_path(view_id, |view_id, capture_phase, cx| {
1155                cx.update_any_view(view_id, |view, cx| {
1156                    let type_id = view.as_any().type_id();
1157                    if let Some((name, mut handlers)) = cx
1158                        .actions_mut(capture_phase)
1159                        .get_mut(&type_id)
1160                        .and_then(|h| h.remove_entry(&action.id()))
1161                    {
1162                        for handler in handlers.iter_mut().rev() {
1163                            cx.halt_action_dispatch = true;
1164                            handler(view, action, cx, view_id);
1165                            if cx.halt_action_dispatch {
1166                                break;
1167                            }
1168                        }
1169                        cx.actions_mut(capture_phase)
1170                            .get_mut(&type_id)
1171                            .unwrap()
1172                            .insert(name, handlers);
1173                    }
1174                });
1175
1176                !cx.halt_action_dispatch
1177            });
1178        }
1179
1180        if !self.halt_action_dispatch {
1181            self.halt_action_dispatch = self.dispatch_global_action_any(action);
1182        }
1183
1184        self.pending_effects
1185            .push_back(Effect::ActionDispatchNotification {
1186                action_id: action.id(),
1187            });
1188        self.halt_action_dispatch
1189    }
1190
1191    /// Returns an iterator over all of the view ids from the passed view up to the root of the window
1192    /// Includes the passed view itself
1193    pub(crate) fn ancestors(&self, mut view_id: usize) -> impl Iterator<Item = usize> + '_ {
1194        std::iter::once(view_id)
1195            .into_iter()
1196            .chain(std::iter::from_fn(move || {
1197                if let Some(parent_id) = self.window.parents.get(&view_id) {
1198                    view_id = *parent_id;
1199                    Some(view_id)
1200                } else {
1201                    None
1202                }
1203            }))
1204    }
1205
1206    // Traverses the parent tree. Walks down the tree toward the passed
1207    // view calling visit with true. Then walks back up the tree calling visit with false.
1208    // If `visit` returns false this function will immediately return.
1209    fn visit_dispatch_path(
1210        &mut self,
1211        view_id: usize,
1212        mut visit: impl FnMut(usize, bool, &mut WindowContext) -> bool,
1213    ) {
1214        // List of view ids from the leaf to the root of the window
1215        let path = self.ancestors(view_id).collect::<Vec<_>>();
1216
1217        // Walk down from the root to the leaf calling visit with capture_phase = true
1218        for view_id in path.iter().rev() {
1219            if !visit(*view_id, true, self) {
1220                return;
1221            }
1222        }
1223
1224        // Walk up from the leaf to the root calling visit with capture_phase = false
1225        for view_id in path.iter() {
1226            if !visit(*view_id, false, self) {
1227                return;
1228            }
1229        }
1230    }
1231
1232    pub fn focused_view_id(&self) -> Option<usize> {
1233        self.window.focused_view_id
1234    }
1235
1236    pub fn focus(&mut self, view_id: Option<usize>) {
1237        self.app_context.focus(self.window_handle, view_id);
1238    }
1239
1240    pub fn window_bounds(&self) -> WindowBounds {
1241        self.window.platform_window.bounds()
1242    }
1243
1244    pub fn titlebar_height(&self) -> f32 {
1245        self.window.titlebar_height
1246    }
1247
1248    pub fn window_appearance(&self) -> Appearance {
1249        self.window.appearance
1250    }
1251
1252    pub fn window_display_uuid(&self) -> Option<Uuid> {
1253        self.window.platform_window.screen().display_uuid()
1254    }
1255
1256    pub fn show_character_palette(&self) {
1257        self.window.platform_window.show_character_palette();
1258    }
1259
1260    pub fn minimize_window(&self) {
1261        self.window.platform_window.minimize();
1262    }
1263
1264    pub fn zoom_window(&self) {
1265        self.window.platform_window.zoom();
1266    }
1267
1268    pub fn toggle_full_screen(&self) {
1269        self.window.platform_window.toggle_full_screen();
1270    }
1271
1272    pub fn prompt(
1273        &self,
1274        level: PromptLevel,
1275        msg: &str,
1276        answers: &[&str],
1277    ) -> oneshot::Receiver<usize> {
1278        self.window.platform_window.prompt(level, msg, answers)
1279    }
1280
1281    pub fn add_view<T, F>(&mut self, build_view: F) -> ViewHandle<T>
1282    where
1283        T: View,
1284        F: FnOnce(&mut ViewContext<T>) -> T,
1285    {
1286        self.add_option_view(|cx| Some(build_view(cx))).unwrap()
1287    }
1288
1289    pub fn add_option_view<T, F>(&mut self, build_view: F) -> Option<ViewHandle<T>>
1290    where
1291        T: View,
1292        F: FnOnce(&mut ViewContext<T>) -> Option<T>,
1293    {
1294        let handle = self.window_handle;
1295        let view_id = post_inc(&mut self.next_id);
1296        let mut cx = ViewContext::mutable(self, view_id);
1297        let handle = if let Some(view) = build_view(&mut cx) {
1298            let mut keymap_context = KeymapContext::default();
1299            view.update_keymap_context(&mut keymap_context, cx.app_context());
1300            self.views_metadata.insert(
1301                (handle, view_id),
1302                ViewMetadata {
1303                    type_id: TypeId::of::<T>(),
1304                    keymap_context,
1305                },
1306            );
1307            self.views.insert((handle, view_id), Box::new(view));
1308            self.window
1309                .invalidation
1310                .get_or_insert_with(Default::default)
1311                .updated
1312                .insert(view_id);
1313            Some(ViewHandle::new(handle, view_id, &self.ref_counts))
1314        } else {
1315            None
1316        };
1317        handle
1318    }
1319
1320    pub fn text_style(&self) -> TextStyle {
1321        self.window
1322            .text_style_stack
1323            .last()
1324            .cloned()
1325            .unwrap_or(TextStyle::default(&self.font_cache))
1326    }
1327
1328    pub fn push_text_style(&mut self, refinement: &TextStyleRefinement) -> Result<()> {
1329        let mut style = self.text_style();
1330        style.refine(refinement, self.font_cache())?;
1331        self.window.text_style_stack.push(style);
1332        Ok(())
1333    }
1334
1335    pub fn pop_text_style(&mut self) {
1336        self.window.text_style_stack.pop();
1337    }
1338
1339    pub fn theme<T: 'static>(&self) -> &T {
1340        self.window
1341            .theme_stack
1342            .iter()
1343            .rev()
1344            .find_map(|theme| theme.downcast_ref())
1345            .ok_or_else(|| anyhow!("no theme provided of type {}", type_name::<T>()))
1346            .unwrap()
1347    }
1348
1349    pub fn push_theme<T: 'static>(&mut self, theme: T) {
1350        self.window.theme_stack.push(Box::new(theme));
1351    }
1352
1353    pub fn pop_theme(&mut self) {
1354        self.window.theme_stack.pop();
1355    }
1356}
1357
1358#[derive(Default)]
1359pub struct LayoutEngine(Taffy);
1360pub use taffy::style::Style as LayoutStyle;
1361
1362impl LayoutEngine {
1363    pub fn new() -> Self {
1364        Default::default()
1365    }
1366
1367    pub fn add_node<C>(&mut self, style: LayoutStyle, children: C) -> Result<LayoutId>
1368    where
1369        C: IntoIterator<Item = LayoutId>,
1370    {
1371        let children = children.into_iter().collect::<Vec<_>>();
1372        if children.is_empty() {
1373            Ok(self.0.new_leaf(style)?)
1374        } else {
1375            Ok(self.0.new_with_children(style, &children)?)
1376        }
1377    }
1378
1379    pub fn add_measured_node<F>(&mut self, style: LayoutStyle, measure: F) -> Result<LayoutId>
1380    where
1381        F: Fn(MeasureParams) -> Size<f32> + Sync + Send + 'static,
1382    {
1383        Ok(self
1384            .0
1385            .new_leaf_with_measure(style, MeasureFunc::Boxed(Box::new(MeasureFn(measure))))?)
1386    }
1387
1388    pub fn compute_layout(&mut self, root: LayoutId, available_space: Vector2F) -> Result<()> {
1389        self.0.compute_layout(
1390            root,
1391            taffy::geometry::Size {
1392                width: available_space.x().into(),
1393                height: available_space.y().into(),
1394            },
1395        )?;
1396        Ok(())
1397    }
1398
1399    pub fn computed_layout(&mut self, node: LayoutId) -> Result<Layout> {
1400        Ok(Layout::from(self.0.layout(node)?))
1401    }
1402}
1403
1404pub struct MeasureFn<F>(F);
1405
1406impl<F: Send + Sync> Measurable for MeasureFn<F>
1407where
1408    F: Fn(MeasureParams) -> Size<f32>,
1409{
1410    fn measure(
1411        &self,
1412        known_dimensions: taffy::prelude::Size<Option<f32>>,
1413        available_space: taffy::prelude::Size<taffy::style::AvailableSpace>,
1414    ) -> taffy::prelude::Size<f32> {
1415        (self.0)(MeasureParams {
1416            known_dimensions: known_dimensions.into(),
1417            available_space: available_space.into(),
1418        })
1419        .into()
1420    }
1421}
1422
1423#[derive(Debug, Clone, Default)]
1424pub struct Layout {
1425    pub bounds: RectF,
1426    pub order: u32,
1427}
1428
1429pub struct MeasureParams {
1430    pub known_dimensions: Size<Option<f32>>,
1431    pub available_space: Size<AvailableSpace>,
1432}
1433
1434#[derive(Clone, Debug)]
1435pub enum AvailableSpace {
1436    /// The amount of space available is the specified number of pixels
1437    Pixels(f32),
1438    /// The amount of space available is indefinite and the node should be laid out under a min-content constraint
1439    MinContent,
1440    /// The amount of space available is indefinite and the node should be laid out under a max-content constraint
1441    MaxContent,
1442}
1443
1444impl Default for AvailableSpace {
1445    fn default() -> Self {
1446        Self::Pixels(0.)
1447    }
1448}
1449
1450impl From<taffy::prelude::AvailableSpace> for AvailableSpace {
1451    fn from(value: taffy::prelude::AvailableSpace) -> Self {
1452        match value {
1453            taffy::prelude::AvailableSpace::Definite(pixels) => Self::Pixels(pixels),
1454            taffy::prelude::AvailableSpace::MinContent => Self::MinContent,
1455            taffy::prelude::AvailableSpace::MaxContent => Self::MaxContent,
1456        }
1457    }
1458}
1459
1460impl From<&taffy::tree::Layout> for Layout {
1461    fn from(value: &taffy::tree::Layout) -> Self {
1462        Self {
1463            bounds: RectF::new(
1464                vec2f(value.location.x, value.location.y),
1465                vec2f(value.size.width, value.size.height),
1466            ),
1467            order: value.order,
1468        }
1469    }
1470}
1471
1472pub type LayoutId = taffy::prelude::NodeId;
1473
1474pub struct RenderParams {
1475    pub view_id: usize,
1476    pub titlebar_height: f32,
1477    pub refreshing: bool,
1478    pub appearance: Appearance,
1479}
1480
1481#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
1482pub enum Axis {
1483    #[default]
1484    Horizontal,
1485    Vertical,
1486}
1487
1488impl Axis {
1489    pub fn invert(self) -> Self {
1490        match self {
1491            Self::Horizontal => Self::Vertical,
1492            Self::Vertical => Self::Horizontal,
1493        }
1494    }
1495
1496    pub fn component(&self, point: Vector2F) -> f32 {
1497        match self {
1498            Self::Horizontal => point.x(),
1499            Self::Vertical => point.y(),
1500        }
1501    }
1502}
1503
1504impl ToJson for Axis {
1505    fn to_json(&self) -> serde_json::Value {
1506        match self {
1507            Axis::Horizontal => json!("horizontal"),
1508            Axis::Vertical => json!("vertical"),
1509        }
1510    }
1511}
1512
1513impl StaticColumnCount for Axis {}
1514impl Bind for Axis {
1515    fn bind(&self, statement: &Statement, start_index: i32) -> anyhow::Result<i32> {
1516        match self {
1517            Axis::Horizontal => "Horizontal",
1518            Axis::Vertical => "Vertical",
1519        }
1520        .bind(statement, start_index)
1521    }
1522}
1523
1524impl Column for Axis {
1525    fn column(statement: &mut Statement, start_index: i32) -> anyhow::Result<(Self, i32)> {
1526        String::column(statement, start_index).and_then(|(axis_text, next_index)| {
1527            Ok((
1528                match axis_text.as_str() {
1529                    "Horizontal" => Axis::Horizontal,
1530                    "Vertical" => Axis::Vertical,
1531                    _ => bail!("Stored serialized item kind is incorrect"),
1532                },
1533                next_index,
1534            ))
1535        })
1536    }
1537}
1538
1539pub trait Vector2FExt {
1540    fn along(self, axis: Axis) -> f32;
1541}
1542
1543impl Vector2FExt for Vector2F {
1544    fn along(self, axis: Axis) -> f32 {
1545        match axis {
1546            Axis::Horizontal => self.x(),
1547            Axis::Vertical => self.y(),
1548        }
1549    }
1550}
1551
1552pub trait RectFExt {
1553    fn length_along(self, axis: Axis) -> f32;
1554}
1555
1556impl RectFExt for RectF {
1557    fn length_along(self, axis: Axis) -> f32 {
1558        match axis {
1559            Axis::Horizontal => self.width(),
1560            Axis::Vertical => self.height(),
1561        }
1562    }
1563}
1564
1565#[derive(Copy, Clone, Debug)]
1566pub struct SizeConstraint {
1567    pub min: Vector2F,
1568    pub max: Vector2F,
1569}
1570
1571impl SizeConstraint {
1572    pub fn new(min: Vector2F, max: Vector2F) -> Self {
1573        Self { min, max }
1574    }
1575
1576    pub fn strict(size: Vector2F) -> Self {
1577        Self {
1578            min: size,
1579            max: size,
1580        }
1581    }
1582    pub fn loose(max: Vector2F) -> Self {
1583        Self {
1584            min: Vector2F::zero(),
1585            max,
1586        }
1587    }
1588
1589    pub fn strict_along(axis: Axis, max: f32) -> Self {
1590        match axis {
1591            Axis::Horizontal => Self {
1592                min: vec2f(max, 0.0),
1593                max: vec2f(max, f32::INFINITY),
1594            },
1595            Axis::Vertical => Self {
1596                min: vec2f(0.0, max),
1597                max: vec2f(f32::INFINITY, max),
1598            },
1599        }
1600    }
1601
1602    pub fn max_along(&self, axis: Axis) -> f32 {
1603        match axis {
1604            Axis::Horizontal => self.max.x(),
1605            Axis::Vertical => self.max.y(),
1606        }
1607    }
1608
1609    pub fn min_along(&self, axis: Axis) -> f32 {
1610        match axis {
1611            Axis::Horizontal => self.min.x(),
1612            Axis::Vertical => self.min.y(),
1613        }
1614    }
1615
1616    pub fn constrain(&self, size: Vector2F) -> Vector2F {
1617        vec2f(
1618            size.x().min(self.max.x()).max(self.min.x()),
1619            size.y().min(self.max.y()).max(self.min.y()),
1620        )
1621    }
1622}
1623
1624impl Sub<Vector2F> for SizeConstraint {
1625    type Output = SizeConstraint;
1626
1627    fn sub(self, rhs: Vector2F) -> SizeConstraint {
1628        SizeConstraint {
1629            min: self.min - rhs,
1630            max: self.max - rhs,
1631        }
1632    }
1633}
1634
1635impl Default for SizeConstraint {
1636    fn default() -> Self {
1637        SizeConstraint {
1638            min: Vector2F::zero(),
1639            max: Vector2F::splat(f32::INFINITY),
1640        }
1641    }
1642}
1643
1644impl ToJson for SizeConstraint {
1645    fn to_json(&self) -> serde_json::Value {
1646        json!({
1647            "min": self.min.to_json(),
1648            "max": self.max.to_json(),
1649        })
1650    }
1651}
1652
1653#[derive(Clone)]
1654pub struct ChildView {
1655    view_id: usize,
1656    view_name: &'static str,
1657}
1658
1659impl ChildView {
1660    pub fn new(view: &AnyViewHandle, cx: &AppContext) -> Self {
1661        let view_name = cx.view_ui_name(view.window, view.id()).unwrap();
1662        Self {
1663            view_id: view.id(),
1664            view_name,
1665        }
1666    }
1667}
1668
1669impl<V: 'static> Element<V> for ChildView {
1670    type LayoutState = ();
1671    type PaintState = ();
1672
1673    fn layout(
1674        &mut self,
1675        constraint: SizeConstraint,
1676        _: &mut V,
1677        cx: &mut ViewContext<V>,
1678    ) -> (Vector2F, Self::LayoutState) {
1679        if let Some(mut rendered_view) = cx.window.rendered_views.remove(&self.view_id) {
1680            let parent_id = cx.view_id();
1681            cx.window.new_parents.insert(self.view_id, parent_id);
1682            let size = rendered_view
1683                .layout(constraint, cx)
1684                .log_err()
1685                .unwrap_or(Vector2F::zero());
1686            cx.window.rendered_views.insert(self.view_id, rendered_view);
1687            (size, ())
1688        } else {
1689            log::error!(
1690                "layout called on a ChildView element whose underlying view was dropped (view_id: {}, name: {:?})",
1691                self.view_id,
1692                self.view_name
1693            );
1694            (Vector2F::zero(), ())
1695        }
1696    }
1697
1698    fn paint(
1699        &mut self,
1700        bounds: RectF,
1701        visible_bounds: RectF,
1702        _: &mut Self::LayoutState,
1703        _: &mut V,
1704        cx: &mut ViewContext<V>,
1705    ) {
1706        if let Some(mut rendered_view) = cx.window.rendered_views.remove(&self.view_id) {
1707            rendered_view
1708                .paint(bounds.origin(), visible_bounds, cx)
1709                .log_err();
1710            cx.window.rendered_views.insert(self.view_id, rendered_view);
1711        } else {
1712            log::error!(
1713                "paint called on a ChildView element whose underlying view was dropped (view_id: {}, name: {:?})",
1714                self.view_id,
1715                self.view_name
1716            );
1717        }
1718    }
1719
1720    fn rect_for_text_range(
1721        &self,
1722        range_utf16: Range<usize>,
1723        _: RectF,
1724        _: RectF,
1725        _: &Self::LayoutState,
1726        _: &Self::PaintState,
1727        _: &V,
1728        cx: &ViewContext<V>,
1729    ) -> Option<RectF> {
1730        if let Some(rendered_view) = cx.window.rendered_views.get(&self.view_id) {
1731            rendered_view
1732                .rect_for_text_range(range_utf16, &cx.window_context)
1733                .log_err()
1734                .flatten()
1735        } else {
1736            log::error!(
1737                "rect_for_text_range called on a ChildView element whose underlying view was dropped (view_id: {}, name: {:?})",
1738                self.view_id,
1739                self.view_name
1740            );
1741            None
1742        }
1743    }
1744
1745    fn debug(
1746        &self,
1747        bounds: RectF,
1748        _: &Self::LayoutState,
1749        _: &Self::PaintState,
1750        _: &V,
1751        cx: &ViewContext<V>,
1752    ) -> serde_json::Value {
1753        json!({
1754            "type": "ChildView",
1755            "bounds": bounds.to_json(),
1756            "child": if let Some(element) = cx.window.rendered_views.get(&self.view_id) {
1757                element.debug(&cx.window_context).log_err().unwrap_or_else(|| json!(null))
1758            } else {
1759                json!(null)
1760            }
1761        })
1762    }
1763}