refactor: deduplicate image allocation
This commit is contained in:
parent
6e45b3afdc
commit
090076a26f
1 changed files with 16 additions and 19 deletions
33
src/main.rs
33
src/main.rs
|
|
@ -31,20 +31,13 @@ impl State {
|
||||||
RgbaImage::new(100, 100)
|
RgbaImage::new(100, 100)
|
||||||
};
|
};
|
||||||
|
|
||||||
let allocate_image = widget::image::allocate(widget::image::Handle::from_rgba(
|
let state = State {
|
||||||
image.width(),
|
|
||||||
image.height(),
|
|
||||||
unsafe { std::mem::transmute::<_, &'static [u8]>(image.as_bytes()) },
|
|
||||||
))
|
|
||||||
.map(Message::ImageDisplayReady);
|
|
||||||
|
|
||||||
(
|
|
||||||
State {
|
|
||||||
image_display: None,
|
image_display: None,
|
||||||
image,
|
image,
|
||||||
},
|
};
|
||||||
allocate_image,
|
let 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() {
|
if let Some(allocation) = self.image_display.as_ref() {
|
||||||
|
|
@ -77,12 +70,7 @@ impl State {
|
||||||
|
|
||||||
Key::Character("r") => {
|
Key::Character("r") => {
|
||||||
self.image = imageops::rotate90(&self.image);
|
self.image = imageops::rotate90(&self.image);
|
||||||
return widget::image::allocate(widget::image::Handle::from_rgba(
|
return self.allocate_image();
|
||||||
self.image.width(),
|
|
||||||
self.image.height(),
|
|
||||||
unsafe { std::mem::transmute::<_, &'static [u8]>(self.image.as_bytes()) },
|
|
||||||
))
|
|
||||||
.map(Message::ImageDisplayReady);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ignore unused keys
|
// ignore unused keys
|
||||||
|
|
@ -96,6 +84,15 @@ impl State {
|
||||||
Task::none()
|
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> {
|
fn subscription(&self) -> Subscription<Message> {
|
||||||
event::listen().map(Message::Event)
|
event::listen().map(Message::Event)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue