window.rs

  1use crate::{
  2    px, AnyView, AppContext, AvailableSpace, Bounds, Context, Effect, Element, EntityId, Handle,
  3    LayoutId, MainThread, MainThreadOnly, Pixels, PlatformWindow, Point, Reference, Scene, Size,
  4    StackContext, Style, TaffyLayoutEngine, WeakHandle, WindowOptions,
  5};
  6use anyhow::Result;
  7use futures::Future;
  8use std::{any::TypeId, marker::PhantomData, mem, 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        cx: &mut MainThread<AppContext>,
 30    ) -> Self {
 31        let platform_window = cx.platform().open_window(handle, options);
 32        let mouse_position = platform_window.mouse_position();
 33        let content_size = platform_window.content_size();
 34        let scale_factor = platform_window.scale_factor();
 35        platform_window.on_resize(Box::new({
 36            let handle = handle;
 37            let cx = cx.to_async();
 38            move |content_size, scale_factor| {
 39                cx.update_window(handle, |cx| {
 40                    cx.window.scene = Scene::new(scale_factor);
 41                    cx.window.content_size = content_size;
 42                    cx.window.dirty = true;
 43                })
 44                .log_err();
 45            }
 46        }));
 47
 48        let platform_window =
 49            MainThreadOnly::new(Arc::new(platform_window), cx.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
 65pub struct WindowContext<'a, 'w> {
 66    app: Reference<'a, AppContext>,
 67    window: Reference<'w, Window>,
 68}
 69
 70impl<'a, 'w> WindowContext<'a, 'w> {
 71    pub(crate) fn mutable(app: &'a mut AppContext, window: &'w mut Window) -> Self {
 72        Self {
 73            app: Reference::Mutable(app),
 74            window: Reference::Mutable(window),
 75        }
 76    }
 77
 78    pub fn notify(&mut self) {
 79        self.window.dirty = true;
 80    }
 81
 82    pub fn request_layout(
 83        &mut self,
 84        style: Style,
 85        children: impl IntoIterator<Item = LayoutId>,
 86    ) -> Result<LayoutId> {
 87        self.app.layout_id_buffer.clear();
 88        self.app.layout_id_buffer.extend(children.into_iter());
 89        let rem_size = self.rem_size();
 90
 91        self.window
 92            .layout_engine
 93            .request_layout(style, rem_size, &self.app.layout_id_buffer)
 94    }
 95
 96    pub fn request_measured_layout<
 97        F: Fn(Size<Option<Pixels>>, Size<AvailableSpace>) -> Size<Pixels> + Send + Sync + 'static,
 98    >(
 99        &mut self,
100        style: Style,
101        rem_size: Pixels,
102        measure: F,
103    ) -> Result<LayoutId> {
104        self.window
105            .layout_engine
106            .request_measured_layout(style, rem_size, measure)
107    }
108
109    pub fn layout(&mut self, layout_id: LayoutId) -> Result<Layout> {
110        Ok(self
111            .window
112            .layout_engine
113            .layout(layout_id)
114            .map(Into::into)?)
115    }
116
117    pub fn rem_size(&self) -> Pixels {
118        self.window.rem_size
119    }
120
121    pub fn mouse_position(&self) -> Point<Pixels> {
122        self.window.mouse_position
123    }
124
125    pub fn scene(&mut self) -> &mut Scene {
126        &mut self.window.scene
127    }
128
129    pub fn run_on_main<R>(
130        &self,
131        f: impl FnOnce(&mut MainThread<WindowContext>) -> R + Send + 'static,
132    ) -> impl Future<Output = Result<R>>
133    where
134        R: Send + 'static,
135    {
136        let id = self.window.handle.id;
137        self.app.run_on_main(move |cx| {
138            cx.update_window(id, |cx| {
139                f(unsafe {
140                    mem::transmute::<&mut WindowContext, &mut MainThread<WindowContext>>(cx)
141                })
142            })
143        })
144    }
145
146    pub(crate) fn draw(&mut self) -> Result<()> {
147        let unit_entity = self.unit_entity.clone();
148        self.update_entity(&unit_entity, |_, cx| {
149            let mut root_view = cx.window.root_view.take().unwrap();
150            let (root_layout_id, mut frame_state) = root_view.layout(&mut (), cx)?;
151            let available_space = cx.window.content_size.map(Into::into);
152
153            dbg!("computing layout");
154            cx.window
155                .layout_engine
156                .compute_layout(root_layout_id, available_space)?;
157            dbg!("asking for layout");
158            let layout = cx.window.layout_engine.layout(root_layout_id)?;
159
160            dbg!("painting root view");
161
162            root_view.paint(layout, &mut (), &mut frame_state, cx)?;
163            cx.window.root_view = Some(root_view);
164            let scene = cx.window.scene.take();
165
166            let _ = cx.run_on_main(|cx| {
167                cx.window
168                    .platform_window
169                    .borrow_on_main_thread()
170                    .draw(scene);
171            });
172
173            cx.window.dirty = false;
174            Ok(())
175        })
176    }
177}
178
179impl MainThread<WindowContext<'_, '_>> {
180    // todo!("implement other methods that use platform window")
181    fn platform_window(&self) -> &dyn PlatformWindow {
182        self.window.platform_window.borrow_on_main_thread().as_ref()
183    }
184}
185
186impl Context for WindowContext<'_, '_> {
187    type EntityContext<'a, 'w, T: 'static + Send + Sync> = ViewContext<'a, 'w, T>;
188    type Result<T> = T;
189
190    fn entity<T: Send + Sync + 'static>(
191        &mut self,
192        build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T,
193    ) -> Handle<T> {
194        let slot = self.app.entities.reserve();
195        let entity = build_entity(&mut ViewContext::mutable(
196            &mut *self.app,
197            &mut self.window,
198            slot.id,
199        ));
200        self.entities.redeem(slot, entity)
201    }
202
203    fn update_entity<T: Send + Sync + 'static, R>(
204        &mut self,
205        handle: &Handle<T>,
206        update: impl FnOnce(&mut T, &mut Self::EntityContext<'_, '_, T>) -> R,
207    ) -> R {
208        let mut entity = self.entities.lease(handle);
209        let result = update(
210            &mut *entity,
211            &mut ViewContext::mutable(&mut *self.app, &mut *self.window, handle.id),
212        );
213        self.entities.end_lease(entity);
214        result
215    }
216}
217
218impl<'a, 'w> std::ops::Deref for WindowContext<'a, 'w> {
219    type Target = AppContext;
220
221    fn deref(&self) -> &Self::Target {
222        &self.app
223    }
224}
225
226impl<'a, 'w> std::ops::DerefMut for WindowContext<'a, 'w> {
227    fn deref_mut(&mut self) -> &mut Self::Target {
228        &mut self.app
229    }
230}
231
232impl<S> StackContext for ViewContext<'_, '_, S> {
233    fn app(&mut self) -> &mut AppContext {
234        &mut *self.window_cx.app
235    }
236
237    fn with_text_style<F, R>(&mut self, style: crate::TextStyleRefinement, f: F) -> R
238    where
239        F: FnOnce(&mut Self) -> R,
240    {
241        self.window_cx.app.push_text_style(style);
242        let result = f(self);
243        self.window_cx.app.pop_text_style();
244        result
245    }
246
247    fn with_state<T: Send + Sync + 'static, F, R>(&mut self, state: T, f: F) -> R
248    where
249        F: FnOnce(&mut Self) -> R,
250    {
251        self.window_cx.app.push_state(state);
252        let result = f(self);
253        self.window_cx.app.pop_state::<T>();
254        result
255    }
256}
257
258pub struct ViewContext<'a, 'w, S> {
259    window_cx: WindowContext<'a, 'w>,
260    entity_type: PhantomData<S>,
261    entity_id: EntityId,
262}
263
264impl<'a, 'w, S: Send + Sync + 'static> ViewContext<'a, 'w, S> {
265    fn mutable(app: &'a mut AppContext, window: &'w mut Window, entity_id: EntityId) -> Self {
266        Self {
267            window_cx: WindowContext::mutable(app, window),
268            entity_id,
269            entity_type: PhantomData,
270        }
271    }
272
273    pub fn handle(&self) -> WeakHandle<S> {
274        self.entities.weak_handle(self.entity_id)
275    }
276
277    pub fn observe<E: Send + Sync + 'static>(
278        &mut self,
279        handle: &Handle<E>,
280        on_notify: impl Fn(&mut S, Handle<E>, &mut ViewContext<'_, '_, S>) + Send + Sync + 'static,
281    ) {
282        let this = self.handle();
283        let handle = handle.downgrade();
284        let window_handle = self.window.handle;
285        self.app
286            .observers
287            .entry(handle.id)
288            .or_default()
289            .push(Arc::new(move |cx| {
290                cx.update_window(window_handle.id, |cx| {
291                    if let Some(handle) = handle.upgrade(cx) {
292                        this.update(cx, |this, cx| on_notify(this, handle, cx))
293                            .is_ok()
294                    } else {
295                        false
296                    }
297                })
298                .unwrap_or(false)
299            }));
300    }
301
302    pub fn notify(&mut self) {
303        self.window_cx.notify();
304        self.window_cx
305            .app
306            .pending_effects
307            .push_back(Effect::Notify(self.entity_id));
308    }
309
310    pub(crate) fn erase_state<R>(&mut self, f: impl FnOnce(&mut ViewContext<()>) -> R) -> R {
311        let entity_id = self.unit_entity.id;
312        let mut cx = ViewContext::mutable(
313            &mut *self.window_cx.app,
314            &mut *self.window_cx.window,
315            entity_id,
316        );
317        f(&mut cx)
318    }
319}
320
321impl<'a, 'w, S> Context for ViewContext<'a, 'w, S> {
322    type EntityContext<'b, 'c, U: 'static + Send + Sync> = ViewContext<'b, 'c, U>;
323    type Result<U> = U;
324
325    fn entity<T2: Send + Sync + 'static>(
326        &mut self,
327        build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T2>) -> T2,
328    ) -> Handle<T2> {
329        self.window_cx.entity(build_entity)
330    }
331
332    fn update_entity<U: Send + Sync + 'static, R>(
333        &mut self,
334        handle: &Handle<U>,
335        update: impl FnOnce(&mut U, &mut Self::EntityContext<'_, '_, U>) -> R,
336    ) -> R {
337        self.window_cx.update_entity(handle, update)
338    }
339}
340
341impl<'a, 'w, S: 'static> std::ops::Deref for ViewContext<'a, 'w, S> {
342    type Target = WindowContext<'a, 'w>;
343
344    fn deref(&self) -> &Self::Target {
345        &self.window_cx
346    }
347}
348
349impl<'a, 'w, S: 'static> std::ops::DerefMut for ViewContext<'a, 'w, S> {
350    fn deref_mut(&mut self) -> &mut Self::Target {
351        &mut self.window_cx
352    }
353}
354
355// #[derive(Clone, Copy, Eq, PartialEq, Hash)]
356slotmap::new_key_type! { pub struct WindowId; }
357
358#[derive(PartialEq, Eq)]
359pub struct WindowHandle<S> {
360    id: WindowId,
361    state_type: PhantomData<S>,
362}
363
364impl<S> Copy for WindowHandle<S> {}
365
366impl<S> Clone for WindowHandle<S> {
367    fn clone(&self) -> Self {
368        WindowHandle {
369            id: self.id,
370            state_type: PhantomData,
371        }
372    }
373}
374
375impl<S> WindowHandle<S> {
376    pub fn new(id: WindowId) -> Self {
377        WindowHandle {
378            id,
379            state_type: PhantomData,
380        }
381    }
382}
383
384impl<S: 'static> Into<AnyWindowHandle> for WindowHandle<S> {
385    fn into(self) -> AnyWindowHandle {
386        AnyWindowHandle {
387            id: self.id,
388            state_type: TypeId::of::<S>(),
389        }
390    }
391}
392
393#[derive(Copy, Clone, PartialEq, Eq)]
394pub struct AnyWindowHandle {
395    pub(crate) id: WindowId,
396    state_type: TypeId,
397}
398
399#[derive(Clone)]
400pub struct Layout {
401    pub order: u32,
402    pub bounds: Bounds<Pixels>,
403}