-
-
Notifications
You must be signed in to change notification settings - Fork 853
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OpenAtLogin.swift into MultitouchExtension
- Loading branch information
Showing
4 changed files
with
89 additions
and
15 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,58 @@ | ||
import Foundation | ||
import ServiceManagement | ||
|
||
final class OpenAtLogin: ObservableObject { | ||
static let shared = OpenAtLogin() | ||
|
||
@Published var registered = false | ||
|
||
var error = "" | ||
|
||
init() { | ||
registered = SMAppService.mainApp.status == .enabled | ||
} | ||
|
||
var developmentBinary: Bool { | ||
let bundlePath = Bundle.main.bundlePath | ||
|
||
// Xcode builds | ||
// - /Build/Products/Debug/*.app | ||
// - /Build/Products/Release/*.app | ||
if bundlePath.contains("/Build/") { | ||
return true | ||
} | ||
|
||
// Command line builds | ||
// - /build/Release/*.app | ||
if bundlePath.contains("/build/") { | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
@MainActor | ||
func update(register: Bool) { | ||
error = "" | ||
|
||
do { | ||
if register { | ||
try SMAppService.mainApp.register() | ||
} else { | ||
// `unregister` throws `Operation not permitted` error in the following cases. | ||
// | ||
// 1. `unregister` is called. | ||
// 2. macOS is restarted to clean up login items entries. | ||
// 3. `unregister` is called again. | ||
// | ||
// So, we ignore the error of `unregister`. | ||
|
||
try? SMAppService.mainApp.unregister() | ||
} | ||
|
||
registered = register | ||
} catch { | ||
self.error = error.localizedDescription | ||
} | ||
} | ||
} |
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