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.
#[deprecated = "Use show_inside() instead"]
pub fn show<R>(
self,
ctx: &Context,

View File

@@ -19,8 +19,8 @@ use crate::{
ImmediateViewportRendererCallback, Key, KeyboardShortcut, Label, LayerId, Memory,
ModifierNames, Modifiers, NumExt as _, Order, Painter, RawInput, Response, RichText,
SafeAreaInsets, ScrollArea, Sense, Style, TextStyle, TextureHandle, TextureOptions, Ui,
ViewportBuilder, ViewportCommand, ViewportId, ViewportIdMap, ViewportIdPair, ViewportIdSet,
ViewportOutput, Visuals, Widget as _, WidgetRect, WidgetText,
UiBuilder, ViewportBuilder, ViewportCommand, ViewportId, ViewportIdMap, ViewportIdPair,
ViewportIdSet, ViewportOutput, Visuals, Widget as _, WidgetRect, WidgetText,
animation_manager::AnimationManager,
containers::{self, area::AreaState},
data::output::PlatformOutput,
@@ -793,11 +793,23 @@ impl Context {
let plugins = self.read(|ctx| ctx.plugins.ordered_plugins());
#[expect(deprecated)]
self.run(new_input, |ctx| {
crate::CentralPanel::no_frame().show(ctx, |ui| {
plugins.on_begin_pass(ui);
run_ui(ui);
plugins.on_end_pass(ui);
});
let mut top_ui = Ui::new(
ctx.clone(),
Id::new((ctx.viewport_id(), "__top_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
/// 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.
/// 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!
///

View File

@@ -140,19 +140,19 @@ pub fn criterion_benchmark(c: &mut Criterion) {
let ctx = egui::Context::default();
ctx.begin_pass(RawInput::default());
egui::CentralPanel::default().show(&ctx, |ui| {
c.bench_function("Painter::rect", |b| {
let painter = ui.painter();
let rect = ui.max_rect();
b.iter(|| {
painter.rect(
rect,
2.0,
egui::Color32::RED,
(1.0, egui::Color32::WHITE),
egui::StrokeKind::Inside,
);
});
let painter =
egui::Painter::new(ctx.clone(), egui::LayerId::background(), ctx.content_rect());
c.bench_function("Painter::rect", |b| {
let rect = painter.clip_rect();
b.iter(|| {
painter.rect(
rect,
2.0,
egui::Color32::RED,
(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!(
output.nodes.len(),
4,
"Expected the root node and two Uis and a Frame for the panel"
2,
"Expected the root node and the top level Ui; found: {output:#?}",
);
assert_eq!(
@@ -28,8 +28,8 @@ fn empty_ui_should_return_tree_with_only_root_window() {
.iter()
.filter(|(_, n)| n.role() == Role::GenericContainer)
.count(),
3,
"Expected two Uis and one Frame as GenericContainer nodes.",
1,
"Expected a single Ui as a GenericContainer node.",
);
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) {
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.horizontal(|ui| {
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)) {
use egui::{CentralPanel, UiBuilder};
fn custom_window_frame(ui: &mut egui::Ui, title: &str, add_contents: impl FnOnce(&mut egui::Ui)) {
use egui::UiBuilder;
let panel_frame = egui::Frame::new()
.fill(ctx.global_style().visuals.window_fill())
.fill(ui.global_style().visuals.window_fill())
.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
CentralPanel::default().frame(panel_frame).show(ctx, |ui| {
panel_frame.show(ui, |ui| {
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_rect = {
let mut rect = app_rect;