From d53ccc857da4d4cda769904342aeb5a82a64f146 Mon Sep 17 00:00:00 2001 From: Bingus Date: Wed, 12 Jul 2023 19:21:05 -0700 Subject: refactored window storage; new helper window events (Destroyed, Created); clippy + fmt; --- core/src/window/event.rs | 17 ++++++++++++++++ core/src/window/id.rs | 15 ++++---------- core/src/window/position.rs | 22 ++++++++++++++++++++ core/src/window/settings.rs | 40 +++++++++++++++++++------------------ core/src/window/settings/macos.rs | 12 +++++++++++ core/src/window/settings/other.rs | 3 +++ core/src/window/settings/wasm.rs | 11 ++++++++++ core/src/window/settings/windows.rs | 21 +++++++++++++++++++ 8 files changed, 111 insertions(+), 30 deletions(-) create mode 100644 core/src/window/settings/macos.rs create mode 100644 core/src/window/settings/other.rs create mode 100644 core/src/window/settings/wasm.rs create mode 100644 core/src/window/settings/windows.rs (limited to 'core/src/window') diff --git a/core/src/window/event.rs b/core/src/window/event.rs index e2fb5e66..3efce05e 100644 --- a/core/src/window/event.rs +++ b/core/src/window/event.rs @@ -1,4 +1,5 @@ use crate::time::Instant; +use crate::Size; use std::path::PathBuf; @@ -32,6 +33,22 @@ pub enum Event { /// occurs. CloseRequested, + /// A window was destroyed by the runtime. + Destroyed, + + /// A window was created. + /// + /// **Note:** this event is not supported on Wayland. + Created { + /// The position of the created window. This is relative to the top-left corner of the desktop + /// the window is on, including virtual desktops. Refers to window's "inner" position, + /// or the client area, in logical pixels. + position: (i32, i32), + /// The size of the created window. This is its "inner" size, or the size of the + /// client area, in logical pixels. + size: Size, + }, + /// A window was focused. Focused, diff --git a/core/src/window/id.rs b/core/src/window/id.rs index 0a11b1aa..65002d43 100644 --- a/core/src/window/id.rs +++ b/core/src/window/id.rs @@ -1,18 +1,17 @@ use std::collections::hash_map::DefaultHasher; -use std::fmt::{Display, Formatter}; use std::hash::{Hash, Hasher}; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] -/// The ID of the window. +/// The id of the window. /// -/// Internally Iced uses `window::Id::MAIN` as the first window spawned. +/// Internally Iced reserves `window::Id::MAIN` for the first window spawned. pub struct Id(u64); impl Id { - /// The reserved window ID for the primary window in an Iced application. + /// The reserved window [`Id`] for the first window in an Iced application. pub const MAIN: Self = Id(0); - /// Creates a new unique window ID. + /// Creates a new unique window [`Id`]. pub fn new(id: impl Hash) -> Id { let mut hasher = DefaultHasher::new(); id.hash(&mut hasher); @@ -20,9 +19,3 @@ impl Id { Id(hasher.finish()) } } - -impl Display for Id { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, "Id({})", self.0) - } -} diff --git a/core/src/window/position.rs b/core/src/window/position.rs index e69de29b..c260c29e 100644 --- a/core/src/window/position.rs +++ b/core/src/window/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/core/src/window/settings.rs b/core/src/window/settings.rs index 458b9232..20811e83 100644 --- a/core/src/window/settings.rs +++ b/core/src/window/settings.rs @@ -1,6 +1,26 @@ use crate::window::{Icon, Level, Position}; -pub use iced_winit::settings::PlatformSpecific; +#[cfg(target_os = "windows")] +#[path = "settings/windows.rs"] +mod platform; + +#[cfg(target_os = "macos")] +#[path = "settings/macos.rs"] +mod platform; + +#[cfg(target_arch = "wasm32")] +#[path = "settings/wasm.rs"] +mod platform; + +#[cfg(not(any( + target_os = "windows", + target_os = "macos", + target_arch = "wasm32" +)))] +#[path = "settings/other.rs"] +mod platform; + +pub use platform::PlatformSpecific; /// The window settings of an application. #[derive(Debug, Clone)] @@ -56,21 +76,3 @@ impl Default for Settings { } } } - -impl From for iced_winit::settings::Window { - fn from(settings: Settings) -> Self { - Self { - size: settings.size, - position: iced_winit::Position::from(settings.position), - min_size: settings.min_size, - max_size: settings.max_size, - visible: settings.visible, - resizable: settings.resizable, - decorations: settings.decorations, - transparent: settings.transparent, - level: settings.level, - icon: settings.icon.map(Icon::into), - platform_specific: settings.platform_specific, - } - } -} diff --git a/core/src/window/settings/macos.rs b/core/src/window/settings/macos.rs new file mode 100644 index 00000000..f86e63ad --- /dev/null +++ b/core/src/window/settings/macos.rs @@ -0,0 +1,12 @@ +//! Platform specific settings for macOS. + +/// The platform specific window settings of an application. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub struct PlatformSpecific { + /// Hides the window title. + pub title_hidden: bool, + /// Makes the titlebar transparent and allows the content to appear behind it. + pub titlebar_transparent: bool, + /// Makes the window content appear behind the titlebar. + pub fullsize_content_view: bool, +} diff --git a/core/src/window/settings/other.rs b/core/src/window/settings/other.rs new file mode 100644 index 00000000..b1103f62 --- /dev/null +++ b/core/src/window/settings/other.rs @@ -0,0 +1,3 @@ +/// The platform specific window settings of an application. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub struct PlatformSpecific; diff --git a/core/src/window/settings/wasm.rs b/core/src/window/settings/wasm.rs new file mode 100644 index 00000000..8e0f1bbc --- /dev/null +++ b/core/src/window/settings/wasm.rs @@ -0,0 +1,11 @@ +//! Platform specific settings for WebAssembly. + +/// The platform specific window settings of an application. +#[derive(Debug, Clone, PartialEq, Eq, Default)] +pub struct PlatformSpecific { + /// The identifier of a DOM element that will be replaced with the + /// application. + /// + /// If set to `None`, the application will be appended to the HTML body. + pub target: Option, +} diff --git a/core/src/window/settings/windows.rs b/core/src/window/settings/windows.rs new file mode 100644 index 00000000..45d753bd --- /dev/null +++ b/core/src/window/settings/windows.rs @@ -0,0 +1,21 @@ +//! Platform specific settings for Windows. +use raw_window_handle::RawWindowHandle; + +/// The platform specific window settings of an application. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct PlatformSpecific { + /// Parent window + pub parent: Option, + + /// Drag and drop support + pub drag_and_drop: bool, +} + +impl Default for PlatformSpecific { + fn default() -> Self { + Self { + parent: None, + drag_and_drop: true, + } + } +} -- cgit From 83c7870c569a2976923ee6243a19813094d44673 Mon Sep 17 00:00:00 2001 From: Bingus Date: Mon, 24 Jul 2023 14:32:59 -0700 Subject: Moved `exit_on_close_request` to window settings. This now controls whether each INDIVIDUAL window should close on CloseRequested events. --- core/src/window/event.rs | 3 --- core/src/window/settings.rs | 11 +++++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) (limited to 'core/src/window') diff --git a/core/src/window/event.rs b/core/src/window/event.rs index 3efce05e..f7759435 100644 --- a/core/src/window/event.rs +++ b/core/src/window/event.rs @@ -28,9 +28,6 @@ pub enum Event { RedrawRequested(Instant), /// The user has requested for the window to close. - /// - /// Usually, you will want to terminate the execution whenever this event - /// occurs. CloseRequested, /// A window was destroyed by the runtime. diff --git a/core/src/window/settings.rs b/core/src/window/settings.rs index 20811e83..eba27914 100644 --- a/core/src/window/settings.rs +++ b/core/src/window/settings.rs @@ -57,6 +57,16 @@ pub struct Settings { /// Platform specific settings. pub platform_specific: PlatformSpecific, + + /// Whether the window will close when the user requests it, e.g. when a user presses the + /// close button. + /// + /// This can be useful if you want to have some behavior that executes before the window is + /// actually destroyed. If you disable this, you must manually close the window with the + /// `window::close` command. + /// + /// By default this is enabled. + pub exit_on_close_request: bool, } impl Default for Settings { @@ -73,6 +83,7 @@ impl Default for Settings { level: Level::default(), icon: None, platform_specific: Default::default(), + exit_on_close_request: true, } } } -- cgit From 67408311f45d341509538f8cc185978da66b6ace Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 30 Nov 2023 23:40:33 +0100 Subject: Use actual floats for logical coordinates --- core/src/window/event.rs | 18 +++++++++--------- core/src/window/position.rs | 6 ++++-- core/src/window/settings.rs | 11 ++++++----- 3 files changed, 19 insertions(+), 16 deletions(-) (limited to 'core/src/window') diff --git a/core/src/window/event.rs b/core/src/window/event.rs index f7759435..3ab7cd81 100644 --- a/core/src/window/event.rs +++ b/core/src/window/event.rs @@ -1,10 +1,10 @@ use crate::time::Instant; -use crate::Size; +use crate::{Point, Size}; use std::path::PathBuf; /// A window-related event. -#[derive(PartialEq, Eq, Clone, Debug)] +#[derive(PartialEq, Clone, Debug)] pub enum Event { /// A window was moved. Moved { @@ -30,22 +30,22 @@ pub enum Event { /// The user has requested for the window to close. CloseRequested, - /// A window was destroyed by the runtime. - Destroyed, - /// A window was created. - /// - /// **Note:** this event is not supported on Wayland. Created { /// The position of the created window. This is relative to the top-left corner of the desktop /// the window is on, including virtual desktops. Refers to window's "inner" position, /// or the client area, in logical pixels. - position: (i32, i32), + /// + /// **Note**: Not available in Wayland. + position: Option, /// The size of the created window. This is its "inner" size, or the size of the /// client area, in logical pixels. - size: Size, + size: Size, }, + /// A window was destroyed by the runtime. + Destroyed, + /// A window was focused. Focused, diff --git a/core/src/window/position.rs b/core/src/window/position.rs index c260c29e..73391e75 100644 --- a/core/src/window/position.rs +++ b/core/src/window/position.rs @@ -1,5 +1,7 @@ +use crate::Point; + /// The position of a window in a given screen. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, PartialEq)] pub enum Position { /// The platform-specific default position for a new window. Default, @@ -12,7 +14,7 @@ pub enum Position { /// 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), + Specific(Point), } impl Default for Position { diff --git a/core/src/window/settings.rs b/core/src/window/settings.rs index 25df8159..fbbf86ab 100644 --- a/core/src/window/settings.rs +++ b/core/src/window/settings.rs @@ -25,22 +25,23 @@ mod platform; mod platform; use crate::window::{Icon, Level, Position}; +use crate::Size; pub use platform::PlatformSpecific; /// The window settings of an application. #[derive(Debug, Clone)] pub struct Settings { - /// The initial size of the window. - pub size: (u32, u32), + /// The initial logical dimensions of the window. + pub size: Size, /// The initial position of the window. pub position: Position, /// The minimum size of the window. - pub min_size: Option<(u32, u32)>, + pub min_size: Option, /// The maximum size of the window. - pub max_size: Option<(u32, u32)>, + pub max_size: Option, /// Whether the window should be visible or not. pub visible: bool, @@ -77,7 +78,7 @@ pub struct Settings { impl Default for Settings { fn default() -> Self { Self { - size: (1024, 768), + size: Size::new(1024.0, 768.0), position: Position::default(), min_size: None, max_size: None, -- cgit From ea42af766f345715ff7a7168182d3896ee79cfbc Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 2 Dec 2023 20:41:58 +0100 Subject: Use `AtomicU64` for `window::Id` --- core/src/window/event.rs | 32 ++++++++++++++++---------------- core/src/window/id.rs | 14 +++++++------- 2 files changed, 23 insertions(+), 23 deletions(-) (limited to 'core/src/window') diff --git a/core/src/window/event.rs b/core/src/window/event.rs index 3ab7cd81..b9ee7aca 100644 --- a/core/src/window/event.rs +++ b/core/src/window/event.rs @@ -6,6 +6,22 @@ use std::path::PathBuf; /// A window-related event. #[derive(PartialEq, Clone, Debug)] pub enum Event { + /// A window was opened. + Opened { + /// The position of the opened window. This is relative to the top-left corner of the desktop + /// the window is on, including virtual desktops. Refers to window's "inner" position, + /// or the client area, in logical pixels. + /// + /// **Note**: Not available in Wayland. + position: Option, + /// The size of the created window. This is its "inner" size, or the size of the + /// client area, in logical pixels. + size: Size, + }, + + /// A window was closed. + Closed, + /// A window was moved. Moved { /// The new logical x location of the window @@ -30,22 +46,6 @@ pub enum Event { /// The user has requested for the window to close. CloseRequested, - /// A window was created. - Created { - /// The position of the created window. This is relative to the top-left corner of the desktop - /// the window is on, including virtual desktops. Refers to window's "inner" position, - /// or the client area, in logical pixels. - /// - /// **Note**: Not available in Wayland. - position: Option, - /// The size of the created window. This is its "inner" size, or the size of the - /// client area, in logical pixels. - size: Size, - }, - - /// A window was destroyed by the runtime. - Destroyed, - /// A window was focused. Focused, diff --git a/core/src/window/id.rs b/core/src/window/id.rs index 65002d43..20474c8f 100644 --- a/core/src/window/id.rs +++ b/core/src/window/id.rs @@ -1,5 +1,6 @@ -use std::collections::hash_map::DefaultHasher; -use std::hash::{Hash, Hasher}; +use std::hash::Hash; + +use std::sync::atomic::{self, AtomicU64}; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] /// The id of the window. @@ -7,15 +8,14 @@ use std::hash::{Hash, Hasher}; /// Internally Iced reserves `window::Id::MAIN` for the first window spawned. pub struct Id(u64); +static COUNT: AtomicU64 = AtomicU64::new(1); + impl Id { /// The reserved window [`Id`] for the first window in an Iced application. pub const MAIN: Self = Id(0); /// Creates a new unique window [`Id`]. - pub fn new(id: impl Hash) -> Id { - let mut hasher = DefaultHasher::new(); - id.hash(&mut hasher); - - Id(hasher.finish()) + pub fn unique() -> Id { + Id(COUNT.fetch_add(1, atomic::Ordering::Relaxed)) } } -- cgit From 082985ade8a108aa3ec1fe573411120b82da0cad Mon Sep 17 00:00:00 2001 From: Calastrophe Date: Tue, 9 Jan 2024 02:39:23 -0600 Subject: Small documentation typo fixed --- core/src/window/event.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core/src/window') diff --git a/core/src/window/event.rs b/core/src/window/event.rs index b9ee7aca..a14d127f 100644 --- a/core/src/window/event.rs +++ b/core/src/window/event.rs @@ -58,7 +58,7 @@ pub enum Event { /// for each file separately. FileHovered(PathBuf), - /// A file has beend dropped into the window. + /// A file has been dropped into the window. /// /// When the user drops multiple files at once, this event will be emitted /// for each file separately. -- cgit