egui-elements 1.4.0

Themes, widgets, and components for egui.
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
use crate::theme::Theme;
use crate::widgets::{
   Button,
   secure_text_edit::{SecureTextEdit, SecureTextEditOutput, TextBuffer},
};
use egui::*;
use egui_lucide::Lucide;

#[cfg(all(
   feature = "qr-scanner",
   any(target_os = "linux", target_os = "windows")
))]
use super::QRScanner;

use secure_types::SecureString;

/// A secure input field that can be used to edit a text containing sensitive information.
///
/// Show/hide uses Lucide `Eye` / `EyeOff`; the QR button (Linux/Windows + `qr-scanner`)
/// uses `ScanQrCode`. Both are colored with [`crate::theme::ThemeColors::text`].
///
/// The host app must register egui's SVG loader once:
/// `egui_extras::install_image_loaders(ctx)` with the `egui_extras` `svg` feature.
#[derive(Clone)]
pub struct SecureInputField {
   open: bool,
   pub(crate) text: SecureString,
   text_hidden: bool,
   id: &'static str,
   icon_size: Vec2,
   min_size: Vec2,
   inner_margin: Option<Margin>,
   #[cfg(all(
      feature = "qr-scanner",
      any(target_os = "linux", target_os = "windows")
   ))]
   qr_scanner: QRScanner,
   qr_enabled: bool,
}

impl SecureInputField {
   /// Create a new secure input field.
   ///
   /// # Arguments
   ///
   /// * `id` - The id of the input field, this will also be used as the name of the input field
   /// * `text_hidden` - Whether the text is masked
   /// * `open` - Whether the input field is open
   ///
   /// # Panics
   ///
   /// If the `SecureString` allocation fails.
   pub fn new(id: &'static str, text_hidden: bool, open: bool) -> Self {
      Self {
         open,
         text: SecureString::new_with_capacity(32).unwrap(),
         text_hidden,
         id,
         icon_size: vec2(20.0, 20.0),
         min_size: vec2(300.0, 20.0),
         inner_margin: None,
         #[cfg(all(
            feature = "qr-scanner",
            any(target_os = "linux", target_os = "windows")
         ))]
         qr_scanner: QRScanner::new(),
         qr_enabled: true,
      }
   }

   /// Whether the field is currently shown.
   pub fn is_open(&self) -> bool {
      self.open
   }

   /// Start showing the field on the next [`Self::show`].
   pub fn open(&mut self) {
      self.open = true;
   }

   /// Hide the field. Does not erase the text.
   pub fn close(&mut self) {
      self.open = false;
   }

   /// Erase the text from memory
   pub fn erase(&mut self) {
      self.text.erase();
   }

   /// Builder: Lucide icon size for the show/hide (and QR) buttons.
   ///
   /// Display size is `size.x` (icons are square).
   pub fn icon_size(mut self, size: Vec2) -> Self {
      self.icon_size = size;
      self
   }

   /// Builder: minimum allocated size of the text field.
   pub fn with_min_size(mut self, size: Vec2) -> Self {
      self.min_size = size;
      self
   }

   /// Return the minimum allocated size of the text field.
   pub fn min_size(&self) -> Vec2 {
      self.min_size
   }

   /// Builder: inner margin around the text.
   pub fn inner_margin(mut self, margin: Margin) -> Self {
      self.inner_margin = Some(margin);
      self
   }

   /// Builder: show the QR-scan button (Linux/Windows + `qr-scanner` only).
   pub fn qr_enabled(mut self, enabled: bool) -> Self {
      self.qr_enabled = enabled;
      self
   }

   /// Whether the text is currently masked.
   pub fn is_text_hidden(&self) -> bool {
      self.text_hidden
   }

   /// Return a clone of the text
   pub fn text(&self) -> SecureString {
      self.text.clone()
   }

   /// Set the id of this input field
   pub fn set_id(&mut self, id: &'static str) {
      self.id = id;
   }

   /// Set whether this input field is hidden
   pub fn set_text_hidden(&mut self, text_hidden: bool) {
      self.text_hidden = text_hidden;
   }

   /// Set the text of this input field
   pub fn set_text(&mut self, text: SecureString) {
      self.text = text;
   }

   /// Set the minimum size of this input field
   pub fn set_min_size(&mut self, size: Vec2) {
      self.min_size = size;
   }

   /// Set the icon size of this input field
   pub fn set_icon_size(&mut self, size: Vec2) {
      self.icon_size = size;
   }

   /// Set the inner margin of this input field
   pub fn set_inner_margin(&mut self, margin: Margin) {
      self.inner_margin = Some(margin);
   }

   /// Enable the QR scanner
   pub fn enable_qr_scanner(&mut self) {
      self.qr_enabled = true;
   }

   /// Disable the QR scanner
   pub fn disable_qr_scanner(&mut self) {
      self.qr_enabled = false;
   }

   /// Show this input field
   ///
   /// # Returns
   /// `SecureTextEditOutput`
   pub fn show(&mut self, ui: &mut Ui) -> Option<SecureTextEditOutput> {
      if !self.open {
         return None;
      }

      let mut hidden = self.is_text_hidden();

      let (output, qr_clicked) = show_field_chrome(
         ui,
         self.id,
         &mut self.text,
         Some(&mut hidden),
         self.min_size,
         self.inner_margin,
         WidgetText::default(),
         self.icon_size,
         self.qr_enabled,
      );

      #[cfg(all(
         feature = "qr-scanner",
         any(target_os = "linux", target_os = "windows")
      ))]
      {
         if let Some(res) = drive_qr_scanner(
            &mut self.qr_scanner,
            ui.ctx(),
            qr_clicked,
            self.qr_enabled,
         ) {
            self.set_text(res);
         }
      }

      #[cfg(not(all(
         feature = "qr-scanner",
         any(target_os = "linux", target_os = "windows")
      )))]
      let _ = qr_clicked;

      self.set_text_hidden(hidden);

      Some(output)
   }
}

