1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use crate::types::WindowInfo;
#[cfg(feature = "sorting")]
use crate::types::{PositionSort, SortCriteria};
#[cfg(feature = "sorting")]
use crate::utils::matches_criteria;
/// Extension methods for [`WindowInfo`] providing display and validation functionality.
impl WindowInfo {
/// Prints detailed information about the window to stdout.
///
/// # Examples
/// ```
/// # use window_enumerator::WindowInfo;
/// # use window_enumerator::WindowPosition;
/// # let window = WindowInfo {
/// # hwnd: 12345,
/// # pid: 1234,
/// # title: "Test".to_string(),
/// # class_name: "TestClass".to_string(),
/// # process_name: "test.exe".to_string(),
/// # process_file: std::path::PathBuf::from("test.exe"),
/// # index: 1,
/// # position: WindowPosition::default(),
/// # };
/// window.print();
/// ```
pub fn print(&self) {
println!("Index: {}", self.index);
println!("Window Handle: 0x{:x}", self.hwnd);
println!("Process ID: {}", self.pid);
println!("Title: {}", self.title);
println!("Class Name: {}", self.class_name);
println!("Process Name: {}", self.process_name);
println!("Process File: {}", self.process_file.display());
println!(
"Position: ({}, {}) Size: {}x{}",
self.position.x, self.position.y, self.position.width, self.position.height
);
println!("----------------------------------------");
}
/// Prints compact window information to stdout.
///
/// # Examples
/// ```
/// # use window_enumerator::WindowInfo;
/// # use window_enumerator::WindowPosition;
/// # let window = WindowInfo {
/// # hwnd: 12345,
/// # pid: 1234,
/// # title: "Test".to_string(),
/// # class_name: "TestClass".to_string(),
/// # process_name: "test.exe".to_string(),
/// # process_file: std::path::PathBuf::from("test.exe"),
/// # index: 1,
/// # position: WindowPosition::default(),
/// # };
/// window.print_compact();
/// ```
pub fn print_compact(&self) {
println!(
"[{}] 0x{:x} (PID: {}) @ ({},{}) - {}",
self.index, self.hwnd, self.pid, self.position.x, self.position.y, self.title
);
}
/// Checks if the window handle is still valid.
///
/// This verifies that the window still exists in the system.
///
/// # Examples
/// ```
/// # use window_enumerator::WindowInfo;
/// # use window_enumerator::WindowPosition;
/// # let window = WindowInfo {
/// # hwnd: 12345,
/// # pid: 1234,
/// # title: "Test".to_string(),
/// # class_name: "TestClass".to_string(),
/// # process_name: "test.exe".to_string(),
/// # process_file: std::path::PathBuf::from("test.exe"),
/// # index: 1,
/// # position: WindowPosition::default(),
/// # };
/// let is_valid = window.is_valid();
/// ```
#[cfg(feature = "windows")]
pub fn is_valid(&self) -> bool {
use windows::Win32::Foundation::*;
use windows::Win32::UI::WindowsAndMessaging::*;
unsafe { IsWindow(HWND(self.hwnd)).as_bool() }
}
}
/// Provides window sorting functionality.
#[cfg(feature = "sorting")]
pub struct WindowSorter;
#[cfg(feature = "sorting")]
impl WindowSorter {
/// Sorts a vector of windows according to the specified criteria.
///
/// # Arguments
///
/// * `windows` - The windows to sort (modified in-place)
/// * `sort_criteria` - The criteria to use for sorting
pub fn sort_windows(windows: &mut [WindowInfo], sort_criteria: &SortCriteria) {
// ← 修改参数类型为切片
if sort_criteria.pid == 0 && sort_criteria.title == 0 && sort_criteria.position.is_none() {
return; // No sorting criteria
}
windows.sort_by(|a, b| {
let mut ordering = std::cmp::Ordering::Equal;
// PID sorting
if sort_criteria.pid != 0 {
ordering = a.pid.cmp(&b.pid);
if sort_criteria.pid < 0 {
ordering = ordering.reverse();
}
if ordering != std::cmp::Ordering::Equal {
return ordering;
}
}
// Title sorting
if sort_criteria.title != 0 {
ordering = a.title.to_lowercase().cmp(&b.title.to_lowercase());
if sort_criteria.title < 0 {
ordering = ordering.reverse();
}
if ordering != std::cmp::Ordering::Equal {
return ordering;
}
}
// Position sorting
if let Some(ref position_sort) = sort_criteria.position {
ordering = Self::compare_positions(a, b, position_sort);
if ordering != std::cmp::Ordering::Equal {
return ordering;
}
}
ordering
});
}
/// Compares two windows based on position sorting criteria.
fn compare_positions(
a: &WindowInfo,
b: &WindowInfo,
position_sort: &PositionSort,
) -> std::cmp::Ordering {
match position_sort {
PositionSort::X(order) => {
let ordering = a.position.x.cmp(&b.position.x);
if *order < 0 {
ordering.reverse()
} else {
ordering
}
}
PositionSort::Y(order) => {
let ordering = a.position.y.cmp(&b.position.y);
if *order < 0 {
ordering.reverse()
} else {
ordering
}
}
PositionSort::XY(x_order, y_order) => {
// Sort by X first
let x_ordering = a.position.x.cmp(&b.position.x);
if x_ordering != std::cmp::Ordering::Equal {
return if *x_order < 0 {
x_ordering.reverse()
} else {
x_ordering
};
}
// If X is equal, sort by Y
let y_ordering = a.position.y.cmp(&b.position.y);
if *y_order < 0 {
y_ordering.reverse()
} else {
y_ordering
}
}
}
}
/// Filters and sorts windows according to the specified criteria.
///
/// # Arguments
///
/// * `windows` - The windows to filter and sort
/// * `criteria` - The filter criteria
/// * `sort_criteria` - The sort criteria
///
/// # Returns
///
/// A new vector containing the filtered and sorted windows.
pub fn filter_and_sort_windows(
windows: &[WindowInfo],
criteria: &crate::types::FilterCriteria,
sort_criteria: &SortCriteria,
) -> Vec<WindowInfo> {
let mut filtered: Vec<WindowInfo> = windows
.iter()
.filter(|window| matches_criteria(window, criteria))
.cloned()
.collect();
Self::sort_windows(&mut filtered, sort_criteria);
filtered
}
}