feat: "x" to copy image_path

This commit is contained in:
electria 2026-11-03 13:04:16 -08:00
commit 8288a0a287
Signed by: electria
SSH key fingerprint: SHA256:8LlB3ucPbBHqozqkhsNbaV5oG3SlzzqUj8FZDL6IPQs
2 changed files with 31 additions and 14 deletions

View file

@ -1,10 +1,4 @@
use std::{ use std::{borrow::Cow, env, ffi::OsStr, os::unix::fs::MetadataExt, path::PathBuf};
borrow::Cow,
env,
ffi::OsStr,
os::unix::fs::MetadataExt,
path::{Path, PathBuf},
};
use iced::{ use iced::{
Alignment, Color, Length, Renderer, Subscription, Task, Theme, Widget, clipboard, event, Alignment, Color, Length, Renderer, Subscription, Task, Theme, Widget, clipboard, event,
@ -31,6 +25,9 @@ enum Message {
ImageYanked(Result<(), clipboard::Error>), ImageYanked(Result<(), clipboard::Error>),
ImagePut(Result<clipboard::Image, clipboard::Error>), ImagePut(Result<clipboard::Image, clipboard::Error>),
YankPath,
PathYanked(Result<(), clipboard::Error>),
FocusNext, FocusNext,
FocusPrevious, FocusPrevious,
@ -46,6 +43,7 @@ struct State {
image: Option<RgbaImage>, image: Option<RgbaImage>,
image_display: Option<widget::image::Allocation>, image_display: Option<widget::image::Allocation>,
image_filter: widget::image::FilterMethod, image_filter: widget::image::FilterMethod,
image_path: Option<PathBuf>,
error: Option<Cow<'static, str>>, error: Option<Cow<'static, str>>,
info: Option<String>, info: Option<String>,
@ -176,9 +174,6 @@ impl State {
} }
Message::Put => return clipboard::read_image().map(Message::ImagePut), Message::Put => return clipboard::read_image().map(Message::ImagePut),
Message::ImageYanked(result) => {
self.error = result.err().map(|e| format!("{e:?}").into());
}
Message::ImagePut(result) => { Message::ImagePut(result) => {
self.error = result.as_ref().err().map(|e| format!("{e:?}").into()); self.error = result.as_ref().err().map(|e| format!("{e:?}").into());
@ -192,6 +187,21 @@ impl State {
} }
} }
Message::YankPath => {
if let Some(path) = self.image_path.as_ref() {
return clipboard::write(clipboard::Content::Text(
path.to_string_lossy().to_string(),
))
.map(Message::PathYanked);
} else {
self.error = Some("no path to yank".into());
}
}
Message::ImageYanked(result) | Message::PathYanked(result) => {
self.error = result.err().map(|e| format!("{e:?}").into());
}
Message::FileDropped(path) => match self.load_image(path) { Message::FileDropped(path) => match self.load_image(path) {
Ok(task) => { Ok(task) => {
self.error = None; self.error = None;
@ -204,14 +214,14 @@ impl State {
Task::none() Task::none()
} }
fn load_image(&mut self, path: impl AsRef<Path>) -> Result<Task<Message>, String> { fn load_image(&mut self, path: impl Into<PathBuf>) -> Result<Task<Message>, String> {
let path = path.into();
utils::load_image(&path).map(|image| { utils::load_image(&path).map(|image| {
self.info = Some(format!( self.info = Some(format!(
"{} {} {}x{}", "{} {} {}x{}",
path.as_ref() path.file_name()
.file_name()
.map_or(Cow::Borrowed("[no file]"), OsStr::to_string_lossy), .map_or(Cow::Borrowed("[no file]"), OsStr::to_string_lossy),
path.as_ref().metadata().map_or_else( path.metadata().map_or_else(
|e| { |e| {
eprintln!("failed to read metadata: {e}"); eprintln!("failed to read metadata: {e}");
Cow::Borrowed("[no size]") Cow::Borrowed("[no size]")
@ -221,9 +231,13 @@ impl State {
image.width(), image.width(),
image.height(), image.height(),
)); ));
self.image_path = Some(path);
if image.dimensions() < (192, 192) { if image.dimensions() < (192, 192) {
self.image_filter = widget::image::FilterMethod::Nearest; self.image_filter = widget::image::FilterMethod::Nearest;
} }
self.image = Some(image); self.image = Some(image);
self.allocate_image() self.allocate_image()
}) })
@ -275,6 +289,7 @@ impl State {
Key::Character("y" | "c") => Some(Message::Yank), Key::Character("y" | "c") => Some(Message::Yank),
Key::Character("p" | "v") => Some(Message::Put), Key::Character("p" | "v") => Some(Message::Put),
Key::Character("x") => Some(Message::YankPath),
_ => None, _ => None,
} }

View file

@ -8,3 +8,5 @@
'y' or 'c' to yank 'y' or 'c' to yank
'p' or 'v' to put 'p' or 'v' to put
'x' to yank the path