Merge pull request #88 from mitchmindtree/resize_cb_builder

Add a `WindowBuilder::with_window_resize_callback` method.
This commit is contained in:
tomaka 2016-11-11 18:14:15 +01:00 committed by GitHub
commit 25b5768cd3
3 changed files with 27 additions and 4 deletions

View file

@ -12,9 +12,11 @@ fn resize_callback(width: u32, height: u32) {
} }
fn main() { fn main() {
let mut window = winit::WindowBuilder::new().build().unwrap(); let window = winit::WindowBuilder::new()
window.set_title("A fantastic window!"); .with_title("A fantastic window!")
window.set_window_resize_callback(Some(resize_callback as fn(u32, u32))); .with_window_resize_callback(resize_callback)
.build()
.unwrap();
for event in window.wait_events() { for event in window.wait_events() {
println!("{:?}", event); println!("{:?}", event);

View file

@ -259,6 +259,10 @@ pub struct WindowAttributes {
/// [iOS only] Enable multitouch, see [UIView#multipleTouchEnabled] /// [iOS only] Enable multitouch, see [UIView#multipleTouchEnabled]
/// (https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/instp/UIView/multipleTouchEnabled) /// (https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/instp/UIView/multipleTouchEnabled)
pub multitouch: bool, pub multitouch: bool,
/// A function called upon resizing, necessary to receive resize events on Mac and possibly
/// other systems.
pub resize_callback: Option<fn(u32, u32)>,
} }
impl Default for WindowAttributes { impl Default for WindowAttributes {
@ -274,6 +278,7 @@ impl Default for WindowAttributes {
transparent: false, transparent: false,
decorations: true, decorations: true,
multitouch: false, multitouch: false,
resize_callback: None,
} }
} }
} }

View file

@ -93,6 +93,15 @@ impl WindowBuilder {
self 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. /// Builds the window.
/// ///
/// Error should be very rare and only occur in case of permission denied, incompatible system, /// Error should be very rare and only occur in case of permission denied, incompatible system,
@ -109,7 +118,14 @@ impl WindowBuilder {
} }
// building // 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. /// Builds the window.