Skip to content

Commit

Permalink
Fix Window positioning bug when bad pivot is stored in app data (#…
Browse files Browse the repository at this point in the history
…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:
rerun-io/rerun@fb5add0.
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:

<img width="1312" alt="image"
src="https://github.com/emilk/egui/assets/49431240/6df1a8bb-d1ea-4e15-866c-5f14138dda0e">

With this PR, the centring is correct even with the "poisoned"
`app.ron`:

<img width="1312" alt="image"
src="https://github.com/emilk/egui/assets/49431240/7f50b2d4-9245-48f3-91de-c747bbc8c40e">
  • Loading branch information
abey79 authored Dec 23, 2023
1 parent ffcc3f0 commit 365a8d2
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion crates/egui/src/containers/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 365a8d2

Please sign in to comment.