-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a screen for opting into permissions
Also add a NSUserDefaults wrapper
- Loading branch information
Showing
5 changed files
with
341 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// | ||
// Preference.swift | ||
// Ensemble | ||
// | ||
// Created by Saagar Jha on 2/6/24. | ||
// | ||
|
||
import SwiftUI | ||
|
||
@propertyWrapper | ||
struct Preference<T>: DynamicProperty { | ||
// Forces an update | ||
@State | ||
var value = false | ||
|
||
let key: String | ||
let defaultValue: T | ||
|
||
init(_ key: String, defaultValue: T) { | ||
self.key = key | ||
self.defaultValue = defaultValue | ||
} | ||
|
||
init(previewing value: T) { | ||
self.init("", defaultValue: value) | ||
} | ||
|
||
var wrappedValue: T { | ||
get { | ||
_ = value | ||
return UserDefaults.standard.object(forKey: key) as? T ?? defaultValue | ||
} | ||
nonmutating set { | ||
value.toggle() | ||
UserDefaults.standard.setValue(newValue, forKey: key) | ||
} | ||
} | ||
|
||
var projectedValue: Binding<T> { | ||
Binding( | ||
get: { | ||
wrappedValue | ||
}, | ||
set: { | ||
wrappedValue = $0 | ||
} | ||
) | ||
} | ||
|
||
var isSet: Bool { | ||
return UserDefaults.standard.value(forKey: key) == nil | ||
} | ||
|
||
func reset() { | ||
UserDefaults.standard.removeObject(forKey: key) | ||
} | ||
} | ||
|
||
@propertyWrapper | ||
struct CodablePreference<T: Codable> { | ||
// Forces an update | ||
@State | ||
var value = false | ||
|
||
var key: String | ||
var defaultValue: T | ||
|
||
init(_ key: String, defaultValue: T) { | ||
self.key = key | ||
self.defaultValue = defaultValue | ||
} | ||
|
||
init(previewing value: T) { | ||
self.init("", defaultValue: value) | ||
} | ||
|
||
var wrappedValue: T { | ||
get { | ||
_ = value | ||
guard let data = UserDefaults.standard.object(forKey: key) as? Data else { | ||
return defaultValue | ||
} | ||
return (try? JSONDecoder().decode(T.self, from: data)) ?? defaultValue | ||
} | ||
nonmutating set { | ||
value.toggle() | ||
UserDefaults.standard.setValue(try! JSONEncoder().encode(newValue), forKey: key) | ||
} | ||
} | ||
|
||
var projectedValue: Binding<T> { | ||
Binding( | ||
get: { | ||
wrappedValue | ||
}, | ||
set: { | ||
wrappedValue = $0 | ||
} | ||
) | ||
} | ||
|
||
var isSet: Bool { | ||
UserDefaults.standard.value(forKey: key) == nil | ||
} | ||
|
||
func reset() { | ||
UserDefaults.standard.removeObject(forKey: key) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// | ||
// Permissions.swift | ||
// macOS | ||
// | ||
// Created by Saagar Jha on 2/5/24. | ||
// | ||
|
||
import AppKit | ||
|
||
enum Permission: CaseIterable, Identifiable { | ||
case screenRecording | ||
case accesibility | ||
|
||
var name: String { | ||
switch self { | ||
case .screenRecording: | ||
return "Screen Recording" | ||
case .accesibility: | ||
return "Accessibility" | ||
} | ||
} | ||
|
||
var id: String { | ||
name | ||
} | ||
|
||
var reason: String { | ||
switch self { | ||
case .screenRecording: | ||
return "Provides access to critical functionality such as the ability to list windows and stream them." | ||
case .accesibility: | ||
return "Used to track certain window elements and synthesize input events to control your Mac remotely." | ||
} | ||
} | ||
|
||
var enabled: Bool { | ||
switch self { | ||
case .screenRecording: | ||
CGPreflightScreenCaptureAccess() | ||
case .accesibility: | ||
// CGPreflightPostEventAccess appears to be a cached value | ||
AXIsProcessTrusted() | ||
} | ||
} | ||
|
||
var requested: Bool { | ||
false | ||
} | ||
|
||
func request() { | ||
var result: Bool | ||
switch self { | ||
case .screenRecording: | ||
result = CGRequestScreenCaptureAccess() | ||
case .accesibility: | ||
result = CGRequestPostEventAccess() | ||
} | ||
|
||
if !result { | ||
backupRequest() | ||
} | ||
} | ||
|
||
func backupRequest() { | ||
let parameter: String | ||
switch self { | ||
case .screenRecording: | ||
parameter = "Privacy_ScreenCapture" | ||
case .accesibility: | ||
parameter = "Privacy_Accessibility" | ||
} | ||
NSWorkspace.shared.open(URL(string: "x-apple.systempreferences:com.apple.preference.security?\(parameter)")!) | ||
|
||
} | ||
} |
Oops, something went wrong.