rust_widgets 2.3.1

Pure Rust cross-platform native GUI library with hardware-adaptive rendering, 60+ 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
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
// SPDX-FileCopyrightText: Copyright (c) 2026 Mike Li/Mikewolfli/Wei Li(mikewolfli@163.com)
// SPDX-License-Identifier: MIT

//! NotificationCenter widget.

use crate::core::{Color, Font, HorizontalAlignment, Point, Rect};
use crate::event::{Event, EventHandler};
use crate::render::RenderContext;
use crate::signal::Signal1;
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::{BaseWidget, Draw, Widget, WidgetKind};
use crate::{impl_widget_property_hooks, property_names_of};

/// Notification severity level.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NotificationLevel {
    /// Neutral informational notification.
    Info,
    /// Non-fatal problem the user should be aware of.
    Warning,
    /// Failure that needs attention.
    Error,
}

/// One notification entry.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NotificationItem {
    /// Stable notification id.
    pub id: String,
    /// Notification title.
    pub title: String,
    /// Notification body.
    pub message: String,
    /// Severity level.
    pub level: NotificationLevel,
    /// Whether this item has been read.
    pub read: bool,
}

impl NotificationItem {
    /// Creates a notification item.
    pub fn new(
        id: impl Into<String>,
        title: impl Into<String>,
        message: impl Into<String>,
        level: NotificationLevel,
    ) -> Self {
        Self { id: id.into(), title: title.into(), message: message.into(), level, read: false }
    }
}

/// Notification list center with unread management.
pub struct NotificationCenter {
    base: BaseWidget,
    items: Vec<NotificationItem>,
    selected_index: Option<usize>,
    row_height: u32,
    /// Emitted when selected notification changes. Payload is id.
    pub notification_selected: Signal1<String>,
    /// Emitted when notification is activated. Payload is id.
    pub notification_activated: Signal1<String>,
    /// Emitted when unread count changes.
    pub unread_count_changed: Signal1<usize>,
}

impl NotificationCenter {
    /// Creates empty notification center.
    pub fn new(geometry: Rect) -> Self {
        Self {
            base: BaseWidget::new(WidgetKind::ListView, geometry, "NotificationCenter"),
            items: Vec::new(),
            selected_index: None,
            row_height: 36,
            notification_selected: Signal1::new(),
            notification_activated: Signal1::new(),
            unread_count_changed: Signal1::new(),
        }
    }

    /// Returns all notifications.
    pub fn items(&self) -> &[NotificationItem] {
        &self.items
    }

    /// Adds a notification.
    pub fn push(&mut self, item: NotificationItem) {
        self.items.push(item);
        if self.selected_index.is_none() {
            self.selected_index = Some(0);
        }
        self.unread_count_changed.emit(self.unread_count());
        self.base.request_layout();
        self.base.request_redraw();
    }

    /// Clears all notifications.
    pub fn clear(&mut self) {
        self.items.clear();
        self.selected_index = None;
        self.unread_count_changed.emit(0);
        self.base.request_layout();
        self.base.request_redraw();
    }

    /// Returns unread count.
    pub fn unread_count(&self) -> usize {
        self.items.iter().filter(|item| !item.read).count()
    }

    /// Marks a notification read/unread by id.
    pub fn set_read(&mut self, id: &str, read: bool) -> bool {
        let before = self.unread_count();
        for item in &mut self.items {
            if item.id == id {
                item.read = read;
                let after = self.unread_count();
                if before != after {
                    self.unread_count_changed.emit(after);
                }
                self.base.request_redraw();
                return true;
            }
        }
        false
    }

    /// Marks all notifications as read.
    pub fn mark_all_read(&mut self) {
        let mut changed = false;
        for item in &mut self.items {
            if !item.read {
                item.read = true;
                changed = true;
            }
        }
        if changed {
            self.unread_count_changed.emit(0);
            self.base.request_redraw();
        }
    }

    /// Returns selected notification id.
    pub fn selected_id(&self) -> Option<&str> {
        let index = self.selected_index?;
        self.items.get(index).map(|item| item.id.as_str())
    }

    /// Selects notification index.
    pub fn select_index(&mut self, index: usize) -> bool {
        if index >= self.items.len() {
            return false;
        }
        if self.selected_index == Some(index) {
            return true;
        }
        self.selected_index = Some(index);
        if let Some(item) = self.items.get(index) {
            self.notification_selected.emit(item.id.clone());
        }
        self.base.request_redraw();
        true
    }

