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

Add Ui::is_sizing_pass for better size estimation of Areas, and menus in particular (#4557)

* Part of https://github.com/emilk/egui/issues/4535
* Closes https://github.com/emilk/egui/issues/3974

This adds a special `sizing_pass` mode to `Ui`, in which we have no
centered or justified layouts, and everything is hidden. This is used by
`Area` to use the first frame to measure the size of its contents so
that it can then set the perfectly correct size the subsequent frames.

For menus, where buttons are justified (span the full width), this
finally the problem of auto-sizing. Before you would have to pick a
width manually, and all buttons would expand to that width. If it was
too wide, it looked weird. If it was too narrow, text would wrap. Now
all menus are exactly the width they need to be. By default menus will
wrap at `Spacing::menu_width`.

This affects all situations when you have something that should be as
small as possible, but still span the full width/height of the parent.
For instance: the `egui::Separator` widget now checks the
`ui.is_sizing_pass` flag before deciding on a size. In the sizing pass a
horizontal separator is always 0 wide, and only in subsequent passes
will it span the full width.
This commit is contained in:
Emil Ernerfeldt
2024-05-29 10:27:04 +02:00
committed by GitHub
parent 942fe4ab31
commit a768d74411
13 changed files with 199 additions and 45 deletions

View File

@@ -0,0 +1,24 @@
[package]
name = "test_size_pass"
version = "0.1.0"
authors = ["Emil Ernerfeldt <emil.ernerfeldt@gmail.com>"]
license = "MIT OR Apache-2.0"
edition = "2021"
rust-version = "1.76"
publish = false
[lints]
workspace = true
[features]
wgpu = ["eframe/wgpu"]
[dependencies]
eframe = { workspace = true, features = [
"default",
"__screenshot", # __screenshot is so we can dump a screenshot using EFRAME_SCREENSHOT_TO
] }
env_logger = { version = "0.10", default-features = false, features = [
"auto-color",
"humantime",
] }

View File

@@ -0,0 +1,25 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
#![allow(rustdoc::missing_crate_level_docs)] // it's a test
use eframe::egui;
fn main() -> eframe::Result<()> {
env_logger::init(); // Use `RUST_LOG=debug` to see logs.
let options = eframe::NativeOptions::default();
eframe::run_simple_native("My egui App", options, move |ctx, _frame| {
egui::CentralPanel::default().show(ctx, |ui| {
ui.label("The menu should be as wide as the widest button");
ui.menu_button("Click for menu", |ui| {
let _ = ui.button("Narrow").clicked();
let _ = ui.button("Very wide text").clicked();
let _ = ui.button("Narrow").clicked();
});
ui.label("Hover for tooltip").on_hover_ui(|ui| {
ui.label("A separator:");
ui.separator();
});
});
})
}