-
Notifications
You must be signed in to change notification settings - Fork 903
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Windows & X11: Window::set_resizable #558
Changes from 2 commits
da3bd4d
1933165
dd1022a
7351b4c
eff0012
ebc930c
098ad70
13978a8
3195c2a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
extern crate winit; | ||
|
||
fn main() { | ||
let mut events_loop = winit::EventsLoop::new(); | ||
|
||
let window = winit::WindowBuilder::new() | ||
.with_title("Hit space to toggle resizability.") | ||
.with_dimensions(400, 200) | ||
.with_resizable(false) | ||
.build(&events_loop) | ||
.unwrap(); | ||
|
||
let mut resizable = false; | ||
|
||
events_loop.run_forever(|event| match event { | ||
winit::Event::WindowEvent { event, .. } => match event { | ||
winit::WindowEvent::CloseRequested => winit::ControlFlow::Break, | ||
winit::WindowEvent::KeyboardInput { | ||
input: | ||
winit::KeyboardInput { | ||
virtual_keycode: Some(winit::VirtualKeyCode::Space), | ||
state: winit::ElementState::Released, | ||
.. | ||
}, | ||
.. | ||
} => { | ||
resizable = !resizable; | ||
println!("Resizable: {}", resizable); | ||
window.set_resizable(resizable); | ||
winit::ControlFlow::Continue | ||
} | ||
_ => winit::ControlFlow::Continue, | ||
}, | ||
_ => winit::ControlFlow::Continue, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As an official winit example™, it would be good if you refactored this to not repeat |
||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -422,7 +422,7 @@ pub struct WindowAttributes { | |
/// The default is `None`. | ||
pub max_dimensions: Option<(u32, u32)>, | ||
|
||
/// [Windows & X11 only] Whether the window is resizable or not | ||
/// [Windows, MacOS, and X11 only] Whether the window is resizable or not | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like you followed the precedent set by
|
||
/// | ||
/// The default is `true`. | ||
pub resizable: bool, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -232,6 +232,15 @@ impl Window { | |
&Window::Wayland(ref w) => w.set_max_dimensions(dimensions) | ||
} | ||
} | ||
|
||
#[inline] | ||
pub fn set_resizable(&self, resizable: bool) { | ||
match self { | ||
&Window::X(ref w) => w.set_resizable(resizable), | ||
// &Window::Wayland(ref w) => w.set_resizable(resizable), | ||
_ => {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We tend to favor The idea is that it makes people more likely to fix it... in my experience, all it does is make people more likely to yell at us, though that does make me more likely to fix it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've released version 0.2.2 of smithay-client-toolkit which adds a |
||
} | ||
} | ||
|
||
#[inline] | ||
pub fn set_cursor(&self, cursor: MouseCursor) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -726,6 +726,26 @@ impl UnownedWindow { | |
}.expect("Failed to call XSetWMNormalHints"); | ||
} | ||
|
||
pub fn set_resizable(&self, resizable: bool) { | ||
unsafe { | ||
self.update_normal_hints(|size_hints| { | ||
if resizable { | ||
(*size_hints).flags &= !ffi::PMinSize; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can cut the number of flag changes in half in this function. |
||
(*size_hints).flags &= !ffi::PMaxSize; | ||
} else { | ||
(*size_hints).flags |= ffi::PMinSize; | ||
(*size_hints).flags |= ffi::PMaxSize; | ||
if let Some((width, height)) = self.get_inner_size() { | ||
(*size_hints).min_width = width as c_int; | ||
(*size_hints).min_height = height as c_int; | ||
(*size_hints).max_width = width as c_int; | ||
(*size_hints).max_height = height as c_int; | ||
} | ||
} | ||
}) | ||
}.expect("Failed to call XSetWMNormalHints"); | ||
} | ||
|
||
#[inline] | ||
pub fn get_xlib_display(&self) -> *mut c_void { | ||
self.xconn.display as _ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -239,6 +239,33 @@ impl Window { | |
} | ||
} | ||
} | ||
|
||
/// See the docs in the crate root file. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need to add this comment, since #548 completely removes them... they don't seem to be at all helpful, especially since these are doc comments on private methods. More importantly, what happens when you use this method while the window is fullscreen? |
||
#[inline] | ||
pub fn set_resizable(&self, resizable: bool) { | ||
if let Ok(mut window_state) = self.window_state.lock() { | ||
if window_state.attributes.resizable == resizable { | ||
return; | ||
} | ||
let window = self.window.clone(); | ||
let mut style = unsafe { | ||
winuser::GetWindowLongW(self.window.0, winuser::GWL_STYLE) | ||
}; | ||
if resizable { | ||
style |= winuser::WS_SIZEBOX as i32; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer you use the winapi |
||
} else { | ||
style &= !winuser::WS_SIZEBOX as i32; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the precedence here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The precedence for removing the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh lol. The precedence is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I just wanted to make sure it was zero-extending before flipping the bits. Or rather, I wanted to make sure you thought about that. |
||
} | ||
unsafe { | ||
winuser::SetWindowLongW( | ||
window.0, | ||
winuser::GWL_STYLE, | ||
style as _, | ||
); | ||
}; | ||
window_state.attributes.resizable = resizable; | ||
} | ||
} | ||
|
||
// TODO: remove | ||
pub fn platform_display(&self) -> *mut ::libc::c_void { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The correct stylization is "macOS", and it also reads a little easier if you use the same ordering as in the previous line.