plushie-core 0.4.0

Extension SDK for Plushie
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
//! Interactive wrapper widgets -- add behavior to child content.
//!
//! - `button` -- clickable wrapper with press/release events, style variants
//! - `mouse_area` -- invisible overlay that captures mouse events (enter,
//!   exit, move, scroll, right/middle click)
//! - `sensor` -- debounced resize observer that reports container dimensions
//! - `tooltip` -- popup hint shown on hover or keyboard focus
//! - `themer` -- overrides the iced theme for its subtree
//! - `window` -- top-level window node (rendered as a container)
//! - `overlay` -- positioned popup anchored to a sibling element

use std::time::Duration;

use iced::widget::{Space, button, container, mouse_area, sensor, text, tooltip};
use iced::{Element, Fill, Length, mouse, widget};

use super::caches::WidgetCaches;
use super::helpers::*;
use crate::extensions::RenderCtx;
use crate::message::Message;
use crate::protocol::TreeNode;

// ---------------------------------------------------------------------------
// Button
// ---------------------------------------------------------------------------

pub(crate) fn render_button<'a>(node: &'a TreeNode, ctx: RenderCtx<'a>) -> Element<'a, Message> {
    let props = node.props.as_object();
    let id = node.id.clone();

    // Button can have either a text label or child content
    let child: Element<'a, Message> = if !node.children.is_empty() {
        node.children
            .first()
            .map(|c| ctx.render_child(c))
            .unwrap_or_else(|| Space::new().into())
    } else {
        let label = prop_str(props, "label")
            .or_else(|| prop_str(props, "content"))
            .unwrap_or_default();
        text(label).into()
    };

    let padding = parse_padding_value(props);
    let width = prop_length(props, "width", Length::Shrink);
    let height = prop_length(props, "height", Length::Shrink);
    let clip = prop_bool_default(props, "clip", false);
    let disabled =
        prop_bool_default(props, "disabled", false) || !prop_bool_default(props, "enabled", true);

    let mut b = button(child).width(width).height(height).clip(clip);

    if let Some(p) = padding {
        b = b.padding(p);
    }

    if !disabled {
        b = b.on_press(Message::Click(id));
    }

    // Style: string name or style map object
    if let Some(style_val) = props.and_then(|p| p.get("style")) {
        if let Some(style_name) = style_val.as_str() {
            b = match style_name {
                "primary" => b.style(button::primary),
                "secondary" => b.style(button::secondary),
                "success" => b.style(button::success),
                "warning" => b.style(button::warning),
                "danger" => b.style(button::danger),
                "text" => b.style(button::text),
                "background" => b.style(button::background),
                "subtle" => b.style(button::subtle),
                _ => {
                    log::warn!(
                        "unknown style {:?} for widget type {:?}, using default",
                        style_name,
                        "button"
                    );
                    b.style(button::primary)
                }
            };
        } else if let Some(obj) = style_val.as_object() {
            let ov = get_style_overrides(&node.id, obj, ctx.caches);
            b = b.style(move |theme: &iced::Theme, status| {
                let mut style = match ov.preset_base.as_deref() {
                    Some("primary") => button::primary(theme, status),
                    Some("secondary") => button::secondary(theme, status),
                    Some("success") => button::success(theme, status),
                    Some("danger") => button::danger(theme, status),
                    Some("warning") => button::warning(theme, status),
                    Some("text") => button::text(theme, status),
                    Some("background") => button::background(theme, status),
                    Some("subtle") => button::subtle(theme, status),
                    _ => button::primary(theme, status),
                };
                apply_button_fields(&mut style, &ov.base);
                match status {
                    button::Status::Hovered => {
                        if let Some(ref f) = ov.hovered {
                            apply_button_fields(&mut style, f);
                        } else {
                            style.background = auto_derive_hover_bg(style.background);
                        }
                    }
                    button::Status::Pressed => {
                        if let Some(ref f) = ov.pressed {
                            apply_button_fields(&mut style, f);
                        }
                    }
                    button::Status::Disabled => {
                        if let Some(ref f) = ov.disabled {
                            apply_button_fields(&mut style, f);
                        } else {
                            style.background = auto_derive_disabled_bg(style.background);
                            style.text_color = auto_derive_disabled_text(style.text_color);
                            style.border = auto_derive_disabled_border(style.border);
                            style.shadow = auto_derive_disabled_shadow(style.shadow);
                        }
                    }
                    _ => {}
                }
                style
            });
        }
    }

    container(b).id(widget::Id::from(node.id.clone())).into()
}

