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
// SPDX-FileCopyrightText: Copyright (c) 2026 Mike Li/Mikewolfli/Wei Li(mikewolfli@163.com)
// SPDX-License-Identifier: MIT

//! `ValidationBubble` — a validation message anchored beside the field it accuses (COMP-B02).
//!
//! # Why not a toast or a tooltip
//!
//! A toast lives in a corner; a tooltip follows the pointer. Neither says "**this** field is
//! wrong", which is the one thing a validation message has to say. The input family's feedback
//! had no such carrier: `line_edit` has an error row, `masked_edit` reports a rejected cell index
//! with no text, and `editable_combo_box` reports that editing finished with nothing to show.
//! This companion is the field-anchored form, and it is what
//! [`Validator::message_for`](crate::widget::input_widgets::input_validator::Validator::message_for)
//! gives the host something specific to put in it.
//!
//! Like the other companions it is mounted beside the control it serves and is not in the
//! capability table.

use crate::compat::{String, ToString};
use crate::core::{Color, HorizontalAlignment, Point, Rect, Size};
use crate::event::{Event, EventHandler};
use crate::impl_widget_property_hooks;
use crate::property_names_of;
use crate::render::RenderContext;
use crate::signal::{EventSignalRef, GenericSignal};
use crate::widget::capability::coercion::{expect_string, 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::input_widgets::popup_support::{expect_rect, rect_value};
use crate::widget::metrics::effective_font;
use crate::widget::{BaseWidget, Draw, Widget, WidgetKind};

/// How far the bubble floats above its anchor, leaving room for the pointer triangle.
const BUBBLE_GAP: i32 = 8;
/// The pointer triangle's half-width.
const POINTER_HALF: i32 = 5;
/// Text inset inside the bubble.
const BUBBLE_INSET: i32 = 8;
/// Vertical padding inside the bubble.
const BUBBLE_PAD_V: i32 = 5;

/// How serious a validation message is.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ValidationSeverity {
    /// A neutral note about the value.
    Info,
    /// The value is worth a second look but is accepted.
    Warning,
    /// The value is refused.
    #[default]
    Error,
}

impl ValidationSeverity {
    /// The published token for this severity.
    pub fn as_token(self) -> &'static str {
        match self {
            Self::Info => "info",
            Self::Warning => "warning",
            Self::Error => "error",
        }
    }

    /// The severity a published token names, or `None` for an unknown token.
    pub fn from_token(token: &str) -> Option<Self> {
        match token {
            "info" => Some(Self::Info),
            "warning" => Some(Self::Warning),
            "error" => Some(Self::Error),
            _ => None,
        }
    }

    /// The semantic colour of this severity, following the toast host's rule.
    fn ink(self) -> Color {
        use crate::style::{semantic_color, SemanticColor};
        match self {
            Self::Info => semantic_color(SemanticColor::Info).unwrap_or(Color::PRIMARY),
            Self::Warning => semantic_color(SemanticColor::Warning).unwrap_or(Color::WARNING),
            Self::Error => semantic_color(SemanticColor::Error).unwrap_or(Color::ERROR),
        }
    }
}

/// A message bubble anchored to a field.
pub struct ValidationBubble {
    base: BaseWidget,
    message: String,
    severity: ValidationSeverity,
    anchor: Option<Rect>,
    /// `None` keeps the bubble up until it is replaced or hidden.
    show_duration_ms: Option<u32>,
    /// Milliseconds left on the current message, or `None` for a persistent one.
    remaining_ms: Option<u32>,
    visible: bool,
    /// Emitted when the bubble goes away: its timer expired, or the host hid it.
    pub bubble_dismissed: GenericSignal,
}

impl ValidationBubble {
    /// Creates a hidden bubble.
    pub fn new(geometry: Rect) -> Self {
        Self {
            base: BaseWidget::new(WidgetKind::Panel, geometry, "ValidationBubble"),
            message: String::new(),
            severity: ValidationSeverity::Error,
            anchor: None,
            show_duration_ms: None,
            remaining_ms: None,
            visible: false,
            bubble_dismissed: GenericSignal::new(),
        }
    }

    /// Shows `message` at `severity`, restarting any countdown.
    pub fn show_message(&mut self, message: &str, severity: ValidationSeverity) {
        self.message.clear();
        self.message.push_str(message);
        self.severity = severity;
        self.remaining_ms = self.show_duration_ms;
        self.visible = !self.message.is_empty();
        self.base.request_redraw();
    }

    /// The message being shown.
    pub fn message(&self) -> &str {
        &self.message
    }

    /// The severity being shown.
    pub fn severity(&self) -> ValidationSeverity {
        self.severity
    }

    /// Anchors the bubble to the field it accuses.
    pub fn set_anchor(&mut self, anchor: Rect) {
        if self.anchor == Some(anchor) {
            return;
        }
        self.anchor = Some(anchor);
        self.base.request_redraw();
    }

    /// The field band the bubble points at.
    pub fn anchor(&self) -> Option<Rect> {
        self.anchor
    }

