From 7473d99c2f4038a36cb7848e0c08f89b47b219b3 Mon Sep 17 00:00:00 2001 From: Ivo Wetzel Date: Sun, 6 Mar 2016 20:47:10 +0100 Subject: Implement `min/max_dimensions` for x11 windows. Size hints are only being set for non-fullscreen windows, if `max_dimensions` are set they'll override the normal `dimensions` since X11 will not automatically resize the window after setting the size hints. `PSize` hint is currently set along with the `min/max` hints for good measure. --- src/api/x11/window.rs | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) (limited to 'src/api') diff --git a/src/api/x11/window.rs b/src/api/x11/window.rs index 8cae2be..86d6c92 100644 --- a/src/api/x11/window.rs +++ b/src/api/x11/window.rs @@ -310,11 +310,7 @@ impl Window { pf_reqs: &PixelFormatRequirements, opengl: &GlAttributes<&Window>) -> Result { - let dimensions = window_attrs.dimensions.unwrap_or((800, 600)); - - // not implemented - assert!(window_attrs.min_dimensions.is_none()); - assert!(window_attrs.max_dimensions.is_none()); + let dimensions = window_attrs.max_dimensions.unwrap_or(window_attrs.dimensions.unwrap_or((800, 600))); let screen_id = match window_attrs.monitor { Some(PlatformMonitorId::X(MonitorId(_, monitor))) => monitor as i32, @@ -339,7 +335,7 @@ impl Window { let m = (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.vdisplay >= dimensions.1 as u16); - + match m { Some(m) => Some(m), None => return Err(OsError(format!("Could not find a suitable graphics mode"))) @@ -589,6 +585,32 @@ impl Window { (display.xf86vmode.XF86VidModeSetViewPort)(display.display, screen_id, 0, 0); display.check_errors().expect("Failed to call XF86VidModeSetViewPort"); } + + } else { + + // set size hints + let mut size_hints: ffi::XSizeHints = unsafe { mem::zeroed() }; + size_hints.flags = ffi::PSize; + size_hints.width = dimensions.0 as i32; + size_hints.height = dimensions.1 as i32; + + if let Some(dimensions) = window_attrs.min_dimensions { + size_hints.flags |= ffi::PMinSize; + size_hints.min_width = dimensions.0 as i32; + size_hints.min_height = dimensions.1 as i32; + } + + if let Some(dimensions) = window_attrs.max_dimensions { + size_hints.flags |= ffi::PMaxSize; + size_hints.max_width = dimensions.0 as i32; + size_hints.max_height = dimensions.1 as i32; + } + + unsafe { + (display.xlib.XSetNormalHints)(display.display, window, &mut size_hints); + display.check_errors().expect("Failed to call XSetNormalHints"); + } + } // finish creating the OpenGL context -- cgit v1.2.3 From 1b7a3cd31febc38b4d0d39d06f99639469a5823d Mon Sep 17 00:00:00 2001 From: Ivo Wetzel Date: Fri, 25 Mar 2016 19:52:57 +0100 Subject: Correctly apply initial size constraints for x11 windows. --- src/api/x11/window.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'src/api') diff --git a/src/api/x11/window.rs b/src/api/x11/window.rs index aeb7efe..b84dc09 100644 --- a/src/api/x11/window.rs +++ b/src/api/x11/window.rs @@ -3,7 +3,7 @@ use CreationError; use CreationError::OsError; use libc; use std::borrow::Borrow; -use std::{mem, ptr}; +use std::{mem, ptr, cmp}; use std::cell::Cell; use std::sync::atomic::AtomicBool; use std::collections::VecDeque; @@ -310,7 +310,23 @@ impl Window { pf_reqs: &PixelFormatRequirements, opengl: &GlAttributes<&Window>) -> Result { - let dimensions = window_attrs.max_dimensions.unwrap_or(window_attrs.dimensions.unwrap_or((800, 600))); + let dimensions = { + + // x11 only applies constraints when the window is actively resized + // by the user, so we have to manually apply the initial constraints + let mut dimensions = window_attrs.dimensions.unwrap_or((800, 600)); + if let Some(max) = window_attrs.max_dimensions { + dimensions.0 = cmp::min(dimensions.0, max.0); + dimensions.1 = cmp::min(dimensions.1, max.1); + } + + if let Some(min) = window_attrs.min_dimensions { + dimensions.0 = cmp::max(dimensions.0, min.0); + dimensions.1 = cmp::max(dimensions.1, min.1); + } + dimensions + + }; let screen_id = match window_attrs.monitor { Some(PlatformMonitorId::X(MonitorId(_, monitor))) => monitor as i32, -- cgit v1.2.3