teksilo_core/window/id.rs
1// SPDX-License-Identifier: MPL-2.0
2// SPDX-FileCopyrightText: 2026 FernTech
3
4//! Opaque per-window identifier.
5
6use std::fmt;
7
8/// Opaque identifier for an application window.
9///
10/// Allocated by the app-level window manager when a window is created
11/// and passed back to the app via [`WindowState::id`](super::state::WindowState::id)
12/// or the return value of window-opening APIs. IDs are `Copy`, unique
13/// within a process, and never reused after a window closes.
14///
15/// `Serialize`/`Deserialize` so it can appear inside a persisted type
16/// (e.g. `teksilo_widgets::toast::ToastRoute::Window` mirrored into an
17/// archived `NotificationEntry`) — but note the id is NOT stable across
18/// process restarts, only "unique within a process": a persisted
19/// window-scoped route from a previous run will not match any window
20/// in the current one, which is the intended, accepted behaviour for
21/// something that was inherently about a transient session's window.
22#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
23pub struct TeksiloWindowId(u64);
24
25impl TeksiloWindowId {
26 /// Construct an id from a raw `u64`. Intended for the window
27 /// manager's allocator — application code should not fabricate ids.
28 pub fn new(id: u64) -> Self {
29 Self(id)
30 }
31
32 /// The raw numeric id (for serialization or debugging).
33 pub fn raw(&self) -> u64 {
34 self.0
35 }
36}
37
38impl fmt::Display for TeksiloWindowId {
39 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40 write!(f, "Window({})", self.0)
41 }
42}
43
44#[cfg(test)]
45mod tests {
46 use super::*;
47
48 #[test]
49 fn equality_is_by_value() {
50 let a = TeksiloWindowId::new(1);
51 let b = TeksiloWindowId::new(1);
52 let c = TeksiloWindowId::new(2);
53 assert_eq!(a, b);
54 assert_ne!(a, c);
55 }
56
57 #[test]
58 fn display_format() {
59 assert_eq!(format!("{}", TeksiloWindowId::new(7)), "Window(7)");
60 }
61}