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
//! Split ratio and sash drag.

use iced::{mouse, Event, Point, Subscription};
use serde::{Deserialize, Serialize};

/// Axis of a split.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Axis {
    Horizontal,
    Vertical,
}

/// Two-pane split with a persistable ratio (0–1 is the first pane share).
///
/// ```
/// use icedtea::layout::{Axis, SplitState};
/// let mut split = SplitState::new(Axis::Horizontal, 0.3);
/// let before = split.ratio;
/// split.drag(10.0, 200.0);
/// assert!(split.ratio > before);
/// assert_eq!(split.persist(), split.ratio);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct SplitState {
    pub axis: Axis,
    pub ratio: f32,
    pub sash: f32,
    pub min_ratio: f32,
    pub max_ratio: f32,
}

impl SplitState {
    pub fn new(axis: Axis, ratio: f32) -> Self {
        Self {
            axis,
            ratio: ratio.clamp(0.05, 0.95),
            sash: 6.0,
            min_ratio: 0.12,
            max_ratio: 0.88,
        }
    }

    pub fn restore(axis: Axis, ratio: f32) -> Self {
        Self::new(axis, ratio)
    }

    pub fn persist(self) -> f32 {
        self.ratio
    }

    pub fn first_size(self, total: f32) -> f32 {
        let usable = (total - self.sash).max(0.0);
        usable * self.ratio
    }

    pub fn second_size(self, total: f32) -> f32 {
        let usable = (total - self.sash).max(0.0);
        usable - self.first_size(total)
    }

    pub fn drag(&mut self, delta_px: f32, total: f32) {
        let usable = (total - self.sash).max(1.0);
        self.ratio = (self.ratio + delta_px / usable).clamp(self.min_ratio, self.max_ratio);
    }
}

/// Pointer events on a split sash.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum SashEvent {
    Press,
    Move(f32),
    Release,
    /// Keyboard nudge in pixels (signed).
    Nudge(f32),
}

/// Window-space pointer while a sash is pressed (not the 6px grip local point).
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum PointerDrive {
    Move(Point),
    Release,
}

/// Axis coordinate of a window-space pointer.
pub fn sash_pointer_pos(axis: Axis, p: Point) -> f32 {
    match axis {
        Axis::Horizontal => p.x,
        Axis::Vertical => p.y,
    }
}

impl PointerDrive {
    /// Convert window-space motion into a sash event on `axis`.
    pub fn into_event(self, axis: Axis) -> SashEvent {
        match self {
            Self::Move(p) => SashEvent::Move(sash_pointer_pos(axis, p)),
            Self::Release => SashEvent::Release,
        }
    }
}

/// Map a window mouse event to sash drive (move uses window position).
pub fn sash_from_window_event(event: &Event) -> Option<PointerDrive> {
    match event {
        Event::Mouse(mouse::Event::CursorMoved { position }) => Some(PointerDrive::Move(*position)),
        Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) => {
            Some(PointerDrive::Release)
        }
        _ => None,
    }
}

/// Listen for window cursor motion and left-button release (sash drag).
pub fn listen_sash() -> Subscription<PointerDrive> {
    iced::event::listen_with(sash_listen)
}

/// Window-space pointer for a placed context menu.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CursorEvent {
    Move(Point),
    Context,
}

/// Cursor motion always. Right-button press opens a context menu even
/// when an editor captured the click (so Copy still has a menu). Row
/// widgets also emit [`crate::collection::ItemClick`].
pub fn listen_cursor() -> Subscription<CursorEvent> {
    iced::event::listen_with(cursor_listen)
}

fn cursor_listen(
    event: iced::Event,
    _status: iced::event::Status,
    _id: iced::window::Id,
) -> Option<CursorEvent> {
    match event {
        Event::Mouse(mouse::Event::CursorMoved { position }) => Some(CursorEvent::Move(position)),
        Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Right)) => {
            Some(CursorEvent::Context)
        }
        _ => None,
    }
}

fn sash_listen(
    event: iced::Event,
    _status: iced::event::Status,
    _id: iced::window::Id,
) -> Option<PointerDrive> {
    sash_from_window_event(&event)
}

/// Drag session for a sash (last pointer position while pressed).
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct SashDrag {
    pub pressed: bool,
    pub last: Option<f32>,
}

