launcher: add cursor and basic operations

This commit is contained in:
Hilmar Gústafsson
2020-07-22 01:44:06 +02:00
committed by Kenny Levinsen
parent 6690cda29f
commit f25909b7f8
+40 -3
View File
@@ -18,6 +18,7 @@ use fuzzy_matcher::skim::{fuzzy_indices, fuzzy_match};
use smithay_client_toolkit::keyboard::keysyms; use smithay_client_toolkit::keyboard::keysyms;
pub struct Launcher<'a> { pub struct Launcher<'a> {
cursor: usize,
options: Vec<Desktop>, options: Vec<Desktop>,
term_opener: String, term_opener: String,
app_opener: String, app_opener: String,
@@ -44,6 +45,7 @@ impl<'a> Launcher<'a> {
url: String, url: String,
) -> Box<Launcher> { ) -> Box<Launcher> {
Box::new(Launcher { Box::new(Launcher {
cursor: 0,
options: load_desktop_files(), options: load_desktop_files(),
term_opener: term, term_opener: term,
app_opener: app, app_opener: app,
@@ -72,6 +74,19 @@ impl<'a> Launcher<'a> {
} else { } else {
Color::new(1.0, 1.0, 1.0, 1.0) Color::new(1.0, 1.0, 1.0, 1.0)
}; };
let dim =
self.font
.borrow_mut()
.auto_draw_text(buf, bg, &c, &self.input[..self.cursor])?;
let cursor_placement = dim.0;
// draw cursor
for i in 1..self.font_size {
buf.put((cursor_placement, i), &Color::new(1.0, 1.0, 1.0, 1.0))
.unwrap();
}
let dim = self let dim = self
.font .font
.borrow_mut() .borrow_mut()
@@ -343,13 +358,20 @@ impl<'a> Widget for Launcher<'a> {
match key { match key {
keysyms::XKB_KEY_u if modifiers.ctrl => self.leave(), keysyms::XKB_KEY_u if modifiers.ctrl => self.leave(),
keysyms::XKB_KEY_BackSpace => { keysyms::XKB_KEY_BackSpace => {
if !self.input.is_empty() { if !self.input.is_empty() && self.cursor > 0 {
self.input = self.input[..self.input.len() - 1].to_string(); self.cursor -= 1;
self.input.remove(self.cursor);
self.offset = 0; self.offset = 0;
self.result = None; self.result = None;
self.dirty = true self.dirty = true
} }
} }
keysyms::XKB_KEY_Delete => {
if !self.input.is_empty() && self.cursor < self.input.len() {
self.input.remove(self.cursor);
self.dirty = true;
}
}
keysyms::XKB_KEY_Return => { keysyms::XKB_KEY_Return => {
match self.input.chars().next() { match self.input.chars().next() {
Some('=') => { Some('=') => {
@@ -358,6 +380,7 @@ impl<'a> Widget for Launcher<'a> {
} }
} }
Some('!') => { Some('!') => {
self.cursor = 0;
let _ = Command::new("sh") let _ = Command::new("sh")
.arg("-c") .arg("-c")
.arg(self.input.chars().skip(1).collect::<String>()) .arg(self.input.chars().skip(1).collect::<String>())
@@ -365,6 +388,7 @@ impl<'a> Widget for Launcher<'a> {
self.tx.send(Cmd::Exit).unwrap(); self.tx.send(Cmd::Exit).unwrap();
} }
_ => { _ => {
self.cursor = 0;
if self.matches.len() > self.offset { if self.matches.len() > self.offset {
let d = &self.matches[self.offset]; let d = &self.matches[self.offset];
if let Some(exec) = &d.exec { if let Some(exec) = &d.exec {
@@ -420,9 +444,22 @@ impl<'a> Widget for Launcher<'a> {
self.dirty = true; self.dirty = true;
} }
} }
keysyms::XKB_KEY_Left => {
if self.cursor > 0 {
self.cursor -= 1;
self.dirty = true;
}
}
keysyms::XKB_KEY_Right => {
if self.cursor < self.input.len() {
self.cursor += 1;
self.dirty = true;
}
}
_ => { _ => {
if let Some(v) = interpreted { if let Some(v) = interpreted {
self.input += &v; self.input.insert_str(self.cursor, &v);
self.cursor += 1;
self.offset = 0; self.offset = 0;
self.result = None; self.result = None;
self.dirty = true; self.dirty = true;