icedtea 0.17.2

Native desktop widgets and chrome for iced
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
//! Window kinds, overlay placement, and hide policy.
//!
//! `Boot::overlay` plus [`place`] or [`place_centered`] position a
//! palette. [`crate::daemon!`] keeps the process up; [`crate::app::Prepared::open`]
//! maps the window. Subscribe with [`crate::key::listen`] and pass Escape to
//! [`should_hide`].
//!
//! ```
//! use icedtea::window::{HideEvent, HidePolicy, should_hide};
//! assert!(should_hide(
//!     HidePolicy::EscapeOrFocusLoss,
//!     HideEvent::Escape,
//!     true,
//! ));
//! ```

use iced::window::{self, Level, Position};
use iced::{Point, Size};

use crate::layout::{window_size_from_dock, DockSpec};

/// Kind of window icedtea can open.
///
/// ```
/// use icedtea::window::{HideEvent, HidePolicy, WindowKind, should_hide};
/// assert!(should_hide(HidePolicy::Escape, HideEvent::Escape, false));
/// assert!(should_hide(HidePolicy::Escape, HideEvent::Escape, true));
/// assert!(!should_hide(HidePolicy::FocusLoss, HideEvent::FocusLoss, true));
/// let s = icedtea::window::settings(WindowKind::Overlay, "dev.icedtea.app");
/// assert!(!s.decorations);
/// assert!(s.max_size.is_none());
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WindowKind {
    /// Desktop tool. Call [`crate::action::ActionTable::seed_quit`].
    Application,
    Dialog,
    Overlay,
}

/// When an overlay hides.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HidePolicy {
    Escape,
    FocusLoss,
    EscapeOrFocusLoss,
    Manual,
}

/// Event that may hide an overlay.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HideEvent {
    Escape,
    FocusLoss,
}

/// Whether the overlay should hide.
///
/// `in_card` (search field or result list) suppresses only
/// [`HideEvent::FocusLoss`]. Escape still hides. iced 0.14 `text_input`
/// captures Escape; the application must forward that captured key
/// into this function.
pub fn should_hide(policy: HidePolicy, event: HideEvent, in_card: bool) -> bool {
    if in_card && event == HideEvent::FocusLoss {
        return false;
    }
    match (policy, event) {
        (HidePolicy::Manual, _) => false,
        (HidePolicy::Escape, HideEvent::Escape) => true,
        (HidePolicy::FocusLoss, HideEvent::FocusLoss) => true,
        (HidePolicy::EscapeOrFocusLoss, _) => true,
        _ => false,
    }
}

/// One display rectangle in the same coordinate space as the pointer.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DisplayBounds {
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
}

impl DisplayBounds {
    pub fn contains(self, pointer: (f32, f32)) -> bool {
        pointer.0 >= self.x
            && pointer.0 < self.x + self.width
            && pointer.1 >= self.y
            && pointer.1 < self.y + self.height
    }
}

/// Place an overlay on the display under `pointer`, clamped so the
/// window stays on that display.
///
/// ```
/// use icedtea::window::{place, DisplayBounds};
/// let d = DisplayBounds { x: 0.0, y: 0.0, width: 1920.0, height: 1080.0 };
/// let p = place((100.0, 80.0), iced::Size::new(400.0, 300.0), &[d]);
/// assert_eq!(p.x, 100.0);
/// assert_eq!(p.y, 80.0);
/// ```
pub fn place(pointer: (f32, f32), size: Size, displays: &[DisplayBounds]) -> Point {
    let Some(d) = display_at(pointer, displays) else {
        return Point::new(pointer.0, pointer.1);
    };
    clamp_on_display(pointer.0, pointer.1, size, d)
}

