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 {
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 get_clipboard_text(&self) -> Result<String, ClipboardError> {
20        Err(ClipboardError)
21    }
22    fn set_clipboard_text(&self, text: String) -> Result<(), ClipboardError> {
23        let _ = text;
24        Err(ClipboardError)
25    }
26}
27
28pub struct DummyShellProvider;
29impl ShellProvider for DummyShellProvider {}
30
31/// The system color scheme (light and dark mode)
32#[derive(Default, Debug, Clone, Copy)]
33pub enum ColorScheme {
34    #[default]
35    Light,
36    Dark,
37}
38
39#[derive(Default, Debug, Clone)]
40pub struct Viewport {
41    pub window_size: (u32, u32),
42
43    hidpi_scale: f32,
44
45    zoom: f32,
46
47    pub font_size: f32,
48
49    pub color_scheme: ColorScheme,
50}
51
52impl Viewport {
53    pub fn new(
54        physical_width: u32,
55        physical_height: u32,
56        scale_factor: f32,
57        color_scheme: ColorScheme,
58    ) -> Self {
59        Self {
60            window_size: (physical_width, physical_height),
61            hidpi_scale: scale_factor,
62            zoom: 1.0,
63            font_size: 16.0,
64            color_scheme,
65        }
66    }
67
68    /// Total scaling, computed as `hidpi_scale_factor * zoom`
69    pub fn scale(&self) -> f32 {
70        self.hidpi_scale * self.zoom
71    }
72    /// Same as [`scale`](Self::scale) but `f64` instead of `f32`
73    pub fn scale_f64(&self) -> f64 {
74        self.scale() as f64
75    }
76
77    /// Set hidpi scale factor
78    pub fn set_hidpi_scale(&mut self, scale: f32) {
79        self.hidpi_scale = scale;
80    }
81
82    /// Get document zoom level
83    pub fn zoom(&self) -> f32 {
84        self.zoom
85    }
86
87    /// Set document zoom level (`1.0` is unzoomed)
88    pub fn set_zoom(&mut self, zoom: f32) {
89        self.zoom = zoom;
90    }
91
92    pub fn zoom_by(&mut self, zoom: f32) {
93        self.zoom += zoom;
94    }
95
96    pub fn zoom_mut(&mut self) -> &mut f32 {
97        &mut self.zoom
98    }
99}