Skip to main content

blitz_traits/
shell.rs

1//! Abstraction over windowing / operating system ("shell") functionality
2
3use cursor_icon::CursorIcon;
4
5/// Type representing an error performing a clipboard operation
6// TODO: fill out with meaningful errors
7pub struct ClipboardError;
8
9/// Abstraction over windowing / operating system ("shell") functionality that allows a Blitz document
10/// to access that functionality without depending on a specific shell environment.
11pub trait ShellProvider: Send + Sync + 'static {
12    fn request_redraw(&self) {}
13    fn set_cursor(&self, icon: CursorIcon) {
14        let _ = icon;
15    }
16    fn set_window_title(&self, title: String) {
17        let _ = title;
18    }
19    fn set_ime_enabled(&self, is_enabled: bool) {
20        let _ = is_enabled;
21    }
22    fn set_ime_cursor_area(&self, x: f32, y: f32, width: f32, height: f32) {
23        let _ = x;
24        let _ = y;
25        let _ = width;
26        let _ = height;
27    }
28    fn get_clipboard_text(&self) -> Result<String, ClipboardError> {
29        Err(ClipboardError)
30    }
31    fn set_clipboard_text(&self, text: String) -> Result<(), ClipboardError> {
32        let _ = text;
33        Err(ClipboardError)
34    }
35    fn open_file_dialog(
36        &self,
37        multiple: bool,
38        filter: Option<FileDialogFilter>,
39    ) -> Vec<std::path::PathBuf> {
40        let _ = multiple;
41        let _ = filter;
42        vec![]
43    }
44
45    // Window chrome controls, for documents that draw their own titlebar
46    // (e.g. a frameless window with an HTML titlebar).
47    fn request_window_close(&self) {}
48    fn set_window_minimized(&self, minimized: bool) {
49        let _ = minimized;
50    }
51    fn set_window_maximized(&self, maximized: bool) {
52        let _ = maximized;
53    }
54    fn is_window_maximized(&self) -> bool {
55        false
56    }
57    fn set_window_decorations(&self, decorations: bool) {
58        let _ = decorations;
59    }
60    /// Begin an interactive user-driven move of the window (call from a
61    /// mousedown handler on a drag region)
62    fn drag_window(&self) {}
63}
64
65pub struct DummyShellProvider;
66impl ShellProvider for DummyShellProvider {}
67
68/// The system color scheme (light and dark mode)
69#[derive(Default, Debug, Clone, Copy, PartialEq)]
70pub enum ColorScheme {
71    #[default]
72    Light,
73    Dark,
74}
75
76#[derive(Debug, Clone, PartialEq)]
77pub struct Viewport {
78    pub color_scheme: ColorScheme,
79    pub window_size: (u32, u32),
80    pub hidpi_scale: f32,
81    pub zoom: f32,
82}
83
84impl Default for Viewport {
85    fn default() -> Self {
86        Self {
87            window_size: (0, 0),
88            hidpi_scale: 1.0,
89            zoom: 1.0,
90            color_scheme: ColorScheme::Light,
91        }
92    }
93}
94
95impl Viewport {
96    pub fn new(
97        physical_width: u32,
98        physical_height: u32,
99        scale_factor: f32,
100        color_scheme: ColorScheme,
101    ) -> Self {
102        Self {
103            window_size: (physical_width, physical_height),
104            hidpi_scale: scale_factor,
105            zoom: 1.0,
106            color_scheme,
107        }
108    }
109
110    /// Total scaling, computed as `hidpi_scale_factor * zoom`
111    pub fn scale(&self) -> f32 {
112        self.hidpi_scale * self.zoom
113    }
114    /// Same as [`scale`](Self::scale) but `f64` instead of `f32`
115    pub fn scale_f64(&self) -> f64 {
116        self.scale() as f64
117    }
118
119    /// Set hidpi scale factor
120    pub fn set_hidpi_scale(&mut self, scale: f32) {
121        self.hidpi_scale = scale;
122    }
123
124    /// Get document zoom level
125    pub fn zoom(&self) -> f32 {
126        self.zoom
127    }
128
129    /// Set document zoom level (`1.0` is unzoomed)
130    pub fn set_zoom(&mut self, zoom: f32) {
131        self.zoom = zoom;
132    }
133
134    pub fn zoom_by(&mut self, zoom: f32) {
135        self.zoom += zoom;
136    }
137
138    pub fn zoom_mut(&mut self) -> &mut f32 {
139        &mut self.zoom
140    }
141}
142
143/// Filter provided by the dom for an file picker
144pub struct FileDialogFilter {
145    pub name: String,
146    pub extensions: Vec<String>,
147}