summaryrefslogtreecommitdiffstats
path: root/core/src
diff options
context:
space:
mode:
Diffstat (limited to 'core/src')
-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.rs24
-rw-r--r--core/src/window/settings.rs95
-rw-r--r--core/src/window/settings/linux.rs11
-rw-r--r--core/src/window/settings/macos.rs12
-rw-r--r--core/src/window/settings/other.rs3
-rw-r--r--core/src/window/settings/wasm.rs11
-rw-r--r--core/src/window/settings/windows.rs21
13 files changed, 266 insertions, 22 deletions
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/core/src/window/position.rs b/core/src/window/position.rs
new file mode 100644
index 00000000..73391e75
--- /dev/null
+++ b/core/src/window/position.rs
@@ -0,0 +1,24 @@
+use crate::Point;
+
+/// The position of a window in a given screen.
+#[derive(Debug, Clone, Copy, PartialEq)]
+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(Point),
+}
+
+impl Default for Position {
+ fn default() -> Self {
+ Self::Default
+ }
+}
diff --git a/core/src/window/settings.rs b/core/src/window/settings.rs
new file mode 100644
index 00000000..fbbf86ab
--- /dev/null
+++ b/core/src/window/settings.rs
@@ -0,0 +1,95 @@
+//! 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;
+
+#[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 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<Size>,
+
+ /// The maximum size of the window.
+ pub max_size: Option<Size>,
+
+ /// Whether the window should be visible or not.
+ pub visible: bool,
+
+ /// Whether the window should be resizable or not.
+ pub resizable: bool,
+
+ /// Whether the window should have a border, a title bar, etc. or not.
+ pub decorations: bool,
+
+ /// Whether the window should be transparent.
+ pub transparent: bool,
+
+ /// The window [`Level`].
+ pub level: Level,
+
+ /// The icon of the window.
+ pub icon: Option<Icon>,
+
+ /// 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() -> Self {
+ Self {
+ size: Size::new(1024.0, 768.0),
+ position: Position::default(),
+ min_size: None,
+ max_size: None,
+ visible: true,
+ resizable: true,
+ decorations: true,
+ transparent: false,
+ level: Level::default(),
+ icon: None,
+ exit_on_close_request: true,
+ platform_specific: PlatformSpecific::default(),
+ }
+ }
+}
diff --git a/core/src/window/settings/linux.rs b/core/src/window/settings/linux.rs
new file mode 100644
index 00000000..009b9d9e
--- /dev/null
+++ b/core/src/window/settings/linux.rs
@@ -0,0 +1,11 @@
+//! Platform specific settings for Linux.
+
+/// The platform specific window settings of an application.
+#[derive(Debug, Clone, PartialEq, Eq, Default)]
+pub struct PlatformSpecific {
+ /// Sets the application id of the window.
+ ///
+ /// As a best practice, it is suggested to select an application id that match
+ /// the basename of the application’s .desktop file.
+ pub application_id: String,
+}
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<String>,
+}
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<RawWindowHandle>,
+
+ /// Drag and drop support
+ pub drag_and_drop: bool,
+}
+
+impl Default for PlatformSpecific {
+ fn default() -> Self {
+ Self {
+ parent: None,
+ drag_and_drop: true,
+ }
+ }
+}