    /// Activates current selected notification.
    pub fn activate_selected(&mut self) -> bool {
        let Some(index) = self.selected_index else {
            return false;
        };
        if index >= self.items.len() {
            return false;
        }

        let id = self.items[index].id.clone();
        let changed = !self.items[index].read;
        self.items[index].read = true;
        self.notification_activated.emit(id);

        if changed {
            self.unread_count_changed.emit(self.unread_count());
        }
        self.base.request_redraw();
        true
    }

    fn row_at(&self, pos: Point) -> Option<usize> {
        let rect = self.geometry();
        if pos.x < rect.x
            || pos.x >= rect.x + rect.width as i32
            || pos.y < rect.y
            || pos.y >= rect.y + rect.height as i32
        {
            return None;
        }
        let index = ((pos.y - rect.y) / self.row_height as i32) as usize;
        (index < self.items.len()).then_some(index)
    }

    /// Returns the height of one notification row, in logical pixels.
    pub fn row_height(&self) -> u32 {
        self.row_height
    }

    /// Sets the height of one notification row, in logical pixels.
    ///
    /// Clamped to at least one pixel so a row can never collapse to zero height
    /// and become unclickable.
    pub fn set_row_height(&mut self, row_height: u32) {
        self.row_height = row_height.max(1);
        self.base.request_layout();
        self.base.request_redraw();
    }
}

impl Widget for NotificationCenter {
    fn base(&self) -> &BaseWidget {
        &self.base
    }

    fn base_mut(&mut self) -> &mut BaseWidget {
        &mut self.base
    }

    fn size_hint(&self) -> crate::core::Size {
        crate::core::Size::new(300, 400)
    }
    impl_draw_bridge!();
    impl_widget_property_hooks!();
}

/// `NotificationCenter`'s property contract.
///
/// `unread_count` and `item_count` are derived from the live list, and
/// `selected_index` is read-only because selection emits `notification_selected`
/// — the counts would go stale if the list were mutated behind the signals.
impl WidgetProperties for NotificationCenter {
    fn get(&self, name: &str) -> Result<CapabilityValue, CapabilityAccessError> {
        match name {
            "item_count" => Ok(CapabilityValue::UInt(self.items().len() as u64)),
            "unread_count" => Ok(CapabilityValue::UInt(self.unread_count() as u64)),
            "selected_index" => {
                let index = self
                    .items()
                    .iter()
                    .position(|item| Some(item.id.as_str()) == self.selected_id());
                match index {
                    Some(index) => Ok(CapabilityValue::UInt(index as u64)),
                    None => Ok(CapabilityValue::Null),
                }
            }
            "row_height" => Ok(CapabilityValue::UInt(self.row_height() as u64)),
            _ => base_property_get(self, name),
        }
    }

    fn set(&mut self, name: &str, value: CapabilityValue) -> Result<(), CapabilityAccessError> {
        match name {
            "row_height" => match value {
                CapabilityValue::UInt(height) => {
                    let height =
                        u32::try_from(height).map_err(|_| CapabilityAccessError::TypeMismatch)?;
                    self.set_row_height(height);
                    Ok(())
                }
                _ => Err(CapabilityAccessError::TypeMismatch),
            },
            "item_count" | "unread_count" | "selected_index" => {
                Err(CapabilityAccessError::ReadOnlyProperty)
            }
            _ => base_property_set(self, name, value),
        }
    }

    fn property_names(&self) -> &'static [&'static str] {
        property_names_of![
            "item_count",
            "unread_count",
            "selected_index",
            "row_height",
            BASE_PROPERTY_NAMES
        ]
    }
}

impl EventHandler for NotificationCenter {
    fn handle_event(&mut self, event: &Event) {
        self.base.handle_event(event);
        if !self.base.is_enabled() {
            return;
        }

        match event {
            Event::MousePress { pos, button: 1 } => {
                if let Some(index) = self.row_at(*pos) {
                    let _ = self.select_index(index);
                }
            }
            Event::MouseDoubleClick { pos, button: 1 } => {
                if let Some(index) = self.row_at(*pos) {
                    let _ = self.select_index(index);
                    let _ = self.activate_selected();
                }
            }
            Event::KeyPress { key, modifiers: _ } => match *key {
                38 => {
                    if let Some(index) = self.selected_index {
                        if index > 0 {
                            let _ = self.select_index(index - 1);
                        }
                    } else if !self.items.is_empty() {
                        let _ = self.select_index(0);
                    }
                }
                40 => {
                    if let Some(index) = self.selected_index {
                        if index + 1 < self.items.len() {
                            let _ = self.select_index(index + 1);
                        }
                    } else if !self.items.is_empty() {
                        let _ = self.select_index(0);
                    }
                }
                13 => {
                    let _ = self.activate_selected();
                }
                // Unknown key; ignore
                _ => {}
            },
            // Other events are not relevant for this widget
            _ => {}
        }
    }
}

