rust_widgets 2.9.2

Pure Rust cross-platform native GUI library with hardware-adaptive rendering, 180 widgets, touch/gesture support, i18n, and SVG-pipeline-accurate output
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
// SPDX-FileCopyrightText: Copyright (c) 2026 Mike Li/Mikewolfli/Wei Li(mikewolfli@163.com)
// SPDX-License-Identifier: MIT

//! `SwipeActions` — a multi-action background for a swiped row (COMP-A03).
//!
//! `SwipeToDismiss` reveals one label per side. The reference toolkit's swiped rows reveal
//! several actions per side ("delete", "pin", "mark"), each with its own colour and icon,
//! appearing in order as the row travels. This companion owns that arrangement: the host
//! feeds it the row's offset and draws it behind the content, and it reports which action a
//! release landed on.

use crate::compat::{String, ToString, Vec};
use crate::core::{Color, HorizontalAlignment, Point, Rect, Size};
use crate::event::{Event, EventHandler};
use crate::render::RenderContext;
use crate::signal::Signal1;
use crate::widget::capability::coercion::expect_u32;
use crate::widget::capability::properties_trait::{base_property_get, base_property_set};
use crate::widget::capability::types::{CapabilityAccessError, CapabilityValue};
use crate::widget::capability::WidgetProperties;
use crate::widget::metrics::role_font;
use crate::widget::{BaseWidget, Draw, Widget, WidgetKind};
use crate::{impl_draw_bridge, impl_widget_property_hooks, property_names_of};

use super::helpers::expect_color;

/// Which side of the row an action belongs to.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SwipeSide {
    /// Revealed by swiping toward the trailing edge (a positive offset).
    Leading,
    /// Revealed by swiping toward the leading edge (a negative offset).
    Trailing,
}

impl SwipeSide {
    /// The published token.
    pub fn as_str(self) -> &'static str {
        match self {
            SwipeSide::Leading => "leading",
            SwipeSide::Trailing => "trailing",
        }
    }

    /// Parses the published token.
    pub fn from_name(name: &str) -> Option<Self> {
        Some(match name {
            "leading" => SwipeSide::Leading,
            "trailing" => SwipeSide::Trailing,
            _ => return None,
        })
    }
}

/// One action revealed behind a swiped row.
#[derive(Debug, Clone, PartialEq)]
pub struct SwipeAction {
    /// The action's label.
    pub text: String,
    /// An optional icon token.
    pub icon: Option<String>,
    /// An optional fill, overriding the semantic destructive red.
    pub fill: Option<Color>,
}

impl SwipeAction {
    /// Creates an action with the default (destructive) fill.
    pub fn new(text: impl Into<String>) -> Self {
        Self { text: text.into(), icon: None, fill: None }
    }

    /// Adds an icon token.
    pub fn with_icon(mut self, icon: impl Into<String>) -> Self {
        self.icon = Some(icon.into());
        self
    }

    /// Sets the fill colour.
    pub fn with_fill(mut self, fill: Color) -> Self {
        self.fill = Some(fill);
        self
    }
}

/// The multi-action background painted behind a swiped row.
pub struct SwipeActions {
    base: BaseWidget,
    leading_actions: Vec<SwipeAction>,
    trailing_actions: Vec<SwipeAction>,
    max_reveal_width: u32,
    icon_side: u32,
    /// The row's current horizontal offset, set by the host.
    offset: f32,
    /// Emitted with the side and index of the action a press landed on.
    pub action_triggered: Signal1<(SwipeSide, usize)>,
}

impl Default for SwipeActions {
    fn default() -> Self {
        Self::new()
    }
}

impl SwipeActions {
    /// Creates an empty background. The host sets its geometry through [`Widget::set_geometry`].
    pub fn new() -> Self {
        Self {
            base: BaseWidget::new(
                WidgetKind::SwipeToDismiss,
                Rect::new(0, 0, 0, 0),
                "SwipeActions",
            ),
            leading_actions: Vec::new(),
            trailing_actions: Vec::new(),
            max_reveal_width: 240,
            icon_side: 18,
            offset: 0.0,
            action_triggered: Signal1::new(),
        }
    }

    /// Replaces the leading-side actions.
    pub fn set_leading_actions(&mut self, actions: Vec<SwipeAction>) {
        self.leading_actions = actions;
        self.base.request_redraw();
    }

    /// Returns the leading-side actions.
    pub fn leading_actions(&self) -> &[SwipeAction] {
        &self.leading_actions
    }

