Fixed issue with window ID on winit

This commit is contained in:
bungoboingo 2023-01-02 18:14:31 -08:00
parent 942f1c91af
commit f43419d475
3 changed files with 63 additions and 52 deletions

View file

@ -8,5 +8,6 @@ publish = false
[dependencies] [dependencies]
iced = { path = "../..", features = ["debug", "multi_window"] } iced = { path = "../..", features = ["debug", "multi_window"] }
env_logger = "0.10.0"
iced_native = { path = "../../native" } iced_native = { path = "../../native" }
iced_lazy = { path = "../../lazy" } iced_lazy = { path = "../../lazy" }

View file

@ -15,6 +15,8 @@ use iced_native::{event, subscription, Event};
use std::collections::HashMap; use std::collections::HashMap;
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
env_logger::init();
Example::run(Settings::default()) Example::run(Settings::default())
} }

View file

@ -33,7 +33,6 @@ use std::mem::ManuallyDrop;
pub enum Event<Message> { pub enum Event<Message> {
/// An [`Application`] generated message /// An [`Application`] generated message
Application(Message), Application(Message),
/// TODO(derezzedex) /// TODO(derezzedex)
// Create a wrapper variant of `window::Event` type instead // Create a wrapper variant of `window::Event` type instead
// (maybe we should also allow users to listen/react to those internal messages?) // (maybe we should also allow users to listen/react to those internal messages?)
@ -197,7 +196,8 @@ where
.map_err(Error::WindowCreationFailed)?; .map_err(Error::WindowCreationFailed)?;
let windows: HashMap<window::Id, winit::window::Window> = let windows: HashMap<window::Id, winit::window::Window> =
HashMap::from([(window::Id::new(0usize), window)]); HashMap::from([(window::Id::MAIN, window)]);
let window = windows.values().next().expect("No window found"); let window = windows.values().next().expect("No window found");
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
@ -515,7 +515,8 @@ async fn run_instance<A, E, C>(
), ),
)); ));
} }
event::Event::UserEvent(event) => match event { event::Event::UserEvent(event) => {
match event {
Event::Application(message) => { Event::Application(message) => {
messages.push(message); messages.push(message);
} }
@ -548,31 +549,38 @@ async fn run_instance<A, E, C>(
let _ = windows.insert(id, window); let _ = windows.insert(id, window);
} }
Event::CloseWindow(id) => { Event::CloseWindow(id) => {
// TODO(derezzedex): log errors println!("Closing window {:?}. Total: {}", id, windows.len());
if let Some(window) = windows.get(&id) { if let Some(window) = windows.get(&id) {
if window_ids.remove(&window.id()).is_none() { if window_ids.remove(&window.id()).is_none() {
println!("Failed to remove from `window_ids`!"); log::error!("Failed to remove window with id {:?} from window_ids.", window.id());
} }
} else {
log::error!("Could not find window with id {:?} in windows.", id);
} }
if states.remove(&id).is_none() { if states.remove(&id).is_none() {
println!("Failed to remove from `states`!") log::error!("Failed to remove window {:?} from states.", id);
} }
if interfaces.remove(&id).is_none() { if interfaces.remove(&id).is_none() {
println!("Failed to remove from `interfaces`!"); log::error!("Failed to remove window {:?} from interfaces.", id);
} }
if windows.remove(&id).is_none() { if windows.remove(&id).is_none() {
println!("Failed to remove from `windows`!") log::error!("Failed to remove window {:?} from windows.", id);
} }
if surfaces.remove(&id).is_none() { if surfaces.remove(&id).is_none() {
println!("Failed to remove from `surfaces`!") log::error!("Failed to remove window {:?} from surfaces.", id);
} }
if windows.is_empty() { if windows.is_empty() {
log::info!("All windows are closed. Terminating program.");
break 'main; break 'main;
} else {
log::info!("Remaining windows: {:?}", windows.len());
} }
} }
Event::NewWindow(_, _) => unreachable!(), Event::NewWindow(_, _) => unreachable!(),
}, }
}
event::Event::RedrawRequested(id) => { event::Event::RedrawRequested(id) => {
let state = window_ids let state = window_ids
.get(&id) .get(&id)