This crate provides a winit-based backend platform for imgui-rs.
A backend platform handles window/input device events and manages their state.
Using the library
There are five things you need to do to use this library correctly:
- Initialize a
WinitPlatform
instance
- Attach it to a winit
Window
- Pass events to the platform (every frame)
- Call frame preparation callback (every frame)
- Call render preparation callback (every frame)
Complete example (without a renderer)
# use imgui::Context;
use imgui_winit_support::{HiDpiMode, WinitPlatform};
use std::time::Instant;
use winit::{Event, EventsLoop, Window, WindowEvent};
fn main() {
let mut events_loop = EventsLoop::new();
let mut window = Window::new(&events_loop).unwrap();
let mut imgui = Context::create();
let mut platform = WinitPlatform::init(&mut imgui); platform.attach_window(imgui.io_mut(), &window, HiDpiMode::Default);
let mut last_frame = Instant::now();
let mut run = true;
while run {
events_loop.poll_events(|event| {
platform.handle_event(imgui.io_mut(), &window, &event);
if let Event::WindowEvent { event, .. } = event {
match event {
WindowEvent::CloseRequested => run = false,
_ => (),
}
}
});
platform.prepare_frame(imgui.io_mut(), &window) .expect("Failed to prepare frame");
last_frame = imgui.io_mut().update_delta_time(last_frame);
let ui = imgui.frame();
platform.prepare_render(&ui, &window); let draw_data = ui.render();
}
}