Compare commits

..
2 changed files with 38 additions and 73 deletions

View file

@ -1,7 +1,7 @@
use std::{borrow::Cow, env, ffi::OsStr, path::Path};
use std::{env, path::Path};
use iced::{
Alignment, Color, Element, Length, Renderer, Subscription, Task, Theme, event,
Alignment, Color, Element, Length, Renderer, Subscription, Task, Theme, color, event,
keyboard::{self, Key, key},
theme, widget, window,
};
@ -20,33 +20,26 @@ enum Message {
struct State {
image: Option<RgbaImage>,
image_display: Option<widget::image::Allocation>,
image_filter: widget::image::FilterMethod,
error: Option<String>,
info: Option<String>,
infobar_shown: bool,
}
impl State {
fn new() -> (Self, Task<Message>) {
let mut state = State::default();
if let Some(path) = env::args().nth(1) {
match state.load_image(path) {
Err(e) => state.error = Some(e),
Ok(task) => return (state, task),
}
}
state.error = env::args().nth(1).and_then(|path| {
utils::load_image(path)
.map(|image| state.image = Some(image))
.err()
});
let allocate_image = state.allocate_image();
(state, Task::none())
(state, allocate_image)
}
fn view(&self) -> Element<'_, Message, Theme, Renderer> {
let mut main = Vec::new();
main.push(if let Some(allocation) = self.image_display.as_ref() {
widget::column([
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()
@ -57,16 +50,14 @@ impl State {
.align_x(Alignment::Center)
.align_y(Alignment::Center)
.into()
});
},
if let Some(error) = self.error.as_ref() {
main.push(widget::text(error).style(widget::text::danger).into());
}
if self.infobar_shown && self.info.is_some() {
main.push(widget::text(self.title()).into());
}
widget::column(main).into()
widget::text(error).style(widget::text::danger).into()
} else {
widget::space().into()
},
])
.into()
}
fn update(&mut self, message: Message) -> Task<Message> {
match message {
@ -116,15 +107,6 @@ impl State {
}
};
}
Key::Character("f") => {
self.image_filter = match self.image_filter {
widget::image::FilterMethod::Linear => widget::image::FilterMethod::Nearest,
widget::image::FilterMethod::Nearest => widget::image::FilterMethod::Linear,
}
}
Key::Character("b") => {
self.infobar_shown = !self.infobar_shown;
}
Key::Character("s") => {
self.save_image();
@ -154,16 +136,7 @@ impl State {
}
fn load_image(&mut self, path: impl AsRef<Path>) -> Result<Task<Message>, String> {
utils::load_image(&path).map(|image| {
self.info = Some(format!(
"{} {}x{}",
path.as_ref()
.file_name()
.map(OsStr::to_string_lossy)
.unwrap_or(Cow::Borrowed("[no file]")),
image.width(),
image.height(),
));
utils::load_image(path).map(|image| {
self.image = Some(image);
self.allocate_image()
})
@ -194,11 +167,8 @@ 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),
),
match self.image.as_ref() {
Some(image) => format!("imagey {}x{}", image.width(), image.height()),
None => "imagey".into(),
}
}
@ -213,9 +183,12 @@ fn main() -> Result<(), iced::Error> {
.theme(Theme::custom(
"custom",
theme::Palette {
background: Color::BLACK,
background: color!(0x080808),
text: Color::WHITE,
..theme::Palette::DARK
primary: color!(0xff00ff),
success: color!(0x00ff00),
warning: color!(0x880000),
danger: color!(0xff0000),
},
))
.run()

View file

@ -1,6 +1,5 @@
use std::path::{Path, PathBuf};
use iced::widget;
use image::{DynamicImage, ImageDecoder, ImageReader, RgbaImage};
use rfd::FileDialog;
@ -80,10 +79,3 @@ pub fn save_image(image: Option<&RgbaImage>) -> Result<(), String> {
Ok(())
}
pub fn string_from_filter_type(f: widget::image::FilterMethod) -> &'static str {
match f {
widget::image::FilterMethod::Linear => "bilinear",
widget::image::FilterMethod::Nearest => "nearest neighbor",
}
}