rust_widgets 0.9.6

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
//! BarcodeScanner widget — barcode and QR code scanner viewfinder.
//!
//! Renders a viewfinder area with scanning animation, corner brackets,
//! and detected barcode result overlay.

use crate::core::{Color, Font, Point, Rect};
use crate::event::{Event, EventHandler};
use crate::render::RenderContext;
use crate::signal::Signal1;
use crate::widget::{BaseWidget, Draw, Widget, WidgetKind};

/// Barcode format types supported by the scanner.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BarcodeFormat {
    /// QR Code (ISO/IEC 18004).
    QRCode,
    /// Code 128 (ISO/IEC 15417).
    Code128,
    /// Code 39 (ISO/IEC 16388).
    Code39,
    /// EAN-13 (European Article Number, 13 digits).
    EAN13,
    /// EAN-8 (European Article Number, 8 digits).
    EAN8,
    /// UPC-A (Universal Product Code, 12 digits).
    UPCA,
    /// Data Matrix (ISO/IEC 16022).
    DataMatrix,
    /// PDF417 (ISO/IEC 15438).
    PDF417,
}

impl BarcodeFormat {
    /// Returns a human-readable name for the format.
    pub fn name(&self) -> &'static str {
        match self {
            BarcodeFormat::QRCode => "QR Code",
            BarcodeFormat::Code128 => "Code 128",
            BarcodeFormat::Code39 => "Code 39",
            BarcodeFormat::EAN13 => "EAN-13",
            BarcodeFormat::EAN8 => "EAN-8",
            BarcodeFormat::UPCA => "UPC-A",
            BarcodeFormat::DataMatrix => "Data Matrix",
            BarcodeFormat::PDF417 => "PDF417",
        }
    }
}

/// Result of a barcode scan operation.
#[derive(Debug, Clone)]
pub struct BarcodeResult {
    /// Decoded data string from the barcode.
    pub data: String,
    /// Format of the scanned barcode.
    pub format: BarcodeFormat,
    /// Timestamp (ms since epoch) when the barcode was scanned.
    pub timestamp: u64,
}

/// Barcode/QR code scanner widget.
///
/// Renders a viewfinder with corner brackets and a scanning animation line.
/// Stores detected barcode results and emits a signal on detection.
pub struct BarcodeScanner {
    base: BaseWidget,
    /// Whether the scanner is actively scanning.
    is_scanning: bool,
    /// Last successfully decoded barcode result.
    last_result: Option<BarcodeResult>,
    /// Interval between scan attempts in milliseconds.
    scan_interval: u64,
    /// Whether to show the viewfinder corner brackets.
    show_viewfinder: bool,
    /// Emitted when a barcode is detected.
    pub barcode_detected: Signal1<BarcodeResult>,
}

impl BarcodeScanner {
    /// Creates a new BarcodeScanner widget with the given geometry.
    pub fn new(geometry: Rect) -> Self {
        Self {
            base: BaseWidget::new(WidgetKind::BarcodeScanner, geometry, "BarcodeScanner"),
            is_scanning: false,
            last_result: None,
            scan_interval: 100,
            show_viewfinder: true,
            barcode_detected: Signal1::new(),
        }
    }

    /// Starts the scanning process.
    pub fn start_scanning(&mut self) {
        self.is_scanning = true;
        self.base.request_redraw();
    }

    /// Stops the scanning process.
    pub fn stop_scanning(&mut self) {
        self.is_scanning = false;
        self.base.request_redraw();
    }

    /// Returns whether the scanner is currently scanning.
    pub fn is_scanning(&self) -> bool {
        self.is_scanning
    }

    /// Toggles scanning on/off.
    pub fn toggle_scanning(&mut self) {
        if self.is_scanning {
            self.stop_scanning();
        } else {
            self.start_scanning();
        }
    }

