1mod event;
2#[cfg(target_os = "macos")]
3pub mod mac;
4pub mod current {
5 #[cfg(target_os = "macos")]
6 pub use super::mac::*;
7}
8
9use crate::{
10 executor,
11 fonts::{FontId, GlyphId, Metrics as FontMetrics, Properties as FontProperties},
12 geometry::{
13 rect::{RectF, RectI},
14 vector::Vector2F,
15 },
16 text_layout::Line,
17 Scene,
18};
19use anyhow::Result;
20use async_task::Runnable;
21pub use event::Event;
22use std::{ops::Range, path::PathBuf, rc::Rc, sync::Arc};
23
24pub trait Runner {
25 fn on_finish_launching<F: 'static + FnOnce()>(self, callback: F) -> Self where;
26 fn on_become_active<F: 'static + FnMut()>(self, callback: F) -> Self;
27 fn on_resign_active<F: 'static + FnMut()>(self, callback: F) -> Self;
28 fn on_event<F: 'static + FnMut(Event) -> bool>(self, callback: F) -> Self;
29 fn on_open_files<F: 'static + FnMut(Vec<PathBuf>)>(self, callback: F) -> Self;
30 fn run(self);
31}
32
33pub trait App {
34 fn dispatcher(&self) -> Arc<dyn Dispatcher>;
35 fn activate(&self, ignoring_other_apps: bool);
36 fn open_window(
37 &self,
38 options: WindowOptions,
39 executor: Rc<executor::Foreground>,
40 ) -> Result<Box<dyn Window>>;
41 fn fonts(&self) -> Arc<dyn FontSystem>;
42}
43
44pub trait Dispatcher: Send + Sync {
45 fn is_main_thread(&self) -> bool;
46 fn run_on_main_thread(&self, task: Runnable);
47}
48
49pub trait Window: WindowContext {
50 fn on_event(&mut self, callback: Box<dyn FnMut(Event)>);
51 fn on_resize(&mut self, callback: Box<dyn FnMut(&mut dyn WindowContext)>);
52}
53
54pub trait WindowContext {
55 fn size(&self) -> Vector2F;
56 fn scale_factor(&self) -> f32;
57 fn present_scene(&mut self, scene: Scene);
58}
59
60pub struct WindowOptions<'a> {
61 pub bounds: RectF,
62 pub title: Option<&'a str>,
63}
64
65pub trait FontSystem: Send + Sync {
66 fn load_family(&self, name: &str) -> anyhow::Result<Vec<FontId>>;
67 fn select_font(
68 &self,
69 font_ids: &[FontId],
70 properties: &FontProperties,
71 ) -> anyhow::Result<FontId>;
72 fn font_metrics(&self, font_id: FontId) -> FontMetrics;
73 fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<RectF>;
74 fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId>;
75 fn rasterize_glyph(
76 &self,
77 font_id: FontId,
78 font_size: f32,
79 glyph_id: GlyphId,
80 subpixel_shift: Vector2F,
81 scale_factor: f32,
82 ) -> Option<(RectI, Vec<u8>)>;
83 fn layout_str(&self, text: &str, font_size: f32, runs: &[(Range<usize>, FontId)]) -> Line;
84}