window.rs

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