2020-01-05 11:02:41 -05:00
|
|
|
//! Winit is a cross-platform window creation and event loop management library.
|
2014-08-02 20:49:48 +02:00
|
|
|
//!
|
2020-01-05 11:02:41 -05:00
|
|
|
//! # Building windows
|
2014-09-04 11:38:33 +02:00
|
|
|
//!
|
2019-02-05 10:30:33 -05:00
|
|
|
//! Before you can build a [`Window`], you first need to build an [`EventLoop`]. This is done with the
|
|
|
|
|
//! [`EventLoop::new()`] function.
|
2014-09-04 11:38:33 +02:00
|
|
|
//!
|
2017-01-28 15:33:54 +01:00
|
|
|
//! ```no_run
|
2019-02-05 10:30:33 -05:00
|
|
|
//! use winit::event_loop::EventLoop;
|
2023-08-13 23:20:09 +04:00
|
|
|
//! let event_loop = EventLoop::new().unwrap();
|
2017-01-28 15:33:54 +01:00
|
|
|
//! ```
|
|
|
|
|
//!
|
2023-12-03 18:39:08 +01:00
|
|
|
//! Once this is done, there are two ways to create a [`Window`]:
|
2017-01-28 15:33:54 +01:00
|
|
|
//!
|
2019-02-05 10:30:33 -05:00
|
|
|
//! - Calling [`Window::new(&event_loop)`][window_new].
|
2023-08-30 11:17:36 +02:00
|
|
|
//! - Calling [`let builder = Window::builder()`][window_builder_new] then [`builder.build(&event_loop)`][window_builder_build].
|
2014-08-02 20:49:48 +02:00
|
|
|
//!
|
2023-12-03 18:39:08 +01:00
|
|
|
//! The first method is the simplest and will give you default values for everything. The second
|
2020-01-05 11:02:41 -05:00
|
|
|
//! 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`].
|
2014-10-04 19:17:02 +02:00
|
|
|
//!
|
2019-02-05 10:30:33 -05:00
|
|
|
//! # Event handling
|
2014-10-04 19:17:02 +02:00
|
|
|
//!
|
2019-06-24 17:30:06 -04:00
|
|
|
//! Once a [`Window`] has been created, it will generate different *events*. A [`Window`] object can
|
2020-01-05 11:02:41 -05:00
|
|
|
//! 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
|
2023-10-25 17:42:51 +02:00
|
|
|
//! [`DeviceEvent`]. You can also create and handle your own custom [`Event::UserEvent`]s, if desired.
|
2020-01-05 11:02:41 -05:00
|
|
|
//!
|
2023-10-25 17:42:51 +02:00
|
|
|
//! You can retrieve events by calling [`EventLoop::run()`]. This function will
|
2020-01-05 11:02:41 -05:00
|
|
|
//! dispatch events for every [`Window`] that was created with that particular [`EventLoop`], and
|
2023-10-25 17:42:51 +02:00
|
|
|
//! will run until [`exit()`] is used, at which point [`Event::LoopExiting`].
|
2017-01-28 15:33:54 +01:00
|
|
|
//!
|
2020-01-05 11:02:41 -05:00
|
|
|
//! Winit no longer uses a `EventLoop::poll_events() -> impl Iterator<Event>`-based event loop
|
2020-11-12 20:49:44 +01:00
|
|
|
//! model, since that can't be implemented properly on some platforms (e.g web, iOS) and works poorly on
|
|
|
|
|
//! most other platforms. However, this model can be re-implemented to an extent with
|
2023-10-25 17:42:51 +02:00
|
|
|
#![cfg_attr(
|
|
|
|
|
any(
|
|
|
|
|
windows_platform,
|
|
|
|
|
macos_platform,
|
|
|
|
|
android_platform,
|
|
|
|
|
x11_platform,
|
|
|
|
|
wayland_platform
|
|
|
|
|
),
|
|
|
|
|
doc = "[`EventLoopExtPumpEvents::pump_events()`][platform::pump_events::EventLoopExtPumpEvents::pump_events()]"
|
|
|
|
|
)]
|
|
|
|
|
#![cfg_attr(
|
|
|
|
|
not(any(
|
|
|
|
|
windows_platform,
|
|
|
|
|
macos_platform,
|
|
|
|
|
android_platform,
|
|
|
|
|
x11_platform,
|
|
|
|
|
wayland_platform
|
|
|
|
|
)),
|
|
|
|
|
doc = "`EventLoopExtPumpEvents::pump_events()`"
|
|
|
|
|
)]
|
|
|
|
|
//! [^1]. See that method's documentation for more reasons about why
|
2023-12-03 18:39:08 +01:00
|
|
|
//! it's discouraged beyond compatibility reasons.
|
2017-01-28 15:33:54 +01:00
|
|
|
//!
|
|
|
|
|
//!
|
|
|
|
|
//! ```no_run
|
2019-06-21 11:33:15 -04:00
|
|
|
//! use winit::{
|
|
|
|
|
//! event::{Event, WindowEvent},
|
2023-09-07 08:25:04 +02:00
|
|
|
//! event_loop::{ControlFlow, EventLoop},
|
2023-08-30 11:17:36 +02:00
|
|
|
//! window::Window,
|
2019-06-21 11:33:15 -04:00
|
|
|
//! };
|
2019-06-22 13:26:06 -04:00
|
|
|
//!
|
2023-08-13 23:20:09 +04:00
|
|
|
//! let event_loop = EventLoop::new().unwrap();
|
2023-08-30 11:17:36 +02:00
|
|
|
//! let window = Window::builder().build(&event_loop).unwrap();
|
2017-01-28 15:33:54 +01:00
|
|
|
//!
|
2023-10-10 22:46:08 +02:00
|
|
|
//! // ControlFlow::Poll continuously runs the event loop, even if the OS hasn't
|
|
|
|
|
//! // dispatched any events. This is ideal for games and similar applications.
|
|
|
|
|
//! event_loop.set_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.
|
|
|
|
|
//! event_loop.set_control_flow(ControlFlow::Wait);
|
|
|
|
|
//!
|
2023-09-07 08:25:04 +02:00
|
|
|
//! event_loop.run(move |event, elwt| {
|
2017-01-28 15:33:54 +01:00
|
|
|
//! match event {
|
2020-01-05 11:02:41 -05:00
|
|
|
//! Event::WindowEvent {
|
|
|
|
|
//! event: WindowEvent::CloseRequested,
|
|
|
|
|
//! ..
|
|
|
|
|
//! } => {
|
|
|
|
|
//! println!("The close button was pressed; stopping");
|
2023-09-07 08:25:04 +02:00
|
|
|
//! elwt.exit();
|
2020-01-05 11:02:41 -05:00
|
|
|
//! },
|
2023-07-28 17:37:56 +01:00
|
|
|
//! Event::AboutToWait => {
|
2019-06-22 13:26:06 -04:00
|
|
|
//! // Application update code.
|
2019-07-09 22:43:15 -06:00
|
|
|
//!
|
2019-06-22 13:26:06 -04:00
|
|
|
//! // Queue a RedrawRequested event.
|
2020-04-19 14:09:08 -07:00
|
|
|
//! //
|
2023-12-03 18:39:08 +01:00
|
|
|
//! // You only need to call this if you've determined that you need to redraw in
|
2020-04-19 14:09:08 -07:00
|
|
|
//! // applications which do not always need to. Applications that redraw continuously
|
2023-12-03 18:39:08 +01:00
|
|
|
//! // can render here instead.
|
2019-06-22 13:26:06 -04:00
|
|
|
//! window.request_redraw();
|
|
|
|
|
//! },
|
2023-08-27 16:15:09 +02:00
|
|
|
//! Event::WindowEvent {
|
|
|
|
|
//! event: WindowEvent::RedrawRequested,
|
|
|
|
|
//! ..
|
|
|
|
|
//! } => {
|
2019-06-22 13:26:06 -04:00
|
|
|
//! // Redraw the application.
|
|
|
|
|
//! //
|
2020-04-19 14:09:08 -07:00
|
|
|
//! // It's preferable for applications that do not render continuously to render in
|
2023-07-28 17:37:56 +01:00
|
|
|
//! // this event rather than in AboutToWait, since rendering in here allows
|
2020-04-19 14:09:08 -07:00
|
|
|
//! // the program to gracefully handle redraws requested by the OS.
|
2019-06-22 13:26:06 -04:00
|
|
|
//! },
|
2020-01-05 11:02:41 -05:00
|
|
|
//! _ => ()
|
2017-01-28 15:33:54 +01:00
|
|
|
//! }
|
|
|
|
|
//! });
|
|
|
|
|
//! ```
|
|
|
|
|
//!
|
2023-10-25 17:42:51 +02:00
|
|
|
//! [`WindowEvent`] has a [`WindowId`] member. In multi-window environments, it should be
|
|
|
|
|
//! compared to the value returned by [`Window::id()`] to determine which [`Window`]
|
2020-01-05 11:02:41 -05:00
|
|
|
//! dispatched the event.
|
2014-10-04 19:17:02 +02:00
|
|
|
//!
|
2016-11-03 09:49:19 +01:00
|
|
|
//! # Drawing on the window
|
|
|
|
|
//!
|
2023-12-03 18:39:08 +01:00
|
|
|
//! Winit doesn't directly provide any methods for drawing on a [`Window`]. However, it allows you to
|
2022-07-21 22:22:36 +03:00
|
|
|
//! retrieve the raw handle of the window and display (see the [`platform`] module and/or the
|
|
|
|
|
//! [`raw_window_handle`] and [`raw_display_handle`] methods), which in turn allows
|
2023-10-25 17:42:51 +02:00
|
|
|
//! you to create an OpenGL/Vulkan/DirectX/Metal/etc. context that can be used to render graphics.
|
2019-02-05 10:30:33 -05:00
|
|
|
//!
|
2020-04-20 00:04:30 -04:00
|
|
|
//! 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`](crate::window::WindowBuilder::with_visible) and explicitly make the
|
|
|
|
|
//! window visible only once you're ready to render into it.
|
|
|
|
|
//!
|
2019-11-11 18:05:59 -05:00
|
|
|
//! [`EventLoop`]: event_loop::EventLoop
|
|
|
|
|
//! [`EventLoop::new()`]: event_loop::EventLoop::new
|
2023-10-25 17:42:51 +02:00
|
|
|
//! [`EventLoop::run()`]: event_loop::EventLoop::run
|
2023-09-07 08:25:04 +02:00
|
|
|
//! [`exit()`]: event_loop::EventLoopWindowTarget::exit
|
2019-11-11 18:05:59 -05:00
|
|
|
//! [`Window`]: window::Window
|
2020-01-05 11:02:41 -05:00
|
|
|
//! [`WindowId`]: window::WindowId
|
2019-11-11 18:05:59 -05:00
|
|
|
//! [`WindowBuilder`]: window::WindowBuilder
|
|
|
|
|
//! [window_new]: window::Window::new
|
2023-08-30 11:17:36 +02:00
|
|
|
//! [window_builder_new]: window::Window::builder
|
2019-11-11 18:05:59 -05:00
|
|
|
//! [window_builder_build]: window::WindowBuilder::build
|
2023-10-25 17:42:51 +02:00
|
|
|
//! [`Window::id()`]: window::Window::id
|
2019-11-11 18:05:59 -05:00
|
|
|
//! [`WindowEvent`]: event::WindowEvent
|
|
|
|
|
//! [`DeviceEvent`]: event::DeviceEvent
|
2023-10-25 17:42:51 +02:00
|
|
|
//! [`Event::UserEvent`]: event::Event::UserEvent
|
|
|
|
|
//! [`Event::LoopExiting`]: event::Event::LoopExiting
|
2020-04-19 11:58:58 -07:00
|
|
|
//! [`raw_window_handle`]: ./window/struct.Window.html#method.raw_window_handle
|
2022-07-21 22:22:36 +03:00
|
|
|
//! [`raw_display_handle`]: ./window/struct.Window.html#method.raw_display_handle
|
2023-10-25 17:42:51 +02:00
|
|
|
//! [^1]: `EventLoopExtPumpEvents::pump_events()` is only available on Windows, macOS, Android, X11 and Wayland.
|
2014-08-02 20:49:48 +02:00
|
|
|
|
2019-06-18 02:27:00 +08:00
|
|
|
#![deny(rust_2018_idioms)]
|
2021-08-30 19:40:02 +02:00
|
|
|
#![deny(rustdoc::broken_intra_doc_links)]
|
2022-06-10 13:43:33 +03:00
|
|
|
#![deny(clippy::all)]
|
2023-09-30 21:43:41 +02:00
|
|
|
#![deny(unsafe_op_in_unsafe_fn)]
|
2022-06-10 13:43:33 +03:00
|
|
|
#![cfg_attr(feature = "cargo-clippy", deny(warnings))]
|
2022-10-24 17:02:52 -05:00
|
|
|
// Doc feature labels can be tested locally by running RUSTDOCFLAGS="--cfg=docsrs" cargo +nightly doc
|
2024-01-04 12:54:35 +01:00
|
|
|
#![cfg_attr(
|
|
|
|
|
docsrs,
|
|
|
|
|
feature(doc_auto_cfg, doc_cfg_hide),
|
|
|
|
|
doc(cfg_hide(doc, docsrs))
|
|
|
|
|
)]
|
2022-06-10 13:43:33 +03:00
|
|
|
#![allow(clippy::missing_safety_doc)]
|
2019-06-18 02:27:00 +08:00
|
|
|
|
2023-10-14 19:07:39 -07:00
|
|
|
#[cfg(feature = "rwh_06")]
|
|
|
|
|
pub use rwh_06 as raw_window_handle;
|
|
|
|
|
|
2018-06-14 19:42:18 -04:00
|
|
|
pub mod dpi;
|
2019-05-29 21:29:54 -04:00
|
|
|
#[macro_use]
|
|
|
|
|
pub mod error;
|
2023-12-16 22:02:17 +02:00
|
|
|
mod cursor;
|
2019-02-05 10:30:33 -05:00
|
|
|
pub mod event;
|
|
|
|
|
pub mod event_loop;
|
2018-05-07 17:36:21 -04:00
|
|
|
mod icon;
|
2023-05-28 20:02:59 +02:00
|
|
|
pub mod keyboard;
|
2019-06-25 03:15:34 +02:00
|
|
|
pub mod monitor;
|
2019-02-05 10:30:33 -05:00
|
|
|
mod platform_impl;
|
|
|
|
|
pub mod window;
|
2015-09-21 09:15:53 +02:00
|
|
|
|
2019-02-05 10:30:33 -05:00
|
|
|
pub mod platform;
|