plushie 0.7.0

Desktop GUI framework for Rust
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
//! Interactive widget builders.
//!
//! Widgets that respond to user input. All interactive widgets
//! require an explicit ID since they generate events that must
//! be matchable in `update/2`.
//!
//! ```ignore
//! use plushie::prelude::*;
//!
//! let view = button("save", "Save")
//!     .style(Style::primary())
//!     .width(Length::Fixed(120.0));
//!
//! let area = pointer_area("canvas")
//!     .child(my_canvas_view);
//! ```

use super::PropMap;
use crate::View;
use crate::derive_support::PlushieType;
use crate::types::*;

// ---------------------------------------------------------------------------
// ButtonBuilder
// ---------------------------------------------------------------------------

/// Builder for a clickable button.
pub struct ButtonBuilder {
    id: String,
    props: PropMap,
}

/// Create a button with the given ID and label text.
pub fn button(id: &str, label: &str) -> ButtonBuilder {
    let mut props = PropMap::new();
    super::set_prop(&mut props, "label", label);
    ButtonBuilder {
        id: id.to_string(),
        props,
    }
}

impl ButtonBuilder {
    /// Apply a named or custom style.
    pub fn style(mut self, s: impl Into<Style>) -> Self {
        let s = s.into();
        super::set_prop(&mut self.props, "style", super::style_to_value(&s));
        self
    }

    /// Disable this widget.
    pub fn disabled(mut self, v: bool) -> Self {
        super::set_prop(&mut self.props, "disabled", v);
        self
    }

    /// Set the preferred width.
    pub fn width(mut self, w: impl Into<Length>) -> Self {
        super::set_prop(&mut self.props, "width", super::length_to_value(w.into()));
        self
    }

    /// Set the preferred height.
    pub fn height(mut self, h: impl Into<Length>) -> Self {
        super::set_prop(&mut self.props, "height", super::length_to_value(h.into()));
        self
    }

    /// Set the inner padding.
    pub fn padding(mut self, p: impl Into<Padding>) -> Self {
        super::set_prop(
            &mut self.props,
            "padding",
            super::padding_to_value(p.into()),
        );
        self
    }

    /// Clip content that overflows the container.
    pub fn clip(mut self, v: bool) -> Self {
        super::set_prop(&mut self.props, "clip", v);
        self
    }

    /// Set the Alt-key mnemonic used to activate this button.
    pub fn mnemonic(mut self, mnemonic: char) -> Self {
        super::set_prop(&mut self.props, "mnemonic", mnemonic.to_string());
        self
    }

    /// Alias for [`Self::mnemonic`].
    pub fn access_key(self, access_key: char) -> Self {
        self.mnemonic(access_key)
    }

    /// Maximum events per second (0 = unbounded).
    pub fn event_rate(mut self, rate: u32) -> Self {
        super::set_prop(&mut self.props, "event_rate", rate);
        self
    }

    /// Attach accessibility metadata.
    pub fn a11y(mut self, a11y: &A11y) -> Self {
        super::set_prop(&mut self.props, "a11y", a11y.wire_encode());
        self
    }
}

impl From<ButtonBuilder> for View {
    fn from(b: ButtonBuilder) -> Self {
        super::view_leaf(b.id, "button", b.props)
    }
}

// ---------------------------------------------------------------------------
// PointerAreaBuilder
// ---------------------------------------------------------------------------

/// Builder for a pointer event capture region.
///
/// Wraps a single child and reports pointer events (press, release,
/// enter, exit, move, scroll) on the child's bounds.
pub struct PointerAreaBuilder {
    id: String,
    props: PropMap,
    child: Option<View>,
}

/// Create a pointer area with the given ID.
pub fn pointer_area(id: &str) -> PointerAreaBuilder {
    PointerAreaBuilder {
        id: id.to_string(),
        props: PropMap::new(),
        child: None,
    }
}

impl PointerAreaBuilder {
    /// Handler for press events.
    pub fn on_press(mut self, tag: &str) -> Self {
        super::set_prop(&mut self.props, "on_press", tag);
        self
    }

