diff --git a/Cargo.lock b/Cargo.lock index a822a3e56..66dcdb9f1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1147,6 +1147,7 @@ dependencies = [ "egui_glow", "glow", "glutin", + "image", "js-sys", "percent-encoding", "puffin", diff --git a/crates/eframe/Cargo.toml b/crates/eframe/Cargo.toml index 596e6d72e..a0021a348 100644 --- a/crates/eframe/Cargo.toml +++ b/crates/eframe/Cargo.toml @@ -52,6 +52,10 @@ puffin = ["dep:puffin", "egui_glow?/puffin", "egui-wgpu?/puffin"] ## Enable screen reader support (requires `ctx.options().screen_reader = true;`) screen_reader = ["egui-winit/screen_reader", "tts"] +## If set, eframe will look for the env-var `EFRAME_SCREENSHOT_TO` and write a screenshot to that location, and then quit. +## This is used to generate images for the examples. +__screenshot = ["dep:image"] + ## Use [`wgpu`](https://docs.rs/wgpu) for painting (via [`egui-wgpu`](https://github.com/emilk/egui/tree/master/crates/egui-wgpu)). ## This overrides the `glow` feature. wgpu = ["dep:wgpu", "dep:egui-wgpu"] @@ -89,6 +93,9 @@ directories-next = { version = "2", optional = true } egui-wgpu = { version = "0.19.0", path = "../egui-wgpu", optional = true, features = [ "winit", ] } +image = { version = "0.24", optional = true, default-features = false, features = [ + "png", +] } puffin = { version = "0.14", optional = true } wgpu = { version = "0.14", optional = true } diff --git a/crates/eframe/src/lib.rs b/crates/eframe/src/lib.rs index b8e2ba2eb..3fa6be938 100644 --- a/crates/eframe/src/lib.rs +++ b/crates/eframe/src/lib.rs @@ -168,6 +168,12 @@ mod native; pub fn run_native(app_name: &str, native_options: NativeOptions, app_creator: AppCreator) { let renderer = native_options.renderer; + #[cfg(not(feature = "__screenshot"))] + assert!( + std::env::var("EFRAME_SCREENSHOT_TO").is_err(), + "EFRAME_SCREENSHOT_TO found without compiling with the '__screenshot' feature" + ); + match renderer { #[cfg(feature = "glow")] Renderer::Glow => { diff --git a/crates/eframe/src/native/run.rs b/crates/eframe/src/native/run.rs index 5d28e3ea4..27987687d 100644 --- a/crates/eframe/src/native/run.rs +++ b/crates/eframe/src/native/run.rs @@ -241,7 +241,7 @@ fn run_and_exit(event_loop: EventLoop, mut winit_app: impl WinitApp + }) } -fn centere_window_pos( +fn center_window_pos( monitor: Option, native_options: &mut epi::NativeOptions, ) { @@ -308,6 +308,8 @@ mod glow_integration { // suspends and resumes. app_creator: Option, is_focused: bool, + + frame_nr: u64, } impl GlowWinitApp { @@ -324,6 +326,7 @@ mod glow_integration { running: None, app_creator: Some(app_creator), is_focused: true, + frame_nr: 0, } } @@ -517,6 +520,26 @@ mod glow_integration { integration.post_present(window); + #[cfg(feature = "__screenshot")] + // give it time to settle: + if self.frame_nr == 2 { + if let Ok(path) = std::env::var("EFRAME_SCREENSHOT_TO") { + assert!( + path.ends_with(".png"), + "Expected EFRAME_SCREENSHOT_TO to end with '.png', got {path:?}" + ); + let [w, h] = screen_size_in_pixels; + let pixels = painter.read_screen_rgba(screen_size_in_pixels); + let image = image::RgbaImage::from_vec(w, h, pixels).unwrap(); + let image = image::imageops::flip_vertical(&image); + image.save(&path).unwrap_or_else(|err| { + panic!("Failed to save screenshot to {path:?}: {err}"); + }); + eprintln!("Screenshot saved to {path:?}."); + std::process::exit(0); + } + } + let control_flow = if integration.should_close() { EventResult::Exit } else if repaint_after.is_zero() { @@ -546,6 +569,8 @@ mod glow_integration { std::thread::sleep(std::time::Duration::from_millis(10)); } + self.frame_nr += 1; + control_flow } else { EventResult::Wait @@ -659,7 +684,7 @@ mod glow_integration { if native_options.run_and_return { with_event_loop(native_options, |event_loop, mut native_options| { if native_options.centered { - centere_window_pos(event_loop.available_monitors().next(), &mut native_options); + center_window_pos(event_loop.available_monitors().next(), &mut native_options); } let glow_eframe = @@ -670,7 +695,7 @@ mod glow_integration { let event_loop = create_event_loop_builder(&mut native_options).build(); if native_options.centered { - centere_window_pos(event_loop.available_monitors().next(), &mut native_options); + center_window_pos(event_loop.available_monitors().next(), &mut native_options); } let glow_eframe = GlowWinitApp::new(&event_loop, app_name, native_options, app_creator); @@ -718,6 +743,12 @@ mod wgpu_integration { native_options: epi::NativeOptions, app_creator: epi::AppCreator, ) -> Self { + #[cfg(feature = "__screenshot")] + assert!( + std::env::var("EFRAME_SCREENSHOT_TO").is_err(), + "EFRAME_SCREENSHOT_TO not yet implemented for wgpu backend" + ); + Self { repaint_proxy: Arc::new(std::sync::Mutex::new(event_loop.create_proxy())), app_name: app_name.to_owned(), @@ -1050,7 +1081,7 @@ mod wgpu_integration { if native_options.run_and_return { with_event_loop(native_options, |event_loop, mut native_options| { if native_options.centered { - centere_window_pos(event_loop.available_monitors().next(), &mut native_options); + center_window_pos(event_loop.available_monitors().next(), &mut native_options); } let wgpu_eframe = @@ -1061,7 +1092,7 @@ mod wgpu_integration { let event_loop = create_event_loop_builder(&mut native_options).build(); if native_options.centered { - centere_window_pos(event_loop.available_monitors().next(), &mut native_options); + center_window_pos(event_loop.available_monitors().next(), &mut native_options); } let wgpu_eframe = WgpuWinitApp::new(&event_loop, app_name, native_options, app_creator); diff --git a/crates/egui_glow/src/painter.rs b/crates/egui_glow/src/painter.rs index 84f2eda1f..27f5571c8 100644 --- a/crates/egui_glow/src/painter.rs +++ b/crates/egui_glow/src/painter.rs @@ -605,6 +605,38 @@ impl Painter { } } + pub fn read_screen_rgba(&self, [w, h]: [u32; 2]) -> Vec { + let mut pixels = vec![0_u8; (w * h * 4) as usize]; + unsafe { + self.gl.read_pixels( + 0, + 0, + w as _, + h as _, + glow::RGBA, + glow::UNSIGNED_BYTE, + glow::PixelPackData::Slice(&mut pixels), + ); + } + pixels + } + + pub fn read_screen_rgb(&self, [w, h]: [u32; 2]) -> Vec { + let mut pixels = vec![0_u8; (w * h * 3) as usize]; + unsafe { + self.gl.read_pixels( + 0, + 0, + w as _, + h as _, + glow::RGB, + glow::UNSIGNED_BYTE, + glow::PixelPackData::Slice(&mut pixels), + ); + } + pixels + } + unsafe fn destroy_gl(&self) { self.gl.delete_program(self.program); for tex in self.textures.values() { diff --git a/examples/confirm_exit/Cargo.toml b/examples/confirm_exit/Cargo.toml index 8393aae60..f9340a999 100644 --- a/examples/confirm_exit/Cargo.toml +++ b/examples/confirm_exit/Cargo.toml @@ -9,4 +9,6 @@ publish = false [dependencies] -eframe = { path = "../../crates/eframe" } +eframe = { path = "../../crates/eframe", features = [ + "__screenshot", # __screenshot is so we can dump a ascreenshot using EFRAME_SCREENSHOT_TO +] } diff --git a/examples/confirm_exit/README.md b/examples/confirm_exit/README.md index b159db585..846d0baea 100644 --- a/examples/confirm_exit/README.md +++ b/examples/confirm_exit/README.md @@ -1,3 +1,7 @@ +Example how to show a confirm dialog before exiting an application. + ```sh cargo run -p confirm_exit ``` + +![](screenshot.png) diff --git a/examples/confirm_exit/screenshot.png b/examples/confirm_exit/screenshot.png new file mode 100644 index 000000000..062cd88ed Binary files /dev/null and b/examples/confirm_exit/screenshot.png differ diff --git a/examples/confirm_exit/src/main.rs b/examples/confirm_exit/src/main.rs index aa0e372a4..f909ede5c 100644 --- a/examples/confirm_exit/src/main.rs +++ b/examples/confirm_exit/src/main.rs @@ -3,7 +3,10 @@ use eframe::egui; fn main() { - let options = eframe::NativeOptions::default(); + let options = eframe::NativeOptions { + initial_window_size: Some(egui::vec2(320.0, 240.0)), + ..Default::default() + }; eframe::run_native( "Confirm exit", options, diff --git a/examples/custom_3d_glow/Cargo.toml b/examples/custom_3d_glow/Cargo.toml index f124b9014..8e7a630d0 100644 --- a/examples/custom_3d_glow/Cargo.toml +++ b/examples/custom_3d_glow/Cargo.toml @@ -9,6 +9,8 @@ publish = false [dependencies] -eframe = { path = "../../crates/eframe", features = ["glow"] } +eframe = { path = "../../crates/eframe", features = [ + "__screenshot", # __screenshot is so we can dump a ascreenshot using EFRAME_SCREENSHOT_TO +] } egui_glow = { path = "../../crates/egui_glow" } glow = "0.11" diff --git a/examples/custom_3d_glow/README.md b/examples/custom_3d_glow/README.md index 41a5e12b3..08f463678 100644 --- a/examples/custom_3d_glow/README.md +++ b/examples/custom_3d_glow/README.md @@ -13,3 +13,5 @@ If you are content of having egui sit on top of a 3D background, take a look at: ```sh cargo run -p custom_3d_glow ``` + +![](screenshot.png) diff --git a/examples/custom_3d_glow/screenshot.png b/examples/custom_3d_glow/screenshot.png new file mode 100644 index 000000000..cb33155b2 Binary files /dev/null and b/examples/custom_3d_glow/screenshot.png differ diff --git a/examples/custom_3d_three-d/Cargo.toml b/examples/custom_3d_three-d/Cargo.toml index 9ce3f9fd7..20527808c 100644 --- a/examples/custom_3d_three-d/Cargo.toml +++ b/examples/custom_3d_three-d/Cargo.toml @@ -11,7 +11,9 @@ publish = false crate-type = ["cdylib", "rlib"] [dependencies] -eframe = { path = "../../crates/eframe", features = ["glow"] } +eframe = { path = "../../crates/eframe", features = [ + "__screenshot", # __screenshot is so we can dump a ascreenshot using EFRAME_SCREENSHOT_TO +] } egui_glow = { path = "../../crates/egui_glow" } glow = "0.11" three-d = { version = "0.13", default-features = false } diff --git a/examples/custom_3d_three-d/README.md b/examples/custom_3d_three-d/README.md index 58c6d23fc..c708a6924 100644 --- a/examples/custom_3d_three-d/README.md +++ b/examples/custom_3d_three-d/README.md @@ -18,3 +18,5 @@ cargo run -p custom_3d_three-d ``` wasm-pack build examples/custom_3d_three-d --target web ``` + +![](screenshot.png) diff --git a/examples/custom_3d_three-d/screenshot.png b/examples/custom_3d_three-d/screenshot.png new file mode 100644 index 000000000..bdff41024 Binary files /dev/null and b/examples/custom_3d_three-d/screenshot.png differ diff --git a/examples/custom_font/Cargo.toml b/examples/custom_font/Cargo.toml index 29868ea23..58adfbec6 100644 --- a/examples/custom_font/Cargo.toml +++ b/examples/custom_font/Cargo.toml @@ -9,4 +9,6 @@ publish = false [dependencies] -eframe = { path = "../../crates/eframe" } +eframe = { path = "../../crates/eframe", features = [ + "__screenshot", # __screenshot is so we can dump a ascreenshot using EFRAME_SCREENSHOT_TO +] } diff --git a/examples/custom_font/README.md b/examples/custom_font/README.md index 20258b587..735098afb 100644 --- a/examples/custom_font/README.md +++ b/examples/custom_font/README.md @@ -1,3 +1,7 @@ +Example of how to use custom fonts. + ```sh cargo run -p custom_font ``` + +![](screenshot.png) diff --git a/examples/custom_font/screenshot.png b/examples/custom_font/screenshot.png new file mode 100644 index 000000000..abf60ca15 Binary files /dev/null and b/examples/custom_font/screenshot.png differ diff --git a/examples/custom_font/src/main.rs b/examples/custom_font/src/main.rs index f0e070ec5..0b763d13d 100644 --- a/examples/custom_font/src/main.rs +++ b/examples/custom_font/src/main.rs @@ -3,7 +3,10 @@ use eframe::egui; fn main() { - let options = eframe::NativeOptions::default(); + let options = eframe::NativeOptions { + initial_window_size: Some(egui::vec2(320.0, 240.0)), + ..Default::default() + }; eframe::run_native( "egui example: custom font", options, diff --git a/examples/custom_font_style/Cargo.toml b/examples/custom_font_style/Cargo.toml index 4db0565e0..9871910b0 100644 --- a/examples/custom_font_style/Cargo.toml +++ b/examples/custom_font_style/Cargo.toml @@ -9,4 +9,6 @@ publish = false [dependencies] -eframe = { path = "../../crates/eframe" } +eframe = { path = "../../crates/eframe", features = [ + "__screenshot", # __screenshot is so we can dump a ascreenshot using EFRAME_SCREENSHOT_TO +] } diff --git a/examples/custom_font_style/README.md b/examples/custom_font_style/README.md index 8b3575f61..66fdac230 100644 --- a/examples/custom_font_style/README.md +++ b/examples/custom_font_style/README.md @@ -1,4 +1,7 @@ +Example how to define custom test styles. + ```sh cargo run -p custom_font_style ``` -![custom_font_style example](custom_font_style.png) \ No newline at end of file + +![](screenshot.png) diff --git a/examples/custom_font_style/custom_font_style.png b/examples/custom_font_style/custom_font_style.png deleted file mode 100644 index 3baabb738..000000000 Binary files a/examples/custom_font_style/custom_font_style.png and /dev/null differ diff --git a/examples/custom_font_style/screenshot.png b/examples/custom_font_style/screenshot.png new file mode 100644 index 000000000..5ab1a298d Binary files /dev/null and b/examples/custom_font_style/screenshot.png differ diff --git a/examples/custom_window_frame/Cargo.toml b/examples/custom_window_frame/Cargo.toml index 5f5ea2c60..ef4d5878f 100644 --- a/examples/custom_window_frame/Cargo.toml +++ b/examples/custom_window_frame/Cargo.toml @@ -9,4 +9,6 @@ publish = false [dependencies] -eframe = { path = "../../crates/eframe" } +eframe = { path = "../../crates/eframe", features = [ + "__screenshot", # __screenshot is so we can dump a ascreenshot using EFRAME_SCREENSHOT_TO +] } diff --git a/examples/custom_window_frame/README.md b/examples/custom_window_frame/README.md index dc1d02a89..c300f408d 100644 --- a/examples/custom_window_frame/README.md +++ b/examples/custom_window_frame/README.md @@ -1,3 +1,7 @@ +Example how to show a custom window frame instead of the default OS window chrome decorations. + ```sh cargo run -p custom_window_frame ``` + +![](screenshot.png) diff --git a/examples/custom_window_frame/screenshot.png b/examples/custom_window_frame/screenshot.png new file mode 100644 index 000000000..afca4c0b6 Binary files /dev/null and b/examples/custom_window_frame/screenshot.png differ diff --git a/examples/custom_window_frame/src/main.rs b/examples/custom_window_frame/src/main.rs index 372fa7c77..10301cc30 100644 --- a/examples/custom_window_frame/src/main.rs +++ b/examples/custom_window_frame/src/main.rs @@ -11,6 +11,7 @@ fn main() { // To have rounded corners we need transparency: transparent: true, min_window_size: Some(egui::vec2(320.0, 100.0)), + initial_window_size: Some(egui::vec2(320.0, 240.0)), ..Default::default() }; eframe::run_native( diff --git a/examples/download_image/Cargo.toml b/examples/download_image/Cargo.toml index 87e0a8eb7..58f582836 100644 --- a/examples/download_image/Cargo.toml +++ b/examples/download_image/Cargo.toml @@ -9,7 +9,9 @@ publish = false [dependencies] -eframe = { path = "../../crates/eframe" } +eframe = { path = "../../crates/eframe", features = [ + "__screenshot", # __screenshot is so we can dump a ascreenshot using EFRAME_SCREENSHOT_TO +] } egui_extras = { path = "../../crates/egui_extras", features = ["image"] } ehttp = "0.2" image = { version = "0.24", default-features = false, features = ["jpeg"] } diff --git a/examples/download_image/README.md b/examples/download_image/README.md index a2123b584..936e1e058 100644 --- a/examples/download_image/README.md +++ b/examples/download_image/README.md @@ -1,3 +1,7 @@ +Example how to download and show an image with eframe/egui. + ```sh cargo run -p download_image ``` + +![](screenshot.png) diff --git a/examples/download_image/screenshot.png b/examples/download_image/screenshot.png new file mode 100644 index 000000000..919c4544f Binary files /dev/null and b/examples/download_image/screenshot.png differ diff --git a/examples/file_dialog/Cargo.toml b/examples/file_dialog/Cargo.toml index 6ac88b00e..f06f3f6b6 100644 --- a/examples/file_dialog/Cargo.toml +++ b/examples/file_dialog/Cargo.toml @@ -9,5 +9,7 @@ publish = false [dependencies] -eframe = { path = "../../crates/eframe" } +eframe = { path = "../../crates/eframe", features = [ + "__screenshot", # __screenshot is so we can dump a ascreenshot using EFRAME_SCREENSHOT_TO +] } rfd = "0.10" diff --git a/examples/file_dialog/README.md b/examples/file_dialog/README.md index 470004f1f..8dff71722 100644 --- a/examples/file_dialog/README.md +++ b/examples/file_dialog/README.md @@ -3,3 +3,5 @@ How to show a file dialog using [`rfd`](https://github.com/PolyMeilex/rfd). ```sh cargo run -p file_dialog ``` + +![](screenshot.png) diff --git a/examples/file_dialog/screenshot.png b/examples/file_dialog/screenshot.png new file mode 100644 index 000000000..f2b59d126 Binary files /dev/null and b/examples/file_dialog/screenshot.png differ diff --git a/examples/file_dialog/src/main.rs b/examples/file_dialog/src/main.rs index 4afa94926..501cae242 100644 --- a/examples/file_dialog/src/main.rs +++ b/examples/file_dialog/src/main.rs @@ -5,6 +5,7 @@ use eframe::egui; fn main() { let options = eframe::NativeOptions { drag_and_drop_support: true, + initial_window_size: Some(egui::vec2(320.0, 240.0)), ..Default::default() }; eframe::run_native( diff --git a/examples/hello_world/Cargo.toml b/examples/hello_world/Cargo.toml index d0c07a053..b83f65571 100644 --- a/examples/hello_world/Cargo.toml +++ b/examples/hello_world/Cargo.toml @@ -9,5 +9,7 @@ publish = false [dependencies] -eframe = { path = "../../crates/eframe" } +eframe = { path = "../../crates/eframe", features = [ + "__screenshot", # __screenshot is so we can dump a ascreenshot using EFRAME_SCREENSHOT_TO +] } tracing-subscriber = "0.3" diff --git a/examples/hello_world/README.md b/examples/hello_world/README.md index d528b1bc6..08c58a7d2 100644 --- a/examples/hello_world/README.md +++ b/examples/hello_world/README.md @@ -1,3 +1,7 @@ +Example showing some UI controls like `Label`, `TextEdit`, `Slider`, `Button`. + ```sh cargo run -p hello_world ``` + +![](screenshot.png) diff --git a/examples/hello_world/screenshot.png b/examples/hello_world/screenshot.png new file mode 100644 index 000000000..765339d40 Binary files /dev/null and b/examples/hello_world/screenshot.png differ diff --git a/examples/hello_world/src/main.rs b/examples/hello_world/src/main.rs index a1f91f058..a3e70a42b 100644 --- a/examples/hello_world/src/main.rs +++ b/examples/hello_world/src/main.rs @@ -6,7 +6,10 @@ fn main() { // Log to stdout (if you run with `RUST_LOG=debug`). tracing_subscriber::fmt::init(); - let options = eframe::NativeOptions::default(); + let options = eframe::NativeOptions { + initial_window_size: Some(egui::vec2(320.0, 240.0)), + ..Default::default() + }; eframe::run_native( "My egui App", options, diff --git a/examples/keyboard_events/Cargo.toml b/examples/keyboard_events/Cargo.toml index f22309427..693ff8b6a 100644 --- a/examples/keyboard_events/Cargo.toml +++ b/examples/keyboard_events/Cargo.toml @@ -9,5 +9,7 @@ publish = false [dependencies] -eframe = { path = "../../crates/eframe" } +eframe = { path = "../../crates/eframe", features = [ + "__screenshot", # __screenshot is so we can dump a ascreenshot using EFRAME_SCREENSHOT_TO +] } tracing-subscriber = "0.3" diff --git a/examples/keyboard_events/README.md b/examples/keyboard_events/README.md index d528b1bc6..afe67b250 100644 --- a/examples/keyboard_events/README.md +++ b/examples/keyboard_events/README.md @@ -1,3 +1,5 @@ ```sh cargo run -p hello_world ``` + +![](screenshot.png) diff --git a/examples/keyboard_events/screenshot.png b/examples/keyboard_events/screenshot.png new file mode 100644 index 000000000..b8ab11720 Binary files /dev/null and b/examples/keyboard_events/screenshot.png differ diff --git a/examples/puffin_profiler/Cargo.toml b/examples/puffin_profiler/Cargo.toml index d27c68eb0..955de856a 100644 --- a/examples/puffin_profiler/Cargo.toml +++ b/examples/puffin_profiler/Cargo.toml @@ -9,6 +9,8 @@ publish = false [dependencies] -eframe = { path = "../../crates/eframe", features = ["puffin"] } +eframe = { path = "../../crates/eframe", features = [ + "__screenshot", # __screenshot is so we can dump a ascreenshot using EFRAME_SCREENSHOT_TO +] } puffin = "0.14" puffin_http = "0.11" diff --git a/examples/puffin_profiler/README.md b/examples/puffin_profiler/README.md index a2c5085b3..4a9a6e461 100644 --- a/examples/puffin_profiler/README.md +++ b/examples/puffin_profiler/README.md @@ -7,3 +7,5 @@ cargo run -p puffin_profiler & cargo install puffin_viewer puffin_viewer --url 127.0.0.1:8585 ``` + +![](screenshot.png) diff --git a/examples/puffin_profiler/screenshot.png b/examples/puffin_profiler/screenshot.png new file mode 100644 index 000000000..fe974d851 Binary files /dev/null and b/examples/puffin_profiler/screenshot.png differ diff --git a/examples/retained_image/Cargo.toml b/examples/retained_image/Cargo.toml index d09bfae4f..b508ed243 100644 --- a/examples/retained_image/Cargo.toml +++ b/examples/retained_image/Cargo.toml @@ -9,6 +9,8 @@ publish = false [dependencies] -eframe = { path = "../../crates/eframe" } +eframe = { path = "../../crates/eframe", features = [ + "__screenshot", # __screenshot is so we can dump a ascreenshot using EFRAME_SCREENSHOT_TO +] } egui_extras = { path = "../../crates/egui_extras", features = ["image"] } image = { version = "0.24", default-features = false, features = ["png"] } diff --git a/examples/retained_image/README.md b/examples/retained_image/README.md index 2896e9ac2..122a7481a 100644 --- a/examples/retained_image/README.md +++ b/examples/retained_image/README.md @@ -1,3 +1,7 @@ +Example how to show an image with eframe/egui. + ```sh cargo run -p retained_image ``` + +![](screenshot.png) diff --git a/examples/retained_image/screenshot.png b/examples/retained_image/screenshot.png new file mode 100644 index 000000000..c605558da Binary files /dev/null and b/examples/retained_image/screenshot.png differ diff --git a/examples/retained_image/src/main.rs b/examples/retained_image/src/main.rs index b67a71d06..dc6b59c08 100644 --- a/examples/retained_image/src/main.rs +++ b/examples/retained_image/src/main.rs @@ -5,7 +5,7 @@ use egui_extras::RetainedImage; fn main() { let options = eframe::NativeOptions { - initial_window_size: Some(egui::vec2(500.0, 900.0)), + initial_window_size: Some(egui::vec2(300.0, 900.0)), ..Default::default() }; diff --git a/examples/screenshot/Cargo.toml b/examples/screenshot/Cargo.toml index 3be923e63..0056c1947 100644 --- a/examples/screenshot/Cargo.toml +++ b/examples/screenshot/Cargo.toml @@ -9,6 +9,8 @@ publish = false [dependencies] -eframe = { path = "../../crates/eframe" } +eframe = { path = "../../crates/eframe", features = [ + "__screenshot", # __screenshot is so we can dump a ascreenshot using EFRAME_SCREENSHOT_TO +] } egui_extras = { path = "../../crates/egui_extras", features = ["image"] } itertools = "0.10.3" diff --git a/examples/screenshot/README.md b/examples/screenshot/README.md index 416c8bbad..fb08d4263 100644 --- a/examples/screenshot/README.md +++ b/examples/screenshot/README.md @@ -1,3 +1,7 @@ +Example how to take screenshots and display them with eframe/egui. + ```sh cargo run -p screenshot ``` + +![](screenshot.png) diff --git a/examples/screenshot/screenshot.png b/examples/screenshot/screenshot.png new file mode 100644 index 000000000..27bf79908 Binary files /dev/null and b/examples/screenshot/screenshot.png differ diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs index 7dfaf7efb..1ac45460d 100644 --- a/examples/screenshot/src/main.rs +++ b/examples/screenshot/src/main.rs @@ -74,23 +74,25 @@ impl eframe::App for MyApp { self.take_screenshot = false; if let Some(gl) = frame.gl() { - let mut buf = vec![0u8; screen_size_px[0] as usize * screen_size_px[1] as usize * 4]; + let [w, h] = screen_size_px; + let mut buf = vec![0u8; w as usize * h as usize * 4]; let pixels = glow::PixelPackData::Slice(&mut buf[..]); unsafe { gl.read_pixels( 0, 0, - screen_size_px[0] as i32, - screen_size_px[1] as i32, + w as i32, + h as i32, glow::RGBA, glow::UNSIGNED_BYTE, pixels, ); } + // Flip vertically: let mut rows: Vec> = buf .into_iter() - .chunks(screen_size_px[0] as usize * 4) + .chunks(w as usize * 4) .into_iter() .map(|chunk| chunk.collect()) .collect(); diff --git a/examples/serial_windows/Cargo.toml b/examples/serial_windows/Cargo.toml index 16a90ec7d..7e585867e 100644 --- a/examples/serial_windows/Cargo.toml +++ b/examples/serial_windows/Cargo.toml @@ -9,4 +9,6 @@ publish = false [dependencies] -eframe = { path = "../../crates/eframe" } +eframe = { path = "../../crates/eframe", features = [ + "__screenshot", # __screenshot is so we can dump a ascreenshot using EFRAME_SCREENSHOT_TO +] } diff --git a/examples/serial_windows/README.md b/examples/serial_windows/README.md index 4941ce270..278023cdb 100644 --- a/examples/serial_windows/README.md +++ b/examples/serial_windows/README.md @@ -6,3 +6,5 @@ See also . ```sh cargo run -p serial_windows ``` + +![](screenshot.png) diff --git a/examples/serial_windows/screenshot.png b/examples/serial_windows/screenshot.png new file mode 100644 index 000000000..bf9c43607 Binary files /dev/null and b/examples/serial_windows/screenshot.png differ diff --git a/examples/serial_windows/src/main.rs b/examples/serial_windows/src/main.rs index 729811644..135443192 100644 --- a/examples/serial_windows/src/main.rs +++ b/examples/serial_windows/src/main.rs @@ -9,6 +9,7 @@ fn main() { let options = eframe::NativeOptions { run_and_return: true, + initial_window_size: Some(egui::vec2(320.0, 240.0)), ..Default::default() }; diff --git a/examples/svg/Cargo.toml b/examples/svg/Cargo.toml index 4bc9682d2..489692e5f 100644 --- a/examples/svg/Cargo.toml +++ b/examples/svg/Cargo.toml @@ -9,5 +9,7 @@ publish = false [dependencies] -eframe = { path = "../../crates/eframe" } +eframe = { path = "../../crates/eframe", features = [ + "__screenshot", # __screenshot is so we can dump a ascreenshot using EFRAME_SCREENSHOT_TO +] } egui_extras = { path = "../../crates/egui_extras", features = ["svg"] } diff --git a/examples/svg/README.md b/examples/svg/README.md index 7fedd1633..c171c1724 100644 --- a/examples/svg/README.md +++ b/examples/svg/README.md @@ -1,6 +1,7 @@ Example how to show an SVG image. - ```sh cargo run -p svg ``` + +![](screenshot.png) diff --git a/examples/svg/screenshot.png b/examples/svg/screenshot.png new file mode 100644 index 000000000..7b3d6222c Binary files /dev/null and b/examples/svg/screenshot.png differ diff --git a/sh/generate_example_screenshots.sh b/sh/generate_example_screenshots.sh new file mode 100755 index 000000000..fd45a6441 --- /dev/null +++ b/sh/generate_example_screenshots.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash +# This script generates screenshots for all the examples in examples/ + +set -eu +script_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P ) +cd "$script_path/.." + +cd examples +for VARIABLE in $(ls -1d */ | sed 's/\/$//'); do + EFRAME_SCREENSHOT_TO="$VARIABLE/screenshot.png" cargo run -p $VARIABLE +done