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
95
src/main.rs
95
src/main.rs
|
|
@ -29,7 +29,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 +43,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());
|
||||
|
|
@ -84,9 +87,8 @@ 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() {
|
||||
|
|
@ -97,26 +99,22 @@ impl State {
|
|||
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("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,
|
||||
|
|
@ -160,8 +158,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(),
|
||||
));
|
||||
|
|
@ -210,13 +207,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),
|
||||
)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,9 +5,9 @@ use image::{DynamicImage, ImageDecoder, ImageReader, ImageResult, RgbaImage};
|
|||
use rfd::FileDialog;
|
||||
|
||||
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()?;
|
||||
|
|
@ -48,12 +48,12 @@ pub fn save_image(image: Option<&RgbaImage>) -> Result<(), String> {
|
|||
|
||||
if let Err(e) = image.save(&path) {
|
||||
return Err(e.to_string());
|
||||
};
|
||||
}
|
||||
|
||||
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 {
|
||||
widget::image::FilterMethod::Linear => "bilinear",
|
||||
widget::image::FilterMethod::Nearest => "nearest neighbor",
|
||||
|
|
|
|||
Loading…
Reference in a new issue