mirror of
https://github.com/emilk/egui.git
synced 2026-08-30 05:10:03 -04:00
Code cleanup
This commit is contained in:
@@ -1,28 +1,6 @@
|
||||
use egui_web::fetch::Response;
|
||||
use std::sync::mpsc::Receiver;
|
||||
|
||||
struct Image {
|
||||
size: (usize, usize),
|
||||
pixels: Vec<egui::Srgba>,
|
||||
}
|
||||
|
||||
impl Image {
|
||||
fn decode(bytes: &[u8]) -> Option<Image> {
|
||||
use image::GenericImageView;
|
||||
let image = image::load_from_memory(&bytes).ok()?;
|
||||
let image_buffer = image.to_rgba();
|
||||
let size = (image.width() as usize, image.height() as usize);
|
||||
let pixels = image_buffer.into_vec();
|
||||
assert_eq!(size.0 * size.1 * 4, pixels.len());
|
||||
let pixels = pixels
|
||||
.chunks(4)
|
||||
.map(|p| egui::Srgba::from_rgba_unmultiplied(p[0], p[1], p[2], p[3]))
|
||||
.collect();
|
||||
|
||||
Some(Image { size, pixels })
|
||||
}
|
||||
}
|
||||
|
||||
struct Resource {
|
||||
/// HTTP response
|
||||
response: Response,
|
||||
@@ -31,11 +9,22 @@ struct Resource {
|
||||
image: Option<Image>,
|
||||
}
|
||||
|
||||
impl Resource {
|
||||
fn from_response(response: Response) -> Self {
|
||||
let image = if response.header_content_type.starts_with("image/") {
|
||||
Image::decode(&response.bytes)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
Self { response, image }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ExampleApp {
|
||||
url: String,
|
||||
in_progress: Option<Receiver<Result<Response, String>>>,
|
||||
result: Option<Result<Resource, String>>,
|
||||
texture_id: Option<egui::TextureId>,
|
||||
tex_mngr: TexMngr,
|
||||
}
|
||||
|
||||
impl Default for ExampleApp {
|
||||
@@ -44,7 +33,7 @@ impl Default for ExampleApp {
|
||||
url: "https://raw.githubusercontent.com/emilk/egui/master/README.md".to_owned(),
|
||||
in_progress: Default::default(),
|
||||
result: Default::default(),
|
||||
texture_id: None,
|
||||
tex_mngr: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -58,7 +47,7 @@ impl egui::app::App for ExampleApp {
|
||||
integration_context: &mut egui::app::IntegrationContext,
|
||||
) {
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
ui.heading("HTTP Get inside of Egui");
|
||||
ui.heading("Egui Example App");
|
||||
ui.add(egui::github_link_file!(
|
||||
"https://github.com/emilk/egui/blob/master/",
|
||||
"(source code)"
|
||||
@@ -81,7 +70,7 @@ impl egui::app::App for ExampleApp {
|
||||
} else if let Some(result) = &self.result {
|
||||
match result {
|
||||
Ok(resource) => {
|
||||
ui_resouce(ui, self.texture_id, resource);
|
||||
ui_resouce(ui, integration_context, &mut self.tex_mngr, resource);
|
||||
}
|
||||
Err(error) => {
|
||||
// This should only happen if the fetch API isn't available or something similar.
|
||||
@@ -91,40 +80,11 @@ impl egui::app::App for ExampleApp {
|
||||
}
|
||||
});
|
||||
|
||||
self.poll_receiver(integration_context);
|
||||
}
|
||||
}
|
||||
|
||||
impl ExampleApp {
|
||||
fn load_image(
|
||||
&mut self,
|
||||
integration_context: &mut egui::app::IntegrationContext,
|
||||
response: &Response,
|
||||
) -> Option<Image> {
|
||||
let tex_allocator = integration_context.tex_allocator.as_mut()?;
|
||||
|
||||
if matches!(
|
||||
response.header_content_type.as_str(),
|
||||
"image/jpeg" | "image/png"
|
||||
) {
|
||||
let image = Image::decode(&response.bytes)?;
|
||||
let texture_id = self.texture_id.unwrap_or_else(|| tex_allocator.alloc());
|
||||
self.texture_id = Some(texture_id);
|
||||
tex_allocator.set_srgba_premultiplied(texture_id, image.size, &image.pixels);
|
||||
return Some(image);
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn poll_receiver(&mut self, integration_context: &mut egui::app::IntegrationContext) {
|
||||
if let Some(receiver) = &mut self.in_progress {
|
||||
// Are we there yet?
|
||||
if let Ok(result) = receiver.try_recv() {
|
||||
self.in_progress = None;
|
||||
self.result = Some(result.map(|response| Resource {
|
||||
image: self.load_image(integration_context, &response),
|
||||
response,
|
||||
}));
|
||||
self.result = Some(result.map(Resource::from_response));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -136,7 +96,10 @@ fn ui_url(ui: &mut egui::Ui, url: &mut String) -> bool {
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("URL:");
|
||||
trigger_fetch |= ui.text_edit_singleline(url).lost_kb_focus;
|
||||
trigger_fetch |= ui.button("GET").clicked;
|
||||
});
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
if ui.button("Source code for this example").clicked {
|
||||
*url = format!(
|
||||
"https://raw.githubusercontent.com/emilk/egui/master/{}",
|
||||
@@ -152,12 +115,16 @@ fn ui_url(ui: &mut egui::Ui, url: &mut String) -> bool {
|
||||
trigger_fetch = true;
|
||||
}
|
||||
});
|
||||
trigger_fetch |= ui.button("GET").clicked;
|
||||
|
||||
trigger_fetch
|
||||
}
|
||||
|
||||
fn ui_resouce(ui: &mut egui::Ui, texture_id: Option<egui::TextureId>, resource: &Resource) {
|
||||
fn ui_resouce(
|
||||
ui: &mut egui::Ui,
|
||||
integration_context: &mut egui::app::IntegrationContext,
|
||||
tex_mngr: &mut TexMngr,
|
||||
resource: &Resource,
|
||||
) {
|
||||
let Resource { response, image } = resource;
|
||||
|
||||
ui.monospace(format!("url: {}", response.url));
|
||||
@@ -172,11 +139,9 @@ fn ui_resouce(ui: &mut egui::Ui, texture_id: Option<egui::TextureId>, resource:
|
||||
));
|
||||
|
||||
if let Some(image) = image {
|
||||
if let Some(texture_id) = texture_id {
|
||||
ui.image(
|
||||
texture_id,
|
||||
egui::Vec2::new(image.size.0 as f32, image.size.1 as f32),
|
||||
);
|
||||
if let Some(texture_id) = tex_mngr.texture(integration_context, &response.url, &image) {
|
||||
let size = egui::Vec2::new(image.size.0 as f32, image.size.1 as f32);
|
||||
ui.image(texture_id, size);
|
||||
}
|
||||
} else if let Some(text) = &response.text {
|
||||
ui.monospace("Body:");
|
||||
@@ -188,3 +153,53 @@ fn ui_resouce(ui: &mut egui::Ui, texture_id: Option<egui::TextureId>, resource:
|
||||
ui.monospace("[binary]");
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Texture/image handling is very manual at the moment.
|
||||
|
||||
/// Immediate mode texture managers that supports at most one texture at the time :)
|
||||
#[derive(Default)]
|
||||
struct TexMngr {
|
||||
loaded_url: String,
|
||||
texture_id: Option<egui::TextureId>,
|
||||
}
|
||||
|
||||
impl TexMngr {
|
||||
fn texture(
|
||||
&mut self,
|
||||
integration_context: &mut egui::app::IntegrationContext,
|
||||
url: &str,
|
||||
image: &Image,
|
||||
) -> Option<egui::TextureId> {
|
||||
let tex_allocator = integration_context.tex_allocator.as_mut()?;
|
||||
let texture_id = self.texture_id.unwrap_or_else(|| tex_allocator.alloc());
|
||||
self.texture_id = Some(texture_id);
|
||||
if self.loaded_url != url {
|
||||
self.loaded_url = url.to_owned();
|
||||
tex_allocator.set_srgba_premultiplied(texture_id, image.size, &image.pixels);
|
||||
}
|
||||
Some(texture_id)
|
||||
}
|
||||
}
|
||||
|
||||
struct Image {
|
||||
size: (usize, usize),
|
||||
pixels: Vec<egui::Srgba>,
|
||||
}
|
||||
|
||||
impl Image {
|
||||
fn decode(bytes: &[u8]) -> Option<Image> {
|
||||
use image::GenericImageView;
|
||||
let image = image::load_from_memory(&bytes).ok()?;
|
||||
let image_buffer = image.to_rgba();
|
||||
let size = (image.width() as usize, image.height() as usize);
|
||||
let pixels = image_buffer.into_vec();
|
||||
assert_eq!(size.0 * size.1 * 4, pixels.len());
|
||||
let pixels = pixels
|
||||
.chunks(4)
|
||||
.map(|p| egui::Srgba::from_rgba_unmultiplied(p[0], p[1], p[2], p[3]))
|
||||
.collect();
|
||||
|
||||
Some(Image { size, pixels })
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user