From 8f0b7d872d90770a5014afcd5b00f888231a2018 Mon Sep 17 00:00:00 2001 From: Dukantic Date: Sun, 13 Apr 2025 19:36:55 +0200 Subject: [PATCH] shortcut for saving and zoom is better by default rip --- src/gui.rs | 31 ++++++++++++++++++++++--------- src/read_and_write.rs | 4 ---- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/gui.rs b/src/gui.rs index 327e824..46d6a8e 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -1,4 +1,8 @@ -use eframe::egui::{self}; +use ::egui::os; +use eframe::{ + egui::{self}, + emath::Float, +}; use std::fs::File; use crate::read_and_write; @@ -8,6 +12,7 @@ pub struct Omelt { file_name: String, text: String, file: File, + save_short: KeyboardShortcut, } impl Omelt { @@ -16,6 +21,16 @@ impl Omelt { file_name: file_name.to_owned(), text: "".to_owned(), file: file, + save_short: KeyboardShortcut::new( + egui::Key::S, + Modifiers { + alt: false, + ctrl: true, + shift: false, + mac_cmd: false, + command: true, + }, + ), }; out.update_txt(); out @@ -31,21 +46,16 @@ impl Omelt { impl eframe::App for Omelt { fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { - let save = KeyboardShortcut::new(egui::Key::S, Modifiers::CTRL); - ctx.set_zoom_factor(2.0); ctx.input(|i| { - //eprintln!("i = {:?}", i); - if save.matches(i) { + if self.save_short.matches(i) { let _ = save_action(&self.text, &mut self.file); - println!("save"); } }); egui::CentralPanel::default().show(ctx, |ui| { ui.heading(&self.file_name); - if ui.add(egui::Button::new("Click me")).clicked() { + if ui.add(egui::Button::new("Save")).clicked() { let _ = self.file.set_len(0); let _ = save_action(&self.text, &mut self.file); - eprintln!("self.text = {:?}", self.text); } egui::ScrollArea::vertical().show(ui, |ui| { //let visible_text = self.text.lines().take(100).collect::>().join("\n"); @@ -57,6 +67,9 @@ impl eframe::App for Omelt { }); } } + +//---------------------ShortCuts----------------------- + use egui::Modifiers; #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -71,6 +84,6 @@ impl KeyboardShortcut { } fn matches(&self, input: &egui::InputState) -> bool { - input.key_down(self.key) && input.modifiers == self.modifiers + input.key_pressed(self.key) && input.modifiers == self.modifiers } } diff --git a/src/read_and_write.rs b/src/read_and_write.rs index 432f787..3658d09 100644 --- a/src/read_and_write.rs +++ b/src/read_and_write.rs @@ -44,13 +44,9 @@ pub fn save_action(txt: &str, file: &mut File) -> Result<(), std::io::Error> { file.write_all(string.as_bytes())?; let map = tree_to_map(&mut tree, true); - eprintln!("&txt = {:?}", &txt); let trad = body_to_bin(&txt, map); - eprintln!("trad = {:?}", trad); let trad_bin = string_to_binary(&trad); - eprintln!("trad_bin = {:?}", trad_bin); let len_body = trad.len() as u32; - eprintln!("len_body = {:?}", len_body); file.write_all(&len_body.to_be_bytes())?; file.write_all(&trad_bin)?; Ok(())