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::Result;
21use async_task::Runnable;
22pub use event::Event;
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 AnyAction)>);
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}
90
91pub trait WindowContext {
92 fn size(&self) -> Vector2F;
93 fn scale_factor(&self) -> f32;
94 fn titlebar_height(&self) -> f32;
95 fn present_scene(&mut self, scene: Scene);
96}
97
98#[derive(Debug)]
99pub struct WindowOptions<'a> {
100 pub bounds: WindowBounds,
101 pub title: Option<&'a str>,
102 pub titlebar_appears_transparent: bool,
103 pub traffic_light_position: Option<Vector2F>,
104}
105
106#[derive(Debug)]
107pub enum WindowBounds {
108 Maximized,
109 Fixed(RectF),
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 advance(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<Vector2F>;
142 fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId>;
143 fn rasterize_glyph(
144 &self,
145 font_id: FontId,
146 font_size: f32,
147 glyph_id: GlyphId,
148 subpixel_shift: Vector2F,
149 scale_factor: f32,
150 ) -> Option<(RectI, Vec<u8>)>;
151 fn layout_line(&self, text: &str, font_size: f32, runs: &[(usize, RunStyle)]) -> LineLayout;
152 fn wrap_line(&self, text: &str, font_id: FontId, font_size: f32, width: f32) -> Vec<usize>;
153}
154
155impl<'a> Default for WindowOptions<'a> {
156 fn default() -> Self {
157 Self {
158 bounds: WindowBounds::Maximized,
159 title: Default::default(),
160 titlebar_appears_transparent: Default::default(),
161 traffic_light_position: Default::default(),
162 }
163 }
164}