refactor: use iced 0.15 rich clipboard
it does use arboard internally, but it's nicer to use stuff built into iced instead of dealing with the Clipboard object ourselves.
This commit is contained in:
parent
95a24fa987
commit
f4241b9711
4 changed files with 228 additions and 486 deletions
56
src/main.rs
56
src/main.rs
|
|
@ -6,9 +6,8 @@ use std::{
|
|||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use arboard::Clipboard;
|
||||
use iced::{
|
||||
Alignment, Color, Element, Length, Renderer, Subscription, Task, Theme, event,
|
||||
Alignment, Color, Element, Length, Renderer, Subscription, Task, Theme, clipboard, event,
|
||||
keyboard::{self, Key, key},
|
||||
theme, widget, window,
|
||||
};
|
||||
|
|
@ -29,6 +28,8 @@ enum Message {
|
|||
|
||||
Yank,
|
||||
Put,
|
||||
ImageYanked(Result<(), clipboard::Error>),
|
||||
ImagePut(Result<clipboard::Image, clipboard::Error>),
|
||||
|
||||
FocusNext,
|
||||
FocusPrevious,
|
||||
|
|
@ -49,8 +50,6 @@ struct State {
|
|||
error: Option<Cow<'static, str>>,
|
||||
info: Option<String>,
|
||||
infobar_shown: bool,
|
||||
|
||||
clipboard: Option<Clipboard>,
|
||||
}
|
||||
impl State {
|
||||
fn new() -> (Self, Task<Message>) {
|
||||
|
|
@ -177,44 +176,27 @@ impl State {
|
|||
|
||||
Message::Yank => {
|
||||
if let Some(image) = self.image.as_ref() {
|
||||
match match self.clipboard.take() {
|
||||
Some(c) => Ok(c),
|
||||
None => Clipboard::new(),
|
||||
} {
|
||||
Ok(mut clipboard) => {
|
||||
self.error = clipboard
|
||||
.set_image(utils::arboard_from_rgbaimage(image))
|
||||
.map_err(|e| e.to_string().into())
|
||||
.err();
|
||||
self.clipboard = Some(clipboard);
|
||||
}
|
||||
Err(e) => self.error = Some(e.to_string().into()),
|
||||
}
|
||||
return clipboard::write(utils::clipboard_image_from_rgbaimage(&image))
|
||||
.map(Message::ImageYanked);
|
||||
} else {
|
||||
self.error = Some("no image to yank".into());
|
||||
}
|
||||
}
|
||||
Message::Put => {
|
||||
match match self.clipboard.take() {
|
||||
Some(c) => Ok(c),
|
||||
None => Clipboard::new(),
|
||||
} {
|
||||
Ok(mut clipboard) => {
|
||||
let result = clipboard.get_image().map(utils::rgbaimage_from_arboard);
|
||||
Message::Put => return clipboard::read_image().map(Message::ImagePut),
|
||||
|
||||
self.error = result.as_ref().map_err(|e| e.to_string().into()).err();
|
||||
self.clipboard = Some(clipboard);
|
||||
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());
|
||||
|
||||
if let Ok(image) = result {
|
||||
self.info = Some(format!("{}x{}", image.width(), image.height(),));
|
||||
if image.dimensions() < (100, 100) {
|
||||
self.image_filter = widget::image::FilterMethod::Nearest;
|
||||
}
|
||||
self.image = Some(image);
|
||||
return self.allocate_image();
|
||||
}
|
||||
if let Ok(image) = result {
|
||||
self.info = Some(format!("{}x{}", image.size.width, image.size.height,));
|
||||
if (image.size.width, image.size.height) < (100, 100) {
|
||||
self.image_filter = widget::image::FilterMethod::Nearest;
|
||||
}
|
||||
Err(e) => self.error = Some(e.to_string().into()),
|
||||
self.image = Some(utils::rgbaimage_from_clipboard_image(image));
|
||||
return self.allocate_image();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -333,10 +315,10 @@ fn main() -> Result<(), iced::Error> {
|
|||
.title(State::title)
|
||||
.theme(Theme::custom(
|
||||
"custom",
|
||||
theme::Palette {
|
||||
theme::palette::Seed {
|
||||
background: Color::BLACK,
|
||||
text: Color::WHITE,
|
||||
..theme::Palette::DARK
|
||||
..theme::palette::Seed::DARK
|
||||
},
|
||||
))
|
||||
.run()
|
||||
|
|
|
|||
Loading…
Reference in a new issue