diff --git a/komorebi/src/main.rs b/komorebi/src/main.rs index fcf11458..3e1d1044 100644 --- a/komorebi/src/main.rs +++ b/komorebi/src/main.rs @@ -202,29 +202,47 @@ pub fn load_configuration() -> Result<()> { Ok(()) } -pub fn current_virtual_desktop() -> Result> { +pub fn current_virtual_desktop() -> Option> { let hkcu = RegKey::predef(HKEY_CURRENT_USER); // This is the path on Windows 10 - let current = match hkcu.open_subkey(format!( - r#"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\SessionInfo\{}\VirtualDesktops"#, - SESSION_ID.load(Ordering::SeqCst) - )) { - Ok(desktops) => { - if let Ok(current) = desktops.get_raw_value("CurrentVirtualDesktop") { - current.bytes - } else { - // This is the path on Windows 11 - let desktops = hkcu.open_subkey( - r#"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VirtualDesktops"#, - )?; - desktops.get_raw_value("CurrentVirtualDesktop")?.bytes - } - } - Err(_) => unreachable!(), - }; + let mut current = hkcu + .open_subkey(format!( + r#"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\SessionInfo\{}\VirtualDesktops"#, + SESSION_ID.load(Ordering::SeqCst) + )) + .ok() + .and_then( + |desktops| match desktops.get_raw_value("CurrentVirtualDesktop") { + Ok(current) => Option::from(current.bytes), + Err(_) => None, + }, + ); - Ok(current) + // This is the path on Windows 11 + if current.is_none() { + current = hkcu + .open_subkey(r#"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VirtualDesktops"#) + .ok() + .and_then( + |desktops| match desktops.get_raw_value("CurrentVirtualDesktop") { + Ok(current) => Option::from(current.bytes), + Err(_) => None, + }, + ); + } + + // For Win10 users that do not use virtual desktops, the CurrentVirtualDesktop value will not + // exist until one has been created in the task view + + // The registry value will also not exist on user login if virtual desktops have been created + // but the task view has not been initiated + + // In both of these cases, we return None, and the virtual desktop validation will never run. In + // the latter case, if the user desires this validation after initiating the task view, komorebi + // should be restarted, and then when this // fn runs again for the first time, it will pick up + // the value of CurrentVirtualDesktop and validate against it accordingly + current } #[derive(Debug, Serialize)] diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index 8069c60f..9b488e90 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -25,6 +25,7 @@ use komorebi_core::SocketMessage; use komorebi_core::StateQuery; use komorebi_core::WindowContainerBehaviour; +use crate::current_virtual_desktop; use crate::notify_subscribers; use crate::window_manager; use crate::window_manager::WindowManager; @@ -68,13 +69,15 @@ pub fn listen_for_commands(wm: Arc>) { impl WindowManager { #[tracing::instrument(skip(self))] pub fn process_command(&mut self, message: SocketMessage) -> Result<()> { - if let Ok(id) = crate::current_virtual_desktop() { - if id != self.virtual_desktop_id { - tracing::info!( - "ignoring events and commands while not on virtual desktop {:?}", - self.virtual_desktop_id - ); - return Ok(()); + if let Some(virtual_desktop_id) = &self.virtual_desktop_id { + if let Some(id) = current_virtual_desktop() { + if id != *virtual_desktop_id { + tracing::info!( + "ignoring events and commands while not on virtual desktop {:?}", + virtual_desktop_id + ); + return Ok(()); + } } } diff --git a/komorebi/src/process_event.rs b/komorebi/src/process_event.rs index 34bc9e7c..084ea076 100644 --- a/komorebi/src/process_event.rs +++ b/komorebi/src/process_event.rs @@ -12,6 +12,7 @@ use komorebi_core::Rect; use komorebi_core::Sizing; use komorebi_core::WindowContainerBehaviour; +use crate::current_virtual_desktop; use crate::notify_subscribers; use crate::window_manager::WindowManager; use crate::window_manager_event::WindowManagerEvent; @@ -51,13 +52,15 @@ impl WindowManager { return Ok(()); } - if let Ok(id) = crate::current_virtual_desktop() { - if id != self.virtual_desktop_id { - tracing::info!( - "ignoring events and commands while not on virtual desktop {:?}", - self.virtual_desktop_id - ); - return Ok(()); + if let Some(virtual_desktop_id) = &self.virtual_desktop_id { + if let Some(id) = current_virtual_desktop() { + if id != *virtual_desktop_id { + tracing::info!( + "ignoring events and commands while not on virtual desktop {:?}", + virtual_desktop_id + ); + return Ok(()); + } } } diff --git a/komorebi/src/window_manager.rs b/komorebi/src/window_manager.rs index c23022e6..aebbf2ba 100644 --- a/komorebi/src/window_manager.rs +++ b/komorebi/src/window_manager.rs @@ -56,7 +56,7 @@ pub struct WindowManager { pub focus_follows_mouse: Option, pub mouse_follows_focus: bool, pub hotwatch: Hotwatch, - pub virtual_desktop_id: Vec, + pub virtual_desktop_id: Option>, pub has_pending_raise_op: bool, pub pending_move_op: Option<(usize, usize, usize)>, } @@ -158,7 +158,7 @@ impl WindowManager { right: 14, bottom: 7, }, - virtual_desktop_id: current_virtual_desktop()?, + virtual_desktop_id: current_virtual_desktop(), work_area_offset: None, window_container_behaviour: WindowContainerBehaviour::Create, resize_delta: 50,