Simplify window methods

This commit is contained in:
Ottatop 2023-10-19 19:55:14 -05:00
parent 17975cbbda
commit f54f73b105

View file

@ -8,12 +8,12 @@ pub struct Window;
impl Window { impl Window {
pub fn get_by_class<'a>(&self, class: &'a str) -> impl Iterator<Item = WindowHandle> + 'a { pub fn get_by_class<'a>(&self, class: &'a str) -> impl Iterator<Item = WindowHandle> + 'a {
self.get_all() 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<WindowHandle> { pub fn get_focused(&self) -> Option<WindowHandle> {
self.get_all() 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<Item = WindowHandle> { pub fn get_all(&self) -> impl Iterator<Item = WindowHandle> {
@ -27,6 +27,17 @@ impl Window {
pub struct WindowHandle(WindowId); pub struct WindowHandle(WindowId);
#[derive(Debug)]
pub struct WindowProperties {
pub size: Option<(i32, i32)>,
pub loc: Option<(i32, i32)>,
pub class: Option<String>,
pub title: Option<String>,
pub focused: Option<bool>,
pub floating: Option<bool>,
pub fullscreen_or_maximized: Option<FullscreenOrMaximized>,
}
impl WindowHandle { impl WindowHandle {
pub fn toggle_floating(&self) { pub fn toggle_floating(&self) {
send_msg(Msg::ToggleFloating { window_id: self.0 }).unwrap(); send_msg(Msg::ToggleFloating { window_id: self.0 }).unwrap();
@ -53,91 +64,28 @@ impl WindowHandle {
send_msg(Msg::CloseWindow { window_id: self.0 }).unwrap(); send_msg(Msg::CloseWindow { window_id: self.0 }).unwrap();
} }
pub fn size(&self) -> Option<(i32, i32)> { pub fn properties(&self) -> WindowProperties {
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<String> {
let RequestResponse::WindowProps { class, .. } =
request(Request::GetWindowProps { window_id: self.0 })
else {
unreachable!()
};
class
}
pub fn title(&self) -> Option<String> {
let RequestResponse::WindowProps { title, .. } =
request(Request::GetWindowProps { window_id: self.0 })
else {
unreachable!()
};
title
}
pub fn floating(&self) -> Option<bool> {
let RequestResponse::WindowProps { floating, .. } =
request(Request::GetWindowProps { window_id: self.0 })
else {
unreachable!()
};
floating
}
pub fn maximized(&self) -> Option<bool> {
let RequestResponse::WindowProps { let RequestResponse::WindowProps {
size,
loc,
class,
title,
focused,
floating,
fullscreen_or_maximized, fullscreen_or_maximized,
..
} = request(Request::GetWindowProps { window_id: self.0 }) } = request(Request::GetWindowProps { window_id: self.0 })
else { else {
unreachable!() unreachable!()
}; };
fullscreen_or_maximized.map(|fullscreen_or_maximized| { WindowProperties {
matches!(fullscreen_or_maximized, FullscreenOrMaximized::Maximized) size,
}) loc,
} class,
title,
pub fn fullscreen(&self) -> Option<bool> { focused,
let RequestResponse::WindowProps { floating,
fullscreen_or_maximized, 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<bool> {
let RequestResponse::WindowProps { focused, .. } =
request(Request::GetWindowProps { window_id: self.0 })
else {
unreachable!()
};
focused
} }
} }