refactor: deduplicate image allocation
This commit is contained in:
parent
6e45b3afdc
commit
090076a26f
1 changed files with 16 additions and 19 deletions
35
src/main.rs
35
src/main.rs
|
|
@ -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);
|
||||
let state = State {
|
||||
image_display: None,
|
||||
image,
|
||||
};
|
||||
let allocate_image = state.allocate_image();
|
||||
|
||||
(
|
||||
State {
|
||||
image_display: None,
|
||||
image,
|
||||
},
|
||||
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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue