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)
This commit is contained in:
parent
94c905db38
commit
9d4e24e809
2 changed files with 51 additions and 52 deletions
53
src/main.rs
53
src/main.rs
|
|
@ -29,7 +29,7 @@ struct State {
|
||||||
}
|
}
|
||||||
impl State {
|
impl State {
|
||||||
fn new() -> (Self, Task<Message>) {
|
fn new() -> (Self, Task<Message>) {
|
||||||
let mut state = State::default();
|
let mut state = Self::default();
|
||||||
|
|
||||||
if let Some(path) = env::args().nth(1) {
|
if let Some(path) = env::args().nth(1) {
|
||||||
match state.load_image(path) {
|
match state.load_image(path) {
|
||||||
|
|
@ -43,7 +43,16 @@ impl State {
|
||||||
fn view(&self) -> Element<'_, Message, Theme, Renderer> {
|
fn view(&self) -> Element<'_, Message, Theme, Renderer> {
|
||||||
let mut main = Vec::new();
|
let mut main = Vec::new();
|
||||||
|
|
||||||
main.push(if let Some(allocation) = self.image_display.as_ref() {
|
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())
|
widget::image::viewer(allocation.handle().clone())
|
||||||
.filter_method(self.image_filter)
|
.filter_method(self.image_filter)
|
||||||
.max_scale(50.)
|
.max_scale(50.)
|
||||||
|
|
@ -51,14 +60,8 @@ impl State {
|
||||||
.width(Length::Fill)
|
.width(Length::Fill)
|
||||||
.height(Length::Fill)
|
.height(Length::Fill)
|
||||||
.into()
|
.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()
|
|
||||||
});
|
|
||||||
|
|
||||||
if let Some(error) = self.error.as_ref() {
|
if let Some(error) = self.error.as_ref() {
|
||||||
main.push(widget::text(error).style(widget::text::danger).into());
|
main.push(widget::text(error).style(widget::text::danger).into());
|
||||||
|
|
@ -84,9 +87,8 @@ impl State {
|
||||||
Key::Named(key::Named::Tab) => {
|
Key::Named(key::Named::Tab) => {
|
||||||
if modifiers.shift() {
|
if modifiers.shift() {
|
||||||
return widget::operation::focus_previous();
|
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() {
|
Key::Character("o") => match self.pick_and_load_image() {
|
||||||
|
|
@ -97,26 +99,22 @@ impl State {
|
||||||
Err(e) => self.error = Some(e),
|
Err(e) => self.error = Some(e),
|
||||||
},
|
},
|
||||||
|
|
||||||
Key::Character("r") => {
|
Key::Character("r") => match self.image.as_ref() {
|
||||||
match self.image.as_ref() {
|
|
||||||
None => self.error = Some("no image to rotate".into()),
|
None => self.error = Some("no image to rotate".into()),
|
||||||
Some(image) => {
|
Some(image) => {
|
||||||
self.error = None;
|
self.error = None;
|
||||||
self.image = Some(imageops::rotate90(image));
|
self.image = Some(imageops::rotate90(image));
|
||||||
return self.allocate_image();
|
return self.allocate_image();
|
||||||
}
|
}
|
||||||
};
|
},
|
||||||
}
|
Key::Character("i") => match self.image.as_mut() {
|
||||||
Key::Character("i") => {
|
|
||||||
match self.image.as_mut() {
|
|
||||||
None => self.error = Some("no image to invert".into()),
|
None => self.error = Some("no image to invert".into()),
|
||||||
Some(image) => {
|
Some(image) => {
|
||||||
self.error = None;
|
self.error = None;
|
||||||
imageops::invert(image);
|
imageops::invert(image);
|
||||||
return self.allocate_image();
|
return self.allocate_image();
|
||||||
}
|
}
|
||||||
};
|
},
|
||||||
}
|
|
||||||
Key::Character("f") => {
|
Key::Character("f") => {
|
||||||
self.image_filter = match self.image_filter {
|
self.image_filter = match self.image_filter {
|
||||||
widget::image::FilterMethod::Linear => widget::image::FilterMethod::Nearest,
|
widget::image::FilterMethod::Linear => widget::image::FilterMethod::Nearest,
|
||||||
|
|
@ -160,8 +158,7 @@ impl State {
|
||||||
"{} {}x{}",
|
"{} {}x{}",
|
||||||
path.as_ref()
|
path.as_ref()
|
||||||
.file_name()
|
.file_name()
|
||||||
.map(OsStr::to_string_lossy)
|
.map_or(Cow::Borrowed("[no file]"), OsStr::to_string_lossy),
|
||||||
.unwrap_or(Cow::Borrowed("[no file]")),
|
|
||||||
image.width(),
|
image.width(),
|
||||||
image.height(),
|
image.height(),
|
||||||
));
|
));
|
||||||
|
|
@ -210,13 +207,15 @@ impl State {
|
||||||
event::listen().map(Message::Event)
|
event::listen().map(Message::Event)
|
||||||
}
|
}
|
||||||
fn title(&self) -> String {
|
fn title(&self) -> String {
|
||||||
match self.info.as_ref() {
|
self.info.as_ref().map_or_else(
|
||||||
Some(info) => format!(
|
|| "imagey".into(),
|
||||||
|
|info| {
|
||||||
|
format!(
|
||||||
"imagey {info} {}",
|
"imagey {info} {}",
|
||||||
utils::string_from_filter_type(self.image_filter),
|
utils::string_from_filter_type(self.image_filter),
|
||||||
),
|
)
|
||||||
None => "imagey".into(),
|
},
|
||||||
}
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,9 @@ use image::{DynamicImage, ImageDecoder, ImageReader, ImageResult, RgbaImage};
|
||||||
use rfd::FileDialog;
|
use rfd::FileDialog;
|
||||||
|
|
||||||
pub fn load_image(path: impl AsRef<Path>) -> Result<RgbaImage, String> {
|
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)?
|
let mut decoder = ImageReader::open(path)?
|
||||||
.with_guessed_format()?
|
.with_guessed_format()?
|
||||||
.into_decoder()?;
|
.into_decoder()?;
|
||||||
|
|
@ -48,12 +48,12 @@ pub fn save_image(image: Option<&RgbaImage>) -> Result<(), String> {
|
||||||
|
|
||||||
if let Err(e) = image.save(&path) {
|
if let Err(e) = image.save(&path) {
|
||||||
return Err(e.to_string());
|
return Err(e.to_string());
|
||||||
};
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
match f {
|
||||||
widget::image::FilterMethod::Linear => "bilinear",
|
widget::image::FilterMethod::Linear => "bilinear",
|
||||||
widget::image::FilterMethod::Nearest => "nearest neighbor",
|
widget::image::FilterMethod::Nearest => "nearest neighbor",
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue