1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-29 04:40:03 -04:00

Add external eventloop support (#6750)

<!--
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 #2875
* Closes https://github.com/emilk/egui/pull/3340
* [x] I have followed the instructions in the PR template

Adds `create_native`. Similiar to `run_native` but it returns an
`EframeWinitApplication` which is a `winit::ApplicationHandler`. This
can be run on your own event loop. A helper fn `pump_eframe_app` is
provided to pump the event loop and get the control flow state back.

I have been using this approach for a few months.

---------

Co-authored-by: Will Brown <opensource@rebeagle.com>
This commit is contained in:
Will Brown
2025-04-29 06:09:23 -04:00
committed by GitHub
parent fed2ab5df3
commit c075053391
10 changed files with 614 additions and 22 deletions

View File

@@ -0,0 +1,25 @@
[package]
name = "external_eventloop"
version = "0.1.0"
authors = ["Will Brown <opensource@rebeagle.com>"]
license = "MIT OR Apache-2.0"
edition = "2021"
rust-version = "1.84"
publish = false
[lints]
workspace = true
[dependencies]
eframe = { workspace = true, features = [
"default",
"__screenshot", # __screenshot is so we can dump a screenshot using EFRAME_SCREENSHOT_TO
] }
env_logger = { version = "0.10", default-features = false, features = [
"auto-color",
"humantime",
] }
winit = { workspace = true }

View File

@@ -0,0 +1,7 @@
Example running an eframe application on an external eventloop.
This allows you to run your eframe application alongside other windows and/or toolkits on the same event loop.
```sh
cargo run -p external_eventloop
```

View File

@@ -0,0 +1,89 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
#![allow(rustdoc::missing_crate_level_docs)] // it's an example
use eframe::{egui, UserEvent};
use std::{cell::Cell, rc::Rc};
use winit::event_loop::{ControlFlow, EventLoop};
fn main() -> eframe::Result {
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
let options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default().with_inner_size([320.0, 240.0]),
..Default::default()
};
let eventloop = EventLoop::<UserEvent>::with_user_event().build().unwrap();
eventloop.set_control_flow(ControlFlow::Poll);
let mut winit_app = eframe::create_native(
"External Eventloop Application",
options,
Box::new(|_| Ok(Box::<MyApp>::default())),
&eventloop,
);
eventloop.run_app(&mut winit_app)?;
Ok(())
}
struct MyApp {
value: Rc<Cell<u32>>,
spin: bool,
blinky: bool,
}
impl Default for MyApp {
fn default() -> Self {
Self {
value: Rc::new(Cell::new(42)),
spin: false,
blinky: false,
}
}
}
impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.heading("My External Eventloop Application");
ui.horizontal(|ui| {
if ui.button("Increment Now").clicked() {
self.value.set(self.value.get() + 1);
}
});
ui.label(format!("Value: {}", self.value.get()));
if ui.button("Toggle Spinner").clicked() {
self.spin = !self.spin;
}
if ui.button("Toggle Blinky").clicked() {
self.blinky = !self.blinky;
}
if self.spin {
ui.spinner();
}
if self.blinky {
let now = ui.ctx().input(|i| i.time);
let blink = now % 1.0 < 0.5;
egui::Frame::new()
.inner_margin(3)
.corner_radius(5)
.fill(if blink {
egui::Color32::RED
} else {
egui::Color32::TRANSPARENT
})
.show(ui, |ui| {
ui.label("Blinky!");
});
ctx.request_repaint_after_secs((0.5 - (now % 0.5)) as f32);
}
});
}
}