Skip to content

Commit

Permalink
✨ Loop can now ask user for Screen Recording access
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKai77 committed Sep 3, 2023
1 parent b251c83 commit b1f7997
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
19 changes: 15 additions & 4 deletions Loop/Helpers/PermissionsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import SwiftUI
import Defaults

class PermissionsManager {
static func requestAccess() {
PermissionsManager.Accessibility.requestAccess()
PermissionsManager.ScreenRecording.requestAccess()
}

class Accessibility {
static func getStatus() -> Bool {
// Get current state for accessibility access
Expand All @@ -18,14 +23,20 @@ class PermissionsManager {
return status
}

static func requestAccess() {
@discardableResult
static func requestAccess() -> Bool {
if PermissionsManager.Accessibility.getStatus() {
return
return true
}
let alert = NSAlert()
alert.messageText = "\(Bundle.main.appName) Needs Accessibility Permissions"
alert.informativeText = "Please grant accessibility access to be able to resize windows."
alert.informativeText = "Please grant access to be able to resize windows."
alert.runModal()

let options: NSDictionary = [kAXTrustedCheckOptionPrompt.takeRetainedValue() as NSString: true]
let status = AXIsProcessTrustedWithOptions(options)

return status
}
}

Expand All @@ -41,7 +52,7 @@ class PermissionsManager {

let alert = NSAlert()
alert.messageText = "\(Bundle.main.appName) Needs Screen Recording Permissions"
alert.informativeText = "Please grant screen recording access to be able to resize windows (with animation)."
alert.informativeText = "Screen recording permissions are required to animate windows being resized."
alert.runModal()

CGRequestScreenCaptureAccess()
Expand Down
4 changes: 1 addition & 3 deletions Loop/LoopApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ notification: Notification) {
NSApp.setActivationPolicy(.accessory)

// Check accessibility access, then if access is not granted,
// show a more informative alert asking for accessibility access
// Check & ask for accessibility access
PermissionsManager.Accessibility.requestAccess()
PermissionsManager.ScreenRecording.requestAccess()

iconManager.restoreCurrentAppIcon()

Expand Down
13 changes: 8 additions & 5 deletions Loop/Settings/Views/GeneralSettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ struct GeneralSettingsView: View {
BetaIndicator("BETA")
}
}
.onChange(of: animateWindowResizes) { _ in
if animateWindowResizes == true {
PermissionsManager.ScreenRecording.requestAccess()
}
}

Slider(value: $windowPadding,
in: 0...25,
Expand Down Expand Up @@ -124,16 +129,14 @@ struct GeneralSettingsView: View {
Spacer()

Button("Refresh Status", action: {
PermissionsManager.Accessibility.requestAccess()
PermissionsManager.requestAccess()
self.isAccessibilityAccessGranted = PermissionsManager.Accessibility.getStatus()

PermissionsManager.ScreenRecording.requestAccess()
self.isScreenRecordingAccessGranted = PermissionsManager.ScreenRecording.getStatus()
})
.buttonStyle(.link)
.foregroundStyle(Color.accentColor)
.disabled(isAccessibilityAccessGranted)
.opacity(isAccessibilityAccessGranted ? 0.6 : 1)
.disabled(isAccessibilityAccessGranted && isScreenRecordingAccessGranted)
.opacity(isAccessibilityAccessGranted ? isScreenRecordingAccessGranted ? 0.6 : 1 : 1)
.help("Refresh the current accessibility permissions")
.onAppear {
self.isAccessibilityAccessGranted = PermissionsManager.Accessibility.getStatus()
Expand Down

0 comments on commit b1f7997

Please sign in to comment.