fix selecting wrong window for key events

This commit is contained in:
Chance 2025-03-25 15:13:57 -04:00 committed by BitSyndicate
parent ce4702ec5f
commit ceefdaf46b
5 changed files with 119 additions and 15 deletions

68
Cargo.lock generated
View file

@ -1281,6 +1281,16 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "nu-ansi-term"
version = "0.46.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84"
dependencies = [
"overload",
"winapi",
]
[[package]] [[package]]
name = "num-traits" name = "num-traits"
version = "0.2.19" version = "0.2.19"
@ -1556,6 +1566,12 @@ dependencies = [
"num-traits", "num-traits",
] ]
[[package]]
name = "overload"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39"
[[package]] [[package]]
name = "owned_ttf_parser" name = "owned_ttf_parser"
version = "0.25.0" version = "0.25.0"
@ -1919,6 +1935,15 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "sharded-slab"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6"
dependencies = [
"lazy_static",
]
[[package]] [[package]]
name = "shlex" name = "shlex"
version = "1.3.0" version = "1.3.0"
@ -2118,6 +2143,16 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "thread_local"
version = "1.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c"
dependencies = [
"cfg-if",
"once_cell",
]
[[package]] [[package]]
name = "tiny-skia" name = "tiny-skia"
version = "0.11.4" version = "0.11.4"
@ -2222,6 +2257,32 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c" checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c"
dependencies = [ dependencies = [
"once_cell", "once_cell",
"valuable",
]
[[package]]
name = "tracing-log"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3"
dependencies = [
"log",
"once_cell",
"tracing-core",
]
[[package]]
name = "tracing-subscriber"
version = "0.3.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8189decb5ac0fa7bc8b96b7cb9b2701d60d48805aca84a238004d665fcc4008"
dependencies = [
"nu-ansi-term",
"sharded-slab",
"smallvec",
"thread_local",
"tracing-core",
"tracing-log",
] ]
[[package]] [[package]]
@ -2295,6 +2356,12 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "valuable"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65"
[[package]] [[package]]
name = "version_check" name = "version_check"
version = "0.9.5" version = "0.9.5"
@ -3131,6 +3198,7 @@ dependencies = [
"thiserror 2.0.12", "thiserror 2.0.12",
"tokio", "tokio",
"tracing", "tracing",
"tracing-subscriber",
"wgpu", "wgpu",
"winit", "winit",
] ]

View file

@ -24,6 +24,7 @@ bytemuck = "1.21.0"
futures = "0.3.31" futures = "0.3.31"
cgmath = "0.18.0" cgmath = "0.18.0"
tracing = "0.1.41" tracing = "0.1.41"
tracing-subscriber = "0.3.19"
[profile.dev] [profile.dev]

View file

