mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-11-16 22:01:22 +01:00
refactor(windows): Window::title, exe, class and path return Option; WindowDetails is infallible
As on KDE. The Win32 error of the four lookups was never told apart: the
callers either turned it into a skip (.ok(), if let Ok, is_ok/is_err), a
default (unwrap_or_default) or a bare ?. They are now Option: .ok() goes,
Ok(..) patterns are Some(..), is_ok/is_err are is_some/is_none, and the
nine places that used ? now say ok_or_eyre("the window has no <field>")?
(IgnoreRule purge, stackbar label, the rule check in process_event, the
workspace-rule title in window_manager). WindowDetails: TryFrom<Window>
becomes From<Window> (unwrap_or_default per field, as KDE), and
visible_window_details pushes every visible window. komorebi-gui and
komorebi-bar adapt (is_some, no .ok()). Items 3 and 4 of the Windows list in
core-model.md section 7.
BEHAVIOUR: (1) komorebic visible-windows now lists a window whose title,
exe or class cannot be read, with empty strings, where it used to skip it;
(2) the error text of the ? sites changes from the Win32 error to "the
window has no <field>". Nothing else changes: the same lookups run in the
same order. e2e gate needed: commands (visible-windows, ignore-rule),
stacking (stackbar labels), lifecycle. Verified: Windows clippy -D warnings
via tools/fast-check PASS (workspace including komorebi-gui and komorebi-bar);
e2e not run.
This commit is contained in:
1 parent
de45c2f82a
commit
07c9ab8fc8
13 files changed
+94
-92
No files matched your search
@@ -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()))
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -19,7 +19,7 @@ impl ContainerWindow for Window {
|
||||
}
|
||||
|
||||
fn exe_name(&self) -> Option<String> {
|
||||
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<isize> {
|
||||
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);
|
||||
|
||||
@@ -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)
|
||||
});
|
||||
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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<u16> = label.encode_utf16().collect();
|
||||
|
||||
@@ -153,7 +153,7 @@ pub fn handle_notifications(wm: Arc<Mutex<WindowManager>>) -> 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(),
|
||||
|
||||
+39
-33
@@ -96,15 +96,13 @@ pub struct WindowDetails {
|
||||
pub class: String,
|
||||
}
|
||||
|
||||
impl TryFrom<Window> for WindowDetails {
|
||||
type Error = eyre::ErrReport;
|
||||
|
||||
fn try_from(value: Window) -> std::result::Result<Self, Self::Error> {
|
||||
Ok(Self {
|
||||
title: value.title()?,
|
||||
exe: value.exe()?,
|
||||
class: value.class()?,
|
||||
})
|
||||
impl From<Window> 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<String> {
|
||||
WindowsApi::window_text_w(self.hwnd)
|
||||
pub fn title(self) -> Option<String> {
|
||||
WindowsApi::window_text_w(self.hwnd).ok()
|
||||
}
|
||||
|
||||
pub fn path(self) -> eyre::Result<String> {
|
||||
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<String> {
|
||||
let lookup = || -> eyre::Result<String> {
|
||||
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<String> {
|
||||
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<String> {
|
||||
let lookup = || -> eyre::Result<String> {
|
||||
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<String> {
|
||||
WindowsApi::real_window_class_w(self.hwnd)
|
||||
pub fn class(self) -> Option<String> {
|
||||
WindowsApi::real_window_class_w(self.hwnd).ok()
|
||||
}
|
||||
|
||||
pub fn is_cloaked(self) -> eyre::Result<bool> {
|
||||
@@ -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());
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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()),
|
||||
|
||||
+15
-20
@@ -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<WindowDetails> {
|
||||
let mut vec: Vec<WindowDetails> = 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));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user