e_window 0.1.15

A window tool. Think WinAPI ShowMessageBox; but more than that.
Documentation
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
//! uxn.rs - Uxn integration for e_window

#[cfg(feature = "uses_uxn")]
pub mod uxn {

    use raven_uxn::{Backend, Uxn};
    use raven_varvara::Varvara;
    use std::path::Path;
    use std::sync::{Arc, Mutex};
    /// UxnModule: Encapsulates a Uxn VM and its state for e_window
    pub struct UxnModule {
        pub uxn: Arc<Mutex<Uxn<'static>>>,
        pub varvara: Option<Varvara>,
    }

    impl UxnModule {
        /// Create a new UxnModule, optionally loading a ROM file
        pub fn new(rom_path: Option<&Path>) -> Result<Self, String> {
            // Use a static RAM buffer for the Uxn VM
            static mut RAM: [u8; 65536] = [0; 65536];
            let ram: &'static mut [u8; 65536] = unsafe { &mut RAM };
            let mut uxn = Uxn::new(ram, Backend::Interpreter);
            let varvara = Varvara::default();
            if let Some(path) = rom_path {
                let rom = std::fs::read(path).map_err(|e| format!("Failed to read ROM: {e}"))?;
                uxn.reset(&rom);
            }
            Ok(UxnModule {
                uxn: Arc::new(Mutex::new(uxn)),
                varvara: Some(varvara),
            })
        }

        /// Reset the Uxn VM (clears memory and state)
        pub fn reset(&self, rom: &[u8]) {
            let mut uxn = self.uxn.lock().unwrap();
            uxn.reset(rom);
        }

        /// Load a new ROM into the Uxn VM (resets VM)
        pub fn load_rom(&self, rom_path: &Path) -> Result<(), String> {
            let rom = std::fs::read(rom_path).map_err(|e| format!("Failed to read ROM: {e}"))?;
            self.reset(&rom);
            Ok(())
        }
    }
}

#[cfg(not(feature = "uses_uxn"))]
pub mod uxn {
    // Stub module for when uses_uxn is disabled
}

use log::{error, info};
use std::path::Path;
use std::sync::{Arc, Mutex};
// Re-export the raven-uxn and raven-varvara crates for Uxn VM and utilities
use raven_uxn::{Backend, Uxn};
use raven_varvara::Key;
use raven_varvara::MouseState;
use raven_varvara::Varvara;
/// UxnModule: Encapsulates a Uxn VM and its state for e_window
pub struct UxnModule {
    pub uxn: Arc<Mutex<Uxn<'static>>>,
    pub varvara: Option<Varvara>,
}

impl UxnModule {
    /// Create a new UxnModule, optionally loading a ROM file
    pub fn new(rom_path: Option<&Path>) -> Result<Self, String> {
        // Use a static RAM buffer for the Uxn VM
        static mut RAM: [u8; 65536] = [0; 65536];
        let ram: &'static mut [u8; 65536] = unsafe { &mut RAM };
        let mut uxn = Uxn::new(ram, Backend::Interpreter);
        let varvara = Varvara::default();
        if let Some(path) = rom_path {
            let rom = std::fs::read(path).map_err(|e| format!("Failed to read ROM: {e}"))?;
            uxn.reset(&rom);
        }
        Ok(UxnModule {
            uxn: Arc::new(Mutex::new(uxn)),
            varvara: Some(varvara),
        })
    }

    // Step/run methods are not available in raven-uxn. Use run/reset as needed.

    // No run_cycles method; use run/reset as needed.

    /// Reset the Uxn VM (clears memory and state)
    pub fn reset(&self, rom: &[u8]) {
        let mut uxn = self.uxn.lock().unwrap();
        uxn.reset(rom);
    }

    /// Load a new ROM into the Uxn VM (resets VM)
    pub fn load_rom(&self, rom_path: &Path) -> Result<(), String> {
        let rom = std::fs::read(rom_path).map_err(|e| format!("Failed to read ROM: {e}"))?;
        self.reset(&rom);
        Ok(())
    }

