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

Add screenshot support for eframe web (#5438)

This implements web support for taking screenshots in an eframe app (and
adds a nice demo).
It also updates the native screenshot implementation to work with the
wgpu gl backend.

The wgpu implementation is quite different than the native one because
we can't block to wait for the screenshot result, so instead I use a
channel to pass the result to a future frame asynchronously.

* Closes <https://github.com/emilk/egui/issues/5425>
* [x] I have followed the instructions in the PR template


https://github.com/user-attachments/assets/67cad40b-0384-431d-96a3-075cc3cb98fb
This commit is contained in:
lucasmerlin
2024-12-12 19:17:42 +01:00
committed by GitHub
parent de8ac88c0e
commit 6c1d695fc6
13 changed files with 613 additions and 272 deletions

View File

@@ -185,6 +185,7 @@ impl<'app> WgpuWinitApp<'app> {
#[allow(unsafe_code, unused_mut, unused_unsafe)]
let mut painter = egui_wgpu::winit::Painter::new(
egui_ctx.clone(),
self.native_options.wgpu_options.clone(),
self.native_options.multisampling.max(1) as _,
egui_wgpu::depth_format_from_bits(
@@ -593,6 +594,8 @@ impl<'app> WgpuWinitRunning<'app> {
.map(|(id, viewport)| (*id, viewport.info.clone()))
.collect();
painter.handle_screenshots(&mut raw_input.events);
(viewport_ui_cb, raw_input)
};
@@ -652,37 +655,14 @@ impl<'app> WgpuWinitRunning<'app> {
true
}
});
let screenshot_requested = !screenshot_commands.is_empty();
let (vsync_secs, screenshot) = painter.paint_and_update_textures(
let vsync_secs = painter.paint_and_update_textures(
viewport_id,
pixels_per_point,
app.clear_color(&egui_ctx.style().visuals),
&clipped_primitives,
&textures_delta,
screenshot_requested,
screenshot_commands,
);
match (screenshot_requested, screenshot) {
(false, None) => {}
(true, Some(screenshot)) => {
let screenshot = Arc::new(screenshot);
for user_data in screenshot_commands {
egui_winit
.egui_input_mut()
.events
.push(egui::Event::Screenshot {
viewport_id,
user_data,
image: screenshot.clone(),
});
}
}
(true, None) => {
log::error!("Bug in egui_wgpu: screenshot requested, but no screenshot was taken");
}
(false, Some(_)) => {
log::warn!("Bug in egui_wgpu: Got screenshot without requesting it");
}
}
for action in viewport.actions_requested.drain() {
match action {
@@ -1024,7 +1004,7 @@ fn render_immediate_viewport(
[0.0, 0.0, 0.0, 0.0],
&clipped_primitives,
&textures_delta,
false,
vec![],
);
egui_winit.handle_platform_output(window, platform_output);