impl SashDrag {
    /// Apply a sash event; returns whether [`SplitState::drag`] ran.
    ///
    /// On a horizontal split in [`crate::i18n::Direction::Rtl`] the first
    /// pane sits on the right, so a pointer move toward +X shrinks it.
    pub fn apply(
        &mut self,
        state: &mut SplitState,
        event: SashEvent,
        total: f32,
        dir: crate::i18n::Direction,
    ) -> bool {
        match event {
            SashEvent::Press => {
                self.pressed = true;
                self.last = None;
                false
            }
            SashEvent::Release => {
                self.pressed = false;
                self.last = None;
                false
            }
            SashEvent::Nudge(delta) => {
                let mut delta = delta;
                if dir == crate::i18n::Direction::Rtl && state.axis == Axis::Horizontal {
                    delta = -delta;
                }
                state.drag(delta, total);
                true
            }
            SashEvent::Move(pos) => {
                let dragged = if self.pressed {
                    if let Some(prev) = self.last {
                        let mut delta = pos - prev;
                        if dir == crate::i18n::Direction::Rtl && state.axis == Axis::Horizontal {
                            delta = -delta;
                        }
                        state.drag(delta, total);
                        true
                    } else {
                        false
                    }
                } else {
                    false
                };
                self.last = Some(pos);
                dragged
            }
        }
    }
}

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

    #[test]
    fn sash_math_and_persist() {
        let s = SplitState::new(Axis::Vertical, 2.0);
        assert!(s.ratio <= 0.95);
        let mut t = SplitState::restore(Axis::Horizontal, 0.25);
        assert_eq!(t.persist(), t.ratio);
        let total = 206.0;
        assert!((t.first_size(total) + t.second_size(total) + t.sash - total).abs() < 0.01);
        t.drag(1000.0, total);
        assert!((t.ratio - t.max_ratio).abs() < 0.001);
        t.drag(-1000.0, total);
        assert!((t.ratio - t.min_ratio).abs() < 0.001);
        let mut z = SplitState::new(Axis::Horizontal, 0.5);
        assert_eq!(z.first_size(0.0), 0.0);
        z.drag(1.0, 0.0);
        let mut drag = SashDrag::default();
        let mut st = SplitState::new(Axis::Horizontal, 0.3);
        assert!(!drag.apply(
            &mut st,
            SashEvent::Press,
            200.0,
            crate::i18n::Direction::Ltr
        ));
        assert!(!drag.apply(
            &mut st,
            SashEvent::Move(10.0),
            200.0,
            crate::i18n::Direction::Ltr
        ));
        let before = st.ratio;
        assert!(drag.apply(
            &mut st,
            SashEvent::Move(30.0),
            200.0,
            crate::i18n::Direction::Ltr
        ));
        assert!(st.ratio > before);
        assert!(!drag.apply(
            &mut st,
            SashEvent::Release,
            200.0,
            crate::i18n::Direction::Ltr
        ));
        assert!(!drag.pressed);
        assert!(!drag.apply(
            &mut st,
            SashEvent::Move(40.0),
            200.0,
            crate::i18n::Direction::Ltr
        ));
        assert!(!drag.pressed);
        let before = st.ratio;
        assert!(drag.apply(
            &mut st,
            SashEvent::Nudge(16.0),
            200.0,
            crate::i18n::Direction::Ltr
        ));
        assert!(st.ratio > before);
        let before = st.ratio;
        assert!(drag.apply(
            &mut st,
            SashEvent::Nudge(16.0),
            200.0,
            crate::i18n::Direction::Rtl
        ));
        assert!(st.ratio < before);

        let moved = Event::Mouse(mouse::Event::CursorMoved {
            position: Point::new(120.0, 40.0),
        });
        let drive = sash_from_window_event(&moved).expect("cursor");
        assert_eq!(drive.into_event(Axis::Horizontal), SashEvent::Move(120.0));
        assert_eq!(drive.into_event(Axis::Vertical), SashEvent::Move(40.0));
        let released = Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left));
        assert_eq!(
            sash_from_window_event(&released)
                .unwrap()
                .into_event(Axis::Horizontal),
            SashEvent::Release
        );
        assert!(sash_from_window_event(&Event::Mouse(mouse::Event::CursorLeft)).is_none());

        let mut drag = SashDrag::default();
        let mut st = SplitState::new(Axis::Horizontal, 0.3);
        let total = 400.0;
        assert!(!drag.apply(
            &mut st,
            SashEvent::Press,
            total,
            crate::i18n::Direction::Ltr
        ));
        let first = Event::Mouse(mouse::Event::CursorMoved {
            position: Point::new(100.0, 8.0),
        });
        assert!(!drag.apply(
            &mut st,
            sash_from_window_event(&first)
                .unwrap()
                .into_event(Axis::Horizontal),
            total,
            crate::i18n::Direction::Ltr,
        ));
        let before = st.ratio;
        let second = Event::Mouse(mouse::Event::CursorMoved {
            position: Point::new(180.0, 8.0),
        });
        assert!(drag.apply(
            &mut st,
            sash_from_window_event(&second)
                .unwrap()
                .into_event(Axis::Horizontal),
            total,
            crate::i18n::Direction::Ltr,
        ));
        assert!(st.ratio > before);
        let window_delta = 180.0 - 100.0;
        assert!(window_delta > st.sash);
        assert!(!drag.apply(
            &mut st,
            sash_from_window_event(&released)
                .unwrap()
                .into_event(Axis::Horizontal),
            total,
            crate::i18n::Direction::Ltr,
        ));
        let mut rtl = SashDrag::default();
        let mut rst = SplitState::new(Axis::Horizontal, 0.3);
        assert!(!rtl.apply(
            &mut rst,
            SashEvent::Press,
            total,
            crate::i18n::Direction::Rtl
        ));
        assert!(!rtl.apply(
            &mut rst,
            SashEvent::Move(100.0),
            total,
            crate::i18n::Direction::Rtl
        ));
        let rtl_before = rst.ratio;
        assert!(rtl.apply(
            &mut rst,
            SashEvent::Move(180.0),
            total,
            crate::i18n::Direction::Rtl
        ));
        assert!(rst.ratio < rtl_before);
        let _ = listen_sash();
        assert!(sash_listen(
            moved.clone(),
            iced::event::Status::Ignored,
            iced::window::Id::unique(),
        )
        .is_some());
        let _ = listen_cursor();
        assert_eq!(
            cursor_listen(
                first,
                iced::event::Status::Ignored,
                iced::window::Id::unique(),
            ),
            Some(CursorEvent::Move(Point::new(100.0, 8.0)))
        );
        let right = Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Right));
        assert_eq!(
            cursor_listen(
                right.clone(),
                iced::event::Status::Ignored,
                iced::window::Id::unique(),
            ),
            Some(CursorEvent::Context)
        );
        assert_eq!(
            cursor_listen(
                right,
                iced::event::Status::Captured,
                iced::window::Id::unique(),
            ),
            Some(CursorEvent::Context)
        );
        assert!(cursor_listen(
            released,
            iced::event::Status::Ignored,
            iced::window::Id::unique(),
        )
        .is_none());
    }
}