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