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
52 fn set_cursor_style(&self, style: CursorStyle);
53
54 fn local_timezone(&self) -> UtcOffset;
55}
56
57pub(crate) trait ForegroundPlatform {
58 fn on_become_active(&self, callback: Box<dyn FnMut()>);
59 fn on_resign_active(&self, callback: Box<dyn FnMut()>);
60 fn on_event(&self, callback: Box<dyn FnMut(Event) -> bool>);
61 fn on_open_files(&self, callback: Box<dyn FnMut(Vec<PathBuf>)>);
62 fn run(&self, on_finish_launching: Box<dyn FnOnce() -> ()>);
63
64 fn on_menu_command(&self, callback: Box<dyn FnMut(&dyn AnyAction)>);
65 fn set_menus(&self, menus: Vec<Menu>);
66 fn prompt_for_paths(
67 &self,
68 options: PathPromptOptions,
69 done_fn: Box<dyn FnOnce(Option<Vec<std::path::PathBuf>>)>,
70 );
71 fn prompt_for_new_path(
72 &self,
73 directory: &Path,
74 done_fn: Box<dyn FnOnce(Option<std::path::PathBuf>)>,
75 );
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(
89 &self,
90 level: PromptLevel,
91 msg: &str,
92 answers: &[&str],
93 done_fn: Box<dyn FnOnce(usize)>,
94 );
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
104pub struct WindowOptions<'a> {
105 pub bounds: RectF,
106 pub title: Option<&'a str>,
107 pub titlebar_appears_transparent: bool,
108 pub traffic_light_position: Option<Vector2F>,
109}
110
111pub struct PathPromptOptions {
112 pub files: bool,
113 pub directories: bool,
114 pub multiple: bool,
115}
116
117pub enum PromptLevel {
118 Info,
119 Warning,
120 Critical,
121}
122
123#[derive(Copy, Clone, Debug)]
124pub enum CursorStyle {
125 Arrow,
126 ResizeLeftRight,
127 PointingHand,
128}
129
130pub trait FontSystem: Send + Sync {
131 fn add_fonts(&self, fonts: &[Arc<Vec<u8>>]) -> anyhow::Result<()>;
132 fn load_family(&self, name: &str) -> anyhow::Result<Vec<FontId>>;
133 fn select_font(
134 &self,
135 font_ids: &[FontId],
136 properties: &FontProperties,
137 ) -> anyhow::Result<FontId>;
138 fn font_metrics(&self, font_id: FontId) -> FontMetrics;
139 fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<RectF>;
140 fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId>;
141 fn rasterize_glyph(
142 &self,
143 font_id: FontId,
144 font_size: f32,
145 glyph_id: GlyphId,
146 subpixel_shift: Vector2F,
147 scale_factor: f32,
148 ) -> Option<(RectI, Vec<u8>)>;
149 fn layout_line(&self, text: &str, font_size: f32, runs: &[(usize, RunStyle)]) -> LineLayout;
150 fn wrap_line(&self, text: &str, font_id: FontId, font_size: f32, width: f32) -> Vec<usize>;
151}
152
153impl<'a> Default for WindowOptions<'a> {
154 fn default() -> Self {
155 Self {
156 bounds: RectF::new(Default::default(), vec2f(1024.0, 768.0)),
157 title: Default::default(),
158 titlebar_appears_transparent: Default::default(),
159 traffic_light_position: Default::default(),
160 }
161 }
162}