From f54f73b1058087ccb2c889197386320ab153a890 Mon Sep 17 00:00:00 2001 From: Ottatop Date: Thu, 19 Oct 2023 19:55:14 -0500 Subject: [PATCH] Simplify window methods --- api/rust/src/window.rs | 108 +++++++++++------------------------------ 1 file changed, 28 insertions(+), 80 deletions(-) diff --git a/api/rust/src/window.rs b/api/rust/src/window.rs index 1c814e2..0d09c19 100644 --- a/api/rust/src/window.rs +++ b/api/rust/src/window.rs @@ -8,12 +8,12 @@ pub struct Window; impl Window { pub fn get_by_class<'a>(&self, class: &'a str) -> impl Iterator + 'a { self.get_all() - .filter(|win| win.class().as_deref() == Some(class)) + .filter(|win| win.properties().class.as_deref() == Some(class)) } pub fn get_focused(&self) -> Option { self.get_all() - .find(|win| win.focused().is_some_and(|focused| focused)) + .find(|win| win.properties().focused.is_some_and(|focused| focused)) } pub fn get_all(&self) -> impl Iterator { @@ -27,6 +27,17 @@ impl Window { pub struct WindowHandle(WindowId); +#[derive(Debug)] +pub struct WindowProperties { + pub size: Option<(i32, i32)>, + pub loc: Option<(i32, i32)>, + pub class: Option, + pub title: Option, + pub focused: Option, + pub floating: Option, + pub fullscreen_or_maximized: Option, +} + impl WindowHandle { pub fn toggle_floating(&self) { send_msg(Msg::ToggleFloating { window_id: self.0 }).unwrap(); @@ -53,91 +64,28 @@ impl WindowHandle { send_msg(Msg::CloseWindow { window_id: self.0 }).unwrap(); } - pub fn size(&self) -> Option<(i32, i32)> { - let RequestResponse::WindowProps { size, .. } = - request(Request::GetWindowProps { window_id: self.0 }) - else { - unreachable!() - }; - - size - } - - pub fn loc(&self) -> Option<(i32, i32)> { - let RequestResponse::WindowProps { loc, .. } = - request(Request::GetWindowProps { window_id: self.0 }) - else { - unreachable!() - }; - - loc - } - - pub fn class(&self) -> Option { - let RequestResponse::WindowProps { class, .. } = - request(Request::GetWindowProps { window_id: self.0 }) - else { - unreachable!() - }; - - class - } - - pub fn title(&self) -> Option { - let RequestResponse::WindowProps { title, .. } = - request(Request::GetWindowProps { window_id: self.0 }) - else { - unreachable!() - }; - - title - } - - pub fn floating(&self) -> Option { - let RequestResponse::WindowProps { floating, .. } = - request(Request::GetWindowProps { window_id: self.0 }) - else { - unreachable!() - }; - - floating - } - - pub fn maximized(&self) -> Option { + pub fn properties(&self) -> WindowProperties { let RequestResponse::WindowProps { + size, + loc, + class, + title, + focused, + floating, fullscreen_or_maximized, - .. } = request(Request::GetWindowProps { window_id: self.0 }) else { unreachable!() }; - fullscreen_or_maximized.map(|fullscreen_or_maximized| { - matches!(fullscreen_or_maximized, FullscreenOrMaximized::Maximized) - }) - } - - pub fn fullscreen(&self) -> Option { - let RequestResponse::WindowProps { + WindowProperties { + size, + loc, + class, + title, + focused, + floating, fullscreen_or_maximized, - .. - } = request(Request::GetWindowProps { window_id: self.0 }) - else { - unreachable!() - }; - - fullscreen_or_maximized.map(|fullscreen_or_maximized| { - matches!(fullscreen_or_maximized, FullscreenOrMaximized::Fullscreen) - }) - } - - pub fn focused(&self) -> Option { - let RequestResponse::WindowProps { focused, .. } = - request(Request::GetWindowProps { window_id: self.0 }) - else { - unreachable!() - }; - - focused + } } }