window-enumerator 0.4.1

A Rust library for enumerating and filtering windows with multiple criteria including sorting and selection
Documentation
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
use std::os::windows::ffi::OsStringExt;
use windows::core::*;
use windows::Win32::Foundation::*;
use windows::Win32::System::ProcessStatus::*;
use windows::Win32::System::Threading::*;
use windows::Win32::UI::WindowsAndMessaging::*;

use crate::errors::{Result, WindowError};
use crate::types::{FilterCriteria, WindowInfo, WindowPosition};
use crate::utils;

#[cfg(feature = "selection")]
use crate::types::Selection;

#[cfg(feature = "sorting")]
use crate::types::SortCriteria;

#[cfg(feature = "sorting")]
use crate::models::WindowSorter;

/// The main window enumeration and inspection interface.
///
/// This struct provides methods to discover, filter, and sort Windows windows
/// with various criteria. It serves as the primary entry point for the library.
pub struct WindowEnumerator {
    windows: Vec<WindowInfo>,
}

impl WindowEnumerator {
    /// Creates a new window enumerator.
    ///
    /// The enumerator starts with no windows loaded. Call [`enumerate_all_windows`]
    /// to populate it with the current system windows.
    ///
    /// # Examples
    ///
    /// ```
    /// use window_enumerator::WindowEnumerator;
    ///
    /// let enumerator = WindowEnumerator::new();
    /// ```
    ///
    /// [`enumerate_all_windows`]: WindowEnumerator::enumerate_all_windows
    pub fn new() -> Self {
        Self {
            windows: Vec::new(),
        }
    }

    /// Enumerates all visible windows on the system.
    ///
    /// This method populates the internal window list with all currently
    /// visible, non-child windows. Each window is assigned a 1-based index.
    ///
    /// # Errors
    ///
    /// Returns [`WindowError::WindowsApiError`] if the Windows API call fails.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use window_enumerator::WindowEnumerator;
    ///
    /// let mut enumerator = WindowEnumerator::new();
    /// enumerator.enumerate_all_windows().unwrap();
    /// ```
    pub fn enumerate_all_windows(&mut self) -> Result<()> {
        self.windows.clear();

        unsafe {
            EnumWindows(
                Some(Self::enum_windows_proc),
                LPARAM(self as *mut _ as isize),
            )
            .map_err(|e| Error::new(e.code(), "Failed to enumerate windows".into()))?;
        }

        // Assign 1-based indices to each window
        for (index, window) in self.windows.iter_mut().enumerate() {
            window.index = index + 1;
        }

        Ok(())
    }

    /// Windows enumeration callback function.
    unsafe extern "system" fn enum_windows_proc(hwnd: HWND, lparam: LPARAM) -> BOOL {
        let enumerator = &mut *(lparam.0 as *mut WindowEnumerator);

        // Skip invisible windows and child windows
        if IsWindowVisible(hwnd).as_bool() && GetParent(hwnd).0 == 0 {
            if let Ok(mut window_info) = enumerator.get_window_info(hwnd) {
                // Temporary index, will be reassigned later
                window_info.index = enumerator.windows.len() + 1;
                enumerator.windows.push(window_info);
            }
        }

        BOOL::from(true) // Continue enumeration
    }

    /// Gathers information about a specific window.
    fn get_window_info(&self, hwnd: HWND) -> Result<WindowInfo> {
        unsafe {
            // Get window title
            let title = Self::get_window_text(hwnd);

            // Get window class name
            let class_name = Self::get_class_name(hwnd);

            // Get process ID
            let pid = Self::get_process_id(hwnd);

            // Get process information
            let (process_name, process_file) = if pid > 0 {
                Self::get_process_info(pid).unwrap_or_default()
            } else {
                (String::new(), std::path::PathBuf::new())
            };

            // Get window position and size
            let position = Self::get_window_position(hwnd);

            Ok(WindowInfo {
                hwnd: hwnd.0,
                pid,
                title,
                class_name,
                process_name,
                process_file,
                position,
                index: 0, // Temporary value, will be set later
            })
        }
    }