    /// Sets the scan interval in milliseconds.
    pub fn set_scan_interval(&mut self, ms: u64) {
        self.scan_interval = ms.max(10);
    }

    /// Returns the current scan interval in milliseconds.
    pub fn scan_interval(&self) -> u64 {
        self.scan_interval
    }

    /// Returns a reference to the last detected barcode result, if any.
    pub fn last_result(&self) -> Option<&BarcodeResult> {
        self.last_result.as_ref()
    }

    /// Clears the last barcode result.
    pub fn clear_result(&mut self) {
        self.last_result = None;
        self.base.request_redraw();
    }

    /// Simulates a barcode detection with the given data and format.
    /// This is the primary way to inject scan results into the widget.
    pub fn detect_barcode(&mut self, data: String, format: BarcodeFormat) {
        use std::time::{SystemTime, UNIX_EPOCH};
        let timestamp =
            SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_millis() as u64;
        let result = BarcodeResult { data, format, timestamp };
        self.last_result = Some(result.clone());
        self.barcode_detected.emit(result);
        self.base.request_redraw();
    }

    /// Shows or hides the viewfinder overlay.
    pub fn set_show_viewfinder(&mut self, show: bool) {
        self.show_viewfinder = show;
        self.base.request_redraw();
    }

    /// Returns whether the viewfinder overlay is shown.
    pub fn show_viewfinder(&self) -> bool {
        self.show_viewfinder
    }
}

impl Widget for BarcodeScanner {
    fn base(&self) -> &BaseWidget {
        &self.base
    }
    fn base_mut(&mut self) -> &mut BaseWidget {
        &mut self.base
    }
}

