From 365a8d2240feb66b10734bfc397070363f797e2c Mon Sep 17 00:00:00 2001
From: Antoine Beyeler <49431240+abey79@users.noreply.github.com>
Date: Sat, 23 Dec 2023 15:53:40 +0100
Subject: [PATCH] Fix `Window` positioning bug when bad `pivot` is stored in
app data (#3721)
This PR fixes an issue where a bad pivot stored in app state would
override the pivot actually set from code. The root code of this issue
if the `Window`'s `State` containing `pivot` in the first place, as
`pivot` is not part of a state to be tracked. It makes the `State`
structure more ergonomic though, so this PR leaves the `pivot` field but
always overrides it with the value provided by user code.
#### Repro of the original issue
This issue can be reproduced using the `re_ui_example` app from the
Rerun repo, at this commit:
https://github.com/rerun-io/rerun/pull/4577/commits/fb5add0047f108769c413393d65d52e362cbcc8e.
By using this `app.ron` file, the bug will appear:
```
{
"egui": "(
areas:{
((0)):(
areas:{
(13430889033718688666):(pivot_pos:(x:565.5,y:328.0),pivot:((Min,Min)),size:(x:364.0,y:75.5),interactable:true),
},
)
}
)",
}
```
The modal is entered based on it's top-left corner even though the code
actually specifies a center-center pivot:
With this PR, the centring is correct even with the "poisoned"
`app.ron`:
---
crates/egui/src/containers/area.rs | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/crates/egui/src/containers/area.rs b/crates/egui/src/containers/area.rs
index a8c1d417..52abb749 100644
--- a/crates/egui/src/containers/area.rs
+++ b/crates/egui/src/containers/area.rs
@@ -265,7 +265,13 @@ impl Area {
let layer_id = LayerId::new(order, id);
- let state = ctx.memory(|mem| mem.areas().get(id).copied());
+ let state = ctx
+ .memory(|mem| mem.areas().get(id).copied())
+ .map(|mut state| {
+ // override the saved state with the correct value
+ state.pivot = pivot;
+ state
+ });
let is_new = state.is_none();
if is_new {
ctx.request_repaint(); // if we don't know the previous size we are likely drawing the area in the wrong place