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..2ed1a3c5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -259,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 { @@ -274,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 d5603f74..cbc9731a 100644 --- a/src/window.rs +++ b/src/window.rs @@ -93,6 +93,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 +118,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.