impl Draw for BarcodeScanner {
    fn draw(&mut self, context: &mut RenderContext) {
        let rect = self.geometry();
        let w = rect.width as i32;
        let h = rect.height as i32;

        if w <= 0 || h <= 0 {
            return;
        }

        // Create fonts
        let small_font = Font::new("sans-serif", 11.0, false, false);
        let normal_font = Font::new("sans-serif", 13.0, false, false);

        // Background
        context.fill_rect(rect, Color::rgba(20, 20, 30, 255));

        // Viewfinder area (centered, 80% of widget size)
        let vf_margin_x = (w as f32 * 0.1) as i32;
        let vf_margin_y = (h as f32 * 0.15) as i32;
        let vf_rect = Rect::new(
            rect.x + vf_margin_x,
            rect.y + vf_margin_y,
            (w - vf_margin_x * 2) as u32,
            (h - vf_margin_y * 2) as u32,
        );

        // Darken area outside viewfinder
        if vf_margin_y > 0 {
            context.fill_rect(
                Rect::new(rect.x, rect.y, w as u32, vf_margin_y as u32),
                Color::rgba(0, 0, 0, 180),
            );
        }
        let vf_bottom = rect.y + vf_margin_y + vf_rect.height as i32;
        if vf_bottom < rect.y + h {
            context.fill_rect(
                Rect::new(rect.x, vf_bottom, w as u32, (rect.y + h - vf_bottom) as u32),
                Color::rgba(0, 0, 0, 180),
            );
        }
        if vf_margin_x > 0 {
            context.fill_rect(
                Rect::new(rect.x, rect.y + vf_margin_y, vf_margin_x as u32, vf_rect.height as u32),
                Color::rgba(0, 0, 0, 180),
            );
        }
        let vf_right = rect.x + vf_margin_x + vf_rect.width as i32;
        if vf_right < rect.x + w {
            context.fill_rect(
                Rect::new(
                    vf_right,
                    rect.y + vf_margin_y,
                    (rect.x + w - vf_right) as u32,
                    vf_rect.height as u32,
                ),
                Color::rgba(0, 0, 0, 180),
            );
        }

        // Viewfinder inner area
        context.fill_rect(vf_rect, Color::rgba(30, 30, 40, 200));
        context.draw_rect_stroke(vf_rect, Color::rgba(200, 200, 200, 100), 1);

        // Corner brackets
        if self.show_viewfinder {
            let bracket_len = 20;
            let bracket_color = Color::rgba(0, 200, 100, 255);

            // Top-left corner
            context.draw_line(
                Point::new(vf_rect.x, vf_rect.y + bracket_len),
                Point::new(vf_rect.x, vf_rect.y),
                bracket_color,
            );
            context.draw_line(
                Point::new(vf_rect.x, vf_rect.y),
                Point::new(vf_rect.x + bracket_len, vf_rect.y),
                bracket_color,
            );

            // Top-right corner
            context.draw_line(
                Point::new(vf_rect.x + vf_rect.width as i32, vf_rect.y),
                Point::new(vf_rect.x + vf_rect.width as i32 - bracket_len, vf_rect.y),
                bracket_color,
            );
            context.draw_line(
                Point::new(vf_rect.x + vf_rect.width as i32, vf_rect.y),
                Point::new(vf_rect.x + vf_rect.width as i32, vf_rect.y + bracket_len),
                bracket_color,
            );

            // Bottom-left corner
            context.draw_line(
                Point::new(vf_rect.x, vf_rect.y + vf_rect.height as i32),
                Point::new(vf_rect.x, vf_rect.y + vf_rect.height as i32 - bracket_len),
                bracket_color,
            );
            context.draw_line(
                Point::new(vf_rect.x, vf_rect.y + vf_rect.height as i32),
                Point::new(vf_rect.x + bracket_len, vf_rect.y + vf_rect.height as i32),
                bracket_color,
            );

            // Bottom-right corner
            context.draw_line(
                Point::new(vf_rect.x + vf_rect.width as i32, vf_rect.y + vf_rect.height as i32),
                Point::new(
                    vf_rect.x + vf_rect.width as i32 - bracket_len,
                    vf_rect.y + vf_rect.height as i32,
                ),
                bracket_color,
            );
            context.draw_line(
                Point::new(vf_rect.x + vf_rect.width as i32, vf_rect.y + vf_rect.height as i32),
                Point::new(
                    vf_rect.x + vf_rect.width as i32,
                    vf_rect.y + vf_rect.height as i32 - bracket_len,
                ),
                bracket_color,
            );
        }

        // Scanning animation line (when active)
        if self.is_scanning {
            let scan_line_y = vf_rect.y
                + 10
                + ((std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap_or_default()
                    .as_millis() as u64
                    / 20)
                    % (vf_rect.height as u64 - 20)) as i32;
            context.draw_line_stroke(
                Point::new(vf_rect.x + 4, scan_line_y),
                Point::new(vf_rect.x + vf_rect.width as i32 - 4, scan_line_y),
                Color::rgba(0, 255, 100, 200),
                2,
            );
        }

        // Draw detected result overlay
        if let Some(ref result) = self.last_result {
            let overlay_y = rect.y + h - 50;
            let overlay_rect = Rect::new(rect.x, overlay_y, w as u32, 50);
            context.fill_rect(overlay_rect, Color::rgba(0, 0, 0, 200));

            let format_text = format!("[{}]", result.format.name());
            context.draw_text(
                Point::new(rect.x + 10, overlay_y + 14),
                &format_text,
                &small_font,
                Color::rgba(100, 255, 150, 255),
            );

            let display_data = if result.data.len() > 30 {
                format!("{}...", &result.data[..30])
            } else {
                result.data.clone()
            };
            context.draw_text(
                Point::new(rect.x + 10, overlay_y + 32),
                &display_data,
                &normal_font,
                Color::rgba(255, 255, 255, 220),
            );
        }

        // Status indicator
        let status_color = if self.is_scanning {
            Color::rgba(0, 200, 50, 255)
        } else {
            Color::rgba(200, 50, 50, 255)
        };
        let dot_rect = Rect::new(rect.x + 6, rect.y + 6, 8, 8);
        context.fill_rect(dot_rect, status_color);
    }
}

