nsh-core 0.6.1

Platform-independent core: shadow tree, styles, layout and mount instructions.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! What the app reads of its windows and the displays (TS `nsh/desktop`'s
//! `WindowRef.state()`, `windowState()` and `displays()`), and the
//! placement it saves and restores a window with ([`WindowPlacement`],
//! GPUI's `WindowBounds`).
//!
//! Every rectangle is in points, in screen coordinates: from the primary
//! display's top left corner, `y` growing down, as
//! `WindowRef.setBounds` takes them.

use serde::{Deserialize, Serialize};

use super::WindowMode;
use crate::{
    element::{JsHost, JsIn, JsObject, JsOwned, JsValue, PropError, TsTypes},
    geometry::{Bounds, Point, Points, Size, point, size},
};

/// A display, the same from one launch of the app to the next on the same
/// machine (an id to compare, not to read: the platform's own name for the
/// display, hashed). TS: a string.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct DisplayId(pub u64);

impl DisplayId {
    /// The id of the display the platform names `name` (a display's UUID,
    /// a monitor's device name, a connector): FNV-1a, which gives the same
    /// id for the same name in every process. For the platform backends,
    /// which all name displays this way.
    #[doc(hidden)]
    pub fn from_name(name: &[u8]) -> DisplayId {
        let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
        for byte in name {
            hash ^= u64::from(*byte);
            hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
        }
        DisplayId(hash)
    }
}

/// TS: `string`, sixteen hexadecimal digits.
impl JsValue for DisplayId {
    fn ts_type(_types: &mut TsTypes) -> String {
        "string".into()
    }

    fn from_js(value: JsIn<'_>) -> Result<Self, PropError> {
        value
            .as_string()
            .and_then(|s| u64::from_str_radix(&s, 16).ok())
            .map(DisplayId)
            .ok_or_else(|| value.expected("a display id"))
    }

    fn to_js<'a>(&self, host: &'a dyn JsHost) -> Result<JsOwned<'a>, PropError> {
        format!("{:016x}", self.0).to_js(host)
    }
}

/// A display (GPUI's `PlatformDisplay`): its area, the part of it windows
/// use, and how it shows them.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Default, JsObject)]
pub struct Display {
    /// Which display it is, the same in the app's next launch.
    pub id: DisplayId,
    /// Its whole area, in screen coordinates.
    pub bounds: Bounds<Points>,
    /// The part windows use: its area without the menu bar, the Dock or the
    /// taskbar (the whole area where the platform does not tell).
    pub visible_bounds: Bounds<Points>,
    /// The primary display, whose top left corner screen coordinates start
    /// from.
    pub primary: bool,
    /// Its name as the system shows it (`""` where the platform does not
    /// tell).
    pub name: String,
    /// Device pixels per point.
    pub scale_factor: f32,
    /// Frames per second it shows, where the platform tells.
    pub refresh_rate: Option<f32>,
    /// The parts of the display a camera housing hides (a MacBook's notch),
    /// in screen coordinates; empty where it has none.
    pub cutouts: Vec<Bounds<Points>>,
}

impl Display {
    /// A display with `id`, `bounds` and `visible_bounds`, at a scale of 1,
    /// with no name and no cutouts.
    pub fn new(
        id: DisplayId,
        bounds: Bounds<Points>,
        visible_bounds: Bounds<Points>,
        primary: bool,
    ) -> Display {
        Display {
            id,
            bounds,
            visible_bounds,
            primary,
            name: String::new(),
            scale_factor: 1.0,
            refresh_rate: None,
            cutouts: Vec::new(),
        }
    }
}

/// The notch of a display of `bounds` (screen coordinates), for the macOS
/// backends, which read its inputs from `NSScreen`: the gap between the
/// menu bar's two parts beside the camera housing (`left` and `right`, the
/// sizes of its auxiliary top left and top right areas), at the display's
/// top and as tall as its safe area's top inset (`top_inset`). Only the
/// areas' sizes are read, measured in from the display's edges, so the
/// coordinate space their origins are in does not matter. `None` on a
/// display without one: an area missing (empty), no inset, no gap.
#[doc(hidden)]
pub fn notch(
    bounds: Bounds<Points>,
    left: Size<Points>,
    right: Size<Points>,
    top_inset: Points,
) -> Option<Bounds<Points>> {
    let present = |area: Size<Points>| area.width > Points::ZERO && area.height > Points::ZERO;
    if !present(left) || !present(right) || top_inset <= Points::ZERO {
        return None;
    }
    let start = bounds.left() + left.width;
    let end = bounds.right() - right.width;
    if end <= start {
        return None;
    }
    Some(Bounds::new(
        point(start, bounds.top()),
        size(end - start, top_inset),
    ))
}

