2017-06-22 05:10:23 +10:00
|
|
|
//! Winit allows you to build a window on as many platforms as possible.
|
2014-08-03 04:49:48 +10:00
|
|
|
//!
|
|
|
|
//! # Building a window
|
2014-09-04 19:38:33 +10:00
|
|
|
//!
|
2019-02-06 02:30:33 +11: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 19:38:33 +10:00
|
|
|
//!
|
2017-01-29 01:33:54 +11:00
|
|
|
//! ```no_run
|
2019-02-06 02:30:33 +11:00
|
|
|
//! use winit::event_loop::EventLoop;
|
|
|
|
//! let event_loop = EventLoop::new();
|
2017-01-29 01:33:54 +11:00
|
|
|
//! ```
|
|
|
|
//!
|
2019-02-06 02:30:33 +11:00
|
|
|
//! Once this is done there are two ways to create a [`Window`]:
|
2017-01-29 01:33:54 +11:00
|
|
|
//!
|
2019-02-06 02:30:33 +11:00
|
|
|
//! - Calling [`Window::new(&event_loop)`][window_new].
|
|
|
|
//! - Calling [`let builder = WindowBuilder::new()`][window_builder_new] then [`builder.build(&event_loop)`][window_builder_build].
|
2014-08-03 04:49:48 +10:00
|
|
|
//!
|
2018-05-21 00:47:22 +10:00
|
|
|
//! The first way is the simplest way and will give you default values for everything.
|
2014-08-03 04:49:48 +10:00
|
|
|
//!
|
2019-02-06 02:30:33 +11:00
|
|
|
//! The second way 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-05 04:17:02 +11:00
|
|
|
//!
|
2019-02-06 02:30:33 +11:00
|
|
|
//! # Event handling
|
2014-10-05 04:17:02 +11:00
|
|
|
//!
|
2019-06-25 07:30:06 +10:00
|
|
|
//! Once a [`Window`] has been created, it will generate different *events*. A [`Window`] object can
|
|
|
|
//! generate a [`WindowEvent`] when certain things happen, like whenever the user moves their mouse
|
|
|
|
//! or presses a key inside the [`Window`]. Devices can generate a [`DeviceEvent`] directly as well,
|
|
|
|
//! which contains 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.
|
2017-01-29 01:33:54 +11:00
|
|
|
//!
|
2019-06-25 07:30:06 +10:00
|
|
|
//! Events can be retreived by using an [`EventLoop`]. A [`Window`] will send its events to the
|
|
|
|
//! [`EventLoop`] object it was created with.
|
2017-01-29 01:33:54 +11:00
|
|
|
//!
|
2019-02-06 02:30:33 +11:00
|
|
|
//! You do this by calling [`event_loop.run(...)`][event_loop_run]. This function will run forever
|
|
|
|
//! unless `control_flow` is set to [`ControlFlow`]`::`[`Exit`], at which point [`Event`]`::`[`LoopDestroyed`]
|
|
|
|
//! is emitted and the entire program terminates.
|
2017-01-29 01:33:54 +11:00
|
|
|
//!
|
|
|
|
//! ```no_run
|
2019-06-22 01:33:15 +10:00
|
|
|
//! use winit::{
|
|
|
|
//! event::{Event, WindowEvent},
|
2019-06-23 03:26:06 +10:00
|
|
|
//! event_loop::{ControlFlow, EventLoop},
|
|
|
|
//! window::WindowBuilder,
|
2019-06-22 01:33:15 +10:00
|
|
|
//! };
|
2019-06-23 03:26:06 +10:00
|
|
|
//!
|
|
|
|
//! let event_loop = EventLoop::new();
|
|
|
|
//! let window = WindowBuilder::new().build(&event_loop).unwrap();
|
2017-01-29 01:33:54 +11:00
|
|
|
//!
|
2019-02-06 02:30:33 +11:00
|
|
|
//! event_loop.run(move |event, _, control_flow| {
|
2017-01-29 01:33:54 +11:00
|
|
|
//! match event {
|
2019-06-23 03:26:06 +10:00
|
|
|
//! Event::EventsCleared => {
|
|
|
|
//! // Application update code.
|
|
|
|
//!
|
|
|
|
//! // Queue a RedrawRequested event.
|
|
|
|
//! window.request_redraw();
|
|
|
|
//! },
|
|
|
|
//! Event::WindowEvent {
|
|
|
|
//! event: WindowEvent::RedrawRequested,
|
|
|
|
//! ..
|
|
|
|
//! } => {
|
|
|
|
//! // Redraw the application.
|
|
|
|
//! //
|
|
|
|
//! // It's preferrable to render in this event rather than in EventsCleared, since
|
|
|
|
//! // rendering in here allows the program to gracefully handle redraws requested
|
|
|
|
//! // by the OS.
|
|
|
|
//! },
|
2019-06-22 01:33:15 +10:00
|
|
|
//! Event::WindowEvent {
|
|
|
|
//! event: WindowEvent::CloseRequested,
|
|
|
|
//! ..
|
|
|
|
//! } => {
|
2018-04-25 06:20:40 +10:00
|
|
|
//! println!("The close button was pressed; stopping");
|
2019-02-06 02:30:33 +11:00
|
|
|
//! *control_flow = ControlFlow::Exit
|
2017-01-29 01:33:54 +11:00
|
|
|
//! },
|
2019-06-23 03:26:06 +10: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.
|
|
|
|
//! _ => *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,
|
2017-01-29 01:33:54 +11:00
|
|
|
//! }
|
|
|
|
//! });
|
|
|
|
//! ```
|
|
|
|
//!
|
2019-02-06 02:30:33 +11:00
|
|
|
//! If you use multiple [`Window`]s, [`Event`]`::`[`WindowEvent`] has a member named `window_id`. You can
|
|
|
|
//! compare it with the value returned by the [`id()`][window_id_fn] method of [`Window`] in order to know which
|
|
|
|
//! [`Window`] has received the event.
|
2014-10-05 04:17:02 +11:00
|
|
|
//!
|
2016-11-03 19:49:19 +11:00
|
|
|
//! # Drawing on the window
|
|
|
|
//!
|
2019-02-06 02:30:33 +11:00
|
|
|
//! Winit doesn't provide any function that allows drawing on a [`Window`]. However it allows you to
|
|
|
|
//! retrieve the raw handle of the window (see the [`platform`] module), which in turn allows you
|
|
|
|
//! to create an OpenGL/Vulkan/DirectX/Metal/etc. context that will draw on the [`Window`].
|
|
|
|
//!
|
|
|
|
//! [`EventLoop`]: ./event_loop/struct.EventLoop.html
|
|
|
|
//! [`EventLoop::new()`]: ./event_loop/struct.EventLoop.html#method.new
|
|
|
|
//! [event_loop_run]: ./event_loop/struct.EventLoop.html#method.run
|
|
|
|
//! [`ControlFlow`]: ./event_loop/enum.ControlFlow.html
|
|
|
|
//! [`Exit`]: ./event_loop/enum.ControlFlow.html#variant.Exit
|
|
|
|
//! [`Window`]: ./window/struct.Window.html
|
|
|
|
//! [`WindowBuilder`]: ./window/struct.WindowBuilder.html
|
|
|
|
//! [window_new]: ./window/struct.Window.html#method.new
|
|
|
|
//! [window_builder_new]: ./window/struct.WindowBuilder.html#method.new
|
|
|
|
//! [window_builder_build]: ./window/struct.WindowBuilder.html#method.build
|
|
|
|
//! [window_id_fn]: ./window/struct.Window.html#method.id
|
|
|
|
//! [`Event`]: ./event/enum.Event.html
|
2019-06-25 07:30:06 +10:00
|
|
|
//! [`WindowEvent`]: ./event/enum.WindowEvent.html
|
|
|
|
//! [`DeviceEvent`]: ./event/enum.DeviceEvent.html
|
|
|
|
//! [`UserEvent`]: ./event/enum.Event.html#variant.UserEvent
|
2019-02-06 02:30:33 +11:00
|
|
|
//! [`LoopDestroyed`]: ./event/enum.Event.html#variant.LoopDestroyed
|
|
|
|
//! [`platform`]: ./platform/index.html
|
2014-08-03 04:49:48 +10:00
|
|
|
|
2019-06-18 04:27:00 +10:00
|
|
|
#![deny(rust_2018_idioms)]
|
|
|
|
|
2018-06-15 09:42:18 +10:00
|
|
|
#[allow(unused_imports)]
|
2015-04-03 17:33:51 +11:00
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
2018-07-02 01:01:46 +10:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
2018-11-01 19:24:56 +11:00
|
|
|
#[cfg(feature = "serde")]
|
|
|
|
#[macro_use]
|
|
|
|
extern crate serde;
|
2019-06-13 04:07:25 +10:00
|
|
|
#[macro_use]
|
|
|
|
extern crate derivative;
|
2019-02-05 03:52:00 +11:00
|
|
|
#[macro_use]
|
2019-07-31 16:57:31 +10:00
|
|
|
#[cfg(any(target_os = "ios", target_os = "windows"))]
|
2019-02-05 03:52:00 +11:00
|
|
|
extern crate bitflags;
|
2015-06-05 23:38:21 +10:00
|
|
|
#[cfg(any(target_os = "macos", target_os = "ios"))]
|
2015-03-22 16:31:32 +11:00
|
|
|
#[macro_use]
|
|
|
|
extern crate objc;
|
2014-10-04 23:49:39 +10:00
|
|
|
|
2018-06-15 09:42:18 +10:00
|
|
|
pub mod dpi;
|
2019-05-30 11:29:54 +10:00
|
|
|
#[macro_use]
|
|
|
|
pub mod error;
|
2019-02-06 02:30:33 +11:00
|
|
|
pub mod event;
|
|
|
|
pub mod event_loop;
|
2018-05-08 07:36:21 +10:00
|
|
|
mod icon;
|
2019-06-22 01:33:15 +10:00
|
|
|
pub mod monitor;
|
2019-02-06 02:30:33 +11:00
|
|
|
mod platform_impl;
|
|
|
|
pub mod window;
|
2015-09-21 17:15:53 +10:00
|
|
|
|
2019-02-06 02:30:33 +11:00
|
|
|
pub mod platform;
|