mirror of
https://github.com/emilk/egui.git
synced 2026-08-29 04:40:03 -04:00
Replace eframe::Frame commands and WindowInfo with egui (#3564)
* Part of https://github.com/emilk/egui/issues/3556 ## In short You now almost never need to use `eframe::Frame` - instead use `ui.input(|i| i.viewport())` for information about the current viewport (native window), and use `ctx.send_viewport_cmd` to modify it. ## In detail This PR removes most commands from `eframe::Frame`, and replaces them with `ViewportCommand`. So `frame.close()` becomes `ctx.send_viewport_cmd(ViewportCommand::Close)`, etc. `frame.info().window_info` is now also gone, replaced with `ui.input(|i| i.viewport())`. `frame.info().native_pixels_per_point` is replaced with `ui.input(|i| i.raw.native_pixels_per_point)`. `RawInput` now contains one `ViewportInfo` for each viewport. Screenshots are taken with `ctx.send_viewport_cmd(ViewportCommand::Screenshots)` and are returned in `egui::Event` which you can check with: ``` ust ui.input(|i| { for event in &i.raw.events { if let egui::Event::Screenshot { viewport_id, image } = event { // handle it here } } }); ``` ### Motivation You no longer need to pass around the `&eframe::Frame` everywhere. This also opens the door for other integrations to use the same API of `ViewportCommand`s.
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use eframe::egui::{self, ColorImage};
|
||||
|
||||
fn main() -> Result<(), eframe::Error> {
|
||||
@@ -19,12 +21,12 @@ fn main() -> Result<(), eframe::Error> {
|
||||
struct MyApp {
|
||||
continuously_take_screenshots: bool,
|
||||
texture: Option<egui::TextureHandle>,
|
||||
screenshot: Option<ColorImage>,
|
||||
screenshot: Option<Arc<ColorImage>>,
|
||||
save_to_file: bool,
|
||||
}
|
||||
|
||||
impl eframe::App for MyApp {
|
||||
fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
|
||||
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
if let Some(screenshot) = self.screenshot.take() {
|
||||
self.texture = Some(ui.ctx().load_texture(
|
||||
@@ -42,7 +44,7 @@ impl eframe::App for MyApp {
|
||||
|
||||
if ui.button("save to 'top_left.png'").clicked() {
|
||||
self.save_to_file = true;
|
||||
frame.request_screenshot();
|
||||
ctx.send_viewport_cmd(egui::ViewportCommand::Screenshot);
|
||||
}
|
||||
|
||||
ui.with_layout(egui::Layout::top_down(egui::Align::RIGHT), |ui| {
|
||||
@@ -55,9 +57,9 @@ impl eframe::App for MyApp {
|
||||
} else {
|
||||
ctx.set_visuals(egui::Visuals::light());
|
||||
};
|
||||
frame.request_screenshot();
|
||||
ctx.send_viewport_cmd(egui::ViewportCommand::Screenshot);
|
||||
} else if ui.button("take screenshot!").clicked() {
|
||||
frame.request_screenshot();
|
||||
ctx.send_viewport_cmd(egui::ViewportCommand::Screenshot);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -68,28 +70,33 @@ impl eframe::App for MyApp {
|
||||
ui.spinner();
|
||||
}
|
||||
|
||||
// Check for returned screenshot:
|
||||
ui.input(|i| {
|
||||
for event in &i.raw.events {
|
||||
if let egui::Event::Screenshot { image, .. } = event {
|
||||
if self.save_to_file {
|
||||
let pixels_per_point = i.pixels_per_point();
|
||||
let region = egui::Rect::from_two_pos(
|
||||
egui::Pos2::ZERO,
|
||||
egui::Pos2 { x: 100., y: 100. },
|
||||
);
|
||||
let top_left_corner = image.region(®ion, Some(pixels_per_point));
|
||||
image::save_buffer(
|
||||
"top_left.png",
|
||||
top_left_corner.as_raw(),
|
||||
top_left_corner.width() as u32,
|
||||
top_left_corner.height() as u32,
|
||||
image::ColorType::Rgba8,
|
||||
)
|
||||
.unwrap();
|
||||
self.save_to_file = false;
|
||||
}
|
||||
self.screenshot = Some(image.clone());
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
ctx.request_repaint();
|
||||
});
|
||||
}
|
||||
|
||||
fn post_rendering(&mut self, _window_size: [u32; 2], frame: &eframe::Frame) {
|
||||
if let Some(screenshot) = frame.screenshot() {
|
||||
if self.save_to_file {
|
||||
let pixels_per_point = frame.info().native_pixels_per_point;
|
||||
let region =
|
||||
egui::Rect::from_two_pos(egui::Pos2::ZERO, egui::Pos2 { x: 100., y: 100. });
|
||||
let top_left_corner = screenshot.region(®ion, pixels_per_point);
|
||||
image::save_buffer(
|
||||
"top_left.png",
|
||||
top_left_corner.as_raw(),
|
||||
top_left_corner.width() as u32,
|
||||
top_left_corner.height() as u32,
|
||||
image::ColorType::Rgba8,
|
||||
)
|
||||
.unwrap();
|
||||
self.save_to_file = false;
|
||||
}
|
||||
self.screenshot = Some(screenshot);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user