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 color::Color,
12 executor,
13 fonts::{FontId, GlyphId, Metrics as FontMetrics, Properties as FontProperties},
14 geometry::{
15 rect::{RectF, RectI},
16 vector::Vector2F,
17 },
18 text_layout::LineLayout,
19 ClipboardItem, Menu, Scene,
20};
21use async_task::Runnable;
22pub use event::Event;
23use std::{
24 any::Any,
25 path::{Path, PathBuf},
26 rc::Rc,
27 sync::Arc,
28};
29
30pub trait Platform: Send + Sync {
31 fn dispatcher(&self) -> Arc<dyn Dispatcher>;
32 fn fonts(&self) -> Arc<dyn FontSystem>;
33
34 fn activate(&self, ignoring_other_apps: bool);
35 fn open_window(
36 &self,
37 id: usize,
38 options: WindowOptions,
39 executor: Rc<executor::Foreground>,
40 ) -> Box<dyn Window>;
41 fn key_window_id(&self) -> Option<usize>;
42 fn quit(&self);
43
44 fn write_to_clipboard(&self, item: ClipboardItem);
45 fn read_from_clipboard(&self) -> Option<ClipboardItem>;
46 fn open_url(&self, url: &str);
47
48 fn write_credentials(&self, url: &str, username: &str, password: &[u8]);
49 fn read_credentials(&self, url: &str) -> Option<(String, Vec<u8>)>;
50}
51
52pub(crate) trait ForegroundPlatform {
53 fn on_become_active(&self, callback: Box<dyn FnMut()>);
54 fn on_resign_active(&self, callback: Box<dyn FnMut()>);
55 fn on_event(&self, callback: Box<dyn FnMut(Event) -> bool>);
56 fn on_open_files(&self, callback: Box<dyn FnMut(Vec<PathBuf>)>);
57 fn run(&self, on_finish_launching: Box<dyn FnOnce() -> ()>);
58
59 fn on_menu_command(&self, callback: Box<dyn FnMut(&str, Option<&dyn Any>)>);
60 fn set_menus(&self, menus: Vec<Menu>);
61 fn prompt_for_paths(
62 &self,
63 options: PathPromptOptions,
64 done_fn: Box<dyn FnOnce(Option<Vec<std::path::PathBuf>>)>,
65 );
66 fn prompt_for_new_path(
67 &self,
68 directory: &Path,
69 done_fn: Box<dyn FnOnce(Option<std::path::PathBuf>)>,
70 );
71}
72
73pub trait Dispatcher: Send + Sync {
74 fn is_main_thread(&self) -> bool;
75 fn run_on_main_thread(&self, task: Runnable);
76}
77
78pub trait Window: WindowContext {
79 fn as_any_mut(&mut self) -> &mut dyn Any;
80 fn on_event(&mut self, callback: Box<dyn FnMut(Event)>);
81 fn on_resize(&mut self, callback: Box<dyn FnMut(&mut dyn WindowContext)>);
82 fn on_close(&mut self, callback: Box<dyn FnOnce()>);
83 fn prompt(
84 &self,
85 level: PromptLevel,
86 msg: &str,
87 answers: &[&str],
88 done_fn: Box<dyn FnOnce(usize)>,
89 );
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(Default)]
100pub struct WindowOptions<'a> {
101 pub bounds: RectF,
102 pub title: Option<&'a str>,
103 pub titlebar_appears_transparent: bool,
104}
105
106pub struct PathPromptOptions {
107 pub files: bool,
108 pub directories: bool,
109 pub multiple: bool,
110}
111
112pub enum PromptLevel {
113 Info,
114 Warning,
115 Critical,
116}
117
118pub trait FontSystem: Send + Sync {
119 fn load_family(&self, name: &str) -> anyhow::Result<Vec<FontId>>;
120 fn select_font(
121 &self,
122 font_ids: &[FontId],
123 properties: &FontProperties,
124 ) -> anyhow::Result<FontId>;
125 fn font_metrics(&self, font_id: FontId) -> FontMetrics;
126 fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<RectF>;
127 fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId>;
128 fn rasterize_glyph(
129 &self,
130 font_id: FontId,
131 font_size: f32,
132 glyph_id: GlyphId,
133 subpixel_shift: Vector2F,
134 scale_factor: f32,
135 ) -> Option<(RectI, Vec<u8>)>;
136 fn layout_line(
137 &self,
138 text: &str,
139 font_size: f32,
140 runs: &[(usize, FontId, Color)],
141 ) -> LineLayout;
142 fn wrap_line(&self, text: &str, font_id: FontId, font_size: f32, width: f32) -> Vec<usize>;
143}