    /// Shows the current message.
    pub fn show(&mut self) {
        if self.visible || self.message.is_empty() {
            return;
        }
        self.visible = true;
        self.remaining_ms = self.show_duration_ms;
        self.base.request_redraw();
    }

    /// Hides the bubble and reports the dismissal when it was showing.
    pub fn hide(&mut self) {
        if !self.visible {
            return;
        }
        self.visible = false;
        self.remaining_ms = None;
        self.base.request_redraw();
        self.bubble_dismissed.emit();
    }

    /// Whether the bubble is showing.
    pub fn is_visible(&self) -> bool {
        self.visible
    }

    /// Sets (or clears, with `None`) the auto-hide delay.
    pub fn set_show_duration(&mut self, duration_ms: Option<u32>) {
        if self.show_duration_ms == duration_ms {
            return;
        }
        self.show_duration_ms = duration_ms;
        // A message already on screen picks the new budget up immediately, so the host does not
        // have to re-show it to change how long it lasts.
        if self.visible {
            self.remaining_ms = duration_ms;
        }
    }

    /// The auto-hide delay, or `None` for a persistent bubble.
    pub fn show_duration(&self) -> Option<u32> {
        self.show_duration_ms
    }

    /// Advances the countdown, returning whether another frame is due.
    ///
    /// A bubble with no delay never owes a frame: a persistent message does not animate.
    pub fn tick(&mut self, delta_ms: u32) -> bool {
        if !self.visible {
            return false;
        }
        let Some(remaining) = self.remaining_ms else {
            return false;
        };
        if delta_ms >= remaining {
            self.remaining_ms = None;
            self.hide();
            return false;
        }
        self.remaining_ms = Some(remaining - delta_ms);
        true
    }

    /// The bubble's own box: wide enough for `width`, tall enough for one line plus padding.
    fn bubble_rect(&self) -> Rect {
        let font = effective_font(self.base.style());
        let line_height = font.effective_line_height().max(1.0) as u32;
        let geometry = self.geometry();
        let width = self.anchor.map(|anchor| anchor.width).unwrap_or(geometry.width);
        let height = line_height.saturating_add(BUBBLE_PAD_V as u32 * 2);
        let (x, y) = match self.anchor {
            Some(anchor) => {
                let above = anchor.y - height as i32 - BUBBLE_GAP;
                if above >= 0 {
                    (anchor.x, above)
                } else {
                    (anchor.x, anchor.y + anchor.height as i32 + BUBBLE_GAP)
                }
            }
            None => (geometry.x, geometry.y),
        };
        Rect::new(x, y, width, height)
    }
}

impl EventHandler for ValidationBubble {
    fn handle_event(&mut self, event: &Event) {
        self.base.handle_event(event);
        if !self.base.is_enabled() || !self.visible {
            return;
        }
        // Any press on the bubble acknowledges it: a message the user has read should not sit over
        // the field they are trying to correct.
        if let Event::MousePress { pos, button: 1, .. } = event {
            if self.bubble_rect().contains(*pos) {
                self.hide();
            }
        }
    }
}

impl Draw for ValidationBubble {
    fn draw(&mut self, context: &mut RenderContext) {
        if !self.visible || self.message.is_empty() {
            return;
        }
        let bubble = self.bubble_rect();
        if bubble.width == 0 || bubble.height == 0 {
            return;
        }
        let style = self.style();
        let font = effective_font(style);
        let tone = self.severity.ink();
        // The bubble is a raised surface tinted by the severity, so the message reads on the same
        // ink the rest of the theme uses.
        let surface = style.background_color.unwrap_or_else(|| tone.blend(&Color::WHITE, 0.88));
        let ink = style.text_color.unwrap_or_else(|| surface.contrast_color());

        context.fill_rounded_rect(bubble, 4, surface);
        context.draw_rounded_rect_stroke(bubble, 4, tone, 1);

        let line = context.text_line(bubble, font);
        context.draw_text_fitted_cow(
            Rect::new(
                bubble.x + BUBBLE_INSET,
                line.y,
                bubble.width.saturating_sub(BUBBLE_INSET as u32 * 2),
                line.height.max(1),
            ),
            &self.message,
            font,
            ink,
            HorizontalAlignment::Left,
        );

        // The pointer: two strokes from the bubble's edge toward the anchor, drawn only when the
        // bubble floats above it (below the field the triangle would point away from the ink).
        if let Some(anchor) = self.anchor {
            if bubble.y + bubble.height as i32 <= anchor.y {
                let cx = (anchor.x + anchor.width as i32 / 2)
                    .clamp(bubble.x + POINTER_HALF, bubble.x + bubble.width as i32 - POINTER_HALF);
                let tip_y = bubble.y + bubble.height as i32;
                context.draw_line_stroke(
                    Point::new(cx - POINTER_HALF, tip_y - 1),
                    Point::new(cx, tip_y + POINTER_HALF),
                    tone,
                    1,
                );
                context.draw_line_stroke(
                    Point::new(cx, tip_y + POINTER_HALF),
                    Point::new(cx + POINTER_HALF, tip_y - 1),
                    tone,
                    1,
                );
            }
        }
    }
}

