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    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_event(&self, callback: Box<dyn FnMut(Event) -> bool>);
 64    fn on_open_files(&self, callback: Box<dyn FnMut(Vec<PathBuf>)>);
 65    fn run(&self, on_finish_launching: Box<dyn FnOnce() -> ()>);
 66
 67    fn on_menu_command(&self, callback: Box<dyn FnMut(&dyn AnyAction)>);
 68    fn set_menus(&self, menus: Vec<Menu>);
 69    fn prompt_for_paths(
 70        &self,
 71        options: PathPromptOptions,
 72        done_fn: Box<dyn FnOnce(Option<Vec<std::path::PathBuf>>)>,
 73    );
 74    fn prompt_for_new_path(
 75        &self,
 76        directory: &Path,
 77        done_fn: Box<dyn FnOnce(Option<std::path::PathBuf>)>,
 78    );
 79}
 80
 81pub trait Dispatcher: Send + Sync {
 82    fn is_main_thread(&self) -> bool;
 83    fn run_on_main_thread(&self, task: Runnable);
 84}
 85
 86pub trait Window: WindowContext {
 87    fn as_any_mut(&mut self) -> &mut dyn Any;
 88    fn on_event(&mut self, callback: Box<dyn FnMut(Event)>);
 89    fn on_resize(&mut self, callback: Box<dyn FnMut()>);
 90    fn on_close(&mut self, callback: Box<dyn FnOnce()>);
 91    fn prompt(
 92        &self,
 93        level: PromptLevel,
 94        msg: &str,
 95        answers: &[&str],
 96        done_fn: Box<dyn FnOnce(usize)>,
 97    );
 98}
 99
100pub trait WindowContext {
101    fn size(&self) -> Vector2F;
102    fn scale_factor(&self) -> f32;
103    fn titlebar_height(&self) -> f32;
104    fn present_scene(&mut self, scene: Scene);
105}
106
107pub struct WindowOptions<'a> {
108    pub bounds: RectF,
109    pub title: Option<&'a str>,
110    pub titlebar_appears_transparent: bool,
111    pub traffic_light_position: Option<Vector2F>,
112}
113
114pub struct PathPromptOptions {
115    pub files: bool,
116    pub directories: bool,
117    pub multiple: bool,
118}
119
120pub enum PromptLevel {
121    Info,
122    Warning,
123    Critical,
124}
125
126#[derive(Copy, Clone, Debug)]
127pub enum CursorStyle {
128    Arrow,
129    ResizeLeftRight,
130    PointingHand,
131}
132
133pub trait FontSystem: Send + Sync {
134    fn add_fonts(&self, fonts: &[Arc<Vec<u8>>]) -> anyhow::Result<()>;
135    fn load_family(&self, name: &str) -> anyhow::Result<Vec<FontId>>;
136    fn select_font(
137        &self,
138        font_ids: &[FontId],
139        properties: &FontProperties,
140    ) -> anyhow::Result<FontId>;
141    fn font_metrics(&self, font_id: FontId) -> FontMetrics;
142    fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<RectF>;
143    fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId>;
144    fn rasterize_glyph(
145        &self,
146        font_id: FontId,
147        font_size: f32,
148        glyph_id: GlyphId,
149        subpixel_shift: Vector2F,
150        scale_factor: f32,
151    ) -> Option<(RectI, Vec<u8>)>;
152    fn layout_line(&self, text: &str, font_size: f32, runs: &[(usize, RunStyle)]) -> LineLayout;
153    fn wrap_line(&self, text: &str, font_id: FontId, font_size: f32, width: f32) -> Vec<usize>;
154}
155
156impl<'a> Default for WindowOptions<'a> {
157    fn default() -> Self {
158        Self {
159            bounds: RectF::new(Default::default(), vec2f(1024.0, 768.0)),
160            title: Default::default(),
161            titlebar_appears_transparent: Default::default(),
162            traffic_light_position: Default::default(),
163        }
164    }
165}