21 releases (12 breaking)

0.12.0 May 5, 2024
0.11.0 Apr 5, 2023
0.10.0 Jan 16, 2023
0.9.0 Nov 30, 2022
0.0.22 Feb 5, 2019

#688 in GUI

Download history 863/week @ 2024-04-03 830/week @ 2024-04-10 771/week @ 2024-04-17 917/week @ 2024-04-24 1137/week @ 2024-05-01 731/week @ 2024-05-08 716/week @ 2024-05-15 848/week @ 2024-05-22 776/week @ 2024-05-29 665/week @ 2024-06-05 677/week @ 2024-06-12 796/week @ 2024-06-19 510/week @ 2024-06-26 171/week @ 2024-07-03 467/week @ 2024-07-10 497/week @ 2024-07-17

1,775 downloads per month
Used in 31 crates (28 directly)

MIT/Apache

680KB
13K SLoC

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:

  1. Initialize a WinitPlatform instance
  2. Attach it to a winit Window
  3. Pass events to the platform (every frame)
  4. Call frame preparation callback (every frame)
  5. 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::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::Window;

let mut event_loop = EventLoop::new().expect("Failed to create EventLoop");
let mut window = Window::new(&event_loop).unwrap();

let mut imgui = Context::create();
// configure imgui-rs Context if necessary

let mut platform = WinitPlatform::init(&mut imgui); // step 1
platform.attach_window(imgui.io_mut(), &window, HiDpiMode::Default); // step 2

let mut last_frame = Instant::now();
let mut run = true;
event_loop.run(move |event, window_target| {
    match event {
        Event::NewEvents(_) => {
            // other application-specific logic
            let now = Instant::now();
            imgui.io_mut().update_delta_time(now - last_frame);
            last_frame = now;
        },
        Event::AboutToWait => {
            // other application-specific logic
            platform.prepare_frame(imgui.io_mut(), &window) // step 4
                .expect("Failed to prepare frame");
            window.request_redraw();
        }
        Event::WindowEvent { event: WindowEvent::RedrawRequested, .. } => {
            let ui = imgui.frame();
            // application-specific rendering *under the UI*

            // construct the UI

            platform.prepare_render(&ui, &window); // step 5
            // render the UI with a renderer
            let draw_data = imgui.render();
            // renderer.render(..., draw_data).expect("UI rendering failed");

            // application-specific rendering *over the UI*
        },
        Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => {
            window_target.exit();
        }
        // other application-specific event handling
        event => {
            platform.handle_event(imgui.io_mut(), &window, &event); // step 3
            // other application-specific event handling
        }
    }
}).expect("EventLoop error");

Dependencies

~14–28MB
~454K SLoC