1
0
mirror of https://github.com/emilk/egui.git synced 2026-06-26 22:53:14 -04:00

Deprecate CentralPanel::show (#7783)

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

Use `show_inside` instead, with a `Ui` instead of a `Context`
This commit is contained in:
Emil Ernerfeldt
2025-12-17 16:39:13 +01:00
committed by GitHub
parent 53ec7333c3
commit 6157a35985
5 changed files with 46 additions and 31 deletions

View File

@@ -1044,6 +1044,7 @@ impl CentralPanel {
} }
/// Show the panel at the top level. /// Show the panel at the top level.
#[deprecated = "Use show_inside() instead"]
pub fn show<R>( pub fn show<R>(
self, self,
ctx: &Context, ctx: &Context,

View File

@@ -19,8 +19,8 @@ use crate::{
ImmediateViewportRendererCallback, Key, KeyboardShortcut, Label, LayerId, Memory, ImmediateViewportRendererCallback, Key, KeyboardShortcut, Label, LayerId, Memory,
ModifierNames, Modifiers, NumExt as _, Order, Painter, RawInput, Response, RichText, ModifierNames, Modifiers, NumExt as _, Order, Painter, RawInput, Response, RichText,
SafeAreaInsets, ScrollArea, Sense, Style, TextStyle, TextureHandle, TextureOptions, Ui, SafeAreaInsets, ScrollArea, Sense, Style, TextStyle, TextureHandle, TextureOptions, Ui,
ViewportBuilder, ViewportCommand, ViewportId, ViewportIdMap, ViewportIdPair, ViewportIdSet, UiBuilder, ViewportBuilder, ViewportCommand, ViewportId, ViewportIdMap, ViewportIdPair,
ViewportOutput, Visuals, Widget as _, WidgetRect, WidgetText, ViewportIdSet, ViewportOutput, Visuals, Widget as _, WidgetRect, WidgetText,
animation_manager::AnimationManager, animation_manager::AnimationManager,
containers::{self, area::AreaState}, containers::{self, area::AreaState},
data::output::PlatformOutput, data::output::PlatformOutput,
@@ -793,11 +793,23 @@ impl Context {
let plugins = self.read(|ctx| ctx.plugins.ordered_plugins()); let plugins = self.read(|ctx| ctx.plugins.ordered_plugins());
#[expect(deprecated)] #[expect(deprecated)]
self.run(new_input, |ctx| { self.run(new_input, |ctx| {
crate::CentralPanel::no_frame().show(ctx, |ui| { let mut top_ui = Ui::new(
plugins.on_begin_pass(ui); ctx.clone(),
run_ui(ui); Id::new((ctx.viewport_id(), "__top_ui")),
plugins.on_end_pass(ui); UiBuilder::new()
}); .layer_id(LayerId::background())
.max_rect(ctx.globally_available_rect().round_ui()),
);
{
plugins.on_begin_pass(&mut top_ui);
run_ui(&mut top_ui);
plugins.on_end_pass(&mut top_ui);
}
// Inform ctx about what we actually used, so we can shrink the native window to fit.
// TODO(emilk): make better use of this somehow
ctx.pass_state_mut(|state| state.allocate_central_panel(top_ui.min_rect()));
}) })
} }
@@ -3628,7 +3640,7 @@ impl Context {
/// If AccessKit support is active for the current frame, get or create /// If AccessKit support is active for the current frame, get or create
/// a node builder with the specified ID and return a mutable reference to it. /// a node builder with the specified ID and return a mutable reference to it.
/// For newly created nodes, the parent is the parent [`Ui`]s ID. /// For newly created nodes, the parent is the parent [`Ui`]s ID.
/// And an [`Ui`]s parent can be set with [`crate::UiBuilder::accessibility_parent`]. /// And an [`Ui`]s parent can be set with [`UiBuilder::accessibility_parent`].
/// ///
/// The `Context` lock is held while the given closure is called! /// The `Context` lock is held while the given closure is called!
/// ///

View File

@@ -140,19 +140,19 @@ pub fn criterion_benchmark(c: &mut Criterion) {
let ctx = egui::Context::default(); let ctx = egui::Context::default();
ctx.begin_pass(RawInput::default()); ctx.begin_pass(RawInput::default());
egui::CentralPanel::default().show(&ctx, |ui| { let painter =
c.bench_function("Painter::rect", |b| { egui::Painter::new(ctx.clone(), egui::LayerId::background(), ctx.content_rect());
let painter = ui.painter();
let rect = ui.max_rect(); c.bench_function("Painter::rect", |b| {
b.iter(|| { let rect = painter.clip_rect();
painter.rect( b.iter(|| {
rect, painter.rect(
2.0, rect,
egui::Color32::RED, 2.0,
(1.0, egui::Color32::WHITE), egui::Color32::RED,
egui::StrokeKind::Inside, (1.0, egui::Color32::WHITE),
); egui::StrokeKind::Inside,
}); );
}); });
}); });

View File

@@ -18,8 +18,8 @@ fn empty_ui_should_return_tree_with_only_root_window() {
assert_eq!( assert_eq!(
output.nodes.len(), output.nodes.len(),
4, 2,
"Expected the root node and two Uis and a Frame for the panel" "Expected the root node and the top level Ui; found: {output:#?}",
); );
assert_eq!( assert_eq!(
@@ -28,8 +28,8 @@ fn empty_ui_should_return_tree_with_only_root_window() {
.iter() .iter()
.filter(|(_, n)| n.role() == Role::GenericContainer) .filter(|(_, n)| n.role() == Role::GenericContainer)
.count(), .count(),
3, 1,
"Expected two Uis and one Frame as GenericContainer nodes.", "Expected a single Ui as a GenericContainer node.",
); );
let (id, root) = &output.nodes[0]; let (id, root) = &output.nodes[0];

View File

@@ -32,7 +32,7 @@ impl eframe::App for MyApp {
} }
fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) { fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
custom_window_frame(ui.ctx(), "egui with custom frame", |ui| { custom_window_frame(ui, "egui with custom frame", |ui| {
ui.label("This is just the contents of the window."); ui.label("This is just the contents of the window.");
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.label("egui theme:"); ui.label("egui theme:");
@@ -42,18 +42,20 @@ impl eframe::App for MyApp {
} }
} }
fn custom_window_frame(ctx: &egui::Context, title: &str, add_contents: impl FnOnce(&mut egui::Ui)) { fn custom_window_frame(ui: &mut egui::Ui, title: &str, add_contents: impl FnOnce(&mut egui::Ui)) {
use egui::{CentralPanel, UiBuilder}; use egui::UiBuilder;
let panel_frame = egui::Frame::new() let panel_frame = egui::Frame::new()
.fill(ctx.global_style().visuals.window_fill()) .fill(ui.global_style().visuals.window_fill())
.corner_radius(10) .corner_radius(10)
.stroke(ctx.global_style().visuals.widgets.noninteractive.fg_stroke) .stroke(ui.global_style().visuals.widgets.noninteractive.fg_stroke)
.outer_margin(1); // so the stroke is within the bounds .outer_margin(1); // so the stroke is within the bounds
CentralPanel::default().frame(panel_frame).show(ctx, |ui| { panel_frame.show(ui, |ui| {
let app_rect = ui.max_rect(); let app_rect = ui.max_rect();
ui.expand_to_include_rect(app_rect); // Expand frame to include it all
let title_bar_height = 32.0; let title_bar_height = 32.0;
let title_bar_rect = { let title_bar_rect = {
let mut rect = app_rect; let mut rect = app_rect;