mod.rs

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