Skip to content

Commit cba5fd2

Browse files
committed
Add global shortcut "CMD + G" to toggle the display of guidelines.
1 parent 1101b90 commit cba5fd2

5 files changed

Lines changed: 62 additions & 2 deletions

File tree

GridGuide/GridGuide.entitlements

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
<plist version="1.0">
44
<dict>
55
<key>com.apple.security.app-sandbox</key>
6-
<true/>
6+
<false/>
77
<key>com.apple.security.files.user-selected.read-only</key>
88
<true/>
9+
<key>com.apple.security.automation.apple-events</key>
10+
<true/>
911
</dict>
1012
</plist>

GridGuide/GridGuideApp.swift

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,22 @@ class AppDelegate: NSObject, NSApplicationDelegate {
6464
}
6565
}
6666

67+
@objc dynamic var isGridVisible: Bool = true {
68+
didSet {
69+
NotificationCenter.default.post(
70+
name: NSNotification.Name("gridVisibilityChanged"),
71+
object: nil,
72+
userInfo: ["isVisible": isGridVisible]
73+
)
74+
}
75+
}
76+
6777
private var transparencyLabel: NSTextField!
6878

6979
func applicationDidFinishLaunching(_ notification: Notification) {
7080
DispatchQueue.main.async { [weak self] in
7181
self?.setupStatusItem()
82+
self?.registerGlobalShortcut()
7283
}
7384
}
7485

@@ -103,7 +114,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
103114
menuItem.view = containerView
104115
menu.addItem(menuItem)
105116

106-
// 添加透明度滑块
117+
// Add transparency slider
107118
let transparencyMenuItem = NSMenuItem()
108119
let transparencyContainerView = NSView(frame: NSRect(x: 0, y: 0, width: 200, height: 40))
109120

@@ -131,6 +142,41 @@ class AppDelegate: NSObject, NSApplicationDelegate {
131142
statusItem.menu = menu
132143
}
133144

145+
private func registerGlobalShortcut() {
146+
let options: NSDictionary = [kAXTrustedCheckOptionPrompt.takeUnretainedValue() as NSString: true]
147+
let accessEnabled = AXIsProcessTrustedWithOptions(options)
148+
print("Accessibility permission status: \(accessEnabled)")
149+
150+
if !accessEnabled {
151+
print("Please grant Accessibility permission in System Settings.")
152+
NSWorkspace.shared.open(URL(string: "x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility")!)
153+
}
154+
155+
print("Registering global shortcut for Command+G")
156+
157+
// Add local monitor
158+
NSEvent.addLocalMonitorForEvents(matching: .keyDown) { [weak self] event in
159+
if event.modifierFlags.contains(.command) && event.charactersIgnoringModifiers == "g" {
160+
print("Command+G detected in local monitor!")
161+
self?.toggleGridVisibility()
162+
return nil // Consume the event
163+
}
164+
return event
165+
}
166+
167+
// Keep global monitor
168+
NSEvent.addGlobalMonitorForEvents(matching: .keyDown) { [weak self] event in
169+
if event.modifierFlags.contains(.command) && event.charactersIgnoringModifiers == "g" {
170+
print("Command+G detected in global monitor!")
171+
self?.toggleGridVisibility()
172+
}
173+
}
174+
}
175+
176+
private func toggleGridVisibility() {
177+
isGridVisible.toggle()
178+
}
179+
134180
@objc func quit() {
135181
NSApplication.shared.terminate(nil)
136182
}

GridGuide/GridGuideView.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ struct GridGuideView: View {
1616
@State private var numColumns: Int = 30
1717
@State private var numRows: Int = 30
1818
@State private var transparency: Double = 0.5
19+
@State private var isGridVisible: Bool = true
1920

2021
init() {
2122
_numColumns = State(initialValue: Int(screenWidth) / gridSize)
@@ -46,7 +47,13 @@ struct GridGuideView: View {
4647
self.transparency = value
4748
}
4849
}
50+
.onReceive(NotificationCenter.default.publisher(for: .gridVisibilityChanged)) { notification in
51+
if let isVisible = notification.userInfo?["isVisible"] as? Bool {
52+
self.isGridVisible = isVisible
53+
}
54+
}
4955
.opacity(transparency)
56+
.opacity(isGridVisible ? 1 : 0)
5057
.allowsHitTesting(false)
5158
}
5259

GridGuide/Info.plist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,9 @@
88
<string>Active</string>
99
<key>NSAppearanceType</key>
1010
<string>NSAppearanceNameVibrantDark</string>
11+
<key>NSAppleEventsUsageDescription</key>
12+
<string>GridGuide needs accessibility access to detect keyboard shortcuts.</string>
13+
<key>NSAppleScriptEnabled</key>
14+
<true/>
1115
</dict>
1216
</plist>

GridGuide/NotificationExtension.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ import Foundation
33
extension Notification.Name {
44
static let gridSizeChanged = Notification.Name("gridSizeChanged")
55
static let gridTransparencyChanged = Notification.Name("gridTransparencyChanged")
6+
static let gridVisibilityChanged = Notification.Name("gridVisibilityChanged")
67
}

0 commit comments

Comments
 (0)