refactor: add error field and display

This commit is contained in:
electria 2026-07-01 09:36:46 -07:00
commit ad0b37d0c3
Signed by: electria
SSH key fingerprint: SHA256:8LlB3ucPbBHqozqkhsNbaV5oG3SlzzqUj8FZDL6IPQs

View file

@ -19,6 +19,8 @@ enum Message {
struct State { struct State {
image: Option<RgbaImage>, image: Option<RgbaImage>,
image_display: Option<widget::image::Allocation>, image_display: Option<widget::image::Allocation>,
error: Option<String>,
} }
impl State { impl State {
fn new() -> (Self, Task<Message>) { fn new() -> (Self, Task<Message>) {
@ -33,25 +35,35 @@ impl State {
.into_rgba8(), .into_rgba8(),
) )
}), }),
error: None,
}; };
let allocate_image = state.allocate_image(); let allocate_image = state.allocate_image();
(state, allocate_image) (state, allocate_image)
} }
fn view(&self) -> Element<'_, Message, Theme, Renderer> { fn view(&self) -> Element<'_, Message, Theme, Renderer> {
if let Some(allocation) = self.image_display.as_ref() { widget::column([
widget::image::viewer(allocation.handle().clone()) if let Some(allocation) = self.image_display.as_ref() {
.width(Length::Fill) widget::image::viewer(allocation.handle().clone())
.height(Length::Fill) .width(Length::Fill)
.into() .height(Length::Fill)
} else { .into()
widget::container(widget::text(include_str!("usage.txt"))) } else {
.height(Length::Fill) widget::container(widget::text(include_str!("usage.txt")))
.width(Length::Fill) .height(Length::Fill)
.align_x(Alignment::Center) .width(Length::Fill)
.align_y(Alignment::Center) .align_x(Alignment::Center)
.into() .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> { fn update(&mut self, message: Message) -> Task<Message> {
match message { match message {