/// A themed single-line input field backed by an owned [`String`].
///
/// Same chrome as [`SecureInputField`] minus the show/hide toggle — the field
/// name above the row and the Lucide `ScanQrCode` button — but the buffer is a
/// plain [`String`] instead of a [`SecureString`]. The text is never masked:
/// this field is for non-sensitive input.
///
/// Owning its buffer is what lets a host **store** the field in its UI state,
/// and that is what makes the QR button work: the scanner's live state lives on
/// the field, so the field has to survive across frames for a scan to land. It
/// does not zeroize on drop or [`Self::erase`]. Use [`SecureInputField`] for
/// secrets.
///
/// The host app must register egui's SVG loader once:
/// `egui_extras::install_image_loaders(ctx)` with the `egui_extras` `svg` feature.
pub struct InputField {
   open: bool,
   text: String,
   id: &'static str,
   icon_size: Vec2,
   min_size: Vec2,
   inner_margin: Option<Margin>,
   /// Placeholder shown while the buffer is empty.
   hint_text: WidgetText,
   #[cfg(all(
      feature = "qr-scanner",
      any(target_os = "linux", target_os = "windows")
   ))]
   qr_scanner: QRScanner,
   qr_enabled: bool,
}

impl InputField {
   /// Create a new input field with an empty buffer.
   ///
   /// # Arguments
   ///
   /// * `id` - The id of the input field; also the field-name label above the row
   ///   (an empty `id` draws no label)
   /// * `open` - Whether the input field is open
   pub fn new(id: &'static str, open: bool) -> Self {
      Self {
         open,
         text: String::new(),
         id,
         icon_size: vec2(20.0, 20.0),
         min_size: vec2(300.0, 20.0),
         inner_margin: None,
         hint_text: WidgetText::default(),
         #[cfg(all(
            feature = "qr-scanner",
            any(target_os = "linux", target_os = "windows")
         ))]
         qr_scanner: QRScanner::new(),
         qr_enabled: true,
      }
   }

   /// Builder: placeholder shown while the buffer is empty.
   pub fn hint_text(mut self, hint_text: impl Into<WidgetText>) -> Self {
      self.hint_text = hint_text.into();
      self
   }

   /// Set the placeholder shown while the buffer is empty.
   pub fn set_hint_text(&mut self, hint_text: impl Into<WidgetText>) {
      self.hint_text = hint_text.into();
   }

   /// Whether the field is currently shown.
   pub fn is_open(&self) -> bool {
      self.open
   }

   /// Start showing the field on the next [`Self::show`].
   pub fn open(&mut self) {
      self.open = true;
   }

   /// Hide the field. Does not erase the text. Stops any open QR overlay.
   pub fn close(&mut self) {
      self.open = false;
      self.close_qr_scanner();
   }

   /// Stop an open QR overlay, if any.
   ///
   /// The scanner keeps a capture worker running while an overlay is open, and
   /// the field keeps the scanner alive — so a host that stops drawing the field
   /// (hides it, replaces it in the layout, …) must call this, or that worker
   /// would outlive the field that opened it.
   pub fn close_qr_scanner(&mut self) {
      #[cfg(all(
         feature = "qr-scanner",
         any(target_os = "linux", target_os = "windows")
      ))]
      self.qr_scanner.close();
   }

   /// Clear the buffer. Does not zeroize; use [`SecureInputField::erase`] for
   /// secrets.
   pub fn erase(&mut self) {
      self.text.clear();
   }

   /// The buffer contents.
   pub fn text(&self) -> &str {
      &self.text
   }

   /// Replace the buffer contents with `text`.
   pub fn set_text(&mut self, text: &str) {
      self.text.clear();
      self.text.push_str(text);
   }

   /// Builder: Lucide icon size for the QR button.
   ///
   /// Display size is `size.x` (icons are square).
   pub fn icon_size(mut self, size: Vec2) -> Self {
      self.icon_size = size;
      self
   }

   /// Builder: minimum allocated size of the text field.
   pub fn with_min_size(mut self, size: Vec2) -> Self {
      self.min_size = size;
      self
   }

   /// Return the minimum allocated size of the text field.
   pub fn min_size(&self) -> Vec2 {
      self.min_size
   }

   /// Builder: inner margin around the text.
   pub fn inner_margin(mut self, margin: Margin) -> Self {
      self.inner_margin = Some(margin);
      self
   }

   /// Set the id of this input field
   pub fn set_id(&mut self, id: &'static str) {
      self.id = id;
   }

   /// Set the minimum size of this input field
   pub fn set_min_size(&mut self, size: Vec2) {
      self.min_size = size;
   }

   /// Set the icon size of this input field
   pub fn set_icon_size(&mut self, size: Vec2) {
      self.icon_size = size;
   }

   /// Set the inner margin of this input field
   pub fn set_inner_margin(&mut self, margin: Margin) {
      self.inner_margin = Some(margin);
   }

   /// Builder: show the QR-scan button (Linux/Windows + `qr-scanner` only).
   pub fn qr_enabled(mut self, enabled: bool) -> Self {
      self.qr_enabled = enabled;
      self
   }

   /// Enable the QR scanner
   pub fn enable_qr_scanner(&mut self) {
      self.qr_enabled = true;
   }

   /// Disable the QR scanner
   pub fn disable_qr_scanner(&mut self) {
      self.qr_enabled = false;
   }

   /// Show this input field
   ///
   /// # Returns
   /// `SecureTextEditOutput`
   pub fn show(&mut self, ui: &mut Ui) -> Option<SecureTextEditOutput> {
      if !self.open {
         return None;
      }

      let (output, qr_clicked) = show_field_chrome(
         ui,
         self.id,
         &mut self.text,
         None,
         self.min_size,
         self.inner_margin,
         self.hint_text.clone(),
         self.icon_size,
         self.qr_enabled,
      );

      #[cfg(all(
         feature = "qr-scanner",
         any(target_os = "linux", target_os = "windows")
      ))]
      {
         if let Some(res) = drive_qr_scanner(
            &mut self.qr_scanner,
            ui.ctx(),
            qr_clicked,
            self.qr_enabled,
         ) {
            let decoded = res.unlock_str(|s| s.to_owned());
            self.set_text(&decoded);
         }
      }

      #[cfg(not(all(
         feature = "qr-scanner",
         any(target_os = "linux", target_os = "windows")
      )))]
      let _ = qr_clicked;

      Some(output)
   }
}