    /// Replaces the trailing-side actions.
    pub fn set_trailing_actions(&mut self, actions: Vec<SwipeAction>) {
        self.trailing_actions = actions;
        self.base.request_redraw();
    }

    /// Returns the trailing-side actions.
    pub fn trailing_actions(&self) -> &[SwipeAction] {
        &self.trailing_actions
    }

    /// The furthest either side may be revealed.
    pub fn max_reveal_width(&self) -> u32 {
        self.max_reveal_width
    }

    /// Sets the furthest either side may be revealed.
    pub fn set_max_reveal_width(&mut self, width: u32) {
        if self.max_reveal_width == width {
            return;
        }
        self.max_reveal_width = width;
        self.base.request_redraw();
    }

    /// The square side of an action's icon.
    pub fn icon_side(&self) -> u32 {
        self.icon_side
    }

    /// Sets the square side of an action's icon.
    pub fn set_icon_side(&mut self, side: u32) {
        if self.icon_side == side {
            return;
        }
        self.icon_side = side;
        self.base.request_redraw();
    }

    /// Reports the row's current offset so the revealed boxes follow the gesture.
    pub fn set_offset(&mut self, offset: f32) {
        if !offset.is_finite() || (self.offset - offset).abs() < f32::EPSILON {
            return;
        }
        self.offset = offset;
        self.base.request_redraw();
    }

    /// The row's current offset.
    pub fn offset(&self) -> f32 {
        self.offset
    }

    /// The revealed box of `index` on `side`, or `None` when the offset has not uncovered it.
    pub fn action_rect(&self, side: SwipeSide, index: usize) -> Option<Rect> {
        self.action_rect_at(side, index, self.offset)
    }

    /// The action under `pos` for a given offset, or `None`.
    ///
    /// Taking the offset as an argument lets a host hit-test the release position against the
    /// same arithmetic a later `set_offset` will draw with.
    pub fn action_at(&self, offset: f32, pos: Point) -> Option<(SwipeSide, usize)> {
        for side in [SwipeSide::Leading, SwipeSide::Trailing] {
            let actions = self.actions_for(side);
            for index in 0..actions.len() {
                if self
                    .action_rect_at(side, index, offset)
                    .is_some_and(|rect| rect.contains_point(pos))
                {
                    return Some((side, index));
                }
            }
        }
        None
    }

    /// The actions for `side`.
    fn actions_for(&self, side: SwipeSide) -> &[SwipeAction] {
        match side {
            SwipeSide::Leading => &self.leading_actions,
            SwipeSide::Trailing => &self.trailing_actions,
        }
    }

    /// The width one action claims on `side`.
    fn action_width(&self, side: SwipeSide) -> u32 {
        let count = self.actions_for(side).len().max(1) as u32;
        (self.max_reveal_width / count).max(1)
    }

    /// The box of `index` at `offset`, clipped to what the offset has revealed.
    fn action_rect_at(&self, side: SwipeSide, index: usize, offset: f32) -> Option<Rect> {
        let actions = self.actions_for(side);
        if index >= actions.len() {
            return None;
        }
        let rect = self.geometry();
        if rect.width == 0 || rect.height == 0 {
            return None;
        }
        let width = self.action_width(side);
        let reveal = match side {
            SwipeSide::Leading => offset.max(0.0).min(self.max_reveal_width as f32) as u32,
            SwipeSide::Trailing => (-offset).max(0.0).min(self.max_reveal_width as f32) as u32,
        };
        let consumed = (index as u32).saturating_mul(width);
        if reveal <= consumed {
            return None;
        }
        let visible = (reveal - consumed).min(width);
        let x = match side {
            SwipeSide::Leading => rect.x + consumed as i32,
            SwipeSide::Trailing => rect.x + rect.width as i32 - (consumed + visible) as i32,
        };
        Some(Rect::new(x, rect.y, visible, rect.height))
    }

    /// The fill an action is painted with.
    fn fill_of(&self, action: &SwipeAction) -> Color {
        action.fill.unwrap_or_else(|| {
            crate::style::semantic_color(crate::style::SemanticColor::Error)
                .unwrap_or(Color::rgba(255, 59, 48, 255))
        })
    }

