Compare commits

...
Author SHA1 Message Date
5d5756121a
feat: make pickers async 2026-07-31 15:11:41 -07:00
9d4e24e809
refactor: apply clippy lints
cargo clippy --no-deps -- --deny clippy::nursery --deny clippy::pedantic
(does not quite pass due to unused `&self` in State::subscription)
I am considering ignoring events when the file picker is opened,
but that isn't a very nice solution (should just make it async atp)
2026-07-31 14:39:18 -07:00
94c905db38
build: seperate linux-specific deps 2026-07-31 14:19:53 -07:00
3 changed files with 109 additions and 96 deletions

View file

@ -19,22 +19,25 @@
cargoToml = fromTOML (builtins.readFile ./Cargo.toml);
name = cargoToml.package.name;
dlDeps = with pkgs; [
# libdbus, for rfd
dbus.lib
dlDeps =
with pkgs;
[
# needed for both x11 and wayland
libxkbcommon
libGL
# needed for both x11 and wayland
libxkbcommon
libGL
vulkan-loader
libx11
libxcursor
libxi
libxcb
]
++ lib.optionals stdenv.hostPlatform.isLinux [
# libdbus, for rfd
dbus.lib
wayland
libx11
libxcursor
libxi
libxcb
];
vulkan-loader
wayland
];
commonArgs = {
# all that's needed for artifacts and checks

View file

@ -1,4 +1,9 @@
use std::{borrow::Cow, env, ffi::OsStr, path::Path};
use std::{
borrow::Cow,
env,
ffi::OsStr,
path::{Path, PathBuf},
};
use iced::{
Alignment, Color, Element, Length, Renderer, Subscription, Task, Theme, event,
@ -12,7 +17,9 @@ mod utils;
#[derive(Clone, Debug)]
enum Message {
ImagePicked(Result<PathBuf, String>),
ImageDisplayReady(Result<widget::image::Allocation, widget::image::Error>),
SavePathPicked(Result<PathBuf, String>),
Event(iced::Event),
}
@ -29,7 +36,7 @@ struct State {
}
impl State {
fn new() -> (Self, Task<Message>) {
let mut state = State::default();
let mut state = Self::default();
if let Some(path) = env::args().nth(1) {
match state.load_image(path) {
@ -43,22 +50,25 @@ impl State {
fn view(&self) -> Element<'_, Message, Theme, Renderer> {
let mut main = Vec::new();
main.push(if let Some(allocation) = self.image_display.as_ref() {
widget::image::viewer(allocation.handle().clone())
.filter_method(self.image_filter)
.max_scale(50.)
.min_scale(1.)
.width(Length::Fill)
.height(Length::Fill)
.into()
} else {
widget::container(widget::text(include_str!("usage.txt")))
.height(Length::Fill)
.width(Length::Fill)
.align_x(Alignment::Center)
.align_y(Alignment::Center)
.into()
});
main.push(self.image_display.as_ref().map_or_else(
|| {
widget::container(widget::text(include_str!("usage.txt")))
.height(Length::Fill)
.width(Length::Fill)
.align_x(Alignment::Center)
.align_y(Alignment::Center)
.into()
},
|allocation| {
widget::image::viewer(allocation.handle().clone())
.filter_method(self.image_filter)
.max_scale(50.)
.min_scale(1.)
.width(Length::Fill)
.height(Length::Fill)
.into()
},
));
if let Some(error) = self.error.as_ref() {
main.push(widget::text(error).style(widget::text::danger).into());
@ -71,9 +81,26 @@ impl State {
}
fn update(&mut self, message: Message) -> Task<Message> {
match message {
Message::ImagePicked(result) => match result.and_then(|path| self.load_image(path)) {
Err(e) => self.error = Some(e),
Ok(task) => {
self.error = None;
return task;
}
},
Message::ImageDisplayReady(result) => {
self.image_display = Some(result.unwrap());
}
Message::SavePathPicked(result) => {
self.error = result
.and_then(|path| {
self.image.as_ref().map_or_else(
|| Err("no image to save".into()),
|image| image.save(path).map_err(|e| e.to_string()),
)
})
.err();
}
Message::Event(iced::Event::Keyboard(keyboard::Event::KeyPressed {
key,
@ -84,39 +111,30 @@ impl State {
Key::Named(key::Named::Tab) => {
if modifiers.shift() {
return widget::operation::focus_previous();
} else {
return widget::operation::focus_next();
}
return widget::operation::focus_next();
}
Key::Character("o") => match self.pick_and_load_image() {
Ok(task) => {
Key::Character("o") => {
return Task::perform(utils::pick_image(), Message::ImagePicked);
}
Key::Character("r") => match self.image.as_ref() {
None => self.error = Some("no image to rotate".into()),
Some(image) => {
self.error = None;
return task;
self.image = Some(imageops::rotate90(image));
return self.allocate_image();
}
},
Key::Character("i") => match self.image.as_mut() {
None => self.error = Some("no image to invert".into()),
Some(image) => {
self.error = None;
imageops::invert(image);
return self.allocate_image();
}
Err(e) => self.error = Some(e),
},
Key::Character("r") => {
match self.image.as_ref() {
None => self.error = Some("no image to rotate".into()),
Some(image) => {
self.error = None;
self.image = Some(imageops::rotate90(image));
return self.allocate_image();
}
};
}
Key::Character("i") => {
match self.image.as_mut() {
None => self.error = Some("no image to invert".into()),
Some(image) => {
self.error = None;
imageops::invert(image);
return self.allocate_image();
}
};
}
Key::Character("f") => {
self.image_filter = match self.image_filter {
widget::image::FilterMethod::Linear => widget::image::FilterMethod::Nearest,
@ -128,7 +146,12 @@ impl State {
}
Key::Character("s") => {
self.save_image();
if self.image.is_some() {
self.error = None;
return Task::perform(utils::pick_save_path(), Message::SavePathPicked);
} else {
self.error = Some("no image to save".into())
}
}
Key::Character("q") => return window::latest().and_then(window::close),
@ -160,8 +183,7 @@ impl State {
"{} {}x{}",
path.as_ref()
.file_name()
.map(OsStr::to_string_lossy)
.unwrap_or(Cow::Borrowed("[no file]")),
.map_or(Cow::Borrowed("[no file]"), OsStr::to_string_lossy),
image.width(),
image.height(),
));
@ -172,13 +194,6 @@ impl State {
self.allocate_image()
})
}
fn pick_and_load_image(&mut self) -> Result<Task<Message>, String> {
utils::pick_image().and_then(|path| self.load_image(path))
}
fn save_image(&mut self) {
self.error = utils::save_image(self.image.as_ref()).err();
}
fn allocate_image(&self) -> Task<Message> {
let Some(image) = self.image.as_ref() else {
@ -210,13 +225,15 @@ impl State {
event::listen().map(Message::Event)
}
fn title(&self) -> String {
match self.info.as_ref() {
Some(info) => format!(
"imagey {info} {}",
utils::string_from_filter_type(self.image_filter),
),
None => "imagey".into(),
}
self.info.as_ref().map_or_else(
|| "imagey".into(),
|info| {
format!(
"imagey {info} {}",
utils::string_from_filter_type(self.image_filter),
)
},
)
}
}

View file

@ -2,12 +2,12 @@ use std::path::{Path, PathBuf};
use iced::widget;
use image::{DynamicImage, ImageDecoder, ImageReader, ImageResult, RgbaImage};
use rfd::FileDialog;
use rfd::AsyncFileDialog;
pub fn load_image(path: impl AsRef<Path>) -> Result<RgbaImage, String> {
_load_image(path).map_err(|e| e.to_string())
load_image_impl(path).map_err(|e| e.to_string())
}
fn _load_image(path: impl AsRef<Path>) -> ImageResult<RgbaImage> {
fn load_image_impl(path: impl AsRef<Path>) -> ImageResult<RgbaImage> {
let mut decoder = ImageReader::open(path)?
.with_guessed_format()?
.into_decoder()?;
@ -20,8 +20,8 @@ fn _load_image(path: impl AsRef<Path>) -> ImageResult<RgbaImage> {
Ok(decoded_image.into_rgba8())
}
pub fn pick_image() -> Result<PathBuf, String> {
let Some(path) = FileDialog::new()
pub async fn pick_image() -> Result<PathBuf, String> {
let Some(filehandle) = AsyncFileDialog::new()
.add_filter(
"image",
&[
@ -30,30 +30,23 @@ pub fn pick_image() -> Result<PathBuf, String> {
],
)
.pick_file()
.await
else {
return Err("no path to open provided".into());
};
Ok(path)
Ok(filehandle.path().to_owned())
}
pub fn save_image(image: Option<&RgbaImage>) -> Result<(), String> {
let Some(image) = image else {
return Err("no image to save".into());
};
let Some(path) = FileDialog::new().save_file() else {
pub async fn pick_save_path() -> Result<PathBuf, String> {
let Some(filehandle) = AsyncFileDialog::new().save_file().await else {
return Err("no path to save provided".into());
};
if let Err(e) = image.save(&path) {
return Err(e.to_string());
};
Ok(())
Ok(filehandle.path().to_owned())
}
pub fn string_from_filter_type(f: widget::image::FilterMethod) -> &'static str {
pub const fn string_from_filter_type(f: widget::image::FilterMethod) -> &'static str {
match f {
widget::image::FilterMethod::Linear => "bilinear",
widget::image::FilterMethod::Nearest => "nearest neighbor",