1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 13:20:05 -04:00

Improve the frame around ImageButton

Helps https://github.com/emilk/egui/issues/721
This commit is contained in:
Emil Ernerfeldt
2021-09-20 22:36:57 +02:00
parent e7cfda4941
commit a5a5d6176d
5 changed files with 43 additions and 20 deletions

View File

@@ -2,7 +2,7 @@ use eframe::{egui, epi};
#[derive(Default)]
struct MyApp {
texture: Option<egui::TextureId>,
texture: Option<(egui::Vec2, egui::TextureId)>,
}
impl epi::App for MyApp {
@@ -13,7 +13,7 @@ impl epi::App for MyApp {
fn update(&mut self, ctx: &egui::CtxRef, frame: &mut epi::Frame<'_>) {
if self.texture.is_none() {
// Load the image:
let image_data = include_bytes!("rust-logo-512x512.png");
let image_data = include_bytes!("rust-logo-256x256.png");
use image::GenericImageView;
let image = image::load_from_memory(image_data).expect("Failed to load image");
let image_buffer = image.to_rgba8();
@@ -29,13 +29,17 @@ impl epi::App for MyApp {
let texture = frame
.tex_allocator()
.alloc_srgba_premultiplied(size, &pixels);
self.texture = Some(texture);
let size = egui::Vec2::new(size.0 as f32, size.1 as f32);
self.texture = Some((size, texture));
}
egui::CentralPanel::default().show(ctx, |ui| {
ui.heading("Here is an image for you:");
if let Some(texture) = self.texture {
ui.image(texture, egui::Vec2::splat(256.0));
if let Some((size, texture)) = self.texture {
ui.heading("This is an image:");
ui.image(texture, size);
ui.heading("This is an image you can click:");
ui.add(egui::ImageButton::new(texture, size));
}
});
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 84 KiB