calc: Improve history

This commit is contained in:
Kenny Levinsen
2023-02-06 18:57:34 +01:00
parent 6f0ca36826
commit b9f4e74062
2 changed files with 38 additions and 20 deletions
+1 -2
View File
@@ -119,11 +119,10 @@ fn main() {
let mut fm = FontMap::new(); let mut fm = FontMap::new();
// Todo: Cache in a file // TODO: Cache in a file
fm.add_font_path("sans", "/usr/share/fonts/noto/NotoSans-Regular.ttf".to_string()); fm.add_font_path("sans", "/usr/share/fonts/noto/NotoSans-Regular.ttf".to_string());
fm.add_font_path("monospace", "/usr/share/fonts/noto/NotoSansMono-Regular.ttf".to_string()); fm.add_font_path("monospace", "/usr/share/fonts/noto/NotoSansMono-Regular.ttf".to_string());
let (widgets, layout): (Vec<Box<dyn Widget>>, Rc<Box<dyn Layout>>) = match version { let (widgets, layout): (Vec<Box<dyn Widget>>, Rc<Box<dyn Layout>>) = match version {
UIVersion::V1 => { UIVersion::V1 => {
let clock = Box::new(Clock::new(&mut fm, "sans", 256.)); let clock = Box::new(Clock::new(&mut fm, "sans", 256.));
+37 -18
View File
@@ -103,6 +103,11 @@ impl Prompt {
} }
} }
fn set(&mut self, v: &str) {
self.input = v.to_string();
self.cursor = self.input.len();
}
fn append(&mut self, v: &str) { fn append(&mut self, v: &str) {
if self.input.len() == 0 { if self.input.len() == 0 {
match v { match v {
@@ -188,7 +193,7 @@ impl Prompt {
} }
trait InterfaceWidget { trait InterfaceWidget {
fn trigger(&mut self, intf: &InnerInterface); fn trigger(&mut self, intf: &mut InnerInterface);
fn draw( fn draw(
&mut self, &mut self,
intf: &InnerInterface, intf: &InnerInterface,
@@ -239,7 +244,7 @@ impl Launcher {
} }
impl InterfaceWidget for Launcher { impl InterfaceWidget for Launcher {
fn trigger(&mut self, intf: &InnerInterface) { fn trigger(&mut self, intf: &mut InnerInterface) {
if self.matches.len() > intf.selection { if self.matches.len() > intf.selection {
let d = &self.matches[intf.selection]; let d = &self.matches[intf.selection];
if let Some(exec) = &d.exec { if let Some(exec) = &d.exec {
@@ -362,7 +367,7 @@ impl InterfaceWidget for Launcher {
struct Shell(); struct Shell();
impl InterfaceWidget for Shell { impl InterfaceWidget for Shell {
fn trigger(&mut self, intf: &InnerInterface) { fn trigger(&mut self, intf: &mut InnerInterface) {
let args = vec![ let args = vec![
"/bin/sh".to_string(), "/bin/sh".to_string(),
"-c".to_string(), "-c".to_string(),
@@ -414,14 +419,21 @@ impl InterfaceWidget for Shell {
} }
struct Calc { struct Calc {
old: Vec<String>, old: Vec<(String, String)>,
result: Option<String>, result: Option<String>,
} }
impl InterfaceWidget for Calc { impl InterfaceWidget for Calc {
fn trigger(&mut self, intf: &InnerInterface) { fn trigger(&mut self, intf: &mut InnerInterface) {
if let Some(res) = &self.result { if intf.selection == 0 {
self.old.push(format!("{} = {}", &intf.prompt.input, res)); if let Some(res) = &self.result {
self.old.push((intf.prompt.input.to_string(), res.to_string()));
}
} else if self.old.len() >= intf.selection {
let (input, res) = &self.old[self.old.len() - intf.selection];
self.result = Some(res.to_string());
intf.prompt.set(input);
intf.selection = 0;
} }
} }
@@ -452,7 +464,7 @@ impl InterfaceWidget for Calc {
// Draw prompt // Draw prompt
let font = fonts.get_font(intf.font, intf.size); let font = fonts.get_font(intf.font, intf.size);
let res_off = font font
.auto_draw_text_with_cursor( .auto_draw_text_with_cursor(
&mut prompt_line, &mut prompt_line,
fg, fg,
@@ -464,27 +476,34 @@ impl InterfaceWidget for Calc {
font.auto_draw_text(&mut prompt_line, Color::BUFF, "=") font.auto_draw_text(&mut prompt_line, Color::BUFF, "=")
.unwrap(); .unwrap();
prompt_offset -= line_height;
if let Some(res) = &self.result { if let Some(res) = &self.result {
let resfg = Color::LIGHTORANGE; let c = if intf.selection == 0 {
let mut result_line = view.offset((res_off.0, prompt_offset)); Color::LIGHTORANGE
font.auto_draw_text(&mut result_line, resfg, &format!(" = {}", &res)) } else {
.unwrap(); Color::GREY50
};
let mut result_line = view.offset((0, prompt_offset));
font.auto_draw_text(&mut result_line, c, res).unwrap();
} }
for m in self.old.iter().rev() { for (idx, (input, res)) in self.old.iter().rev().enumerate() {
let dimfg = Color::GREY50;
if prompt_offset < line_height { if prompt_offset < line_height {
break; break;
} }
let c = if idx + 1 == intf.selection {
Color::GREY75
} else {
Color::GREY50
};
prompt_offset -= line_height; prompt_offset -= line_height;
let mut result_line = view.offset((0, prompt_offset)); let mut result_line = view.offset((0, prompt_offset));
font.auto_draw_text(&mut result_line, dimfg, &m) font.auto_draw_text(&mut result_line, c, &format!("{} = {}", input, res)).unwrap();
.unwrap();
} }
let content_height = min( let content_height = min(
intf.geometry.height, intf.geometry.height,
(self.old.len() + 1) as u32 * line_height + 16, (self.old.len() + 2) as u32 * line_height + 16,
); );
Geometry { Geometry {
x: intf.geometry.x, x: intf.geometry.x,
@@ -588,7 +607,7 @@ impl Interface {
self.inner.selection = 0; self.inner.selection = 0;
widget.update(&self.inner); widget.update(&self.inner);
} }
keysyms::XKB_KEY_Return => widget.trigger(&self.inner), keysyms::XKB_KEY_Return => widget.trigger(&mut self.inner),
keysyms::XKB_KEY_Left => self.inner.prompt.move_cursor(-1), keysyms::XKB_KEY_Left => self.inner.prompt.move_cursor(-1),
keysyms::XKB_KEY_Right => self.inner.prompt.move_cursor(1), keysyms::XKB_KEY_Right => self.inner.prompt.move_cursor(1),
keysyms::XKB_KEY_Up => { keysyms::XKB_KEY_Up => {