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