diff --git a/komorebi-bar/src/widgets/komorebi.rs b/komorebi-bar/src/widgets/komorebi.rs index a9a3a1f1..2f19b9c7 100644 --- a/komorebi-bar/src/widgets/komorebi.rs +++ b/komorebi-bar/src/widgets/komorebi.rs @@ -1015,7 +1015,7 @@ pub struct WindowInfo { impl From<&Window> for WindowInfo { fn from(value: &Window) -> Self { Self { - title: value.title().ok(), + title: value.title(), icon: ImageIcon::try_load(value.hwnd, || { windows_icons::get_icon_by_hwnd(value.hwnd) .or_else(|| windows_icons_fallback::get_icon_by_process_id(value.process_id())) diff --git a/komorebi-gui/src/main.rs b/komorebi-gui/src/main.rs index 9af90da9..d00aa942 100644 --- a/komorebi-gui/src/main.rs +++ b/komorebi-gui/src/main.rs @@ -226,8 +226,8 @@ extern "system" fn enum_window( if window.is_window() && !window.is_miminized() && window.is_visible() - && window.title().is_ok() - && window.exe().is_ok() + && window.title().is_some() + && window.exe().is_some() { windows.push(window); } @@ -252,7 +252,7 @@ impl eframe::App for KomorebiGui { ui.collapsing("Window Rules", |ui| { let window = Window::from(self.debug_hwnd); - let label = if let (Ok(title), Ok(exe)) = (window.title(), window.exe()) { + let label = if let (Some(title), Some(exe)) = (window.title(), window.exe()) { format!("{title} ({exe})") } else { String::from("Select a Window") diff --git a/komorebi/src/container.rs b/komorebi/src/container.rs index 4e5b329c..92dc5341 100644 --- a/komorebi/src/container.rs +++ b/komorebi/src/container.rs @@ -19,7 +19,7 @@ impl ContainerWindow for Window { } fn exe_name(&self) -> Option { - Window::exe(*self).ok() + Window::exe(*self) } fn hide_window(&self) { @@ -48,7 +48,7 @@ pub trait ContainerExt { impl ContainerExt for Container { fn hwnd_from_exe(&self, exe: &str) -> Option { for window in self.windows() { - if let Ok(window_exe) = window.exe() + if let Some(window_exe) = window.exe() && exe == window_exe { return Option::from(window.hwnd); diff --git a/komorebi/src/monitor_reconciliator/mod.rs b/komorebi/src/monitor_reconciliator/mod.rs index 0974a667..58249c3b 100644 --- a/komorebi/src/monitor_reconciliator/mod.rs +++ b/komorebi/src/monitor_reconciliator/mod.rs @@ -786,7 +786,7 @@ where workspace.containers_mut().iter_mut().enumerate() { container.windows_mut().retain(|window| { - window.exe().is_ok() + window.exe().is_some() && !known_hwnds.contains_key(&window.hwnd) }); @@ -828,7 +828,7 @@ where } if let Some(window) = &workspace.maximized_window { - if window.exe().is_err() + if window.exe().is_none() || known_hwnds.contains_key(&window.hwnd) { workspace.maximized_window = None; @@ -841,7 +841,7 @@ where if let Some(container) = &mut workspace.monocle_container { container.windows_mut().retain(|window| { - window.exe().is_ok() + window.exe().is_some() && !known_hwnds.contains_key(&window.hwnd) }); @@ -868,7 +868,7 @@ where } workspace.floating_windows_mut().retain(|window| { - window.exe().is_ok() + window.exe().is_some() && !known_hwnds.contains_key(&window.hwnd) }); diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index ef834346..309df3c9 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -2002,7 +2002,7 @@ if (!(Get-Process komorebi-bar -ErrorAction SilentlyContinue)) pub fn add_session_float_rule(&mut self) -> eyre::Result<()> { let foreground_window = WindowsApi::foreground_window()?; let window = Window::from(foreground_window); - if let (Ok(exe), Ok(title), Ok(class)) = (window.exe(), window.title(), window.class()) { + if let (Some(exe), Some(title), Some(class)) = (window.exe(), window.title(), window.class()) { let rule = MatchingRule::Composite(vec![ IdWithIdentifier { kind: ApplicationIdentifier::Exe, @@ -2052,22 +2052,22 @@ if (!(Get-Process komorebi-bar -ErrorAction SilentlyContinue)) for window in container.windows() { match identifier { ApplicationIdentifier::Path => { - if window.path()? == *id { + if window.path().ok_or_eyre("the window has no path")? == *id { hwnds_to_purge.push((i, window.hwnd)); } } ApplicationIdentifier::Exe => { - if window.exe()? == *id { + if window.exe().ok_or_eyre("the window has no exe")? == *id { hwnds_to_purge.push((i, window.hwnd)); } } ApplicationIdentifier::Class => { - if window.class()? == *id { + if window.class().ok_or_eyre("the window has no class")? == *id { hwnds_to_purge.push((i, window.hwnd)); } } ApplicationIdentifier::Title => { - if window.title()? == *id { + if window.title().ok_or_eyre("the window has no title")? == *id { hwnds_to_purge.push((i, window.hwnd)); } } diff --git a/komorebi/src/process_event.rs b/komorebi/src/process_event.rs index a5f031bd..0b1f9cbb 100644 --- a/komorebi/src/process_event.rs +++ b/komorebi/src/process_event.rs @@ -282,7 +282,7 @@ impl WindowManager { // // This check ensures that we only update the focused monitor when the window // triggering monitor reconciliation is known to not be tied to a specific monitor. - if let Ok(class) = window.class() + if let Some(class) = window.class() && class != "OleMainThreadWndClass" && self.focused_monitor_idx() != monitor_idx { @@ -364,10 +364,10 @@ impl WindowManager { TRAY_AND_MULTI_WINDOW_IDENTIFIERS.lock(); let regex_identifiers = REGEX_IDENTIFIERS.lock(); - let title = &window.title()?; - let exe_name = &window.exe()?; - let class = &window.class()?; - let path = &window.path()?; + let title = &window.title().ok_or_eyre("the window has no title")?; + let exe_name = &window.exe().ok_or_eyre("the window has no exe")?; + let class = &window.class().ok_or_eyre("the window has no class")?; + let path = &window.path().ok_or_eyre("the window has no path")?; // We don't want to purge windows that have been deliberately hidden by us, eg. when // they are not on the top of a container stack. @@ -550,7 +550,7 @@ impl WindowManager { if !floating_applications.is_empty() { let regex_identifiers = REGEX_IDENTIFIERS.lock(); - if let (Ok(title), Ok(exe_name), Ok(class), Ok(path)) = + if let (Some(title), Some(exe_name), Some(class), Some(path)) = (window.title(), window.exe(), window.class(), window.path()) { should_float = should_act( diff --git a/komorebi/src/stackbar_manager/stackbar.rs b/komorebi/src/stackbar_manager/stackbar.rs index 78e34d84..d2b31a54 100644 --- a/komorebi/src/stackbar_manager/stackbar.rs +++ b/komorebi/src/stackbar_manager/stackbar.rs @@ -1,3 +1,4 @@ +use color_eyre::eyre::OptionExt; use crate::DEFAULT_CONTAINER_PADDING; use crate::WINDOWS_11; use crate::WindowsApi; @@ -276,10 +277,10 @@ impl Stackbar { let label = match STACKBAR_LABEL.load() { StackbarLabel::Process => { - let exe = window.exe()?; + let exe = window.exe().ok_or_eyre("the window has no exe")?; exe.trim_end_matches(".exe").to_string() } - StackbarLabel::Title => window.title()?, + StackbarLabel::Title => window.title().ok_or_eyre("the window has no title")?, }; let mut tab_title: Vec = label.encode_utf16().collect(); diff --git a/komorebi/src/transparency_manager.rs b/komorebi/src/transparency_manager.rs index daf542d3..1194a9e8 100644 --- a/komorebi/src/transparency_manager.rs +++ b/komorebi/src/transparency_manager.rs @@ -153,7 +153,7 @@ pub fn handle_notifications(wm: Arc>) -> color_eyre::Result if window_idx == focused_window_idx { let mut should_make_transparent = true; if !transparency_blacklist.is_empty() - && let (Ok(title), Ok(exe_name), Ok(class), Ok(path)) = ( + && let (Some(title), Some(exe_name), Some(class), Some(path)) = ( window.title(), window.exe(), window.class(), diff --git a/komorebi/src/window.rs b/komorebi/src/window.rs index eef4f5e5..6bebdbbf 100644 --- a/komorebi/src/window.rs +++ b/komorebi/src/window.rs @@ -96,15 +96,13 @@ pub struct WindowDetails { pub class: String, } -impl TryFrom for WindowDetails { - type Error = eyre::ErrReport; - - fn try_from(value: Window) -> std::result::Result { - Ok(Self { - title: value.title()?, - exe: value.exe()?, - class: value.class()?, - }) +impl From for WindowDetails { + fn from(value: Window) -> Self { + Self { + title: value.title().unwrap_or_default(), + exe: value.exe().unwrap_or_default(), + class: value.class().unwrap_or_default(), + } } } @@ -112,15 +110,15 @@ impl Display for Window { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { let mut display = format!("(hwnd: {}", self.hwnd); - if let Ok(title) = self.title() { + if let Some(title) = self.title() { write!(display, ", title: {title}")?; } - if let Ok(exe) = self.exe() { + if let Some(exe) = self.exe() { write!(display, ", exe: {exe}")?; } - if let Ok(class) = self.class() { + if let Some(class) = self.class() { write!(display, ", class: {class}")?; } @@ -141,19 +139,19 @@ impl Serialize for Window { "title", &self .title() - .unwrap_or_else(|_| String::from("could not get window title")), + .unwrap_or_else(|| String::from("could not get window title")), )?; state.serialize_field( "exe", &self .exe() - .unwrap_or_else(|_| String::from("could not get window exe")), + .unwrap_or_else(|| String::from("could not get window exe")), )?; state.serialize_field( "class", &self .class() - .unwrap_or_else(|_| String::from("could not get window class")), + .unwrap_or_else(|| String::from("could not get window class")), )?; state.serialize_field( "rect", @@ -710,24 +708,32 @@ impl Window { Ok(ExtendedWindowStyle::from_bits_truncate(bits)) } - pub fn title(self) -> eyre::Result { - WindowsApi::window_text_w(self.hwnd) + pub fn title(self) -> Option { + WindowsApi::window_text_w(self.hwnd).ok() } - pub fn path(self) -> eyre::Result { - let (process_id, _) = WindowsApi::window_thread_process_id(self.hwnd); - let handle = WindowsApi::process_handle(process_id)?; - let path = WindowsApi::exe_path(handle); - WindowsApi::close_process(handle)?; - path + pub fn path(self) -> Option { + let lookup = || -> eyre::Result { + let (process_id, _) = WindowsApi::window_thread_process_id(self.hwnd); + let handle = WindowsApi::process_handle(process_id)?; + let path = WindowsApi::exe_path(handle); + WindowsApi::close_process(handle)?; + path + }; + + lookup().ok() } - pub fn exe(self) -> eyre::Result { - let (process_id, _) = WindowsApi::window_thread_process_id(self.hwnd); - let handle = WindowsApi::process_handle(process_id)?; - let exe = WindowsApi::exe(handle); - WindowsApi::close_process(handle)?; - exe + pub fn exe(self) -> Option { + let lookup = || -> eyre::Result { + let (process_id, _) = WindowsApi::window_thread_process_id(self.hwnd); + let handle = WindowsApi::process_handle(process_id)?; + let exe = WindowsApi::exe(handle); + WindowsApi::close_process(handle)?; + exe + }; + + lookup().ok() } pub fn process_id(self) -> u32 { @@ -735,8 +741,8 @@ impl Window { process_id } - pub fn class(self) -> eyre::Result { - WindowsApi::real_window_class_w(self.hwnd) + pub fn class(self) -> Option { + WindowsApi::real_window_class_w(self.hwnd).ok() } pub fn is_cloaked(self) -> eyre::Result { @@ -836,7 +842,7 @@ impl Window { debug.has_minimum_height = true; - if self.title().is_err() { + if self.title().is_none() { return Ok(false); } @@ -864,7 +870,7 @@ impl Window { (true, _) | // If not allowing cloaked windows, we need to ensure the window is not cloaked (false, false) => { - if let (Ok(title), Ok(exe_name), Ok(class), Ok(path)) = (self.title(), self.exe(), self.class(), self.path()) { + if let (Some(title), Some(exe_name), Some(class), Some(path)) = (self.title(), self.exe(), self.class(), self.path()) { debug.title = Some(title.clone()); debug.exe_name = Some(exe_name.clone()); debug.class = Some(class.clone()); diff --git a/komorebi/src/window_manager.rs b/komorebi/src/window_manager.rs index 23b42707..f5e63666 100644 --- a/komorebi/src/window_manager.rs +++ b/komorebi/src/window_manager.rs @@ -212,7 +212,7 @@ impl WindowManager { for workspace in monitor.workspaces() { for container in workspace.containers() { for window in container.windows() { - if window.exe().is_err() { + if window.exe().is_none() { can_apply = false; break; } @@ -220,7 +220,7 @@ impl WindowManager { } if let Some(window) = workspace.maximized_window - && window.exe().is_err() + && window.exe().is_none() { can_apply = false; break; @@ -228,7 +228,7 @@ impl WindowManager { if let Some(container) = &workspace.monocle_container { for window in container.windows() { - if window.exe().is_err() { + if window.exe().is_none() { can_apply = false; break; } @@ -236,7 +236,7 @@ impl WindowManager { } for window in workspace.floating_windows() { - if window.exe().is_err() { + if window.exe().is_none() { can_apply = false; break; } @@ -599,7 +599,7 @@ impl WindowManager { let mut already_moved_window_handles = self.already_moved_window_handles.lock(); - if let (Ok(exe_name), Ok(title), Ok(class), Ok(path)) = + if let (Some(exe_name), Some(title), Some(class), Some(path)) = (window.exe(), window.title(), window.class(), window.path()) { for rule in &*workspace_matching_rules { @@ -637,7 +637,7 @@ impl WindowManager { already_moved_window_handles.insert(window.hwnd); self.add_window_handle_to_move_based_on_workspace_rule( - &window.title()?, + &window.title().ok_or_eyre("the window has no title")?, window.hwnd, i, j, @@ -649,7 +649,7 @@ impl WindowManager { } } else { self.add_window_handle_to_move_based_on_workspace_rule( - &window.title()?, + &window.title().ok_or_eyre("the window has no title")?, window.hwnd, i, j, diff --git a/komorebi/src/window_manager_event.rs b/komorebi/src/window_manager_event.rs index 2e62be3e..9ef24502 100644 --- a/komorebi/src/window_manager_event.rs +++ b/komorebi/src/window_manager_event.rs @@ -195,10 +195,10 @@ impl WindowManagerEvent { OBJECT_NAME_CHANGE_TITLE_IGNORE_LIST.lock(); let regex_identifiers = REGEX_IDENTIFIERS.lock(); - let title = &window.title().ok()?; - let exe_name = &window.exe().ok()?; - let class = &window.class().ok()?; - let path = &window.path().ok()?; + let title = &window.title()?; + let exe_name = &window.exe()?; + let class = &window.class()?; + let path = &window.path()?; let mut should_trigger_show = should_act( title, diff --git a/komorebi/src/windows_backend.rs b/komorebi/src/windows_backend.rs index 8dc87437..febc1a04 100644 --- a/komorebi/src/windows_backend.rs +++ b/komorebi/src/windows_backend.rs @@ -66,10 +66,10 @@ impl WindowsBackend { rect: window.window_rect().unwrap_or_default(), minimized: window.is_miminized(), alive: true, - title: window.title().ok(), - class: window.class().ok(), - exe: window.exe().ok(), - path: window.path().ok(), + title: window.title(), + class: window.class(), + exe: window.exe(), + path: window.path(), // fullscreen has no source here: what komorebi calls it is decided in the core fullscreen: None, maximized: Some(window.is_maximized()), diff --git a/komorebi/src/workspace.rs b/komorebi/src/workspace.rs index c3a51d20..990450f0 100644 --- a/komorebi/src/workspace.rs +++ b/komorebi/src/workspace.rs @@ -948,7 +948,7 @@ impl Workspace { } if let Some(window) = self.maximized_window - && let Ok(window_exe) = window.exe() + && let Some(window_exe) = window.exe() && exe == window_exe { return Option::from(window.hwnd); @@ -961,7 +961,7 @@ impl Workspace { } for window in self.floating_windows() { - if let Ok(window_exe) = window.exe() + if let Some(window_exe) = window.exe() && exe == window_exe { return Option::from(window.hwnd); @@ -982,7 +982,7 @@ impl Workspace { } if let Some(window) = self.maximized_window - && let Ok(window_exe) = window.exe() + && let Some(window_exe) = window.exe() && exe == window_exe { return Some(WorkspaceWindowLocation::Maximized); @@ -995,7 +995,7 @@ impl Workspace { } for (window_idx, window) in self.floating_windows().iter().enumerate() { - if let Ok(window_exe) = window.exe() + if let Some(window_exe) = window.exe() && exe == window_exe { return Some(WorkspaceWindowLocation::Floating(window_idx)); @@ -1403,16 +1403,16 @@ impl Workspace { self.resolve_placement_target(target, fallback_idx) } InitialWindowPlacementRules::Rules(rules_map) => { - let Ok(title) = window.title() else { + let Some(title) = window.title() else { return fallback_idx; }; - let Ok(exe_name) = window.exe() else { + let Some(exe_name) = window.exe() else { return fallback_idx; }; - let Ok(class) = window.class() else { + let Some(class) = window.class() else { return fallback_idx; }; - let Ok(path) = window.path() else { + let Some(path) = window.path() else { return fallback_idx; }; @@ -2087,30 +2087,25 @@ impl Workspace { pub fn visible_window_details(&self) -> Vec { let mut vec: Vec = vec![]; - if let Some(maximized) = self.maximized_window - && let Ok(details) = (maximized).try_into() - { - vec.push(details); + if let Some(maximized) = self.maximized_window { + vec.push(WindowDetails::from(maximized)); } if let Some(monocle) = &self.monocle_container && let Some(focused) = monocle.focused_window() - && let Ok(details) = (*focused).try_into() { - vec.push(details); + vec.push(WindowDetails::from(*focused)); } for container in self.containers() { - if let Some(focused) = container.focused_window() - && let Ok(details) = (*focused).try_into() - { - vec.push(details); + if let Some(focused) = container.focused_window() { + vec.push(WindowDetails::from(*focused)); } } for window in self.floating_windows() { - if let Ok(details) = (*window).try_into() { - vec.push(details); + { + vec.push(WindowDetails::from(*window)); } }