Compare commits

..

View file

@ -79,13 +79,9 @@ impl State {
}
}
Key::Character("o") => match self.pick_and_load_image() {
Ok(task) => {
self.error = None;
return task;
Key::Character("o") => {
return self.pick_and_load_image();
}
Err(e) => self.error = Some(e),
},
Key::Character("r") => {
match self.image.as_ref() {
@ -119,13 +115,9 @@ impl State {
},
Message::Event(iced::Event::Window(window_event)) => match window_event {
window::Event::FileDropped(file) => match self.load_image(file) {
Ok(task) => {
self.error = None;
return task;
window::Event::FileDropped(file) => {
return self.load_image(file);
}
Err(e) => self.error = Some(e),
},
// ignore unused window events
_ => {}
@ -138,14 +130,28 @@ impl State {
Task::none()
}
fn load_image(&mut self, path: impl AsRef<Path>) -> Result<Task<Message>, String> {
utils::load_image(path).and_then(|image| {
#[must_use]
fn load_image(&mut self, path: impl AsRef<Path>) -> Task<Message> {
let task;
(self.error, task) = match utils::load_image(path) {
Ok(image) => {
self.image = Some(image);
Ok(self.allocate_image())
})
(None, self.allocate_image())
}
fn pick_and_load_image(&mut self) -> Result<Task<Message>, String> {
utils::pick_image().and_then(|path| self.load_image(path))
Err(e) => (Some(e), Task::none()),
};
task
}
#[must_use]
fn pick_and_load_image(&mut self) -> Task<Message> {
let task;
(self.error, task) = match utils::pick_image() {
Ok(path) => (None, self.load_image(path)),
Err(e) => (Some(e), Task::none()),
};
task
}
fn save_image(&mut self) {
@ -170,18 +176,11 @@ impl State {
fn subscription(&self) -> Subscription<Message> {
event::listen().map(Message::Event)
}
fn title(&self) -> String {
match self.image.as_ref() {
Some(image) => format!("imagey {}x{}", image.width(), image.height()),
None => format!("imagey"),
}
}
}
fn main() -> Result<(), iced::Error> {
iced::application(State::new, State::update, State::view)
.subscription(State::subscription)
.title(State::title)
.theme(Theme::custom(
"custom",
theme::Palette {