Movatterモバイル変換


[0]ホーム

URL:


Docs.rs

[][src]Cratewinit

Winit is a cross-platform window creation and event loop management library.

Building windows

Before you can build aWindow, you first need to build anEventLoop. This is done with theEventLoop::new() function.

usewinit::event_loop::EventLoop;letevent_loop=EventLoop::new();

Once this is done there are two ways to create aWindow:

The first method is the simplest, and will give you default values for everything. The secondmethod allows you to customize the way yourWindow will look and behave by modifying thefields of theWindowBuilder object before you create theWindow.

Event handling

Once aWindow has been created, it will generate differentevents. AWindow object cangenerateWindowEvents when certain input events occur, such as a cursor moving over thewindow or a key getting pressed while the window is focused. Devices can generateDeviceEvents, which contain unfiltered event data that isn't specific to a certain window.Some user activity, like mouse movement, can generate both aWindowEventand aDeviceEvent. You can also create and handle your own customUserEvents, if desired.

You can retreive events by callingEventLoop::run. This function willdispatch events for everyWindow that was created with that particularEventLoop, andwill run until thecontrol_flow argument given to the closure is set toControlFlow::Exit, at which pointEvent::LoopDestroyed is emitted and theentire program terminates.

Winit no longer uses aEventLoop::poll_events() -> impl Iterator<Event>-based event loopmodel, since that can't be implemented properly on web and mobile platforms and works poorly onmost desktop platforms. However, this model can be re-implemented to an extent on desktops withEventLoopExtDesktop::run_return. See that method's documentation for more reasons about whyit's discouraged, beyond mobile/web compatibility reasons.

usewinit::{event::{Event,WindowEvent},event_loop::{ControlFlow,EventLoop},window::WindowBuilder,};letevent_loop=EventLoop::new();letwindow=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;matchevent {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.window.request_redraw();        },Event::RedrawRequested(_)=> {// Redraw the application.//// It's preferrable 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 aWindowId member. In multi-window environments, it should becompared to the value returned byWindow::id() to determine whichWindowdispatched the event.

Drawing on the window

Winit doesn't directly provide any methods for drawing on aWindow. However it allows you toretrieve the raw handle of the window (see theplatform module), which in turn allows youto create an OpenGL/Vulkan/DirectX/Metal/etc. context that can be used to render graphics.

Modules

dpi

UI scaling is important, so read the docs for this module if you don't want to be confused.

error
event

TheEvent enum and assorted supporting types.

event_loop

TheEventLoop struct and assorted supporting types, includingControlFlow.

monitor

Types useful for interacting with a user's monitors.

platform

Contains traits with platform-specific methods in them.

window

TheWindow struct and associated types.


[8]ページ先頭

©2009-2025 Movatter.jp