feat: saving image
dialog is missing!!
This commit is contained in:
parent
ef42175744
commit
68964f24fd
3 changed files with 71 additions and 2 deletions
36
src/main.rs
36
src/main.rs
|
|
@ -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
|
||||
_ => {}
|
||||
},
|
||||
|
|
|
|||
Loading…
Reference in a new issue