// ---------------------------------------------------------------------------
// MouseArea
// ---------------------------------------------------------------------------

pub(crate) fn render_mouse_area<'a>(
    node: &'a TreeNode,
    ctx: RenderCtx<'a>,
) -> Element<'a, Message> {
    let props = node.props.as_object();
    let child: Element<'a, Message> = node
        .children
        .first()
        .map(|c| ctx.render_child(c))
        .unwrap_or_else(|| Space::new().into());

    let id = node.id.clone();
    let release_id = format!("{}:release", node.id);

    let mut ma = mouse_area(child)
        .on_press(Message::Click(id))
        .on_release(Message::Click(release_id));

    // Conditional event handlers (opt-in via boolean props)
    if prop_bool_default(props, "on_middle_press", false) {
        let ev_id = node.id.clone();
        ma = ma.on_middle_press(Message::MouseAreaEvent(ev_id, "middle_press".into()));
    }
    if prop_bool_default(props, "on_right_press", false) {
        let ev_id = node.id.clone();
        ma = ma.on_right_press(Message::MouseAreaEvent(ev_id, "right_press".into()));
    }
    if prop_bool_default(props, "on_right_release", false) {
        let ev_id = node.id.clone();
        ma = ma.on_right_release(Message::MouseAreaEvent(ev_id, "right_release".into()));
    }
    if prop_bool_default(props, "on_middle_release", false) {
        let ev_id = node.id.clone();
        ma = ma.on_middle_release(Message::MouseAreaEvent(ev_id, "middle_release".into()));
    }
    if prop_bool_default(props, "on_double_click", false) {
        let ev_id = node.id.clone();
        ma = ma.on_double_click(Message::MouseAreaEvent(ev_id, "double_click".into()));
    }
    if prop_bool_default(props, "on_enter", false) {
        let ev_id = node.id.clone();
        ma = ma.on_enter(Message::MouseAreaEvent(ev_id, "enter".into()));
    }
    if prop_bool_default(props, "on_exit", false) {
        let ev_id = node.id.clone();
        ma = ma.on_exit(Message::MouseAreaEvent(ev_id, "exit".into()));
    }
    if prop_bool_default(props, "on_move", false) {
        let ev_id = node.id.clone();
        ma = ma.on_move(move |p| Message::MouseAreaMove(ev_id.clone(), p.x, p.y));
    }
    if prop_bool_default(props, "on_scroll", false) {
        let ev_id = node.id.clone();
        ma = ma.on_scroll(move |delta| {
            let (dx, dy) = match delta {
                mouse::ScrollDelta::Lines { x, y } => (x, y),
                mouse::ScrollDelta::Pixels { x, y } => (x, y),
            };
            Message::MouseAreaScroll(ev_id.clone(), dx, dy)
        });
    }

    if let Some(cursor) = prop_str(props, "cursor")
        && let Some(interaction) = parse_interaction(&cursor)
    {
        ma = ma.interaction(interaction);
    }

    ma.into()
}

// ---------------------------------------------------------------------------
// Sensor
// ---------------------------------------------------------------------------

