diff --git a/komorebi-for-kde/komorebi/src/backend/fake.rs b/komorebi-for-kde/komorebi/src/backend/fake.rs index cd67ae3a..6967c5e9 100644 --- a/komorebi-for-kde/komorebi/src/backend/fake.rs +++ b/komorebi-for-kde/komorebi/src/backend/fake.rs @@ -38,8 +38,6 @@ use super::Backend; use super::Look; -use super::MonitorInfo; -use super::WindowInfo; use crate::core::Rect; use color_eyre::eyre; use komorebi_backend as shared; @@ -130,23 +128,6 @@ fn wid(id: u32) -> WindowId { WindowId::from_u32(id) } -fn window_info(info: shared::WindowInfo) -> WindowInfo { - WindowInfo { - id: raw(info.id), - pid: info.pid, - title: info.title.unwrap_or_default(), - class: info.class.unwrap_or_default(), - exe: info.exe.unwrap_or_default(), - path: info.path.unwrap_or_default(), - rect: info.rect, - minimized: info.minimized, - fullscreen: info.fullscreen.unwrap_or_default(), - maximized: info.maximized.unwrap_or_default(), - normal: info.normal.unwrap_or(true), - alive: info.alive, - } -} - fn command(command: SharedCommand) -> Option { Some(match command { SharedCommand::SetRect(id, rect) => Command::SetRect(raw(id), rect), @@ -402,31 +383,20 @@ impl FakeBackend { } impl Backend for FakeBackend { - fn windows(&self) -> Vec { - self.inner.windows().into_iter().map(window_info).collect() + fn windows(&self) -> Vec { + self.inner.windows() } - fn window(&self, id: WindowId) -> Option { - self.inner.window(id).map(window_info) + fn window(&self, id: WindowId) -> Option { + self.inner.window(id) } fn foreground_window(&self) -> Option { self.inner.foreground_window() } - fn monitors(&self) -> shared::Result> { - Ok(self - .inner - .monitors()? - .into_iter() - .map(|m| MonitorInfo { - id: raw_monitor(m.id), - name: m.name.unwrap_or_default(), - serial_number_id: m.serial_number_id.unwrap_or_default(), - size: m.size, - work_area: m.work_area, - }) - .collect()) + fn monitors(&self) -> shared::Result> { + self.inner.monitors() } fn cursor_pos(&self) -> Option<(i32, i32)> { @@ -649,8 +619,9 @@ mod tests { fake.window(1.into()) .expect("window 1 was opened") .maximized + .unwrap_or_default() ); - assert!(fake.windows()[0].maximized); + assert!(fake.windows()[0].maximized.unwrap_or_default()); assert_eq!(fake.rect_of(1), Some(tile(0, 1920))); assert!(fake.commands().is_empty()); @@ -661,6 +632,7 @@ mod tests { .window(1.into()) .expect("window 1 was opened") .maximized + .unwrap_or_default() ); assert_eq!(fake.rect_of(1), Some(tile(100, 200))); } @@ -677,8 +649,9 @@ mod tests { fake.window(1.into()) .expect("window 1 was opened") .fullscreen + .unwrap_or_default() ); - assert!(fake.windows()[0].fullscreen); + assert!(fake.windows()[0].fullscreen.unwrap_or_default()); assert_eq!( fake.rect_of(1), Some(Rect { @@ -698,6 +671,7 @@ mod tests { .window(1.into()) .expect("window 1 was opened") .fullscreen + .unwrap_or_default() ); assert_eq!(fake.rect_of(1), Some(tile(100, 200))); } @@ -779,8 +753,8 @@ mod tests { assert!(fake.commands().is_empty()); assert_eq!(fake.rect_of(1), Some(tile(300, 300))); let window = fake.window(1.into()).expect("window 1 was opened"); - assert_eq!(window.title, "renamed"); - assert!(!window.normal); + assert_eq!(window.title, Some("renamed".to_string())); + assert!(window.normal == Some(false)); } #[test] @@ -788,7 +762,11 @@ mod tests { let fake = fake_with(&[3, 1, 2]); fake.close_window_now(2); - let ids: Vec<_> = fake.windows().iter().map(|w| w.id).collect(); + let ids: Vec<_> = fake + .windows() + .iter() + .map(|w| w.id.to_u32().unwrap()) + .collect(); assert_eq!(ids, [1, 3]); } diff --git a/komorebi-for-kde/komorebi/src/backend/mod.rs b/komorebi-for-kde/komorebi/src/backend/mod.rs index 1ce213c4..1eb88569 100644 --- a/komorebi-for-kde/komorebi/src/backend/mod.rs +++ b/komorebi-for-kde/komorebi/src/backend/mod.rs @@ -93,6 +93,46 @@ pub struct WindowInfo { pub alive: bool, } +impl From for WindowInfo { + /// KDE fills every field of a window, so the optional ones are always there; a backend that has + /// not got one gets the empty string or false, and a window is normal unless it says it is not. + fn from(info: komorebi_backend::WindowInfo) -> Self { + Self { + id: info.id.to_u32().unwrap_or(u32::MAX), + pid: info.pid, + title: info.title.unwrap_or_default(), + class: info.class.unwrap_or_default(), + exe: info.exe.unwrap_or_default(), + path: info.path.unwrap_or_default(), + rect: info.rect, + minimized: info.minimized, + fullscreen: info.fullscreen.unwrap_or_default(), + maximized: info.maximized.unwrap_or_default(), + normal: info.normal.unwrap_or(true), + alive: info.alive, + } + } +} + +impl From for komorebi_backend::WindowInfo { + fn from(info: WindowInfo) -> Self { + Self { + id: WindowId::from_u32(info.id), + pid: info.pid, + rect: info.rect, + minimized: info.minimized, + alive: info.alive, + title: Some(info.title), + class: Some(info.class), + exe: Some(info.exe), + path: Some(info.path), + fullscreen: Some(info.fullscreen), + maximized: Some(info.maximized), + normal: Some(info.normal), + } + } +} + /// A connected monitor, in logical pixels. #[derive(Clone, Debug, Default, PartialEq)] pub struct MonitorInfo { @@ -108,6 +148,30 @@ pub struct MonitorInfo { pub work_area: Rect, } +impl From for MonitorInfo { + fn from(info: komorebi_backend::MonitorInfo) -> Self { + Self { + id: info.id.to_u32().unwrap_or(u32::MAX), + name: info.name.unwrap_or_default(), + serial_number_id: info.serial_number_id.unwrap_or_default(), + size: info.size, + work_area: info.work_area, + } + } +} + +impl From for komorebi_backend::MonitorInfo { + fn from(info: MonitorInfo) -> Self { + Self { + id: MonitorId::from_u32(info.id), + size: info.size, + work_area: info.work_area, + name: Some(info.name), + serial_number_id: Some(info.serial_number_id), + } + } +} + /// How the compositor is asked to dress the windows komorebi arranges. /// /// komorebi decides what the windows look like and the compositor draws it, so this only @@ -212,16 +276,16 @@ pub struct BorderLook { /// ```rust,compile_fail,E0277 /// # use std::rc::Rc; /// # use color_eyre::eyre; -/// # use komorebi::backend::{Backend, MonitorInfo, WindowInfo}; +/// # use komorebi::backend::Backend; /// # use komorebi::core::Rect; /// # use komorebi_backend::{MonitorId, WindowId}; /// struct NotShareable(Rc); /// /// # impl Backend for NotShareable { -/// # fn windows(&self) -> Vec { vec![] } -/// # fn window(&self, _: WindowId) -> Option { None } +/// # fn windows(&self) -> Vec { vec![] } +/// # fn window(&self, _: WindowId) -> Option { None } /// # fn foreground_window(&self) -> Option { None } -/// # fn monitors(&self) -> komorebi_backend::Result> { Ok(vec![]) } +/// # fn monitors(&self) -> komorebi_backend::Result> { Ok(vec![]) } /// # fn cursor_pos(&self) -> Option<(i32, i32)> { None } /// # fn set_rect(&self, _: WindowId, _: &Rect) -> komorebi_backend::Result<()> { Ok(()) } /// # fn focus(&self, _: WindowId, _: bool) -> komorebi_backend::Result<()> { Ok(()) } @@ -243,10 +307,10 @@ pub trait Backend: Send + Sync + 'static { // ---- queries ------------------------------------------------------------------------------- /// Returns every window on the desktop that is a candidate for management, ordered by id. - fn windows(&self) -> Vec; + fn windows(&self) -> Vec; /// Returns what is known about the window, or `None` if the compositor has never heard of it. - fn window(&self, id: WindowId) -> Option; + fn window(&self, id: WindowId) -> Option; /// Returns the id of the window that has the focus, if any window does. fn foreground_window(&self) -> Option; @@ -256,7 +320,7 @@ pub trait Backend: Send + Sync + 'static { /// # Errors /// /// When the compositor cannot report its outputs. - fn monitors(&self) -> komorebi_backend::Result>; + fn monitors(&self) -> komorebi_backend::Result>; /// Returns where the pointer is, if the compositor says. fn cursor_pos(&self) -> Option<(i32, i32)>; @@ -627,7 +691,7 @@ pub trait Backend: Send + Sync + 'static { self.monitors().ok()?.into_iter().find_map(|m| { let (l, t) = (m.size.left, m.size.top); let (r, b) = (l + m.size.right, t + m.size.bottom); - (x >= l && x < r && y >= t && y < b).then_some(MonitorId::from_u32(m.id)) + (x >= l && x < r && y >= t && y < b).then_some(m.id) }) } } @@ -693,7 +757,7 @@ fn test_default() -> Option> { /// fake.open_window(7, "foot", "foot", "a terminal"); /// /// let titles = with_backend(fake.clone(), || { -/// backend().windows().into_iter().map(|w| w.title).collect::>() +/// backend().windows().into_iter().filter_map(|w| w.title).collect::>() /// }); /// /// assert_eq!(titles, ["a terminal"]); diff --git a/komorebi-for-kde/komorebi/src/backend/shared.rs b/komorebi-for-kde/komorebi/src/backend/shared.rs index 6f1ad541..2412b305 100644 --- a/komorebi-for-kde/komorebi/src/backend/shared.rs +++ b/komorebi-for-kde/komorebi/src/backend/shared.rs @@ -35,30 +35,13 @@ impl Shared { } } -fn window_info(info: super::WindowInfo) -> shared::WindowInfo { - shared::WindowInfo { - id: WindowId::from_u32(info.id), - pid: info.pid, - rect: info.rect, - minimized: info.minimized, - alive: info.alive, - title: Some(info.title), - class: Some(info.class), - exe: Some(info.exe), - path: Some(info.path), - fullscreen: Some(info.fullscreen), - maximized: Some(info.maximized), - normal: Some(info.normal), - } -} - impl shared::Backend for Shared { fn windows(&self) -> Vec { - self.0.windows().into_iter().map(window_info).collect() + self.0.windows() } fn window(&self, id: WindowId) -> Option { - self.0.window(id).map(window_info) + self.0.window(id) } fn foreground_window(&self) -> Option { @@ -66,18 +49,7 @@ impl shared::Backend for Shared { } fn monitors(&self) -> shared::Result> { - Ok(self - .0 - .monitors()? - .into_iter() - .map(|m| shared::MonitorInfo { - id: MonitorId::from_u32(m.id), - size: m.size, - work_area: m.work_area, - name: Some(m.name), - serial_number_id: Some(m.serial_number_id), - }) - .collect()) + self.0.monitors() } fn cursor_pos(&self) -> Option<(i32, i32)> { diff --git a/komorebi-for-kde/komorebi/src/desktop.rs b/komorebi-for-kde/komorebi/src/desktop.rs index 2378e213..6ced9a55 100644 --- a/komorebi-for-kde/komorebi/src/desktop.rs +++ b/komorebi-for-kde/komorebi/src/desktop.rs @@ -86,6 +86,7 @@ pub fn latest_monitor_information() -> eyre::Result> { Ok(backend() .monitors()? .into_iter() + .map(crate::backend::MonitorInfo::from) .map(|m| Monitor::new(m.id, m.size, m.work_area, &m.name, &m.serial_number_id)) .collect()) } @@ -351,7 +352,11 @@ pub fn load_workspace_information(wm: &mut WindowManager) -> eyre::Result<()> { } } - for info in backend().windows() { + for info in backend() + .windows() + .into_iter() + .map(crate::backend::WindowInfo::from) + { if info.minimized { continue; } diff --git a/komorebi-for-kde/komorebi/src/kwin/mod.rs b/komorebi-for-kde/komorebi/src/kwin/mod.rs index bfc6d8bc..f800c244 100644 --- a/komorebi-for-kde/komorebi/src/kwin/mod.rs +++ b/komorebi-for-kde/komorebi/src/kwin/mod.rs @@ -93,8 +93,6 @@ mod wallpaper; use crate::DATA_DIR; use crate::backend::Backend; use crate::backend::Look; -use crate::backend::MonitorInfo; -use crate::backend::WindowInfo; use crate::core::Rect; use crate::monitor_reconciliator; use crate::window_manager_event_listener; @@ -825,13 +823,19 @@ impl KWinBackend { } impl Backend for KWinBackend { - fn windows(&self) -> Vec { - self.shared.table.lock().windows() + fn windows(&self) -> Vec { + self.shared + .table + .lock() + .windows() + .into_iter() + .map(Into::into) + .collect() } - fn window(&self, id: WindowId) -> Option { + fn window(&self, id: WindowId) -> Option { let id = id.to_u32()?; - self.shared.table.lock().window(id) + self.shared.table.lock().window(id).map(Into::into) } fn foreground_window(&self) -> Option { @@ -852,12 +856,12 @@ impl Backend for KWinBackend { self.shared.table.lock().drag_end(id) } - fn monitors(&self) -> komorebi_backend::Result> { + fn monitors(&self) -> komorebi_backend::Result> { let monitors = self.shared.table.lock().monitors(); if monitors.is_empty() { return Err(platform(eyre::eyre!("KWin reported no outputs"))); } - Ok(monitors) + Ok(monitors.into_iter().map(Into::into).collect()) } fn cursor_pos(&self) -> Option<(i32, i32)> { diff --git a/komorebi-for-kde/komorebi/src/process_event.rs b/komorebi-for-kde/komorebi/src/process_event.rs index 2f654728..34b43004 100644 --- a/komorebi-for-kde/komorebi/src/process_event.rs +++ b/komorebi-for-kde/komorebi/src/process_event.rs @@ -460,7 +460,7 @@ impl WindowManager { // an app that remembers it was maximized opens that way if backend() .window(WindowId::from_u32(window_id)) - .is_some_and(|w| w.maximized) + .is_some_and(|w| w.maximized == Some(true)) { self.update_known_window_ids(); self.adopt_maximize_change(window_id, true)?; diff --git a/komorebi-for-kde/komorebi/src/window.rs b/komorebi-for-kde/komorebi/src/window.rs index a90902a1..4e8c4e02 100644 --- a/komorebi-for-kde/komorebi/src/window.rs +++ b/komorebi-for-kde/komorebi/src/window.rs @@ -192,7 +192,7 @@ impl Window { /// What the compositor knows about this window, including for a few moments after it closed pub fn info(self) -> Option { - backend().window(self.window_id()) + backend().window(self.window_id()).map(Into::into) } /// Whether the window still exists @@ -263,7 +263,7 @@ impl Window { pub fn is_fullscreen(self) -> bool { backend() .window(self.window_id()) - .is_some_and(|w| w.fullscreen) + .is_some_and(|w| w.fullscreen == Some(true)) } /// Asks the compositor to draw the window with this opacity, 255 being not see-through at all