/// A window's mode and its normal bounds (GPUI's `WindowBounds`): what an
/// app saves to open its window as the user left it, and gives
/// `WindowRef.setPlacement` (Rust [`crate::element::desktop::WindowRef::set_placement`])
/// to restore it. A maximized or full screen window carries the bounds it
/// returns to.
///
/// TS: `{ mode: WindowMode; bounds: Bounds }`, which is also its serde form:
/// what one side saves `JSON.stringify` reads back with `serde_json`, and
/// the other way round.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Default, Serialize, Deserialize, JsObject)]
pub struct WindowPlacement {
    /// Normal, maximized (zoomed on macOS) or in full screen.
    #[prop(default)]
    #[serde(default)]
    pub mode: WindowMode,
    /// Its bounds in the normal mode: where it is, or returns to, in
    /// points, in screen coordinates.
    pub bounds: Bounds<Points>,
}

/// The least of a window's top strip ([`TITLE_STRIP`] tall) that stays
/// on a display's visible area for `fit_on_displays` to leave it where it
/// is: enough to take it by its title bar.
const GRAB: Size<Points> = Size {
    width: Points(100.0),
    height: Points(20.0),
};

/// The top of a window that holds its title bar.
const TITLE_STRIP: Points = Points(40.0);

impl WindowPlacement {
    /// A placement in `mode` with normal bounds `bounds`.
    pub fn new(mode: WindowMode, bounds: Bounds<Points>) -> WindowPlacement {
        WindowPlacement { mode, bounds }
    }

    /// The same mode with `bounds`.
    pub fn with_bounds(self, bounds: Bounds<Points>) -> WindowPlacement {
        WindowPlacement { bounds, ..self }
    }

    /// These bounds on the displays there are now (saved bounds of a
    /// display the user has since unplugged, or of another arrangement):
    /// unchanged when at least 100 × 20 points of the window's top 40
    /// points are on a display's visible area, where the user can take it
    /// by its title bar; otherwise centered on the primary display's
    /// visible area (the first display's without one), no larger than it.
    /// A size that is not finite and positive takes the visible area's.
    /// The mode stays. Without displays (off desktops) the bounds stay.
    /// What `WindowRef::set_placement` does to the bounds it is given.
    pub(crate) fn fit_on_displays(self, displays: &[Display]) -> WindowPlacement {
        let bounds = self.bounds;
        let valid = |v: Points| v.0.is_finite() && v.0 > 0.0;
        let sized = valid(bounds.size.width)
            && valid(bounds.size.height)
            && bounds.origin.x.0.is_finite()
            && bounds.origin.y.0.is_finite();
        if sized && displays.iter().any(|d| grabbable(bounds, d.visible_bounds)) {
            return self;
        }
        let Some(display) = displays
            .iter()
            .find(|d| d.primary)
            .or_else(|| displays.first())
        else {
            return self;
        };
        let area = display.visible_bounds;
        let fit = |v: Points, max: Points| if valid(v) { v.min(max) } else { max };
        let size = Size {
            width: fit(bounds.size.width, area.size.width),
            height: fit(bounds.size.height, area.size.height),
        };
        let origin = Point {
            x: area.origin.x + (area.size.width - size.width) / 2.0,
            y: area.origin.y + (area.size.height - size.height) / 2.0,
        };
        self.with_bounds(Bounds::new(origin, size))
    }
}

/// Whether enough of `bounds`' top strip is on `area` ([`GRAB`]).
fn grabbable(bounds: Bounds<Points>, area: Bounds<Points>) -> bool {
    let strip = Bounds::new(
        bounds.origin,
        Size {
            width: bounds.size.width,
            height: bounds.size.height.min(TITLE_STRIP),
        },
    );
    if !strip.intersects(&area) {
        return false;
    }
    let on = strip.intersect(&area);
    on.size.width >= GRAB.width.min(strip.size.width)
        && on.size.height >= GRAB.height.min(strip.size.height)
}

