1use crate::{
2 px, AnyView, AppContext, AtlasTile, AvailableSpace, Bounds, Context, DevicePixels, Effect,
3 Element, EntityId, FontId, GlyphId, Handle, LayoutId, MainThread, MainThreadOnly,
4 MonochromeSprite, Pixels, PlatformAtlas, PlatformWindow, Point, RasterizedGlyphId, Reference,
5 Scene, Size, StackContext, StackingOrder, Style, TaffyLayoutEngine, WeakHandle, WindowOptions,
6 SUBPIXEL_VARIANTS,
7};
8use anyhow::Result;
9use futures::Future;
10use smallvec::SmallVec;
11use std::{any::TypeId, marker::PhantomData, mem, sync::Arc};
12use util::ResultExt;
13
14pub struct AnyWindow {}
15
16pub struct Window {
17 handle: AnyWindowHandle,
18 platform_window: MainThreadOnly<Box<dyn PlatformWindow>>,
19 glyph_atlas: Arc<dyn PlatformAtlas<RasterizedGlyphId>>,
20 rem_size: Pixels,
21 content_size: Size<Pixels>,
22 layout_engine: TaffyLayoutEngine,
23 pub(crate) root_view: Option<AnyView<()>>,
24 mouse_position: Point<Pixels>,
25 current_layer_id: StackingOrder,
26 pub(crate) scene: Scene,
27 pub(crate) dirty: bool,
28}
29
30impl Window {
31 pub fn new(
32 handle: AnyWindowHandle,
33 options: WindowOptions,
34 cx: &mut MainThread<AppContext>,
35 ) -> Self {
36 let platform_window = cx.platform().open_window(handle, options);
37 let glyph_atlas = platform_window.glyph_atlas();
38 let mouse_position = platform_window.mouse_position();
39 let content_size = platform_window.content_size();
40 let scale_factor = platform_window.scale_factor();
41 platform_window.on_resize(Box::new({
42 let handle = handle;
43 let cx = cx.to_async();
44 move |content_size, scale_factor| {
45 cx.update_window(handle, |cx| {
46 cx.window.scene = Scene::new(scale_factor);
47 cx.window.content_size = content_size;
48 cx.window.dirty = true;
49 })
50 .log_err();
51 }
52 }));
53
54 let platform_window =
55 MainThreadOnly::new(Arc::new(platform_window), cx.platform().dispatcher());
56
57 Window {
58 handle,
59 platform_window,
60 glyph_atlas,
61 rem_size: px(16.),
62 content_size,
63 layout_engine: TaffyLayoutEngine::new(),
64 root_view: None,
65 mouse_position,
66 current_layer_id: SmallVec::new(),
67 scene: Scene::new(scale_factor),
68 dirty: true,
69 }
70 }
71}
72
73pub struct WindowContext<'a, 'w> {
74 app: Reference<'a, AppContext>,
75 window: Reference<'w, Window>,
76}
77
78impl<'a, 'w> WindowContext<'a, 'w> {
79 pub(crate) fn mutable(app: &'a mut AppContext, window: &'w mut Window) -> Self {
80 Self {
81 app: Reference::Mutable(app),
82 window: Reference::Mutable(window),
83 }
84 }
85
86 pub fn notify(&mut self) {
87 self.window.dirty = true;
88 }
89
90 pub fn request_layout(
91 &mut self,
92 style: Style,
93 children: impl IntoIterator<Item = LayoutId>,
94 ) -> Result<LayoutId> {
95 self.app.layout_id_buffer.clear();
96 self.app.layout_id_buffer.extend(children.into_iter());
97 let rem_size = self.rem_size();
98
99 self.window
100 .layout_engine
101 .request_layout(style, rem_size, &self.app.layout_id_buffer)
102 }
103
104 pub fn request_measured_layout<
105 F: Fn(Size<Option<Pixels>>, Size<AvailableSpace>) -> Size<Pixels> + Send + Sync + 'static,
106 >(
107 &mut self,
108 style: Style,
109 rem_size: Pixels,
110 measure: F,
111 ) -> Result<LayoutId> {
112 self.window
113 .layout_engine
114 .request_measured_layout(style, rem_size, measure)
115 }
116
117 pub fn layout(&mut self, layout_id: LayoutId) -> Result<Layout> {
118 Ok(self
119 .window
120 .layout_engine
121 .layout(layout_id)
122 .map(Into::into)?)
123 }
124
125 pub fn scale_factor(&self) -> f32 {
126 self.window.scene.scale_factor
127 }
128
129 pub fn rem_size(&self) -> Pixels {
130 self.window.rem_size
131 }
132
133 pub fn mouse_position(&self) -> Point<Pixels> {
134 self.window.mouse_position
135 }
136
137 pub fn scene(&mut self) -> &mut Scene {
138 &mut self.window.scene
139 }
140
141 pub fn stack<R>(&mut self, order: u32, f: impl FnOnce(&mut Self) -> R) -> R {
142 self.window.current_layer_id.push(order);
143 let result = f(self);
144 self.window.current_layer_id.pop();
145 result
146 }
147
148 pub fn current_layer_id(&self) -> StackingOrder {
149 self.window.current_layer_id.clone()
150 }
151
152 pub fn run_on_main<R>(
153 &self,
154 f: impl FnOnce(&mut MainThread<WindowContext>) -> R + Send + 'static,
155 ) -> impl Future<Output = Result<R>>
156 where
157 R: Send + 'static,
158 {
159 let id = self.window.handle.id;
160 self.app.run_on_main(move |cx| {
161 cx.update_window(id, |cx| {
162 f(unsafe {
163 mem::transmute::<&mut WindowContext, &mut MainThread<WindowContext>>(cx)
164 })
165 })
166 })
167 }
168
169 pub fn rasterize_glyph(
170 &self,
171 font_id: FontId,
172 glyph_id: GlyphId,
173 font_size: Pixels,
174 scale_factor: f32,
175 target_position: Point<Pixels>,
176 ) -> Result<(AtlasTile, Bounds<Pixels>)> {
177 let target_position = target_position * scale_factor;
178 let subpixel_variant = Point {
179 x: (target_position.x.0.fract() * SUBPIXEL_VARIANTS as f32).floor() as u8,
180 y: (target_position.y.0.fract() * SUBPIXEL_VARIANTS as f32).floor() as u8,
181 };
182 let rasterized_glyph_id = RasterizedGlyphId {
183 font_id,
184 glyph_id,
185 font_size,
186 subpixel_variant,
187 scale_factor,
188 };
189 let mut offset = Default::default();
190 let tile = self
191 .window
192 .glyph_atlas
193 .get_or_insert_with(&rasterized_glyph_id, &mut || {
194 let (bounds, pixels) = self.text_system().rasterize_glyph(&rasterized_glyph_id)?;
195 offset = bounds.origin;
196 Ok((bounds.size, pixels))
197 })?;
198
199 // Align bounding box surrounding glyph to pixel grid
200 let mut origin = (target_position * scale_factor).map(|p| p.floor());
201 // Position glyph within bounding box
202 origin += offset.map(|o| px(u32::from(o) as f32));
203 let size = tile.bounds_in_atlas.size.map(|b| px(b.0 as f32));
204 let bounds = Bounds { origin, size };
205
206 Ok((tile, bounds))
207 }
208
209 pub(crate) fn draw(&mut self) -> Result<()> {
210 let unit_entity = self.unit_entity.clone();
211 self.update_entity(&unit_entity, |_, cx| {
212 let mut root_view = cx.window.root_view.take().unwrap();
213 let (root_layout_id, mut frame_state) = root_view.layout(&mut (), cx)?;
214 let available_space = cx.window.content_size.map(Into::into);
215
216 dbg!("computing layout");
217 cx.window
218 .layout_engine
219 .compute_layout(root_layout_id, available_space)?;
220 dbg!("asking for layout");
221 let layout = cx.window.layout_engine.layout(root_layout_id)?;
222
223 dbg!("painting root view");
224
225 root_view.paint(layout, &mut (), &mut frame_state, cx)?;
226 cx.window.root_view = Some(root_view);
227 let scene = cx.window.scene.take();
228
229 let _ = cx.run_on_main(|cx| {
230 cx.window
231 .platform_window
232 .borrow_on_main_thread()
233 .draw(scene);
234 });
235
236 cx.window.dirty = false;
237 Ok(())
238 })
239 }
240}
241
242impl MainThread<WindowContext<'_, '_>> {
243 // todo!("implement other methods that use platform window")
244 fn platform_window(&self) -> &dyn PlatformWindow {
245 self.window.platform_window.borrow_on_main_thread().as_ref()
246 }
247}
248
249impl Context for WindowContext<'_, '_> {
250 type EntityContext<'a, 'w, T: 'static + Send + Sync> = ViewContext<'a, 'w, T>;
251 type Result<T> = T;
252
253 fn entity<T: Send + Sync + 'static>(
254 &mut self,
255 build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T,
256 ) -> Handle<T> {
257 let slot = self.app.entities.reserve();
258 let entity = build_entity(&mut ViewContext::mutable(
259 &mut *self.app,
260 &mut self.window,
261 slot.id,
262 ));
263 self.entities.redeem(slot, entity)
264 }
265
266 fn update_entity<T: Send + Sync + 'static, R>(
267 &mut self,
268 handle: &Handle<T>,
269 update: impl FnOnce(&mut T, &mut Self::EntityContext<'_, '_, T>) -> R,
270 ) -> R {
271 let mut entity = self.entities.lease(handle);
272 let result = update(
273 &mut *entity,
274 &mut ViewContext::mutable(&mut *self.app, &mut *self.window, handle.id),
275 );
276 self.entities.end_lease(entity);
277 result
278 }
279}
280
281impl<'a, 'w> std::ops::Deref for WindowContext<'a, 'w> {
282 type Target = AppContext;
283
284 fn deref(&self) -> &Self::Target {
285 &self.app
286 }
287}
288
289impl<'a, 'w> std::ops::DerefMut for WindowContext<'a, 'w> {
290 fn deref_mut(&mut self) -> &mut Self::Target {
291 &mut self.app
292 }
293}
294
295impl<S> StackContext for ViewContext<'_, '_, S> {
296 fn app(&mut self) -> &mut AppContext {
297 &mut *self.window_cx.app
298 }
299
300 fn with_text_style<F, R>(&mut self, style: crate::TextStyleRefinement, f: F) -> R
301 where
302 F: FnOnce(&mut Self) -> R,
303 {
304 self.window_cx.app.push_text_style(style);
305 let result = f(self);
306 self.window_cx.app.pop_text_style();
307 result
308 }
309
310 fn with_state<T: Send + Sync + 'static, F, R>(&mut self, state: T, f: F) -> R
311 where
312 F: FnOnce(&mut Self) -> R,
313 {
314 self.window_cx.app.push_state(state);
315 let result = f(self);
316 self.window_cx.app.pop_state::<T>();
317 result
318 }
319}
320
321pub struct ViewContext<'a, 'w, S> {
322 window_cx: WindowContext<'a, 'w>,
323 entity_type: PhantomData<S>,
324 entity_id: EntityId,
325}
326
327impl<'a, 'w, S: Send + Sync + 'static> ViewContext<'a, 'w, S> {
328 fn mutable(app: &'a mut AppContext, window: &'w mut Window, entity_id: EntityId) -> Self {
329 Self {
330 window_cx: WindowContext::mutable(app, window),
331 entity_id,
332 entity_type: PhantomData,
333 }
334 }
335
336 pub fn handle(&self) -> WeakHandle<S> {
337 self.entities.weak_handle(self.entity_id)
338 }
339
340 pub fn observe<E: Send + Sync + 'static>(
341 &mut self,
342 handle: &Handle<E>,
343 on_notify: impl Fn(&mut S, Handle<E>, &mut ViewContext<'_, '_, S>) + Send + Sync + 'static,
344 ) {
345 let this = self.handle();
346 let handle = handle.downgrade();
347 let window_handle = self.window.handle;
348 self.app
349 .observers
350 .entry(handle.id)
351 .or_default()
352 .push(Arc::new(move |cx| {
353 cx.update_window(window_handle.id, |cx| {
354 if let Some(handle) = handle.upgrade(cx) {
355 this.update(cx, |this, cx| on_notify(this, handle, cx))
356 .is_ok()
357 } else {
358 false
359 }
360 })
361 .unwrap_or(false)
362 }));
363 }
364
365 pub fn notify(&mut self) {
366 self.window_cx.notify();
367 self.window_cx
368 .app
369 .pending_effects
370 .push_back(Effect::Notify(self.entity_id));
371 }
372
373 pub(crate) fn erase_state<R>(&mut self, f: impl FnOnce(&mut ViewContext<()>) -> R) -> R {
374 let entity_id = self.unit_entity.id;
375 let mut cx = ViewContext::mutable(
376 &mut *self.window_cx.app,
377 &mut *self.window_cx.window,
378 entity_id,
379 );
380 f(&mut cx)
381 }
382}
383
384impl<'a, 'w, S> Context for ViewContext<'a, 'w, S> {
385 type EntityContext<'b, 'c, U: 'static + Send + Sync> = ViewContext<'b, 'c, U>;
386 type Result<U> = U;
387
388 fn entity<T2: Send + Sync + 'static>(
389 &mut self,
390 build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T2>) -> T2,
391 ) -> Handle<T2> {
392 self.window_cx.entity(build_entity)
393 }
394
395 fn update_entity<U: Send + Sync + 'static, R>(
396 &mut self,
397 handle: &Handle<U>,
398 update: impl FnOnce(&mut U, &mut Self::EntityContext<'_, '_, U>) -> R,
399 ) -> R {
400 self.window_cx.update_entity(handle, update)
401 }
402}
403
404impl<'a, 'w, S: 'static> std::ops::Deref for ViewContext<'a, 'w, S> {
405 type Target = WindowContext<'a, 'w>;
406
407 fn deref(&self) -> &Self::Target {
408 &self.window_cx
409 }
410}
411
412impl<'a, 'w, S: 'static> std::ops::DerefMut for ViewContext<'a, 'w, S> {
413 fn deref_mut(&mut self) -> &mut Self::Target {
414 &mut self.window_cx
415 }
416}
417
418// #[derive(Clone, Copy, Eq, PartialEq, Hash)]
419slotmap::new_key_type! { pub struct WindowId; }
420
421#[derive(PartialEq, Eq)]
422pub struct WindowHandle<S> {
423 id: WindowId,
424 state_type: PhantomData<S>,
425}
426
427impl<S> Copy for WindowHandle<S> {}
428
429impl<S> Clone for WindowHandle<S> {
430 fn clone(&self) -> Self {
431 WindowHandle {
432 id: self.id,
433 state_type: PhantomData,
434 }
435 }
436}
437
438impl<S> WindowHandle<S> {
439 pub fn new(id: WindowId) -> Self {
440 WindowHandle {
441 id,
442 state_type: PhantomData,
443 }
444 }
445}
446
447impl<S: 'static> Into<AnyWindowHandle> for WindowHandle<S> {
448 fn into(self) -> AnyWindowHandle {
449 AnyWindowHandle {
450 id: self.id,
451 state_type: TypeId::of::<S>(),
452 }
453 }
454}
455
456#[derive(Copy, Clone, PartialEq, Eq)]
457pub struct AnyWindowHandle {
458 pub(crate) id: WindowId,
459 state_type: TypeId,
460}
461
462#[derive(Clone)]
463pub struct Layout {
464 pub order: u32,
465 pub bounds: Bounds<Pixels>,
466}