know what the main window is

This commit is contained in:
Alex Janka 2023-10-11 13:47:57 +11:00
parent e53dff0a30
commit 5e8b3e612c
2 changed files with 41 additions and 5 deletions

View file

@ -167,7 +167,7 @@ fn run(args: Args) -> ! {
let mut window_manager = WindowManager::new(sender, stream); let mut window_manager = WindowManager::new(sender, stream);
let window = window_manager.add( let window = window_manager.add_main(
scale_override, scale_override,
Some(Gilrs::new().unwrap()), Some(Gilrs::new().unwrap()),
shader_path, shader_path,

View file

@ -45,6 +45,7 @@ pub struct WindowManager {
} }
struct WindowManagerData { struct WindowManagerData {
main_window: Option<WindowId>,
windows: HashMap<WindowId, Arc<WindowData>>, windows: HashMap<WindowId, Arc<WindowData>>,
window_data_manager: Arc<RendererBackendManager>, window_data_manager: Arc<RendererBackendManager>,
input: Arc<Mutex<WinitInputHelper>>, input: Arc<Mutex<WinitInputHelper>>,
@ -63,6 +64,7 @@ impl WindowManager {
Self { Self {
event_loop, event_loop,
data: WindowManagerData { data: WindowManagerData {
main_window: None,
windows: HashMap::new(), windows: HashMap::new(),
window_data_manager, window_data_manager,
input: Arc::new(Mutex::new(WinitInputHelper::new())), input: Arc::new(Mutex::new(WinitInputHelper::new())),
@ -72,6 +74,23 @@ impl WindowManager {
} }
} }
pub(crate) fn add_main(
&mut self,
factor: usize,
gamepad_handler: Option<Gilrs>,
shader_path: Option<PathBuf>,
resizable: bool,
) -> WindowRenderer {
self.data.add(
factor,
gamepad_handler,
shader_path,
resizable,
&self.event_loop,
true,
)
}
pub(crate) fn add( pub(crate) fn add(
&mut self, &mut self,
factor: usize, factor: usize,
@ -85,6 +104,7 @@ impl WindowManager {
shader_path, shader_path,
resizable, resizable,
&self.event_loop, &self.event_loop,
false,
) )
} }
@ -109,6 +129,7 @@ impl WindowManagerData {
shader_path: Option<PathBuf>, shader_path: Option<PathBuf>,
resizable: bool, resizable: bool,
event_loop: &EventLoop<()>, event_loop: &EventLoop<()>,
is_main: bool,
) -> WindowRenderer { ) -> WindowRenderer {
let (r, info) = WindowRenderer::new( let (r, info) = WindowRenderer::new(
factor, factor,
@ -120,6 +141,9 @@ impl WindowManagerData {
resizable, resizable,
); );
self.windows.insert(info.id, info.data); self.windows.insert(info.id, info.data);
if is_main {
self.main_window = Some(info.id);
}
r r
} }
@ -134,7 +158,11 @@ impl WindowManagerData {
if let Ok(mut i) = self.input.lock() { if let Ok(mut i) = self.input.lock() {
if i.update(&event) && i.key_pressed(VirtualKeyCode::Space) { if i.update(&event) && i.key_pressed(VirtualKeyCode::Space) {
self.windows.iter().for_each(|(_id, window)| { if let Some(window) = self
.main_window
.as_ref()
.and_then(|id| self.windows.get(id))
{
if let Ok(buf) = window.last_buf.lock() { if let Ok(buf) = window.last_buf.lock() {
if let Ok(size) = window.rendered_size.read() { if let Ok(size) = window.rendered_size.read() {
let image = ImageBuffer::<image::Rgba<u8>, _>::from_raw( let image = ImageBuffer::<image::Rgba<u8>, _>::from_raw(
@ -169,7 +197,7 @@ impl WindowManagerData {
.expect("Could not save screenshot!"); .expect("Could not save screenshot!");
} }
} }
}); }
} }
} }
@ -179,9 +207,17 @@ impl WindowManagerData {
} }
Event::WindowEvent { Event::WindowEvent {
event: WindowEvent::CloseRequested, event: WindowEvent::CloseRequested,
window_id: _, window_id,
} => { } => {
if self.main_window.is_some_and(|v| v == window_id) {
self.sender.send(EmulatorMessage::Exit).unwrap(); self.sender.send(EmulatorMessage::Exit).unwrap();
} else if let Some(window) = self
.main_window
.as_ref()
.and_then(|id| self.windows.get(id))
{
window.window.focus_window();
}
} }
Event::MainEventsCleared => { Event::MainEventsCleared => {
if run_return { if run_return {