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 fn delete_credentials(&self, url: &str) -> Result<()>;
52
53 fn set_cursor_style(&self, style: CursorStyle);
54
55 fn local_timezone(&self) -> UtcOffset;
56
57 fn path_for_resource(&self, name: Option<&str>, extension: Option<&str>) -> Result<PathBuf>;
58}
59
60pub(crate) trait ForegroundPlatform {
61 fn on_become_active(&self, callback: Box<dyn FnMut()>);
62 fn on_resign_active(&self, callback: Box<dyn FnMut()>);
63 fn on_quit(&self, callback: Box<dyn FnMut()>);
64 fn on_event(&self, callback: Box<dyn FnMut(Event) -> bool>);
65 fn on_open_files(&self, callback: Box<dyn FnMut(Vec<PathBuf>)>);
66 fn run(&self, on_finish_launching: Box<dyn FnOnce() -> ()>);
67
68 fn on_menu_command(&self, callback: Box<dyn FnMut(&dyn AnyAction)>);
69 fn set_menus(&self, menus: Vec<Menu>);
70 fn prompt_for_paths(
71 &self,
72 options: PathPromptOptions,
73 done_fn: Box<dyn FnOnce(Option<Vec<std::path::PathBuf>>)>,
74 );
75 fn prompt_for_new_path(
76 &self,
77 directory: &Path,
78 done_fn: Box<dyn FnOnce(Option<std::path::PathBuf>)>,
79 );
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_resize(&mut self, callback: Box<dyn FnMut()>);
91 fn on_close(&mut self, callback: Box<dyn FnOnce()>);
92 fn prompt(
93 &self,
94 level: PromptLevel,
95 msg: &str,
96 answers: &[&str],
97 done_fn: Box<dyn FnOnce(usize)>,
98 );
99}
100
101pub trait WindowContext {
102 fn size(&self) -> Vector2F;
103 fn scale_factor(&self) -> f32;
104 fn titlebar_height(&self) -> f32;
105 fn present_scene(&mut self, scene: Scene);
106}
107
108pub struct WindowOptions<'a> {
109 pub bounds: RectF,
110 pub title: Option<&'a str>,
111 pub titlebar_appears_transparent: bool,
112 pub traffic_light_position: Option<Vector2F>,
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
134pub trait FontSystem: Send + Sync {
135 fn add_fonts(&self, fonts: &[Arc<Vec<u8>>]) -> anyhow::Result<()>;
136 fn load_family(&self, name: &str) -> anyhow::Result<Vec<FontId>>;
137 fn select_font(
138 &self,
139 font_ids: &[FontId],
140 properties: &FontProperties,
141 ) -> anyhow::Result<FontId>;
142 fn font_metrics(&self, font_id: FontId) -> FontMetrics;
143 fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<RectF>;
144 fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId>;
145 fn rasterize_glyph(
146 &self,
147 font_id: FontId,
148 font_size: f32,
149 glyph_id: GlyphId,
150 subpixel_shift: Vector2F,
151 scale_factor: f32,
152 ) -> Option<(RectI, Vec<u8>)>;
153 fn layout_line(&self, text: &str, font_size: f32, runs: &[(usize, RunStyle)]) -> LineLayout;
154 fn wrap_line(&self, text: &str, font_id: FontId, font_size: f32, width: f32) -> Vec<usize>;
155}
156
157impl<'a> Default for WindowOptions<'a> {
158 fn default() -> Self {
159 Self {
160 bounds: RectF::new(Default::default(), vec2f(1024.0, 768.0)),
161 title: Default::default(),
162 titlebar_appears_transparent: Default::default(),
163 traffic_light_position: Default::default(),
164 }
165 }
166}