Improve window::Position API

This commit is contained in:
Héctor Ramón Jiménez 2021-07-21 18:59:24 +07:00
parent 6793a7e00d
commit 72b3bf95de
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
7 changed files with 121 additions and 16 deletions

View file

@ -1,9 +1,11 @@
//! Configure the window of your application in native platforms.
mod mode;
mod position;
mod settings;
pub mod icon;
pub use icon::Icon;
pub use mode::Mode;
pub use position::Position;
pub use settings::Settings;

33
src/window/position.rs Normal file
View file

@ -0,0 +1,33 @@
/// The position of a window in a given screen.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Position {
/// The platform-specific default position for a new window.
Default,
/// The window is completely centered on the screen.
Centered,
/// The window is positioned with specific coordinates: `(X, Y)`.
///
/// When the decorations of the window are enabled, Windows 10 will add some
/// invisible padding to the window. This padding gets included in the
/// position. So if you have decorations enabled and want the window to be
/// at (0, 0) you would have to set the position to
/// `(PADDING_X, PADDING_Y)`.
Specific(i32, i32),
}
impl Default for Position {
fn default() -> Self {
Self::Default
}
}
#[cfg(not(target_arch = "wasm32"))]
impl From<Position> for iced_winit::Position {
fn from(position: Position) -> Self {
match position {
Position::Default => Self::Default,
Position::Centered => Self::Centered,
Position::Specific(x, y) => Self::Specific(x, y),
}
}
}

View file

@ -1,4 +1,4 @@
use crate::window::Icon;
use crate::window::{Icon, Position};
/// The window settings of an application.
#[derive(Debug, Clone)]
@ -7,13 +7,7 @@ pub struct Settings {
pub size: (u32, u32),
/// The initial position of the window.
///
/// When the decorations of the window are enabled, Windows 10 will add some
/// invisible padding to the window. This padding gets included in the
/// position. So if you have decorations enabled and want the window to be
/// at (0, 0) you would have to set the position to
/// `(PADDING_X, PADDING_Y)`.
pub position: (i32, i32),
pub position: Position,
/// The minimum size of the window.
pub min_size: Option<(u32, u32)>,
@ -41,7 +35,7 @@ impl Default for Settings {
fn default() -> Settings {
Settings {
size: (1024, 768),
position: (100, 100),
position: Position::default(),
min_size: None,
max_size: None,
resizable: true,
@ -58,7 +52,7 @@ impl From<Settings> for iced_winit::settings::Window {
fn from(settings: Settings) -> Self {
Self {
size: settings.size,
position: settings.position,
position: iced_winit::Position::from(settings.position),
min_size: settings.min_size,
max_size: settings.max_size,
resizable: settings.resizable,

View file

@ -6,7 +6,7 @@ use crate::keyboard;
use crate::mouse;
use crate::touch;
use crate::window;
use crate::{Event, Mode, Point};
use crate::{Event, Mode, Point, Position};
/// Converts a winit window event into an iced event.
pub fn window_event(
@ -133,6 +133,49 @@ pub fn window_event(
}
}
/// Converts a [`Position`] to a [`winit`] logical position for a given monitor.
///
/// [`winit`]: https://github.com/rust-windowing/winit
pub fn position(
monitor: Option<&winit::monitor::MonitorHandle>,
(width, height): (u32, u32),
position: Position,
) -> Option<winit::dpi::Position> {
match position {
Position::Default => None,
Position::Specific(x, y) => {
Some(winit::dpi::Position::Logical(winit::dpi::LogicalPosition {
x: f64::from(x),
y: f64::from(y),
}))
}
Position::Centered => {
if let Some(monitor) = monitor {
let start = monitor.position();
let resolution: winit::dpi::LogicalSize<f64> =
monitor.size().to_logical(monitor.scale_factor());
let centered: winit::dpi::PhysicalPosition<i32> =
winit::dpi::LogicalPosition {
x: (resolution.width - f64::from(width)) / 2.0,
y: (resolution.height - f64::from(height)) / 2.0,
}
.to_physical(monitor.scale_factor());
Some(winit::dpi::Position::Physical(
winit::dpi::PhysicalPosition {
x: start.x + centered.x,
y: start.y + centered.y,
},
))
} else {
None
}
}
}
}
/// Converts a [`Mode`] to a [`winit`] fullscreen mode.
///
/// [`winit`]: https://github.com/rust-windowing/winit

View file

@ -31,12 +31,14 @@ pub mod settings;
mod clipboard;
mod error;
mod mode;
mod position;
mod proxy;
pub use application::Application;
pub use clipboard::Clipboard;
pub use error::Error;
pub use mode::Mode;
pub use position::Position;
pub use proxy::Proxy;
pub use settings::Settings;

22
winit/src/position.rs Normal file
View file

@ -0,0 +1,22 @@
/// The position of a window in a given screen.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Position {
/// The platform-specific default position for a new window.
Default,
/// The window is completely centered on the screen.
Centered,
/// The window is positioned with specific coordinates: `(X, Y)`.
///
/// When the decorations of the window are enabled, Windows 10 will add some
/// invisible padding to the window. This padding gets included in the
/// position. So if you have decorations enabled and want the window to be
/// at (0, 0) you would have to set the position to
/// `(PADDING_X, PADDING_Y)`.
Specific(i32, i32),
}
impl Default for Position {
fn default() -> Self {
Self::Default
}
}

View file

@ -9,7 +9,7 @@ mod platform;
pub use platform::PlatformSpecific;
use crate::conversion;
use crate::Mode;
use crate::{Mode, Position};
use winit::monitor::MonitorHandle;
use winit::window::WindowBuilder;
@ -36,7 +36,7 @@ pub struct Window {
pub size: (u32, u32),
/// The position of the window.
pub position: (i32, i32),
pub position: Position,
/// The minimum size of the window.
pub min_size: Option<(u32, u32)>,
@ -78,15 +78,21 @@ impl Window {
window_builder = window_builder
.with_title(title)
.with_inner_size(winit::dpi::LogicalSize { width, height })
.with_position(winit::dpi::LogicalPosition { x: self.position.0, y: self.position.1 })
.with_resizable(self.resizable)
.with_decorations(self.decorations)
.with_transparent(self.transparent)
.with_window_icon(self.icon)
.with_always_on_top(self.always_on_top)
.with_fullscreen(conversion::fullscreen(primary_monitor, mode))
.with_visible(conversion::visible(mode));
if let Some(position) = conversion::position(
primary_monitor.as_ref(),
self.size,
self.position,
) {
window_builder = window_builder.with_position(position);
}
if let Some((width, height)) = self.min_size {
window_builder = window_builder
.with_min_inner_size(winit::dpi::LogicalSize { width, height });
@ -108,6 +114,9 @@ impl Window {
.with_drag_and_drop(self.platform_specific.drag_and_drop);
}
window_builder = window_builder
.with_fullscreen(conversion::fullscreen(primary_monitor, mode));
window_builder
}
}
@ -116,7 +125,7 @@ impl Default for Window {
fn default() -> Window {
Window {
size: (1024, 768),
position: (100, 100),
position: Position::default(),
min_size: None,
max_size: None,
resizable: true,