I love egui! Thank you Emil <3 This request specifically enables an `eframe::App` which stores a lifetime. In general, I believe this is necessary because `eframe::App` currently does not seem to provide a good place to allocate and then borrow from long-lived data between `update()` calls. To attempt to borrow such long-lived data from a field of the `App` itself would be to create a self-referential struct. A hacky alternative is to allocate long-lived data with `Box::leak`, but that's a code smell and would cause problems if a program ever creates multiple Apps. As a more specific motivating example, I am developing with the [inkwell](https://github.com/TheDan64/inkwell/) crate which requires creating a `inkwell::context::Context` instance which is then borrowed from by a bazillion things with a dedicated `'ctx` lifetime. I need such a `inkwell::context::Context` for the duration of my `eframe::App` but I can't store it as a field of the app. The most natural solution to me is to simply to lift the inkwell context outside of the App and borrow from it, but that currently fails because the AppCreator implicitly has a `'static` lifetime requirement due to the use of `dyn` trait objects. Here is a simpler, self-contained motivating example adapted from the current [hello world example](https://docs.rs/eframe/latest/eframe/): ```rust use eframe::egui; struct LongLivedThing { message: String, } fn main() { let long_lived_thing = LongLivedThing { message: "Hello World!".to_string(), }; let native_options = eframe::NativeOptions::default(); eframe::run_native( "My egui App", native_options, Box::new(|cc| Ok(Box::new(MyEguiApp::new(cc, &long_lived_thing)))), // ^^^^^^^^^^^^^^^^^ // BORROWING from long_lived_thing in App ); } struct MyEguiApp<'a> { long_lived_thing: &'a LongLivedThing, } impl<'a> MyEguiApp<'a> { fn new(cc: &eframe::CreationContext<'_>, long_lived_thing: &'a LongLivedThing) -> Self { // Customize egui here with cc.egui_ctx.set_fonts and cc.egui_ctx.set_visuals. // Restore app state using cc.storage (requires the "persistence" feature). // Use the cc.gl (a glow::Context) to create graphics shaders and buffers that you can use // for e.g. egui::PaintCallback. MyEguiApp { long_lived_thing } } } impl<'a> eframe::App for MyEguiApp<'a> { fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) { egui::CentralPanel::default().show(ctx, |ui| { ui.heading(&self.long_lived_thing.message); }); } } ``` This currently fails to compile with: ```plaintext error[E0597]: `long_lived_thing` does not live long enough --> src/main.rs:16:55 | 16 | Box::new(|cc| Ok(Box::new(MyEguiApp::new(cc, &long_lived_thing)))), | ----------------------------------------------^^^^^^^^^^^^^^^^---- | | | | | | | borrowed value does not live long enough | | value captured here | cast requires that `long_lived_thing` is borrowed for `'static` 17 | ); 18 | } | - `long_lived_thing` dropped here while still borrowed | = note: due to object lifetime defaults, `Box<dyn for<'a, 'b> FnOnce(&'a CreationContext<'b>) -> Result<Box<dyn App>, Box<dyn std::error::Error + Send + Sync>>>` actually means `Box<(dyn for<'a, 'b> FnOnce(&'a CreationContext<'b>) -> Result<Box<dyn App>, Box<dyn std::error::Error + Send + Sync>> + 'static)>` ``` With the added lifetimes in this request, I'm able to compile and run this as expected on Ubuntu + Wayland. I see the CI has been emailing me about some build failures and I'll do what I can to address those. Currently running the check.sh script as well. This is intended to resolve https://github.com/emilk/egui/issues/2152 <!-- Please read the "Making a PR" section of [`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/master/CONTRIBUTING.md) before opening a Pull Request! * Keep your PR:s small and focused. * The PR title is what ends up in the changelog, so make it descriptive! * If applicable, add a screenshot or gif. * If it is a non-trivial addition, consider adding a demo for it to `egui_demo_lib`, or a new example. * Do NOT open PR:s from your `master` branch, as that makes it hard for maintainers to test and add commits to your PR. * Remember to run `cargo fmt` and `cargo clippy`. * Open the PR as a draft until you have self-reviewed it and run `./scripts/check.sh`. * When you have addressed a PR comment, mark it as resolved. Please be patient! I will review your PR, but my time is limited! --> * Closes https://github.com/emilk/egui/issues/2152 * [x] I have followed the instructions in the PR template |
||
|---|---|---|
| .. | ||
| data | ||
| src | ||
| CHANGELOG.md | ||
| Cargo.toml | ||
| README.md | ||
README.md
eframe: the egui framework
eframe is the official framework library for writing apps using egui. The app can be compiled both to run natively (for Linux, Mac, Windows, and Android) or as a web app (using Wasm).
To get started, see the examples.
To learn how to set up eframe for web and native, go to https://github.com/emilk/eframe_template/ and follow the instructions there!
There is also a tutorial video at https://www.youtube.com/watch?v=NtUkr_z7l84.
For how to use egui, see the egui docs.
eframe uses egui_glow for rendering, and on native it uses egui-winit.
To use on Linux, first run:
sudo apt-get install libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev libxkbcommon-dev libssl-dev
You need to either use edition = "2021", or set resolver = "2" in the [workspace] section of your to-level Cargo.toml. See this link for more info.
You can opt-in to the using egui_wgpu for rendering by enabling the wgpu feature and setting NativeOptions::renderer to Renderer::Wgpu.
Alternatives
eframe is not the only way to write an app using egui! You can also try egui-miniquad, bevy_egui, egui_sdl2_gl, and others.
You can also use egui_glow and winit to build your own app as demonstrated in https://github.com/emilk/egui/blob/master/crates/egui_glow/examples/pure_glow.rs.
Limitations when running egui on the web
eframe uses WebGL (via glow) and Wasm, and almost nothing else from the web tech stack. This has some benefits, but also produces some challenges and serious downsides.
- Rendering: Getting pixel-perfect rendering right on the web is very difficult.
- Search: you cannot search an egui web page like you would a normal web page.
- Bringing up an on-screen keyboard on mobile: there is no JS function to do this, so
eframefakes it by adding some invisible DOM elements. It doesn't always work. - Mobile text editing is not as good as for a normal web app.
- No integration with browser settings for colors and fonts.
- Accessibility: There is an experimental screen reader for
eframe, but it has to be enabled explicitly. There is no JS function to ask "Does the user want a screen reader?" (and there should probably not be such a function, due to user tracking/integrity concerns).eguisupports AccessKit, but as of early 2024, AccessKit lacks a Web backend.
In many ways, eframe is trying to make the browser do something it wasn't designed to do (though there are many things browser vendors could do to improve how well libraries like egui work).
The suggested use for eframe are for web apps where performance and responsiveness are more important than accessibility and mobile text editing.
Companion crates
Not all rust crates work when compiled to Wasm, but here are some useful crates have been designed to work well both natively and as Wasm:
Name
The frame in eframe stands both for the frame in which your egui app resides and also for "framework" (eframe is a framework, egui is a library).