    /// Paints one side's revealed actions.
    fn draw_side(&self, context: &mut RenderContext, side: SwipeSide) {
        let actions = self.actions_for(side);
        if actions.is_empty() {
            return;
        }
        let font = role_font(self.base.style(), 14.0);
        for (index, action) in actions.iter().enumerate() {
            let Some(rect) = self.action_rect(side, index) else {
                continue;
            };
            context.fill_rect(rect, self.fill_of(action));
            if let Some(icon) = action.icon.as_deref() {
                let side_px = self.icon_side.min(rect.width).min(rect.height).max(1);
                let icon_rect = Rect::new(
                    rect.x + (rect.width.saturating_sub(side_px) / 2) as i32,
                    rect.y + (rect.height.saturating_sub(side_px) / 2) as i32,
                    side_px,
                    side_px,
                );
                if !crate::widget::draw_icon_named(
                    context,
                    icon_rect,
                    self.fill_of(action).contrast_color(),
                    icon,
                ) {
                    context.draw_text(
                        Point::new(icon_rect.x, icon_rect.y),
                        icon,
                        &font,
                        self.fill_of(action).contrast_color(),
                        HorizontalAlignment::Left,
                    );
                }
            }
            let line = context.text_line(rect, &font);
            context.draw_text_fitted_cow(
                Rect::new(rect.x + 4, line.y, rect.width.saturating_sub(8), line.height.max(1)),
                &action.text,
                &font,
                self.fill_of(action).contrast_color(),
                HorizontalAlignment::Center,
            );
        }
    }
}

impl EventHandler for SwipeActions {
    fn handle_event(&mut self, event: &Event) {
        self.base.handle_event(event);
        if !self.base.is_enabled() {
            return;
        }
        if let Event::MousePress { pos, button: 1, .. } = event {
            if let Some((side, index)) = self.action_at(self.offset, *pos) {
                self.action_triggered.emit((side, index));
            }
        }
    }
}

impl Draw for SwipeActions {
    fn draw(&mut self, context: &mut RenderContext) {
        let rect = self.geometry();
        if rect.width == 0 || rect.height == 0 {
            return;
        }
        // The background is chrome: it only occupies the revealed band, so an unswiped row is
        // left untouched (the row's own content paints over the same rectangle).
        if self.offset > 0.0 {
            self.draw_side(context, SwipeSide::Leading);
        } else if self.offset < 0.0 {
            self.draw_side(context, SwipeSide::Trailing);
        }
    }
}

impl Widget for SwipeActions {
    fn base(&self) -> &BaseWidget {
        &self.base
    }
    fn set_state_theme_hook(&mut self) {
        crate::style::reapply_active_theme_state(self);
    }
    fn base_mut(&mut self) -> &mut BaseWidget {
        &mut self.base
    }
    fn size_hint(&self) -> Size {
        Size::new(self.max_reveal_width, 48)
    }
    impl_draw_bridge!();
    impl_widget_property_hooks!();
    fn event_signal_dyn(&self, name: &str) -> Option<crate::signal::EventSignalRef> {
        use crate::signal::EventSignalRef;
        match name {
            "action_triggered" => {
                Some(EventSignalRef::mapped("action_triggered", &self.action_triggered, |value| {
                    CapabilityValue::Tuple(Vec::from([
                        CapabilityValue::String(value.0.as_str().to_string()),
                        CapabilityValue::UInt(value.1 as u64),
                    ]))
                }))
            }
            _ => None,
        }
    }
}

/// Encodes one action as `[text, icon-or-null, fill-or-null]`.
fn action_value(action: &SwipeAction) -> CapabilityValue {
    CapabilityValue::Tuple(Vec::from([
        CapabilityValue::String(action.text.clone()),
        match action.icon.as_deref() {
            Some(icon) => CapabilityValue::String(icon.to_string()),
            None => CapabilityValue::Null,
        },
        match action.fill {
            Some(fill) => CapabilityValue::Color(fill),
            None => CapabilityValue::Null,
        },
    ]))
}

