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::{vec2f, Vector2F},
16 },
17 text_layout::{LineLayout, RunStyle},
18 AnyAction, ClipboardItem, Menu, Scene,
19};
20use anyhow::Result;
21use async_task::Runnable;
22pub use event::Event;
23use std::{
24 any::Any,
25 path::{Path, PathBuf},
26 rc::Rc,
27 sync::Arc,
28};
29use time::UtcOffset;
30
31pub trait Platform: Send + Sync {
32 fn dispatcher(&self) -> Arc<dyn Dispatcher>;
33 fn fonts(&self) -> Arc<dyn FontSystem>;
34
35 fn activate(&self, ignoring_other_apps: bool);
36 fn open_window(
37 &self,
38 id: usize,
39 options: WindowOptions,
40 executor: Rc<executor::Foreground>,
41 ) -> Box<dyn Window>;
42 fn key_window_id(&self) -> Option<usize>;
43 fn quit(&self);
44
45 fn write_to_clipboard(&self, item: ClipboardItem);
46 fn read_from_clipboard(&self) -> Option<ClipboardItem>;
47 fn open_url(&self, url: &str);
48
49 fn write_credentials(&self, url: &str, username: &str, password: &[u8]) -> Result<()>;
50 fn read_credentials(&self, url: &str) -> Result<Option<(String, Vec<u8>)>>;
51 fn delete_credentials(&self, url: &str) -> Result<()>;
52
53 fn set_cursor_style(&self, style: CursorStyle);
54
55 fn local_timezone(&self) -> UtcOffset;
56}
57
58pub(crate) trait ForegroundPlatform {
59 fn on_become_active(&self, callback: Box<dyn FnMut()>);
60 fn on_resign_active(&self, callback: Box<dyn FnMut()>);
61 fn on_event(&self, callback: Box<dyn FnMut(Event) -> bool>);
62 fn on_open_files(&self, callback: Box<dyn FnMut(Vec<PathBuf>)>);
63 fn run(&self, on_finish_launching: Box<dyn FnOnce() -> ()>);
64
65 fn on_menu_command(&self, callback: Box<dyn FnMut(&dyn AnyAction)>);
66 fn set_menus(&self, menus: Vec<Menu>);
67 fn prompt_for_paths(
68 &self,
69 options: PathPromptOptions,
70 done_fn: Box<dyn FnOnce(Option<Vec<std::path::PathBuf>>)>,
71 );
72 fn prompt_for_new_path(
73 &self,
74 directory: &Path,
75 done_fn: Box<dyn FnOnce(Option<std::path::PathBuf>)>,
76 );
77}
78
79pub trait Dispatcher: Send + Sync {
80 fn is_main_thread(&self) -> bool;
81 fn run_on_main_thread(&self, task: Runnable);
82}
83
84pub trait Window: WindowContext {
85 fn as_any_mut(&mut self) -> &mut dyn Any;
86 fn on_event(&mut self, callback: Box<dyn FnMut(Event)>);
87 fn on_resize(&mut self, callback: Box<dyn FnMut()>);
88 fn on_close(&mut self, callback: Box<dyn FnOnce()>);
89 fn prompt(
90 &self,
91 level: PromptLevel,
92 msg: &str,
93 answers: &[&str],
94 done_fn: Box<dyn FnOnce(usize)>,
95 );
96}
97
98pub trait WindowContext {
99 fn size(&self) -> Vector2F;
100 fn scale_factor(&self) -> f32;
101 fn titlebar_height(&self) -> f32;
102 fn present_scene(&mut self, scene: Scene);
103}
104
105pub struct WindowOptions<'a> {
106 pub bounds: RectF,
107 pub title: Option<&'a str>,
108 pub titlebar_appears_transparent: bool,
109 pub traffic_light_position: Option<Vector2F>,
110}
111
112pub struct PathPromptOptions {
113 pub files: bool,
114 pub directories: bool,
115 pub multiple: bool,
116}
117
118pub enum PromptLevel {
119 Info,
120 Warning,
121 Critical,
122}
123
124#[derive(Copy, Clone, Debug)]
125pub enum CursorStyle {
126 Arrow,
127 ResizeLeftRight,
128 PointingHand,
129}
130
131pub trait FontSystem: Send + Sync {
132 fn add_fonts(&self, fonts: &[Arc<Vec<u8>>]) -> anyhow::Result<()>;
133 fn load_family(&self, name: &str) -> anyhow::Result<Vec<FontId>>;
134 fn select_font(
135 &self,
136 font_ids: &[FontId],
137 properties: &FontProperties,
138 ) -> anyhow::Result<FontId>;
139 fn font_metrics(&self, font_id: FontId) -> FontMetrics;
140 fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<RectF>;
141 fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId>;
142 fn rasterize_glyph(
143 &self,
144 font_id: FontId,
145 font_size: f32,
146 glyph_id: GlyphId,
147 subpixel_shift: Vector2F,
148 scale_factor: f32,
149 ) -> Option<(RectI, Vec<u8>)>;
150 fn layout_line(&self, text: &str, font_size: f32, runs: &[(usize, RunStyle)]) -> LineLayout;
151 fn wrap_line(&self, text: &str, font_id: FontId, font_size: f32, width: f32) -> Vec<usize>;
152}
153
154impl<'a> Default for WindowOptions<'a> {
155 fn default() -> Self {
156 Self {
157 bounds: RectF::new(Default::default(), vec2f(1024.0, 768.0)),
158 title: Default::default(),
159 titlebar_appears_transparent: Default::default(),
160 traffic_light_position: Default::default(),
161 }
162 }
163}