feat: saving image

dialog is missing!!
This commit is contained in:
electria 2026-06-29 08:46:45 -07:00
commit 68964f24fd
Signed by: electria
SSH key fingerprint: SHA256:8LlB3ucPbBHqozqkhsNbaV5oG3SlzzqUj8FZDL6IPQs
3 changed files with 71 additions and 2 deletions

View file

@ -1,11 +1,14 @@
use std::env;
use std::{env, ffi::OsStr, fs};
use iced::{
Color, Element, Renderer, Subscription, Task, Theme, color, event,
keyboard::{self, Key, key},
theme, widget, window,
};
use image::{EncodableLayout, ImageReader, Pixel, Rgba, RgbaImage, imageops};
use image::{
EncodableLayout, ImageReader, Pixel, Rgba, RgbaImage, codecs::webp::WebPEncoder, imageops,
};
use rfd::FileDialog;
#[derive(Clone, Debug)]
enum Message {
@ -85,6 +88,35 @@ impl State {
return self.allocate_image();
}
Key::Character("s") => {
let Some(path) = FileDialog::new().save_file() else {
eprintln!("no path to save provided");
return Task::none();
};
let Ok(file) = fs::File::create(&path)
.inspect_err(|e| eprintln!("failed to create file: {e}"))
else {
return Task::none();
};
let maybe_encoder =
match path.extension().map(OsStr::to_string_lossy).as_deref() {
Some("webp") => Some(WebPEncoder::new_lossless(file)),
None | Some(_) => None,
};
let result = if let Some(encoder) = maybe_encoder {
self.image.write_with_encoder(encoder)
} else {
self.image.save(path)
};
#[allow(unused_must_use)]
result.inspect_err(|e| eprintln!("failed to save image: {e}"));
}
// ignore unused keys
_ => {}
},