Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run App::update even when hidden #5112

Open
emilk opened this issue Sep 16, 2024 · 2 comments
Open

Run App::update even when hidden #5112

emilk opened this issue Sep 16, 2024 · 2 comments
Labels
bug Something is broken eframe Relates to epi and eframe web Related to running Egui on the web

Comments

@emilk
Copy link
Owner

emilk commented Sep 16, 2024

If the eframe web app is in hidden (e.g. is in a background tab in the browser), then any call to request_repaint will be ignored, and not result in a call to App::update.

That's because eframe checks if request_repaint has been called from requestAnimationFrame callback, which is not called if the application is hidden.

Solution

If the web page is hidden, then calling request_repaint should schedule a setTimer(…) callback to call App::update() (must done via a setTimer delay, or we could have App::update call request_repaint which in turn calls App::update.

We also need some guards to make sure that multiple calls to request_repaint in a row only results in one call to App::update, for instance via this pseudo-code:

// Won't be called if hidden
fn on_request_animation_frame() {
    if state.needs_repaint {
       app.update();
       paint();
       state.needs_repaint = false;
    }
}

fn on_request_repaint() {
   state.needs_repaint = true;
    if document.is_hidden {
       setTime(10ms, app_update_if_needed);
    }
}

fn app_update_if_needed() {
   if state.needs_repaint {
       app.update();
       state.needs_repaint = false;  
    }
}

Related

@emilk emilk added web Related to running Egui on the web eframe Relates to epi and eframe bug Something is broken labels Sep 16, 2024
@emilk emilk added this to the Next Major Release milestone Sep 16, 2024
@emilk emilk changed the title Run App::update even when in background thread Run App::update even when in background tab Sep 16, 2024
@emilk emilk changed the title Run App::update even when in background tab Run App::update even when hidden Sep 16, 2024
@Mingun
Copy link
Contributor

Mingun commented Sep 16, 2024

Probably you shouldn't do that. Correct me if I'm wrong, but it seems there a battle of two opinions. When egui starts updating in background, someone complain why application consumes CPU when it hidden. Then egui stops updating in background and another people asks why his code didn't run when app not on the screen. It seems to me that there was a several iterations of this issue at the time present.

egui is a GUI library. The GUI code should not drive the application logic. Period. If you need other activities except GUI, create another thread and do it there as frequently as you want.

@emilk
Copy link
Owner Author

emilk commented Sep 16, 2024

When egui starts updating in background, someone complain why application consumes CPU when it hidden.

It will only update if someone calls ctx.request_repaint. With #5113 it will also be free if nobody adds any logic to tick().

If you need other activities except GUI, create another thread and do it there as frequently as you want.

That's difficult on web (no easy access to threads). It also introduces the need for users to add locks to the data in their App.

emilk added a commit to rerun-io/rerun that referenced this issue Sep 18, 2024
### What
* Part of #7378

### Issues found
* #7425
* emilk/egui#5114
* #7427

## How to test
#### Test setup - build the viewer
* `pixi run rerun-build`
* `pixi run rerun-build-web`

#### Test matrix
* Run `cargo r -p test_ui_wakeup` and test:
  * That the viewer wakes up in the background when it's alt-tabbed
* That the viewer wakes up when minimized (it should log "Received a
message from…")
* Run `cargo r -p test_ui_wakeup -- --serve` and test:
  * The viewer wakes up when browser is alt-tabbed away
* Switch to a different browser tab, send a few messages, switch back.
The messages should be there
(this is not a conclusive test, as the messages might have been received
on tab select)

## Tested

### Web
* [x] ✅ Browser alt-tabbed
* [x] ❌ Web background tab
* Has never _properly_ worked. Is blocked on
emilk/egui#5112
  * It catches up when we switch back to the tab though
       
### Linux
* [x] ✅X11 alt-tabbed
* [x] ✅ X11 minimized
* [x] ✅  Wayland alt-tabbed
* [x] ❌ Wayland minimized (Hyprland placed on non-visible workspace)
* [x] ✅ X-Wayland alt-tabbed
* [x] ✅ X-Wayland minimized (Hyprland placed on non-visible workspace)

### Mac
* [x] ✅ Native alt-tabbed
* [x] ✅ Native minimized

### Windows
* [x] ✅ Native alt-tabbed
* [x] ✅ Native minimized

### Checklist
* [x] I have read and agree to [Contributor
Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and
the [Code of
Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md)
* [x] I've included a screenshot or gif (if applicable)
* [x] I have tested the web demo (if applicable):
* Using examples from latest `main` build:
[rerun.io/viewer](https://rerun.io/viewer/pr/7422?manifest_url=https://app.rerun.io/version/main/examples_manifest.json)
* Using full set of examples from `nightly` build:
[rerun.io/viewer](https://rerun.io/viewer/pr/7422?manifest_url=https://app.rerun.io/version/nightly/examples_manifest.json)
* [x] The PR title and labels are set such as to maximize their
usefulness for the next release's CHANGELOG
* [x] If applicable, add a new check to the [release
checklist](https://github.com/rerun-io/rerun/blob/main/tests/python/release_checklist)!
* [x] If have noted any breaking changes to the log API in
`CHANGELOG.md` and the migration guide

- [PR Build Summary](https://build.rerun.io/pr/7422)
- [Recent benchmark results](https://build.rerun.io/graphs/crates.html)
- [Wasm size tracking](https://build.rerun.io/graphs/sizes.html)

To run all checks from `main`, comment on the PR with `@rerun-bot
full-check`.

---------

Co-authored-by: Antoine Beyeler <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is broken eframe Relates to epi and eframe web Related to running Egui on the web
Projects
None yet
Development

No branches or pull requests

2 participants