feat: "x" to copy image_path
This commit is contained in:
parent
7c271f8be9
commit
8288a0a287
2 changed files with 31 additions and 14 deletions
43
src/main.rs
43
src/main.rs
|
|
@ -1,10 +1,4 @@
|
|||
use std::{
|
||||
borrow::Cow,
|
||||
env,
|
||||
ffi::OsStr,
|
||||
os::unix::fs::MetadataExt,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
use std::{borrow::Cow, env, ffi::OsStr, os::unix::fs::MetadataExt, path::PathBuf};
|
||||
|
||||
use iced::{
|
||||
Alignment, Color, Length, Renderer, Subscription, Task, Theme, Widget, clipboard, event,
|
||||
|
|
@ -31,6 +25,9 @@ enum Message {
|
|||
ImageYanked(Result<(), clipboard::Error>),
|
||||
ImagePut(Result<clipboard::Image, clipboard::Error>),
|
||||
|
||||
YankPath,
|
||||
PathYanked(Result<(), clipboard::Error>),
|
||||
|
||||
FocusNext,
|
||||
FocusPrevious,
|
||||
|
||||
|
|
@ -46,6 +43,7 @@ struct State {
|
|||
image: Option<RgbaImage>,
|
||||
image_display: Option<widget::image::Allocation>,
|
||||
image_filter: widget::image::FilterMethod,
|
||||
image_path: Option<PathBuf>,
|
||||
|
||||
error: Option<Cow<'static, str>>,
|
||||
info: Option<String>,
|
||||
|
|
@ -176,9 +174,6 @@ impl State {
|
|||
}
|
||||
Message::Put => return clipboard::read_image().map(Message::ImagePut),
|
||||
|
||||
Message::ImageYanked(result) => {
|
||||
self.error = result.err().map(|e| format!("{e:?}").into());
|
||||
}
|
||||
Message::ImagePut(result) => {
|
||||
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) {
|
||||
Ok(task) => {
|
||||
self.error = None;
|
||||
|
|
@ -204,14 +214,14 @@ impl State {
|
|||
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| {
|
||||
self.info = Some(format!(
|
||||
"{} {} {}x{}",
|
||||
path.as_ref()
|
||||
.file_name()
|
||||
path.file_name()
|
||||
.map_or(Cow::Borrowed("[no file]"), OsStr::to_string_lossy),
|
||||
path.as_ref().metadata().map_or_else(
|
||||
path.metadata().map_or_else(
|
||||
|e| {
|
||||
eprintln!("failed to read metadata: {e}");
|
||||
Cow::Borrowed("[no size]")
|
||||
|
|
@ -221,9 +231,13 @@ impl State {
|
|||
image.width(),
|
||||
image.height(),
|
||||
));
|
||||
|
||||
self.image_path = Some(path);
|
||||
|
||||
if image.dimensions() < (192, 192) {
|
||||
self.image_filter = widget::image::FilterMethod::Nearest;
|
||||
}
|
||||
|
||||
self.image = Some(image);
|
||||
self.allocate_image()
|
||||
})
|
||||
|
|
@ -275,6 +289,7 @@ impl State {
|
|||
|
||||
Key::Character("y" | "c") => Some(Message::Yank),
|
||||
Key::Character("p" | "v") => Some(Message::Put),
|
||||
Key::Character("x") => Some(Message::YankPath),
|
||||
|
||||
_ => None,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,3 +8,5 @@
|
|||
|
||||
'y' or 'c' to yank
|
||||
'p' or 'v' to put
|
||||
|
||||
'x' to yank the path
|
||||
|
|
|
|||
Loading…
Reference in a new issue