From 48fe9b26442662517b1590cb98ab81d79b059953 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Mon, 21 Sep 2015 09:15:53 +0200 Subject: Extract WindowAttributes from the BuilderAttribs --- src/api/cocoa/headless.rs | 2 +- src/api/cocoa/mod.rs | 12 +++---- src/api/egl/mod.rs | 2 +- src/api/ios/mod.rs | 2 +- src/api/osmesa/mod.rs | 2 +- src/api/wayland/mod.rs | 6 ++-- src/api/win32/init.rs | 22 ++++++------- src/api/x11/window.rs | 18 +++++------ src/headless.rs | 6 +++- src/lib.rs | 77 ++++++++++++++++++++++++++++++++------------- src/platform/windows/mod.rs | 2 +- src/window.rs | 22 ++++++------- 12 files changed, 106 insertions(+), 67 deletions(-) diff --git a/src/api/cocoa/headless.rs b/src/api/cocoa/headless.rs index 7cd0f88..ad1a464 100644 --- a/src/api/cocoa/headless.rs +++ b/src/api/cocoa/headless.rs @@ -28,7 +28,7 @@ pub struct HeadlessContext { impl HeadlessContext { pub fn new(builder: BuilderAttribs) -> Result { - let (width, height) = builder.dimensions.unwrap_or((1024, 768)); + let (width, height) = builder.window.dimensions.unwrap_or((1024, 768)); let context = unsafe { let attributes = [ NSOpenGLPFAAccelerated as u32, diff --git a/src/api/cocoa/mod.rs b/src/api/cocoa/mod.rs index e259f39..e3feda2 100644 --- a/src/api/cocoa/mod.rs +++ b/src/api/cocoa/mod.rs @@ -301,7 +301,7 @@ impl Window { }; unsafe { - if builder.transparent { + if builder.window.transparent { let clear_col = { let cls = Class::get("NSColor").unwrap(); @@ -317,7 +317,7 @@ impl Window { } app.activateIgnoringOtherApps_(YES); - if builder.visible { + if builder.window.visible { window.makeKeyAndOrderFront_(nil); } else { window.makeKeyWindow(); @@ -358,7 +358,7 @@ impl Window { fn create_window(builder: &BuilderAttribs) -> Option { unsafe { - let screen = match builder.monitor { + let screen = match builder.window.monitor { Some(ref monitor_id) => { let native_id = match monitor_id.get_native_identifier() { NativeMonitorId::Numeric(num) => num, @@ -390,12 +390,12 @@ impl Window { let frame = match screen { Some(screen) => NSScreen::frame(screen), None => { - let (width, height) = builder.dimensions.unwrap_or((800, 600)); + let (width, height) = builder.window.dimensions.unwrap_or((800, 600)); NSRect::new(NSPoint::new(0., 0.), NSSize::new(width as f64, height as f64)) } }; - let masks = if screen.is_some() || !builder.decorations { + let masks = if screen.is_some() || !builder.window.decorations { NSBorderlessWindowMask as NSUInteger | NSResizableWindowMask as NSUInteger } else { @@ -412,7 +412,7 @@ impl Window { NO, )); window.non_nil().map(|window| { - let title = IdRef::new(NSString::alloc(nil).init_str(&builder.title)); + let title = IdRef::new(NSString::alloc(nil).init_str(&builder.window.title)); window.setTitle_(*title); window.setAcceptsMouseMovedEvents_(YES); if screen.is_some() { diff --git a/src/api/egl/mod.rs b/src/api/egl/mod.rs index c06c8ef..32ab71e 100644 --- a/src/api/egl/mod.rs +++ b/src/api/egl/mod.rs @@ -367,7 +367,7 @@ impl<'a> ContextPrototype<'a> { } pub fn finish_pbuffer(self) -> Result { - let dimensions = self.builder.dimensions.unwrap_or((800, 600)); + let dimensions = self.builder.window.dimensions.unwrap_or((800, 600)); let attrs = &[ ffi::egl::WIDTH as libc::c_int, dimensions.0 as libc::c_int, diff --git a/src/api/ios/mod.rs b/src/api/ios/mod.rs index 9d1b527..0163f45 100644 --- a/src/api/ios/mod.rs +++ b/src/api/ios/mod.rs @@ -219,7 +219,7 @@ impl Window { let state = &mut *self.delegate_state; - if builder.multitouch { + if builder.window.multitouch { let _: () = msg_send![state.view, setMultipleTouchEnabled:YES]; } diff --git a/src/api/osmesa/mod.rs b/src/api/osmesa/mod.rs index 2bfc8f5..2debc7e 100644 --- a/src/api/osmesa/mod.rs +++ b/src/api/osmesa/mod.rs @@ -37,7 +37,7 @@ impl OsMesaContext { return Err(OsMesaCreationError::NotSupported); } - let dimensions = builder.dimensions.unwrap(); + let dimensions = builder.window.dimensions.unwrap(); match builder.opengl.robustness { Robustness::RobustNoResetNotification | Robustness::RobustLoseContextOnReset => { diff --git a/src/api/wayland/mod.rs b/src/api/wayland/mod.rs index b46e2d1..7bc5798 100644 --- a/src/api/wayland/mod.rs +++ b/src/api/wayland/mod.rs @@ -251,7 +251,7 @@ impl Window { if !is_egl_available() { return Err(CreationError::NotSupported) } - let (w, h) = builder.dimensions.unwrap_or((800, 600)); + let (w, h) = builder.window.dimensions.unwrap_or((800, 600)); let surface = EGLSurface::new( wayland_context.compositor.create_surface(), @@ -259,12 +259,12 @@ impl Window { h as i32 ); - let mut shell_window = if let Some(PlatformMonitorID::Wayland(ref monitor)) = builder.monitor { + let mut shell_window = if let Some(PlatformMonitorID::Wayland(ref monitor)) = builder.window.monitor { let shell_surface = wayland_context.shell.get_shell_surface(surface); shell_surface.set_fullscreen(ShellFullscreenMethod::Default, Some(&monitor.output)); ShellWindow::Plain(shell_surface) } else { - if builder.decorations { + if builder.window.decorations { ShellWindow::Decorated(match DecoratedSurface::new( surface, w as i32, diff --git a/src/api/win32/init.rs b/src/api/win32/init.rs index daa6b5a..8c2d86c 100644 --- a/src/api/win32/init.rs +++ b/src/api/win32/init.rs @@ -47,7 +47,7 @@ pub fn new_window(builder: BuilderAttribs<'static>, builder_sharelists: Option>(); let (tx, rx) = channel(); @@ -92,20 +92,20 @@ unsafe fn init(title: Vec, builder: BuilderAttribs<'static>, // building a RECT object with coordinates let mut rect = winapi::RECT { - left: 0, right: builder.dimensions.unwrap_or((1024, 768)).0 as winapi::LONG, - top: 0, bottom: builder.dimensions.unwrap_or((1024, 768)).1 as winapi::LONG, + left: 0, right: builder.window.dimensions.unwrap_or((1024, 768)).0 as winapi::LONG, + top: 0, bottom: builder.window.dimensions.unwrap_or((1024, 768)).1 as winapi::LONG, }; // switching to fullscreen if necessary // this means adjusting the window's position so that it overlaps the right monitor, // and change the monitor's resolution if necessary - if builder.monitor.is_some() { - let monitor = builder.monitor.as_ref().unwrap(); + if builder.window.monitor.is_some() { + let monitor = builder.window.monitor.as_ref().unwrap(); try!(switch_to_fullscreen(&mut rect, monitor)); } // computing the style and extended style of the window - let (ex_style, style) = if builder.monitor.is_some() || builder.decorations == false { + let (ex_style, style) = if builder.window.monitor.is_some() || builder.window.decorations == false { (winapi::WS_EX_APPWINDOW, winapi::WS_POPUP | winapi::WS_CLIPSIBLINGS | winapi::WS_CLIPCHILDREN) } else { (winapi::WS_EX_APPWINDOW | winapi::WS_EX_WINDOWEDGE, @@ -117,19 +117,19 @@ unsafe fn init(title: Vec, builder: BuilderAttribs<'static>, // creating the real window this time, by using the functions in `extra_functions` let real_window = { - let (width, height) = if builder.monitor.is_some() || builder.dimensions.is_some() { + let (width, height) = if builder.window.monitor.is_some() || builder.window.dimensions.is_some() { (Some(rect.right - rect.left), Some(rect.bottom - rect.top)) } else { (None, None) }; - let (x, y) = if builder.monitor.is_some() { + let (x, y) = if builder.window.monitor.is_some() { (Some(rect.left), Some(rect.top)) } else { (None, None) }; - let style = if !builder.visible || builder.headless { + let style = if !builder.window.visible || builder.headless { style } else { style | winapi::WS_VISIBLE @@ -203,7 +203,7 @@ unsafe fn init(title: Vec, builder: BuilderAttribs<'static>, }; // making the window transparent - if builder.transparent { + if builder.window.transparent { let bb = winapi::DWM_BLURBEHIND { dwFlags: 0x1, // FIXME: DWM_BB_ENABLE; fEnable: 1, @@ -215,7 +215,7 @@ unsafe fn init(title: Vec, builder: BuilderAttribs<'static>, } // calling SetForegroundWindow if fullscreen - if builder.monitor.is_some() { + if builder.window.monitor.is_some() { user32::SetForegroundWindow(real_window.0); } diff --git a/src/api/x11/window.rs b/src/api/x11/window.rs index 8e2bdb5..d39e701 100644 --- a/src/api/x11/window.rs +++ b/src/api/x11/window.rs @@ -296,9 +296,9 @@ pub struct Window { impl Window { pub fn new(display: &Arc, builder: BuilderAttribs) -> Result { - let dimensions = builder.dimensions.unwrap_or((800, 600)); + let dimensions = builder.window.dimensions.unwrap_or((800, 600)); - let screen_id = match builder.monitor { + let screen_id = match builder.window.monitor { Some(PlatformMonitorID::X(MonitorID(_, monitor))) => monitor as i32, _ => unsafe { (display.xlib.XDefaultScreen)(display.display) }, }; @@ -316,7 +316,7 @@ impl Window { // FIXME: `XF86VidModeModeInfo` is missing its `hskew` field. Therefore we point to // `vsyncstart` instead of `vdisplay` as a temporary hack. - let mode_to_switch_to = if builder.monitor.is_some() { + let mode_to_switch_to = if builder.window.monitor.is_some() { let matching_mode = (0 .. mode_num).map(|i| { let m: ffi::XF86VidModeModeInfo = ptr::read(*modes.offset(i as isize) as *const _); m }).find(|m| m.hdisplay == dimensions.0 as u16 && m.vsyncstart == dimensions.1 as u16); @@ -415,7 +415,7 @@ impl Window { ffi::KeyReleaseMask | ffi::ButtonPressMask | ffi::ButtonReleaseMask | ffi::KeymapStateMask; swa.border_pixel = 0; - if builder.transparent { + if builder.window.transparent { swa.background_pixel = 0; } swa.override_redirect = 0; @@ -424,7 +424,7 @@ impl Window { let mut window_attributes = ffi::CWBorderPixel | ffi::CWColormap | ffi::CWEventMask; - if builder.transparent { + if builder.window.transparent { window_attributes |= ffi::CWBackPixel; } @@ -448,7 +448,7 @@ impl Window { }; // set visibility - if builder.visible { + if builder.window.visible { unsafe { (display.xlib.XMapRaised)(display.display, window); (display.xlib.XFlush)(display.display); @@ -461,7 +461,7 @@ impl Window { (display.xlib.XInternAtom)(display.display, delete_window, 0) ); (display.xlib.XSetWMProtocols)(display.display, window, &mut wm_delete_window, 1); - with_c_str(&*builder.title, |title| {; + with_c_str(&*builder.window.title, |title| {; (display.xlib.XStoreName)(display.display, window, title); }); (display.xlib.XFlush)(display.display); @@ -509,7 +509,7 @@ impl Window { // Set ICCCM WM_CLASS property based on initial window title unsafe { - with_c_str(&*builder.title, |c_name| { + with_c_str(&*builder.window.title, |c_name| { let hint = (display.xlib.XAllocClassHint)(); (*hint).res_name = c_name as *mut libc::c_char; (*hint).res_class = c_name as *mut libc::c_char; @@ -518,7 +518,7 @@ impl Window { }); } - let is_fullscreen = builder.monitor.is_some(); + let is_fullscreen = builder.window.monitor.is_some(); // finish creating the OpenGL context let context = match context { diff --git a/src/headless.rs b/src/headless.rs index 1d14a4c..83e35b3 100644 --- a/src/headless.rs +++ b/src/headless.rs @@ -6,6 +6,7 @@ use GlRequest; use GlContext; use PixelFormat; use Robustness; +use WindowAttributes; use gl_common; use libc; @@ -23,7 +24,10 @@ impl HeadlessRendererBuilder { HeadlessRendererBuilder { attribs: BuilderAttribs { headless: true, - dimensions: Some((width, height)), + window: WindowAttributes { + dimensions: Some((width, height)), + .. Default::default() + }, .. BuilderAttribs::new() }, } diff --git a/src/lib.rs b/src/lib.rs index 82d0f3b..438938a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -371,10 +371,6 @@ pub struct BuilderAttribs<'a> { #[allow(dead_code)] headless: bool, strict: bool, - dimensions: Option<(u32, u32)>, - title: String, - monitor: Option, - visible: bool, multisampling: Option, depth_bits: Option, stencil_bits: Option, @@ -382,9 +378,7 @@ pub struct BuilderAttribs<'a> { alpha_bits: Option, stereoscopy: bool, srgb: Option, - transparent: bool, - decorations: bool, - multitouch: bool, + window: WindowAttributes, opengl: GlAttributes<&'a platform::Window>, } @@ -393,10 +387,6 @@ impl BuilderAttribs<'static> { BuilderAttribs { headless: false, strict: false, - dimensions: None, - title: "glutin window".to_string(), - monitor: None, - visible: true, multisampling: None, depth_bits: None, stencil_bits: None, @@ -404,9 +394,7 @@ impl BuilderAttribs<'static> { alpha_bits: None, stereoscopy: false, srgb: None, - transparent: false, - decorations: true, - multitouch: false, + window: Default::default(), opengl: Default::default(), } } @@ -420,10 +408,6 @@ impl<'a> BuilderAttribs<'a> { let new_attribs = BuilderAttribs { headless: self.headless, strict: self.strict, - dimensions: self.dimensions, - title: self.title, - monitor: self.monitor, - visible: self.visible, multisampling: self.multisampling, depth_bits: self.depth_bits, stencil_bits: self.stencil_bits, @@ -431,9 +415,7 @@ impl<'a> BuilderAttribs<'a> { alpha_bits: self.alpha_bits, stereoscopy: self.stereoscopy, srgb: self.srgb, - transparent: self.transparent, - decorations: self.decorations, - multitouch: self.multitouch, + window: self.window, opengl: GlAttributes { sharing: None, version: self.opengl.version, @@ -547,6 +529,59 @@ impl<'a> BuilderAttribs<'a> { } } +/// Attributes to use when creating a window. +#[derive(Clone)] +pub struct WindowAttributes { + /// The dimensions of the window. If this is `None`, some platform-specific dimensions will be + /// used. + /// + /// The default is `None`. + pub dimensions: Option<(u32, u32)>, + + /// If `Some`, the window will be in fullscreen mode with the given monitor. + /// + /// The default is `None`. + pub monitor: Option, + + /// The title of the window in the title bar. + /// + /// The default is `"glutin window"`. + pub title: String, + + /// Whether the window should be immediately visible upon creation. + /// + /// The default is `true`. + pub visible: bool, + + /// Whether the the window should be transparent. If this is true, writing colors + /// with alpha values different than `1.0` will produce a transparent window. + /// + /// The default is `false`. + pub transparent: bool, + + /// Whether the window should have borders and bars. + /// + /// The default is `true`. + pub decorations: bool, + + /// ??? TODO: document me + pub multitouch: bool, +} + +impl Default for WindowAttributes { + fn default() -> WindowAttributes { + WindowAttributes { + dimensions: None, + monitor: None, + title: "glutin window".to_owned(), + visible: true, + transparent: false, + decorations: true, + multitouch: false, + } + } +} + /// Attributes to use when creating an OpenGL context. #[derive(Clone)] pub struct GlAttributes { diff --git a/src/platform/windows/mod.rs b/src/platform/windows/mod.rs index 7087a23..521939c 100644 --- a/src/platform/windows/mod.rs +++ b/src/platform/windows/mod.rs @@ -86,7 +86,7 @@ pub enum HeadlessContext { impl HeadlessContext { pub fn new(mut builder: BuilderAttribs) -> Result { - builder.visible = false; + builder.window.visible = false; // if EGL is available, we try using EGL first // if EGL returns an error, we try the hidden window method diff --git a/src/window.rs b/src/window.rs index afca319..4a45a01 100644 --- a/src/window.rs +++ b/src/window.rs @@ -37,13 +37,13 @@ impl<'a> WindowBuilder<'a> { /// /// Width and height are in pixels. pub fn with_dimensions(mut self, width: u32, height: u32) -> WindowBuilder<'a> { - self.attribs.dimensions = Some((width, height)); + self.attribs.window.dimensions = Some((width, height)); self } /// Requests a specific title for the window. pub fn with_title(mut self, title: String) -> WindowBuilder<'a> { - self.attribs.title = title; + self.attribs.window.title = title; self } @@ -52,7 +52,7 @@ impl<'a> WindowBuilder<'a> { /// If you don't specify dimensions for the window, it will match the monitor's. pub fn with_fullscreen(mut self, monitor: MonitorID) -> WindowBuilder<'a> { let MonitorID(monitor) = monitor; - self.attribs.monitor = Some(monitor); + self.attribs.window.monitor = Some(monitor); self } @@ -99,7 +99,7 @@ impl<'a> WindowBuilder<'a> { /// Sets whether the window will be initially hidden or visible. pub fn with_visibility(mut self, visible: bool) -> WindowBuilder<'a> { - self.attribs.visible = visible; + self.attribs.window.visible = visible; self } @@ -147,19 +147,19 @@ impl<'a> WindowBuilder<'a> { /// Sets whether the background of the window should be transparent. pub fn with_transparency(mut self, transparent: bool) -> WindowBuilder<'a> { - self.attribs.transparent = transparent; + self.attribs.window.transparent = transparent; self } /// Sets whether the window should have a border, a title bar, etc. pub fn with_decorations(mut self, decorations: bool) -> WindowBuilder<'a> { - self.attribs.decorations = decorations; + self.attribs.window.decorations = decorations; self } /// Enables multitouch pub fn with_multitouch(mut self) -> WindowBuilder<'a> { - self.attribs.multitouch = true; + self.attribs.window.multitouch = true; self } @@ -169,13 +169,13 @@ impl<'a> WindowBuilder<'a> { /// out of memory, etc. pub fn build(mut self) -> Result { // resizing the window to the dimensions of the monitor when fullscreen - if self.attribs.dimensions.is_none() && self.attribs.monitor.is_some() { - self.attribs.dimensions = Some(self.attribs.monitor.as_ref().unwrap().get_dimensions()) + if self.attribs.window.dimensions.is_none() && self.attribs.window.monitor.is_some() { + self.attribs.window.dimensions = Some(self.attribs.window.monitor.as_ref().unwrap().get_dimensions()) } // default dimensions - if self.attribs.dimensions.is_none() { - self.attribs.dimensions = Some((1024, 768)); + if self.attribs.window.dimensions.is_none() { + self.attribs.window.dimensions = Some((1024, 768)); } // building -- cgit v1.2.3