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

feat: position field on WindowAttribute #219

Merged
merged 1 commit into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions .changes/position.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"wry": minor
---

Add position field on WindowAttribute

43 changes: 43 additions & 0 deletions src/application/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,31 @@ pub struct WindowAttributes {
/// The default is `None`.
pub max_inner_size: Option<Size>,

/// The desired position of the window. If this is `None`, some platform-specific position
/// will be chosen.
///
/// The default is `None`.
///
/// ## Platform-specific
///
/// - **macOS**: The top left corner position of the window content, the window's "inner"
/// position. The window title bar will be placed above it.
/// The window will be positioned such that it fits on screen, maintaining
/// set `inner_size` if any.
/// If you need to precisely position the top left corner of the whole window you have to
/// use [`Window::set_outer_position`] after creating the window.
/// - **Windows**: The top left corner position of the window title bar, the window's "outer"
/// position.
/// There may be a small gap between this position and the window due to the specifics of the
/// Window Manager.
/// - **X11**: The top left corner of the window, the window's "outer" position.
/// - **Others**: Ignored.
///
/// See [`Window::set_outer_position`].
///
/// [`Window::set_outer_position`]: crate::window::Window::set_outer_position
pub position: Option<Position>,

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

/// Sets a desired initial position for the window.
///
/// See [`WindowAttributes::position`] for details.
///
/// [`WindowAttributes::position`]: crate::window::WindowAttributes::position
#[inline]
pub fn with_position<P: Into<Position>>(mut self, position: P) -> Self {
self.window.position = Some(position.into());
self
}

/// Sets whether the window is resizable or not.
///
/// See [`Window::set_resizable`] for details.
Expand Down Expand Up @@ -408,6 +445,12 @@ impl Window {
geom_mask,
);

// Set Position
if let Some(position) = attributes.position {
let (x, y): (i32, i32) = position.to_physical::<i32>(win_scale_factor as f64).into();
window.move_(x, y);
}

// Set Transparent
if attributes.transparent {
if let Some(screen) = window.get_screen() {
Expand Down