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,14 +1,12 @@
|
||||
use eframe::{
|
||||
egui::{Button, CentralPanel, Context, UserAttentionType},
|
||||
CreationContext, NativeOptions,
|
||||
};
|
||||
use eframe::{egui, CreationContext, NativeOptions};
|
||||
use egui::{Button, CentralPanel, Context, UserAttentionType};
|
||||
|
||||
use std::time::{Duration, SystemTime};
|
||||
|
||||
fn main() -> eframe::Result<()> {
|
||||
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
|
||||
let native_options = NativeOptions {
|
||||
initial_window_size: Some(eframe::egui::vec2(400., 200.)),
|
||||
initial_window_size: Some(egui::vec2(400., 200.)),
|
||||
..Default::default()
|
||||
};
|
||||
eframe::run_native(
|
||||
@@ -54,11 +52,11 @@ impl Application {
|
||||
}
|
||||
|
||||
impl eframe::App for Application {
|
||||
fn update(&mut self, ctx: &Context, frame: &mut eframe::Frame) {
|
||||
fn update(&mut self, ctx: &Context, _frame: &mut eframe::Frame) {
|
||||
if let Some(request_at) = self.request_at {
|
||||
if request_at < SystemTime::now() {
|
||||
self.request_at = None;
|
||||
frame.request_user_attention(self.attention);
|
||||
ctx.send_viewport_cmd(egui::ViewportCommand::RequestUserAttention(self.attention));
|
||||
if self.auto_reset {
|
||||
self.auto_reset = false;
|
||||
self.reset_at = Some(SystemTime::now() + Self::attention_reset_timeout());
|
||||
@@ -69,7 +67,9 @@ impl eframe::App for Application {
|
||||
if let Some(reset_at) = self.reset_at {
|
||||
if reset_at < SystemTime::now() {
|
||||
self.reset_at = None;
|
||||
frame.request_user_attention(UserAttentionType::Reset);
|
||||
ctx.send_viewport_cmd(egui::ViewportCommand::RequestUserAttention(
|
||||
UserAttentionType::Reset,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,7 +77,7 @@ impl eframe::App for Application {
|
||||
ui.vertical(|ui| {
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Attention type:");
|
||||
eframe::egui::ComboBox::new("attention", "")
|
||||
egui::ComboBox::new("attention", "")
|
||||
.selected_text(repr(self.attention))
|
||||
.show_ui(ui, |ui| {
|
||||
for kind in [
|
||||
|
||||
Reference in New Issue
Block a user