refactor: deduplicate image allocation

This commit is contained in:
electria 2026-06-28 18:02:09 -07:00
commit 090076a26f
Signed by: electria
SSH key fingerprint: SHA256:8LlB3ucPbBHqozqkhsNbaV5oG3SlzzqUj8FZDL6IPQs

View file

@ -31,20 +31,13 @@ impl State {
RgbaImage::new(100, 100)
};
let allocate_image = widget::image::allocate(widget::image::Handle::from_rgba(
image.width(),
image.height(),
unsafe { std::mem::transmute::<_, &'static [u8]>(image.as_bytes()) },
))
.map(Message::ImageDisplayReady);
(
State {
let state = State {
image_display: None,
image,
},
allocate_image,
)
};
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() {
@ -77,12 +70,7 @@ impl State {
Key::Character("r") => {
self.image = imageops::rotate90(&self.image);
return widget::image::allocate(widget::image::Handle::from_rgba(
self.image.width(),
self.image.height(),
unsafe { std::mem::transmute::<_, &'static [u8]>(self.image.as_bytes()) },
))
.map(Message::ImageDisplayReady);
return self.allocate_image();
}
// ignore unused keys
@ -96,6 +84,15 @@ impl State {
Task::none()
}
fn allocate_image(&self) -> Task<Message> {
widget::image::allocate(widget::image::Handle::from_rgba(
self.image.width(),
self.image.height(),
unsafe { std::mem::transmute::<_, &'static [u8]>(self.image.as_bytes()) },
))
.map(Message::ImageDisplayReady)
}
fn subscription(&self) -> Subscription<Message> {
event::listen().map(Message::Event)
}