From 5097faab66792884e5e527a9192a272fe20cf5d2 Mon Sep 17 00:00:00 2001 From: Kenny Levinsen Date: Tue, 14 Feb 2023 00:04:58 +0100 Subject: [PATCH] launcher: Save prompt on exit --- src/event.rs | 1 + src/keyboard/keysyms.rs | 3 ++- src/state.rs | 6 ++++-- src/widgets/launcher.rs | 21 +++++++++++++++++++++ 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/event.rs b/src/event.rs index eb0811e..b9b2b9d 100644 --- a/src/event.rs +++ b/src/event.rs @@ -28,6 +28,7 @@ pub enum Event { KeyEvent(KeyEvent), PointerEvent(PointerEvent), TokenUpdate(String), + FocusLost, } pub struct Events { diff --git a/src/keyboard/keysyms.rs b/src/keyboard/keysyms.rs index 898303f..7b413fd 100644 --- a/src/keyboard/keysyms.rs +++ b/src/keyboard/keysyms.rs @@ -3,7 +3,7 @@ pub const XKB_KEY_BackSpace: u32 = 0xff08; /* Back space, back char */ //pub const XKB_KEY_Tab: u32 = 0xff09; pub const XKB_KEY_Return: u32 = 0xff0d; /* Return, enter */ -//pub const XKB_KEY_Escape: u32 = 0xff1b; +pub const XKB_KEY_Escape: u32 = 0xff1b; pub const XKB_KEY_Delete: u32 = 0xffff; /* Delete, rubout */ pub const XKB_KEY_Home: u32 = 0xff50; pub const XKB_KEY_Left: u32 = 0xff51; /* Move left, left arrow */ @@ -17,4 +17,5 @@ pub const XKB_KEY_a: u32 = 0x0061; /* U+0061 LATIN SMALL LETTER A */ pub const XKB_KEY_e: u32 = 0x0065; /* U+0065 LATIN SMALL LETTER E */ //pub const XKB_KEY_c: u32 = 0x0063; /* U+0063 LATIN SMALL LETTER C */ pub const XKB_KEY_k: u32 = 0x006b; /* U+006B LATIN SMALL LETTER K */ +pub const XKB_KEY_r: u32 = 0x0072; /* U+0072 LATIN SMALL LETTER R */ pub const XKB_KEY_u: u32 = 0x0075; /* U+0075 LATIN SMALL LETTER U */ diff --git a/src/state.rs b/src/state.rs index 4a8af78..992e8ad 100644 --- a/src/state.rs +++ b/src/state.rs @@ -2,7 +2,6 @@ use std::{ cmp::max, default::Default, env, - process::exit, rc::Rc, sync::{Arc, Mutex}, }; @@ -498,7 +497,10 @@ impl Dispatch for State { if activated { state.activated = true; } else if state.activated && !activated { - exit(0); + let ev = Event::FocusLost; + for widget in state.widgets.iter_mut() { + widget.event(&ev); + } } if state.dimensions != (width, height) { diff --git a/src/widgets/launcher.rs b/src/widgets/launcher.rs index 43ac8de..1531062 100644 --- a/src/widgets/launcher.rs +++ b/src/widgets/launcher.rs @@ -629,6 +629,19 @@ impl Interface { } } + fn exit(&mut self) { + if self.inner.prompt.input.len() == 0 { + std::process::exit(0); + } + + let home = env::var_os("HOME").unwrap().into_string().unwrap(); + if let Ok(mut f) = File::create(format!("{}/.cache/wldash/prompt", home)) { + write!(f, "{}", self.inner.prompt.input).unwrap(); + f.sync_data().unwrap(); + } + std::process::exit(0); + } + fn keyboard_input(&mut self, event: &KeyEvent) { if event.state != WEnum::Value(wl_keyboard::KeyState::Pressed) { return; @@ -652,6 +665,12 @@ impl Interface { self.inner.selection = 0; widget.update(&self.inner); } + keysyms::XKB_KEY_r if event.modifiers.ctrl => { + let home = env::var_os("HOME").unwrap().into_string().unwrap(); + if let Ok(s) = read_to_string(format!("{}/.cache/wldash/prompt", home)) { + self.inner.prompt.set(&s); + } + } keysyms::XKB_KEY_Home => self.inner.prompt.home(), keysyms::XKB_KEY_End => self.inner.prompt.end(), keysyms::XKB_KEY_BackSpace => { @@ -664,6 +683,7 @@ impl Interface { self.inner.selection = 0; widget.update(&self.inner); } + keysyms::XKB_KEY_Escape => self.exit(), keysyms::XKB_KEY_Return => widget.trigger(&mut self.inner), keysyms::XKB_KEY_Left => self.inner.prompt.move_cursor(-1), keysyms::XKB_KEY_Right => self.inner.prompt.move_cursor(1), @@ -724,6 +744,7 @@ impl Widget for Interface { match event { Event::KeyEvent(e) => self.keyboard_input(e), Event::PointerEvent(e) => self.pointer_input(e), + Event::FocusLost => self.exit(), Event::LauncherUpdate => { self.inner.dirty = true; self.launcher.update(&self.inner);