Add WindowId type

This commit is contained in:
Pierre Krieger 2017-02-03 09:13:11 +01:00
parent 9cd0430ec7
commit b988c174fe
4 changed files with 20 additions and 6 deletions

View file

@ -31,7 +31,7 @@ macro_rules! gen_api_transition {
for window in windows.iter() { for window in windows.iter() {
for event in window.poll_events() { for event in window.poll_events() {
callback(::Event::WindowEvent { callback(::Event::WindowEvent {
window_id: &**window as *const Window as usize, window_id: ::WindowId(WindowId(&**window as *const Window as usize)),
event: event, event: event,
}) })
} }
@ -54,6 +54,9 @@ macro_rules! gen_api_transition {
} }
} }
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct WindowId(usize);
pub struct Window2 { pub struct Window2 {
pub window: ::std::sync::Arc<Window>, pub window: ::std::sync::Arc<Window>,
events_loop: ::std::sync::Weak<EventsLoop>, events_loop: ::std::sync::Weak<EventsLoop>,
@ -81,8 +84,8 @@ macro_rules! gen_api_transition {
} }
#[inline] #[inline]
pub fn id(&self) -> usize { pub fn id(&self) -> WindowId {
&*self.window as *const Window as usize WindowId(&*self.window as *const Window as usize)
} }
} }

View file

@ -1,9 +1,10 @@
use std::path::PathBuf; use std::path::PathBuf;
use WindowId;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum Event { pub enum Event {
WindowEvent { WindowEvent {
window_id: usize, window_id: WindowId,
event: WindowEvent, event: WindowEvent,
} }
} }

View file

@ -161,6 +161,15 @@ pub struct Window {
window: platform::Window2, window: platform::Window2,
} }
/// Identifier of a window. Unique for each window.
///
/// Can be obtained with `window.id()`.
///
/// Whenever you receive an event specific to a window, this event contains a `WindowId` which you
/// can then compare to the ids of your windows.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct WindowId(platform::WindowId);
/// Provides a way to retreive events from the windows that were registered to it. /// Provides a way to retreive events from the windows that were registered to it.
// TODO: document usage in multiple threads // TODO: document usage in multiple threads
pub struct EventsLoop { pub struct EventsLoop {

View file

@ -7,6 +7,7 @@ use EventsLoop;
use MouseCursor; use MouseCursor;
use Window; use Window;
use WindowBuilder; use WindowBuilder;
use WindowId;
use native_monitor::NativeMonitorId; use native_monitor::NativeMonitorId;
use libc; use libc;
@ -293,8 +294,8 @@ impl Window {
} }
#[inline] #[inline]
pub fn id(&self) -> usize { pub fn id(&self) -> WindowId {
self.window.id() WindowId(self.window.id())
} }
} }