@@ -14,11 +14,20 @@ interface Remote {
14
14
} ;
15
15
}
16
16
17
+ interface MacPermissions {
18
+ screenProtection : {
19
+ checkSupport : ( ) => Promise < { supported : boolean ; platform ?: string ; reason ?: string ; error ?: string } > ;
20
+ set : ( enabled : boolean ) => Promise < { success : boolean ; enabled ?: boolean ; error ?: string } > ;
21
+ getStatus : ( ) => Promise < { status : string ; reason ?: string ; error ?: string } > ;
22
+ } ;
23
+ }
24
+
17
25
declare global {
18
26
interface Window {
19
27
electronAPI ?: ElectronAPI ;
20
28
remote ?: Remote ;
21
29
electron ?: any ;
30
+ macPermissions ?: MacPermissions ;
22
31
}
23
32
}
24
33
@@ -39,11 +48,19 @@ export function useScreenProtection() {
39
48
} ;
40
49
41
50
// Check if protection is supported
42
- const checkSupport = ( ) => {
51
+ const checkSupport = async ( ) => {
43
52
// Try different methods to access Electron APIs
44
53
try {
54
+ // Method 1: Use new IPC handlers via macPermissions API
55
+ if ( window . macPermissions ?. screenProtection ) {
56
+ const result = await window . macPermissions . screenProtection . checkSupport ( ) ;
57
+ if ( result . supported ) {
58
+ isProtectionSupported . value = true ;
59
+ return true ;
60
+ }
61
+ }
45
62
46
- // Method 1 : Check if window.remote is available (NativePHP exposes this)
63
+ // Method 2 : Check if window.remote is available (NativePHP exposes this)
47
64
if ( window . remote && typeof window . remote . getCurrentWindow === 'function' ) {
48
65
const currentWindow = window . remote . getCurrentWindow ( ) ;
49
66
if ( currentWindow && typeof currentWindow . setContentProtection === 'function' ) {
@@ -52,14 +69,15 @@ export function useScreenProtection() {
52
69
}
53
70
}
54
71
55
- // Method 2 : Check custom electronAPI (for custom preload)
72
+ // Method 3 : Check custom electronAPI (for custom preload)
56
73
if ( window . electronAPI ?. screenProtection ?. isContentProtectionSupported ) {
57
74
isProtectionSupported . value = window . electronAPI . screenProtection . isContentProtectionSupported ( ) ;
58
75
return isProtectionSupported . value ;
59
76
}
60
77
61
- // Method 3 : Check if we're in Electron environment
78
+ // Method 4 : Check if we're in Electron environment
62
79
if ( window . electron || ( window as any ) . process ?. versions ?. electron ) {
80
+ console . log ( 'Electron detected but no screen protection API available' ) ;
63
81
}
64
82
} catch ( error ) {
65
83
console . error ( 'Error checking screen protection support:' , error ) ;
@@ -71,7 +89,7 @@ export function useScreenProtection() {
71
89
} ;
72
90
73
91
// Toggle screen protection
74
- const toggleProtection = ( ) => {
92
+ const toggleProtection = async ( ) => {
75
93
if ( ! isProtectionSupported . value ) {
76
94
return false ;
77
95
}
@@ -80,8 +98,20 @@ export function useScreenProtection() {
80
98
let success = false ;
81
99
82
100
try {
83
- // Method 1: Try window.remote first (NativePHP)
84
- if ( window . remote && typeof window . remote . getCurrentWindow === 'function' ) {
101
+ // Method 1: Use new IPC handlers via macPermissions API
102
+ if ( window . macPermissions ?. screenProtection ) {
103
+ try {
104
+ const result = await window . macPermissions . screenProtection . set ( newState ) ;
105
+ if ( result . success ) {
106
+ success = true ;
107
+ }
108
+ } catch ( error ) {
109
+ console . error ( 'Error using macPermissions for screen protection:' , error ) ;
110
+ }
111
+ }
112
+
113
+ // Method 2: Try window.remote (NativePHP) as fallback
114
+ if ( ! success && window . remote && typeof window . remote . getCurrentWindow === 'function' ) {
85
115
const currentWindow = window . remote . getCurrentWindow ( ) ;
86
116
87
117
if ( currentWindow && typeof currentWindow . setContentProtection === 'function' ) {
@@ -116,12 +146,11 @@ export function useScreenProtection() {
116
146
} catch {
117
147
// Silently ignore errors
118
148
}
119
- } else {
120
149
}
121
150
}
122
151
123
- // Method 2 : Try custom electronAPI
124
- else if ( window . electronAPI ?. screenProtection ?. setContentProtection ) {
152
+ // Method 3 : Try custom electronAPI
153
+ if ( ! success && window . electronAPI ?. screenProtection ?. setContentProtection ) {
125
154
success = window . electronAPI . screenProtection . setContentProtection ( newState ) ;
126
155
}
127
156
@@ -139,7 +168,9 @@ export function useScreenProtection() {
139
168
140
169
return true ;
141
170
}
142
- } catch { }
171
+ } catch ( error ) {
172
+ console . error ( 'Error toggling screen protection:' , error ) ;
173
+ }
143
174
144
175
return false ;
145
176
} ;
@@ -166,10 +197,21 @@ export function useScreenProtection() {
166
197
} ;
167
198
168
199
// Check if protection is actually active
169
- const verifyProtection = ( ) => {
200
+ const verifyProtection = async ( ) => {
170
201
if ( ! isProtectionSupported . value ) return false ;
171
202
172
203
try {
204
+ // Method 1: Use new IPC handlers via macPermissions API
205
+ if ( window . macPermissions ?. screenProtection ) {
206
+ const result = await window . macPermissions . screenProtection . getStatus ( ) ;
207
+ if ( result . status === 'active' ) {
208
+ return true ;
209
+ } else if ( result . status === 'inactive' ) {
210
+ return false ;
211
+ }
212
+ }
213
+
214
+ // Method 2: Try window.remote as fallback
173
215
if ( window . remote && typeof window . remote . getCurrentWindow === 'function' ) {
174
216
const currentWindow = window . remote . getCurrentWindow ( ) ;
175
217
@@ -195,15 +237,15 @@ export function useScreenProtection() {
195
237
// Initialize on mount
196
238
onMounted ( ( ) => {
197
239
// Add a small delay to ensure Electron APIs are loaded
198
- setTimeout ( ( ) => {
199
- if ( checkSupport ( ) ) {
240
+ setTimeout ( async ( ) => {
241
+ if ( await checkSupport ( ) ) {
200
242
// Load saved preference
201
243
const savedPreference = loadPreference ( ) ;
202
244
if ( savedPreference ) {
203
- enableProtection ( ) ;
245
+ await enableProtection ( ) ;
204
246
// Verify after enabling
205
- setTimeout ( ( ) => {
206
- verifyProtection ( ) ;
247
+ setTimeout ( async ( ) => {
248
+ await verifyProtection ( ) ;
207
249
} , 500 ) ;
208
250
}
209
251
}
0 commit comments