-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathwindow_exclusion.rs
More file actions
111 lines (98 loc) · 3.31 KB
/
window_exclusion.rs
File metadata and controls
111 lines (98 loc) · 3.31 KB
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
#[cfg(any(target_os = "macos", target_os = "linux"))]
use scap_targets::{Window, WindowId};
use serde::{Deserialize, Serialize};
use specta::Type;
#[derive(Debug, Clone, Serialize, Deserialize, Type, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct WindowExclusion {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub bundle_identifier: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub owner_name: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub window_title: Option<String>,
}
impl WindowExclusion {
pub fn matches(
&self,
bundle_identifier: Option<&str>,
owner_name: Option<&str>,
window_title: Option<&str>,
) -> bool {
if let Some(identifier) = self.bundle_identifier.as_deref()
&& bundle_identifier
.map(|candidate| candidate == identifier)
.unwrap_or(false)
{
return true;
}
if let Some(expected_owner) = self.owner_name.as_deref() {
let owner_matches = owner_name
.map(|candidate| candidate == expected_owner)
.unwrap_or(false);
if self.window_title.is_some() {
return owner_matches
&& self
.window_title
.as_deref()
.map(|expected_title| {
window_title
.map(|candidate| candidate == expected_title)
.unwrap_or(false)
})
.unwrap_or(false);
}
if owner_matches {
return true;
}
}
if let Some(expected_title) = self.window_title.as_deref() {
return window_title
.map(|candidate| candidate == expected_title)
.unwrap_or(false);
}
false
}
}
#[cfg(target_os = "macos")]
pub fn resolve_window_ids(exclusions: &[WindowExclusion]) -> Vec<WindowId> {
if exclusions.is_empty() {
return Vec::new();
}
Window::list()
.into_iter()
.filter_map(|window| {
let owner_name = window.owner_name();
let window_title = window.name();
let bundle_identifier = window.raw_handle().bundle_identifier();
exclusions
.iter()
.find(|entry| {
entry.matches(
bundle_identifier.as_deref(),
owner_name.as_deref(),
window_title.as_deref(),
)
})
.map(|_| window.id())
})
.collect()
}
#[cfg(target_os = "linux")]
#[allow(dead_code)]
pub fn resolve_window_ids(exclusions: &[WindowExclusion]) -> Vec<WindowId> {
if exclusions.is_empty() {
return Vec::new();
}
Window::list()
.into_iter()
.filter_map(|window| {
let owner_name = window.owner_name();
let window_title = window.name();
exclusions
.iter()
.find(|entry| entry.matches(None, owner_name.as_deref(), window_title.as_deref()))
.map(|_| window.id())
})
.collect()
}