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

eframe: Replace Frame::update with fn logic and fn ui (#7775)

* Part of https://github.com/emilk/egui/issues/5113
* Part of https://github.com/emilk/egui/issues/3524

## What
This deprecates `eframe::App::update` and replaces it with two new
functions:

```rs
pub trait App {
	/// Called just before `ui`, and in the future this will
    /// also be called for background apps when needed.
	fn logic(&mut self, ctx: &egui::Context, frame: &mut Frame) { }
	
    /// Show your user interface to the user.
	fn ui(&mut self, ui: &mut egui::Ui, frame: &mut Frame);

	…
}
```

Similarly, `Context::run` is deprecated in favor of `Context::run_ui`.

`Plugin`s are now handed a `Ui` instead of just a `Context` in
`on_begin/end_frame`.

## TODO
…either in this PR or a later one
* [x] Deprecate `App::update`
* [x] Deprecate `Context::run`
* [x] Change plugins to get a `Ui`
* [x] Update kittest
* [x] Change viewports to get UI:s (`show_viewport_immediate` etc)
  - https://github.com/emilk/egui/pull/7779

## Later PRs
* [ ] Deprecate `Panel::show`
* [ ] Deprecate `CentralPanel::show`
* [ ] Deprecate `CentralPanel` ?
This commit is contained in:
Emil Ernerfeldt
2025-12-16 17:05:50 +01:00
committed by GitHub
parent 9487dc35ec
commit 2f6fe9c572
52 changed files with 417 additions and 321 deletions

View File

@@ -66,10 +66,10 @@ pub fn criterion_benchmark(c: &mut Criterion) {
{
let ctx = egui::Context::default();
let _ = ctx.run(RawInput::default(), |ctx| {
let _ = ctx.run_ui(RawInput::default(), |ui| {
c.bench_function("label &str", |b| {
b.iter_batched_ref(
|| create_benchmark_ui(ctx),
|| create_benchmark_ui(ui),
|ui| {
ui.label("the quick brown fox jumps over the lazy dog");
},
@@ -78,7 +78,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
});
c.bench_function("label format!", |b| {
b.iter_batched_ref(
|| create_benchmark_ui(ctx),
|| create_benchmark_ui(ui),
|ui| {
ui.label("the quick brown fox jumps over the lazy dog".to_owned());
},
@@ -90,7 +90,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
{
let ctx = egui::Context::default();
let _ = ctx.run(RawInput::default(), |ctx| {
let _ = ctx.run_ui(RawInput::default(), |ui| {
let mut group = c.benchmark_group("button");
// To ensure we have a valid image, let's use the font texture. The size
@@ -99,7 +99,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
group.bench_function("1_button_text", |b| {
b.iter_batched_ref(
|| create_benchmark_ui(ctx),
|| create_benchmark_ui(ui),
|ui| {
ui.add(Button::new("Hello World"));
},
@@ -108,7 +108,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
});
group.bench_function("2_button_text_image", |b| {
b.iter_batched_ref(
|| create_benchmark_ui(ctx),
|| create_benchmark_ui(ui),
|ui| {
ui.add(Button::image_and_text(image, "Hello World"));
},
@@ -117,7 +117,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
});
group.bench_function("3_button_text_image_right_text", |b| {
b.iter_batched_ref(
|| create_benchmark_ui(ctx),
|| create_benchmark_ui(ui),
|ui| {
ui.add(Button::image_and_text(image, "Hello World").right_text(""));
},
@@ -126,7 +126,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
});
group.bench_function("4_button_italic", |b| {
b.iter_batched_ref(
|| create_benchmark_ui(ctx),
|| create_benchmark_ui(ui),
|ui| {
ui.add(Button::new(RichText::new("Hello World").italics()));
},

View File

@@ -372,7 +372,7 @@ fn file_menu_button(ui: &mut Ui) {
mod tests {
use crate::{Demo as _, demo::demo_app_windows::DemoGroups};
use egui_kittest::kittest::{NodeT as _, Queryable as _};
use egui_kittest::kittest::Queryable as _;
use egui_kittest::{Harness, OsThreshold, SnapshotOptions, SnapshotResults};
#[test]
@@ -394,12 +394,15 @@ mod tests {
let name = remove_leading_emoji(demo.name());
let mut harness = Harness::new(|ctx| {
egui_extras::install_image_loaders(ctx);
demo.show(ctx, &mut true);
let mut harness = Harness::new_ui(|ui| {
egui_extras::install_image_loaders(ui);
demo.show(ui, &mut true);
});
let window = harness.queryable_node().children().next().unwrap();
let window = harness
.get_all_by_role(egui::accesskit::Role::Window)
.next()
.unwrap();
// TODO(lucasmerlin): Windows should probably have a label?
//let window = harness.get_by_label(name);

View File

@@ -176,9 +176,9 @@ mod tests {
..Modals::default()
};
let mut harness = Harness::new_state(
|ctx, modals| {
modals.show(ctx, &mut true);
let mut harness = Harness::new_ui_state(
|ui, modals| {
modals.show(ui, &mut true);
},
initial_state,
);
@@ -204,9 +204,9 @@ mod tests {
..Modals::default()
};
let mut harness = Harness::new_state(
|ctx, modals| {
modals.show(ctx, &mut true);
let mut harness = Harness::new_ui_state(
|ui, modals| {
modals.show(ui, &mut true);
},
initial_state,
);
@@ -228,9 +228,9 @@ mod tests {
..Modals::default()
};
let mut harness = Harness::new_state(
|ctx, modals| {
modals.show(ctx, &mut true);
let mut harness = Harness::new_ui_state(
|ui, modals| {
modals.show(ui, &mut true);
},
initial_state,
);
@@ -258,9 +258,9 @@ mod tests {
..Modals::default()
};
let mut harness = Harness::new_state(
|ctx, modals| {
modals.show(ctx, &mut true);
let mut harness = Harness::new_ui_state(
|ui, modals| {
modals.show(ui, &mut true);
},
initial_state,
);

View File

@@ -120,9 +120,9 @@ mod tests {
#[test]
pub fn should_type() {
let text = "Hello, world!".to_owned();
let mut harness = Harness::new_state(
move |ctx, text| {
CentralPanel::default().show(ctx, |ui| {
let mut harness = Harness::new_ui_state(
move |ui, text| {
CentralPanel::default().show_inside(ui, |ui| {
ui.text_edit_singleline(text);
});
},