Skip to content

Commit

Permalink
Return window::Id in window::open
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Jul 30, 2024
1 parent 8f33575 commit fd593f8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
11 changes: 7 additions & 4 deletions examples/multi_window/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ enum Message {

impl Example {
fn new() -> (Self, Task<Message>) {
let (_id, open) = window::open(window::Settings::default());

(
Self {
windows: BTreeMap::new(),
},
window::open(window::Settings::default())
.map(Message::WindowOpened),
open.map(Message::WindowOpened),
)
}

Expand Down Expand Up @@ -74,10 +75,12 @@ impl Example {
},
);

window::open(window::Settings {
let (_id, open) = window::open(window::Settings {
position,
..window::Settings::default()
})
});

open
})
.map(Message::WindowOpened)
}
Expand Down
11 changes: 7 additions & 4 deletions runtime/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,15 @@ pub fn close_requests() -> Subscription<Id> {

/// Opens a new window with the given [`Settings`]; producing the [`Id`]
/// of the new window on completion.
pub fn open(settings: Settings) -> Task<Id> {
pub fn open(settings: Settings) -> (Id, Task<Id>) {
let id = Id::unique();

task::oneshot(|channel| {
crate::Action::Window(Action::Open(id, settings, channel))
})
(
id,
task::oneshot(|channel| {
crate::Action::Window(Action::Open(id, settings, channel))
}),
)
}

/// Closes the window with `id`.
Expand Down
5 changes: 3 additions & 2 deletions winit/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,9 @@ where
let task = if let Some(window_settings) = window_settings {
let mut task = Some(task);

runtime::window::open(window_settings)
.then(move |_| task.take().unwrap_or(Task::none()))
let (_id, open) = runtime::window::open(window_settings);

open.then(move |_| task.take().unwrap_or(Task::none()))
} else {
task
};
Expand Down

0 comments on commit fd593f8

Please sign in to comment.