window_enumerator_formatter/
models.rs

1use serde::Serialize;
2use std::path::PathBuf;
3
4/// Window position and size information.
5#[derive(Debug, Clone, Serialize, Default)]
6pub struct WindowPosition {
7    /// X coordinate of the window.
8    pub x: i32,
9    /// Y coordinate of the window.
10    pub y: i32,
11    /// Width of the window.
12    pub width: i32,
13    /// Height of the window.
14    pub height: i32,
15}
16
17/// Information about a window.
18#[derive(Debug, Clone, Serialize)]
19pub struct WindowInfo {
20    /// Window handle.
21    pub hwnd: isize,
22    /// Process ID.
23    pub pid: u32,
24    /// Window title.
25    pub title: String,
26    /// Window class name.
27    pub class_name: String,
28    /// Process name.
29    pub process_name: String,
30    /// Process file path.
31    pub process_file: PathBuf,
32    /// Index in the enumeration.
33    pub index: usize,
34    /// Window position and size.
35    pub position: WindowPosition,
36}
37
38impl WindowInfo {
39    /// Create a new WindowInfo instance using builder pattern.
40    pub fn builder() -> WindowInfoBuilder {
41        WindowInfoBuilder::default()
42    }
43
44    // format 和 format_with 方法已移到 lib.rs 中
45}
46
47/// Builder for WindowInfo to avoid too many arguments.
48#[derive(Default)]
49pub struct WindowInfoBuilder {
50    hwnd: Option<isize>,
51    pid: Option<u32>,
52    title: Option<String>,
53    class_name: Option<String>,
54    process_name: Option<String>,
55    process_file: Option<PathBuf>,
56    index: Option<usize>,
57    position: Option<WindowPosition>,
58}
59
60impl WindowInfoBuilder {
61    /// Set the window handle.
62    pub fn hwnd(mut self, hwnd: isize) -> Self {
63        self.hwnd = Some(hwnd);
64        self
65    }
66
67    /// Set the process ID.
68    pub fn pid(mut self, pid: u32) -> Self {
69        self.pid = Some(pid);
70        self
71    }
72
73    /// Set the window title.
74    pub fn title(mut self, title: String) -> Self {
75        self.title = Some(title);
76        self
77    }
78
79    /// Set the window class name.
80    pub fn class_name(mut self, class_name: String) -> Self {
81        self.class_name = Some(class_name);
82        self
83    }
84
85    /// Set the process name.
86    pub fn process_name(mut self, process_name: String) -> Self {
87        self.process_name = Some(process_name);
88        self
89    }
90
91    /// Set the process file path.
92    pub fn process_file(mut self, process_file: PathBuf) -> Self {
93        self.process_file = Some(process_file);
94        self
95    }
96
97    /// Set the index.
98    pub fn index(mut self, index: usize) -> Self {
99        self.index = Some(index);
100        self
101    }
102
103    /// Set the window position.
104    pub fn position(mut self, position: WindowPosition) -> Self {
105        self.position = Some(position);
106        self
107    }
108
109    /// Build the WindowInfo instance.
110    pub fn build(self) -> WindowInfo {
111        WindowInfo {
112            hwnd: self.hwnd.unwrap_or(0),
113            pid: self.pid.unwrap_or(0),
114            title: self.title.unwrap_or_default(),
115            class_name: self.class_name.unwrap_or_default(),
116            process_name: self.process_name.unwrap_or_default(),
117            process_file: self.process_file.unwrap_or_default(),
118            index: self.index.unwrap_or(0),
119            position: self.position.unwrap_or_default(),
120        }
121    }
122}
123
124/// Conversion from window-enumerator types
125#[cfg(feature = "window-enumerator")]
126impl From<&window_enumerator::WindowInfo> for WindowInfo {
127    fn from(window: &window_enumerator::WindowInfo) -> Self {
128        Self::builder()
129            .hwnd(window.hwnd)
130            .pid(window.pid)
131            .title(window.title.clone())
132            .class_name(window.class_name.clone())
133            .process_name(window.process_name.clone())
134            .process_file(window.process_file.clone())
135            .index(window.index)
136            .position(WindowPosition {
137                x: window.position.x,
138                y: window.position.y,
139                width: window.position.width,
140                height: window.position.height,
141            })
142            .build()
143    }
144}
145
146#[cfg(feature = "window-enumerator")]
147impl From<window_enumerator::WindowInfo> for WindowInfo {
148    fn from(window: window_enumerator::WindowInfo) -> Self {
149        Self::from(&window)
150    }
151}