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 anyhow::Result;
 22use async_task::Runnable;
 23pub use event::Event;
 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
 53    fn set_cursor_style(&self, style: CursorStyle);
 54
 55    fn local_timezone(&self) -> UtcOffset;
 56}
 57
 58pub(crate) trait ForegroundPlatform {
 59    fn on_become_active(&self, callback: Box<dyn FnMut()>);
 60    fn on_resign_active(&self, callback: Box<dyn FnMut()>);
 61    fn on_event(&self, callback: Box<dyn FnMut(Event) -> bool>);
 62    fn on_open_files(&self, callback: Box<dyn FnMut(Vec<PathBuf>)>);
 63    fn run(&self, on_finish_launching: Box<dyn FnOnce() -> ()>);
 64
 65    fn on_menu_command(&self, callback: Box<dyn FnMut(&dyn AnyAction)>);
 66    fn set_menus(&self, menus: Vec<Menu>);
 67    fn prompt_for_paths(
 68        &self,
 69        options: PathPromptOptions,
 70        done_fn: Box<dyn FnOnce(Option<Vec<std::path::PathBuf>>)>,
 71    );
 72    fn prompt_for_new_path(
 73        &self,
 74        directory: &Path,
 75        done_fn: Box<dyn FnOnce(Option<std::path::PathBuf>)>,
 76    );
 77}
 78
 79pub trait Dispatcher: Send + Sync {
 80    fn is_main_thread(&self) -> bool;
 81    fn run_on_main_thread(&self, task: Runnable);
 82}
 83
 84pub trait Window: WindowContext {
 85    fn as_any_mut(&mut self) -> &mut dyn Any;
 86    fn on_event(&mut self, callback: Box<dyn FnMut(Event)>);
 87    fn on_resize(&mut self, callback: Box<dyn FnMut()>);
 88    fn on_close(&mut self, callback: Box<dyn FnOnce()>);
 89    fn prompt(
 90        &self,
 91        level: PromptLevel,
 92        msg: &str,
 93        answers: &[&str],
 94        done_fn: Box<dyn FnOnce(usize)>,
 95    );
 96}
 97
 98pub trait WindowContext {
 99    fn size(&self) -> Vector2F;
100    fn scale_factor(&self) -> f32;
101    fn titlebar_height(&self) -> f32;
102    fn present_scene(&mut self, scene: Scene);
103}
104
105pub struct WindowOptions<'a> {
106    pub bounds: RectF,
107    pub title: Option<&'a str>,
108    pub titlebar_appears_transparent: bool,
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(
150        &self,
151        text: &str,
152        font_size: f32,
153        runs: &[(usize, FontId, Color)],
154    ) -> LineLayout;
155    fn wrap_line(&self, text: &str, font_id: FontId, font_size: f32, width: f32) -> Vec<usize>;
156}
157
158impl<'a> Default for WindowOptions<'a> {
159    fn default() -> Self {
160        Self {
161            bounds: RectF::new(Default::default(), vec2f(1024.0, 768.0)),
162            title: Default::default(),
163            titlebar_appears_transparent: Default::default(),
164        }
165    }
166}