From 52d44769a332d1d96a5e9292805a5884073b9185 Mon Sep 17 00:00:00 2001 From: TimUntersberger Date: Fri, 25 Jun 2021 15:38:30 +0200 Subject: add initial attempt at adding winit example --- Cargo.toml | 1 + examples/winit/Cargo.toml | 9 +++++++ examples/winit/README.md | 18 ++++++++++++++ examples/winit/src/main.rs | 62 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+) create mode 100644 examples/winit/Cargo.toml create mode 100644 examples/winit/README.md create mode 100644 examples/winit/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index 329877c8..b5ceb20f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -67,6 +67,7 @@ members = [ "examples/counter", "examples/custom_widget", "examples/download_progress", + "examples/winit", "examples/events", "examples/game_of_life", "examples/geometry", diff --git a/examples/winit/Cargo.toml b/examples/winit/Cargo.toml new file mode 100644 index 00000000..6d16a7a9 --- /dev/null +++ b/examples/winit/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "counter_winit" +version = "0.1.0" +authors = ["Héctor Ramón Jiménez "] +edition = "2018" +publish = false + +[dependencies] +iced_winit = { path = "../../winit" } diff --git a/examples/winit/README.md b/examples/winit/README.md new file mode 100644 index 00000000..4d9fc5b9 --- /dev/null +++ b/examples/winit/README.md @@ -0,0 +1,18 @@ +## Counter + +The classic counter example explained in the [`README`](../../README.md). + +The __[`main`]__ file contains all the code of the example. + +
+ + + +
+ +You can run it with `cargo run`: +``` +cargo run --package counter +``` + +[`main`]: src/main.rs diff --git a/examples/winit/src/main.rs b/examples/winit/src/main.rs new file mode 100644 index 00000000..890ded79 --- /dev/null +++ b/examples/winit/src/main.rs @@ -0,0 +1,62 @@ +use iced_winit::{button, Align, Button, Column, Element, Application, Settings, Text, Renderer, Program, Command}; + +pub fn main() { + Counter::run(Settings::default()).unwrap() +} + +#[derive(Default)] +struct Counter { + value: i32, + increment_button: button::State, + decrement_button: button::State, +} + +#[derive(Debug, Clone, Copy)] +enum Message { + IncrementPressed, + DecrementPressed, +} + +impl Application for Counter { + type Flags = (); + + fn new(flags: Self::Flags) -> (Self, Command) { + (Self::default(), Command::none()) + } + + fn title(&self) -> String { + String::from("Counter with winit - Iced") + } +} + +impl Program for Counter { + type Renderer = Renderer; + type Message = Message; + + fn update(&mut self, message: Message) { + match message { + Message::IncrementPressed => { + self.value += 1; + } + Message::DecrementPressed => { + self.value -= 1; + } + } + } + + fn view(&mut self) -> Element { + Column::new() + .padding(20) + .align_items(Align::Center) + .push( + Button::new(&mut self.increment_button, Text::new("Increment")) + .on_press(Message::IncrementPressed), + ) + .push(Text::new(self.value.to_string()).size(50)) + .push( + Button::new(&mut self.decrement_button, Text::new("Decrement")) + .on_press(Message::DecrementPressed), + ) + .into() + } +} -- cgit From 5c45d36d1a8cfd92cd1a454a7f4deedcd4d13fe7 Mon Sep 17 00:00:00 2001 From: TimUntersberger Date: Fri, 25 Jun 2021 17:16:44 +0200 Subject: wip --- examples/winit/Cargo.toml | 2 +- examples/winit/src/main.rs | 27 +++++++++++++++------------ src/window/settings.rs | 7 +++++++ winit/src/settings.rs | 4 ++++ 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/examples/winit/Cargo.toml b/examples/winit/Cargo.toml index 6d16a7a9..2d58f031 100644 --- a/examples/winit/Cargo.toml +++ b/examples/winit/Cargo.toml @@ -6,4 +6,4 @@ edition = "2018" publish = false [dependencies] -iced_winit = { path = "../../winit" } +iced = { path = "../.." } diff --git a/examples/winit/src/main.rs b/examples/winit/src/main.rs index 890ded79..202d9b5a 100644 --- a/examples/winit/src/main.rs +++ b/examples/winit/src/main.rs @@ -1,7 +1,15 @@ -use iced_winit::{button, Align, Button, Column, Element, Application, Settings, Text, Renderer, Program, Command}; +use iced::{button, Align, Button, Column, Element, Sandbox, Settings, window::Settings as WindowSettings, Text}; pub fn main() { - Counter::run(Settings::default()).unwrap() + let settings = Settings { + window: WindowSettings { + size: (400, 200), + position: (100, 100), + ..Default::default() + }, + ..Default::default() + }; + Counter::run(settings).unwrap() } #[derive(Default)] @@ -17,21 +25,16 @@ enum Message { DecrementPressed, } -impl Application for Counter { - type Flags = (); +impl Sandbox for Counter { + type Message = Message; - fn new(flags: Self::Flags) -> (Self, Command) { - (Self::default(), Command::none()) + fn new() -> Self { + Self::default() } fn title(&self) -> String { String::from("Counter with winit - Iced") } -} - -impl Program for Counter { - type Renderer = Renderer; - type Message = Message; fn update(&mut self, message: Message) { match message { @@ -44,7 +47,7 @@ impl Program for Counter { } } - fn view(&mut self) -> Element { + fn view(&mut self) -> Element { Column::new() .padding(20) .align_items(Align::Center) diff --git a/src/window/settings.rs b/src/window/settings.rs index 6b5d2985..981f0858 100644 --- a/src/window/settings.rs +++ b/src/window/settings.rs @@ -6,6 +6,11 @@ pub struct Settings { /// The initial size of the window. pub size: (u32, u32), + /// The initial position of the window. + /// + /// Note: this gets ignored on the web + pub position: (u32, u32), + /// The minimum size of the window. pub min_size: Option<(u32, u32)>, @@ -32,6 +37,7 @@ impl Default for Settings { fn default() -> Settings { Settings { size: (1024, 768), + position: (100, 100), min_size: None, max_size: None, resizable: true, @@ -48,6 +54,7 @@ impl From for iced_winit::settings::Window { fn from(settings: Settings) -> Self { Self { size: settings.size, + position: settings.position, min_size: settings.min_size, max_size: settings.max_size, resizable: settings.resizable, diff --git a/winit/src/settings.rs b/winit/src/settings.rs index 941d88ce..0508cd9b 100644 --- a/winit/src/settings.rs +++ b/winit/src/settings.rs @@ -35,6 +35,9 @@ pub struct Window { /// The size of the window. pub size: (u32, u32), + /// The position of the window. + pub position: (u32, u32), + /// The minimum size of the window. pub min_size: Option<(u32, u32)>, @@ -75,6 +78,7 @@ impl Window { window_builder = window_builder .with_title(title) .with_inner_size(winit::dpi::LogicalSize { width, height }) + .with_outer_position(self.position) .with_resizable(self.resizable) .with_decorations(self.decorations) .with_transparent(self.transparent) -- cgit From de79a01b88e1610d374ed06077ac78f3e10b9c3d Mon Sep 17 00:00:00 2001 From: TimUntersberger Date: Fri, 25 Jun 2021 18:03:18 +0200 Subject: done --- examples/winit/src/main.rs | 63 ++++++++++++++++------------------------------ src/window/settings.rs | 17 ++++++++++++- winit/src/settings.rs | 5 ++-- 3 files changed, 41 insertions(+), 44 deletions(-) diff --git a/examples/winit/src/main.rs b/examples/winit/src/main.rs index 202d9b5a..a1364ea6 100644 --- a/examples/winit/src/main.rs +++ b/examples/winit/src/main.rs @@ -1,65 +1,46 @@ -use iced::{button, Align, Button, Column, Element, Sandbox, Settings, window::Settings as WindowSettings, Text}; +use iced::{Column, Element, Sandbox, Settings, window::Settings as WindowSettings}; + +const WINDOW_WIDTH: i32 = 200; +const WINDOW_HEIGHT: i32 = 200; +const DISPLAY_WIDTH: i32 = 1920; +const DISPLAY_HEIGHT: i32 = 1080; +// These numbers are specific to a 1920x1080 monitor +const BORDER_X: i32 = 8; +const BORDER_Y: i32 = 2; +const CAPTION_HEIGHT: i32 = 4; pub fn main() { + let x = DISPLAY_WIDTH / 2 - WINDOW_WIDTH / 2 - BORDER_X; + let y = DISPLAY_HEIGHT / 2 - WINDOW_HEIGHT / 2 - BORDER_Y - CAPTION_HEIGHT; let settings = Settings { window: WindowSettings { - size: (400, 200), - position: (100, 100), + size: (WINDOW_WIDTH as u32, WINDOW_HEIGHT as u32), + position: (x, y), ..Default::default() }, ..Default::default() }; - Counter::run(settings).unwrap() + Winit::run(settings).unwrap() } #[derive(Default)] -struct Counter { - value: i32, - increment_button: button::State, - decrement_button: button::State, -} - -#[derive(Debug, Clone, Copy)] -enum Message { - IncrementPressed, - DecrementPressed, -} +struct Winit; -impl Sandbox for Counter { - type Message = Message; +impl Sandbox for Winit { + type Message = (); fn new() -> Self { Self::default() } fn title(&self) -> String { - String::from("Counter with winit - Iced") + String::from("winit - Iced") } - fn update(&mut self, message: Message) { - match message { - Message::IncrementPressed => { - self.value += 1; - } - Message::DecrementPressed => { - self.value -= 1; - } - } + fn update(&mut self, _message: Self::Message) { } - fn view(&mut self) -> Element { - Column::new() - .padding(20) - .align_items(Align::Center) - .push( - Button::new(&mut self.increment_button, Text::new("Increment")) - .on_press(Message::IncrementPressed), - ) - .push(Text::new(self.value.to_string()).size(50)) - .push( - Button::new(&mut self.decrement_button, Text::new("Decrement")) - .on_press(Message::DecrementPressed), - ) - .into() + fn view(&mut self) -> Element { + Column::new().into() } } diff --git a/src/window/settings.rs b/src/window/settings.rs index 981f0858..0fecc3bb 100644 --- a/src/window/settings.rs +++ b/src/window/settings.rs @@ -8,8 +8,23 @@ pub struct Settings { /// The initial position of the window. /// + /// When the decorations of the window are enabled, Windows 10 will add some inivisble 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 + /// -DPI_BORDER_X,-DPI_BORDER_Y. + /// + /// DPI_BORDER_X/DPI_BORDER_Y are the usual size of the padding, which changes based on the DPI of the display. + /// + /// On a 1920x1080 monitor you would have to set the position to -8,-2. + /// + /// For info on how you could implement positioning that supports all DPI monitors look at the + /// following WINAPI calls: + /// + /// * GetDpiForMonitor (with MDT_RAW_DPI) + /// * GetSystemMetricsForDpi (with SM_CXFRAME and SM_CYFRAME) + /// /// Note: this gets ignored on the web - pub position: (u32, u32), + pub position: (i32, i32), /// The minimum size of the window. pub min_size: Option<(u32, u32)>, diff --git a/winit/src/settings.rs b/winit/src/settings.rs index 0508cd9b..1769c676 100644 --- a/winit/src/settings.rs +++ b/winit/src/settings.rs @@ -36,7 +36,7 @@ pub struct Window { pub size: (u32, u32), /// The position of the window. - pub position: (u32, u32), + pub position: (i32, i32), /// The minimum size of the window. pub min_size: Option<(u32, u32)>, @@ -78,7 +78,7 @@ impl Window { window_builder = window_builder .with_title(title) .with_inner_size(winit::dpi::LogicalSize { width, height }) - .with_outer_position(self.position) + .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) @@ -116,6 +116,7 @@ impl Default for Window { fn default() -> Window { Window { size: (1024, 768), + position: (100, 100), min_size: None, max_size: None, resizable: true, -- cgit From 552b7a1307ceaf83d00cbee88dc33ae5d92ee53e Mon Sep 17 00:00:00 2001 From: TimUntersberger Date: Fri, 25 Jun 2021 18:07:03 +0200 Subject: update cargo.toml of example --- examples/winit/Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/winit/Cargo.toml b/examples/winit/Cargo.toml index 2d58f031..2286dfc9 100644 --- a/examples/winit/Cargo.toml +++ b/examples/winit/Cargo.toml @@ -1,7 +1,7 @@ [package] -name = "counter_winit" +name = "winit" version = "0.1.0" -authors = ["Héctor Ramón Jiménez "] +authors = ["Tim Untersberger "] edition = "2018" publish = false -- cgit From 4de27142133b2dd6f780f0f955f620787102e581 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 21 Jul 2021 18:11:31 +0700 Subject: Remove `winit` example ... we can extend an existing example instead! --- Cargo.toml | 1 - examples/winit/Cargo.toml | 9 --------- examples/winit/README.md | 18 ------------------ examples/winit/src/main.rs | 46 ---------------------------------------------- 4 files changed, 74 deletions(-) delete mode 100644 examples/winit/Cargo.toml delete mode 100644 examples/winit/README.md delete mode 100644 examples/winit/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index b5ceb20f..329877c8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -67,7 +67,6 @@ members = [ "examples/counter", "examples/custom_widget", "examples/download_progress", - "examples/winit", "examples/events", "examples/game_of_life", "examples/geometry", diff --git a/examples/winit/Cargo.toml b/examples/winit/Cargo.toml deleted file mode 100644 index 2286dfc9..00000000 --- a/examples/winit/Cargo.toml +++ /dev/null @@ -1,9 +0,0 @@ -[package] -name = "winit" -version = "0.1.0" -authors = ["Tim Untersberger "] -edition = "2018" -publish = false - -[dependencies] -iced = { path = "../.." } diff --git a/examples/winit/README.md b/examples/winit/README.md deleted file mode 100644 index 4d9fc5b9..00000000 --- a/examples/winit/README.md +++ /dev/null @@ -1,18 +0,0 @@ -## Counter - -The classic counter example explained in the [`README`](../../README.md). - -The __[`main`]__ file contains all the code of the example. - - - -You can run it with `cargo run`: -``` -cargo run --package counter -``` - -[`main`]: src/main.rs diff --git a/examples/winit/src/main.rs b/examples/winit/src/main.rs deleted file mode 100644 index a1364ea6..00000000 --- a/examples/winit/src/main.rs +++ /dev/null @@ -1,46 +0,0 @@ -use iced::{Column, Element, Sandbox, Settings, window::Settings as WindowSettings}; - -const WINDOW_WIDTH: i32 = 200; -const WINDOW_HEIGHT: i32 = 200; -const DISPLAY_WIDTH: i32 = 1920; -const DISPLAY_HEIGHT: i32 = 1080; -// These numbers are specific to a 1920x1080 monitor -const BORDER_X: i32 = 8; -const BORDER_Y: i32 = 2; -const CAPTION_HEIGHT: i32 = 4; - -pub fn main() { - let x = DISPLAY_WIDTH / 2 - WINDOW_WIDTH / 2 - BORDER_X; - let y = DISPLAY_HEIGHT / 2 - WINDOW_HEIGHT / 2 - BORDER_Y - CAPTION_HEIGHT; - let settings = Settings { - window: WindowSettings { - size: (WINDOW_WIDTH as u32, WINDOW_HEIGHT as u32), - position: (x, y), - ..Default::default() - }, - ..Default::default() - }; - Winit::run(settings).unwrap() -} - -#[derive(Default)] -struct Winit; - -impl Sandbox for Winit { - type Message = (); - - fn new() -> Self { - Self::default() - } - - fn title(&self) -> String { - String::from("winit - Iced") - } - - fn update(&mut self, _message: Self::Message) { - } - - fn view(&mut self) -> Element { - Column::new().into() - } -} -- cgit From 6793a7e00dc96425ae720309182ec8b03c076111 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 21 Jul 2021 18:15:32 +0700 Subject: Remove DPI from docs in `window::Settings::position` --- src/window/settings.rs | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/src/window/settings.rs b/src/window/settings.rs index 0fecc3bb..59654edc 100644 --- a/src/window/settings.rs +++ b/src/window/settings.rs @@ -8,22 +8,11 @@ pub struct Settings { /// The initial position of the window. /// - /// When the decorations of the window are enabled, Windows 10 will add some inivisble 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 - /// -DPI_BORDER_X,-DPI_BORDER_Y. - /// - /// DPI_BORDER_X/DPI_BORDER_Y are the usual size of the padding, which changes based on the DPI of the display. - /// - /// On a 1920x1080 monitor you would have to set the position to -8,-2. - /// - /// For info on how you could implement positioning that supports all DPI monitors look at the - /// following WINAPI calls: - /// - /// * GetDpiForMonitor (with MDT_RAW_DPI) - /// * GetSystemMetricsForDpi (with SM_CXFRAME and SM_CYFRAME) - /// - /// Note: this gets ignored on the web + /// 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), /// The minimum size of the window. -- cgit From 72b3bf95de3f11812b1541fe2ffea76a515aee79 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 21 Jul 2021 18:59:24 +0700 Subject: Improve `window::Position` API --- src/window.rs | 2 ++ src/window/position.rs | 33 +++++++++++++++++++++++++++++++++ src/window/settings.rs | 14 ++++---------- winit/src/conversion.rs | 45 ++++++++++++++++++++++++++++++++++++++++++++- winit/src/lib.rs | 2 ++ winit/src/position.rs | 22 ++++++++++++++++++++++ winit/src/settings.rs | 19 ++++++++++++++----- 7 files changed, 121 insertions(+), 16 deletions(-) create mode 100644 src/window/position.rs create mode 100644 winit/src/position.rs diff --git a/src/window.rs b/src/window.rs index a2883b62..7d441062 100644 --- a/src/window.rs +++ b/src/window.rs @@ -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; diff --git a/src/window/position.rs b/src/window/position.rs new file mode 100644 index 00000000..8535ef6a --- /dev/null +++ b/src/window/position.rs @@ -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 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), + } + } +} diff --git a/src/window/settings.rs b/src/window/settings.rs index 59654edc..ec6c3071 100644 --- a/src/window/settings.rs +++ b/src/window/settings.rs @@ -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 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, diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index b850a805..f785fce6 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -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 { + 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 = + monitor.size().to_logical(monitor.scale_factor()); + + let centered: winit::dpi::PhysicalPosition = + 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 diff --git a/winit/src/lib.rs b/winit/src/lib.rs index c9f324dd..1707846a 100644 --- a/winit/src/lib.rs +++ b/winit/src/lib.rs @@ -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; diff --git a/winit/src/position.rs b/winit/src/position.rs new file mode 100644 index 00000000..c260c29e --- /dev/null +++ b/winit/src/position.rs @@ -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 + } +} diff --git a/winit/src/settings.rs b/winit/src/settings.rs index 1769c676..743f79bc 100644 --- a/winit/src/settings.rs +++ b/winit/src/settings.rs @@ -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, -- cgit From 39b8f7de50ca53c67214c4ec2af2d3a0944f006a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 21 Jul 2021 19:10:11 +0700 Subject: Center window in `game_of_life` example --- examples/game_of_life/src/main.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/examples/game_of_life/src/main.rs b/examples/game_of_life/src/main.rs index 64599163..642262b0 100644 --- a/examples/game_of_life/src/main.rs +++ b/examples/game_of_life/src/main.rs @@ -9,6 +9,7 @@ use iced::executor; use iced::pick_list::{self, PickList}; use iced::slider::{self, Slider}; use iced::time; +use iced::window; use iced::{ Align, Application, Checkbox, Clipboard, Column, Command, Container, Element, Length, Row, Settings, Subscription, Text, @@ -19,6 +20,10 @@ use std::time::{Duration, Instant}; pub fn main() -> iced::Result { GameOfLife::run(Settings { antialiasing: true, + window: window::Settings { + position: window::Position::Centered, + ..window::Settings::default() + }, ..Settings::default() }) } -- cgit