/// Drive the QR overlay for one frame: open it when `clicked`, advance it, and
/// take a decode if one arrived.
///
/// Returns the decoded payload (a `SecureString`, zeroized when dropped) or
/// `None`. No-op when `!enabled`, so the caller can gate the button and the
/// scanner with the same flag. Shared by [`SecureInputField`] and [`InputField`].
#[cfg(all(
   feature = "qr-scanner",
   any(target_os = "linux", target_os = "windows")
))]
fn drive_qr_scanner(
   scanner: &mut QRScanner,
   ctx: &Context,
   clicked: bool,
   enabled: bool,
) -> Option<SecureString> {
   if !enabled {
      return None;
   }
   if clicked {
      scanner.open(ctx.clone());
   }
   scanner.show(ctx);
   if let Some(res) = scanner.take_result() {
      scanner.reset();
      Some(res)
   } else {
      None
   }
}

/// Shared chrome for [`SecureInputField`] and [`InputField`]: the field name
/// above the row, a single-line [`SecureTextEdit`], the Lucide `Eye` / `EyeOff`
/// show/hide toggle when `hidden` is `Some` — plus the QR button when
/// `qr_enabled` and the `qr-scanner` feature is active on Linux/Windows.
///
/// An empty `id` skips the field-name label: the host already labels the field.
///
/// `hidden` is the masking state and the flag behind the toggle; `None` means
/// the field is never masked and no toggle is drawn. It is toggled in place, and
/// the caller stores it back on its field.
///
/// Returns the edit output and whether the QR button was clicked. The caller
/// owns the scanner lifecycle, so the scanner stays out of this helper.
#[allow(clippy::too_many_arguments)]
fn show_field_chrome(
   ui: &mut Ui,
   id: &'static str,
   text: &mut dyn TextBuffer,
   mut hidden: Option<&mut bool>,
   min_size: Vec2,
   inner_margin: Option<Margin>,
   hint_text: WidgetText,
   icon_size: Vec2,
   qr_enabled: bool,
) -> (SecureTextEditOutput, bool) {
   let theme = Theme::current(ui.ctx());

   if !id.is_empty() {
      ui.label(RichText::new(id).size(theme.typography.large));
   }

   let margin = inner_margin.unwrap_or_else(|| Margin::same(theme.inner_margin));
   let row_height = (theme.typography.normal + margin.sum().y).max(min_size.y);
   let row_size = vec2(min_size.x, row_height);

   let password = hidden.as_deref().copied().unwrap_or(false);
   let text_edit = SecureTextEdit::singleline(text)
      .min_size(min_size)
      .margin(margin)
      .hint_text(hint_text)
      .password(password)
      .font(FontId::proportional(theme.typography.normal));

   #[cfg_attr(
      not(all(
         feature = "qr-scanner",
         any(target_os = "linux", target_os = "windows")
      )),
      allow(unused_mut)
   )]
   let mut qr_clicked = false;

   let output = ui.allocate_ui_with_layout(
      row_size,
      Layout::left_to_right(Align::Center),
      |ui| {
         let output = text_edit.show(ui);

         let text_color = theme.colors.text;
         if let Some(state) = hidden.as_deref_mut() {
            let icon = if *state { Lucide::EyeOff } else { Lucide::Eye };
            let img = icon.size(icon_size.x).color(text_color).image();
            if ui.add(Button::image(img)).clicked() {
               *state = !*state;
            }
         }

         #[cfg(all(
            feature = "qr-scanner",
            any(target_os = "linux", target_os = "windows")
         ))]
         {
            if qr_enabled {
               let icon = Lucide::ScanQrCode;
               let img = icon.size(icon_size.x).color(text_color).image();
               if ui.add(Button::image(img)).clicked() {
                  qr_clicked = true;
               }
            }
         }

         output
      },
   );

   #[cfg(not(all(
      feature = "qr-scanner",
      any(target_os = "linux", target_os = "windows")
   )))]
   let _ = qr_enabled;

   (output.inner, qr_clicked)
}

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

   #[test]
   fn input_field_drives_a_plain_string() {
      let mut field = InputField::new("Name", true);

      field.set_text("hello");
      assert_eq!(field.text(), "hello");

      field.erase();
      assert_eq!(field.text(), "");
   }
}