impl Widget for ValidationBubble {
    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 {
        let bubble = self.bubble_rect();
        Size::new(bubble.width, bubble.height)
    }
    impl_draw_bridge!();
    impl_widget_property_hooks!();
    fn tick(&mut self, delta_ms: u32) -> bool {
        ValidationBubble::tick(self, delta_ms)
    }
    fn is_animating(&self) -> bool {
        self.visible && self.remaining_ms.is_some()
    }
    fn event_signal_dyn(&self, name: &str) -> Option<EventSignalRef> {
        match name {
            "bubble_dismissed" => {
                Some(EventSignalRef::unit("bubble_dismissed", &self.bubble_dismissed))
            }
            _ => None,
        }
    }
}

impl WidgetProperties for ValidationBubble {
    fn get(&self, name: &str) -> Result<CapabilityValue, CapabilityAccessError> {
        match name {
            "message" => Ok(CapabilityValue::String(self.message.clone())),
            "severity" => Ok(CapabilityValue::String(self.severity.as_token().to_string())),
            "anchor" => match self.anchor {
                Some(anchor) => Ok(rect_value(anchor)),
                None => Ok(CapabilityValue::Null),
            },
            "show_duration_ms" => match self.show_duration_ms {
                Some(duration) => Ok(CapabilityValue::UInt(u64::from(duration))),
                None => Ok(CapabilityValue::Null),
            },
            "visible" => Ok(CapabilityValue::Bool(self.visible)),
            _ => base_property_get(self, name),
        }
    }

    fn set(&mut self, name: &str, value: CapabilityValue) -> Result<(), CapabilityAccessError> {
        match name {
            "message" => {
                let message = expect_string(value)?;
                let severity = self.severity;
                self.show_message(&message, severity);
                Ok(())
            }
            "severity" => {
                let token = expect_string(value)?;
                let severity = ValidationSeverity::from_token(&token)
                    .ok_or(CapabilityAccessError::OutOfRange)?;
                self.severity = severity;
                self.base.request_redraw();
                Ok(())
            }
            "anchor" => match value {
                CapabilityValue::Null => {
                    self.anchor = None;
                    self.base.request_redraw();
                    Ok(())
                }
                other => {
                    self.set_anchor(expect_rect(other)?);
                    Ok(())
                }
            },
            "show_duration_ms" => match value {
                CapabilityValue::Null => {
                    self.set_show_duration(None);
                    Ok(())
                }
                other => {
                    self.set_show_duration(Some(expect_u32(other)?));
                    Ok(())
                }
            },
            "visible" => {
                if matches!(value, CapabilityValue::Bool(true)) {
                    self.show();
                } else {
                    self.hide();
                }
                Ok(())
            }
            _ => base_property_set(self, name, value),
        }
    }

    fn property_names(&self) -> &'static [&'static str] {
        property_names_of![
            "message",
            "severity",
            "anchor",
            "show_duration_ms",
            "visible",
            BASE_PROPERTY_NAMES
        ]
    }
}

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

    #[test]
    fn a_message_shows_and_reports_its_severity() {
        let mut bubble = ValidationBubble::new(Rect::new(0, 0, 160, 1));
        bubble.show_message("3 is outside 1..=2", ValidationSeverity::Error);
        assert!(bubble.is_visible());
        assert_eq!(bubble.message(), "3 is outside 1..=2");
        assert_eq!(bubble.severity(), ValidationSeverity::Error);
        bubble.hide();
        assert!(!bubble.is_visible());
    }

    #[test]
    fn a_timed_message_hides_itself() {
        let mut bubble = ValidationBubble::new(Rect::new(0, 0, 160, 1));
        bubble.set_show_duration(Some(100));
        bubble.show_message("try again", ValidationSeverity::Warning);
        assert!(bubble.tick(60));
        assert!(bubble.is_visible());
        assert!(!bubble.tick(60));
        assert!(!bubble.is_visible());
    }

    #[test]
    fn a_persistent_message_never_expires() {
        let mut bubble = ValidationBubble::new(Rect::new(0, 0, 160, 1));
        bubble.show_message("still wrong", ValidationSeverity::Info);
        assert!(!bubble.tick(10_000));
        assert!(bubble.is_visible());
    }

    #[test]
    fn the_severity_tokens_round_trip() {
        for severity in
            [ValidationSeverity::Info, ValidationSeverity::Warning, ValidationSeverity::Error]
        {
            assert_eq!(ValidationSeverity::from_token(severity.as_token()), Some(severity));
        }
        assert_eq!(ValidationSeverity::default(), ValidationSeverity::Error);
    }

    #[test]
    fn the_bubble_sits_above_its_anchor_when_there_is_room() {
        let mut bubble = ValidationBubble::new(Rect::new(0, 0, 160, 1));
        bubble.set_anchor(Rect::new(10, 80, 120, 24));
        let rect = bubble.bubble_rect();
        assert!(rect.y + rect.height as i32 <= 80);
    }
}