mirror of
https://github.com/emilk/egui.git
synced 2026-09-02 23:00:04 -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:
@@ -24,58 +24,60 @@ pub(crate) enum AppKind<'a, State> {
|
||||
impl<State> AppKind<'_, State> {
|
||||
pub fn run(
|
||||
&mut self,
|
||||
ctx: &egui::Context,
|
||||
ui: &mut egui::Ui,
|
||||
state: &mut State,
|
||||
sizing_pass: bool,
|
||||
) -> Option<egui::Response> {
|
||||
match self {
|
||||
AppKind::Context(f) => {
|
||||
debug_assert!(!sizing_pass, "Context closures cannot do a sizing pass");
|
||||
f(ctx);
|
||||
f(ui);
|
||||
None
|
||||
}
|
||||
AppKind::ContextState(f) => {
|
||||
debug_assert!(!sizing_pass, "Context closures cannot do a sizing pass");
|
||||
f(ctx, state);
|
||||
f(ui, state);
|
||||
None
|
||||
}
|
||||
#[cfg(feature = "eframe")]
|
||||
AppKind::Eframe((get_app, frame)) => {
|
||||
let app = get_app(state);
|
||||
app.update(ctx, frame);
|
||||
|
||||
app.logic(ui, frame);
|
||||
|
||||
#[expect(deprecated)]
|
||||
app.update(ui, frame);
|
||||
|
||||
app.ui(ui, frame);
|
||||
|
||||
None
|
||||
}
|
||||
kind_ui => Some(kind_ui.run_ui(ctx, state, sizing_pass)),
|
||||
kind_ui => Some(kind_ui.run_ui(ui, state, sizing_pass)),
|
||||
}
|
||||
}
|
||||
|
||||
fn run_ui(
|
||||
&mut self,
|
||||
ctx: &egui::Context,
|
||||
ui: &mut egui::Ui,
|
||||
state: &mut State,
|
||||
sizing_pass: bool,
|
||||
) -> egui::Response {
|
||||
egui::CentralPanel::default()
|
||||
.frame(Frame::NONE)
|
||||
.show(ctx, |ui| {
|
||||
let mut builder = egui::UiBuilder::new();
|
||||
if sizing_pass {
|
||||
builder.sizing_pass = true;
|
||||
}
|
||||
ui.scope_builder(builder, |ui| {
|
||||
Frame::central_panel(ui.style())
|
||||
.outer_margin(8.0)
|
||||
.inner_margin(0.0)
|
||||
.show(ui, |ui| match self {
|
||||
AppKind::Ui(f) => f(ui),
|
||||
AppKind::UiState(f) => f(ui, state),
|
||||
_ => unreachable!(
|
||||
"run_ui should only be called with AppKind::Ui or AppKind UiState"
|
||||
),
|
||||
});
|
||||
})
|
||||
.response
|
||||
})
|
||||
.inner
|
||||
let mut builder = egui::UiBuilder::new();
|
||||
if sizing_pass {
|
||||
builder.sizing_pass = true;
|
||||
}
|
||||
ui.scope_builder(builder, |ui| {
|
||||
Frame::central_panel(ui.style())
|
||||
.outer_margin(8.0)
|
||||
.inner_margin(0.0)
|
||||
.show(ui, |ui| match self {
|
||||
AppKind::Ui(f) => f(ui),
|
||||
AppKind::UiState(f) => f(ui, state),
|
||||
_ => unreachable!(
|
||||
"run_ui should only be called with AppKind::Ui or AppKind UiState"
|
||||
),
|
||||
});
|
||||
})
|
||||
.response
|
||||
}
|
||||
}
|
||||
|
||||
@@ -167,6 +167,7 @@ impl<State> HarnessBuilder<State> {
|
||||
/// assert_eq!(*harness.state(), true);
|
||||
/// ```
|
||||
#[track_caller]
|
||||
#[deprecated = "use `build_ui_state` instead"]
|
||||
pub fn build_state<'a>(
|
||||
self,
|
||||
app: impl FnMut(&egui::Context, &mut State) + 'a,
|
||||
@@ -251,6 +252,7 @@ impl HarnessBuilder {
|
||||
/// ```
|
||||
#[must_use]
|
||||
#[track_caller]
|
||||
#[deprecated = "use `build_ui` instead"]
|
||||
pub fn build<'a>(self, app: impl FnMut(&egui::Context) + 'a) -> Harness<'a> {
|
||||
Harness::from_builder(self, AppKind::Context(Box::new(app)), (), None)
|
||||
}
|
||||
|
||||
@@ -137,8 +137,8 @@ impl<'a, State> Harness<'a, State> {
|
||||
|
||||
// We need to run egui for a single frame so that the AccessKit state can be initialized
|
||||
// and users can immediately start querying for widgets.
|
||||
let mut output = ctx.run(input.clone(), |ctx| {
|
||||
response = app.run(ctx, &mut state, false);
|
||||
let mut output = ctx.run_ui(input.clone(), |ui| {
|
||||
response = app.run(ui, &mut state, false);
|
||||
});
|
||||
|
||||
renderer.handle_delta(&output.textures_delta);
|
||||
@@ -204,7 +204,9 @@ impl<'a, State> Harness<'a, State> {
|
||||
/// assert_eq!(*harness.state(), true);
|
||||
/// ```
|
||||
#[track_caller]
|
||||
#[deprecated = "use `new_ui_state` instead"]
|
||||
pub fn new_state(app: impl FnMut(&egui::Context, &mut State) + 'a, state: State) -> Self {
|
||||
#[expect(deprecated)]
|
||||
Self::builder().build_state(app, state)
|
||||
}
|
||||
|
||||
@@ -287,8 +289,8 @@ impl<'a, State> Harness<'a, State> {
|
||||
fn _step(&mut self, sizing_pass: bool) {
|
||||
self.input.predicted_dt = self.step_dt;
|
||||
|
||||
let mut output = self.ctx.run(self.input.take(), |ctx| {
|
||||
self.response = self.app.run(ctx, &mut self.state, sizing_pass);
|
||||
let mut output = self.ctx.run_ui(self.input.take(), |ui| {
|
||||
self.response = self.app.run(ui, &mut self.state, sizing_pass);
|
||||
});
|
||||
self.kittest.update(
|
||||
output
|
||||
@@ -735,7 +737,9 @@ impl<'a> Harness<'a> {
|
||||
/// });
|
||||
/// ```
|
||||
#[track_caller]
|
||||
#[deprecated = "use `new_ui` instead"]
|
||||
pub fn new(app: impl FnMut(&egui::Context) + 'a) -> Self {
|
||||
#[expect(deprecated)]
|
||||
Self::builder().build(app)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! Tests the accesskit accessibility output of egui.
|
||||
|
||||
use egui::{
|
||||
CentralPanel, Context, RawInput, Window,
|
||||
CentralPanel, Context, RawInput, Ui, Window,
|
||||
accesskit::{NodeId, Role, TreeUpdate},
|
||||
};
|
||||
|
||||
@@ -12,8 +12,8 @@ use egui::{
|
||||
/// are put there because of the widgets rendered.
|
||||
#[test]
|
||||
fn empty_ui_should_return_tree_with_only_root_window() {
|
||||
let output = accesskit_output_single_egui_frame(|ctx| {
|
||||
CentralPanel::default().show(ctx, |_| {});
|
||||
let output = accesskit_output_single_egui_frame(|_ui| {
|
||||
// Nothing here beyond the default empty UI
|
||||
});
|
||||
|
||||
assert_eq!(
|
||||
@@ -42,8 +42,8 @@ fn empty_ui_should_return_tree_with_only_root_window() {
|
||||
fn button_node() {
|
||||
let button_text = "This is a test button!";
|
||||
|
||||
let output = accesskit_output_single_egui_frame(|ctx| {
|
||||
CentralPanel::default().show(ctx, |ui| ui.button(button_text));
|
||||
let output = accesskit_output_single_egui_frame(|ui| {
|
||||
CentralPanel::default().show_inside(ui, |ui| ui.button(button_text));
|
||||
});
|
||||
|
||||
let (_, button) = output
|
||||
@@ -60,8 +60,8 @@ fn button_node() {
|
||||
fn disabled_button_node() {
|
||||
let button_text = "This is a test button!";
|
||||
|
||||
let output = accesskit_output_single_egui_frame(|ctx| {
|
||||
CentralPanel::default().show(ctx, |ui| {
|
||||
let output = accesskit_output_single_egui_frame(|ui| {
|
||||
CentralPanel::default().show_inside(ui, |ui| {
|
||||
ui.add_enabled(false, egui::Button::new(button_text))
|
||||
});
|
||||
});
|
||||
@@ -81,8 +81,8 @@ fn toggle_button_node() {
|
||||
let button_text = "A toggle button";
|
||||
|
||||
let mut selected = false;
|
||||
let output = accesskit_output_single_egui_frame(|ctx| {
|
||||
CentralPanel::default().show(ctx, |ui| ui.toggle_value(&mut selected, button_text));
|
||||
let output = accesskit_output_single_egui_frame(|ui| {
|
||||
CentralPanel::default().show_inside(ui, |ui| ui.toggle_value(&mut selected, button_text));
|
||||
});
|
||||
|
||||
let (_, toggle) = output
|
||||
@@ -97,8 +97,8 @@ fn toggle_button_node() {
|
||||
|
||||
#[test]
|
||||
fn multiple_disabled_widgets() {
|
||||
let output = accesskit_output_single_egui_frame(|ctx| {
|
||||
CentralPanel::default().show(ctx, |ui| {
|
||||
let output = accesskit_output_single_egui_frame(|ui| {
|
||||
CentralPanel::default().show_inside(ui, |ui| {
|
||||
ui.add_enabled_ui(false, |ui| {
|
||||
let _ = ui.button("Button 1");
|
||||
let _ = ui.button("Button 2");
|
||||
@@ -120,12 +120,12 @@ fn multiple_disabled_widgets() {
|
||||
|
||||
#[test]
|
||||
fn window_children() {
|
||||
let output = accesskit_output_single_egui_frame(|ctx| {
|
||||
let output = accesskit_output_single_egui_frame(|ui| {
|
||||
let mut open = true;
|
||||
Window::new("test window")
|
||||
.open(&mut open)
|
||||
.resizable(false)
|
||||
.show(ctx, |ui| {
|
||||
.show(ui.ctx(), |ui| {
|
||||
let _ = ui.button("A button");
|
||||
});
|
||||
});
|
||||
@@ -138,13 +138,13 @@ fn window_children() {
|
||||
assert_button_exists(&output, "Hide", window_id);
|
||||
}
|
||||
|
||||
fn accesskit_output_single_egui_frame(run_ui: impl FnMut(&Context)) -> TreeUpdate {
|
||||
fn accesskit_output_single_egui_frame(run_ui: impl FnMut(&mut Ui)) -> TreeUpdate {
|
||||
let ctx = Context::default();
|
||||
// Disable animations, so we do not need to wait for animations to end to see the result.
|
||||
ctx.global_style_mut(|style| style.animation_time = 0.0);
|
||||
ctx.enable_accesskit();
|
||||
|
||||
let output = ctx.run(RawInput::default(), run_ui);
|
||||
let output = ctx.run_ui(RawInput::default(), run_ui);
|
||||
|
||||
output
|
||||
.platform_output
|
||||
|
||||
Reference in New Issue
Block a user