summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón <hector@hecrj.dev>2023-12-05 01:03:09 +0100
committerLibravatar GitHub <noreply@github.com>2023-12-05 01:03:09 +0100
commitfc285d3e461626408c56bbc1605fcf0c974b2f69 (patch)
tree8aca292516d9aa43b78a14f51dd90caf60c691d7 /core
parent8727b3fc50ec251d9c117c51ca1289be5ba9b117 (diff)
parent5c5e7653bed248ba63faa6563e4d673e4441415e (diff)
downloadiced-fc285d3e461626408c56bbc1605fcf0c974b2f69.tar.gz
iced-fc285d3e461626408c56bbc1605fcf0c974b2f69.tar.bz2
iced-fc285d3e461626408c56bbc1605fcf0c974b2f69.zip
Merge pull request #1964 from bungoboingo/feat/multi-window-support
[Feature] 🪟 Multi Window 🪟 .. redux!
Diffstat (limited to '')
-rw-r--r--core/Cargo.toml3
-rw-r--r--core/src/event.rs2
-rw-r--r--core/src/point.rs58
-rw-r--r--core/src/widget/tree.rs2
-rw-r--r--core/src/window.rs6
-rw-r--r--core/src/window/event.rs22
-rw-r--r--core/src/window/id.rs21
-rw-r--r--core/src/window/position.rs (renamed from winit/src/position.rs)6
-rw-r--r--core/src/window/settings.rs (renamed from src/window/settings.rs)73
-rw-r--r--core/src/window/settings/linux.rs (renamed from winit/src/settings/linux.rs)0
-rw-r--r--core/src/window/settings/macos.rs (renamed from winit/src/settings/macos.rs)0
-rw-r--r--core/src/window/settings/other.rs (renamed from winit/src/settings/other.rs)0
-rw-r--r--core/src/window/settings/wasm.rs (renamed from winit/src/settings/wasm.rs)0
-rw-r--r--core/src/window/settings/windows.rs (renamed from winit/src/settings/windows.rs)0
14 files changed, 142 insertions, 51 deletions
diff --git a/core/Cargo.toml b/core/Cargo.toml
index 82946847..4672c754 100644
--- a/core/Cargo.toml
+++ b/core/Cargo.toml
@@ -23,5 +23,8 @@ palette.optional = true
[target.'cfg(target_arch = "wasm32")'.dependencies]
instant.workspace = true
+[target.'cfg(windows)'.dependencies]
+raw-window-handle.workspace = true
+
[dev-dependencies]
approx = "0.5"
diff --git a/core/src/event.rs b/core/src/event.rs
index 953cd73f..870b3074 100644
--- a/core/src/event.rs
+++ b/core/src/event.rs
@@ -19,7 +19,7 @@ pub enum Event {
Mouse(mouse::Event),
/// A window event
- Window(window::Event),
+ Window(window::Id, window::Event),
/// A touch event
Touch(touch::Event),
diff --git a/core/src/point.rs b/core/src/point.rs
index 9bf7726b..ef42852f 100644
--- a/core/src/point.rs
+++ b/core/src/point.rs
@@ -1,26 +1,34 @@
use crate::Vector;
+use num_traits::{Float, Num};
+use std::fmt;
+
/// A 2D point.
-#[derive(Debug, Clone, Copy, PartialEq, Default)]
-pub struct Point {
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
+pub struct Point<T = f32> {
/// The X coordinate.
- pub x: f32,
+ pub x: T,
/// The Y coordinate.
- pub y: f32,
+ pub y: T,
}
impl Point {
/// The origin (i.e. a [`Point`] at (0, 0)).
- pub const ORIGIN: Point = Point::new(0.0, 0.0);
+ pub const ORIGIN: Self = Self::new(0.0, 0.0);
+}
+impl<T: Num> Point<T> {
/// Creates a new [`Point`] with the given coordinates.
- pub const fn new(x: f32, y: f32) -> Self {
+ pub const fn new(x: T, y: T) -> Self {
Self { x, y }
}
/// Computes the distance to another [`Point`].
- pub fn distance(&self, to: Point) -> f32 {
+ pub fn distance(&self, to: Self) -> T
+ where
+ T: Float,
+ {
let a = self.x - to.x;
let b = self.y - to.y;
@@ -34,9 +42,9 @@ impl From<[f32; 2]> for Point {
}
}
-impl From<[u16; 2]> for Point {
+impl From<[u16; 2]> for Point<u16> {
fn from([x, y]: [u16; 2]) -> Self {
- Point::new(x.into(), y.into())
+ Point::new(x, y)
}
}
@@ -46,10 +54,13 @@ impl From<Point> for [f32; 2] {
}
}
-impl std::ops::Add<Vector> for Point {
+impl<T> std::ops::Add<Vector<T>> for Point<T>
+where
+ T: std::ops::Add<Output = T>,
+{
type Output = Self;
- fn add(self, vector: Vector) -> Self {
+ fn add(self, vector: Vector<T>) -> Self {
Self {
x: self.x + vector.x,
y: self.y + vector.y,
@@ -57,10 +68,13 @@ impl std::ops::Add<Vector> for Point {
}
}
-impl std::ops::Sub<Vector> for Point {
+impl<T> std::ops::Sub<Vector<T>> for Point<T>
+where
+ T: std::ops::Sub<Output = T>,
+{
type Output = Self;
- fn sub(self, vector: Vector) -> Self {
+ fn sub(self, vector: Vector<T>) -> Self {
Self {
x: self.x - vector.x,
y: self.y - vector.y,
@@ -68,10 +82,22 @@ impl std::ops::Sub<Vector> for Point {
}
}
-impl std::ops::Sub<Point> for Point {
- type Output = Vector;
+impl<T> std::ops::Sub<Point<T>> for Point<T>
+where
+ T: std::ops::Sub<Output = T>,
+{
+ type Output = Vector<T>;
- fn sub(self, point: Point) -> Vector {
+ fn sub(self, point: Self) -> Vector<T> {
Vector::new(self.x - point.x, self.y - point.y)
}
}
+
+impl<T> fmt::Display for Point<T>
+where
+ T: fmt::Display,
+{
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(f, "Point {{ x: {}, y: {} }}", self.x, self.y)
+ }
+}
diff --git a/core/src/widget/tree.rs b/core/src/widget/tree.rs
index d4b8828a..ff52b1ce 100644
--- a/core/src/widget/tree.rs
+++ b/core/src/widget/tree.rs
@@ -67,7 +67,7 @@ impl Tree {
}
}
- /// Reconciliates the children of the tree with the provided list of widgets.
+ /// Reconciles the children of the tree with the provided list of widgets.
pub fn diff_children<'a, Message, Renderer>(
&mut self,
new_children: &[impl Borrow<dyn Widget<Message, Renderer> + 'a>],
diff --git a/core/src/window.rs b/core/src/window.rs
index a6dbdfb4..448ffc45 100644
--- a/core/src/window.rs
+++ b/core/src/window.rs
@@ -1,15 +1,21 @@
//! Build window-based GUI applications.
pub mod icon;
+pub mod settings;
mod event;
+mod id;
mod level;
mod mode;
+mod position;
mod redraw_request;
mod user_attention;
pub use event::Event;
pub use icon::Icon;
+pub use id::Id;
pub use level::Level;
pub use mode::Mode;
+pub use position::Position;
pub use redraw_request::RedrawRequest;
+pub use settings::Settings;
pub use user_attention::UserAttention;
diff --git a/core/src/window/event.rs b/core/src/window/event.rs
index e2fb5e66..b9ee7aca 100644
--- a/core/src/window/event.rs
+++ b/core/src/window/event.rs
@@ -1,10 +1,27 @@
use crate::time::Instant;
+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 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<Point>,
+ /// 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
@@ -27,9 +44,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 focused.
diff --git a/core/src/window/id.rs b/core/src/window/id.rs
new file mode 100644
index 00000000..20474c8f
--- /dev/null
+++ b/core/src/window/id.rs
@@ -0,0 +1,21 @@
+use std::hash::Hash;
+
+use std::sync::atomic::{self, AtomicU64};
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
+/// The id of the window.
+///
+/// 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 unique() -> Id {
+ Id(COUNT.fetch_add(1, atomic::Ordering::Relaxed))
+ }
+}
diff --git a/winit/src/position.rs b/core/src/window/position.rs
index c260c29e..73391e75 100644
--- a/winit/src/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/src/window/settings.rs b/core/src/window/settings.rs
index 0ee573e5..fbbf86ab 100644
--- a/src/window/settings.rs
+++ b/core/src/window/settings.rs
@@ -1,21 +1,47 @@
-use crate::window::{Icon, Level, Position};
+//! Configure your windows.
+#[cfg(target_os = "windows")]
+#[path = "settings/windows.rs"]
+mod platform;
+
+#[cfg(target_os = "macos")]
+#[path = "settings/macos.rs"]
+mod platform;
+
+#[cfg(target_os = "linux")]
+#[path = "settings/linux.rs"]
+mod platform;
+
+#[cfg(target_arch = "wasm32")]
+#[path = "settings/wasm.rs"]
+mod platform;
-pub use iced_winit::settings::PlatformSpecific;
+#[cfg(not(any(
+ target_os = "windows",
+ target_os = "macos",
+ target_os = "linux",
+ target_arch = "wasm32"
+)))]
+#[path = "settings/other.rs"]
+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<Size>,
/// The maximum size of the window.
- pub max_size: Option<(u32, u32)>,
+ pub max_size: Option<Size>,
/// Whether the window should be visible or not.
pub visible: bool,
@@ -37,12 +63,22 @@ 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 {
- fn default() -> Settings {
- Settings {
- size: (1024, 768),
+ fn default() -> Self {
+ Self {
+ size: Size::new(1024.0, 768.0),
position: Position::default(),
min_size: None,
max_size: None,
@@ -52,25 +88,8 @@ impl Default for Settings {
transparent: false,
level: Level::default(),
icon: None,
+ exit_on_close_request: true,
platform_specific: PlatformSpecific::default(),
}
}
}
-
-impl From<Settings> 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/winit/src/settings/linux.rs b/core/src/window/settings/linux.rs
index 009b9d9e..009b9d9e 100644
--- a/winit/src/settings/linux.rs
+++ b/core/src/window/settings/linux.rs
diff --git a/winit/src/settings/macos.rs b/core/src/window/settings/macos.rs
index f86e63ad..f86e63ad 100644
--- a/winit/src/settings/macos.rs
+++ b/core/src/window/settings/macos.rs
diff --git a/winit/src/settings/other.rs b/core/src/window/settings/other.rs
index b1103f62..b1103f62 100644
--- a/winit/src/settings/other.rs
+++ b/core/src/window/settings/other.rs
diff --git a/winit/src/settings/wasm.rs b/core/src/window/settings/wasm.rs
index 8e0f1bbc..8e0f1bbc 100644
--- a/winit/src/settings/wasm.rs
+++ b/core/src/window/settings/wasm.rs
diff --git a/winit/src/settings/windows.rs b/core/src/window/settings/windows.rs
index 45d753bd..45d753bd 100644
--- a/winit/src/settings/windows.rs
+++ b/core/src/window/settings/windows.rs