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,64 +515,72 @@ async fn run_instance<A, E, C>(
), ),
)); ));
} }
event::Event::UserEvent(event) => match event { event::Event::UserEvent(event) => {
Event::Application(message) => { match event {
messages.push(message); Event::Application(message) => {
} messages.push(message);
Event::WindowCreated(id, window) => { }
let mut surface = compositor.create_surface(&window); Event::WindowCreated(id, window) => {
let mut surface = compositor.create_surface(&window);
let state = State::new(&application, &window); let state = State::new(&application, &window);
let physical_size = state.physical_size(); let physical_size = state.physical_size();
compositor.configure_surface( compositor.configure_surface(
&mut surface, &mut surface,
physical_size.width, physical_size.width,
physical_size.height, physical_size.height,
); );
let user_interface = build_user_interface( let user_interface = build_user_interface(
&application, &application,
user_interface::Cache::default(), user_interface::Cache::default(),
&mut renderer, &mut renderer,
state.logical_size(), state.logical_size(),
&mut debug, &mut debug,
id, id,
); );
let _ = states.insert(id, state); let _ = states.insert(id, state);
let _ = surfaces.insert(id, surface); let _ = surfaces.insert(id, surface);
let _ = interfaces.insert(id, user_interface); let _ = interfaces.insert(id, user_interface);
let _ = window_ids.insert(window.id(), id); let _ = window_ids.insert(window.id(), id);
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 window_ids.remove(&window.id()).is_none() { if let Some(window) = windows.get(&id) {
println!("Failed to remove from `window_ids`!"); if window_ids.remove(&window.id()).is_none() {
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() {
log::error!("Failed to remove window {:?} from states.", id);
}
if interfaces.remove(&id).is_none() {
log::error!("Failed to remove window {:?} from interfaces.", id);
}
if windows.remove(&id).is_none() {
log::error!("Failed to remove window {:?} from windows.", id);
}
if surfaces.remove(&id).is_none() {
log::error!("Failed to remove window {:?} from surfaces.", id);
}
if windows.is_empty() {
log::info!("All windows are closed. Terminating program.");
break 'main;
} else {
log::info!("Remaining windows: {:?}", windows.len());
} }
} }
if states.remove(&id).is_none() { Event::NewWindow(_, _) => unreachable!(),
println!("Failed to remove from `states`!")
}
if interfaces.remove(&id).is_none() {
println!("Failed to remove from `interfaces`!");
}
if windows.remove(&id).is_none() {
println!("Failed to remove from `windows`!")
}
if surfaces.remove(&id).is_none() {
println!("Failed to remove from `surfaces`!")
}
if windows.is_empty() {
break 'main;
}
} }
Event::NewWindow(_, _) => unreachable!(), }
},
event::Event::RedrawRequested(id) => { event::Event::RedrawRequested(id) => {
let state = window_ids let state = window_ids
.get(&id) .get(&id)