window.rs

  1use crate::{
  2    image_cache::RenderImageParams, px, AnyView, AppContext, AvailableSpace, BorrowAppContext,
  3    Bounds, Context, Corners, DevicePixels, Effect, Element, EntityId, FontId, GlyphId, Handle,
  4    Hsla, ImageData, IsZero, LayerId, LayoutId, MainThread, MainThreadOnly, MonochromeSprite,
  5    Pixels, PlatformAtlas, PlatformWindow, Point, PolychromeSprite, Reference, RenderGlyphParams,
  6    RenderSvgParams, ScaledPixels, Scene, SharedString, Size, Style, TaffyLayoutEngine, WeakHandle,
  7    WindowOptions, SUBPIXEL_VARIANTS,
  8};
  9use anyhow::Result;
 10use futures::Future;
 11use smallvec::SmallVec;
 12use std::{any::TypeId, borrow::Cow, marker::PhantomData, mem, sync::Arc};
 13use util::ResultExt;
 14
 15pub struct AnyWindow {}
 16
 17pub struct Window {
 18    handle: AnyWindowHandle,
 19    platform_window: MainThreadOnly<Box<dyn PlatformWindow>>,
 20    sprite_atlas: Arc<dyn PlatformAtlas>,
 21    rem_size: Pixels,
 22    content_size: Size<Pixels>,
 23    layout_engine: TaffyLayoutEngine,
 24    pub(crate) root_view: Option<AnyView<()>>,
 25    mouse_position: Point<Pixels>,
 26    current_layer_id: LayerId,
 27    content_mask_stack: Vec<ContentMask>,
 28    pub(crate) scene: Scene,
 29    pub(crate) dirty: bool,
 30}
 31
 32impl Window {
 33    pub fn new(
 34        handle: AnyWindowHandle,
 35        options: WindowOptions,
 36        cx: &mut MainThread<AppContext>,
 37    ) -> Self {
 38        let platform_window = cx.platform().open_window(handle, options);
 39        let sprite_atlas = platform_window.sprite_atlas();
 40        let mouse_position = platform_window.mouse_position();
 41        let content_size = platform_window.content_size();
 42        let scale_factor = platform_window.scale_factor();
 43        platform_window.on_resize(Box::new({
 44            let handle = handle;
 45            let cx = cx.to_async();
 46            move |content_size, scale_factor| {
 47                cx.update_window(handle, |cx| {
 48                    cx.window.scene = Scene::new(scale_factor);
 49                    cx.window.content_size = content_size;
 50                    cx.window.dirty = true;
 51                })
 52                .log_err();
 53            }
 54        }));
 55
 56        let platform_window =
 57            MainThreadOnly::new(Arc::new(platform_window), cx.platform().dispatcher());
 58
 59        Window {
 60            handle,
 61            platform_window,
 62            sprite_atlas,
 63            rem_size: px(16.),
 64            content_size,
 65            layout_engine: TaffyLayoutEngine::new(),
 66            root_view: None,
 67            mouse_position,
 68            current_layer_id: SmallVec::new(),
 69            content_mask_stack: Vec::new(),
 70            scene: Scene::new(scale_factor),
 71            dirty: true,
 72        }
 73    }
 74}
 75
 76#[derive(Clone, Debug)]
 77pub struct ContentMask {
 78    pub bounds: Bounds<Pixels>,
 79    pub corner_radii: Corners<Pixels>,
 80}
 81
 82impl ContentMask {
 83    pub fn scale(&self, factor: f32) -> ScaledContentMask {
 84        ScaledContentMask {
 85            bounds: self.bounds.scale(factor),
 86            corner_radii: self.corner_radii.scale(factor),
 87        }
 88    }
 89}
 90
 91#[derive(Clone, Debug, PartialEq, Eq)]
 92#[repr(C)]
 93pub struct ScaledContentMask {
 94    bounds: Bounds<ScaledPixels>,
 95    corner_radii: Corners<ScaledPixels>,
 96}
 97
 98pub struct WindowContext<'a, 'w> {
 99    app: Reference<'a, AppContext>,
100    window: Reference<'w, Window>,
101}
102
103impl<'a, 'w> WindowContext<'a, 'w> {
104    pub(crate) fn mutable(app: &'a mut AppContext, window: &'w mut Window) -> Self {
105        Self {
106            app: Reference::Mutable(app),
107            window: Reference::Mutable(window),
108        }
109    }
110
111    pub fn notify(&mut self) {
112        self.window.dirty = true;
113    }
114
115    pub fn request_layout(
116        &mut self,
117        style: Style,
118        children: impl IntoIterator<Item = LayoutId>,
119    ) -> Result<LayoutId> {
120        self.app.layout_id_buffer.clear();
121        self.app.layout_id_buffer.extend(children.into_iter());
122        let rem_size = self.rem_size();
123
124        self.window
125            .layout_engine
126            .request_layout(style, rem_size, &self.app.layout_id_buffer)
127    }
128
129    pub fn request_measured_layout<
130        F: Fn(Size<Option<Pixels>>, Size<AvailableSpace>) -> Size<Pixels> + Send + Sync + 'static,
131    >(
132        &mut self,
133        style: Style,
134        rem_size: Pixels,
135        measure: F,
136    ) -> Result<LayoutId> {
137        self.window
138            .layout_engine
139            .request_measured_layout(style, rem_size, measure)
140    }
141
142    pub fn layout(&mut self, layout_id: LayoutId) -> Result<Layout> {
143        Ok(self
144            .window
145            .layout_engine
146            .layout(layout_id)
147            .map(Into::into)?)
148    }
149
150    pub fn scale_factor(&self) -> f32 {
151        self.window.scene.scale_factor
152    }
153
154    pub fn rem_size(&self) -> Pixels {
155        self.window.rem_size
156    }
157
158    pub fn mouse_position(&self) -> Point<Pixels> {
159        self.window.mouse_position
160    }
161
162    pub fn scene(&mut self) -> &mut Scene {
163        &mut self.window.scene
164    }
165
166    pub fn stack<R>(&mut self, order: u32, f: impl FnOnce(&mut Self) -> R) -> R {
167        self.window.current_layer_id.push(order);
168        let result = f(self);
169        self.window.current_layer_id.pop();
170        result
171    }
172
173    pub fn clip<F, R>(
174        &mut self,
175        bounds: Bounds<Pixels>,
176        corner_radii: Corners<Pixels>,
177        f: impl FnOnce(&mut Self) -> R,
178    ) -> R {
179        let clip_mask = ContentMask {
180            bounds,
181            corner_radii,
182        };
183
184        self.window.content_mask_stack.push(clip_mask);
185        let result = f(self);
186        self.window.content_mask_stack.pop();
187        result
188    }
189
190    pub fn current_layer_id(&self) -> LayerId {
191        self.window.current_layer_id.clone()
192    }
193
194    pub fn run_on_main<R>(
195        &self,
196        f: impl FnOnce(&mut MainThread<WindowContext>) -> R + Send + 'static,
197    ) -> impl Future<Output = Result<R>>
198    where
199        R: Send + 'static,
200    {
201        let id = self.window.handle.id;
202        self.app.run_on_main(move |cx| {
203            cx.update_window(id, |cx| {
204                f(unsafe {
205                    mem::transmute::<&mut WindowContext, &mut MainThread<WindowContext>>(cx)
206                })
207            })
208        })
209    }
210
211    pub fn paint_glyph(
212        &mut self,
213        origin: Point<Pixels>,
214        order: u32,
215        font_id: FontId,
216        glyph_id: GlyphId,
217        font_size: Pixels,
218        color: Hsla,
219    ) -> Result<()> {
220        let scale_factor = self.scale_factor();
221        let glyph_origin = origin.scale(scale_factor);
222        let subpixel_variant = Point {
223            x: (glyph_origin.x.0.fract() * SUBPIXEL_VARIANTS as f32).floor() as u8,
224            y: (glyph_origin.y.0.fract() * SUBPIXEL_VARIANTS as f32).floor() as u8,
225        };
226        let params = RenderGlyphParams {
227            font_id,
228            glyph_id,
229            font_size,
230            subpixel_variant,
231            scale_factor,
232            is_emoji: false,
233        };
234
235        let raster_bounds = self.text_system().raster_bounds(&params)?;
236        if !raster_bounds.is_zero() {
237            let layer_id = self.current_layer_id();
238            let tile =
239                self.window
240                    .sprite_atlas
241                    .get_or_insert_with(&params.clone().into(), &mut || {
242                        let (size, bytes) = self.text_system().rasterize_glyph(&params)?;
243                        Ok((size, Cow::Owned(bytes)))
244                    })?;
245            let bounds = Bounds {
246                origin: glyph_origin.map(|px| px.floor()) + raster_bounds.origin.map(Into::into),
247                size: tile.bounds.size.map(Into::into),
248            };
249            let content_mask = self.content_mask().scale(scale_factor);
250
251            self.window.scene.insert(
252                layer_id,
253                MonochromeSprite {
254                    order,
255                    bounds,
256                    content_mask,
257                    color,
258                    tile,
259                },
260            );
261        }
262        Ok(())
263    }
264
265    pub fn paint_emoji(
266        &mut self,
267        origin: Point<Pixels>,
268        order: u32,
269        font_id: FontId,
270        glyph_id: GlyphId,
271        font_size: Pixels,
272    ) -> Result<()> {
273        let scale_factor = self.scale_factor();
274        let glyph_origin = origin.scale(scale_factor);
275        let params = RenderGlyphParams {
276            font_id,
277            glyph_id,
278            font_size,
279            // We don't render emojis with subpixel variants.
280            subpixel_variant: Default::default(),
281            scale_factor,
282            is_emoji: true,
283        };
284
285        let raster_bounds = self.text_system().raster_bounds(&params)?;
286        if !raster_bounds.is_zero() {
287            let layer_id = self.current_layer_id();
288            let tile =
289                self.window
290                    .sprite_atlas
291                    .get_or_insert_with(&params.clone().into(), &mut || {
292                        let (size, bytes) = self.text_system().rasterize_glyph(&params)?;
293                        Ok((size, Cow::Owned(bytes)))
294                    })?;
295            let bounds = Bounds {
296                origin: glyph_origin.map(|px| px.floor()) + raster_bounds.origin.map(Into::into),
297                size: tile.bounds.size.map(Into::into),
298            };
299            let content_mask = self.content_mask().scale(scale_factor);
300
301            self.window.scene.insert(
302                layer_id,
303                PolychromeSprite {
304                    order,
305                    bounds,
306                    content_mask,
307                    tile,
308                    grayscale: false,
309                },
310            );
311        }
312        Ok(())
313    }
314
315    pub fn paint_svg(
316        &mut self,
317        bounds: Bounds<Pixels>,
318        order: u32,
319        path: SharedString,
320        color: Hsla,
321    ) -> Result<()> {
322        let scale_factor = self.scale_factor();
323        let bounds = bounds.scale(scale_factor);
324        // Render the SVG at twice the size to get a higher quality result.
325        let params = RenderSvgParams {
326            path,
327            size: bounds
328                .size
329                .map(|pixels| DevicePixels::from((pixels.0 * 2.).ceil() as i32)),
330        };
331
332        let layer_id = self.current_layer_id();
333        let tile =
334            self.window
335                .sprite_atlas
336                .get_or_insert_with(&params.clone().into(), &mut || {
337                    let bytes = self.svg_renderer.render(&params)?;
338                    Ok((params.size, Cow::Owned(bytes)))
339                })?;
340        let content_mask = self.content_mask().scale(scale_factor);
341
342        self.window.scene.insert(
343            layer_id,
344            MonochromeSprite {
345                order,
346                bounds,
347                content_mask,
348                color,
349                tile,
350            },
351        );
352
353        Ok(())
354    }
355
356    pub fn paint_image(
357        &mut self,
358        bounds: Bounds<Pixels>,
359        order: u32,
360        data: Arc<ImageData>,
361        grayscale: bool,
362    ) -> Result<()> {
363        let scale_factor = self.scale_factor();
364        let bounds = bounds.scale(scale_factor);
365        let params = RenderImageParams { image_id: data.id };
366
367        let layer_id = self.current_layer_id();
368        let tile = self
369            .window
370            .sprite_atlas
371            .get_or_insert_with(&params.clone().into(), &mut || {
372                Ok((data.size(), Cow::Borrowed(data.as_bytes())))
373            })?;
374        let content_mask = self.content_mask().scale(scale_factor);
375
376        self.window.scene.insert(
377            layer_id,
378            PolychromeSprite {
379                order,
380                bounds,
381                content_mask,
382                tile,
383                grayscale,
384            },
385        );
386
387        Ok(())
388    }
389
390    pub(crate) fn draw(&mut self) -> Result<()> {
391        let unit_entity = self.unit_entity.clone();
392        self.update_entity(&unit_entity, |_, cx| {
393            let mut root_view = cx.window.root_view.take().unwrap();
394            let (root_layout_id, mut frame_state) = root_view.layout(&mut (), cx)?;
395            let available_space = cx.window.content_size.map(Into::into);
396
397            cx.window
398                .layout_engine
399                .compute_layout(root_layout_id, available_space)?;
400            let layout = cx.window.layout_engine.layout(root_layout_id)?;
401
402            root_view.paint(layout, &mut (), &mut frame_state, cx)?;
403            cx.window.root_view = Some(root_view);
404            let scene = cx.window.scene.take();
405
406            let _ = cx.run_on_main(|cx| {
407                cx.window
408                    .platform_window
409                    .borrow_on_main_thread()
410                    .draw(scene);
411            });
412
413            cx.window.dirty = false;
414            Ok(())
415        })
416    }
417}
418
419impl Context for WindowContext<'_, '_> {
420    type EntityContext<'a, 'w, T: 'static + Send + Sync> = ViewContext<'a, 'w, T>;
421    type Result<T> = T;
422
423    fn entity<T: Send + Sync + 'static>(
424        &mut self,
425        build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T,
426    ) -> Handle<T> {
427        let slot = self.app.entities.reserve();
428        let entity = build_entity(&mut ViewContext::mutable(
429            &mut *self.app,
430            &mut self.window,
431            slot.id,
432        ));
433        self.entities.redeem(slot, entity)
434    }
435
436    fn update_entity<T: Send + Sync + 'static, R>(
437        &mut self,
438        handle: &Handle<T>,
439        update: impl FnOnce(&mut T, &mut Self::EntityContext<'_, '_, T>) -> R,
440    ) -> R {
441        let mut entity = self.entities.lease(handle);
442        let result = update(
443            &mut *entity,
444            &mut ViewContext::mutable(&mut *self.app, &mut *self.window, handle.id),
445        );
446        self.entities.end_lease(entity);
447        result
448    }
449}
450
451impl<'a, 'w> std::ops::Deref for WindowContext<'a, 'w> {
452    type Target = AppContext;
453
454    fn deref(&self) -> &Self::Target {
455        &self.app
456    }
457}
458
459impl<'a, 'w> std::ops::DerefMut for WindowContext<'a, 'w> {
460    fn deref_mut(&mut self) -> &mut Self::Target {
461        &mut self.app
462    }
463}
464
465impl BorrowAppContext for WindowContext<'_, '_> {
466    fn app_mut(&mut self) -> &mut AppContext {
467        &mut *self.app
468    }
469}
470
471pub trait BorrowWindow: BorrowAppContext {
472    fn window(&self) -> &Window;
473    fn window_mut(&mut self) -> &mut Window;
474
475    fn with_content_mask<R>(&mut self, mask: ContentMask, f: impl FnOnce(&mut Self) -> R) -> R {
476        self.window_mut().content_mask_stack.push(mask);
477        let result = f(self);
478        self.window_mut().content_mask_stack.pop();
479        result
480    }
481
482    fn content_mask(&self) -> ContentMask {
483        self.window()
484            .content_mask_stack
485            .last()
486            .cloned()
487            .unwrap_or_else(|| ContentMask {
488                bounds: Bounds {
489                    origin: Point::default(),
490                    size: self.window().content_size,
491                },
492                corner_radii: Default::default(),
493            })
494    }
495
496    fn rem_size(&self) -> Pixels {
497        self.window().rem_size
498    }
499}
500
501impl BorrowWindow for WindowContext<'_, '_> {
502    fn window(&self) -> &Window {
503        &*self.window
504    }
505
506    fn window_mut(&mut self) -> &mut Window {
507        &mut *self.window
508    }
509}
510
511pub struct ViewContext<'a, 'w, S> {
512    window_cx: WindowContext<'a, 'w>,
513    entity_type: PhantomData<S>,
514    entity_id: EntityId,
515}
516
517impl<S> BorrowAppContext for ViewContext<'_, '_, S> {
518    fn app_mut(&mut self) -> &mut AppContext {
519        &mut *self.window_cx.app
520    }
521
522    fn with_text_style<F, R>(&mut self, style: crate::TextStyleRefinement, f: F) -> R
523    where
524        F: FnOnce(&mut Self) -> R,
525    {
526        self.window_cx.app.push_text_style(style);
527        let result = f(self);
528        self.window_cx.app.pop_text_style();
529        result
530    }
531
532    fn with_state<T: Send + Sync + 'static, F, R>(&mut self, state: T, f: F) -> R
533    where
534        F: FnOnce(&mut Self) -> R,
535    {
536        self.window_cx.app.push_state(state);
537        let result = f(self);
538        self.window_cx.app.pop_state::<T>();
539        result
540    }
541}
542
543impl<S> BorrowWindow for ViewContext<'_, '_, S> {
544    fn window(&self) -> &Window {
545        &self.window_cx.window
546    }
547
548    fn window_mut(&mut self) -> &mut Window {
549        &mut *self.window_cx.window
550    }
551}
552
553impl<'a, 'w, S: Send + Sync + 'static> ViewContext<'a, 'w, S> {
554    fn mutable(app: &'a mut AppContext, window: &'w mut Window, entity_id: EntityId) -> Self {
555        Self {
556            window_cx: WindowContext::mutable(app, window),
557            entity_id,
558            entity_type: PhantomData,
559        }
560    }
561
562    pub fn handle(&self) -> WeakHandle<S> {
563        self.entities.weak_handle(self.entity_id)
564    }
565
566    pub fn observe<E: Send + Sync + 'static>(
567        &mut self,
568        handle: &Handle<E>,
569        on_notify: impl Fn(&mut S, Handle<E>, &mut ViewContext<'_, '_, S>) + Send + Sync + 'static,
570    ) {
571        let this = self.handle();
572        let handle = handle.downgrade();
573        let window_handle = self.window.handle;
574        self.app
575            .observers
576            .entry(handle.id)
577            .or_default()
578            .push(Arc::new(move |cx| {
579                cx.update_window(window_handle.id, |cx| {
580                    if let Some(handle) = handle.upgrade(cx) {
581                        this.update(cx, |this, cx| on_notify(this, handle, cx))
582                            .is_ok()
583                    } else {
584                        false
585                    }
586                })
587                .unwrap_or(false)
588            }));
589    }
590
591    pub fn notify(&mut self) {
592        self.window_cx.notify();
593        self.window_cx
594            .app
595            .pending_effects
596            .push_back(Effect::Notify(self.entity_id));
597    }
598
599    pub(crate) fn erase_state<R>(&mut self, f: impl FnOnce(&mut ViewContext<()>) -> R) -> R {
600        let entity_id = self.unit_entity.id;
601        let mut cx = ViewContext::mutable(
602            &mut *self.window_cx.app,
603            &mut *self.window_cx.window,
604            entity_id,
605        );
606        f(&mut cx)
607    }
608}
609
610impl<'a, 'w, S> Context for ViewContext<'a, 'w, S> {
611    type EntityContext<'b, 'c, U: 'static + Send + Sync> = ViewContext<'b, 'c, U>;
612    type Result<U> = U;
613
614    fn entity<T2: Send + Sync + 'static>(
615        &mut self,
616        build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T2>) -> T2,
617    ) -> Handle<T2> {
618        self.window_cx.entity(build_entity)
619    }
620
621    fn update_entity<U: Send + Sync + 'static, R>(
622        &mut self,
623        handle: &Handle<U>,
624        update: impl FnOnce(&mut U, &mut Self::EntityContext<'_, '_, U>) -> R,
625    ) -> R {
626        self.window_cx.update_entity(handle, update)
627    }
628}
629
630impl<'a, 'w, S: 'static> std::ops::Deref for ViewContext<'a, 'w, S> {
631    type Target = WindowContext<'a, 'w>;
632
633    fn deref(&self) -> &Self::Target {
634        &self.window_cx
635    }
636}
637
638impl<'a, 'w, S: 'static> std::ops::DerefMut for ViewContext<'a, 'w, S> {
639    fn deref_mut(&mut self) -> &mut Self::Target {
640        &mut self.window_cx
641    }
642}
643
644// #[derive(Clone, Copy, Eq, PartialEq, Hash)]
645slotmap::new_key_type! { pub struct WindowId; }
646
647#[derive(PartialEq, Eq)]
648pub struct WindowHandle<S> {
649    id: WindowId,
650    state_type: PhantomData<S>,
651}
652
653impl<S> Copy for WindowHandle<S> {}
654
655impl<S> Clone for WindowHandle<S> {
656    fn clone(&self) -> Self {
657        WindowHandle {
658            id: self.id,
659            state_type: PhantomData,
660        }
661    }
662}
663
664impl<S> WindowHandle<S> {
665    pub fn new(id: WindowId) -> Self {
666        WindowHandle {
667            id,
668            state_type: PhantomData,
669        }
670    }
671}
672
673impl<S: 'static> Into<AnyWindowHandle> for WindowHandle<S> {
674    fn into(self) -> AnyWindowHandle {
675        AnyWindowHandle {
676            id: self.id,
677            state_type: TypeId::of::<S>(),
678        }
679    }
680}
681
682#[derive(Copy, Clone, PartialEq, Eq)]
683pub struct AnyWindowHandle {
684    pub(crate) id: WindowId,
685    state_type: TypeId,
686}
687
688#[derive(Clone)]
689pub struct Layout {
690    pub order: u32,
691    pub bounds: Bounds<Pixels>,
692}