Merge pull request #659 from Aceeri/minmaxwindow

Minimum/maximum dimensions for windows in win32 API
This commit is contained in:
tomaka 2015-12-05 18:01:55 +01:00
commit 48a02a0c8c
9 changed files with 112 additions and 15 deletions

View file

@ -120,6 +120,10 @@ impl Window {
{ {
use std::{mem, ptr}; use std::{mem, ptr};
// not implemented
assert!(win_attribs.min_dimensions.is_none());
assert!(win_attribs.max_dimensions.is_none());
let opengl = opengl.clone().map_sharing(|w| &w.context); let opengl = opengl.clone().map_sharing(|w| &w.context);
let native_window = unsafe { android_glue::get_native_window() }; let native_window = unsafe { android_glue::get_native_window() };

View file

@ -268,6 +268,10 @@ impl Window {
unimplemented!() unimplemented!()
} }
// not implemented
assert!(win_attribs.min_dimensions.is_none());
assert!(win_attribs.max_dimensions.is_none());
match opengl.robustness { match opengl.robustness {
Robustness::RobustNoResetNotification | Robustness::RobustLoseContextOnReset => { Robustness::RobustNoResetNotification | Robustness::RobustLoseContextOnReset => {
return Err(CreationError::RobustnessNotSupported); return Err(CreationError::RobustnessNotSupported);

View file

@ -257,6 +257,10 @@ impl Window {
{ {
use self::wayland::internals::FFI; use self::wayland::internals::FFI;
// not implemented
assert!(window.min_dimensions.is_none());
assert!(window.max_dimensions.is_none());
let wayland_context = match *WAYLAND_CONTEXT { let wayland_context = match *WAYLAND_CONTEXT {
Some(ref c) => c, Some(ref c) => c,
None => return Err(CreationError::NotSupported), None => return Err(CreationError::NotSupported),

View file

@ -6,9 +6,11 @@ use std::sync::{Arc, Mutex};
use std::ffi::OsString; use std::ffi::OsString;
use std::os::windows::ffi::OsStringExt; use std::os::windows::ffi::OsStringExt;
use WindowAttributes;
use CursorState; use CursorState;
use Event; use Event;
use super::event; use super::event;
use super::WindowState;
use user32; use user32;
use shell32; use shell32;
@ -22,7 +24,15 @@ thread_local!(pub static CONTEXT_STASH: RefCell<Option<ThreadLocalData>> = RefCe
pub struct ThreadLocalData { pub struct ThreadLocalData {
pub win: winapi::HWND, pub win: winapi::HWND,
pub sender: Sender<Event>, pub sender: Sender<Event>,
pub cursor_state: Arc<Mutex<CursorState>> pub window_state: Arc<Mutex<WindowState>>
}
struct MinMaxInfo {
reserved: winapi::POINT, // Do not use/change
max_size: winapi::POINT,
max_position: winapi::POINT,
min_track: winapi::POINT,
max_track: winapi::POINT
} }
/// Checks that the window is the good one, and if so send the event to it. /// Checks that the window is the good one, and if so send the event to it.
@ -241,8 +251,8 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT,
// there's a very bizarre borrow checker bug // there's a very bizarre borrow checker bug
// possibly related to rust-lang/rust/#23338 // possibly related to rust-lang/rust/#23338
let _cursor_state = if let Some(cstash) = cstash { let _cursor_state = if let Some(cstash) = cstash {
if let Ok(cursor_state) = cstash.cursor_state.lock() { if let Ok(window_state) = cstash.window_state.lock() {
match *cursor_state { match window_state.cursor_state {
CursorState::Normal => { CursorState::Normal => {
user32::SetCursor(user32::LoadCursorW( user32::SetCursor(user32::LoadCursorW(
ptr::null_mut(), ptr::null_mut(),
@ -281,6 +291,36 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT,
0 0
}, },
winapi::WM_GETMINMAXINFO => {
let mut mmi = lparam as *mut MinMaxInfo;
//(*mmi).max_position = winapi::POINT { x: -8, y: -8 }; // The upper left corner of the window if it were maximized on the primary monitor.
//(*mmi).max_size = winapi::POINT { x: .., y: .. }; // The dimensions of the primary monitor.
CONTEXT_STASH.with(|context_stash| {
match context_stash.borrow().as_ref() {
Some(cstash) => {
let window_state = cstash.window_state.lock().unwrap();
match window_state.attributes.min_dimensions {
Some((width, height)) => {
(*mmi).min_track = winapi::POINT { x: width as i32, y: height as i32 };
},
None => { }
}
match window_state.attributes.max_dimensions {
Some((width, height)) => {
(*mmi).max_track = winapi::POINT { x: width as i32, y: height as i32 };
},
None => { }
}
},
None => { }
}
});
0
},
x if x == *super::WAKEUP_MSG_ID => { x if x == *super::WAKEUP_MSG_ID => {
use events::Event::Awakened; use events::Event::Awakened;
send_event(window, Awakened); send_event(window, Awakened);

View file

@ -5,6 +5,7 @@ use std::mem;
use std::thread; use std::thread;
use super::callback; use super::callback;
use super::WindowState;
use super::Window; use super::Window;
use super::MonitorId; use super::MonitorId;
use super::WindowWrapper; use super::WindowWrapper;
@ -214,8 +215,11 @@ unsafe fn init(title: Vec<u16>, window: &WindowAttributes, pf_reqs: &PixelFormat
user32::SetForegroundWindow(real_window.0); user32::SetForegroundWindow(real_window.0);
} }
// Creating a mutex to track the current cursor state // Creating a mutex to track the current window state
let cursor_state = Arc::new(Mutex::new(CursorState::Normal)); let window_state = Arc::new(Mutex::new(WindowState {
cursor_state: CursorState::Normal,
attributes: window.clone()
}));
// filling the CONTEXT_STASH task-local storage so that we can start receiving events // filling the CONTEXT_STASH task-local storage so that we can start receiving events
let events_receiver = { let events_receiver = {
@ -225,7 +229,7 @@ unsafe fn init(title: Vec<u16>, window: &WindowAttributes, pf_reqs: &PixelFormat
let data = callback::ThreadLocalData { let data = callback::ThreadLocalData {
win: real_window.0, win: real_window.0,
sender: tx.take().unwrap(), sender: tx.take().unwrap(),
cursor_state: cursor_state.clone() window_state: window_state.clone()
}; };
(*context_stash.borrow_mut()) = Some(data); (*context_stash.borrow_mut()) = Some(data);
}); });
@ -237,7 +241,7 @@ unsafe fn init(title: Vec<u16>, window: &WindowAttributes, pf_reqs: &PixelFormat
window: real_window, window: real_window,
context: context, context: context,
events_receiver: events_receiver, events_receiver: events_receiver,
cursor_state: cursor_state, window_state: window_state,
}) })
} }

View file

@ -42,6 +42,13 @@ lazy_static! {
static ref WAKEUP_MSG_ID: u32 = unsafe { user32::RegisterWindowMessageA("Glutin::EventID".as_ptr() as *const i8) }; static ref WAKEUP_MSG_ID: u32 = unsafe { user32::RegisterWindowMessageA("Glutin::EventID".as_ptr() as *const i8) };
} }
/// Contains information about states and the window for the callback.
#[derive(Clone)]
pub struct WindowState {
pub cursor_state: CursorState,
pub attributes: WindowAttributes
}
/// The Win32 implementation of the main `Window` object. /// The Win32 implementation of the main `Window` object.
pub struct Window { pub struct Window {
/// Main handle for the window. /// Main handle for the window.
@ -53,8 +60,8 @@ pub struct Window {
/// Receiver for the events dispatched by the window callback. /// Receiver for the events dispatched by the window callback.
events_receiver: Receiver<Event>, events_receiver: Receiver<Event>,
/// The current cursor state. /// The current window state.
cursor_state: Arc<Mutex<CursorState>>, window_state: Arc<Mutex<WindowState>>,
} }
unsafe impl Send for Window {} unsafe impl Send for Window {}
@ -258,14 +265,14 @@ impl Window {
} }
pub fn set_cursor_state(&self, state: CursorState) -> Result<(), String> { pub fn set_cursor_state(&self, state: CursorState) -> Result<(), String> {
let mut current_state = self.cursor_state.lock().unwrap(); let mut current_state = self.window_state.lock().unwrap().cursor_state;
let foreground_thread_id = unsafe { user32::GetWindowThreadProcessId(self.window.0, ptr::null_mut()) }; let foreground_thread_id = unsafe { user32::GetWindowThreadProcessId(self.window.0, ptr::null_mut()) };
let current_thread_id = unsafe { kernel32::GetCurrentThreadId() }; let current_thread_id = unsafe { kernel32::GetCurrentThreadId() };
unsafe { user32::AttachThreadInput(foreground_thread_id, current_thread_id, 1) }; unsafe { user32::AttachThreadInput(foreground_thread_id, current_thread_id, 1) };
let res = match (state, *current_state) { let res = match (state, current_state) {
(CursorState::Normal, CursorState::Normal) => Ok(()), (CursorState::Normal, CursorState::Normal) => Ok(()),
(CursorState::Hide, CursorState::Hide) => Ok(()), (CursorState::Hide, CursorState::Hide) => Ok(()),
(CursorState::Grab, CursorState::Grab) => Ok(()), (CursorState::Grab, CursorState::Grab) => Ok(()),
@ -273,7 +280,7 @@ impl Window {
(CursorState::Hide, CursorState::Normal) => { (CursorState::Hide, CursorState::Normal) => {
unsafe { unsafe {
user32::SetCursor(ptr::null_mut()); user32::SetCursor(ptr::null_mut());
*current_state = CursorState::Hide; current_state = CursorState::Hide;
Ok(()) Ok(())
} }
}, },
@ -281,7 +288,7 @@ impl Window {
(CursorState::Normal, CursorState::Hide) => { (CursorState::Normal, CursorState::Hide) => {
unsafe { unsafe {
user32::SetCursor(user32::LoadCursorW(ptr::null_mut(), winapi::IDC_ARROW)); user32::SetCursor(user32::LoadCursorW(ptr::null_mut(), winapi::IDC_ARROW));
*current_state = CursorState::Normal; current_state = CursorState::Normal;
Ok(()) Ok(())
} }
}, },
@ -298,7 +305,7 @@ impl Window {
if user32::ClipCursor(&rect) == 0 { if user32::ClipCursor(&rect) == 0 {
return Err(format!("ClipCursor failed")); return Err(format!("ClipCursor failed"));
} }
*current_state = CursorState::Grab; current_state = CursorState::Grab;
Ok(()) Ok(())
} }
}, },
@ -309,7 +316,7 @@ impl Window {
if user32::ClipCursor(ptr::null()) == 0 { if user32::ClipCursor(ptr::null()) == 0 {
return Err(format!("ClipCursor failed")); return Err(format!("ClipCursor failed"));
} }
*current_state = CursorState::Normal; current_state = CursorState::Normal;
Ok(()) Ok(())
} }
}, },

View file

@ -305,6 +305,10 @@ impl Window {
{ {
let dimensions = window_attrs.dimensions.unwrap_or((800, 600)); 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 screen_id = match window_attrs.monitor { let screen_id = match window_attrs.monitor {
Some(PlatformMonitorId::X(MonitorId(_, monitor))) => monitor as i32, Some(PlatformMonitorId::X(MonitorId(_, monitor))) => monitor as i32,
_ => unsafe { (display.xlib.XDefaultScreen)(display.display) }, _ => unsafe { (display.xlib.XDefaultScreen)(display.display) },

View file

@ -527,6 +527,16 @@ pub struct WindowAttributes {
/// The default is `None`. /// The default is `None`.
pub dimensions: Option<(u32, u32)>, pub dimensions: Option<(u32, u32)>,
/// The minimum dimensions a window can be, If this is `None`, the window will have no minimum dimensions (aside from reserved).
///
/// The default is `None`.
pub min_dimensions: Option<(u32, u32)>,
/// The maximum dimensions a window can be, If this is `None`, the maximum will have no maximum or will be set to the primary monitor's dimensions by the platform.
///
/// The default is `None`.
pub max_dimensions: Option<(u32, u32)>,
/// If `Some`, the window will be in fullscreen mode with the given monitor. /// If `Some`, the window will be in fullscreen mode with the given monitor.
/// ///
/// The default is `None`. /// The default is `None`.
@ -563,6 +573,8 @@ impl Default for WindowAttributes {
fn default() -> WindowAttributes { fn default() -> WindowAttributes {
WindowAttributes { WindowAttributes {
dimensions: None, dimensions: None,
min_dimensions: None,
max_dimensions: None,
monitor: None, monitor: None,
title: "glutin window".to_owned(), title: "glutin window".to_owned(),
visible: true, visible: true,

View file

@ -53,6 +53,24 @@ impl<'a> WindowBuilder<'a> {
self self
} }
/// Sets a minimum dimension size for the window
///
/// Width and height are in pixels.
#[inline]
pub fn with_min_dimensions(mut self, width: u32, height: u32) -> WindowBuilder<'a> {
self.window.min_dimensions = Some((width, height));
self
}
/// Sets a maximum dimension size for the window
///
/// Width and height are in pixels.
#[inline]
pub fn with_max_dimensions(mut self, width: u32, height: u32) -> WindowBuilder<'a> {
self.window.max_dimensions = Some((width, height));
self
}
/// Requests a specific title for the window. /// Requests a specific title for the window.
#[inline] #[inline]
pub fn with_title(mut self, title: String) -> WindowBuilder<'a> { pub fn with_title(mut self, title: String) -> WindowBuilder<'a> {