    /// Handler for release events.
    pub fn on_release(mut self, tag: &str) -> Self {
        super::set_prop(&mut self.props, "on_release", tag);
        self
    }

    /// Handler when the pointer enters.
    pub fn on_enter(mut self, v: bool) -> Self {
        super::set_prop(&mut self.props, "on_enter", v);
        self
    }

    /// Handler when the pointer leaves.
    pub fn on_exit(mut self, v: bool) -> Self {
        super::set_prop(&mut self.props, "on_exit", v);
        self
    }

    /// Handler for pointer-move events.
    pub fn on_move(mut self, v: bool) -> Self {
        super::set_prop(&mut self.props, "on_move", v);
        self
    }

    /// Handler for scroll events.
    pub fn on_scroll(mut self, v: bool) -> Self {
        super::set_prop(&mut self.props, "on_scroll", v);
        self
    }

    /// Handler for middle-press events.
    pub fn on_middle_press(mut self, v: bool) -> Self {
        super::set_prop(&mut self.props, "on_middle_press", v);
        self
    }

    /// Handler for right-press events.
    pub fn on_right_press(mut self, v: bool) -> Self {
        super::set_prop(&mut self.props, "on_right_press", v);
        self
    }

    /// Enable right mouse button release events.
    pub fn on_right_release(mut self, v: bool) -> Self {
        super::set_prop(&mut self.props, "on_right_release", v);
        self
    }

    /// Enable middle mouse button release events.
    pub fn on_middle_release(mut self, v: bool) -> Self {
        super::set_prop(&mut self.props, "on_middle_release", v);
        self
    }

    /// Enable double-click events.
    pub fn on_double_click(mut self, v: bool) -> Self {
        super::set_prop(&mut self.props, "on_double_click", v);
        self
    }

    /// Mouse cursor to show on hover.
    pub fn cursor(mut self, cursor: CursorStyle) -> Self {
        super::set_prop(&mut self.props, "cursor", cursor.wire_encode());
        self
    }

    /// Maximum events per second (0 = unbounded).
    pub fn event_rate(mut self, rate: u32) -> Self {
        super::set_prop(&mut self.props, "event_rate", rate);
        self
    }

    /// Attach accessibility metadata.
    pub fn a11y(mut self, a11y: &A11y) -> Self {
        super::set_prop(&mut self.props, "a11y", a11y.wire_encode());
        self
    }

    /// Set the single child of this pointer area.
    pub fn child(mut self, child: impl Into<View>) -> Self {
        self.child = Some(child.into());
        self
    }
}

impl From<PointerAreaBuilder> for View {
    fn from(b: PointerAreaBuilder) -> Self {
        let children = b.child.into_iter().collect();
        super::view_node(b.id, "pointer_area", b.props, children)
    }
}

// ---------------------------------------------------------------------------
// SensorBuilder
// ---------------------------------------------------------------------------

/// Builder for a layout sensor.
///
/// Wraps a single child and reports layout events (size changes,
/// position) without capturing pointer input.
pub struct SensorBuilder {
    id: String,
    props: PropMap,
    child: Option<View>,
}

/// Create a sensor with the given ID.
pub fn sensor(id: &str) -> SensorBuilder {
    SensorBuilder {
        id: id.to_string(),
        props: PropMap::new(),
        child: None,
    }
}

impl SensorBuilder {
    /// Delay in milliseconds before emitting events.
    pub fn delay(mut self, ms: u32) -> Self {
        super::set_prop(&mut self.props, "delay", ms);
        self
    }

    /// Distance in pixels to anticipate visibility.
    pub fn anticipate(mut self, pixels: f32) -> Self {
        super::set_prop(&mut self.props, "anticipate", pixels);
        self
    }

    /// Enable resize events with a custom event tag.
    pub fn on_resize(mut self, tag: &str) -> Self {
        super::set_prop(&mut self.props, "on_resize", tag);
        self
    }

    /// Maximum events per second (0 = unbounded).
    pub fn event_rate(mut self, rate: u32) -> Self {
        super::set_prop(&mut self.props, "event_rate", rate);
        self
    }

