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