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