winit/
lib.rs

1//! Winit is a cross-platform window creation and event loop management library.
2//!
3//! # Building windows
4//!
5//! Before you can build a [`Window`], you first need to build an [`EventLoop`]. This is done with the
6//! [`EventLoop::new()`] function.
7//!
8//! ```no_run
9//! use winit::event_loop::EventLoop;
10//! let event_loop = EventLoop::new();
11//! ```
12//!
13//! Once this is done there are two ways to create a [`Window`]:
14//!
15//!  - Calling [`Window::new(&event_loop)`][window_new].
16//!  - Calling [`let builder = WindowBuilder::new()`][window_builder_new] then [`builder.build(&event_loop)`][window_builder_build].
17//!
18//! The first method is the simplest, and will give you default values for everything. The second
19//! method allows you to customize the way your [`Window`] will look and behave by modifying the
20//! fields of the [`WindowBuilder`] object before you create the [`Window`].
21//!
22//! # Event handling
23//!
24//! Once a [`Window`] has been created, it will generate different *events*. A [`Window`] object can
25//! generate [`WindowEvent`]s when certain input events occur, such as a cursor moving over the
26//! window or a key getting pressed while the window is focused. Devices can generate
27//! [`DeviceEvent`]s, which contain unfiltered event data that isn't specific to a certain window.
28//! Some user activity, like mouse movement, can generate both a [`WindowEvent`] *and* a
29//! [`DeviceEvent`]. You can also create and handle your own custom [`UserEvent`]s, if desired.
30//!
31//! You can retrieve events by calling [`EventLoop::run`][event_loop_run]. This function will
32//! dispatch events for every [`Window`] that was created with that particular [`EventLoop`], and
33//! will run until the `control_flow` argument given to the closure is set to
34//! [`ControlFlow`]`::`[`ExitWithCode`] (which [`ControlFlow`]`::`[`Exit`] aliases to), at which
35//! point [`Event`]`::`[`LoopDestroyed`] is emitted and the entire program terminates.
36//!
37//! Winit no longer uses a `EventLoop::poll_events() -> impl Iterator<Event>`-based event loop
38//! model, since that can't be implemented properly on some platforms (e.g web, iOS) and works poorly on
39//! most other platforms. However, this model can be re-implemented to an extent with
40//! [`EventLoopExtRunReturn::run_return`]. See that method's documentation for more reasons about why
41//! it's discouraged, beyond compatibility reasons.
42//!
43//!
44//! ```no_run
45//! use winit::{
46//!     event::{Event, WindowEvent},
47//!     event_loop::EventLoop,
48//!     window::WindowBuilder,
49//! };
50//!
51//! let event_loop = EventLoop::new();
52//! let window = WindowBuilder::new().build(&event_loop).unwrap();
53//!
54//! event_loop.run(move |event, _, control_flow| {
55//!     // ControlFlow::Poll continuously runs the event loop, even if the OS hasn't
56//!     // dispatched any events. This is ideal for games and similar applications.
57//!     control_flow.set_poll();
58//!
59//!     // ControlFlow::Wait pauses the event loop if no events are available to process.
60//!     // This is ideal for non-game applications that only update in response to user
61//!     // input, and uses significantly less power/CPU time than ControlFlow::Poll.
62//!     control_flow.set_wait();
63//!
64//!     match event {
65//!         Event::WindowEvent {
66//!             event: WindowEvent::CloseRequested,
67//!             ..
68//!         } => {
69//!             println!("The close button was pressed; stopping");
70//!             control_flow.set_exit();
71//!         },
72//!         Event::MainEventsCleared => {
73//!             // Application update code.
74//!
75//!             // Queue a RedrawRequested event.
76//!             //
77//!             // You only need to call this if you've determined that you need to redraw, in
78//!             // applications which do not always need to. Applications that redraw continuously
79//!             // can just render here instead.
80//!             window.request_redraw();
81//!         },
82//!         Event::RedrawRequested(_) => {
83//!             // Redraw the application.
84//!             //
85//!             // It's preferable for applications that do not render continuously to render in
86//!             // this event rather than in MainEventsCleared, since rendering in here allows
87//!             // the program to gracefully handle redraws requested by the OS.
88//!         },
89//!         _ => ()
90//!     }
91//! });
92//! ```
93//!
94//! [`Event`]`::`[`WindowEvent`] has a [`WindowId`] member. In multi-window environments, it should be
95//! compared to the value returned by [`Window::id()`][window_id_fn] to determine which [`Window`]
96//! dispatched the event.
97//!
98//! # Drawing on the window
99//!
100//! Winit doesn't directly provide any methods for drawing on a [`Window`]. However it allows you to
101//! retrieve the raw handle of the window and display (see the [`platform`] module and/or the
102//! [`raw_window_handle`] and [`raw_display_handle`] methods), which in turn allows
103//!  you to create an OpenGL/Vulkan/DirectX/Metal/etc. context that can be used to render graphics.
104//!
105//! Note that many platforms will display garbage data in the window's client area if the
106//! application doesn't render anything to the window by the time the desktop compositor is ready to
107//! display the window to the user. If you notice this happening, you should create the window with
108//! [`visible` set to `false`](crate::window::WindowBuilder::with_visible) and explicitly make the
109//! window visible only once you're ready to render into it.
110//!
111//! [`EventLoop`]: event_loop::EventLoop
112//! [`EventLoopExtRunReturn::run_return`]: ./platform/run_return/trait.EventLoopExtRunReturn.html#tymethod.run_return
113//! [`EventLoop::new()`]: event_loop::EventLoop::new
114//! [event_loop_run]: event_loop::EventLoop::run
115//! [`ControlFlow`]: event_loop::ControlFlow
116//! [`Exit`]: event_loop::ControlFlow::Exit
117//! [`ExitWithCode`]: event_loop::ControlFlow::ExitWithCode
118//! [`Window`]: window::Window
119//! [`WindowId`]: window::WindowId
120//! [`WindowBuilder`]: window::WindowBuilder
121//! [window_new]: window::Window::new
122//! [window_builder_new]: window::WindowBuilder::new
123//! [window_builder_build]: window::WindowBuilder::build
124//! [window_id_fn]: window::Window::id
125//! [`Event`]: event::Event
126//! [`WindowEvent`]: event::WindowEvent
127//! [`DeviceEvent`]: event::DeviceEvent
128//! [`UserEvent`]: event::Event::UserEvent
129//! [`LoopDestroyed`]: event::Event::LoopDestroyed
130//! [`platform`]: platform
131//! [`raw_window_handle`]: ./window/struct.Window.html#method.raw_window_handle
132//! [`raw_display_handle`]: ./window/struct.Window.html#method.raw_display_handle
133
134#![deny(rust_2018_idioms)]
135#![deny(rustdoc::broken_intra_doc_links)]
136#![deny(clippy::all)]
137#![cfg_attr(feature = "cargo-clippy", deny(warnings))]
138// Doc feature labels can be tested locally by running RUSTDOCFLAGS="--cfg=docsrs" cargo +nightly doc
139#![cfg_attr(docsrs, feature(doc_auto_cfg))]
140#![allow(clippy::missing_safety_doc)]
141
142#[allow(unused_imports)]
143#[macro_use]
144extern crate log;
145#[cfg(feature = "serde")]
146#[macro_use]
147extern crate serde;
148#[macro_use]
149extern crate bitflags;
150
151pub mod dpi;
152#[macro_use]
153pub mod error;
154pub mod event;
155pub mod event_loop;
156mod icon;
157pub mod monitor;
158mod platform_impl;
159pub mod window;
160
161pub mod platform;