Embedding
Drive Chromium from winit
Run Kurogane's windows from a winit event loop and pump Chromium when it asks.
Your app can keep its own winit event loop and drive Chromium from it. Kurogane still owns the windows here. To put a browser inside a winit window of your own see Embed a browser in your window.
See Runtime and event loop for start, pump and should_shutdown.
Choose how often to pump
Chromium’s UI thread does its work when your loop calls AppInstance::pump. The strategies differ only in when they call it:
| Strategy | Pumps | Idle cost | Use it for |
|---|---|---|---|
| Scheduler | at the deadlines Chromium asks for | low | production apps |
| Fixed interval | on a timer | a steady wake-up | simple tools |
| Every turn | each time the loop turns | a busy core | quick experiments |
The scheduler is what CEF recommends for an app that pumps from its own loop.
Pump when Chromium asks
App::scheduler hands your closure a PumpRequest whenever Chromium needs a pump. Chromium may call it from any thread. An EventLoopProxy carries the deadline to the event loop:
use std::sync::{Arc, OnceLock};
use std::time::{Duration, Instant};
use kurogane::{App, AppInstance, PumpRequest};
use winit::application::ApplicationHandler;
use winit::event::WindowEvent;
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop, EventLoopProxy};
use winit::window::WindowId;
// The longest the loop waits between pumps, as CEF's cefclient does
const MAX_PUMP_DELAY: Duration = Duration::from_millis(1000 / 30);
struct Driver {
runtime: AppInstance,
next_pump: Instant,
}
impl ApplicationHandler<Instant> for Driver {
fn resumed(&mut self, _: &ActiveEventLoop) {}
fn user_event(&mut self, _: &ActiveEventLoop, deadline: Instant) {
// Keep the earliest; pumping early is harmless
self.next_pump = self.next_pump.min(deadline);
}
fn window_event(&mut self, _: &ActiveEventLoop, _: WindowId, _: WindowEvent) {}
fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) {
let now = Instant::now();
if now >= self.next_pump {
self.next_pump = now + MAX_PUMP_DELAY;
self.runtime.pump();
}
if self.runtime.should_shutdown() {
self.runtime.shutdown();
event_loop.exit();
return;
}
event_loop.set_control_flow(ControlFlow::WaitUntil(self.next_pump));
}
}
fn main() {
// Kurogane starts first; the scheduler reaches the loop once it exists
let wake: Arc<OnceLock<EventLoopProxy<Instant>>> = Arc::default();
let runtime = App::new("content")
.scheduler({
let wake = wake.clone();
move |request: PumpRequest| {
if let Some(proxy) = wake.get() {
let _ = proxy.send_event(request.deadline(Instant::now()));
}
}
})
.start()
.expect("Kurogane failed to start");
#[cfg(target_os = "macos")]
let event_loop = {
use winit::platform::macos::EventLoopBuilderExtMacOS;
EventLoop::<Instant>::with_user_event().with_default_menu(false).build().unwrap()
};
#[cfg(not(target_os = "macos"))]
let event_loop = EventLoop::<Instant>::with_user_event().build().unwrap();
let _ = wake.set(event_loop.create_proxy());
// The first pump comes at once; requests made before the proxy existed went nowhere
let mut driver = Driver { runtime, next_pump: Instant::now() };
event_loop.run_app(&mut driver).unwrap();
}
- The earliest deadline wins. A later request can’t push back one the loop hasn’t served yet.
- At least every 33 ms. CEF doesn’t promise to ask again after every pump. Its sample app cefclient never waits longer than 1000/30 ms. This loop does the same.
- Shutdown. The loop ends on
should_shutdownand then shuts Chromium down. Kurogane’s windows close themselves as usual.
Start Kurogane before winit
Build the App and start it before winit’s event loop exists. On macOS Kurogane installs the NSApplication subclass Chromium needs. That must happen before winit creates the application. The scheduler reads the proxy through a OnceLock for that reason. The first pump covers the requests made before the loop existed.
Keep the Edit menu on macOS
Kurogane installs the standard App, Edit and Window menus when it starts. winit replaces them at launch with an App menu alone. Without an Edit menu Cmd+C, Cmd+V and Cmd+A reach no page.
Build the loop with with_default_menu(false) as the example does, or install a menu of your own. Quit (Cmd+Q or the Dock’s) closes Kurogane’s browsers. The loop ends once should_shutdown turns true.
On Linux
Under an external pump pump also runs glib’s default main context. Chromium reads its X11 and Wayland events there, such as input, a window’s close and resizes. Your loop needs no glib of its own.
Pump on a timer or every turn
Without App::scheduler Chromium doesn’t say when it needs a pump. Pump on your own schedule instead:
use std::time::{Duration, Instant};
use kurogane::AppInstance;
use winit::event_loop::{ActiveEventLoop, ControlFlow};
const PUMP_INTERVAL: Duration = Duration::from_millis(16);
fn about_to_wait(runtime: &AppInstance, next_pump: &mut Instant, event_loop: &ActiveEventLoop) {
if Instant::now() >= *next_pump {
runtime.pump();
*next_pump = Instant::now() + PUMP_INTERVAL;
}
if runtime.should_shutdown() {
event_loop.exit();
return;
}
event_loop.set_control_flow(ControlFlow::WaitUntil(*next_pump));
}
A timer wakes your app while Chromium is idle. A longer interval wakes it less and makes animations stutter. ControlFlow::Poll with a pump on every turn works too and keeps a core busy.
Run the examples
The Kurogane repository has each strategy as a runnable example in kurogane-suite/winit. From kurogane-suite:
kurogane run --example winit_views_scheduler
The others are winit_views_poll, winit_views_timer and winit_native_embedding.