Skip to content
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

Implement WindowBuilder::with_outer_position for X11 #1189

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/platform_impl/linux/x11/util/hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,24 @@ impl<'a> NormalHints<'a> {
}
}

pub fn get_position(&self) -> Option<(i32, i32)> {
if self.has_flag(ffi::PPosition) {
Some((self.size_hints.x as i32, self.size_hints.y as i32))
} else {
None
}
}

pub fn set_position(&mut self, position: Option<(i32, i32)>) {
if let Some((x, y)) = position {
self.size_hints.flags |= ffi::PPosition;
self.size_hints.x = x as c_int;
self.size_hints.y = y as c_int;
} else {
self.size_hints.flags &= !ffi::PPosition;
}
}

pub fn get_size(&self) -> Option<(u32, u32)> {
self.getter(ffi::PSize, &self.size_hints.width, &self.size_hints.height)
}
Expand Down
20 changes: 15 additions & 5 deletions src/platform_impl/linux/x11/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ impl UnownedWindow {
.min_inner_size
.map(|size| size.to_physical(dpi_factor).into());

let position = window_attrs
.outer_position
.map(|position| (position.x as i32, position.y as i32));

let dimensions = {
// x11 only applies constraints when the window is actively resized
// by the user, so we have to manually apply the initial constraints
Expand Down Expand Up @@ -196,8 +200,8 @@ impl UnownedWindow {
(xconn.xlib.XCreateWindow)(
xconn.display,
root,
0,
0,
position.map_or(0, |p| p.0 as c_int),
position.map_or(0, |p| p.1 as c_int),
dimensions.0 as c_uint,
dimensions.1 as c_uint,
0,
Expand All @@ -223,7 +227,7 @@ impl UnownedWindow {
)
};

let window = UnownedWindow {
let mut window = UnownedWindow {
xconn: Arc::clone(xconn),
xwindow,
root,
Expand Down Expand Up @@ -326,6 +330,7 @@ impl UnownedWindow {
}

let mut normal_hints = util::NormalHints::new(xconn);
normal_hints.set_position(position);
normal_hints.set_size(Some(dimensions));
normal_hints.set_min_size(min_inner_size.map(Into::into));
normal_hints.set_max_size(max_inner_size.map(Into::into));
Expand Down Expand Up @@ -414,8 +419,13 @@ impl UnownedWindow {
if window_attrs.fullscreen.is_some() {
window
.set_fullscreen_inner(window_attrs.fullscreen.clone())
.unwrap()
.queue();
.map(|flusher| flusher.queue());

if let Some((x, y)) = position {
let shared_state = window.shared_state.get_mut();

shared_state.restore_position = Some((x, y));
}
}
if window_attrs.always_on_top {
window
Expand Down
18 changes: 18 additions & 0 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ pub struct WindowAttributes {
/// The default is `None`.
pub max_inner_size: Option<LogicalSize>,

/// The desired position of the window. If this is `None`,
/// some platform-specific position will be chosen.
///
/// The default is `None`.
murarth marked this conversation as resolved.
Show resolved Hide resolved
pub outer_position: Option<LogicalPosition>,

/// Whether the window is resizable or not.
///
/// The default is `true`.
Expand Down Expand Up @@ -168,6 +174,7 @@ impl Default for WindowAttributes {
inner_size: None,
min_inner_size: None,
max_inner_size: None,
outer_position: None,
resizable: true,
title: "winit window".to_owned(),
maximized: false,
Expand Down Expand Up @@ -212,6 +219,17 @@ impl WindowBuilder {
self
}

/// Sets a desired initial position for the window
///
/// ## Platform-specific
///
/// **Wayland**: This has no effect.
#[inline]
pub fn with_outer_position(mut self, position: LogicalPosition) -> Self {
self.window.outer_position = Some(position);
self
}

/// Sets whether the window is resizable or not
///
/// Note that making the window unresizable doesn't exempt you from handling `Resized`, as that event can still be
Expand Down