    /// Attach accessibility metadata.
    pub fn a11y(mut self, a11y: &A11y) -> Self {
        super::set_prop(&mut self.props, "a11y", a11y.wire_encode());
        self
    }

    /// Set the single child of this sensor.
    pub fn child(mut self, child: impl Into<View>) -> Self {
        self.child = Some(child.into());
        self
    }
}

impl From<SensorBuilder> for View {
    fn from(b: SensorBuilder) -> Self {
        let children = b.child.into_iter().collect();
        super::view_node(b.id, "sensor", b.props, children)
    }
}

// ---------------------------------------------------------------------------
// TooltipBuilder
// ---------------------------------------------------------------------------

/// Builder for a tooltip wrapper.
///
/// Wraps a single child and shows a tooltip on hover.
pub struct TooltipBuilder {
    id: String,
    props: PropMap,
    child: Option<View>,
}

/// Create a tooltip with the given ID and tip text.
pub fn tooltip(id: &str, tip: &str) -> TooltipBuilder {
    let mut props = PropMap::new();
    super::set_prop(&mut props, "tip", tip);
    TooltipBuilder {
        id: id.to_string(),
        props,
        child: None,
    }
}

impl TooltipBuilder {
    /// Set the window's screen position (pixels).
    pub fn position(mut self, pos: Position) -> Self {
        super::set_prop(&mut self.props, "position", pos.wire_encode());
        self
    }

    /// Gap between children, in pixels.
    pub fn gap(mut self, v: impl Into<Animatable<f32>>) -> Self {
        super::set_prop(&mut self.props, "gap", v.into().wire_encode());
        self
    }

    /// Tooltip padding in pixels.
    pub fn padding(mut self, v: impl Into<Animatable<f32>>) -> Self {
        super::set_prop(&mut self.props, "padding", v.into().wire_encode());
        self
    }

    /// Keep tooltip within the viewport bounds.
    pub fn snap_within_viewport(mut self, v: bool) -> Self {
        super::set_prop(&mut self.props, "snap_within_viewport", v);
        self
    }

    /// Delay in milliseconds before showing the tooltip.
    pub fn delay(mut self, ms: u32) -> Self {
        super::set_prop(&mut self.props, "delay", ms);
        self
    }

    /// Apply a named or custom style.
    pub fn style(mut self, s: impl Into<Style>) -> Self {
        let s = s.into();
        super::set_prop(&mut self.props, "style", super::style_to_value(&s));
        self
    }

    /// Maximum events per second (0 = unbounded).
    pub fn event_rate(mut self, rate: u32) -> Self {
        super::set_prop(&mut self.props, "event_rate", rate);
        self
    }

    /// Attach accessibility metadata.
    pub fn a11y(mut self, a11y: &A11y) -> Self {
        super::set_prop(&mut self.props, "a11y", a11y.wire_encode());
        self
    }

    /// Set the single child of this tooltip.
    pub fn child(mut self, child: impl Into<View>) -> Self {
        self.child = Some(child.into());
        self
    }
}

impl From<TooltipBuilder> for View {
    fn from(b: TooltipBuilder) -> Self {
        let children = b.child.into_iter().collect();
        super::view_node(b.id, "tooltip", b.props, children)
    }
}

// ---------------------------------------------------------------------------
// ThemerBuilder
// ---------------------------------------------------------------------------

/// Builder for a theme override container.
///
/// Applies a different theme to its child subtree.
pub struct ThemerBuilder {
    id: String,
    props: PropMap,
    child: Option<View>,
}

/// Create a themer with the given ID.
pub fn themer(id: &str) -> ThemerBuilder {
    ThemerBuilder {
        id: id.to_string(),
        props: PropMap::new(),
        child: None,
    }
}

impl ThemerBuilder {
    /// Set the window theme.
    pub fn theme(mut self, theme: impl Into<Theme>) -> Self {
        let theme: Theme = theme.into();
        super::set_prop(&mut self.props, "theme", theme.wire_encode());
        self
    }

    /// Maximum events per second (0 = unbounded).
    pub fn event_rate(mut self, rate: u32) -> Self {
        super::set_prop(&mut self.props, "event_rate", rate);
        self
    }

