window.rs

  1use crate::{
  2    px, AnyView, AppContext, AvailableSpace, Bounds, Context, Effect, Element, EntityId, Handle,
  3    LayoutId, MainThreadOnly, Pixels, Platform, PlatformWindow, Point, Reference, Scene, Size,
  4    StackContext, Style, TaffyLayoutEngine, WeakHandle, WindowOptions,
  5};
  6use anyhow::Result;
  7use derive_more::{Deref, DerefMut};
  8use std::{any::TypeId, future, marker::PhantomData, sync::Arc};
  9use util::ResultExt;
 10
 11pub struct AnyWindow {}
 12
 13pub struct Window {
 14    handle: AnyWindowHandle,
 15    platform_window: MainThreadOnly<Box<dyn PlatformWindow>>,
 16    rem_size: Pixels,
 17    content_size: Size<Pixels>,
 18    layout_engine: TaffyLayoutEngine,
 19    pub(crate) root_view: Option<AnyView<()>>,
 20    mouse_position: Point<Pixels>,
 21    pub(crate) scene: Scene,
 22    pub(crate) dirty: bool,
 23}
 24
 25impl Window {
 26    pub fn new(
 27        handle: AnyWindowHandle,
 28        options: WindowOptions,
 29        platform: &dyn Platform,
 30        cx: &mut AppContext,
 31    ) -> Self {
 32        let platform_window = platform.open_window(handle, options);
 33        let mouse_position = platform_window.mouse_position();
 34        let content_size = platform_window.content_size();
 35        let scale_factor = platform_window.scale_factor();
 36        platform_window.on_resize(Box::new({
 37            let handle = handle;
 38            let cx = cx.to_async();
 39            move |content_size, scale_factor| {
 40                cx.update_window(handle, |cx| {
 41                    cx.window.scene = Scene::new(scale_factor);
 42                    cx.window.content_size = content_size;
 43                    cx.window.dirty = true;
 44                })
 45                .log_err();
 46            }
 47        }));
 48
 49        let platform_window = MainThreadOnly::new(Arc::new(platform_window), platform.dispatcher());
 50
 51        Window {
 52            handle,
 53            platform_window,
 54            rem_size: px(16.),
 55            content_size,
 56            layout_engine: TaffyLayoutEngine::new(),
 57            root_view: None,
 58            mouse_position,
 59            scene: Scene::new(scale_factor),
 60            dirty: true,
 61        }
 62    }
 63}
 64
 65#[derive(Deref, DerefMut)]
 66pub struct WindowContext<'a, 'b> {
 67    #[deref]
 68    #[deref_mut]
 69    app: Reference<'a, AppContext>,
 70    window: Reference<'b, Window>,
 71}
 72
 73impl<'a, 'w> WindowContext<'a, 'w> {
 74    pub(crate) fn mutable(app: &'a mut AppContext, window: &'w mut Window) -> Self {
 75        Self {
 76            app: Reference::Mutable(app),
 77            window: Reference::Mutable(window),
 78        }
 79    }
 80
 81    pub(crate) fn draw(&mut self) -> Result<()> {
 82        let unit_entity = self.unit_entity.clone();
 83        self.update_entity(&unit_entity, |_, cx| {
 84            let mut root_view = cx.window.root_view.take().unwrap();
 85            let (root_layout_id, mut frame_state) = root_view.layout(&mut (), cx)?;
 86            let available_space = cx.window.content_size.map(Into::into);
 87            cx.window
 88                .layout_engine
 89                .compute_layout(root_layout_id, available_space)?;
 90            let layout = cx.window.layout_engine.layout(root_layout_id)?;
 91            root_view.paint(layout, &mut (), &mut frame_state, cx)?;
 92            cx.window.root_view = Some(root_view);
 93            let scene = cx.window.scene.take();
 94            dbg!(&scene);
 95            let _ = cx.window.platform_window.read(|platform_window| {
 96                platform_window.draw(scene);
 97                future::ready(())
 98            });
 99
100            Ok(())
101        })
102    }
103
104    pub fn request_layout(
105        &mut self,
106        style: Style,
107        children: impl IntoIterator<Item = LayoutId>,
108    ) -> Result<LayoutId> {
109        self.app.layout_id_buffer.clear();
110        self.app.layout_id_buffer.extend(children.into_iter());
111        let rem_size = self.rem_size();
112
113        self.window
114            .layout_engine
115            .request_layout(style, rem_size, &self.app.layout_id_buffer)
116    }
117
118    pub fn request_measured_layout<
119        F: Fn(Size<Option<Pixels>>, Size<AvailableSpace>) -> Size<Pixels> + Send + Sync + 'static,
120    >(
121        &mut self,
122        style: Style,
123        rem_size: Pixels,
124        measure: F,
125    ) -> Result<LayoutId> {
126        self.window
127            .layout_engine
128            .request_measured_layout(style, rem_size, measure)
129    }
130
131    pub fn layout(&mut self, layout_id: LayoutId) -> Result<Layout> {
132        Ok(self
133            .window
134            .layout_engine
135            .layout(layout_id)
136            .map(Into::into)?)
137    }
138
139    pub fn rem_size(&self) -> Pixels {
140        self.window.rem_size
141    }
142
143    pub fn mouse_position(&self) -> Point<Pixels> {
144        self.window.mouse_position
145    }
146}
147
148impl Context for WindowContext<'_, '_> {
149    type EntityContext<'a, 'w, T: Send + Sync + 'static> = ViewContext<'a, 'w, T>;
150    type Result<T> = T;
151
152    fn entity<T: Send + Sync + 'static>(
153        &mut self,
154        build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T,
155    ) -> Handle<T> {
156        let slot = self.entities.reserve();
157        let entity = build_entity(&mut ViewContext::mutable(
158            &mut *self.app,
159            &mut self.window,
160            slot.id,
161        ));
162        self.entities.redeem(slot, entity)
163    }
164
165    fn update_entity<T: Send + Sync + 'static, R>(
166        &mut self,
167        handle: &Handle<T>,
168        update: impl FnOnce(&mut T, &mut Self::EntityContext<'_, '_, T>) -> R,
169    ) -> R {
170        let mut entity = self.entities.lease(handle);
171        let result = update(
172            &mut *entity,
173            &mut ViewContext::mutable(&mut *self.app, &mut *self.window, handle.id),
174        );
175        self.entities.end_lease(entity);
176        result
177    }
178}
179
180impl<S> StackContext for ViewContext<'_, '_, S> {
181    fn app(&mut self) -> &mut AppContext {
182        &mut *self.app
183    }
184
185    fn with_text_style<F, R>(&mut self, style: crate::TextStyleRefinement, f: F) -> R
186    where
187        F: FnOnce(&mut Self) -> R,
188    {
189        self.push_text_style(style);
190        let result = f(self);
191        self.pop_text_style();
192        result
193    }
194
195    fn with_state<T: Send + Sync + 'static, F, R>(&mut self, state: T, f: F) -> R
196    where
197        F: FnOnce(&mut Self) -> R,
198    {
199        self.push_state(state);
200        let result = f(self);
201        self.pop_state::<T>();
202        result
203    }
204}
205
206#[derive(Deref, DerefMut)]
207pub struct ViewContext<'a, 'w, S> {
208    #[deref]
209    #[deref_mut]
210    window_cx: WindowContext<'a, 'w>,
211    entity_type: PhantomData<S>,
212    entity_id: EntityId,
213}
214
215impl<'a, 'w, S: Send + Sync + 'static> ViewContext<'a, 'w, S> {
216    fn mutable(app: &'a mut AppContext, window: &'w mut Window, entity_id: EntityId) -> Self {
217        Self {
218            window_cx: WindowContext::mutable(app, window),
219            entity_id,
220            entity_type: PhantomData,
221        }
222    }
223
224    pub fn handle(&self) -> WeakHandle<S> {
225        self.entities.weak_handle(self.entity_id)
226    }
227
228    pub fn observe<E: Send + Sync + 'static>(
229        &mut self,
230        handle: &Handle<E>,
231        on_notify: impl Fn(&mut S, Handle<E>, &mut ViewContext<'_, '_, S>) + Send + Sync + 'static,
232    ) {
233        let this = self.handle();
234        let handle = handle.downgrade();
235        let window_handle = self.window.handle;
236        self.app
237            .observers
238            .entry(handle.id)
239            .or_default()
240            .push(Arc::new(move |cx| {
241                cx.update_window(window_handle.id, |cx| {
242                    if let Some(handle) = handle.upgrade(cx) {
243                        this.update(cx, |this, cx| on_notify(this, handle, cx))
244                            .is_ok()
245                    } else {
246                        false
247                    }
248                })
249                .unwrap_or(false)
250            }));
251    }
252
253    pub fn notify(&mut self) {
254        let entity_id = self.entity_id;
255        self.app
256            .pending_effects
257            .push_back(Effect::Notify(entity_id));
258        self.window.dirty = true;
259    }
260
261    pub(crate) fn erase_state<R>(&mut self, f: impl FnOnce(&mut ViewContext<()>) -> R) -> R {
262        let entity_id = self.unit_entity.id;
263        let mut cx = ViewContext::mutable(
264            &mut *self.window_cx.app,
265            &mut *self.window_cx.window,
266            entity_id,
267        );
268        f(&mut cx)
269    }
270}
271
272impl<'a, 'w, S: 'static> Context for ViewContext<'a, 'w, S> {
273    type EntityContext<'b, 'c, U: Send + Sync + 'static> = ViewContext<'b, 'c, U>;
274    type Result<U> = U;
275
276    fn entity<T2: Send + Sync + 'static>(
277        &mut self,
278        build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T2>) -> T2,
279    ) -> Handle<T2> {
280        self.window_cx.entity(build_entity)
281    }
282
283    fn update_entity<U: Send + Sync + 'static, R>(
284        &mut self,
285        handle: &Handle<U>,
286        update: impl FnOnce(&mut U, &mut Self::EntityContext<'_, '_, U>) -> R,
287    ) -> R {
288        self.window_cx.update_entity(handle, update)
289    }
290}
291
292// #[derive(Clone, Copy, Eq, PartialEq, Hash)]
293slotmap::new_key_type! { pub struct WindowId; }
294
295#[derive(PartialEq, Eq)]
296pub struct WindowHandle<S> {
297    id: WindowId,
298    state_type: PhantomData<S>,
299}
300
301impl<S> Copy for WindowHandle<S> {}
302
303impl<S> Clone for WindowHandle<S> {
304    fn clone(&self) -> Self {
305        WindowHandle {
306            id: self.id,
307            state_type: PhantomData,
308        }
309    }
310}
311
312impl<S> WindowHandle<S> {
313    pub fn new(id: WindowId) -> Self {
314        WindowHandle {
315            id,
316            state_type: PhantomData,
317        }
318    }
319}
320
321impl<S: 'static> Into<AnyWindowHandle> for WindowHandle<S> {
322    fn into(self) -> AnyWindowHandle {
323        AnyWindowHandle {
324            id: self.id,
325            state_type: TypeId::of::<S>(),
326        }
327    }
328}
329
330#[derive(Copy, Clone, PartialEq, Eq)]
331pub struct AnyWindowHandle {
332    pub(crate) id: WindowId,
333    state_type: TypeId,
334}
335
336#[derive(Clone)]
337pub struct Layout {
338    pub order: u32,
339    pub bounds: Bounds<Pixels>,
340}