platform.rs

  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::{vec2f, Vector2F},
 17    },
 18    text_layout::LineLayout,
 19    AnyAction, 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};
 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]);
 50    fn read_credentials(&self, url: &str) -> 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}
109
110pub struct PathPromptOptions {
111    pub files: bool,
112    pub directories: bool,
113    pub multiple: bool,
114}
115
116pub enum PromptLevel {
117    Info,
118    Warning,
119    Critical,
120}
121
122#[derive(Copy, Clone, Debug)]
123pub enum CursorStyle {
124    Arrow,
125    ResizeLeftRight,
126    PointingHand,
127}
128
129pub trait FontSystem: Send + Sync {
130    fn add_fonts(&self, fonts: Vec<Arc<Vec<u8>>>) -> anyhow::Result<()>;
131    fn load_family(&self, name: &str) -> anyhow::Result<Vec<FontId>>;
132    fn select_font(
133        &self,
134        font_ids: &[FontId],
135        properties: &FontProperties,
136    ) -> anyhow::Result<FontId>;
137    fn font_metrics(&self, font_id: FontId) -> FontMetrics;
138    fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<RectF>;
139    fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId>;
140    fn rasterize_glyph(
141        &self,
142        font_id: FontId,
143        font_size: f32,
144        glyph_id: GlyphId,
145        subpixel_shift: Vector2F,
146        scale_factor: f32,
147    ) -> Option<(RectI, Vec<u8>)>;
148    fn layout_line(
149        &self,
150        text: &str,
151        font_size: f32,
152        runs: &[(usize, FontId, Color)],
153    ) -> 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        }
164    }
165}