impl EventHandler for BarcodeScanner {
    fn handle_event(&mut self, event: &Event) {
        if !self.base.is_enabled() {
            return;
        }
        match event {
            Event::MousePress { pos: _, button } => {
                if *button == 1 {
                    // Left-click toggles scanning
                    self.toggle_scanning();
                }
            }
            _ => {
                self.base.handle_event(event);
            }
        }
    }
}

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

    #[test]
    fn barcode_scanner_default_state() {
        let bs = BarcodeScanner::new(Rect::new(0, 0, 300, 200));
        assert!(!bs.is_scanning());
        assert!(bs.last_result().is_none());
        assert_eq!(bs.scan_interval(), 100);
        assert!(bs.show_viewfinder());
        assert_eq!(bs.kind(), WidgetKind::BarcodeScanner);
    }

    #[test]
    fn barcode_scanner_toggle_scanning() {
        let mut bs = BarcodeScanner::new(Rect::new(0, 0, 300, 200));
        assert!(!bs.is_scanning());
        bs.start_scanning();
        assert!(bs.is_scanning());
        bs.stop_scanning();
        assert!(!bs.is_scanning());
        bs.toggle_scanning();
        assert!(bs.is_scanning());
        bs.toggle_scanning();
        assert!(!bs.is_scanning());
    }

    #[test]
    fn barcode_scanner_detect_and_clear() {
        let mut bs = BarcodeScanner::new(Rect::new(0, 0, 300, 200));
        assert!(bs.last_result().is_none());

        bs.detect_barcode("HelloWorld".to_string(), BarcodeFormat::QRCode);
        let result = bs.last_result().unwrap();
        assert_eq!(result.data, "HelloWorld");
        assert_eq!(result.format, BarcodeFormat::QRCode);

        bs.clear_result();
        assert!(bs.last_result().is_none());
    }

    #[test]
    fn barcode_scanner_detect_triggers_signal() {
        let mut bs = BarcodeScanner::new(Rect::new(0, 0, 300, 200));
        let detected = Arc::new(AtomicBool::new(false));
        let d = detected.clone();
        bs.barcode_detected.connect(move |_result| {
            d.store(true, Ordering::SeqCst);
        });

        bs.detect_barcode("TestData".to_string(), BarcodeFormat::Code128);
        assert!(detected.load(Ordering::SeqCst));
    }

    #[test]
    fn barcode_scanner_set_scan_interval() {
        let mut bs = BarcodeScanner::new(Rect::new(0, 0, 300, 200));
        bs.set_scan_interval(500);
        assert_eq!(bs.scan_interval(), 500);
        bs.set_scan_interval(5); // Should clamp to 10
        assert_eq!(bs.scan_interval(), 10);
    }

    #[test]
    fn barcode_scanner_viewfinder_visibility() {
        let mut bs = BarcodeScanner::new(Rect::new(0, 0, 300, 200));
        assert!(bs.show_viewfinder());
        bs.set_show_viewfinder(false);
        assert!(!bs.show_viewfinder());
        bs.set_show_viewfinder(true);
        assert!(bs.show_viewfinder());
    }

    #[test]
    fn barcode_format_names() {
        assert_eq!(BarcodeFormat::QRCode.name(), "QR Code");
        assert_eq!(BarcodeFormat::Code128.name(), "Code 128");
        assert_eq!(BarcodeFormat::Code39.name(), "Code 39");
        assert_eq!(BarcodeFormat::EAN13.name(), "EAN-13");
        assert_eq!(BarcodeFormat::EAN8.name(), "EAN-8");
        assert_eq!(BarcodeFormat::UPCA.name(), "UPC-A");
        assert_eq!(BarcodeFormat::DataMatrix.name(), "Data Matrix");
        assert_eq!(BarcodeFormat::PDF417.name(), "PDF417");
    }
}