mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-11-20 15:41:38 +01:00
New crate crates/komorebi-backend (edition 2024, root workspace member, depends only on komorebi-layouts for Rect). - WindowId and MonitorId: opaque u64 newtypes. KDE and macOS ids convert with from_u32/to_u32; a Windows HWND/HMONITOR goes through from_hwnd/to_hwnd, which keep the bits of the isize unchanged (isize as usize as u64), so no handle is lost whether or not it fits in 32 bits (the e2e samples stay below 2^32, which is an observation, not a guarantee). - WindowInfo and MonitorInfo: plain data; only id, pid, rect, minimized and alive are required, the rest (title, class, exe, path, fullscreen, maximized, normal; name and serial id of a monitor) are Option (seam.md consequence 6). - Error: WindowGone(id) (finding 9: macOS uses a failed write as its signal that a window died), Unsupported(Capability) and Platform(cause). - Backend: only the operations of seam.md section 2.1 that at least two platforms have (5 queries, 9 commands), a Capability query for workspaces, maximize, opacity and focus-follows-mouse (their methods default to Unsupported), begin_batch/end_batch with a no-op default plus the Batch guard (finding 8), the documented set_rect contract (a request; reads after a write return what the platform defines, consequence 2 as corrected) and the thread contract (any non-main thread, no waiting for the platform's main thread: consequences 3 and 4, written as TARGET rules). Every doc comment says which platforms implement the method. Verified: native cargo test -p komorebi-backend (2 unit tests, 3 doctests, 0 failed) and cargo clippy -p komorebi-backend --all-targets -- -D warnings clean. The Windows/KDE/macOS gates are not affected yet (new crate, nothing depends on it).
129 lines
4.0 KiB
Rust
129 lines
4.0 KiB
Rust
use std::fmt;
|
|
|
|
/// The name a backend gives a window, which is how every other part of komorebi refers to it.
|
|
///
|
|
/// It is an opaque 64-bit number. What it is made from is the backend's own business, and the
|
|
/// conversions below are the only ones to use:
|
|
///
|
|
/// - **KDE**: a counter the daemon assigns to each KWin window UUID (a `u32`):
|
|
/// [`WindowId::from_u32`], [`WindowId::to_u32`].
|
|
/// - **macOS**: the `CGWindowID` (a `u32`), same conversions.
|
|
/// - **Windows**: the `HWND`, a pointer-sized integer (`isize`). [`WindowId::from_hwnd`] keeps
|
|
/// the bits of the handle unchanged, so it never loses one and [`WindowId::to_hwnd`] gives the
|
|
/// same `isize` back, whether or not a handle fits in 32 bits (the e2e samples in
|
|
/// `docs/dev/seam.md` section 4.1 stayed below 2^32, but that is an observation, not a
|
|
/// guarantee).
|
|
///
|
|
/// # Example
|
|
///
|
|
/// ```rust
|
|
/// use komorebi_backend::WindowId;
|
|
///
|
|
/// let id = WindowId::from_u32(7);
|
|
/// assert_eq!(id.to_u32(), Some(7));
|
|
/// assert_eq!(id.to_string(), "7");
|
|
///
|
|
/// // a handle with the top bit set, as a 64-bit HWND may have
|
|
/// let hwnd = -123_isize;
|
|
/// assert_eq!(WindowId::from_hwnd(hwnd).to_hwnd(), hwnd);
|
|
/// assert_eq!(WindowId::from_hwnd(hwnd).to_u32(), None);
|
|
/// ```
|
|
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
|
pub struct WindowId(pub u64);
|
|
|
|
impl WindowId {
|
|
/// The id of a KDE or macOS window.
|
|
pub const fn from_u32(id: u32) -> Self {
|
|
Self(id as u64)
|
|
}
|
|
|
|
/// The id as a `u32`, or `None` if it does not fit (which no KDE or macOS id can be).
|
|
pub fn to_u32(self) -> Option<u32> {
|
|
u32::try_from(self.0).ok()
|
|
}
|
|
|
|
/// The id of a Windows window: the bits of the `HWND`, unchanged.
|
|
pub const fn from_hwnd(hwnd: isize) -> Self {
|
|
Self(hwnd as usize as u64)
|
|
}
|
|
|
|
/// The `HWND` this id was made from by [`WindowId::from_hwnd`].
|
|
pub const fn to_hwnd(self) -> isize {
|
|
self.0 as usize as isize
|
|
}
|
|
}
|
|
|
|
impl From<u32> for WindowId {
|
|
fn from(id: u32) -> Self {
|
|
Self::from_u32(id)
|
|
}
|
|
}
|
|
|
|
impl fmt::Display for WindowId {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
self.0.fmt(f)
|
|
}
|
|
}
|
|
|
|
/// The name a backend gives a monitor.
|
|
///
|
|
/// An opaque 64-bit number, like [`WindowId`]: KDE's output id and macOS's `CGDirectDisplayID`
|
|
/// are `u32` ([`MonitorId::from_u32`]), a Windows monitor handle (`HMONITOR`, an `isize`) goes
|
|
/// through [`MonitorId::from_hmonitor`] and back through [`MonitorId::to_hmonitor`], bit for bit.
|
|
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
|
pub struct MonitorId(pub u64);
|
|
|
|
impl MonitorId {
|
|
/// The id of a KDE or macOS monitor.
|
|
pub const fn from_u32(id: u32) -> Self {
|
|
Self(id as u64)
|
|
}
|
|
|
|
/// The id as a `u32`, or `None` if it does not fit.
|
|
pub fn to_u32(self) -> Option<u32> {
|
|
u32::try_from(self.0).ok()
|
|
}
|
|
|
|
/// The id of a Windows monitor: the bits of the handle, unchanged.
|
|
pub const fn from_hmonitor(handle: isize) -> Self {
|
|
Self(handle as usize as u64)
|
|
}
|
|
|
|
/// The handle this id was made from by [`MonitorId::from_hmonitor`].
|
|
pub const fn to_hmonitor(self) -> isize {
|
|
self.0 as usize as isize
|
|
}
|
|
}
|
|
|
|
impl From<u32> for MonitorId {
|
|
fn from(id: u32) -> Self {
|
|
Self::from_u32(id)
|
|
}
|
|
}
|
|
|
|
impl fmt::Display for MonitorId {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
self.0.fmt(f)
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn a_hwnd_survives_the_round_trip_with_any_bits() {
|
|
for hwnd in [0, 1, 0x1_0000_0000, isize::MAX, isize::MIN, -1] {
|
|
assert_eq!(WindowId::from_hwnd(hwnd).to_hwnd(), hwnd);
|
|
assert_eq!(MonitorId::from_hmonitor(hwnd).to_hmonitor(), hwnd);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_u32_id_round_trips_and_a_wide_one_does_not_fit() {
|
|
assert_eq!(WindowId::from_u32(u32::MAX).to_u32(), Some(u32::MAX));
|
|
assert_eq!(WindowId(u64::from(u32::MAX) + 1).to_u32(), None);
|
|
assert_eq!(MonitorId::from(3).to_u32(), Some(3));
|
|
}
|
|
}
|