From eb18b3d8b6eb04b426c3e3009cc5571743024609 Mon Sep 17 00:00:00 2001 From: mitchmindtree Date: Fri, 11 Nov 2016 22:08:46 +1100 Subject: [PATCH 1/2] Add a `WindowBuilder::with_window_resize_callback` method. This allows for passing the window_resize_callback fn during the window building stage. More importantly, this allows setting the callback without the need for mutable access to the Window, making it possible to set the callback in the downstream glium crate. This may solve tomaka/glium#1232 for most folk. --- examples/window.rs | 8 +++++--- src/lib.rs | 4 ++++ src/window.rs | 19 ++++++++++++++++++- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/examples/window.rs b/examples/window.rs index ad7abda9..633057ee 100644 --- a/examples/window.rs +++ b/examples/window.rs @@ -12,9 +12,11 @@ fn resize_callback(width: u32, height: u32) { } fn main() { - let mut window = winit::WindowBuilder::new().build().unwrap(); - window.set_title("A fantastic window!"); - window.set_window_resize_callback(Some(resize_callback as fn(u32, u32))); + let window = winit::WindowBuilder::new() + .with_title("A fantastic window!") + .with_window_resize_callback(resize_callback) + .build() + .unwrap(); for event in window.wait_events() { println!("{:?}", event); diff --git a/src/lib.rs b/src/lib.rs index 1f1c626b..de61b773 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -107,6 +107,10 @@ pub struct WindowBuilder { /// Platform-specific configuration. platform_specific: platform::PlatformSpecificWindowBuilderAttributes, + + /// A function called upon resizing, necessary to receive resize events on Mac and possibly + /// other systems. + window_resize_callback: Option, } /// Error that can happen while creating a window or a headless renderer. diff --git a/src/window.rs b/src/window.rs index d5603f74..99100004 100644 --- a/src/window.rs +++ b/src/window.rs @@ -18,6 +18,7 @@ impl WindowBuilder { WindowBuilder { window: Default::default(), platform_specific: Default::default(), + window_resize_callback: None, } } @@ -93,6 +94,15 @@ impl WindowBuilder { self } + /// Provides a resize callback that is called by Mac (and potentially other + /// operating systems) during resize operations. This can be used to repaint + /// during window resizing. + #[inline] + pub fn with_window_resize_callback(mut self, cb: fn(u32, u32)) -> WindowBuilder { + self.window_resize_callback = Some(cb); + self + } + /// Builds the window. /// /// Error should be very rare and only occur in case of permission denied, incompatible system, @@ -109,7 +119,14 @@ impl WindowBuilder { } // building - platform::Window::new(&self.window, &self.platform_specific).map(|w| Window { window: w }) + let mut w = try!(platform::Window::new(&self.window, &self.platform_specific)); + + // a window resize callback was given + if let Some(callback) = self.window_resize_callback { + w.set_window_resize_callback(Some(callback)); + } + + Ok(Window { window: w }) } /// Builds the window. From 53065bda40e0ef89b6d161068fedbacf23966c71 Mon Sep 17 00:00:00 2001 From: mitchmindtree Date: Sat, 12 Nov 2016 02:55:21 +1100 Subject: [PATCH 2/2] Move resize_callback field from WindowBuilder into WindowAttributes struct --- src/lib.rs | 9 +++++---- src/window.rs | 5 ++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index de61b773..2ed1a3c5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -107,10 +107,6 @@ pub struct WindowBuilder { /// Platform-specific configuration. platform_specific: platform::PlatformSpecificWindowBuilderAttributes, - - /// A function called upon resizing, necessary to receive resize events on Mac and possibly - /// other systems. - window_resize_callback: Option, } /// Error that can happen while creating a window or a headless renderer. @@ -263,6 +259,10 @@ pub struct WindowAttributes { /// [iOS only] Enable multitouch, see [UIView#multipleTouchEnabled] /// (https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/instp/UIView/multipleTouchEnabled) pub multitouch: bool, + + /// A function called upon resizing, necessary to receive resize events on Mac and possibly + /// other systems. + pub resize_callback: Option, } impl Default for WindowAttributes { @@ -278,6 +278,7 @@ impl Default for WindowAttributes { transparent: false, decorations: true, multitouch: false, + resize_callback: None, } } } diff --git a/src/window.rs b/src/window.rs index 99100004..cbc9731a 100644 --- a/src/window.rs +++ b/src/window.rs @@ -18,7 +18,6 @@ impl WindowBuilder { WindowBuilder { window: Default::default(), platform_specific: Default::default(), - window_resize_callback: None, } } @@ -99,7 +98,7 @@ impl WindowBuilder { /// during window resizing. #[inline] pub fn with_window_resize_callback(mut self, cb: fn(u32, u32)) -> WindowBuilder { - self.window_resize_callback = Some(cb); + self.window.resize_callback = Some(cb); self } @@ -122,7 +121,7 @@ impl WindowBuilder { let mut w = try!(platform::Window::new(&self.window, &self.platform_specific)); // a window resize callback was given - if let Some(callback) = self.window_resize_callback { + if let Some(callback) = self.window.resize_callback { w.set_window_resize_callback(Some(callback)); }