/// Center `size` on the display under `pointer` (else the first display).
///
/// ```
/// use icedtea::window::{place_centered, DisplayBounds};
/// let left = DisplayBounds { x: 0.0, y: 0.0, width: 1920.0, height: 1080.0 };
/// let right = DisplayBounds { x: 1920.0, y: 0.0, width: 1280.0, height: 800.0 };
/// let p = place_centered((2000.0, 10.0), iced::Size::new(400.0, 300.0), &[left, right]);
/// assert_eq!(p.x, 2360.0);
/// assert_eq!(p.y, 250.0);
/// ```
pub fn place_centered(pointer: (f32, f32), size: Size, displays: &[DisplayBounds]) -> Point {
    let Some(d) = display_at(pointer, displays) else {
        return Point::new(pointer.0 - size.width / 2.0, pointer.1 - size.height / 2.0);
    };
    clamp_on_display(
        d.x + (d.width - size.width) / 2.0,
        d.y + (d.height - size.height) / 2.0,
        size,
        d,
    )
}

/// Display under `pointer`, or `pin` when that index exists.
pub fn chosen_display(
    pin: Option<usize>,
    pointer: (f32, f32),
    displays: &[DisplayBounds],
) -> Option<DisplayBounds> {
    if let Some(i) = pin {
        if let Some(d) = displays.get(i).copied() {
            return Some(d);
        }
    }
    display_at(pointer, displays)
}

/// Place `size` on a pinned display. `prefer` is clamped onto that
/// display even when the pointer has moved to another screen.
///
/// ```
/// use icedtea::window::{place_pinned, DisplayBounds};
/// let left = DisplayBounds { x: 0.0, y: 0.0, width: 1920.0, height: 1080.0 };
/// let right = DisplayBounds { x: 1920.0, y: 0.0, width: 1280.0, height: 800.0 };
/// let p = place_pinned(Some(1), (10.0, 10.0), iced::Size::new(400.0, 300.0), &[left, right]);
/// assert!(p.x >= 1920.0);
/// ```
pub fn place_pinned(
    pin: Option<usize>,
    pointer: (f32, f32),
    size: Size,
    displays: &[DisplayBounds],
) -> Point {
    let Some(d) = chosen_display(pin, pointer, displays) else {
        return Point::new(pointer.0, pointer.1);
    };
    clamp_on_display(pointer.0, pointer.1, size, d)
}

fn display_at(pointer: (f32, f32), displays: &[DisplayBounds]) -> Option<DisplayBounds> {
    displays
        .iter()
        .copied()
        .find(|d| d.contains(pointer))
        .or_else(|| displays.first().copied())
}

fn clamp_on_display(x: f32, y: f32, size: Size, d: DisplayBounds) -> Point {
    let max_x = (d.x + d.width - size.width).max(d.x);
    let max_y = (d.y + d.height - size.height).max(d.y);
    Point::new(x.clamp(d.x, max_x), y.clamp(d.y, max_y))
}

/// iced window settings for a kind.
pub fn settings(kind: WindowKind, application_id: &str) -> window::Settings {
    let (def, min) = window_size_from_dock(DockSpec::default());
    let mut win = match kind {
        WindowKind::Application => window::Settings {
            size: def,
            min_size: Some(min),
            resizable: true,
            decorations: true,
            level: Level::Normal,
            position: Position::Default,
            exit_on_close_request: true,
            ..window::Settings::default()
        },
        WindowKind::Dialog => window::Settings {
            size: Size::new(480.0, 320.0),
            min_size: Some(Size::new(360.0, 200.0)),
            resizable: true,
            decorations: true,
            level: Level::Normal,
            position: Position::Centered,
            exit_on_close_request: true,
            ..window::Settings::default()
        },
        WindowKind::Overlay => window::Settings {
            size: Size::new(720.0, 480.0),
            min_size: Some(Size::new(200.0, 160.0)),
            max_size: None,
            resizable: false,
            decorations: false,
            level: Level::AlwaysOnTop,
            position: Position::Centered,
            exit_on_close_request: false,
            ..window::Settings::default()
        },
    };
    apply_app_id(&mut win, application_id, kind);
    win
}

