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::{
 13        Features as FontFeatures, FontId, GlyphId, Metrics as FontMetrics,
 14        Properties as FontProperties,
 15    },
 16    geometry::{
 17        rect::{RectF, RectI},
 18        vector::Vector2F,
 19    },
 20    keymap_matcher::KeymapMatcher,
 21    text_layout::{LineLayout, RunStyle},
 22    Action, ClipboardItem, Menu, Scene,
 23};
 24use anyhow::{anyhow, bail, Result};
 25use async_task::Runnable;
 26pub use event::*;
 27use postage::oneshot;
 28use serde::Deserialize;
 29use sqlez::{
 30    bindable::{Bind, Column, StaticColumnCount},
 31    statement::Statement,
 32};
 33use std::{
 34    any::Any,
 35    fmt::{self, Debug, Display},
 36    ops::Range,
 37    path::{Path, PathBuf},
 38    rc::Rc,
 39    str::FromStr,
 40    sync::Arc,
 41};
 42use time::UtcOffset;
 43use uuid::Uuid;
 44
 45pub trait Platform: Send + Sync {
 46    fn dispatcher(&self) -> Arc<dyn Dispatcher>;
 47    fn fonts(&self) -> Arc<dyn FontSystem>;
 48
 49    fn activate(&self, ignoring_other_apps: bool);
 50    fn hide(&self);
 51    fn hide_other_apps(&self);
 52    fn unhide_other_apps(&self);
 53    fn quit(&self);
 54
 55    fn screen_by_id(&self, id: Uuid) -> Option<Rc<dyn Screen>>;
 56    fn screens(&self) -> Vec<Rc<dyn Screen>>;
 57
 58    fn open_window(
 59        &self,
 60        id: usize,
 61        options: WindowOptions,
 62        executor: Rc<executor::Foreground>,
 63    ) -> Box<dyn Window>;
 64    fn main_window_id(&self) -> Option<usize>;
 65
 66    fn add_status_item(&self, id: usize) -> Box<dyn Window>;
 67
 68    fn write_to_clipboard(&self, item: ClipboardItem);
 69    fn read_from_clipboard(&self) -> Option<ClipboardItem>;
 70    fn open_url(&self, url: &str);
 71
 72    fn write_credentials(&self, url: &str, username: &str, password: &[u8]) -> Result<()>;
 73    fn read_credentials(&self, url: &str) -> Result<Option<(String, Vec<u8>)>>;
 74    fn delete_credentials(&self, url: &str) -> Result<()>;
 75
 76    fn set_cursor_style(&self, style: CursorStyle);
 77    fn should_auto_hide_scrollbars(&self) -> bool;
 78
 79    fn local_timezone(&self) -> UtcOffset;
 80
 81    fn path_for_auxiliary_executable(&self, name: &str) -> Result<PathBuf>;
 82    fn app_path(&self) -> Result<PathBuf>;
 83    fn app_version(&self) -> Result<AppVersion>;
 84    fn os_name(&self) -> &'static str;
 85    fn os_version(&self) -> Result<AppVersion>;
 86    fn restart(&self);
 87}
 88
 89pub(crate) trait ForegroundPlatform {
 90    fn on_become_active(&self, callback: Box<dyn FnMut()>);
 91    fn on_resign_active(&self, callback: Box<dyn FnMut()>);
 92    fn on_quit(&self, callback: Box<dyn FnMut()>);
 93
 94    /// Handle the application being re-activated with no windows open.
 95    fn on_reopen(&self, callback: Box<dyn FnMut()>);
 96
 97    fn on_event(&self, callback: Box<dyn FnMut(Event) -> bool>);
 98    fn on_open_urls(&self, callback: Box<dyn FnMut(Vec<String>)>);
 99    fn run(&self, on_finish_launching: Box<dyn FnOnce()>);
100
101    fn on_menu_command(&self, callback: Box<dyn FnMut(&dyn Action)>);
102    fn on_validate_menu_command(&self, callback: Box<dyn FnMut(&dyn Action) -> bool>);
103    fn on_will_open_menu(&self, callback: Box<dyn FnMut()>);
104    fn set_menus(&self, menus: Vec<Menu>, matcher: &KeymapMatcher);
105    fn prompt_for_paths(
106        &self,
107        options: PathPromptOptions,
108    ) -> oneshot::Receiver<Option<Vec<PathBuf>>>;
109    fn prompt_for_new_path(&self, directory: &Path) -> oneshot::Receiver<Option<PathBuf>>;
110    fn reveal_path(&self, path: &Path);
111}
112
113pub trait Dispatcher: Send + Sync {
114    fn is_main_thread(&self) -> bool;
115    fn run_on_main_thread(&self, task: Runnable);
116}
117
118pub trait InputHandler {
119    fn selected_text_range(&self) -> Option<Range<usize>>;
120    fn marked_text_range(&self) -> Option<Range<usize>>;
121    fn text_for_range(&self, range_utf16: Range<usize>) -> Option<String>;
122    fn replace_text_in_range(&mut self, replacement_range: Option<Range<usize>>, text: &str);
123    fn replace_and_mark_text_in_range(
124        &mut self,
125        range_utf16: Option<Range<usize>>,
126        new_text: &str,
127        new_selected_range: Option<Range<usize>>,
128    );
129    fn unmark_text(&mut self);
130    fn rect_for_range(&self, range_utf16: Range<usize>) -> Option<RectF>;
131}
132
133pub trait Screen: Debug {
134    fn as_any(&self) -> &dyn Any;
135    fn bounds(&self) -> RectF;
136    fn display_uuid(&self) -> Option<Uuid>;
137}
138
139pub trait Window {
140    fn bounds(&self) -> WindowBounds;
141    fn content_size(&self) -> Vector2F;
142    fn scale_factor(&self) -> f32;
143    fn titlebar_height(&self) -> f32;
144    fn appearance(&self) -> Appearance;
145    fn screen(&self) -> Rc<dyn Screen>;
146
147    fn as_any_mut(&mut self) -> &mut dyn Any;
148    fn set_input_handler(&mut self, input_handler: Box<dyn InputHandler>);
149    fn prompt(&self, level: PromptLevel, msg: &str, answers: &[&str]) -> oneshot::Receiver<usize>;
150    fn activate(&self);
151    fn set_title(&mut self, title: &str);
152    fn set_edited(&mut self, edited: bool);
153    fn show_character_palette(&self);
154    fn minimize(&self);
155    fn zoom(&self);
156    fn present_scene(&mut self, scene: Scene);
157    fn toggle_full_screen(&self);
158
159    fn on_event(&mut self, callback: Box<dyn FnMut(Event) -> bool>);
160    fn on_active_status_change(&mut self, callback: Box<dyn FnMut(bool)>);
161    fn on_resize(&mut self, callback: Box<dyn FnMut()>);
162    fn on_fullscreen(&mut self, callback: Box<dyn FnMut(bool)>);
163    fn on_moved(&mut self, callback: Box<dyn FnMut()>);
164    fn on_should_close(&mut self, callback: Box<dyn FnMut() -> bool>);
165    fn on_close(&mut self, callback: Box<dyn FnOnce()>);
166    fn on_appearance_changed(&mut self, callback: Box<dyn FnMut()>);
167    fn is_topmost_for_position(&self, position: Vector2F) -> bool;
168}
169
170#[derive(Debug)]
171pub struct WindowOptions<'a> {
172    pub bounds: WindowBounds,
173    pub titlebar: Option<TitlebarOptions<'a>>,
174    pub center: bool,
175    pub focus: bool,
176    pub kind: WindowKind,
177    pub is_movable: bool,
178    pub screen: Option<Rc<dyn Screen>>,
179}
180
181#[derive(Debug)]
182pub struct TitlebarOptions<'a> {
183    pub title: Option<&'a str>,
184    pub appears_transparent: bool,
185    pub traffic_light_position: Option<Vector2F>,
186}
187
188#[derive(Copy, Clone, Debug)]
189pub enum Appearance {
190    Light,
191    VibrantLight,
192    Dark,
193    VibrantDark,
194}
195
196impl Default for Appearance {
197    fn default() -> Self {
198        Self::Light
199    }
200}
201
202#[derive(Copy, Clone, Debug, PartialEq, Eq)]
203pub enum WindowKind {
204    Normal,
205    PopUp,
206}
207
208#[derive(Copy, Clone, Debug, PartialEq)]
209pub enum WindowBounds {
210    Fullscreen,
211    Maximized,
212    Fixed(RectF),
213}
214
215impl StaticColumnCount for WindowBounds {
216    fn column_count() -> usize {
217        5
218    }
219}
220
221impl Bind for WindowBounds {
222    fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
223        let (region, next_index) = match self {
224            WindowBounds::Fullscreen => {
225                let next_index = statement.bind("Fullscreen", start_index)?;
226                (None, next_index)
227            }
228            WindowBounds::Maximized => {
229                let next_index = statement.bind("Maximized", start_index)?;
230                (None, next_index)
231            }
232            WindowBounds::Fixed(region) => {
233                let next_index = statement.bind("Fixed", start_index)?;
234                (Some(*region), next_index)
235            }
236        };
237
238        statement.bind(
239            region.map(|region| {
240                (
241                    region.min_x(),
242                    region.min_y(),
243                    region.width(),
244                    region.height(),
245                )
246            }),
247            next_index,
248        )
249    }
250}
251
252impl Column for WindowBounds {
253    fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
254        let (window_state, next_index) = String::column(statement, start_index)?;
255        let bounds = match window_state.as_str() {
256            "Fullscreen" => WindowBounds::Fullscreen,
257            "Maximized" => WindowBounds::Maximized,
258            "Fixed" => {
259                let ((x, y, width, height), _) = Column::column(statement, next_index)?;
260                WindowBounds::Fixed(RectF::new(
261                    Vector2F::new(x, y),
262                    Vector2F::new(width, height),
263                ))
264            }
265            _ => bail!("Window State did not have a valid string"),
266        };
267
268        Ok((bounds, next_index + 4))
269    }
270}
271
272pub struct PathPromptOptions {
273    pub files: bool,
274    pub directories: bool,
275    pub multiple: bool,
276}
277
278pub enum PromptLevel {
279    Info,
280    Warning,
281    Critical,
282}
283
284#[derive(Copy, Clone, Debug, Deserialize)]
285pub enum CursorStyle {
286    Arrow,
287    ResizeLeftRight,
288    ResizeUpDown,
289    PointingHand,
290    IBeam,
291}
292
293impl Default for CursorStyle {
294    fn default() -> Self {
295        Self::Arrow
296    }
297}
298
299#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
300pub struct AppVersion {
301    major: usize,
302    minor: usize,
303    patch: usize,
304}
305
306impl FromStr for AppVersion {
307    type Err = anyhow::Error;
308
309    fn from_str(s: &str) -> Result<Self> {
310        let mut components = s.trim().split('.');
311        let major = components
312            .next()
313            .ok_or_else(|| anyhow!("missing major version number"))?
314            .parse()?;
315        let minor = components
316            .next()
317            .ok_or_else(|| anyhow!("missing minor version number"))?
318            .parse()?;
319        let patch = components
320            .next()
321            .ok_or_else(|| anyhow!("missing patch version number"))?
322            .parse()?;
323        Ok(Self {
324            major,
325            minor,
326            patch,
327        })
328    }
329}
330
331impl Display for AppVersion {
332    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
333        write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
334    }
335}
336
337#[derive(Copy, Clone, Debug)]
338pub enum RasterizationOptions {
339    Alpha,
340    Bgra,
341}
342
343pub trait FontSystem: Send + Sync {
344    fn add_fonts(&self, fonts: &[Arc<Vec<u8>>]) -> anyhow::Result<()>;
345    fn load_family(&self, name: &str, features: &FontFeatures) -> anyhow::Result<Vec<FontId>>;
346    fn select_font(
347        &self,
348        font_ids: &[FontId],
349        properties: &FontProperties,
350    ) -> anyhow::Result<FontId>;
351    fn font_metrics(&self, font_id: FontId) -> FontMetrics;
352    fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<RectF>;
353    fn advance(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<Vector2F>;
354    fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId>;
355    fn rasterize_glyph(
356        &self,
357        font_id: FontId,
358        font_size: f32,
359        glyph_id: GlyphId,
360        subpixel_shift: Vector2F,
361        scale_factor: f32,
362        options: RasterizationOptions,
363    ) -> Option<(RectI, Vec<u8>)>;
364    fn layout_line(&self, text: &str, font_size: f32, runs: &[(usize, RunStyle)]) -> LineLayout;
365    fn wrap_line(&self, text: &str, font_id: FontId, font_size: f32, width: f32) -> Vec<usize>;
366}
367
368impl<'a> Default for WindowOptions<'a> {
369    fn default() -> Self {
370        Self {
371            bounds: WindowBounds::Maximized,
372            titlebar: Some(TitlebarOptions {
373                title: Default::default(),
374                appears_transparent: Default::default(),
375                traffic_light_position: Default::default(),
376            }),
377            center: false,
378            focus: true,
379            kind: WindowKind::Normal,
380            is_movable: true,
381            screen: None,
382        }
383    }
384}