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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ open -g tinyhand://toggle
| `tinyhand://clear` | Clear the drawing |
| `tinyhand://style/<id>` | 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
Expand Down
22 changes: 19 additions & 3 deletions Sources/TinyHand/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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] = []

Expand All @@ -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]
Expand All @@ -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)
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -210,7 +226,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
overlay.soundOn.toggle()
}

@objc private func openSettings() {
@objc func openSettings() {
overlay.exit()
settings.show()
}
Expand Down
6 changes: 5 additions & 1 deletion Sources/TinyHand/Automation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -35,6 +37,8 @@ final class Automation {
selectStyle(argument)
case "sound":
setSound(argument)
case "settings":
openSettings()
default:
Log.write("unknown url command: \(command)")
}
Expand Down
8 changes: 7 additions & 1 deletion Sources/TinyHand/Preferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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") }
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions Sources/TinyHand/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }))
Expand Down