@ -5,7 +5,7 @@ use std::time::Instant;
use cgmath::{Matrix4, Point3, Rad, Vector3, perspective}; use cgmath::{Matrix4, Point3, Rad, Vector3, perspective};
use futures::executor::block_on; use futures::executor::block_on;
use thiserror::Error; use thiserror::Error;
use wgpu::util::DeviceExt; use wgpu::{Backends, InstanceDescriptor, util::DeviceExt};
use winit::window::Window; use winit::window::Window;
#[derive(Debug, Error)] #[derive(Debug, Error)]
@ -240,7 +240,10 @@ pub struct WgpuCtx<'window> {
impl<'window> WgpuCtx<'window> { impl<'window> WgpuCtx<'window> {
pub async fn new(window: Arc<Window>) -> Result<WgpuCtx<'window>, ContextError> { pub async fn new(window: Arc<Window>) -> Result<WgpuCtx<'window>, ContextError> {
let instance = wgpu::Instance::default(); let instance = wgpu::Instance::new(&InstanceDescriptor {
backends: Backends::from_comma_list("dx12,metal,opengl,webgpu"),
..Default::default()
});
let surface = instance.create_surface(Arc::clone(&window))?; let surface = instance.create_surface(Arc::clone(&window))?;
let adapter = instance let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions { .request_adapter(&wgpu::RequestAdapterOptions {
@ -255,8 +258,7 @@ impl<'window> WgpuCtx<'window> {
&wgpu::DeviceDescriptor { &wgpu::DeviceDescriptor {
label: None, label: None,
required_features: wgpu::Features::empty(), required_features: wgpu::Features::empty(),
required_limits: wgpu::Limits::downlevel_webgl2_defaults() required_limits: wgpu::Limits::default().using_resolution(adapter.limits()),
.using_resolution(adapter.limits()),
memory_hints: wgpu::MemoryHints::Performance, memory_hints: wgpu::MemoryHints::Performance,
}, },
None, None,

View file

@ -31,7 +31,13 @@ impl ApplicationHandler for App<'_> {
); );
let window_id = window.id(); let window_id = window.id();
let wgpu_ctx = WgpuCtx::new_blocking(window.clone()).unwrap(); let wgpu_ctx = WgpuCtx::new_blocking(window.clone()).unwrap();
self.windows.insert(window_id, WindowContext { window, ctx: wgpu_ctx }); self.windows.insert(
window_id,
WindowContext {
window,
ctx: wgpu_ctx,
},
);
} }
} }
@ -45,7 +51,7 @@ impl ApplicationHandler for App<'_> {
WindowEvent::CloseRequested => { WindowEvent::CloseRequested => {
if let Some(window_context) = self.windows.remove(&window_id) { if let Some(window_context) = self.windows.remove(&window_id) {
drop(window_context); drop(window_context);
println!("Window: {:?} closed, exiting", window_id); debug!("Window: {:?} closed, exiting", window_id);
} }
if self.windows.is_empty() { if self.windows.is_empty() {
event_loop.exit(); event_loop.exit();
@ -63,10 +69,14 @@ impl ApplicationHandler for App<'_> {
match code { match code {
winit::keyboard::KeyCode::Space => { winit::keyboard::KeyCode::Space => {
debug!("Space key pressed"); debug!("Space key pressed");
if let Some(window_context) = self.windows.values_mut().next() { if let Some(window_context) = self.windows.get_mut(&window_id){
match window_context.ctx.bg_color() { match window_context.ctx.bg_color() {
wgpu::Color::WHITE => window_context.ctx.change_bg_color(wgpu::Color::BLACK), wgpu::Color::WHITE => {
wgpu::Color::BLACK => window_context.ctx.change_bg_color(wgpu::Color::WHITE), window_context.ctx.change_bg_color(wgpu::Color::BLACK)
}
wgpu::Color::BLACK => {
window_context.ctx.change_bg_color(wgpu::Color::WHITE)
}
_ => window_context.ctx.change_bg_color(wgpu::Color::WHITE), _ => window_context.ctx.change_bg_color(wgpu::Color::WHITE),
} }
} }
@ -82,7 +92,13 @@ impl ApplicationHandler for App<'_> {
); );
let window_id = new_window.id(); let window_id = new_window.id();
let wgpu_ctx = WgpuCtx::new_blocking(new_window.clone()).unwrap(); let wgpu_ctx = WgpuCtx::new_blocking(new_window.clone()).unwrap();
self.windows.insert(window_id, WindowContext { window: new_window, ctx: wgpu_ctx }); self.windows.insert(
window_id,
WindowContext {
window: new_window,
ctx: wgpu_ctx,
},
);
} }
_ => info!("Unimplemented keycode: {:?}", code), _ => info!("Unimplemented keycode: {:?}", code),
} }
@ -99,8 +115,7 @@ impl ApplicationHandler for App<'_> {
if let Some(window_context) = self.windows.get_mut(&window_id) { if let Some(window_context) = self.windows.get_mut(&window_id) {
window_context.ctx.resize(size.into()); window_context.ctx.resize(size.into());
window_context.window.request_redraw(); window_context.window.request_redraw();
let size_str: String = let size_str: String = size.height.to_string() + "x" + &size.width.to_string();
size.height.to_string() + "x" + &size.width.to_string();
debug!("Window resized to {:?}", size_str); debug!("Window resized to {:?}", size_str);
} }
} }

View file

@ -4,18 +4,33 @@ use colored::Colorize;
use tokio::runtime; use tokio::runtime;
#[allow(unused_imports)] #[allow(unused_imports)]
use tracing::{debug, error, info, warn}; use tracing::{debug, error, info, warn};
use tracing::{level_filters::LevelFilter, subscriber::set_global_default};
use tracing_subscriber::{layer::Filter, util::SubscriberInitExt};
use winit::event_loop::EventLoop; use winit::event_loop::EventLoop;
pub mod core; pub mod core;
fn init_logger() {
let subscriber = tracing_subscriber::fmt()
.with_max_level(LevelFilter::DEBUG)
.with_level(true)
.compact()
.pretty()
.log_internal_errors(false)
.without_time()
.with_thread_names(true)
.finish();
set_global_default(subscriber).expect("Failed to set default subscriber");
}
#[tokio::main(flavor = "current_thread")] #[tokio::main(flavor = "current_thread")]
async fn main() -> anyhow::Result<()> { async fn main() -> anyhow::Result<()> {
init_logger();
if !cfg!(debug_assertions) { if !cfg!(debug_assertions) {
println!("{}", "Debug mode disabled".bright_blue()); info!("{}", "Debug mode disabled".bright_blue());
set_panic_hook(); set_panic_hook();
} }
setup(); setup();
splash::print_splash();
info!("Type 'help' for a list of commands.");
let repl_thread = std::thread::spawn(|| { let repl_thread = std::thread::spawn(|| {
let rt = runtime::Builder::new_current_thread() let rt = runtime::Builder::new_current_thread()
@ -26,6 +41,9 @@ async fn main() -> anyhow::Result<()> {
}); });
let event_loop = EventLoop::new().unwrap(); let event_loop = EventLoop::new().unwrap();
splash::print_splash();
info!("Type 'help' for a list of commands.");
core::render::init_renderer(event_loop); core::render::init_renderer(event_loop);
if let Err(_) = repl_thread.join() { if let Err(_) = repl_thread.join() {