From b988c174fe1a2b466f3735a027943a25b892628e Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Fri, 3 Feb 2017 09:13:11 +0100 Subject: [PATCH] Add WindowId type --- src/api_transition.rs | 9 ++++++--- src/events.rs | 3 ++- src/lib.rs | 9 +++++++++ src/window.rs | 5 +++-- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/api_transition.rs b/src/api_transition.rs index 2d0af294..464a7f05 100644 --- a/src/api_transition.rs +++ b/src/api_transition.rs @@ -31,7 +31,7 @@ macro_rules! gen_api_transition { for window in windows.iter() { for event in window.poll_events() { callback(::Event::WindowEvent { - window_id: &**window as *const Window as usize, + window_id: ::WindowId(WindowId(&**window as *const Window as usize)), 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 window: ::std::sync::Arc, events_loop: ::std::sync::Weak, @@ -81,8 +84,8 @@ macro_rules! gen_api_transition { } #[inline] - pub fn id(&self) -> usize { - &*self.window as *const Window as usize + pub fn id(&self) -> WindowId { + WindowId(&*self.window as *const Window as usize) } } diff --git a/src/events.rs b/src/events.rs index 4e664120..7288ea9e 100644 --- a/src/events.rs +++ b/src/events.rs @@ -1,9 +1,10 @@ use std::path::PathBuf; +use WindowId; #[derive(Clone, Debug)] pub enum Event { WindowEvent { - window_id: usize, + window_id: WindowId, event: WindowEvent, } } diff --git a/src/lib.rs b/src/lib.rs index 58bbd948..d0ba5911 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -161,6 +161,15 @@ pub struct Window { 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. // TODO: document usage in multiple threads pub struct EventsLoop { diff --git a/src/window.rs b/src/window.rs index 7f205062..ae9c8d38 100644 --- a/src/window.rs +++ b/src/window.rs @@ -7,6 +7,7 @@ use EventsLoop; use MouseCursor; use Window; use WindowBuilder; +use WindowId; use native_monitor::NativeMonitorId; use libc; @@ -293,8 +294,8 @@ impl Window { } #[inline] - pub fn id(&self) -> usize { - self.window.id() + pub fn id(&self) -> WindowId { + WindowId(self.window.id()) } }