fix: image flickering
This commit is contained in:
parent
9da1395f8e
commit
6e45b3afdc
1 changed files with 30 additions and 11 deletions
41
src/main.rs
41
src/main.rs
|
|
@ -9,16 +9,18 @@ use image::{EncodableLayout, ImageReader, RgbaImage, imageops};
|
|||
|
||||
#[derive(Clone, Debug)]
|
||||
enum Message {
|
||||
ImageDisplayReady(Result<widget::image::Allocation, widget::image::Error>),
|
||||
|
||||
Event(iced::Event),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct State {
|
||||
image: RgbaImage,
|
||||
image_handle: widget::image::Handle,
|
||||
image_display: Option<widget::image::Allocation>,
|
||||
}
|
||||
impl State {
|
||||
fn new() -> Self {
|
||||
fn new() -> (Self, Task<Message>) {
|
||||
let image = if let Some(path) = env::args().nth(1) {
|
||||
ImageReader::open(path)
|
||||
.unwrap()
|
||||
|
|
@ -29,18 +31,34 @@ impl State {
|
|||
RgbaImage::new(100, 100)
|
||||
};
|
||||
|
||||
State {
|
||||
image_handle: widget::image::Handle::from_rgba(image.width(), image.height(), unsafe {
|
||||
std::mem::transmute::<_, &'static [u8]>(image.as_bytes())
|
||||
}),
|
||||
image,
|
||||
}
|
||||
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 {
|
||||
image_display: None,
|
||||
image,
|
||||
},
|
||||
allocate_image,
|
||||
)
|
||||
}
|
||||
fn view(&self) -> Element<'_, Message, Theme, Renderer> {
|
||||
widget::image(&self.image_handle).expand(true).into()
|
||||
if let Some(allocation) = self.image_display.as_ref() {
|
||||
widget::image(allocation.handle()).expand(true).into()
|
||||
} else {
|
||||
widget::space().into()
|
||||
}
|
||||
}
|
||||
fn update(&mut self, message: Message) -> Task<Message> {
|
||||
match message {
|
||||
Message::ImageDisplayReady(result) => {
|
||||
self.image_display = Some(result.unwrap());
|
||||
}
|
||||
|
||||
Message::Event(iced::Event::Keyboard(keyboard::Event::KeyPressed {
|
||||
key,
|
||||
modifiers,
|
||||
|
|
@ -59,11 +77,12 @@ impl State {
|
|||
|
||||
Key::Character("r") => {
|
||||
self.image = imageops::rotate90(&self.image);
|
||||
self.image_handle = widget::image::Handle::from_rgba(
|
||||
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);
|
||||
}
|
||||
|
||||
// ignore unused keys
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue