2018-10-28 21:14:05 -05:00

320 lines
22 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="API documentation for the Rust `winit` crate."><meta name="keywords" content="rust, rustlang, rust-lang, winit"><title>winit - Rust</title><link rel="stylesheet" type="text/css" href="../normalize.css"><link rel="stylesheet" type="text/css" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../dark.css"><link rel="stylesheet" type="text/css" href="../light.css" id="themeStyle"><script src="../storage.js"></script></head><body class="rustdoc mod"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">&#9776;</div><p class='location'>Crate winit</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#modules">Modules</a></li><li><a href="#structs">Structs</a></li><li><a href="#enums">Enums</a></li><li><a href="#types">Type Definitions</a></li></ul></div><p class='location'></p><script>window.sidebarCurrent = {name: 'winit', ty: 'mod', relpath: '../'};</script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../theme.js"></script><nav class="sub"><form class="search-form js-only"><div class="search-container"><input class="search-input" name="search" autocomplete="off" placeholder="Click or press S to search, ? for more options…" type="search"><a id="settings-menu" href="../settings.html"><img src="../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='in-band'>Crate <a class="mod" href=''>winit</a></span><span class='out-of-band'><span id='render-detail'><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class='inner'>&#x2212;</span>]</a></span><a class='srclink' href='../src/winit/lib.rs.html#1-484' title='goto source code'>[src]</a></span></h1><div class='docblock'><p>Winit allows you to build a window on as many platforms as possible.</p>
<h1 id="building-a-window" class="section-header"><a href="#building-a-window">Building a window</a></h1>
<p>Before you can build a window, you first need to build an <code>EventsLoop</code>. This is done with the
<code>EventsLoop::new()</code> function. Example:</p>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">winit</span>::<span class="ident">EventsLoop</span>;
<span class="kw">let</span> <span class="ident">events_loop</span> <span class="op">=</span> <span class="ident">EventsLoop</span>::<span class="ident">new</span>();</pre>
<p>Once this is done there are two ways to create a window:</p>
<ul>
<li>Calling <code>Window::new(&amp;events_loop)</code>.</li>
<li>Calling <code>let builder = WindowBuilder::new()</code> then <code>builder.build(&amp;events_loop)</code>.</li>
</ul>
<p>The first way is the simplest way and will give you default values for everything.</p>
<p>The second way allows you to customize the way your window will look and behave by modifying
the fields of the <code>WindowBuilder</code> object before you create the window.</p>
<h1 id="events-handling" class="section-header"><a href="#events-handling">Events handling</a></h1>
<p>Once a window has been created, it will <em>generate events</em>. For example whenever the user moves
the window, resizes the window, moves the mouse, etc. an event is generated.</p>
<p>The events generated by a window can be retrieved from the <code>EventsLoop</code> the window was created
with.</p>
<p>There are two ways to do so. The first is to call <code>events_loop.poll_events(...)</code>, which will
retrieve all the events pending on the windows and immediately return after no new event is
available. You usually want to use this method in application that render continuously on the
screen, such as video games.</p>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">winit</span>::{<span class="ident">Event</span>, <span class="ident">WindowEvent</span>};
<span class="kw">use</span> <span class="ident">winit</span>::<span class="ident">dpi</span>::<span class="ident">LogicalSize</span>;
<span class="kw">loop</span> {
<span class="ident">events_loop</span>.<span class="ident">poll_events</span>(<span class="op">|</span><span class="ident">event</span><span class="op">|</span> {
<span class="kw">match</span> <span class="ident">event</span> {
<span class="ident">Event</span>::<span class="ident">WindowEvent</span> {
<span class="ident">event</span>: <span class="ident">WindowEvent</span>::<span class="ident">Resized</span>(<span class="ident">LogicalSize</span> { <span class="ident">width</span>, <span class="ident">height</span> }),
..
} <span class="op">=&gt;</span> {
<span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;The window was resized to {}x{}&quot;</span>, <span class="ident">width</span>, <span class="ident">height</span>);
},
<span class="kw">_</span> <span class="op">=&gt;</span> ()
}
});
}</pre>
<p>The second way is to call <code>events_loop.run_forever(...)</code>. As its name tells, it will run
forever unless it is stopped by returning <code>ControlFlow::Break</code>.</p>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">winit</span>::{<span class="ident">ControlFlow</span>, <span class="ident">Event</span>, <span class="ident">WindowEvent</span>};
<span class="ident">events_loop</span>.<span class="ident">run_forever</span>(<span class="op">|</span><span class="ident">event</span><span class="op">|</span> {
<span class="kw">match</span> <span class="ident">event</span> {
<span class="ident">Event</span>::<span class="ident">WindowEvent</span> { <span class="ident">event</span>: <span class="ident">WindowEvent</span>::<span class="ident">CloseRequested</span>, .. } <span class="op">=&gt;</span> {
<span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;The close button was pressed; stopping&quot;</span>);
<span class="ident">ControlFlow</span>::<span class="ident">Break</span>
},
<span class="kw">_</span> <span class="op">=&gt;</span> <span class="ident">ControlFlow</span>::<span class="ident">Continue</span>,
}
});</pre>
<p>If you use multiple windows, the <code>WindowEvent</code> event has a member named <code>window_id</code>. You can
compare it with the value returned by the <code>id()</code> method of <code>Window</code> in order to know which
window has received the event.</p>
<h1 id="drawing-on-the-window" class="section-header"><a href="#drawing-on-the-window">Drawing on the window</a></h1>
<p>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 <code>os</code> module for that), which in turn allows you
to create an OpenGL/Vulkan/DirectX/Metal/etc. context that will draw on the window.</p>
</div><h2 id='modules' class='section-header'><a href="#modules">Modules</a></h2>
<table>
<tr class=' module-item'>
<td><a class="mod" href="dpi/index.html"
title='mod winit::dpi'>dpi</a></td>
<td class='docblock-short'>
<p>DPI is important, so read the docs for this module if you don't want to be confused.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="mod" href="os/index.html"
title='mod winit::os'>os</a></td>
<td class='docblock-short'>
<p>Contains traits with platform-specific methods in them.</p>
</td>
</tr></table><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
<table>
<tr class=' module-item'>
<td><a class="struct" href="struct.AvailableMonitorsIter.html"
title='struct winit::AvailableMonitorsIter'>AvailableMonitorsIter</a></td>
<td class='docblock-short'>
<p>An iterator for the list of available monitors.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.DeviceId.html"
title='struct winit::DeviceId'>DeviceId</a></td>
<td class='docblock-short'>
<p>Identifier of an input device.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.EventsLoop.html"
title='struct winit::EventsLoop'>EventsLoop</a></td>
<td class='docblock-short'>
<p>Provides a way to retrieve events from the system and from the windows that were registered to
the events loop.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.EventsLoopClosed.html"
title='struct winit::EventsLoopClosed'>EventsLoopClosed</a></td>
<td class='docblock-short'>
<p>The error that is returned when an <code>EventsLoopProxy</code> attempts to wake up an <code>EventsLoop</code> that
no longer exists.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.EventsLoopProxy.html"
title='struct winit::EventsLoopProxy'>EventsLoopProxy</a></td>
<td class='docblock-short'>
<p>Used to wake up the <code>EventsLoop</code> from another thread.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.Icon.html"
title='struct winit::Icon'>Icon</a></td>
<td class='docblock-short'>
<p>An icon used for the window titlebar, taskbar, etc.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.KeyboardInput.html"
title='struct winit::KeyboardInput'>KeyboardInput</a></td>
<td class='docblock-short'>
<p>Describes a keyboard input event.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.ModifiersState.html"
title='struct winit::ModifiersState'>ModifiersState</a></td>
<td class='docblock-short'>
<p>Represents the current state of the keyboard modifiers</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.MonitorId.html"
title='struct winit::MonitorId'>MonitorId</a></td>
<td class='docblock-short'>
<p>Identifier for a monitor.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.Touch.html"
title='struct winit::Touch'>Touch</a></td>
<td class='docblock-short'>
<p>Represents touch event</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.Window.html"
title='struct winit::Window'>Window</a></td>
<td class='docblock-short'>
<p>Represents a window.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.WindowAttributes.html"
title='struct winit::WindowAttributes'>WindowAttributes</a></td>
<td class='docblock-short'>
<p>Attributes to use when creating a window.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.WindowBuilder.html"
title='struct winit::WindowBuilder'>WindowBuilder</a></td>
<td class='docblock-short'>
<p>Object that allows you to build windows.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.WindowId.html"
title='struct winit::WindowId'>WindowId</a></td>
<td class='docblock-short'>
<p>Identifier of a window. Unique for each window.</p>
</td>
</tr></table><h2 id='enums' class='section-header'><a href="#enums">Enums</a></h2>
<table>
<tr class=' module-item'>
<td><a class="enum" href="enum.BadIcon.html"
title='enum winit::BadIcon'>BadIcon</a></td>
<td class='docblock-short'>
<p>An error produced when using <code>Icon::from_rgba</code> with invalid arguments.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="enum" href="enum.ControlFlow.html"
title='enum winit::ControlFlow'>ControlFlow</a></td>
<td class='docblock-short'>
<p>Returned by the user callback given to the <code>EventsLoop::run_forever</code> method.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="enum" href="enum.CreationError.html"
title='enum winit::CreationError'>CreationError</a></td>
<td class='docblock-short'>
<p>Error that can happen while creating a window or a headless renderer.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="enum" href="enum.DeviceEvent.html"
title='enum winit::DeviceEvent'>DeviceEvent</a></td>
<td class='docblock-short'>
<p>Represents raw hardware events that are not associated with any particular window.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="enum" href="enum.ElementState.html"
title='enum winit::ElementState'>ElementState</a></td>
<td class='docblock-short'>
<p>Describes the input state of a key.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="enum" href="enum.Event.html"
title='enum winit::Event'>Event</a></td>
<td class='docblock-short'>
<p>Describes a generic event.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="enum" href="enum.MouseButton.html"
title='enum winit::MouseButton'>MouseButton</a></td>
<td class='docblock-short'>
<p>Describes a button of a mouse controller.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="enum" href="enum.MouseCursor.html"
title='enum winit::MouseCursor'>MouseCursor</a></td>
<td class='docblock-short'>
<p>Describes the appearance of the mouse cursor.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="enum" href="enum.MouseScrollDelta.html"
title='enum winit::MouseScrollDelta'>MouseScrollDelta</a></td>
<td class='docblock-short'>
<p>Describes a difference in the mouse scroll wheel state.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="enum" href="enum.TouchPhase.html"
title='enum winit::TouchPhase'>TouchPhase</a></td>
<td class='docblock-short'>
<p>Describes touch-screen input state.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="enum" href="enum.VirtualKeyCode.html"
title='enum winit::VirtualKeyCode'>VirtualKeyCode</a></td>
<td class='docblock-short'>
<p>Symbolic name for a keyboard key.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="enum" href="enum.WindowEvent.html"
title='enum winit::WindowEvent'>WindowEvent</a></td>
<td class='docblock-short'>
<p>Describes an event from a <code>Window</code>.</p>
</td>
</tr></table><h2 id='types' class='section-header'><a href="#types">Type Definitions</a></h2>
<table>
<tr class=' module-item'>
<td><a class="type" href="type.AxisId.html"
title='type winit::AxisId'>AxisId</a></td>
<td class='docblock-short'>
<p>Identifier for a specific analog axis on some device.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="type" href="type.ButtonId.html"
title='type winit::ButtonId'>ButtonId</a></td>
<td class='docblock-short'>
<p>Identifier for a specific button on some device.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="type" href="type.ScanCode.html"
title='type winit::ScanCode'>ScanCode</a></td>
<td class='docblock-short'>
<p>Hardware-dependent keyboard scan code.</p>
</td>
</tr></table></section><section id="search" class="content hidden"></section><section class="footer"></section><aside id="help" class="hidden"><div><h1 class="hidden">Help</h1><div class="shortcuts"><h2>Keyboard Shortcuts</h2><dl><dt><kbd>?</kbd></dt><dd>Show this help dialog</dd><dt><kbd>S</kbd></dt><dd>Focus the search field</dd><dt><kbd></kbd></dt><dd>Move up in search results</dd><dt><kbd></kbd></dt><dd>Move down in search results</dd><dt><kbd></kbd></dt><dd>Switch tab</dd><dt><kbd>&#9166;</kbd></dt><dd>Go to active search result</dd><dt><kbd>+</kbd></dt><dd>Expand all sections</dd><dt><kbd>-</kbd></dt><dd>Collapse all sections</dd></dl></div><div class="infos"><h2>Search Tricks</h2><p>Prefix searches with a type followed by a colon (e.g. <code>fn:</code>) to restrict the search to a given type.</p><p>Accepted types are: <code>fn</code>, <code>mod</code>, <code>struct</code>, <code>enum</code>, <code>trait</code>, <code>type</code>, <code>macro</code>, and <code>const</code>.</p><p>Search functions by type signature (e.g. <code>vec -> usize</code> or <code>* -> vec</code>)</p><p>Search multiple things at once by splitting your query with comma (e.g. <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath = "../";window.currentCrate = "winit";</script><script src="../aliases.js"></script><script src="../main.js"></script><script defer src="../search-index.js"></script></body></html>