/// A window as the platform reports it (TS `WindowRef.state()` and
/// `windowState()`, Rust [`crate::element::desktop::WindowRef::state`]).
/// A move or a resize is reported once the window stops for a moment; a
/// change of mode, of display, of the focus or of being minimized or
/// hidden at once.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Default, JsObject)]
pub struct WindowState {
    /// Its mode.
    pub mode: WindowMode,
    /// It is minimized (in the Dock, the taskbar).
    pub minimized: bool,
    /// It is on the screen: shown and not minimized (the app not hidden by
    /// macOS's Hide). One there that `lifecycle()` reports in the
    /// background is covered by other windows.
    pub visible: bool,
    /// It has the keyboard focus.
    pub focused: bool,
    /// Its outer frame now, window decorations included, in screen
    /// coordinates (a maximized or full screen window's as it shows).
    pub bounds: Bounds<Points>,
    /// Its content area now, in screen coordinates.
    pub content_bounds: Bounds<Points>,
    /// Its mode and normal bounds: what to save to open it the same way
    /// next time.
    pub placement: WindowPlacement,
    /// The display it is on (the one with most of it): one of
    /// `displays()`.
    pub display: Option<DisplayId>,
    /// Device pixels per point.
    pub scale_factor: f32,
}

impl WindowState {
    /// A window on the screen at `bounds` (its content too) in
    /// `placement`'s mode, at a scale of 1.
    pub fn new(bounds: Bounds<Points>, placement: WindowPlacement) -> WindowState {
        WindowState {
            mode: placement.mode,
            visible: true,
            bounds,
            content_bounds: bounds,
            placement,
            scale_factor: 1.0,
            ..WindowState::default()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::geometry::px;

    fn rect(x: f32, y: f32, w: f32, h: f32) -> Bounds<Points> {
        Bounds::new(point(px(x), px(y)), size(px(w), px(h)))
    }

    #[test]
    fn a_notch_is_the_gap_between_the_auxiliary_areas() {
        // A 14-inch MacBook Pro's built-in screen, the primary one.
        let left = size(px(664.0), px(32.0));
        let right = size(px(663.0), px(32.0));
        assert_eq!(
            notch(rect(0.0, 0.0, 1512.0, 982.0), left, right, px(32.0)),
            Some(rect(664.0, 0.0, 185.0, 32.0)),
        );
    }

    #[test]
    fn a_display_without_a_notch_has_none() {
        let bounds = rect(0.0, 0.0, 2560.0, 1440.0);
        let none = Size::default();
        assert_eq!(notch(bounds, none, none, px(0.0)), None);
        // Areas without an inset, or an inset without areas.
        let left = size(px(1200.0), px(25.0));
        let right = size(px(1200.0), px(25.0));
        assert_eq!(notch(bounds, left, right, px(0.0)), None);
        assert_eq!(notch(bounds, none, none, px(25.0)), None);
        // Areas that meet leave no gap.
        let right = size(px(1360.0), px(25.0));
        assert_eq!(notch(bounds, left, right, px(25.0)), None);
    }

    #[test]
    fn a_notch_on_a_secondary_display_is_in_screen_coordinates() {
        // The built-in screen right of a 2560 × 1440 primary display, its
        // top 658 points below the primary's.
        let left = size(px(664.0), px(32.0));
        let right = size(px(663.0), px(32.0));
        assert_eq!(
            notch(rect(2560.0, 658.0, 1512.0, 982.0), left, right, px(32.0)),
            Some(rect(3224.0, 658.0, 185.0, 32.0)),
        );
    }

    fn displays() -> Vec<Display> {
        vec![
            // A laptop's screen under a menu bar, and a display to its right.
            Display::new(
                DisplayId(1),
                rect(0.0, 0.0, 1440.0, 900.0),
                rect(0.0, 25.0, 1440.0, 875.0),
                true,
            ),
            Display::new(
                DisplayId(2),
                rect(1440.0, 0.0, 1920.0, 1080.0),
                rect(1440.0, 0.0, 1920.0, 1080.0),
                false,
            ),
        ]
    }

    #[test]
    fn display_ids_are_stable_hashes_of_names() {
        assert_eq!(DisplayId::from_name(b""), DisplayId(0xcbf2_9ce4_8422_2325));
        assert_eq!(DisplayId::from_name(b"a"), DisplayId(0xaf63_dc4c_8601_ec8c));
        assert_ne!(
            DisplayId::from_name(b"\\\\.\\DISPLAY1"),
            DisplayId::from_name(b"\\\\.\\DISPLAY2")
        );
    }

    #[test]
    fn bounds_on_a_display_stay() {
        let on_second = WindowPlacement::new(WindowMode::Normal, rect(2000.0, 100.0, 800.0, 600.0));
        assert_eq!(on_second.fit_on_displays(&displays()), on_second);
        // Across two displays.
        let across = WindowPlacement::new(WindowMode::Maximized, rect(1000.0, 200.0, 900.0, 600.0));
        assert_eq!(across.fit_on_displays(&displays()), across);
        // Mostly off the screen, but its title bar can be taken.
        let corner = WindowPlacement::new(WindowMode::Normal, rect(-700.0, 860.0, 800.0, 600.0));
        assert_eq!(corner.fit_on_displays(&displays()), corner);
    }

    #[test]
    fn bounds_off_every_display_move_to_the_primary_one() {
        // The display on the left is gone.
        let gone = WindowPlacement::new(WindowMode::Normal, rect(-1500.0, 100.0, 800.0, 600.0));
        assert_eq!(
            gone.fit_on_displays(&displays()),
            WindowPlacement::new(WindowMode::Normal, rect(320.0, 162.5, 800.0, 600.0))
        );
        // Larger than the primary display: as large as its visible area.
        let large = WindowPlacement::new(WindowMode::FullScreen, rect(5000.0, 0.0, 2560.0, 1440.0));
        assert_eq!(
            large.fit_on_displays(&displays()),
            WindowPlacement::new(WindowMode::FullScreen, rect(0.0, 25.0, 1440.0, 875.0))
        );
        // Only its bottom edge shows: its title bar is above the screen.
        let above = WindowPlacement::new(WindowMode::Normal, rect(100.0, -590.0, 800.0, 600.0));
        assert_eq!(
            above.fit_on_displays(&displays()),
            WindowPlacement::new(WindowMode::Normal, rect(320.0, 162.5, 800.0, 600.0))
        );
        // Too little of its title bar shows to take it.
        let sliver = WindowPlacement::new(WindowMode::Normal, rect(-750.0, 100.0, 800.0, 600.0));
        assert_ne!(sliver.fit_on_displays(&displays()), sliver);
    }

    #[test]
    fn invalid_bounds_take_the_visible_area() {
        let nan = WindowPlacement::new(WindowMode::Normal, rect(f32::NAN, 0.0, 0.0, -1.0));
        assert_eq!(
            nan.fit_on_displays(&displays()),
            WindowPlacement::new(WindowMode::Normal, rect(0.0, 25.0, 1440.0, 875.0))
        );
    }

    #[test]
    fn without_displays_the_bounds_stay() {
        let gone = WindowPlacement::new(WindowMode::Normal, rect(-1500.0, 100.0, 800.0, 600.0));
        assert_eq!(gone.fit_on_displays(&[]), gone);
        // No primary display: the first one.
        let second = &displays()[1..];
        assert_eq!(
            gone.fit_on_displays(second),
            WindowPlacement::new(WindowMode::Normal, rect(2000.0, 240.0, 800.0, 600.0))
        );
    }

    #[test]
    fn bounds_save_as_the_ts_object() {
        let bounds = WindowPlacement::new(WindowMode::Maximized, rect(10.0, 20.5, 800.0, 600.0));
        let json = serde_json::to_string(&bounds).unwrap();
        assert_eq!(
            json,
            r#"{"mode":"maximized","bounds":{"origin":{"x":10.0,"y":20.5},"size":{"width":800.0,"height":600.0}}}"#
        );
        assert_eq!(
            serde_json::from_str::<WindowPlacement>(&json).unwrap(),
            bounds
        );
        // As `JSON.stringify` writes it.
        let ts = r#"{"mode":"fullScreen","bounds":{"origin":{"x":-1,"y":0},"size":{"width":800,"height":600}}}"#;
        assert_eq!(
            serde_json::from_str::<WindowPlacement>(ts).unwrap(),
            WindowPlacement::new(WindowMode::FullScreen, rect(-1.0, 0.0, 800.0, 600.0))
        );
    }
}