    // No get_state method; UxnState is not available in raven-uxn.
}

// Optionally, add egui integration for UxnModule (UI panel, etc.)
#[cfg(feature = "egui")]
pub mod egui_ui {
    use super::*;
    use egui::{CollapsingHeader, Ui};

    pub fn show_uxn_panel(ui: &mut Ui, _uxn_mod: &UxnModule) {
        CollapsingHeader::new("Uxn VM State").show(ui, |ui| {
            ui.label("Uxn state display not implemented (no UxnState in raven_uxn)");
        });
    }
}
use eframe::egui;
use std::sync::mpsc;
// use log::{error, info};

#[derive(Debug)]
pub enum Event {
    LoadRom(Vec<u8>),
    SetMuted(bool),
    Console(u8),
}

pub struct UxnApp<'a> {
    vm: Uxn<'a>,
    dev: Varvara,
    scale: f32,
    size: (u16, u16),
    next_frame: f64,
    scroll: (f32, f32),
    cursor_pos: Option<(f32, f32)>,
    texture: egui::TextureHandle,
    event_rx: mpsc::Receiver<Event>,
    resized: Option<Box<dyn FnMut(u16, u16)>>,
    window_mode: String,
    aspect_ratio: f32,
    // For auto ROM cycling
    auto_rom_select: bool,
    auto_timer: f64,
    auto_index: usize,
    auto_roms: Vec<Vec<u8>>,
    /// Parallel to auto_roms: labels or filenames for each ROM
    auto_rom_labels: Vec<String>,
    /// Callback for when the ROM changes (filename or label)
    on_rom_change: Option<Box<dyn Fn(&str) + Send + Sync>>,
    /// The current ROM label or filename (if available)
    current_rom_label: Option<String>,
}

impl<'a> UxnApp<'a> {
    /// Set the current ROM label or filename (for title/callback)
    pub fn set_rom_label<S: Into<String>>(&mut self, label: S) {
        self.current_rom_label = Some(label.into());
    }
    pub fn new_with_mode(
        mut vm: Uxn<'a>,
        mut dev: Varvara,
        size: (u16, u16),
        scale: f32,
        event_rx: mpsc::Receiver<Event>,
        ctx: &egui::Context,
        window_mode: String,
        auto_roms: Vec<Vec<u8>>,
        auto_rom_labels: Vec<String>,
        auto_rom_select: bool,
    ) -> Self {
        // Run the VM and redraw once to initialize the framebuffer
        vm.run(&mut dev, 0x100);
        dev.redraw(&mut vm);

        let w = usize::from(size.0);
        let h = usize::from(size.1);
        let image = egui::ColorImage::new([w, h], vec![egui::Color32::BLACK; w * h]);
        let texture = ctx.load_texture("frame", image, egui::TextureOptions::NEAREST);
        let aspect_ratio = w as f32 / h as f32;
        let mut auto_index = 0;
        let mut auto_timer = 0.0;
        let mut current_rom_label = None;
        if auto_rom_select && !auto_roms.is_empty() {
            println!(
                "[AUTO ROM CYCLING] Enabled. Cycling through {} ROMs every 10s.",
                auto_roms.len()
            );
            // Load the first ROM immediately
            let rom = &auto_roms[0];
            let _ = vm.reset(rom);
            dev.reset(rom);
            vm.run(&mut dev, 0x100);
            dev.redraw(&mut vm);
            auto_index = 0;
            auto_timer = 0.0;
            // Use the provided label if available, else fallback
            if !auto_rom_labels.is_empty() {
                current_rom_label = Some(auto_rom_labels[0].clone());
            } else {
                current_rom_label = Some("ROM 1".to_string());
            }
        }
        UxnApp {
            vm,
            dev,
            scale,
            size,
            next_frame: 0.0,
            event_rx,
            resized: None,
            scroll: (0.0, 0.0),
            cursor_pos: None,
            texture,
            window_mode,
            aspect_ratio,
            auto_rom_select,
            auto_timer,
            auto_index,
            auto_roms,
            on_rom_change: None,
            current_rom_label,
            auto_rom_labels,
        }
    }
    /// Set a callback to be called when the ROM changes (filename or label)
    pub fn set_on_rom_change<F>(&mut self, f: F)
    where
        F: Fn(&str) + Send + Sync + 'static,
    {
        self.on_rom_change = Some(Box::new(f));
    }

