mirror of
https://github.com/storytold/egui.git
synced 2026-11-26 04:21:16 +01:00
* Closes https://github.com/emilk/egui/issues/4976 * Part of #4378 * Implements parts of #843 ### Background Some widgets (like `Grid` and `Table`) needs to know the width of future elements in order to properly size themselves. For instance, the width of the first column of a grid may not be known until all rows of the grid has been added, at which point it is too late. Therefore these widgets store sizes from the previous frame. This leads to "first-frame jitter", were the content is placed in the wrong place for one frame, before being accurately laid out in subsequent frames. ### What This PR adds the function `ctx.request_discard` which discards the visual output and does another _pass_, i.e. calls the whole app UI code once again (in eframe this means calling `App::update` again). This will thus discard the shapes produced by the wrongly placed widgets, and replace it with new shapes. Note that only the visual output is discarded - all other output events are accumulated. Calling `ctx.request_discard` should only be done in very rare circumstances, e.g. when a `Grid` is first shown. Calling it every frame will mean the UI code will become unnecessarily slow. Two safe-guards are in place: * `Options::max_passes` is by default 2, meaning egui will never do more than 2 passes even if `request_discard` is called on every pass * If multiple passes is done for multiple frames in a row, a warning will be printed on the screen in debug builds:  ### Breaking changes A bunch of things that had "frame" in the name now has "pass" in them instead: * Functions called `begin_frame` and `end_frame` are now called `begin_pass` and `end_pass` * `FrameState` is now `PassState` * etc ### TODO * [x] Figure out good names for everything (`ctx.request_discard`) * [x] Add API to query if we're gonna repeat this frame (to early-out from expensive rendering) * [x] Clear up naming confusion (pass vs frame) e.g. for `FrameState` * [x] Figure out when to call this * [x] Show warning on screen when there are several frames in a row with multiple passes * [x] Document * [x] Default on or off? * [x] Change `Context::frame_nr` name/docs * [x] Rename `Context::begin_frame/end_frame` and deprecate the old ones * [x] Test with Rerun * [x] Document breaking changes
136 lines
4.3 KiB
Rust
136 lines
4.3 KiB
Rust
//! This is an example of how to create a plugin for egui.
|
|
//!
|
|
//! A plugin usually consist of a struct that holds some state,
|
|
//! which is stored using [`Context::data_mut`].
|
|
//! The plugin registers itself onto a specific [`Context`]
|
|
//! to get callbacks on certain events ([`Context::on_begin_pass`], [`Context::on_end_pass`]).
|
|
|
|
use crate::{
|
|
text, Align, Align2, Color32, Context, FontFamily, FontId, Id, Rect, Shape, Vec2, WidgetText,
|
|
};
|
|
|
|
/// Register this plugin on the given egui context,
|
|
/// so that it will be called every pass.
|
|
///
|
|
/// This is a built-in plugin in egui,
|
|
/// meaning [`Context`] calls this from its `Default` implementation,
|
|
/// so this is marked as `pub(crate)`.
|
|
pub(crate) fn register(ctx: &Context) {
|
|
ctx.on_end_pass("debug_text", std::sync::Arc::new(State::end_pass));
|
|
}
|
|
|
|
/// Print this text next to the cursor at the end of the pass.
|
|
///
|
|
/// If you call this multiple times, the text will be appended.
|
|
///
|
|
/// This only works if compiled with `debug_assertions`.
|
|
///
|
|
/// ```
|
|
/// # let ctx = &egui::Context::default();
|
|
/// # let state = true;
|
|
/// egui::debug_text::print(ctx, format!("State: {state:?}"));
|
|
/// ```
|
|
#[track_caller]
|
|
pub fn print(ctx: &Context, text: impl Into<WidgetText>) {
|
|
if !cfg!(debug_assertions) {
|
|
return;
|
|
}
|
|
|
|
let location = std::panic::Location::caller();
|
|
let location = format!("{}:{}", location.file(), location.line());
|
|
ctx.data_mut(|data| {
|
|
// We use `Id::NULL` as the id, since we only have one instance of this plugin.
|
|
// We use the `temp` version instead of `persisted` since we don't want to
|
|
// persist state on disk when the egui app is closed.
|
|
let state = data.get_temp_mut_or_default::<State>(Id::NULL);
|
|
state.entries.push(Entry {
|
|
location,
|
|
text: text.into(),
|
|
});
|
|
});
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
struct Entry {
|
|
location: String,
|
|
text: WidgetText,
|
|
}
|
|
|
|
/// A plugin for easily showing debug-text on-screen.
|
|
///
|
|
/// This is a built-in plugin in egui.
|
|
#[derive(Clone, Default)]
|
|
struct State {
|
|
// This gets re-filled every pass.
|
|
entries: Vec<Entry>,
|
|
}
|
|
|
|
impl State {
|
|
fn end_pass(ctx: &Context) {
|
|
let state = ctx.data_mut(|data| data.remove_temp::<Self>(Id::NULL));
|
|
if let Some(state) = state {
|
|
state.paint(ctx);
|
|
}
|
|
}
|
|
|
|
fn paint(self, ctx: &Context) {
|
|
let Self { entries } = self;
|
|
|
|
if entries.is_empty() {
|
|
return;
|
|
}
|
|
|
|
// Show debug-text next to the cursor.
|
|
let mut pos = ctx
|
|
.input(|i| i.pointer.latest_pos())
|
|
.unwrap_or_else(|| ctx.screen_rect().center())
|
|
+ 8.0 * Vec2::Y;
|
|
|
|
let painter = ctx.debug_painter();
|
|
let where_to_put_background = painter.add(Shape::Noop);
|
|
|
|
let mut bounding_rect = Rect::from_points(&[pos]);
|
|
|
|
let color = Color32::GRAY;
|
|
let font_id = FontId::new(10.0, FontFamily::Proportional);
|
|
|
|
for Entry { location, text } in entries {
|
|
{
|
|
// Paint location to left of `pos`:
|
|
let location_galley =
|
|
ctx.fonts(|f| f.layout(location, font_id.clone(), color, f32::INFINITY));
|
|
let location_rect =
|
|
Align2::RIGHT_TOP.anchor_size(pos - 4.0 * Vec2::X, location_galley.size());
|
|
painter.galley(location_rect.min, location_galley, color);
|
|
bounding_rect = bounding_rect.union(location_rect);
|
|
}
|
|
|
|
{
|
|
// Paint `text` to right of `pos`:
|
|
let available_width = ctx.screen_rect().max.x - pos.x;
|
|
let galley = text.into_galley_impl(
|
|
ctx,
|
|
&ctx.style(),
|
|
text::TextWrapping::wrap_at_width(available_width),
|
|
font_id.clone().into(),
|
|
Align::TOP,
|
|
);
|
|
let rect = Align2::LEFT_TOP.anchor_size(pos, galley.size());
|
|
painter.galley(rect.min, galley, color);
|
|
bounding_rect = bounding_rect.union(rect);
|
|
}
|
|
|
|
pos.y = bounding_rect.max.y + 4.0;
|
|
}
|
|
|
|
painter.set(
|
|
where_to_put_background,
|
|
Shape::rect_filled(
|
|
bounding_rect.expand(4.0),
|
|
2.0,
|
|
Color32::from_black_alpha(192),
|
|
),
|
|
);
|
|
}
|
|
}
|