diff --git a/src/app.rs b/src/app.rs index 41b13db..c1737f5 100644 --- a/src/app.rs +++ b/src/app.rs @@ -26,6 +26,28 @@ use std::sync::mpsc::{channel, Receiver, Sender}; use std::sync::Arc; use std::time::{Duration, Instant, SystemTime}; +/// A file error as a person would say it, for the end of a "Couldn't …" message. +pub fn why(e: &std::io::Error) -> String { + use std::io::ErrorKind::*; + match e.kind() { + NotFound => "it isn't there".into(), + PermissionDenied => "you don't have permission".into(), + AlreadyExists => "something with that name is already there".into(), + IsADirectory => "it's a folder".into(), + NotADirectory => "part of that path is a file, not a folder".into(), + DirectoryNotEmpty => "the folder isn't empty".into(), + StorageFull => "the disk is full".into(), + ReadOnlyFilesystem => "the drive is read-only".into(), + _ => { + // the system's wording, without "(os error 2)" and with a small first letter + let s = e.to_string(); + let s = s.split(" (os error").next().unwrap_or(&s).trim().to_string(); + let mut c = s.chars(); + c.next().map_or(s.clone(), |f| f.to_lowercase().chain(c).collect()) + } + } +} + pub enum Msg { Search(Vec<(PathBuf, usize, String)>), Git(Box), @@ -295,7 +317,7 @@ impl App { if self.root.is_none() { self.adopt_root(&path) } self.add_doc(ctx, d); } - Err(e) => return self.toast("error", format!("Couldn't open {}: {e}", path.display())), + Err(e) => return self.toast("error", format!("Couldn't open {}: {}", path.display(), why(&e))), } } } @@ -414,7 +436,7 @@ impl App { let name = d.title(); self.dialog = Some(Dialog::Sudo { id, name, password: String::new(), error: None, busy: false }); } - Err(e) => self.toast("error", format!("Couldn't save: {e}")), + Err(e) => self.toast("error", format!("Couldn't save: {}", why(&e))), } } @@ -458,20 +480,20 @@ impl App { self.side_state.dirs.clear(); self.toast("delete", "Moved to the trash".to_string()); } else if let Err(e) = done { - self.toast("error", format!("Couldn't move it to the trash: {e}")); + self.toast("error", format!("Couldn't move it to the trash: {}", why(&e))); } } pub fn prompt_done(&mut self, ctx: &egui::Context, act: Act, value: String) { if value.trim().is_empty() { return } - let res = match act { + let (what, res) = match act { Act::NewFile(dir) => { let p = dir.join(&value); let r = std::fs::write(&p, ""); if r.is_ok() { self.open_path(ctx, p, None) } - r + ("create the file", r) } - Act::NewFolder(dir) => std::fs::create_dir_all(dir.join(&value)), + Act::NewFolder(dir) => ("create the folder", std::fs::create_dir_all(dir.join(&value))), Act::Rename(p) => { let to = p.with_file_name(&value); let r = std::fs::rename(&p, &to); @@ -480,11 +502,11 @@ impl App { if d.path.as_ref() == Some(&p) { d.path = Some(to.clone()) } } } - r + ("rename it", r) } - _ => Ok(()), + _ => ("", Ok(())), }; - if let Err(e) = res { self.toast("error", e.to_string()) } + if let Err(e) = res { self.toast("error", format!("Couldn't {what}: {}", why(&e))) } self.side_state.dirs.clear(); } @@ -1105,7 +1127,7 @@ impl App { fn reload_current(&mut self) { let Some(id) = self.cur_doc_id() else { return }; if let Err(e) = self.docs.get_mut(&id).unwrap().reload() { - self.toast("error", format!("Couldn't reload: {e}")); + self.toast("error", format!("Couldn't reload: {}", why(&e))); } } diff --git a/src/ui/terminal.rs b/src/ui/terminal.rs index f847db8..cae6635 100644 --- a/src/ui/terminal.rs +++ b/src/ui/terminal.rs @@ -261,7 +261,7 @@ impl App { self.terms.push(s); self.term_cur = self.terms.len() - 1; } - Err(e) => self.toast("error", format!("Terminal failed to start: {e}")), + Err(e) => self.toast("error", format!("Couldn't start the terminal: {}", crate::app::why(&e))), } } diff --git a/src/ui/widgets.rs b/src/ui/widgets.rs index 282cfe1..777386b 100644 --- a/src/ui/widgets.rs +++ b/src/ui/widgets.rs @@ -96,7 +96,7 @@ pub fn pill(ui: &mut Ui, t: &Theme, rect: Rect, key: &str, ic: &str, text: &str, /// M3 switch; the thumb swells while pressed and grows when on. pub fn switch(ui: &mut Ui, t: &Theme, rect: Rect, key: &str, on: bool) -> Response { let r = ui.interact(rect, ui.scope_id().with(key), Sense::click()); - let (_, press) = states(key, &r); + let (hov, press) = states(key, &r); let v = spring(format!("{key}/v"), on as u8 as f32, FAST); let track = Rect::from_center_size(rect.center(), vec2(50.0, 30.0)); let p = ui.painter(); @@ -107,6 +107,11 @@ pub fn switch(ui: &mut Ui, t: &Theme, rect: Rect, key: &str, on: bool) -> Respon } let x = lerp(track.left() + 15.0, track.right() - 15.0, v); let d = lerp(16.0, 24.0, c) + 4.0 * press; + // the state layer: a halo round the thumb on hover, deeper while pressed + let halo = 0.08 * hov + 0.08 * press.clamp(0.0, 1.0); + if halo > 0.005 { + p.circle_filled(pos2(x, track.center().y), 20.0, alpha(mix(t.on_surface, t.primary, c), halo)); + } p.circle_filled(pos2(x, track.center().y), d / 2.0, mix(t.outline, t.on_primary, c)); r } @@ -118,15 +123,26 @@ pub fn segmented(ui: &mut Ui, t: &Theme, rect: Rect, key: &str, opts: &[(&str, & p.rect_filled(rect, cr(rect.height() / 2.0), t.highest); let idx = opts.iter().position(|o| o.0 == cur).unwrap_or(0); let x = spring(format!("{key}/x"), idx as f32, FAST); - let sel = Rect::from_min_size(pos2(rect.left() + x * w, rect.top()), vec2(w, rect.height())).shrink(3.0); - p.rect_filled(sel, cr(sel.height() / 2.0), t.primary_container); + let mut sel = Rect::from_min_size(pos2(rect.left() + x * w, rect.top()), vec2(w, rect.height())).shrink(3.0); let mut out = None; - for (i, (v, label)) in opts.iter().enumerate() { + let mut fills = vec![]; + for (i, (v, _)) in opts.iter().enumerate() { let r = Rect::from_min_size(pos2(rect.left() + i as f32 * w, rect.top()), vec2(w, rect.height())); let resp = ui.interact(r, ui.scope_id().with(key).with(i), Sense::click()); if resp.clicked() { out = Some(v.to_string()); } + // hover tints a segment; pressing the chosen one squeezes its fill like the other buttons + let (hov, press) = states(&format!("{key}/{i}"), &resp); + if i == idx { sel = scale_rect(sel, 1.0 - 0.05 * press) } + if hov > 0.01 { fills.push((scale_rect(r.shrink(3.0), 1.0 - 0.05 * press), hov)) } + } + for (f, hov) in fills { + p.rect_filled(f, cr(f.height() / 2.0), alpha(t.on_surface, 0.07 * hov)); + } + p.rect_filled(sel, cr(sel.height() / 2.0), t.primary_container); + for (i, (_, label)) in opts.iter().enumerate() { + let r = Rect::from_min_size(pos2(rect.left() + i as f32 * w, rect.top()), vec2(w, rect.height())); let col = mix(t.on_variant, t.on_primary_container, 1.0 - (x - i as f32).abs().min(1.0)); p.text(r.center(), Align2::CENTER_CENTER, *label, fonts::med(13.0), col); } diff --git a/tests/features.rs b/tests/features.rs index f4c2b39..bdd9960 100644 --- a/tests/features.rs +++ b/tests/features.rs @@ -779,3 +779,28 @@ fn large_file_stays_usable() { assert!(h.state().find.hits.len() >= 50_000); std::fs::remove_dir_all(dir).ok(); } + +#[test] +fn file_errors_read_plainly() { + use damson::app::why; + use std::io::{Error, ErrorKind}; + assert_eq!(why(&std::fs::read("/no/such/file").unwrap_err()), "it isn't there"); + assert_eq!(why(&Error::from(ErrorKind::PermissionDenied)), "you don't have permission"); + assert!(!why(&Error::from_raw_os_error(5)).contains("os error")); +} + +/// Settings with the pointer on a segmented button and a switch: `DAMSON_SHOTS=dir`, and +/// `HOVER=x,y` to put the pointer somewhere. +#[test] +fn settings_controls_shot() { + let Ok(out) = std::env::var("DAMSON_SHOTS") else { return }; + tmp("settings"); + let mut h = app(vec![]); + h.state_mut().settings_open = true; + settle(&mut h, 30); + if let Some((x, y)) = std::env::var("HOVER").ok().and_then(|v| v.split_once(',').map(|(a, b)| (a.parse().unwrap(), b.parse().unwrap()))) { + h.input_mut().events.push(Event::PointerMoved(eframe::egui::pos2(x, y))); + settle(&mut h, 20); + } + h.render().unwrap().save(format!("{out}/settings.png")).unwrap(); +}