emilk / emilk/egui

Add model change detection to the widget API

Open
#7,734 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
30.6k
Forks
2.1k
Avg merge
1d 9h
Merged PRs (30d)
72

Description

Instead of taking `&mut state`, notify the model of updates using `DerefMut`/`Deref` traits.

Notify the model of updates using `DerefMut`/`Dere` traits.

Let's say we want to implement a demo like [this WebGPU demo](https://kai.graphics/webgpu-samples/?sample=animometer): we're animating n bodies and there is a slider to select how many. When we move the slider, we recreate the scene (expensive). Otherwise, we just animate the scene (cheap).

If we were to do this using Bevy/Egui integration, you'd have the `count` stored in the ECS and a system would detect changes to it and recreate the scene. Except egui wants `&mut count`, which through the `DerefMut` trait always marks the value as changed and you would always recreate the scene. The standard workaround is this:

```rust

#[derive(Resource)]
struct SliderValue(f32);

fn ui_system(
mut contexts: EguiContexts,
mut val: ResMut,
) {
Window::new("Demo Slider").show(contexts.ctx_mut(), |ui| {
let new_val = *val.0;
ui.add(Slider::new(&mut new_val, 0.0..=1.0));
if new_val != *val {
*val = new_val;
}
});
}
```

If the slider widget took `DerefMut` then rendering the slider would not trigger modification updates.

Contributor guide

Open the contributing guide

Research direction

Start by locating the widget API and the existing model-change detection behavior, then inspect how mutable widget values are passed through Rust's Deref and DerefMut traits. Done means a slider can update its value without falsely notifying the model on every render, while genuine model changes are still detected.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
frontend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.