    /// Retrieves the text of a window.
    unsafe fn get_window_text(hwnd: HWND) -> String {
        let mut buffer = [0u16; 256];
        let len = GetWindowTextW(hwnd, &mut buffer);
        if len > 0 {
            std::ffi::OsString::from_wide(&buffer[..len as usize])
                .to_string_lossy()
                .into_owned()
        } else {
            String::new()
        }
    }

    /// Retrieves the class name of a window.
    unsafe fn get_class_name(hwnd: HWND) -> String {
        let mut buffer = [0u16; 256];
        let len = GetClassNameW(hwnd, &mut buffer);
        if len > 0 {
            std::ffi::OsString::from_wide(&buffer[..len as usize])
                .to_string_lossy()
                .into_owned()
        } else {
            String::new()
        }
    }

    /// Retrieves the process ID of a window.
    unsafe fn get_process_id(hwnd: HWND) -> u32 {
        let mut pid: u32 = 0;
        GetWindowThreadProcessId(hwnd, Some(&mut pid));
        pid
    }

    /// Retrieves the position and dimensions of a window.
    unsafe fn get_window_position(hwnd: HWND) -> WindowPosition {
        let mut rect = RECT::default();
        if GetWindowRect(hwnd, &mut rect).is_ok() {
            WindowPosition {
                x: rect.left,
                y: rect.top,
                width: rect.right - rect.left,
                height: rect.bottom - rect.top,
            }
        } else {
            WindowPosition::default()
        }
    }

    /// Retrieves process information for a given process ID.
    unsafe fn get_process_info(pid: u32) -> Result<(String, std::path::PathBuf)> {
        let process_handle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, pid)?;

        let mut file_buffer = [0u16; MAX_PATH as usize];
        let len = GetProcessImageFileNameW(process_handle, &mut file_buffer);

