[−][src]Crate cart_tmp_winit
Winit is a cross-platform window creation and event loop management library.
Building windows
Before you can build a Window
, you first need to build an EventLoop
. This is done with the
EventLoop::new()
function.
use winit::event_loop::EventLoop; let event_loop = EventLoop::new();
Once this is done there are two ways to create a Window
:
- Calling
Window::new(&event_loop)
. - Calling
let builder = WindowBuilder::new()
thenbuilder.build(&event_loop)
.
The first method is the simplest, and will give you default values for everything. The second
method allows you to customize the way your Window
will look and behave by modifying the
fields of the WindowBuilder
object before you create the Window
.
Event handling
Once a Window
has been created, it will generate different events. A Window
object can
generate WindowEvent
s when certain input events occur, such as a cursor moving over the
window or a key getting pressed while the window is focused. Devices can generate
DeviceEvent
s, which contain unfiltered event data that isn't specific to a certain window.
Some user activity, like mouse movement, can generate both a WindowEvent
and a
DeviceEvent
. You can also create and handle your own custom UserEvent
s, if desired.
You can retrieve events by calling EventLoop::run
. This function will
dispatch events for every Window
that was created with that particular EventLoop
, and
will run until the control_flow
argument given to the closure is set to
ControlFlow
::
Exit
, at which point Event
::
LoopDestroyed
is emitted and the
entire program terminates.
Winit no longer uses a EventLoop::poll_events() -> impl Iterator<Event>
-based event loop
model, since that can't be implemented properly on web and mobile platforms and works poorly on
most desktop platforms. However, this model can be re-implemented to an extent on desktops with
EventLoopExtDesktop::run_return
. See that method's documentation for more reasons about why
it's discouraged, beyond mobile/web compatibility reasons.
use winit::{ event::{Event, WindowEvent}, event_loop::{ControlFlow, EventLoop}, window::WindowBuilder, }; let event_loop = EventLoop::new(); let window = WindowBuilder::new().build(&event_loop).unwrap(); event_loop.run(move |event, _, control_flow| { // ControlFlow::Poll continuously runs the event loop, even if the OS hasn't // dispatched any events. This is ideal for games and similar applications. *control_flow = ControlFlow::Poll; // ControlFlow::Wait pauses the event loop if no events are available to process. // This is ideal for non-game applications that only update in response to user // input, and uses significantly less power/CPU time than ControlFlow::Poll. *control_flow = ControlFlow::Wait; match event { Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { println!("The close button was pressed; stopping"); *control_flow = ControlFlow::Exit }, Event::MainEventsCleared => { // Application update code. // Queue a RedrawRequested event. // // You only need to call this if you've determined that you need to redraw, in // applications which do not always need to. Applications that redraw continuously // can just render here instead. window.request_redraw(); }, Event::RedrawRequested(_) => { // Redraw the application. // // It's preferable for applications that do not render continuously to render in // this event rather than in MainEventsCleared, since rendering in here allows // the program to gracefully handle redraws requested by the OS. }, _ => () } });
Event
::
WindowEvent
has a WindowId
member. In multi-window environments, it should be
compared to the value returned by Window::id()
to determine which Window
dispatched the event.
Drawing on the window
Winit doesn't directly provide any methods for drawing on a Window
. However it allows you to
retrieve the raw handle of the window (see the platform
module and/or the
raw_window_handle
method), which in turn allows you to create an
OpenGL/Vulkan/DirectX/Metal/etc. context that can be used to render graphics.
Note that many platforms will display garbage data in the window's client area if the
application doesn't render anything to the window by the time the desktop compositor is ready to
display the window to the user. If you notice this happening, you should create the window with
visible
set to false
and explicitly make the
window visible only once you're ready to render into it.
Modules
dpi | UI scaling is important, so read the docs for this module if you don't want to be confused. |
error | |
event | The |
event_loop | The |
monitor | Types useful for interacting with a user's monitors. |
platform | Contains traits with platform-specific methods in them. |
window | The |