Skip to content

Commit 9d481d1

Browse files
committed
fix: implement screen protection via IPC handlers for context isolation compatibility
- Update composer.json to use local nativephp-electron package - Modify useScreenProtection composable to use new IPC-based approach - Add support for window.macPermissions.screenProtection API - Maintain fallback to window.remote for backward compatibility - Fix 'N/A' display issue when context isolation is enabled
1 parent 7c10b19 commit 9d481d1

File tree

3 files changed

+72
-33
lines changed

3 files changed

+72
-33
lines changed

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,11 @@
8989
},
9090
"repositories": [
9191
{
92-
"type": "vcs",
93-
"url": "https://github.com/vijaythecoder/nativephp-electron"
92+
"type": "path",
93+
"url": "./nativephp-electron",
94+
"options": {
95+
"symlink": true
96+
}
9497
}
9598
],
9699
"config": {

composer.lock

Lines changed: 8 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/js/composables/useScreenProtection.ts

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,20 @@ interface Remote {
1414
};
1515
}
1616

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+
1725
declare global {
1826
interface Window {
1927
electronAPI?: ElectronAPI;
2028
remote?: Remote;
2129
electron?: any;
30+
macPermissions?: MacPermissions;
2231
}
2332
}
2433

@@ -39,11 +48,19 @@ export function useScreenProtection() {
3948
};
4049

4150
// Check if protection is supported
42-
const checkSupport = () => {
51+
const checkSupport = async () => {
4352
// Try different methods to access Electron APIs
4453
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+
}
4562

46-
// Method 1: Check if window.remote is available (NativePHP exposes this)
63+
// Method 2: Check if window.remote is available (NativePHP exposes this)
4764
if (window.remote && typeof window.remote.getCurrentWindow === 'function') {
4865
const currentWindow = window.remote.getCurrentWindow();
4966
if (currentWindow && typeof currentWindow.setContentProtection === 'function') {
@@ -52,14 +69,15 @@ export function useScreenProtection() {
5269
}
5370
}
5471

55-
// Method 2: Check custom electronAPI (for custom preload)
72+
// Method 3: Check custom electronAPI (for custom preload)
5673
if (window.electronAPI?.screenProtection?.isContentProtectionSupported) {
5774
isProtectionSupported.value = window.electronAPI.screenProtection.isContentProtectionSupported();
5875
return isProtectionSupported.value;
5976
}
6077

61-
// Method 3: Check if we're in Electron environment
78+
// Method 4: Check if we're in Electron environment
6279
if (window.electron || (window as any).process?.versions?.electron) {
80+
console.log('Electron detected but no screen protection API available');
6381
}
6482
} catch (error) {
6583
console.error('Error checking screen protection support:', error);
@@ -71,7 +89,7 @@ export function useScreenProtection() {
7189
};
7290

7391
// Toggle screen protection
74-
const toggleProtection = () => {
92+
const toggleProtection = async () => {
7593
if (!isProtectionSupported.value) {
7694
return false;
7795
}
@@ -80,8 +98,20 @@ export function useScreenProtection() {
8098
let success = false;
8199

82100
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') {
85115
const currentWindow = window.remote.getCurrentWindow();
86116

87117
if (currentWindow && typeof currentWindow.setContentProtection === 'function') {
@@ -116,12 +146,11 @@ export function useScreenProtection() {
116146
} catch {
117147
// Silently ignore errors
118148
}
119-
} else {
120149
}
121150
}
122151

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) {
125154
success = window.electronAPI.screenProtection.setContentProtection(newState);
126155
}
127156

@@ -139,7 +168,9 @@ export function useScreenProtection() {
139168

140169
return true;
141170
}
142-
} catch {}
171+
} catch (error) {
172+
console.error('Error toggling screen protection:', error);
173+
}
143174

144175
return false;
145176
};
@@ -166,10 +197,21 @@ export function useScreenProtection() {
166197
};
167198

168199
// Check if protection is actually active
169-
const verifyProtection = () => {
200+
const verifyProtection = async () => {
170201
if (!isProtectionSupported.value) return false;
171202

172203
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
173215
if (window.remote && typeof window.remote.getCurrentWindow === 'function') {
174216
const currentWindow = window.remote.getCurrentWindow();
175217

@@ -195,15 +237,15 @@ export function useScreenProtection() {
195237
// Initialize on mount
196238
onMounted(() => {
197239
// Add a small delay to ensure Electron APIs are loaded
198-
setTimeout(() => {
199-
if (checkSupport()) {
240+
setTimeout(async () => {
241+
if (await checkSupport()) {
200242
// Load saved preference
201243
const savedPreference = loadPreference();
202244
if (savedPreference) {
203-
enableProtection();
245+
await enableProtection();
204246
// Verify after enabling
205-
setTimeout(() => {
206-
verifyProtection();
247+
setTimeout(async () => {
248+
await verifyProtection();
207249
}, 500);
208250
}
209251
}

0 commit comments

Comments
 (0)