2016-10-31 17:19:37 +01:00
|
|
|
# winit - Cross-platform window creation and management in Rust
|
2014-07-27 10:55:37 +02:00
|
|
|
|
2016-10-31 17:19:37 +01:00
|
|
|
[![](http://meritbadge.herokuapp.com/winit)](https://crates.io/crates/winit)
|
|
|
|
[![Docs.rs](https://docs.rs/winit/badge.svg)](https://docs.rs/winit)
|
2018-06-17 12:08:01 -07:00
|
|
|
[![Build Status](https://travis-ci.org/tomaka/winit.svg?branch=master)](https://travis-ci.org/tomaka/winit)
|
2016-10-31 17:19:37 +01:00
|
|
|
[![Build status](https://ci.appveyor.com/api/projects/status/5h87hj0g4q2xe3j9/branch/master?svg=true)](https://ci.appveyor.com/project/tomaka/winit/branch/master)
|
2014-09-19 19:51:54 +02:00
|
|
|
|
2015-02-15 18:28:12 +01:00
|
|
|
```toml
|
|
|
|
[dependencies]
|
2019-01-09 03:16:41 -11:00
|
|
|
winit = "0.18.1"
|
2015-02-15 18:28:12 +01:00
|
|
|
```
|
|
|
|
|
2016-10-31 17:19:37 +01:00
|
|
|
## [Documentation](https://docs.rs/winit)
|
2014-07-28 11:45:59 +02:00
|
|
|
|
2014-07-27 15:59:58 +02:00
|
|
|
## Usage
|
|
|
|
|
2016-10-31 17:33:36 +01:00
|
|
|
Winit is a window creation and management library. It can create windows and lets you handle
|
2017-12-17 13:13:16 -06:00
|
|
|
events (for example: the window being resized, a key being pressed, a mouse movement, etc.)
|
2016-10-31 17:19:37 +01:00
|
|
|
produced by window.
|
2015-02-22 11:31:27 +01:00
|
|
|
|
2016-10-31 17:19:37 +01:00
|
|
|
Winit is designed to be a low-level brick in a hierarchy of libraries. Consequently, in order to
|
|
|
|
show something on the window you need to use the platform-specific getters provided by winit, or
|
|
|
|
another library.
|
2015-02-22 11:31:27 +01:00
|
|
|
|
2014-07-27 15:59:58 +02:00
|
|
|
```rust
|
2016-10-31 17:19:37 +01:00
|
|
|
extern crate winit;
|
2014-07-27 15:59:58 +02:00
|
|
|
|
|
|
|
fn main() {
|
2019-02-05 10:30:33 -05:00
|
|
|
let mut event_loop = winit::EventLoop::new();
|
|
|
|
let window = winit::Window::new(&event_loop).unwrap();
|
2014-07-27 15:59:58 +02:00
|
|
|
|
2019-02-05 10:30:33 -05:00
|
|
|
event_loop.run(|event| {
|
2015-06-16 13:48:08 +02:00
|
|
|
match event {
|
2018-04-24 16:20:40 -04:00
|
|
|
winit::Event::WindowEvent {
|
|
|
|
event: winit::WindowEvent::CloseRequested,
|
|
|
|
..
|
|
|
|
} => winit::ControlFlow::Break,
|
2017-06-10 13:43:15 +10:00
|
|
|
_ => winit::ControlFlow::Continue,
|
2015-06-16 13:48:08 +02:00
|
|
|
}
|
2017-05-21 22:03:22 -07:00
|
|
|
});
|
2014-07-27 15:59:58 +02:00
|
|
|
}
|
|
|
|
```
|
2017-09-14 16:31:34 +02:00
|
|
|
|
2018-12-29 15:02:02 -11:00
|
|
|
Winit is only officially supported on the latest stable version of the Rust compiler.
|
2018-11-19 16:56:11 -05:00
|
|
|
|
2018-11-01 04:24:56 -04:00
|
|
|
### Cargo Features
|
|
|
|
|
|
|
|
Winit provides the following features, which can be enabled in your `Cargo.toml` file:
|
|
|
|
* `serde`: Enables serialization/deserialization of certain types with [Serde](https://crates.io/crates/serde).
|
|
|
|
|
2017-09-14 16:31:34 +02:00
|
|
|
### Platform-specific usage
|
|
|
|
|
|
|
|
#### Emscripten and WebAssembly
|
|
|
|
|
|
|
|
Building a binary will yield a `.js` file. In order to use it in an HTML file, you need to:
|
|
|
|
|
|
|
|
- Put a `<canvas id="my_id"></canvas>` element somewhere. A canvas corresponds to a winit "window".
|
|
|
|
- Write a Javascript code that creates a global variable named `Module`. Set `Module.canvas` to
|
2018-03-22 12:56:21 -04:00
|
|
|
the element of the `<canvas>` element (in the example you would retrieve it via `document.getElementById("my_id")`).
|
|
|
|
More information [here](https://kripken.github.io/emscripten-site/docs/api_reference/module.html).
|
2017-09-14 16:31:34 +02:00
|
|
|
- Make sure that you insert the `.js` file generated by Rust after the `Module` variable is created.
|