Skip to content

Commit ea42af7

Browse files
committed
Use AtomicU64 for window::Id
1 parent 6740831 commit ea42af7

File tree

9 files changed

+228
-212
lines changed

9 files changed

+228
-212
lines changed

Diff for: core/src/window/event.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@ use std::path::PathBuf;
66
/// A window-related event.
77
#[derive(PartialEq, Clone, Debug)]
88
pub enum Event {
9+
/// A window was opened.
10+
Opened {
11+
/// The position of the opened window. This is relative to the top-left corner of the desktop
12+
/// the window is on, including virtual desktops. Refers to window's "inner" position,
13+
/// or the client area, in logical pixels.
14+
///
15+
/// **Note**: Not available in Wayland.
16+
position: Option<Point>,
17+
/// The size of the created window. This is its "inner" size, or the size of the
18+
/// client area, in logical pixels.
19+
size: Size,
20+
},
21+
22+
/// A window was closed.
23+
Closed,
24+
925
/// A window was moved.
1026
Moved {
1127
/// The new logical x location of the window
@@ -30,22 +46,6 @@ pub enum Event {
3046
/// The user has requested for the window to close.
3147
CloseRequested,
3248

33-
/// A window was created.
34-
Created {
35-
/// The position of the created window. This is relative to the top-left corner of the desktop
36-
/// the window is on, including virtual desktops. Refers to window's "inner" position,
37-
/// or the client area, in logical pixels.
38-
///
39-
/// **Note**: Not available in Wayland.
40-
position: Option<Point>,
41-
/// The size of the created window. This is its "inner" size, or the size of the
42-
/// client area, in logical pixels.
43-
size: Size,
44-
},
45-
46-
/// A window was destroyed by the runtime.
47-
Destroyed,
48-
4949
/// A window was focused.
5050
Focused,
5151

Diff for: core/src/window/id.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
use std::collections::hash_map::DefaultHasher;
2-
use std::hash::{Hash, Hasher};
1+
use std::hash::Hash;
2+
3+
use std::sync::atomic::{self, AtomicU64};
34

45
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
56
/// The id of the window.
67
///
78
/// Internally Iced reserves `window::Id::MAIN` for the first window spawned.
89
pub struct Id(u64);
910

11+
static COUNT: AtomicU64 = AtomicU64::new(1);
12+
1013
impl Id {
1114
/// The reserved window [`Id`] for the first window in an Iced application.
1215
pub const MAIN: Self = Id(0);
1316

1417
/// Creates a new unique window [`Id`].
15-
pub fn new(id: impl Hash) -> Id {
16-
let mut hasher = DefaultHasher::new();
17-
id.hash(&mut hasher);
18-
19-
Id(hasher.finish())
18+
pub fn unique() -> Id {
19+
Id(COUNT.fetch_add(1, atomic::Ordering::Relaxed))
2020
}
2121
}

Diff for: examples/multi_window/src/main.rs

+25-24
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ enum Message {
3535
ScaleChanged(window::Id, String),
3636
TitleChanged(window::Id, String),
3737
CloseWindow(window::Id),
38-
WindowCreated(window::Id, Option<Point>),
39-
WindowDestroyed(window::Id),
38+
WindowOpened(window::Id, Option<Point>),
39+
WindowClosed(window::Id),
4040
NewWindow,
4141
}
4242

@@ -69,6 +69,8 @@ impl multi_window::Application for Example {
6969
let window =
7070
self.windows.get_mut(&id).expect("Window not found!");
7171
window.scale_input = scale;
72+
73+
Command::none()
7274
}
7375
Message::ScaleChanged(id, scale) => {
7476
let window =
@@ -78,48 +80,49 @@ impl multi_window::Application for Example {
7880
.parse::<f64>()
7981
.unwrap_or(window.current_scale)
8082
.clamp(0.5, 5.0);
83+
84+
Command::none()
8185
}
8286
Message::TitleChanged(id, title) => {
8387
let window =
8488
self.windows.get_mut(&id).expect("Window not found.");
8589

8690
window.title = title;
91+
92+
Command::none()
8793
}
88-
Message::CloseWindow(id) => {
89-
return window::close(id);
90-
}
91-
Message::WindowDestroyed(id) => {
94+
Message::CloseWindow(id) => window::close(id),
95+
Message::WindowClosed(id) => {
9296
self.windows.remove(&id);
97+
Command::none()
9398
}
94-
Message::WindowCreated(id, position) => {
99+
Message::WindowOpened(id, position) => {
95100
if let Some(position) = position {
96101
self.next_window_pos = window::Position::Specific(
97102
position + Vector::new(20.0, 20.0),
98103
);
99104
}
100105

101106
if let Some(window) = self.windows.get(&id) {
102-
return text_input::focus(window.input_id.clone());
107+
text_input::focus(window.input_id.clone())
108+
} else {
109+
Command::none()
103110
}
104111
}
105112
Message::NewWindow => {
106113
let count = self.windows.len() + 1;
107-
let id = window::Id::new(count);
114+
115+
let (id, spawn_window) = window::spawn(window::Settings {
116+
position: self.next_window_pos,
117+
exit_on_close_request: count % 2 == 0,
118+
..Default::default()
119+
});
108120

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

111-
return window::spawn(
112-
id,
113-
window::Settings {
114-
position: self.next_window_pos,
115-
exit_on_close_request: count % 2 == 0,
116-
..Default::default()
117-
},
118-
);
123+
spawn_window
119124
}
120125
}
121-
122-
Command::none()
123126
}
124127

125128
fn view(&self, window: window::Id) -> Element<Message> {
@@ -151,12 +154,10 @@ impl multi_window::Application for Example {
151154
window::Event::CloseRequested => {
152155
Some(Message::CloseWindow(id))
153156
}
154-
window::Event::Destroyed => {
155-
Some(Message::WindowDestroyed(id))
156-
}
157-
window::Event::Created { position, .. } => {
158-
Some(Message::WindowCreated(id, position))
157+
window::Event::Opened { position, .. } => {
158+
Some(Message::WindowOpened(id, position))
159159
}
160+
window::Event::Closed => Some(Message::WindowClosed(id)),
160161
_ => None,
161162
}
162163
} else {

Diff for: runtime/src/command/action.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub enum Action<T> {
2727
Clipboard(clipboard::Action<T>),
2828

2929
/// Run a window action.
30-
Window(window::Id, window::Action<T>),
30+
Window(window::Action<T>),
3131

3232
/// Run a system action.
3333
System(system::Action<T>),
@@ -63,7 +63,7 @@ impl<T> Action<T> {
6363
Self::Future(future) => Action::Future(Box::pin(future.map(f))),
6464
Self::Stream(stream) => Action::Stream(Box::pin(stream.map(f))),
6565
Self::Clipboard(action) => Action::Clipboard(action.map(f)),
66-
Self::Window(id, window) => Action::Window(id, window.map(f)),
66+
Self::Window(window) => Action::Window(window.map(f)),
6767
Self::System(system) => Action::System(system.map(f)),
6868
Self::Widget(operation) => {
6969
Action::Widget(Box::new(widget::operation::map(operation, f)))
@@ -84,8 +84,8 @@ impl<T> fmt::Debug for Action<T> {
8484
Self::Clipboard(action) => {
8585
write!(f, "Action::Clipboard({action:?})")
8686
}
87-
Self::Window(id, action) => {
88-
write!(f, "Action::Window({id:?}, {action:?})")
87+
Self::Window(action) => {
88+
write!(f, "Action::Window({action:?})")
8989
}
9090
Self::System(action) => write!(f, "Action::System({action:?})"),
9191
Self::Widget(_action) => write!(f, "Action::Widget"),

Diff for: runtime/src/multi_window/program.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Build interactive programs using The Elm Architecture.
2-
use crate::{window, Command};
3-
42
use crate::core::text;
3+
use crate::core::window;
54
use crate::core::{Element, Renderer};
5+
use crate::Command;
66

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

0 commit comments

Comments
 (0)