Skip to main content

nsh_core/element/
env.rs

1//! What components read from the environment: the surface, system UI, the
2//! user's settings ([`super::window`] and the like).
3
4use crate::{
5    EdgeInsets, Points, Size,
6    platform::{Idiom, Lifecycle, Locale, LocaleSettings, Network, Os},
7};
8
9/// The environment of a surface, as the platform reports it.
10#[derive(Debug, Clone, Copy, PartialEq)]
11pub struct Environment {
12    /// The surface's size in points.
13    pub window: Size<Points>,
14    /// Native horizontal size class, or `None` to use the window width at 600pt.
15    pub horizontal_size_class: Option<crate::platform::SizeClass>,
16    /// Native vertical size class, or `None` to use the window height at 600pt.
17    pub vertical_size_class: Option<crate::platform::SizeClass>,
18    /// The edges system UI covers.
19    pub safe_area: EdgeInsets,
20    /// The parts of the surface the device reserves
21    /// ([`crate::platform::Environment::reserved_regions`]).
22    pub reserved_regions: crate::platform::ReservedRegions,
23    /// Space reserved for system controls in a custom desktop title bar.
24    pub title_bar: crate::geometry::Edges<crate::geometry::Points>,
25    /// The app's window: its bounds, mode and display (desktops).
26    pub window_state: Option<crate::desktop::WindowState>,
27    /// The software keyboard's height, 0 when hidden.
28    pub keyboard_height: Points,
29    /// How long the keyboard's show / hide animation takes, in milliseconds.
30    pub keyboard_duration: f32,
31    /// Dark appearance.
32    pub dark: bool,
33    /// The user's text size relative to the default.
34    pub font_scale: f32,
35    /// Less motion asked for.
36    pub reduce_motion: bool,
37    /// A screen reader runs.
38    pub screen_reader: bool,
39    /// The platform's screen readers follow active descendants
40    /// ([`crate::platform::Environment::reads_active_descendants`]).
41    pub reads_active_descendants: bool,
42    /// Right-to-left layout.
43    pub rtl: bool,
44    /// Device pixels per point.
45    pub pixel_ratio: f32,
46    /// The operating system.
47    pub os: Os,
48    /// The platform's name where it is not the OS's (a platform built
49    /// outside NSH: [`crate::platform::Environment::platform`]).
50    pub platform: Option<&'static str>,
51    /// The OS version: major and minor (iOS), Android's API level and 0.
52    pub os_version: (u16, u16),
53    /// The kind of device.
54    pub idiom: Idiom,
55    /// The user's locale, for formatting numbers and dates.
56    pub locale: Locale,
57    /// The user's locale settings: the preferred languages, the time zone
58    /// and the regional preferences.
59    pub locale_settings: LocaleSettings,
60    /// In a browser, it runs on an Apple platform: shortcuts follow Apple's
61    /// conventions ([`crate::keymap::apple_keys`]); the other OSes imply
62    /// theirs.
63    pub apple_keyboard: bool,
64    /// Ctrl stands in for Command: labels show Command as Ctrl
65    /// ([`crate::platform::Environment::ctrl_for_command`]).
66    pub ctrl_for_command: bool,
67    /// The embedding app answers `nsh/host`'s calls
68    /// ([`crate::platform::Environment::host`]).
69    pub host: bool,
70    /// Whether the app is in use: in the foreground, visible without the
71    /// focus, or in the background.
72    pub lifecycle: Lifecycle,
73    /// How many times the system woke from sleep while the surface ran
74    /// ([`crate::platform::Environment::wake_count`]).
75    pub wake_count: u32,
76    /// How the device reaches the network
77    /// ([`crate::platform::Environment::network`]).
78    pub network: Network,
79    /// The platform picks colors from the screen
80    /// ([`crate::platform::Environment::screen_color`]).
81    pub screen_color: bool,
82    /// The platform reports clipboard writes' results
83    /// ([`crate::platform::Environment::clipboard_write_result`]).
84    pub clipboard_write_result: bool,
85    /// The platform renders canvas drawings offscreen into images
86    /// ([`crate::platform::Environment::canvas_render`]).
87    pub canvas_render: bool,
88    /// The platform opens the app's page in the system's settings
89    /// ([`crate::platform::Environment::app_settings`]).
90    pub app_settings: bool,
91    /// Links of the schemes the app claims reach it
92    /// ([`field@crate::platform::Environment::scheme_links`], resolved: `true`
93    /// on every OS but the web unless the platform says otherwise).
94    pub scheme_links: bool,
95    /// The platform shows file dialogs and opens, reveals and trashes local
96    /// paths ([`crate::platform::Environment::file_requests`]).
97    pub file_requests: bool,
98    /// The platform shows notifications
99    /// ([`crate::platform::Environment::notifications`]).
100    pub notifications: bool,
101    /// The platform plays sound
102    /// ([`crate::platform::Environment::audio`]).
103    pub audio: bool,
104    /// The platform shows the system's tab bar on iOS and Android
105    /// ([`crate::platform::Environment::tab_bar`]).
106    pub tab_bar: bool,
107    /// The Cell size of a grid surface (a terminal): the core lays out in
108    /// its Cells ([`crate::platform::Environment::cell`]); `None` elsewhere.
109    pub cell: Option<Size<Points>>,
110    /// The platform gives web views trusted input
111    /// ([`crate::platform::Environment::web_input`]).
112    pub web_input: bool,
113    /// The platform answers awaitable WebView evaluations
114    /// ([`crate::Command::Evaluate`]); false rejects as unavailable.
115    pub web_evaluate: bool,
116    /// The platform shows web views
117    /// ([`crate::platform::Environment::web_views`]).
118    pub web_views: bool,
119    /// The platform carries out web view requests
120    /// ([`crate::platform::Environment::webview_requests`]).
121    pub webview_requests: bool,
122    /// The platform draws shader views
123    /// ([`crate::platform::Environment::shaders`]).
124    pub shaders: bool,
125}
126
127impl Environment {
128    /// The platform's name (TS: `platform.name`): a platform built outside
129    /// NSH's own, else the OS's.
130    pub fn platform_name(&self) -> &'static str {
131        self.platform.unwrap_or(self.os.name())
132    }
133}
134
135impl Default for Environment {
136    fn default() -> Self {
137        Self {
138            window: crate::geometry::size_pt(0.0, 0.0),
139            horizontal_size_class: None,
140            vertical_size_class: None,
141            safe_area: EdgeInsets::default(),
142            reserved_regions: crate::platform::ReservedRegions::NONE,
143            title_bar: crate::geometry::Edges::default(),
144            window_state: None,
145            keyboard_height: Points(0.0),
146            keyboard_duration: 0.0,
147            dark: false,
148            font_scale: 1.0,
149            reduce_motion: false,
150            screen_reader: false,
151            reads_active_descendants: false,
152            rtl: false,
153            pixel_ratio: 1.0,
154            os: Os::default(),
155            platform: None,
156            os_version: (0, 0),
157            idiom: Idiom::Phone,
158            locale: Locale::EN_US,
159            locale_settings: LocaleSettings::default(),
160            apple_keyboard: false,
161            ctrl_for_command: false,
162            host: false,
163            lifecycle: Lifecycle::Active,
164            wake_count: 0,
165            network: Network::default(),
166            screen_color: false,
167            clipboard_write_result: false,
168            canvas_render: false,
169            app_settings: false,
170            scheme_links: true,
171            file_requests: false,
172            notifications: false,
173            audio: false,
174            tab_bar: true,
175            cell: None,
176            web_input: false,
177            web_evaluate: false,
178            web_views: true,
179            webview_requests: false,
180            shaders: true,
181        }
182    }
183}