mirror of
https://github.com/emilk/egui.git
synced 2026-09-01 06:10:06 -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:
@@ -117,7 +117,7 @@ impl BackendPanel {
|
||||
{
|
||||
ui.separator();
|
||||
if ui.button("Quit").clicked() {
|
||||
frame.close();
|
||||
ui.ctx().send_viewport_cmd(egui::ViewportCommand::Close);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -151,20 +151,21 @@ impl BackendPanel {
|
||||
// On web, the browser controls `pixels_per_point`.
|
||||
let integration_controls_pixels_per_point = frame.is_web();
|
||||
if !integration_controls_pixels_per_point {
|
||||
self.pixels_per_point_ui(ui, frame.info());
|
||||
self.pixels_per_point_ui(ui);
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
ui.horizontal(|ui| {
|
||||
{
|
||||
let mut fullscreen = frame.info().window_info.fullscreen;
|
||||
let mut fullscreen = ui.input(|i| i.viewport().fullscreen.unwrap_or(false));
|
||||
if ui
|
||||
.checkbox(&mut fullscreen, "🗖 Fullscreen (F11)")
|
||||
.on_hover_text("Fullscreen the window")
|
||||
.changed()
|
||||
{
|
||||
frame.set_fullscreen(fullscreen);
|
||||
ui.ctx()
|
||||
.send_viewport_cmd(egui::ViewportCommand::Fullscreen(fullscreen));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -173,29 +174,29 @@ impl BackendPanel {
|
||||
.on_hover_text("Resize the window to be small like a phone.")
|
||||
.clicked()
|
||||
{
|
||||
// frame.set_window_size(egui::vec2(375.0, 812.0)); // iPhone 12 mini
|
||||
frame.set_window_size(egui::vec2(375.0, 667.0)); // iPhone SE 2nd gen
|
||||
frame.set_fullscreen(false);
|
||||
// let size = egui::vec2(375.0, 812.0); // iPhone 12 mini
|
||||
let size = egui::vec2(375.0, 667.0); // iPhone SE 2nd gen
|
||||
|
||||
ui.ctx()
|
||||
.send_viewport_cmd(egui::ViewportCommand::InnerSize(size));
|
||||
ui.ctx()
|
||||
.send_viewport_cmd(egui::ViewportCommand::Fullscreen(false));
|
||||
ui.close_menu();
|
||||
}
|
||||
});
|
||||
|
||||
if !frame.info().window_info.fullscreen
|
||||
let fullscreen = ui.input(|i| i.viewport().fullscreen.unwrap_or(false));
|
||||
if !fullscreen
|
||||
&& ui
|
||||
.button("Drag me to drag window")
|
||||
.is_pointer_button_down_on()
|
||||
{
|
||||
frame.drag_window();
|
||||
ui.ctx().send_viewport_cmd(egui::ViewportCommand::StartDrag);
|
||||
}
|
||||
|
||||
ui.button("Native window info (hover me)")
|
||||
.on_hover_ui(|ui| {
|
||||
window_info_ui(ui, &frame.info().window_info);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
fn pixels_per_point_ui(&mut self, ui: &mut egui::Ui, info: &eframe::IntegrationInfo) {
|
||||
fn pixels_per_point_ui(&mut self, ui: &mut egui::Ui) {
|
||||
let pixels_per_point = self
|
||||
.pixels_per_point
|
||||
.get_or_insert_with(|| ui.ctx().pixels_per_point());
|
||||
@@ -223,7 +224,7 @@ impl BackendPanel {
|
||||
reset = true;
|
||||
}
|
||||
|
||||
if let Some(native_pixels_per_point) = info.native_pixels_per_point {
|
||||
if let Some(native_pixels_per_point) = ui.input(|i| i.raw.native_pixels_per_point) {
|
||||
let enabled = ui.ctx().pixels_per_point() != native_pixels_per_point;
|
||||
if ui
|
||||
.add_enabled(enabled, egui::Button::new("Reset"))
|
||||
@@ -285,55 +286,6 @@ impl BackendPanel {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn window_info_ui(ui: &mut egui::Ui, window_info: &eframe::WindowInfo) {
|
||||
let eframe::WindowInfo {
|
||||
position,
|
||||
fullscreen,
|
||||
minimized,
|
||||
maximized,
|
||||
focused,
|
||||
size,
|
||||
monitor_size,
|
||||
} = window_info;
|
||||
|
||||
egui::Grid::new("window_info_grid")
|
||||
.num_columns(2)
|
||||
.show(ui, |ui| {
|
||||
if let Some(egui::Pos2 { x, y }) = position {
|
||||
ui.label("Position:");
|
||||
ui.monospace(format!("{x:.0}, {y:.0}"));
|
||||
ui.end_row();
|
||||
}
|
||||
|
||||
ui.label("Fullscreen:");
|
||||
ui.label(fullscreen.to_string());
|
||||
ui.end_row();
|
||||
|
||||
ui.label("Minimized:");
|
||||
ui.label(minimized.to_string());
|
||||
ui.end_row();
|
||||
|
||||
ui.label("Maximized:");
|
||||
ui.label(maximized.to_string());
|
||||
ui.end_row();
|
||||
|
||||
ui.label("Focused:");
|
||||
ui.label(focused.to_string());
|
||||
ui.end_row();
|
||||
|
||||
ui.label("Window size:");
|
||||
ui.monospace(format!("{x:.0} x {y:.0}", x = size.x, y = size.y));
|
||||
ui.end_row();
|
||||
|
||||
if let Some(egui::Vec2 { x, y }) = monitor_size {
|
||||
ui.label("Monitor size:");
|
||||
ui.monospace(format!("{x:.0} x {y:.0}"));
|
||||
ui.end_row();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
|
||||
|
||||
@@ -259,7 +259,8 @@ impl eframe::App for WrapApp {
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
if ctx.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::F11)) {
|
||||
frame.set_fullscreen(!frame.info().window_info.fullscreen);
|
||||
let fullscreen = ctx.input(|i| i.viewport().fullscreen.unwrap_or(false));
|
||||
ctx.send_viewport_cmd(egui::ViewportCommand::Fullscreen(!fullscreen));
|
||||
}
|
||||
|
||||
let mut cmd = Command::Nothing;
|
||||
@@ -284,7 +285,7 @@ impl eframe::App for WrapApp {
|
||||
|
||||
// On web, the browser controls `pixels_per_point`.
|
||||
if !frame.is_web() {
|
||||
egui::gui_zoom::zoom_with_keyboard_shortcuts(ctx, frame.info().native_pixels_per_point);
|
||||
egui::gui_zoom::zoom_with_keyboard_shortcuts(ctx);
|
||||
}
|
||||
|
||||
self.run_cmd(ctx, cmd);
|
||||
|
||||
Reference in New Issue
Block a user