/// Turn overlay settings into a decorated application window.
///
/// Keeps size and position. The application opens or replaces the
/// window; iced cannot change Linux `override_redirect` or Windows
/// `skip_taskbar` on a live window.
///
/// ```
/// use icedtea::window::{settings, retarget, WindowKind};
/// let mut s = settings(WindowKind::Overlay, "dev.icedtea.app");
/// retarget(&mut s, "dev.icedtea.app");
/// assert!(s.decorations && s.resizable);
/// assert_eq!(s.level, iced::window::Level::Normal);
/// ```
pub fn retarget(win: &mut window::Settings, application_id: &str) {
    win.resizable = true;
    win.decorations = true;
    win.level = Level::Normal;
    win.exit_on_close_request = true;
    apply_app_id(win, application_id, WindowKind::Application);
}

fn apply_app_id(win: &mut window::Settings, application_id: &str, kind: WindowKind) {
    #[cfg(target_os = "linux")]
    {
        win.platform_specific.application_id = application_id.to_string();
        win.platform_specific.override_redirect = matches!(kind, WindowKind::Overlay);
    }
    #[cfg(target_os = "windows")]
    {
        win.platform_specific.skip_taskbar = matches!(kind, WindowKind::Overlay);
    }
    let _ = (win, application_id, kind);
}

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

    #[test]
    fn kinds_and_hide_policy() {
        assert!(!should_hide(HidePolicy::Manual, HideEvent::Escape, false));
        assert!(!should_hide(
            HidePolicy::Manual,
            HideEvent::FocusLoss,
            false
        ));
        assert!(should_hide(HidePolicy::Escape, HideEvent::Escape, false));
        assert!(!should_hide(
            HidePolicy::Escape,
            HideEvent::FocusLoss,
            false
        ));
        assert!(should_hide(
            HidePolicy::FocusLoss,
            HideEvent::FocusLoss,
            false
        ));
        assert!(!should_hide(
            HidePolicy::FocusLoss,
            HideEvent::Escape,
            false
        ));
        assert!(should_hide(
            HidePolicy::EscapeOrFocusLoss,
            HideEvent::Escape,
            false
        ));
        assert!(should_hide(
            HidePolicy::EscapeOrFocusLoss,
            HideEvent::FocusLoss,
            false
        ));
        let app = settings(WindowKind::Application, "dev.icedtea.test");
        assert!(app.decorations && app.resizable);
        let dlg = settings(WindowKind::Dialog, "dev.icedtea.test");
        assert!(matches!(dlg.position, Position::Centered));
        let ov = settings(WindowKind::Overlay, "dev.icedtea.test");
        assert!(!ov.decorations);
        assert_eq!(ov.level, Level::AlwaysOnTop);
        assert!(ov.max_size.is_none());
        assert_eq!(ov.size, Size::new(720.0, 480.0));
        let d = DisplayBounds {
            x: 0.0,
            y: 0.0,
            width: 1920.0,
            height: 1080.0,
        };
        let other = DisplayBounds {
            x: 1920.0,
            y: 0.0,
            width: 1280.0,
            height: 800.0,
        };
        let p = place((100.0, 80.0), Size::new(400.0, 300.0), &[d, other]);
        assert_eq!(p, Point::new(100.0, 80.0));
        let edge = place((1900.0, 1000.0), Size::new(400.0, 300.0), &[d]);
        assert!((edge.x - 1520.0).abs() < 0.01);
        assert!((edge.y - 780.0).abs() < 0.01);
        let second = place((2000.0, 10.0), Size::new(200.0, 100.0), &[d, other]);
        assert!(second.x >= 1920.0);
        let raw = place((8.0, 9.0), Size::new(10.0, 10.0), &[]);
        assert_eq!(raw, Point::new(8.0, 9.0));
        let fallback = place((-10.0, -10.0), Size::new(100.0, 80.0), &[other]);
        assert_eq!(fallback.x, other.x);
        assert_eq!(fallback.y, other.y);
        assert!(d.contains((10.0, 10.0)));
        assert!(!d.contains((2000.0, 10.0)));
        let pinned = place_pinned(Some(1), (10.0, 10.0), Size::new(400.0, 300.0), &[d, other]);
        assert!(pinned.x >= 1920.0);
        assert_eq!(
            chosen_display(Some(1), (10.0, 10.0), &[d, other]),
            Some(other)
        );
        assert_eq!(chosen_display(None, (100.0, 80.0), &[d, other]), Some(d));
        let miss_pin = place_pinned(Some(9), (100.0, 80.0), Size::new(10.0, 10.0), &[d, other]);
        assert_eq!(miss_pin, Point::new(100.0, 80.0));
        let empty_pin = place_pinned(Some(0), (3.0, 4.0), Size::new(10.0, 10.0), &[]);
        assert_eq!(empty_pin, Point::new(3.0, 4.0));
        let none_pin = place_pinned(None, (3.0, 4.0), Size::new(10.0, 10.0), &[]);
        assert_eq!(none_pin, Point::new(3.0, 4.0));
    }

    #[test]
    fn place_centered_uses_pointer_display() {
        let left = DisplayBounds {
            x: 0.0,
            y: 0.0,
            width: 1920.0,
            height: 1080.0,
        };
        let right = DisplayBounds {
            x: 1920.0,
            y: 0.0,
            width: 1280.0,
            height: 800.0,
        };
        let size = Size::new(400.0, 300.0);
        let mid = place_centered((2000.0, 10.0), size, &[left, right]);
        assert_eq!(mid, Point::new(2360.0, 250.0));
        let first = place_centered((100.0, 80.0), size, &[left, right]);
        assert_eq!(first, Point::new(760.0, 390.0));
        let miss = place_centered((-10.0, -10.0), size, &[left, right]);
        assert_eq!(miss, Point::new(760.0, 390.0));
        let empty = place_centered((8.0, 9.0), Size::new(10.0, 10.0), &[]);
        assert_eq!(empty, Point::new(3.0, 4.0));
        let huge = place_centered((100.0, 80.0), Size::new(3000.0, 2000.0), &[left]);
        assert_eq!(huge, Point::new(0.0, 0.0));
    }

    #[test]
    fn in_card_suppresses_focus_loss_only() {
        assert!(should_hide(HidePolicy::Escape, HideEvent::Escape, true));
        assert!(should_hide(
            HidePolicy::EscapeOrFocusLoss,
            HideEvent::Escape,
            true
        ));
        assert!(!should_hide(HidePolicy::Escape, HideEvent::FocusLoss, true));
        assert!(!should_hide(
            HidePolicy::EscapeOrFocusLoss,
            HideEvent::FocusLoss,
            true
        ));
        assert!(!should_hide(
            HidePolicy::FocusLoss,
            HideEvent::FocusLoss,
            true
        ));
        assert!(!should_hide(HidePolicy::Manual, HideEvent::Escape, true));
    }

    #[test]
    fn retarget_makes_decorated_application() {
        let mut s = settings(WindowKind::Overlay, "dev.icedtea.test");
        let size = s.size;
        assert!(!s.decorations);
        assert!(!s.resizable);
        assert_eq!(s.level, Level::AlwaysOnTop);
        assert!(!s.exit_on_close_request);
        #[cfg(target_os = "linux")]
        assert!(s.platform_specific.override_redirect);
        #[cfg(target_os = "windows")]
        assert!(s.platform_specific.skip_taskbar);
        retarget(&mut s, "dev.icedtea.test");
        assert!(s.decorations && s.resizable);
        assert_eq!(s.level, Level::Normal);
        assert!(s.exit_on_close_request);
        assert_eq!(s.size, size);
        #[cfg(target_os = "linux")]
        {
            assert!(!s.platform_specific.override_redirect);
            assert_eq!(s.platform_specific.application_id, "dev.icedtea.test");
        }
        #[cfg(target_os = "windows")]
        assert!(!s.platform_specific.skip_taskbar);
    }
}