        if len > 0 {
            let full_path = std::ffi::OsString::from_wide(&file_buffer[..len as usize]);
            let path_buf = std::path::PathBuf::from(&full_path);

            // Extract just the filename
            let process_name = path_buf
                .file_name()
                .map(|name| name.to_string_lossy().into_owned())
                .unwrap_or_default();

            CloseHandle(process_handle).ok();
            Ok((process_name, path_buf))
        } else {
            CloseHandle(process_handle).ok();
            // 使用标准库的方法获取错误代码
            let last_error = std::io::Error::last_os_error();
            Err(WindowError::WindowsApiError(
                last_error.raw_os_error().unwrap_or(0) as u32,
            ))
        }
    }

    /// Finds windows by title containing the specified string (case-insensitive).
    ///
    /// This is a convenience method for simple title-based filtering.
    ///
    /// # Arguments
    ///
    /// * `title_substring` - The substring to search for in window titles
    ///
    /// # Returns
    ///
    /// A vector containing windows whose titles contain the specified substring.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use window_enumerator::WindowEnumerator;
    ///
    /// let mut enumerator = WindowEnumerator::new();
    /// enumerator.enumerate_all_windows().unwrap();
    ///
    /// let chrome_windows = enumerator.find_by_title("Chrome");
    /// for window in chrome_windows {
    ///     window.print_compact();
    /// }
    /// ```
    pub fn find_by_title(&self, title_substring: &str) -> Vec<WindowInfo> {
        let criteria = FilterCriteria {
            title_contains: Some(title_substring.to_string()),
            ..Default::default()
        };
        self.filter_windows(&criteria)
    }

    /// Filters windows based on the specified criteria.
    ///
    /// # Arguments
    ///
    /// * `criteria` - The filter criteria to apply
    ///
    /// # Returns
    ///
    /// A vector containing only the windows that match all criteria.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use window_enumerator::{WindowEnumerator, FilterCriteria};
    ///
    /// let mut enumerator = WindowEnumerator::new();
    /// enumerator.enumerate_all_windows().unwrap();
    ///
    /// let criteria = FilterCriteria {
    ///     title_contains: Some("Chrome".to_string()),
    ///     ..Default::default()
    /// };
    /// let chrome_windows = enumerator.filter_windows(&criteria);
    /// ```
    pub fn filter_windows(&self, criteria: &FilterCriteria) -> Vec<WindowInfo> {
        self.windows
            .iter()
            .filter(|window| utils::matches_criteria(window, criteria))
            .cloned()
            .collect()
    }

    /// Filters and sorts windows based on the specified criteria.
    ///
    /// Requires the `sorting` feature.
    ///
    /// # Arguments
    ///
    /// * `criteria` - The filter criteria to apply
    /// * `sort_criteria` - The sort criteria to apply
    ///
    /// # Returns
    ///
    /// A vector containing the filtered and sorted windows.
    #[cfg(feature = "sorting")]
    pub fn filter_and_sort_windows(
        &self,
        criteria: &FilterCriteria,
        sort_criteria: &SortCriteria,
    ) -> Vec<WindowInfo> {
        WindowSorter::filter_and_sort_windows(&self.windows, criteria, sort_criteria)
    }

    /// Filters windows with selection criteria.
    ///
    /// Requires the `selection` feature.
    ///
    /// # Arguments
    ///
    /// * `criteria` - The filter criteria to apply
    /// * `selection` - The selection criteria to apply
    ///
    /// # Returns
    ///
    /// A vector containing the selected windows that match the filter criteria.
    #[cfg(feature = "selection")]
    pub fn filter_windows_with_selection(
        &self,
        criteria: &FilterCriteria,
        selection: &Selection,
    ) -> Vec<WindowInfo> {
        let filtered = self.filter_windows(criteria);

        match selection {
            Selection::All => filtered,
            Selection::Indices(indices) => filtered
                .into_iter()
                .filter(|window| indices.contains(&window.index))
                .collect(),
        }
    }

    /// Filters, sorts, and selects windows based on the specified criteria.
    ///
    /// Requires both `sorting` and `selection` features.
    ///
    /// # Arguments
    ///
    /// * `criteria` - The filter criteria to apply
    /// * `sort_criteria` - The sort criteria to apply
    /// * `selection` - The selection criteria to apply
    ///
    /// # Returns
    ///
    /// A vector containing the filtered, sorted, and selected windows.
    #[cfg(all(feature = "sorting", feature = "selection"))]
    pub fn filter_sort_windows_with_selection(
        &self,
        criteria: &FilterCriteria,
        sort_criteria: &SortCriteria,
        selection: &Selection,
    ) -> Vec<WindowInfo> {
        let filtered =
            WindowSorter::filter_and_sort_windows(&self.windows, criteria, sort_criteria);

        match selection {
            Selection::All => filtered,
            Selection::Indices(indices) => filtered
                .into_iter()
                .filter(|window| indices.contains(&window.index))
                .collect(),
        }
    }

    /// Returns a reference to all enumerated windows.
    ///
    /// # Returns
    ///
    /// A slice containing all windows that were enumerated.
    pub fn get_windows(&self) -> &[WindowInfo] {
        &self.windows
    }

    /// Retrieves a window by its 1-based index.
    ///
    /// # Arguments
    ///
    /// * `index` - The 1-based index of the window to retrieve
    ///
    /// # Returns
    ///
    /// `Some(&WindowInfo)` if a window with the given index exists, `None` otherwise.
    pub fn get_window_by_index(&self, index: usize) -> Option<&WindowInfo> {
        self.windows.iter().find(|w| w.index == index)
    }

    /// Prints all enumerated windows with their indices in a formatted table.
    ///
    /// This is useful for debugging and for users to see available windows
    /// before making selections.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use window_enumerator::WindowEnumerator;
    ///
    /// let mut enumerator = WindowEnumerator::new();
    /// enumerator.enumerate_all_windows().unwrap();
    /// enumerator.print_windows_with_indices();
    /// ```
    pub fn print_windows_with_indices(&self) {
        println!("Index | Handle      | PID    | Position    | Title");
        println!("------|-------------|--------|-------------|-------------------");
        for window in &self.windows {
            println!(
                "{:5} | 0x{:08x} | {:6} | {:4},{:4}     | {}",
                window.index,
                window.hwnd,
                window.pid,
                window.position.x,
                window.position.y,
                window.title
            );
        }
    }
}

impl Default for WindowEnumerator {
    fn default() -> Self {
        Self::new()
    }
}