/// Decodes an action-list property.
fn expect_actions(value: CapabilityValue) -> Result<Vec<SwipeAction>, CapabilityAccessError> {
    let CapabilityValue::Tuple(rows) = value else {
        return Err(CapabilityAccessError::TypeMismatch);
    };
    let mut actions = Vec::with_capacity(rows.len());
    for row in rows {
        let CapabilityValue::Tuple(parts) = row else {
            return Err(CapabilityAccessError::TypeMismatch);
        };
        if parts.len() != 3 {
            return Err(CapabilityAccessError::TypeMismatch);
        }
        let mut parts = parts.into_iter();
        let text = match parts.next().ok_or(CapabilityAccessError::TypeMismatch)? {
            CapabilityValue::String(text) => text,
            _ => return Err(CapabilityAccessError::TypeMismatch),
        };
        let icon = match parts.next().ok_or(CapabilityAccessError::TypeMismatch)? {
            CapabilityValue::String(icon) => Some(icon),
            CapabilityValue::Null => None,
            _ => return Err(CapabilityAccessError::TypeMismatch),
        };
        let fill = match parts.next().ok_or(CapabilityAccessError::TypeMismatch)? {
            CapabilityValue::Null => None,
            other => Some(expect_color(other)?),
        };
        actions.push(SwipeAction { text, icon, fill });
    }
    Ok(actions)
}

impl WidgetProperties for SwipeActions {
    fn get(&self, name: &str) -> Result<CapabilityValue, CapabilityAccessError> {
        match name {
            "leading_actions" => {
                Ok(CapabilityValue::Tuple(self.leading_actions.iter().map(action_value).collect()))
            }
            "trailing_actions" => {
                Ok(CapabilityValue::Tuple(self.trailing_actions.iter().map(action_value).collect()))
            }
            "max_reveal_width" => Ok(CapabilityValue::UInt(self.max_reveal_width as u64)),
            "icon_side" => Ok(CapabilityValue::UInt(self.icon_side as u64)),
            "offset" => Ok(CapabilityValue::Float(f64::from(self.offset))),
            _ => base_property_get(self, name),
        }
    }

    fn set(&mut self, name: &str, value: CapabilityValue) -> Result<(), CapabilityAccessError> {
        match name {
            "max_reveal_width" => {
                self.set_max_reveal_width(expect_u32(value)?);
                Ok(())
            }
            "icon_side" => {
                self.set_icon_side(expect_u32(value)?);
                Ok(())
            }
            "leading_actions" => {
                self.set_leading_actions(expect_actions(value)?);
                Ok(())
            }
            "trailing_actions" => {
                self.set_trailing_actions(expect_actions(value)?);
                Ok(())
            }
            "offset" => {
                let CapabilityValue::Float(offset) = value else {
                    return Err(CapabilityAccessError::TypeMismatch);
                };
                self.set_offset(offset as f32);
                Ok(())
            }
            _ => base_property_set(self, name, value),
        }
    }

    fn property_names(&self) -> &'static [&'static str] {
        property_names_of![
            "leading_actions",
            "trailing_actions",
            "max_reveal_width",
            "icon_side",
            "offset",
            BASE_PROPERTY_NAMES
        ]
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::{Arc, Mutex};

    fn actions() -> SwipeActions {
        let mut actions = SwipeActions::new();
        actions.set_geometry(Rect::new(0, 0, 300, 60));
        actions.set_max_reveal_width(200);
        actions.set_trailing_actions(vec![SwipeAction::new("Delete"), SwipeAction::new("Pin")]);
        actions.set_leading_actions(vec![SwipeAction::new("Archive")]);
        actions
    }

    #[test]
    fn the_offset_reveals_actions_in_order() {
        let mut actions = actions();
        actions.set_offset(-50.0);
        assert!(actions.action_rect(SwipeSide::Trailing, 0).is_some());
        assert!(actions.action_rect(SwipeSide::Trailing, 1).is_none());
        actions.set_offset(-200.0);
        assert!(actions.action_rect(SwipeSide::Trailing, 1).is_some());
        assert!(actions.action_rect(SwipeSide::Leading, 0).is_none());
        actions.set_offset(20.0);
        assert!(actions.action_rect(SwipeSide::Leading, 0).is_some());
    }

    #[test]
    fn a_press_reports_the_action_under_it() {
        let mut actions = actions();
        actions.set_offset(-200.0);
        let rect = actions.action_rect(SwipeSide::Trailing, 1).expect("second action");
        let pos = Point::new(rect.x + rect.width as i32 / 2, rect.y + 4);
        assert_eq!(actions.action_at(-200.0, pos), Some((SwipeSide::Trailing, 1)));
        let fired = Arc::new(Mutex::new(Vec::new()));
        let sink = Arc::clone(&fired);
        actions.action_triggered.connect(move |value| sink.lock().unwrap().push(*value));
        actions.handle_event(&Event::MousePress { pos, button: 1, modifiers: 0 });
        assert_eq!(*fired.lock().unwrap(), vec![(SwipeSide::Trailing, 1)]);
    }
}