Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions Click2Minimize/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ struct Click2MinimizeApp: App {
Divider()

// New button to open System Preferences for Accessibility
Button(action: appDelegate.openAccessibilityPreferences, label: { Text("Accessibility Preferences") })
Button(action: { appDelegate.openPreferences(for: "Accessibility") }, label: { Text("Accessibility Preferences") })

// New button to open System Preferences for Automation
Button(action: appDelegate.openAutomationPreferences, label: { Text("Automation Preferences") })
Button(action: { appDelegate.openPreferences(for: "Automation") }, label: { Text("Automation Preferences") })
Divider()

Button(action: appDelegate.quitApp, label: { Text("Quit") })
Expand Down Expand Up @@ -359,7 +359,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {

let response = alert.runModal()
if response == .alertFirstButtonReturn {
openAccessibilityPreferences()
openPreferences(for: "Accessibility")
// Quit the application as it won't work without permission
// Delay termination by 1 second
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
Expand All @@ -368,13 +368,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
}

func openAccessibilityPreferences() {
let url = URL(string: "x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility")!
NSWorkspace.shared.open(url)
}

func openAutomationPreferences() {
let url = URL(string: "x-apple.systempreferences:com.apple.preference.security?Privacy_Automation")!
func openPreferences(for pane: String) {
let url = URL(string: "x-apple.systempreferences:com.apple.preference.security?Privacy_\(pane)")!
NSWorkspace.shared.open(url)
}

Expand Down
6 changes: 6 additions & 0 deletions pr_description.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
🧹 [Refactor openPreferences methods to reduce duplication]

🎯 **What:** The code health issue addressed was the duplication of the methods `openAccessibilityPreferences` and `openAutomationPreferences` in `AppDelegate.swift`.
💡 **Why:** Combining these into a single method `openPreferences(for: String)` improves maintainability and readability by eliminating duplicated logic and creating a reusable pattern for opening other preference panes in the future.
✅ **Verification:** I verified the change by manually inspecting all usages and making sure they correctly supply "Accessibility" or "Automation" as the pane string argument to the new method. Note: The environment does not have macOS build tools like `swift` or `xcodebuild`, so actual compilation couldn't be done, but a static code review confirmed correctness and absence of syntax errors.
✨ **Result:** The codebase is cleaner, shorter, and more DRY (Don't Repeat Yourself), without changing existing behavior.