impl Draw for NotificationCenter {
    fn draw(&mut self, context: &mut RenderContext) {
        let rect = self.geometry();
        context.fill_rect(rect, Color::rgb(250, 251, 253));
        context.draw_rect(rect, Color::rgb(194, 201, 214));

        for (index, item) in self.items.iter().enumerate() {
            let y = rect.y + index as i32 * self.row_height as i32;
            if y >= rect.y + rect.height as i32 {
                break;
            }
            let row_rect = Rect::new(rect.x, y, rect.width, self.row_height);

            let bg = if self.selected_index == Some(index) {
                Color::rgb(220, 232, 249)
            } else if !item.read {
                Color::rgb(240, 246, 255)
            } else {
                Color::rgb(250, 251, 253)
            };
            context.fill_rect(row_rect, bg);

            let badge_color = match item.level {
                NotificationLevel::Info => Color::rgb(74, 122, 199),
                NotificationLevel::Warning => Color::rgb(222, 153, 42),
                NotificationLevel::Error => Color::rgb(208, 82, 72),
            };

            context.fill_rect(Rect::new(rect.x + 8, y + 14, 8, 8), badge_color);
            context.draw_text(
                Point::new(rect.x + 22, y + 14),
                &item.title,
                &Font::default(),
                Color::rgb(40, 51, 68),
                HorizontalAlignment::Left,
            );
            context.draw_text(
                Point::new(rect.x + 22, y + 28),
                &item.message,
                &Font::default(),
                Color::rgb(91, 103, 121),
                HorizontalAlignment::Left,
            );

            context.draw_line(
                Point::new(rect.x, y + self.row_height as i32),
                Point::new(rect.x + rect.width as i32, y + self.row_height as i32),
                Color::rgb(229, 233, 240),
            );
        }
    }
}

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

    fn sample_center() -> NotificationCenter {
        let mut center = NotificationCenter::new(Rect::new(0, 0, 420, 160));
        center.push(NotificationItem::new(
            "n1",
            "Build finished",
            "All checks passed",
            NotificationLevel::Info,
        ));
        center.push(NotificationItem::new(
            "n2",
            "Deploy warning",
            "One region has latency spike",
            NotificationLevel::Warning,
        ));
        center.push(NotificationItem::new(
            "n3",
            "Runtime error",
            "Worker crashed",
            NotificationLevel::Error,
        ));
        center
    }

    #[test]
    fn unread_count_and_mark_read_work() {
        let mut center = sample_center();
        assert_eq!(center.unread_count(), 3);

        assert!(center.set_read("n2", true));
        assert_eq!(center.unread_count(), 2);

        center.mark_all_read();
        assert_eq!(center.unread_count(), 0);
    }

    #[test]
    fn activate_selected_marks_read_and_emits() {
        let mut center = sample_center();
        let activated = Arc::new(Mutex::new(Vec::<String>::new()));
        let sink = activated.clone();
        center.notification_activated.connect(move |id| {
            if let Ok(mut guard) = sink.lock() {
                guard.push(id.as_ref().clone());
            }
        });

        assert!(center.select_index(1));
        assert!(center.activate_selected());
        assert_eq!(center.selected_id(), Some("n2"));
        assert_eq!(center.unread_count(), 2);

        let got = activated.lock().ok().map(|guard| guard.clone()).unwrap_or_default();
        assert_eq!(got, vec!["n2".to_string()]);
    }

    #[test]
    fn keyboard_navigation_changes_selection() {
        let mut center = sample_center();

        center.handle_event(&Event::key_press(40, 0));
        assert_eq!(center.selected_id(), Some("n2"));

        center.handle_event(&Event::key_press(40, 0));
        assert_eq!(center.selected_id(), Some("n3"));

        center.handle_event(&Event::key_press(38, 0));
        assert_eq!(center.selected_id(), Some("n2"));
    }

    #[test]
    fn new_creates_default_state() {
        let center = NotificationCenter::new(Rect::new(0, 0, 800, 600));
        assert!(center.items().is_empty());
        assert_eq!(center.selected_id(), None);
        assert_eq!(center.unread_count(), 0);
    }

    #[test]
    fn items_returns_items() {
        let center = sample_center();
        assert_eq!(center.items().len(), 3);
        assert_eq!(center.items()[0].id, "n1");
        assert_eq!(center.items()[1].id, "n2");
        assert_eq!(center.items()[2].id, "n3");
    }

    #[test]
    fn push_sets_selection_to_first() {
        let mut center = NotificationCenter::new(Rect::new(0, 0, 800, 600));
        center.push(NotificationItem::new("n1", "Title", "Body", NotificationLevel::Info));
        assert_eq!(center.selected_id(), Some("n1"));
    }

    #[test]
    fn clear_removes_all_and_resets_state() {
        let mut center = sample_center();
        center.clear();
        assert!(center.items().is_empty());
        assert_eq!(center.selected_id(), None);
        assert_eq!(center.unread_count(), 0);
    }

    #[test]
    fn select_index_invalid_returns_false() {
        let mut center = NotificationCenter::new(Rect::new(0, 0, 800, 600));
        assert!(!center.select_index(0));

        center.push(NotificationItem::new("n1", "Title", "Body", NotificationLevel::Info));
        assert!(!center.select_index(5));
        assert_eq!(center.selected_id(), Some("n1"));
    }

    #[test]
    fn unread_count_changed_signal_on_push() {
        let mut center = NotificationCenter::new(Rect::new(0, 0, 800, 600));
        let counts = Arc::new(Mutex::new(Vec::<usize>::new()));
        let sink = counts.clone();
        center.unread_count_changed.connect(move |count| {
            if let Ok(mut guard) = sink.lock() {
                guard.push(*count);
            }
        });

        center.push(NotificationItem::new("n1", "Title", "Body", NotificationLevel::Info));
        let got = counts.lock().ok().map(|g| g.clone()).unwrap_or_default();
        assert_eq!(got, vec![1]);
    }

    #[test]
    fn keyboard_navigation_on_empty_does_nothing() {
        let mut center = NotificationCenter::new(Rect::new(0, 0, 800, 600));

        // Should not panic
        center.handle_event(&Event::key_press(40, 0));
        assert_eq!(center.selected_id(), None);

        center.handle_event(&Event::key_press(38, 0));
        assert_eq!(center.selected_id(), None);
    }

    #[test]
    fn activate_selected_marks_read_and_updates_unread_count() {
        let mut center = NotificationCenter::new(Rect::new(0, 0, 800, 600));
        center.push(NotificationItem::new("n1", "Title", "Body", NotificationLevel::Info));
        assert_eq!(center.unread_count(), 1);

        // activate_selected on index 0 (first/only item)
        assert!(center.activate_selected());
        assert_eq!(center.unread_count(), 0);
    }

    #[test]
    fn set_read_nonexistent_returns_false() {
        let mut center = sample_center();
        assert!(!center.set_read("nonexistent", true));
        assert_eq!(center.unread_count(), 3);
    }

    #[test]
    fn select_index_duplicate_guard_returns_true() {
        let mut center = sample_center();
        // First select a different index
        assert!(center.select_index(1));

        let emitted = Arc::new(Mutex::new(Vec::<String>::new()));
        let sink = emitted.clone();
        center.notification_selected.connect(move |id| {
            if let Ok(mut guard) = sink.lock() {
                guard.push(id.as_ref().clone());
            }
        });

        // Select index 0 - emits "n1"
        assert!(center.select_index(0));
        // Second call with same index returns true but doesn't emit
        assert!(center.select_index(0));
        let got = emitted.lock().ok().map(|g| g.clone()).unwrap_or_default();
        assert_eq!(got.len(), 1);
    }

    #[test]
    fn enter_key_activates_selected() {
        let mut center = sample_center();
        let activated = Arc::new(Mutex::new(Vec::<String>::new()));
        let sink = activated.clone();
        center.notification_activated.connect(move |id| {
            if let Ok(mut guard) = sink.lock() {
                guard.push(id.as_ref().clone());
            }
        });

        // Navigate down to n2, then activate
        center.handle_event(&Event::key_press(40, 0));
        center.handle_event(&Event::key_press(13, 0));

        let got = activated.lock().ok().map(|guard| guard.clone()).unwrap_or_default();
        assert_eq!(got, vec!["n2".to_string()]);
    }
}