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::{LineLayout, RunStyle},
18 Action, ClipboardItem, Menu, Scene,
19};
20use anyhow::{anyhow, Result};
21use async_task::Runnable;
22pub use event::{Event, NavigationDirection};
23use postage::oneshot;
24use serde::Deserialize;
25use std::{
26 any::Any,
27 path::{Path, PathBuf},
28 rc::Rc,
29 str::FromStr,
30 sync::Arc,
31};
32use time::UtcOffset;
33
34pub trait Platform: Send + Sync {
35 fn dispatcher(&self) -> Arc<dyn Dispatcher>;
36 fn fonts(&self) -> Arc<dyn FontSystem>;
37
38 fn activate(&self, ignoring_other_apps: bool);
39 fn open_window(
40 &self,
41 id: usize,
42 options: WindowOptions,
43 executor: Rc<executor::Foreground>,
44 ) -> Box<dyn Window>;
45 fn key_window_id(&self) -> Option<usize>;
46 fn quit(&self);
47
48 fn write_to_clipboard(&self, item: ClipboardItem);
49 fn read_from_clipboard(&self) -> Option<ClipboardItem>;
50 fn open_url(&self, url: &str);
51
52 fn write_credentials(&self, url: &str, username: &str, password: &[u8]) -> Result<()>;
53 fn read_credentials(&self, url: &str) -> Result<Option<(String, Vec<u8>)>>;
54 fn delete_credentials(&self, url: &str) -> Result<()>;
55
56 fn set_cursor_style(&self, style: CursorStyle);
57
58 fn local_timezone(&self) -> UtcOffset;
59
60 fn path_for_auxiliary_executable(&self, name: &str) -> Result<PathBuf>;
61 fn app_path(&self) -> Result<PathBuf>;
62 fn app_version(&self) -> Result<AppVersion>;
63}
64
65pub(crate) trait ForegroundPlatform {
66 fn on_become_active(&self, callback: Box<dyn FnMut()>);
67 fn on_resign_active(&self, callback: Box<dyn FnMut()>);
68 fn on_quit(&self, callback: Box<dyn FnMut()>);
69 fn on_event(&self, callback: Box<dyn FnMut(Event) -> bool>);
70 fn on_open_urls(&self, callback: Box<dyn FnMut(Vec<String>)>);
71 fn run(&self, on_finish_launching: Box<dyn FnOnce() -> ()>);
72
73 fn on_menu_command(&self, callback: Box<dyn FnMut(&dyn Action)>);
74 fn set_menus(&self, menus: Vec<Menu>);
75 fn prompt_for_paths(
76 &self,
77 options: PathPromptOptions,
78 ) -> oneshot::Receiver<Option<Vec<PathBuf>>>;
79 fn prompt_for_new_path(&self, directory: &Path) -> oneshot::Receiver<Option<PathBuf>>;
80}
81
82pub trait Dispatcher: Send + Sync {
83 fn is_main_thread(&self) -> bool;
84 fn run_on_main_thread(&self, task: Runnable);
85}
86
87pub trait Window: WindowContext {
88 fn as_any_mut(&mut self) -> &mut dyn Any;
89 fn on_event(&mut self, callback: Box<dyn FnMut(Event)>);
90 fn on_active_status_change(&mut self, callback: Box<dyn FnMut(bool)>);
91 fn on_resize(&mut self, callback: Box<dyn FnMut()>);
92 fn on_close(&mut self, callback: Box<dyn FnOnce()>);
93 fn prompt(&self, level: PromptLevel, msg: &str, answers: &[&str]) -> oneshot::Receiver<usize>;
94 fn activate(&self);
95}
96
97pub trait WindowContext {
98 fn size(&self) -> Vector2F;
99 fn scale_factor(&self) -> f32;
100 fn titlebar_height(&self) -> f32;
101 fn present_scene(&mut self, scene: Scene);
102}
103
104#[derive(Debug)]
105pub struct WindowOptions<'a> {
106 pub bounds: WindowBounds,
107 pub title: Option<&'a str>,
108 pub titlebar_appears_transparent: bool,
109 pub traffic_light_position: Option<Vector2F>,
110}
111
112#[derive(Debug)]
113pub enum WindowBounds {
114 Maximized,
115 Fixed(RectF),
116}
117
118pub struct PathPromptOptions {
119 pub files: bool,
120 pub directories: bool,
121 pub multiple: bool,
122}
123
124pub enum PromptLevel {
125 Info,
126 Warning,
127 Critical,
128}
129
130#[derive(Copy, Clone, Debug, Deserialize)]
131pub enum CursorStyle {
132 Arrow,
133 ResizeLeftRight,
134 PointingHand,
135 IBeam,
136}
137
138#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
139pub struct AppVersion {
140 major: usize,
141 minor: usize,
142 patch: usize,
143}
144
145impl FromStr for AppVersion {
146 type Err = anyhow::Error;
147
148 fn from_str(s: &str) -> Result<Self> {
149 let mut components = s.trim().split('.');
150 let major = components
151 .next()
152 .ok_or_else(|| anyhow!("missing major version number"))?
153 .parse()?;
154 let minor = components
155 .next()
156 .ok_or_else(|| anyhow!("missing minor version number"))?
157 .parse()?;
158 let patch = components
159 .next()
160 .ok_or_else(|| anyhow!("missing patch version number"))?
161 .parse()?;
162 Ok(Self {
163 major,
164 minor,
165 patch,
166 })
167 }
168}
169
170#[derive(Copy, Clone, Debug)]
171pub enum RasterizationOptions {
172 Alpha,
173 Bgra,
174}
175
176pub trait FontSystem: Send + Sync {
177 fn add_fonts(&self, fonts: &[Arc<Vec<u8>>]) -> anyhow::Result<()>;
178 fn load_family(&self, name: &str) -> anyhow::Result<Vec<FontId>>;
179 fn select_font(
180 &self,
181 font_ids: &[FontId],
182 properties: &FontProperties,
183 ) -> anyhow::Result<FontId>;
184 fn font_metrics(&self, font_id: FontId) -> FontMetrics;
185 fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<RectF>;
186 fn advance(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<Vector2F>;
187 fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId>;
188 fn rasterize_glyph(
189 &self,
190 font_id: FontId,
191 font_size: f32,
192 glyph_id: GlyphId,
193 subpixel_shift: Vector2F,
194 scale_factor: f32,
195 options: RasterizationOptions,
196 ) -> Option<(RectI, Vec<u8>)>;
197 fn layout_line(&self, text: &str, font_size: f32, runs: &[(usize, RunStyle)]) -> LineLayout;
198 fn wrap_line(&self, text: &str, font_id: FontId, font_size: f32, width: f32) -> Vec<usize>;
199}
200
201impl<'a> Default for WindowOptions<'a> {
202 fn default() -> Self {
203 Self {
204 bounds: WindowBounds::Maximized,
205 title: Default::default(),
206 titlebar_appears_transparent: Default::default(),
207 traffic_light_position: Default::default(),
208 }
209 }
210}