diff --git a/README.md b/README.md index bc0a8c3..95bb464 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,15 @@ Open `TinyHand.dmg`, drag TinyHand to Applications and allow **Accessibility** w | **⌃⌥X** | Clear the drawing | | **Esc** | Clear everything and turn the pointer off | +### Pointer mode + +In **Settings → General → Clicks**, set **When the pointer is on** to **Work as usual** to use the +hand as your everyday cursor. Clicks, drags and scrolls reach your apps as normal, and the hand stays over +the menu bar and menus, right-click ones included. Hold **⌥** to tap +and draw, and let go to get normal clicks back. Double-tap **⇧** to stay in taps until you double-tap +again. In this mode Esc doesn't turn the pointer off, and the pointer comes back after sleep, a lock +or Settings. Turn on **Turn the pointer on when TinyHand opens** to have the hand from login. + While the pointer is on, a small toolbar at the bottom of the screen lets you pick one of 36 hands or a laser dot, mute the tap sound, or exit. diff --git a/Sources/TinyHand/AppDelegate.swift b/Sources/TinyHand/AppDelegate.swift index e759900..e621018 100644 --- a/Sources/TinyHand/AppDelegate.swift +++ b/Sources/TinyHand/AppDelegate.swift @@ -17,6 +17,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate { private var clearHotKey: UInt32? private lazy var automation = Automation(overlay: overlay) private var launched = false + private var resumeAfterSettings = false private var pendingURLs: [URL] = [] func application(_ application: NSApplication, open urls: [URL]) { @@ -60,6 +61,8 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate { } if !UserDefaults.standard.bool(forKey: WelcomeWindow.shownKey) || !AXIsProcessTrusted() { welcome.show() + } else if Preferences.turnOnAtLaunch { + overlay.activate() } automation.writeState() @@ -111,6 +114,13 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate { self?.apply(s, isToggle: false) ?? false } settings.updater = Updater.isAvailable ? updater : nil + settings.onClose = { [weak self] in + guard let self = self, self.resumeAfterSettings else { + return + } + self.resumeAfterSettings = false + self.overlay.activate() + } settings.onRecording = { [weak self] recording in if recording { self?.unregisterHotKeys() @@ -211,6 +221,8 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate { } @objc private func openSettings() { + // In pointer mode the hand is the everyday cursor, so it comes back when Settings closes. + resumeAfterSettings = resumeAfterSettings || (overlay.isActive && Preferences.pointerMode) overlay.exit() settings.show() } @@ -250,9 +262,11 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate { image?.size = NSSize(width: 18, height: 18) image?.isTemplate = true statusItem.button?.image = image - if overlay.isClickThrough, let image = image { + if overlay.isLatched, let image = image { statusItem.button?.image = AppDelegate.tinted(image, .systemOrange) - statusItem.button?.toolTip = "TinyHand clicks go through — double-tap ⇧ to tap again, click to turn off" + statusItem.button?.toolTip = overlay.isClickThrough + ? "TinyHand clicks go through — double-tap ⇧ to tap again, click to turn off" + : "TinyHand taps — double-tap ⇧ for normal clicks, click to turn off" return } statusItem.button?.toolTip = overlay.isActive diff --git a/Sources/TinyHand/InputTap.swift b/Sources/TinyHand/InputTap.swift index f9f7971..568691c 100644 --- a/Sources/TinyHand/InputTap.swift +++ b/Sources/TinyHand/InputTap.swift @@ -30,7 +30,8 @@ final class InputTap { set { lock.withLock { _interactKey = newValue } } } - /// Latched by double-tapping Shift: every click reaches the app, as if the interact key were held. + /// Latched by double-tapping Shift, or on from the start in pointer mode: clicks reach the app. + /// Holding the interact key flips it, so a held key then makes taps and draws. var clickThrough: Bool { get { lock.withLock { _clickThrough } } set { lock.withLock { _clickThrough = newValue } } @@ -151,7 +152,7 @@ final class InputTap { switch type { case .leftMouseDown, .rightMouseDown, .otherMouseDown: let viaKey = !key.isEmpty && (event.flags.contains(key) || CGEventSource.flagsState(.hidSystemState).contains(key)) - if viaKey || clickThrough { + if viaKey != clickThrough { lock.withLock { _interactPress = true } DispatchQueue.main.async { [weak self] in self?.onInteractDown?() } } @@ -210,7 +211,7 @@ final class InputTap { } // Backstop for lock paths that don't post com.apple.screenIsLocked. - private static var screenLocked: Bool { + static var screenLocked: Bool { guard let session = CGSessionCopyCurrentDictionary() as? [String: Any] else { return false } diff --git a/Sources/TinyHand/OverlayController.swift b/Sources/TinyHand/OverlayController.swift index b0eae63..5ccc4f5 100644 --- a/Sources/TinyHand/OverlayController.swift +++ b/Sources/TinyHand/OverlayController.swift @@ -12,6 +12,8 @@ final class OverlayController { private let inputTap = InputTap() private let tapSound = TapSound() private var interactHeld = false + private var presenting = true + private var resumeAfterSystem = false private var menuHover = false private var guardCursorUntil = Date.distantPast private var timer: Timer? @@ -137,8 +139,8 @@ final class OverlayController { NotificationCenter.default.addObserver(forName: Preferences.changed, object: nil, queue: .main) { [weak self] _ in self?.syncSound() self?.inputTap.interactKey = Preferences.interactKey.cgFlags - if let self = self, !Preferences.doubleShiftClickThrough, self.isClickThrough { - self.setClickThrough(false) + if let self = self, !Preferences.doubleShiftClickThrough, self.isLatched { + self.setClickThrough(Preferences.pointerMode) } self?.armView.updateInkColor() self?.tapSound.load(Preferences.tapSound) @@ -209,6 +211,21 @@ final class OverlayController { self?.turnOffForSystem(name.rawValue) } } + distributed.addObserver(forName: Notification.Name("com.apple.screenIsUnlocked"), object: nil, queue: .main) { [weak self] _ in + self?.resumeAfterSystemIfNeeded("unlocked") + } + let wakes: [Notification.Name] = [ + NSWorkspace.screensDidWakeNotification, + NSWorkspace.didWakeNotification, + NSWorkspace.sessionDidBecomeActiveNotification, + ] + for name in wakes { + workspace.addObserver(forName: name, object: nil, queue: .main) { [weak self] _ in + DispatchQueue.main.asyncAfter(deadline: .now() + 1) { + self?.resumeAfterSystemIfNeeded(name.rawValue) + } + } + } workspace.addObserver(forName: NSWorkspace.activeSpaceDidChangeNotification, object: nil, queue: .main) { [weak self] _ in self?.followSpace() } @@ -234,6 +251,11 @@ final class OverlayController { isActive && inputTap.clickThrough } + /// Double-tapping Shift has switched away from the mode the pointer starts in. + var isLatched: Bool { + isActive && inputTap.clickThrough != Preferences.pointerMode + } + private func installShiftMonitors() { guard shiftMonitors.isEmpty else { return @@ -310,9 +332,7 @@ final class OverlayController { if on { release() } - toolbar.showHint(on - ? [("Clicks go through", "the hand stays"), ("Double-tap ⇧", "to tap again")] - : [("Taps", "clicks don't reach apps"), ("Double-tap ⇧", "to click through")]) + toolbar.showHint(OverlayController.modeHint(clicksThrough: on)) DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) { [weak self] in self?.toolbar.hideHint() } @@ -320,12 +340,37 @@ final class OverlayController { onActiveChanged?() } + private static func modeHint(clicksThrough: Bool) -> [(key: String, text: String)] { + let key = Preferences.interactKey + var parts: [(key: String, text: String)] = clicksThrough + ? [("Clicks go through", "the hand stays")] + : [("Taps", "clicks don't reach apps")] + if key != .off { + parts.append(clicksThrough ? ("Hold \(key.symbol)", "to tap and draw") : ("Hold \(key.symbol)", "to click for real")) + } + if Preferences.doubleShiftClickThrough { + parts.append(clicksThrough ? ("Double-tap ⇧", "to tap") : ("Double-tap ⇧", "to click through")) + } + return parts + } + private func turnOffForSystem(_ reason: String) { guard isActive else { return } Log.write("turning pointer off: \(reason)") deactivate() + resumeAfterSystem = Preferences.pointerMode + } + + // In pointer mode the hand is the everyday cursor, so it comes back after sleep or a lock. + private func resumeAfterSystemIfNeeded(_ reason: String) { + guard resumeAfterSystem, !isActive, !InputTap.screenLocked else { + return + } + resumeAfterSystem = false + Log.write("turning pointer back on: \(reason)") + activate() } private func release() { @@ -409,6 +454,7 @@ final class OverlayController { return false } isActive = true + resumeAfterSystem = false currentFrame = nil tapSound.warm() @@ -423,7 +469,7 @@ final class OverlayController { options: [.userInitiated, .latencyCritical], reason: "TinyHand pointer is on") let installed = inputTap.install() inputTap.interactKey = Preferences.interactKey.cgFlags - inputTap.clickThrough = false + inputTap.clickThrough = Preferences.pointerMode installShiftMonitors() inputTap.setActive(installed) Log.write("activate trusted=\(AXIsProcessTrusted()) tapInstalled=\(installed)") @@ -436,14 +482,19 @@ final class OverlayController { revealUntil = Date().addingTimeInterval(3) if installed && !UserDefaults.standard.bool(forKey: OverlayController.hintShownKey) { UserDefaults.standard.set(true, forKey: OverlayController.hintShownKey) - var parts: [(key: String, text: String)] = [("Click", "to tap"), ("Drag", "to draw")] - if Preferences.interactKey != .off { - parts.append(("Hold \(Preferences.interactKey.symbol)", "to click for real")) - } - if Preferences.doubleShiftClickThrough { - parts.append(("Double-tap ⇧", "to click through")) + var parts: [(key: String, text: String)] + if Preferences.pointerMode { + parts = OverlayController.modeHint(clicksThrough: true) + } else { + parts = [("Click", "to tap"), ("Drag", "to draw")] + if Preferences.interactKey != .off { + parts.append(("Hold \(Preferences.interactKey.symbol)", "to click for real")) + } + if Preferences.doubleShiftClickThrough { + parts.append(("Double-tap ⇧", "to click through")) + } + parts.append(("Esc", "to exit")) } - parts.append(("Esc", "to exit")) toolbar.showHint(parts) revealUntil = Date().addingTimeInterval(10) DispatchQueue.main.asyncAfter(deadline: .now() + 10) { [weak self] in @@ -507,11 +558,15 @@ final class OverlayController { let overMenu = menuBar.contains(mouse) || menuRects.contains { $0.contains(cgMouse) } || Date() < menuOpeningUntil let key = Preferences.interactKey.cgFlags - let interacting = (!key.isEmpty && CGEventSource.flagsState(.hidSystemState).contains(key)) || inputTap.interactPressing + let keyHeld = !key.isEmpty && CGEventSource.flagsState(.hidSystemState).contains(key) + let interacting = keyHeld || inputTap.interactPressing interactHeld = interacting menuHover = overMenu let through = inputTap.clickThrough - let normal = !drawing && !Preferences.interactKeepsHand && ((interacting && !through) || overMenu || ownMenuOpen) + presenting = keyHeld == through && !inputTap.interactPressing + // In pointer mode the hand is the cursor, so it stays over the menu bar and menus, right-click ones too. + let keepHand = Preferences.interactKeepsHand || Preferences.pointerMode + let normal = !drawing && !keepHand && ((interacting && !through) || overMenu || ownMenuOpen) setEscEnabled(menuRects.isEmpty && !ownMenuOpen && !through) if normal != normalCursor { @@ -726,7 +781,7 @@ final class OverlayController { updateZones(mouse: mouse, screen: screen) let overDock = DockArea.contains(OverlayController.cgPoint(mouse)) - let catchMouse = overDock && inputTap.isInstalled && !normalCursor && !interactHeld && !inputTap.clickThrough + let catchMouse = overDock && inputTap.isInstalled && !normalCursor && presenting if window.ignoresMouseEvents == catchMouse { window.ignoresMouseEvents = !catchMouse } diff --git a/Sources/TinyHand/Preferences.swift b/Sources/TinyHand/Preferences.swift index 6eff8f7..309a737 100644 --- a/Sources/TinyHand/Preferences.swift +++ b/Sources/TinyHand/Preferences.swift @@ -129,6 +129,17 @@ enum Preferences { set { set(newValue, "interactKeepsHand") } } + /// Pointer mode: the hand is the everyday cursor and clicks reach apps; holding the interact key taps and draws. + static var pointerMode: Bool { + get { defaults.bool(forKey: "pointerMode") } + set { set(newValue, "pointerMode") } + } + + static var turnOnAtLaunch: Bool { + get { defaults.bool(forKey: "turnOnAtLaunch") } + set { set(newValue, "turnOnAtLaunch") } + } + static var doubleShiftClickThrough: Bool { get { defaults.object(forKey: "doubleShiftClickThrough") as? Bool ?? true } set { set(newValue, "doubleShiftClickThrough") } @@ -186,7 +197,7 @@ enum Preferences { static func resetAll() { for key in ["shortcut.toggle", "shortcut.clear", "pointerSize", "showShadow", "fadeSeconds", - "sound.hands", "sound.laser", "interactKey", "interactKeepsHand", "doubleShiftClickThrough", "pinnedStyles", "pointerStyle", + "sound.hands", "sound.laser", "interactKey", "interactKeepsHand", "pointerMode", "turnOnAtLaunch", "doubleShiftClickThrough", "pinnedStyles", "pointerStyle", "inkChoice", "showInkButton", "showToolbar", "tapSound"] { defaults.removeObject(forKey: key) } diff --git a/Sources/TinyHand/SettingsView.swift b/Sources/TinyHand/SettingsView.swift index 4b62fc9..db1a9a8 100644 --- a/Sources/TinyHand/SettingsView.swift +++ b/Sources/TinyHand/SettingsView.swift @@ -423,8 +423,21 @@ private struct GeneralPage: View { if let error = model.shortcutError { Text(error).font(.system(size: 12)).foregroundColor(.red) } - Card(title: "Clicking for real") { - Row("Hold to use the app", detail: "Clicks, drags and scrolls reach the app.") { + Card(title: "Clicks") { + Row("When the pointer is on", detail: Preferences.pointerMode + ? "The hand is your cursor, over menus too, and clicks work as usual. It comes back after sleep, a lock or Settings, and Esc doesn't turn it off." + : "Clicks tap and dragging draws. Nothing underneath gets clicked.") { + Picker("", selection: model.binding({ Preferences.pointerMode }, { Preferences.pointerMode = $0 })) { + Text("Tap and draw").tag(false) + Text("Work as usual").tag(true) + } + .labelsHidden() + .pickerStyle(.menu) + .fixedSize() + .focusable(false) + } + Row(Preferences.pointerMode ? "Hold to tap and draw" : "Hold to use the app", + detail: Preferences.pointerMode ? "Clicks tap and dragging draws while you hold it." : "Clicks, drags and scrolls reach the app.") { Picker("", selection: model.binding({ Preferences.interactKey }, { Preferences.interactKey = $0 })) { ForEach(InteractKey.allCases, id: \.self) { key in Text(key.title).tag(key) @@ -435,7 +448,7 @@ private struct GeneralPage: View { .fixedSize() .focusable(false) } - if Preferences.interactKey != .off { + if Preferences.interactKey != .off && !Preferences.pointerMode { Row("While holding", detail: Preferences.interactKeepsHand ? "The hand stays, in menus too." : "The normal cursor comes back, in menus too.") { Picker("", selection: model.binding({ Preferences.interactKeepsHand }, { Preferences.interactKeepsHand = $0 })) { Text("Show the cursor").tag(false) @@ -447,7 +460,10 @@ private struct GeneralPage: View { .focusable(false) } } - Row("Double-tap ⇧ to click through", detail: "Clicks reach the app and the hand stays. Double-tap again for taps. The menu bar icon turns orange.") { + Row(Preferences.pointerMode ? "Double-tap ⇧ to switch to taps" : "Double-tap ⇧ to click through", + detail: Preferences.pointerMode + ? "Clicks tap until you double-tap again. The menu bar icon turns orange." + : "Clicks reach the app and the hand stays. Double-tap again for taps. The menu bar icon turns orange.") { Toggle("", isOn: model.binding({ Preferences.doubleShiftClickThrough }, { Preferences.doubleShiftClickThrough = $0 })) .toggleStyle(.switch) .labelsHidden() @@ -459,6 +475,10 @@ private struct GeneralPage: View { Toggle("", isOn: model.binding({ Preferences.launchAtLogin }, { Preferences.launchAtLogin = $0 })) .toggleStyle(.switch).labelsHidden().focusable(false) } + Row("Turn the pointer on when TinyHand opens") { + Toggle("", isOn: model.binding({ Preferences.turnOnAtLaunch }, { Preferences.turnOnAtLaunch = $0 })) + .toggleStyle(.switch).labelsHidden().focusable(false) + } if let updater = model.updater { Row("Check for updates automatically") { Toggle("", isOn: model.binding({ updater.automaticallyChecks }, { updater.automaticallyChecks = $0 })) diff --git a/Sources/TinyHand/SettingsWindow.swift b/Sources/TinyHand/SettingsWindow.swift index 801406a..8a8fa94 100644 --- a/Sources/TinyHand/SettingsWindow.swift +++ b/Sources/TinyHand/SettingsWindow.swift @@ -15,6 +15,7 @@ final class SettingsWindow: NSObject, NSWindowDelegate { var updater: Updater? { didSet { model.updater = updater } } + var onClose: (() -> Void)? private var window: NSWindow? private let model = SettingsModel() @@ -81,6 +82,7 @@ final class SettingsWindow: NSObject, NSWindowDelegate { func windowWillClose(_ notification: Notification) { onRecording?(false) + onClose?() } }