mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-12-03 05:34:32 +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).
81 lines
2.7 KiB
Rust
81 lines
2.7 KiB
Rust
use crate::backend::Capability;
|
|
use crate::id::WindowId;
|
|
use std::fmt;
|
|
|
|
/// What a [`crate::Backend`] call can fail with.
|
|
pub type Result<T> = std::result::Result<T, Error>;
|
|
|
|
/// Why a backend call failed.
|
|
///
|
|
/// Every platform has its own errors (`eyre` reports on KDE, Win32 `HRESULT`s, macOS
|
|
/// `AccessibilityError`); this is what the shared code can act on. In particular it can tell
|
|
/// "the window is gone", which macOS uses as its only signal that a window has died (a failed
|
|
/// write asks the reaper whether the window still exists, `docs/dev/seam.md` finding 9), from
|
|
/// every other failure.
|
|
///
|
|
/// # Example
|
|
///
|
|
/// ```rust
|
|
/// use komorebi_backend::{Error, WindowId};
|
|
///
|
|
/// let error = Error::WindowGone(WindowId::from_u32(9));
|
|
/// assert!(error.is_window_gone());
|
|
/// assert_eq!(error.to_string(), "no such window: 9");
|
|
/// ```
|
|
#[derive(Debug)]
|
|
#[non_exhaustive]
|
|
pub enum Error {
|
|
/// The window is not known to the backend, or has been destroyed. Every window command may
|
|
/// fail with this, and (see [`crate::Backend::set_rect`]) with nothing else that the caller
|
|
/// should treat as a property of the request.
|
|
WindowGone(WindowId),
|
|
/// The platform does not have this operation: the [`Capability`] is not
|
|
/// [supported](crate::Backend::supports).
|
|
Unsupported(Capability),
|
|
/// The platform refused or failed, with its own error as the cause.
|
|
Platform(Box<dyn std::error::Error + Send + Sync>),
|
|
}
|
|
|
|
impl Error {
|
|
/// Returns true if and only if this is [`Error::WindowGone`].
|
|
pub fn is_window_gone(&self) -> bool {
|
|
matches!(self, Self::WindowGone(_))
|
|
}
|
|
|
|
/// Returns true if and only if this is [`Error::Unsupported`].
|
|
pub fn is_unsupported(&self) -> bool {
|
|
matches!(self, Self::Unsupported(_))
|
|
}
|
|
|
|
/// A [`Error::Platform`] failure that is only a message.
|
|
pub fn platform(message: impl Into<String>) -> Self {
|
|
Self::Platform(message.into().into())
|
|
}
|
|
|
|
/// A [`Error::Platform`] failure with the platform's own error as the cause.
|
|
pub fn from_platform(cause: impl std::error::Error + Send + Sync + 'static) -> Self {
|
|
Self::Platform(Box::new(cause))
|
|
}
|
|
}
|
|
|
|
impl fmt::Display for Error {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
match self {
|
|
Self::WindowGone(id) => write!(f, "no such window: {id}"),
|
|
Self::Unsupported(capability) => {
|
|
write!(f, "{capability:?} is not supported on this platform")
|
|
}
|
|
Self::Platform(cause) => cause.fmt(f),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl std::error::Error for Error {
|
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
|
match self {
|
|
Self::Platform(cause) => Some(cause.as_ref()),
|
|
_ => None,
|
|
}
|
|
}
|
|
}
|