    pub fn set_resize_callback(&mut self, f: Box<dyn FnMut(u16, u16)>) {
        self.resized = Some(f);
    }

    fn load_rom(&mut self, data: &[u8]) -> anyhow::Result<()> {
        let data = self.vm.reset(data);
        self.dev.reset(data);
        self.vm.run(&mut self.dev, 0x100);
        let out = self.dev.output(&self.vm);
        out.check()?;
        // Try to get a label from dev, fallback to current_rom_label or unknown
        let label = self.current_rom_label.as_deref().unwrap_or("[unknown ROM]");
        if let Some(cb) = &self.on_rom_change {
            cb(label);
        }
        Ok(())
    }
}

impl eframe::App for UxnApp<'_> {
    fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
        // --- AUTO ROM CYCLING: Switch between ROMs every 10 seconds if enabled ---
        if self.auto_rom_select && !self.auto_roms.is_empty() {
            let dt = ctx.input(|i| i.stable_dt) as f64;
            log::debug!(
                "[AUTO ROM CYCLING] auto_timer: {:.3}, dt: {:.3}, auto_index: {}, roms: {}",
                self.auto_timer,
                dt,
                self.auto_index,
                self.auto_roms.len()
            );
            self.auto_timer += dt;
            if self.auto_timer == 0.0 && self.auto_index == 0 {
                log::debug!("[AUTO ROM CYCLING] First ROM already loaded.");
                // Already loaded first ROM in new_with_mode
            } else if self.auto_timer > 10.0 {
                log::debug!(
                    "[AUTO ROM CYCLING] Switching ROM: {} -> {}",
                    self.auto_index,
                    (self.auto_index + 1) % self.auto_roms.len()
                );
                self.auto_timer = 0.0;
                self.auto_index = (self.auto_index + 1) % self.auto_roms.len();
                let rom = self.auto_roms[self.auto_index].clone();
                // Use the correct label for the new ROM
                let label = if self.auto_index < self.auto_rom_labels.len() {
                    self.auto_rom_labels[self.auto_index].clone()
                } else {
                    format!("ROM {}", self.auto_index + 1)
                };
                self.set_rom_label(label);
                let _ = self.load_rom(&rom);
            }
        }
        while let Ok(e) = self.event_rx.try_recv() {
            match e {
                Event::LoadRom(data) => {
                    if let Err(e) = self.load_rom(&data) {
                        error!("could not load rom: {e:?}");
                    }
                }
                Event::SetMuted(m) => {
                    self.dev.audio_set_muted(m);
                }
                Event::Console(b) => {
                    self.dev.console(&mut self.vm, b);
                }
            }
        }

        ctx.request_repaint();
        ctx.input(|i| {
            while i.time >= self.next_frame {
                self.next_frame += 0.0166667;
                self.dev.redraw(&mut self.vm);
            }
            if i.raw.dropped_files.len() == 1 {
                let target = &i.raw.dropped_files[0];
                let r = if let Some(path) = &target.path {
                    let data = std::fs::read(path).expect("failed to read file");
                    info!("loading {} bytes from {path:?}", data.len());
                    self.load_rom(&data)
                } else if let Some(data) = &target.bytes {
                    self.load_rom(data)
                } else {
                    Ok(())
                };
                if let Err(e) = r {
                    error!("could not load ROM: {e:?}");
                }
            }
            let shift_held = i.modifiers.shift;
            for e in i.events.iter() {
                match e {
                    egui::Event::Text(s) => {
                        const RAW_CHARS: [u8; 16] = [
                            b'"', b'\'', b'{', b'}', b'_', b')', b'(', b'*', b'&', b'^', b'%',
                            b'$', b'#', b'@', b'!', b'~',
                        ];
                        for c in s.bytes() {
                            if RAW_CHARS.contains(&c) {
                                self.dev.char(&mut self.vm, c);
                            }
                        }
                    }
                    egui::Event::Key {
                        key,
                        pressed,
                        repeat,
                        ..
                    } => {
                        if let Some(k) = decode_key(*key, shift_held) {
                            if *pressed {
                                self.dev.pressed(&mut self.vm, k, *repeat);
                            } else {
                                self.dev.released(&mut self.vm, k);
                            }
                        }
                    }
                    // egui::Event::ScrollDelta(s) => {
                    //     self.scroll.0 += s.x;
                    //     self.scroll.1 -= s.y;
                    // }
                    _ => (),
                }
            }
            for (b, k) in [
                (i.modifiers.ctrl, Key::Ctrl),
                (i.modifiers.alt, Key::Alt),
                (i.modifiers.shift, Key::Shift),
            ] {
                if b {
                    self.dev.pressed(&mut self.vm, k, false)
                } else {
                    self.dev.released(&mut self.vm, k)
                }
            }
            let ptr = &i.pointer;
            if let Some(p) = ptr.latest_pos() {
                self.cursor_pos = Some((p.x / self.scale, p.y / self.scale));
            }
            let buttons = [
                egui::PointerButton::Primary,
                egui::PointerButton::Middle,
                egui::PointerButton::Secondary,
            ]
            .into_iter()
            .enumerate()
            .map(|(i, b)| (ptr.button_down(b) as u8) << i)
            .fold(0, |a, b| a | b);
            let m = MouseState {
                pos: self.cursor_pos.unwrap_or((0.0, 0.0)),
                scroll: std::mem::take(&mut self.scroll),
                buttons,
            };
            self.dev.mouse(&mut self.vm, m);
            i.time
        });
        self.dev.audio(&mut self.vm);
        let out = self.dev.output(&self.vm);
        if out.hide_mouse {
            ctx.set_cursor_icon(egui::CursorIcon::None);
        }
        if self.size != out.size {
            // Get current window size in logical points
            let current_window_size = ctx.input(|i| {
                i.viewport()
                    .inner_rect
                    .map_or(egui::Vec2::ZERO, |rect| rect.size())
            });
            let new_size = egui::Vec2::new(out.size.0 as f32, out.size.1 as f32) * self.scale;
            // let should_resize = new_size.x > current_window_size.x || new_size.y > current_window_size.y
            //     || new_size.x < current_window_size.x || new_size.y < current_window_size.y;
            // // Only resize if the new frame is larger, or if it's smaller than the window
            // if should_resize {
            // Only resize if the new frame is larger than the current window
            if new_size.x > current_window_size.x || new_size.y > current_window_size.y {
                info!("resizing window to {:?}", out.size);
                self.size = out.size;
                let mut size = new_size;
                // Enforce proportional resizing if needed
                if self.window_mode == "proportional" {
                    let aspect = self.aspect_ratio;
                    let w = size.x;
                    let h = size.y;
                    let (new_w, new_h) = if w / h > aspect {
                        (h * aspect, h)
                    } else {
                        (w, w / aspect)
                    };
                    size = egui::Vec2::new(new_w, new_h);
                }
                ctx.send_viewport_cmd(egui::ViewportCommand::InnerSize(size));
                if let Some(f) = self.resized.as_mut() {
                    f(out.size.0, out.size.1);
                }
            }
        }
        let w = out.size.0 as usize;
        let h = out.size.1 as usize;
        let mut image = egui::ColorImage::new([w, h], vec![egui::Color32::BLACK; w * h]);
        for (i, o) in out.frame.chunks(4).zip(image.pixels.iter_mut()) {
            *o = egui::Color32::from_rgba_unmultiplied(i[2], i[1], i[0], i[3]);
        }
        self.texture.set(image, egui::TextureOptions::NEAREST);
        egui::CentralPanel::default()
            .frame(egui::Frame::default().fill(egui::Color32::BLACK))
            .show(ctx, |ui| {
                let available = ui.available_size();
                let frame_size = egui::Vec2::new(
                    out.size.0 as f32 * self.scale,
                    out.size.1 as f32 * self.scale,
                );
                let offset = egui::Vec2::new(
                    (available.x - frame_size.x) * 0.5,
                    (available.y - frame_size.y) * 0.5,
                );
                let top_left = ui.min_rect().min + offset;
                let mut mesh = egui::Mesh::with_texture(self.texture.id());
                mesh.add_rect_with_uv(
                    egui::Rect {
                        min: top_left,
                        max: top_left + frame_size,
                    },
                    egui::Rect {
                        min: egui::Pos2::new(0.0, 0.0),
                        max: egui::Pos2::new(1.0, 1.0),
                    },
                    egui::Color32::WHITE,
                );
                ui.painter().add(egui::Shape::mesh(mesh));
                // Show auto ROM cycling info
                if self.auto_rom_select && !self.auto_roms.is_empty() {
                    ui.label(format!(
                        "[AUTO ROM CYCLING] Switching ROM every 10s. ROMs loaded: {}",
                        self.auto_roms.len()
                    ));
                }
            });
        out.check().expect("failed to print output?");
    }
}

