refactor: add error field and display
This commit is contained in:
parent
c8810f3e86
commit
ad0b37d0c3
1 changed files with 25 additions and 13 deletions
38
src/main.rs
38
src/main.rs
|
|
@ -19,6 +19,8 @@ enum Message {
|
|||
struct State {
|
||||
image: Option<RgbaImage>,
|
||||
image_display: Option<widget::image::Allocation>,
|
||||
|
||||
error: Option<String>,
|
||||
}
|
||||
impl State {
|
||||
fn new() -> (Self, Task<Message>) {
|
||||
|
|
@ -33,25 +35,35 @@ impl State {
|
|||
.into_rgba8(),
|
||||
)
|
||||
}),
|
||||
|
||||
error: None,
|
||||
};
|
||||
let allocate_image = state.allocate_image();
|
||||
|
||||
(state, allocate_image)
|
||||
}
|
||||
fn view(&self) -> Element<'_, Message, Theme, Renderer> {
|
||||
if let Some(allocation) = self.image_display.as_ref() {
|
||||
widget::image::viewer(allocation.handle().clone())
|
||||
.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()
|
||||
}
|
||||
widget::column([
|
||||
if let Some(allocation) = self.image_display.as_ref() {
|
||||
widget::image::viewer(allocation.handle().clone())
|
||||
.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()
|
||||
},
|
||||
if let Some(error) = self.error.as_ref() {
|
||||
widget::text(error).style(widget::text::danger).into()
|
||||
} else {
|
||||
widget::space().into()
|
||||
},
|
||||
])
|
||||
.into()
|
||||
}
|
||||
fn update(&mut self, message: Message) -> Task<Message> {
|
||||
match message {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue