diff --git a/README.md b/README.md index bc0a8c3..24da3ac 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ open -g tinyhand://toggle | `tinyhand://clear` | Clear the drawing | | `tinyhand://style/` | Switch to a hand (or `laser`) and turn the pointer on | | `tinyhand://sound/on`, `/off`, `/toggle` | Tap sound for the current pointer | +| `tinyhand://settings` | Open Settings | While TinyHand runs, it keeps its current state in `~/Library/Application Support/TinyHand/state.json`: whether the pointer is on, the diff --git a/Sources/TinyHand/AppDelegate.swift b/Sources/TinyHand/AppDelegate.swift index e759900..8bc933e 100644 --- a/Sources/TinyHand/AppDelegate.swift +++ b/Sources/TinyHand/AppDelegate.swift @@ -15,7 +15,9 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate { private var soundItem: NSMenuItem? private var toggleHotKey: UInt32? private var clearHotKey: UInt32? - private lazy var automation = Automation(overlay: overlay) + private lazy var automation = Automation(overlay: overlay) { [weak self] in + self?.openSettings() + } private var launched = false private var pendingURLs: [URL] = [] @@ -41,7 +43,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate { self?.automation.writeState() } overlay.extraPassThrough = { [weak self] in - guard let frame = self?.statusItem.button?.window?.frame else { + guard self?.statusItem.isVisible == true, let frame = self?.statusItem.button?.window?.frame else { return [] } return [frame] @@ -68,6 +70,12 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate { pendingURLs = [] } + // Opening TinyHand while it is running shows Settings, which is the way back when the menu bar icon is hidden. + func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool { + openSettings() + return false + } + func applicationWillTerminate(_ notification: Notification) { overlay.deactivate() try? FileManager.default.removeItem(at: Automation.stateURL) @@ -101,6 +109,14 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate { menu.addItem(NSMenuItem(title: "Quit TinyHand", action: #selector(quit), keyEquivalent: "q")) menu.items.forEach { $0.target = self } menu.delegate = self + + statusItem.isVisible = Preferences.showMenuBarIcon + NotificationCenter.default.addObserver(forName: Preferences.changed, object: nil, queue: .main) { [weak self] _ in + guard let item = self?.statusItem, item.isVisible != Preferences.showMenuBarIcon else { + return + } + item.isVisible = Preferences.showMenuBarIcon + } } private func setupSettings() { @@ -210,7 +226,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate { overlay.soundOn.toggle() } - @objc private func openSettings() { + @objc func openSettings() { overlay.exit() settings.show() } diff --git a/Sources/TinyHand/Automation.swift b/Sources/TinyHand/Automation.swift index 6122f25..76b4a5a 100644 --- a/Sources/TinyHand/Automation.swift +++ b/Sources/TinyHand/Automation.swift @@ -7,9 +7,11 @@ final class Automation { .appendingPathComponent("Library/Application Support/TinyHand/state.json") private let overlay: OverlayController + private let openSettings: () -> Void - init(overlay: OverlayController) { + init(overlay: OverlayController, openSettings: @escaping () -> Void) { self.overlay = overlay + self.openSettings = openSettings NotificationCenter.default.addObserver(forName: CustomHands.changed, object: nil, queue: .main) { [weak self] _ in self?.writeState() } @@ -35,6 +37,8 @@ final class Automation { selectStyle(argument) case "sound": setSound(argument) + case "settings": + openSettings() default: Log.write("unknown url command: \(command)") } diff --git a/Sources/TinyHand/Preferences.swift b/Sources/TinyHand/Preferences.swift index 6eff8f7..3f10e75 100644 --- a/Sources/TinyHand/Preferences.swift +++ b/Sources/TinyHand/Preferences.swift @@ -149,6 +149,12 @@ enum Preferences { set { set(newValue, "showToolbar") } } + /// Whether the TinyHand icon is in the menu bar. When it is hidden, opening the app again shows Settings. + static var showMenuBarIcon: Bool { + get { defaults.object(forKey: "showMenuBarIcon") as? Bool ?? true } + set { set(newValue, "showMenuBarIcon") } + } + static var showInkButton: Bool { get { defaults.object(forKey: "showInkButton") as? Bool ?? true } set { set(newValue, "showInkButton") } @@ -187,7 +193,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", - "inkChoice", "showInkButton", "showToolbar", "tapSound"] { + "inkChoice", "showInkButton", "showToolbar", "showMenuBarIcon", "tapSound"] { defaults.removeObject(forKey: key) } NotificationCenter.default.post(name: changed, object: nil) diff --git a/Sources/TinyHand/SettingsView.swift b/Sources/TinyHand/SettingsView.swift index 4b62fc9..8c1c28b 100644 --- a/Sources/TinyHand/SettingsView.swift +++ b/Sources/TinyHand/SettingsView.swift @@ -454,6 +454,12 @@ private struct GeneralPage: View { .focusable(false) } } + Card(title: "Menu bar") { + Row("Show TinyHand in the menu bar", detail: "When it's hidden, open TinyHand again from Spotlight, Raycast or Finder to get to Settings.") { + Toggle("", isOn: model.binding({ Preferences.showMenuBarIcon }, { Preferences.showMenuBarIcon = $0 })) + .toggleStyle(.switch).labelsHidden().focusable(false) + } + } Card(title: "Startup") { Row("Open TinyHand when you log in") { Toggle("", isOn: model.binding({ Preferences.launchAtLogin }, { Preferences.launchAtLogin = $0 }))