pub fn decode_key(k: egui::Key, shift: bool) -> Option<Key> {
    let c = match (k, shift) {
        (egui::Key::ArrowUp, _) => Key::Up,
        (egui::Key::ArrowDown, _) => Key::Down,
        (egui::Key::ArrowLeft, _) => Key::Left,
        (egui::Key::ArrowRight, _) => Key::Right,
        (egui::Key::Home, _) => Key::Home,
        (egui::Key::Num0, false) => Key::Char(b'0'),
        (egui::Key::Num0, true) => Key::Char(b')'),
        (egui::Key::Num1, false) => Key::Char(b'1'),
        (egui::Key::Num1, true) => Key::Char(b'!'),
        (egui::Key::Num2, false) => Key::Char(b'2'),
        (egui::Key::Num2, true) => Key::Char(b'@'),
        (egui::Key::Num3, false) => Key::Char(b'3'),
        (egui::Key::Num3, true) => Key::Char(b'#'),
        (egui::Key::Num4, false) => Key::Char(b'4'),
        (egui::Key::Num4, true) => Key::Char(b'$'),
        (egui::Key::Num5, false) => Key::Char(b'5'),
        (egui::Key::Num5, true) => Key::Char(b'5'),
        (egui::Key::Num6, false) => Key::Char(b'6'),
        (egui::Key::Num6, true) => Key::Char(b'^'),
        (egui::Key::Num7, false) => Key::Char(b'7'),
        (egui::Key::Num7, true) => Key::Char(b'&'),
        (egui::Key::Num8, false) => Key::Char(b'8'),
        (egui::Key::Num8, true) => Key::Char(b'*'),
        (egui::Key::Num9, false) => Key::Char(b'9'),
        (egui::Key::Num9, true) => Key::Char(b'('),
        (egui::Key::A, false) => Key::Char(b'a'),
        (egui::Key::A, true) => Key::Char(b'A'),
        (egui::Key::B, false) => Key::Char(b'b'),
        (egui::Key::B, true) => Key::Char(b'B'),
        (egui::Key::C, false) => Key::Char(b'c'),
        (egui::Key::C, true) => Key::Char(b'C'),
        (egui::Key::D, false) => Key::Char(b'd'),
        (egui::Key::D, true) => Key::Char(b'D'),
        (egui::Key::E, false) => Key::Char(b'e'),
        (egui::Key::E, true) => Key::Char(b'E'),
        (egui::Key::F, false) => Key::Char(b'f'),
        (egui::Key::F, true) => Key::Char(b'F'),
        (egui::Key::G, false) => Key::Char(b'g'),
        (egui::Key::G, true) => Key::Char(b'G'),
        (egui::Key::H, false) => Key::Char(b'h'),
        (egui::Key::H, true) => Key::Char(b'H'),
        (egui::Key::I, false) => Key::Char(b'i'),
        (egui::Key::I, true) => Key::Char(b'I'),
        (egui::Key::J, false) => Key::Char(b'j'),
        (egui::Key::J, true) => Key::Char(b'J'),
        (egui::Key::K, false) => Key::Char(b'k'),
        (egui::Key::K, true) => Key::Char(b'K'),
        (egui::Key::L, false) => Key::Char(b'l'),
        (egui::Key::L, true) => Key::Char(b'L'),
        (egui::Key::M, false) => Key::Char(b'm'),
        (egui::Key::M, true) => Key::Char(b'M'),
        (egui::Key::N, false) => Key::Char(b'n'),
        (egui::Key::N, true) => Key::Char(b'N'),
        (egui::Key::O, false) => Key::Char(b'o'),
        (egui::Key::O, true) => Key::Char(b'O'),
        (egui::Key::P, false) => Key::Char(b'p'),
        (egui::Key::P, true) => Key::Char(b'P'),
        (egui::Key::Q, false) => Key::Char(b'q'),
        (egui::Key::Q, true) => Key::Char(b'Q'),
        (egui::Key::R, false) => Key::Char(b'r'),
        (egui::Key::R, true) => Key::Char(b'R'),
        (egui::Key::S, false) => Key::Char(b's'),
        (egui::Key::S, true) => Key::Char(b'S'),
        (egui::Key::T, false) => Key::Char(b't'),
        (egui::Key::T, true) => Key::Char(b'T'),
        (egui::Key::U, false) => Key::Char(b'u'),
        (egui::Key::U, true) => Key::Char(b'U'),
        (egui::Key::V, false) => Key::Char(b'v'),
        (egui::Key::V, true) => Key::Char(b'V'),
        (egui::Key::W, false) => Key::Char(b'w'),
        (egui::Key::W, true) => Key::Char(b'W'),
        (egui::Key::X, false) => Key::Char(b'x'),
        (egui::Key::X, true) => Key::Char(b'X'),
        (egui::Key::Y, false) => Key::Char(b'y'),
        (egui::Key::Y, true) => Key::Char(b'Y'),
        (egui::Key::Z, false) => Key::Char(b'z'),
        (egui::Key::Z, true) => Key::Char(b'Z'),
        (egui::Key::Backtick, false) => Key::Char(b'`'),
        (egui::Key::Backtick, true) => Key::Char(b'~'),
        (egui::Key::Backslash, _) => Key::Char(b'\\'),
        (egui::Key::Pipe, _) => Key::Char(b'|'),
        (egui::Key::Comma, false) => Key::Char(b','),
        (egui::Key::Comma, true) => Key::Char(b'<'),
        (egui::Key::Equals, _) => Key::Char(b'='),
        (egui::Key::Plus, _) => Key::Char(b'+'),
        (egui::Key::OpenBracket, false) => Key::Char(b'['),
        (egui::Key::OpenBracket, true) => Key::Char(b'{'),
        (egui::Key::Minus, false) => Key::Char(b'-'),
        (egui::Key::Minus, true) => Key::Char(b'_'),
        (egui::Key::Period, false) => Key::Char(b'.'),
        (egui::Key::Period, true) => Key::Char(b'>'),
        (egui::Key::CloseBracket, false) => Key::Char(b']'),
        (egui::Key::CloseBracket, true) => Key::Char(b'}'),
        (egui::Key::Semicolon, _) => Key::Char(b';'),
        (egui::Key::Colon, _) => Key::Char(b':'),
        (egui::Key::Slash, _) => Key::Char(b'/'),
        (egui::Key::Questionmark, _) => Key::Char(b'?'),
        (egui::Key::Space, _) => Key::Char(b' '),
        (egui::Key::Tab, _) => Key::Char(b'\t'),
        (egui::Key::Enter, _) => Key::Char(b'\r'),
        (egui::Key::Backspace, _) => Key::Char(0x08),
        _ => return None,
    };
    Some(c)
}