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:
electria 2026-08-28 12:28:45 -07:00
commit f4241b9711
Signed by: electria
SSH key fingerprint: SHA256:8LlB3ucPbBHqozqkhsNbaV5oG3SlzzqUj8FZDL6IPQs
4 changed files with 228 additions and 486 deletions

View file

@ -4,8 +4,8 @@ use std::{
path::{Path, PathBuf},
};
use iced::widget;
use image::{DynamicImage, ImageDecoder, ImageReader, ImageResult, RgbaImage};
use iced::{clipboard, widget};
use image::{DynamicImage, EncodableLayout, ImageDecoder, ImageReader, ImageResult, RgbaImage};
use rfd::AsyncFileDialog;
pub fn load_image(path: impl AsRef<Path>) -> Result<RgbaImage, String> {
@ -67,18 +67,18 @@ pub const fn string_from_filter_type(f: widget::image::FilterMethod) -> &'static
}
}
pub fn arboard_from_rgbaimage<'a>(image: &'a RgbaImage) -> arboard::ImageData<'a> {
arboard::ImageData {
width: image.width() as usize,
height: image.height() as usize,
bytes: Cow::Borrowed(image.as_ref()),
pub fn clipboard_image_from_rgbaimage(image: &RgbaImage) -> clipboard::Image {
clipboard::Image {
size: (image.width(), image.height()).into(),
// SAFETY: this is also probably a race condition,
// where if the user switches the image before it's done,
// then something will die.
//
// however, especially with larger images,
// copying them is just too expensive.
rgba: unsafe { std::mem::transmute::<&[u8], &'static [u8]>(image.as_bytes()) }.into(),
}
}
pub fn rgbaimage_from_arboard(image: arboard::ImageData) -> RgbaImage {
RgbaImage::from_raw(
image.width as u32,
image.height as u32,
image.bytes.into_owned(),
)
.unwrap()
pub fn rgbaimage_from_clipboard_image(image: clipboard::Image) -> RgbaImage {
RgbaImage::from_raw(image.size.width, image.size.height, image.rgba.into()).unwrap()
}