    /// Attach accessibility metadata.
    pub fn a11y(mut self, a11y: &A11y) -> Self {
        super::set_prop(&mut self.props, "a11y", a11y.wire_encode());
        self
    }

    /// Set the single child of this themer.
    pub fn child(mut self, child: impl Into<View>) -> Self {
        self.child = Some(child.into());
        self
    }
}

impl From<ThemerBuilder> for View {
    fn from(b: ThemerBuilder) -> Self {
        let children = b.child.into_iter().collect();
        super::view_node(b.id, "themer", b.props, children)
    }
}

// ---------------------------------------------------------------------------
// OverlayBuilder
// ---------------------------------------------------------------------------

/// Builder for a popup overlay container.
///
/// Renders its children as an overlay positioned relative to
/// the parent widget.
pub struct OverlayBuilder {
    id: String,
    props: PropMap,
    children: Vec<View>,
}

/// Create an overlay with the given ID.
pub fn overlay(id: &str) -> OverlayBuilder {
    OverlayBuilder {
        id: id.to_string(),
        props: PropMap::new(),
        children: vec![],
    }
}

impl OverlayBuilder {
    /// Set the window's screen position (pixels).
    pub fn position(mut self, pos: Position) -> Self {
        super::set_prop(&mut self.props, "position", pos.wire_encode());
        self
    }

    /// Set the alignment.
    pub fn align(mut self, a: Align) -> Self {
        super::set_prop(&mut self.props, "align", super::cross_align_to_value(a));
        self
    }

    /// Auto-flip when overflowing the viewport.
    pub fn flip(mut self, v: bool) -> Self {
        super::set_prop(&mut self.props, "flip", v);
        self
    }

    /// Gap between children, in pixels.
    pub fn gap(mut self, v: impl Into<Animatable<f32>>) -> Self {
        super::set_prop(&mut self.props, "gap", v.into().wire_encode());
        self
    }

    /// Horizontal offset in pixels after positioning.
    pub fn offset_x(mut self, v: impl Into<Animatable<f32>>) -> Self {
        super::set_prop(&mut self.props, "offset_x", v.into().wire_encode());
        self
    }

    /// Vertical offset in pixels after positioning.
    pub fn offset_y(mut self, v: impl Into<Animatable<f32>>) -> Self {
        super::set_prop(&mut self.props, "offset_y", v.into().wire_encode());
        self
    }

    /// Set the preferred width.
    pub fn width(mut self, w: impl Into<Length>) -> Self {
        super::set_prop(&mut self.props, "width", super::length_to_value(w.into()));
        self
    }

    /// Maximum events per second (0 = unbounded).
    pub fn event_rate(mut self, rate: u32) -> Self {
        super::set_prop(&mut self.props, "event_rate", rate);
        self
    }

    /// Attach accessibility metadata.
    pub fn a11y(mut self, a11y: &A11y) -> Self {
        super::set_prop(&mut self.props, "a11y", a11y.wire_encode());
        self
    }

    /// Append a child.
    pub fn child(mut self, child: impl Into<View>) -> Self {
        self.children.push(child.into());
        self
    }

    /// Replace the child list.
    pub fn children<I, V>(mut self, items: I) -> Self
    where
        I: IntoIterator<Item = V>,
        V: Into<View>,
    {
        self.children.extend(items.into_iter().map(Into::into));
        self
    }
}

impl From<OverlayBuilder> for View {
    fn from(b: OverlayBuilder) -> Self {
        super::view_node(b.id, "overlay", b.props, b.children)
    }
}

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

    #[test]
    fn button_mnemonic_builder_sets_wire_prop() {
        let view: View = button("save", "Save").mnemonic('S').into();

        assert_eq!(view.type_name(), "button");
        assert_eq!(view.props().get_str("mnemonic"), Some("S"));
    }

    #[test]
    fn button_access_key_builder_uses_mnemonic_wire_prop() {
        let view: View = button("open", "Open").access_key('O').into();

        assert_eq!(view.props().get_str("mnemonic"), Some("O"));
    }
}