Skip to content

Commit

Permalink
Use AtomicU64 for window::Id
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Dec 2, 2023
1 parent 6740831 commit ea42af7
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 212 deletions.
32 changes: 16 additions & 16 deletions core/src/window/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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
Expand All @@ -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<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 destroyed by the runtime.
Destroyed,

/// A window was focused.
Focused,

Expand Down
14 changes: 7 additions & 7 deletions core/src/window/id.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
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.
///
/// 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))
}
}
49 changes: 25 additions & 24 deletions examples/multi_window/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ enum Message {
ScaleChanged(window::Id, String),
TitleChanged(window::Id, String),
CloseWindow(window::Id),
WindowCreated(window::Id, Option<Point>),
WindowDestroyed(window::Id),
WindowOpened(window::Id, Option<Point>),
WindowClosed(window::Id),
NewWindow,
}

Expand Down Expand Up @@ -69,6 +69,8 @@ impl multi_window::Application for Example {
let window =
self.windows.get_mut(&id).expect("Window not found!");
window.scale_input = scale;

Command::none()
}
Message::ScaleChanged(id, scale) => {
let window =
Expand All @@ -78,48 +80,49 @@ impl multi_window::Application for Example {
.parse::<f64>()
.unwrap_or(window.current_scale)
.clamp(0.5, 5.0);

Command::none()
}
Message::TitleChanged(id, title) => {
let window =
self.windows.get_mut(&id).expect("Window not found.");

window.title = title;

Command::none()
}
Message::CloseWindow(id) => {
return window::close(id);
}
Message::WindowDestroyed(id) => {
Message::CloseWindow(id) => window::close(id),
Message::WindowClosed(id) => {
self.windows.remove(&id);
Command::none()
}
Message::WindowCreated(id, position) => {
Message::WindowOpened(id, position) => {
if let Some(position) = position {
self.next_window_pos = window::Position::Specific(
position + Vector::new(20.0, 20.0),
);
}

if let Some(window) = self.windows.get(&id) {
return text_input::focus(window.input_id.clone());
text_input::focus(window.input_id.clone())
} else {
Command::none()
}
}
Message::NewWindow => {
let count = self.windows.len() + 1;
let id = window::Id::new(count);

let (id, spawn_window) = window::spawn(window::Settings {
position: self.next_window_pos,
exit_on_close_request: count % 2 == 0,
..Default::default()
});

self.windows.insert(id, Window::new(count));

return window::spawn(
id,
window::Settings {
position: self.next_window_pos,
exit_on_close_request: count % 2 == 0,
..Default::default()
},
);
spawn_window
}
}

Command::none()
}

fn view(&self, window: window::Id) -> Element<Message> {
Expand Down Expand Up @@ -151,12 +154,10 @@ impl multi_window::Application for Example {
window::Event::CloseRequested => {
Some(Message::CloseWindow(id))
}
window::Event::Destroyed => {
Some(Message::WindowDestroyed(id))
}
window::Event::Created { position, .. } => {
Some(Message::WindowCreated(id, position))
window::Event::Opened { position, .. } => {
Some(Message::WindowOpened(id, position))
}
window::Event::Closed => Some(Message::WindowClosed(id)),
_ => None,
}
} else {
Expand Down
8 changes: 4 additions & 4 deletions runtime/src/command/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub enum Action<T> {
Clipboard(clipboard::Action<T>),

/// Run a window action.
Window(window::Id, window::Action<T>),
Window(window::Action<T>),

/// Run a system action.
System(system::Action<T>),
Expand Down Expand Up @@ -63,7 +63,7 @@ impl<T> Action<T> {
Self::Future(future) => Action::Future(Box::pin(future.map(f))),
Self::Stream(stream) => Action::Stream(Box::pin(stream.map(f))),
Self::Clipboard(action) => Action::Clipboard(action.map(f)),
Self::Window(id, window) => Action::Window(id, window.map(f)),
Self::Window(window) => Action::Window(window.map(f)),
Self::System(system) => Action::System(system.map(f)),
Self::Widget(operation) => {
Action::Widget(Box::new(widget::operation::map(operation, f)))
Expand All @@ -84,8 +84,8 @@ impl<T> fmt::Debug for Action<T> {
Self::Clipboard(action) => {
write!(f, "Action::Clipboard({action:?})")
}
Self::Window(id, action) => {
write!(f, "Action::Window({id:?}, {action:?})")
Self::Window(action) => {
write!(f, "Action::Window({action:?})")
}
Self::System(action) => write!(f, "Action::System({action:?})"),
Self::Widget(_action) => write!(f, "Action::Widget"),
Expand Down
4 changes: 2 additions & 2 deletions runtime/src/multi_window/program.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Build interactive programs using The Elm Architecture.
use crate::{window, Command};

use crate::core::text;
use crate::core::window;
use crate::core::{Element, Renderer};
use crate::Command;

/// The core of a user interface for a multi-window application following The Elm Architecture.
pub trait Program: Sized {
Expand Down
Loading

0 comments on commit ea42af7

Please sign in to comment.