pub(crate) fn render_sensor<'a>(node: &'a TreeNode, ctx: RenderCtx<'a>) -> Element<'a, Message> {
    let child: Element<'a, Message> = node
        .children
        .first()
        .map(|c| ctx.render_child(c))
        .unwrap_or_else(|| Space::new().into());

    // Sensor needs a key. Use the node id.
    let id = node.id.clone();
    let show_id = node.id.clone();
    let resize_id = node.id.clone();
    let hide_id = format!("{}:hide", node.id);

    let props = node.props.as_object();

    let mut s = sensor(child)
        .key(id)
        .on_show(move |size| {
            Message::SensorResize(format!("{}:show", show_id), size.width, size.height)
        })
        .on_resize(move |size| Message::SensorResize(resize_id.clone(), size.width, size.height))
        .on_hide(Message::Click(hide_id));

    if let Some(d) = prop_f64(props, "delay") {
        s = s.delay(Duration::from_millis(d as u64));
    }
    if let Some(a) = prop_f32(props, "anticipate") {
        s = s.anticipate(a);
    }

    s.into()
}

// ---------------------------------------------------------------------------
// Tooltip
// ---------------------------------------------------------------------------

/// Render a tooltip widget that shows a popup hint on hover/focus.
///
/// # Accessibility
///
/// The tooltip `tip` text is rendered visually on hover but is not
/// automatically exposed as the child widget's accessible description.
/// For AT users to hear the tooltip content, the host should wire the
/// tooltip text into the child's `a11y.description` prop, or use
/// `a11y.described_by` to point to a separate text node containing the
/// same content. iced's tooltip widget itself does not currently emit
/// an accessible `Tooltip` role -- the visual popup appears/disappears
/// without AT notification.
pub(crate) fn render_tooltip<'a>(node: &'a TreeNode, ctx: RenderCtx<'a>) -> Element<'a, Message> {
    let props = node.props.as_object();
    let tip = prop_str(props, "tip").unwrap_or_default();
    let gap = prop_f32(props, "gap");
    let position = prop_str(props, "position")
        .map(|s| match s.to_ascii_lowercase().as_str() {
            "bottom" => tooltip::Position::Bottom,
            "left" => tooltip::Position::Left,
            "right" => tooltip::Position::Right,
            "follow_cursor" | "follow" => tooltip::Position::FollowCursor,
            _ => tooltip::Position::Top,
        })
        .unwrap_or(tooltip::Position::Top);

    let child: Element<'a, Message> = node
        .children
        .first()
        .map(|c| ctx.render_child(c))
        .unwrap_or_else(|| Space::new().into());

    let mut tt = tooltip(child, text(tip), position);
    if let Some(g) = gap {
        tt = tt.gap(g);
    }

    // Tooltip padding is a single f32 value (not per-side)
    if let Some(p) = prop_f32(props, "padding") {
        tt = tt.padding(p);
    }

    let snap = prop_bool_default(props, "snap_within_viewport", true);
    tt = tt.snap_within_viewport(snap);

    if let Some(d) = prop_f64(props, "delay") {
        tt = tt.delay(Duration::from_millis(d as u64));
    }

    // Style: string name or style map (tooltip uses container::Style)
    if let Some(style_val) = props.and_then(|p| p.get("style")) {
        if let Some(style_name) = style_val.as_str() {
            tt = match style_name {
                "transparent" => tt.style(container::transparent),
                "rounded_box" => tt.style(container::rounded_box),
                "bordered_box" => tt.style(container::bordered_box),
                "dark" => tt.style(container::dark),
                "primary" => tt.style(container::primary),
                "secondary" => tt.style(container::secondary),
                "success" => tt.style(container::success),
                "danger" => tt.style(container::danger),
                "warning" => tt.style(container::warning),
                _ => {
                    log::warn!(
                        "unknown style {:?} for widget type {:?}, using default",
                        style_name,
                        "tooltip"
                    );
                    tt
                }
            };
        } else if let Some(obj) = style_val.as_object() {
            let ov = get_style_overrides(&node.id, obj, ctx.caches);
            tt = tt.style(move |_theme| container_style_from_base(&ov.base));
        }
    }

    tt.into()
}

// ---------------------------------------------------------------------------
// Themer (applies a sub-theme to child content)
// ---------------------------------------------------------------------------

pub(crate) fn render_themer<'a>(node: &'a TreeNode, ctx: RenderCtx<'a>) -> Element<'a, Message> {
    // The resolved theme lives in ctx.caches.themer_themes (populated by
    // ensure_caches) so we can borrow it with lifetime 'a for child rendering.
    let cached_theme = ctx.caches.themer_themes.get(&node.id);
    let child_theme = cached_theme.unwrap_or(ctx.theme);

    // Build a child ctx with the resolved sub-theme so children render
    // against the overridden theme.
    let child_ctx = ctx.with_theme(child_theme);

    let child: Element<'a, Message> = node
        .children
        .first()
        .map(|c| child_ctx.render_child(c))
        .unwrap_or_else(|| Space::new().into());

    // Clone the cached theme into an owned Option for the Themer wrapper.
    let themer_theme = cached_theme.cloned();
    iced::widget::Themer::new(themer_theme, child).into()
}

// ---------------------------------------------------------------------------
// Window (top-level container)
// ---------------------------------------------------------------------------

pub(crate) fn render_window<'a>(node: &'a TreeNode, ctx: RenderCtx<'a>) -> Element<'a, Message> {
    let props = node.props.as_object();
    let padding = parse_padding_value(props);
    let width = prop_length(props, "width", Fill);
    let height = prop_length(props, "height", Fill);

    let child: Element<'a, Message> = node
        .children
        .first()
        .map(|c| ctx.render_child(c))
        .unwrap_or_else(|| Space::new().into());

    let mut c = container(child).width(width).height(height);

    if let Some(p) = padding {
        c = c.padding(p);
    }

    c.into()
}

// ---------------------------------------------------------------------------
// Overlay
// ---------------------------------------------------------------------------

pub(crate) fn render_overlay<'a>(node: &'a TreeNode, ctx: RenderCtx<'a>) -> Element<'a, Message> {
    use super::overlay;

    let props = node.props.as_object();
    let position = prop_str(props, "position").unwrap_or_else(|| "below".to_string());
    let gap = prop_f32(props, "gap").unwrap_or(0.0);
    let offset_x = prop_f32(props, "offset_x").unwrap_or(0.0);
    let offset_y = prop_f32(props, "offset_y").unwrap_or(0.0);
    let flip = prop_bool_default(props, "flip", false);
    let align = match prop_str(props, "align").as_deref() {
        Some("start") => overlay::Align::Start,
        Some("end") => overlay::Align::End,
        _ => overlay::Align::Center,
    };

    let children = &node.children;
    if children.len() < 2 {
        return text(format!("overlay requires 2 children (id={})", node.id)).into();
    }

    let anchor = ctx.render_child(&children[0]);
    let content = ctx.render_child(&children[1]);

    let pos = match position.as_str() {
        "above" => overlay::Position::Above,
        "left" => overlay::Position::Left,
        "right" => overlay::Position::Right,
        _ => overlay::Position::Below,
    };

    overlay::OverlayWrapper::new(anchor, content, pos, gap, offset_x, offset_y, flip, align).into()
}

// ---------------------------------------------------------------------------
// Cache ensure function
// ---------------------------------------------------------------------------

pub(crate) fn ensure_themer_cache(node: &TreeNode, caches: &mut WidgetCaches) {
    let props = node.props.as_object();
    if let Some(resolved) = props
        .and_then(|p| p.get("theme"))
        .and_then(crate::theming::resolve_theme_only)
    {
        caches.themer_themes.insert(node.id.clone(), resolved);
    } else {
        // No valid theme prop -- remove stale cache entry if present.
        caches.themer_themes.remove(&node.id);
    }
}