Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/526aa585.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
bump: patch
---

Type an app's first letter to select it in the overlay; repeat to cycle matches.
4 changes: 3 additions & 1 deletion Sources/cmdcmd/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ struct Config: Codable {
var bindings: [String: Action]
var livePreviews: Bool?
var displayMode: DisplayMode?
var letterJump: Bool?

var triggerSpec: String { trigger ?? "cmd-cmd" }
var livePreviewsEnabled: Bool { livePreviews ?? true }
var displayModeOrDefault: DisplayMode { displayMode ?? .dock }
var letterJumpEnabled: Bool { letterJump ?? true }

static let `default` = Config(animations: true, trigger: nil, bindings: [:], livePreviews: nil, displayMode: nil)
static let `default` = Config(animations: true, trigger: nil, bindings: [:], livePreviews: nil, displayMode: nil, letterJump: nil)

static var fileURL: URL {
URL(fileURLWithPath: NSHomeDirectory())
Expand Down
18 changes: 18 additions & 0 deletions Sources/cmdcmd/Overlay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ final class Overlay {
private var prevPickedWindowID: CGWindowID?
private var showIgnored: Bool = false
private var dragState: DragState?
private var lastLetterJump: String?
private let tracker: SpaceTracker
private var config: Config

Expand Down Expand Up @@ -397,6 +398,21 @@ private static func windowMostlyOn(displayBounds: CGRect, window: SCWindow) -> B
updateHint()
}

private func selectApp(startingWith letter: String) {
guard config.letterJumpEnabled, !tiles.isEmpty else { return }
let needle = letter.lowercased()
let start = lastLetterJump == needle ? selectedIndex + 1 : 0
let order = Array(start..<tiles.count) + Array(0..<min(start, tiles.count))
guard let match = order.first(where: { idx in
(tiles[idx].scWindow.owningApplication?.applicationName ?? "")
.lowercased()
.hasPrefix(needle)
}) else { return }
lastLetterJump = needle
selectedIndex = match
updateSelection()
}

private func dispatch(_ action: Action) {
switch action {
case .pick: pick()
Expand Down Expand Up @@ -507,6 +523,7 @@ private static func windowMostlyOn(displayBounds: CGRect, window: SCWindow) -> B
allTiles = []
selectedIndex = 0
showIgnored = false
lastLetterJump = nil
view?.resetMomentaryPeek()
hint.hide()
Task(priority: .utility) {
Expand Down Expand Up @@ -852,6 +869,7 @@ private static func windowMostlyOn(displayBounds: CGRect, window: SCWindow) -> B
v.onMouseDown = { [weak self] p in self?.mouseDownAt(p) }
v.onMouseDragged = { [weak self] p in self?.mouseDraggedAt(p) }
v.onMouseUp = { [weak self] p in self?.mouseUpAt(p) }
v.onLetter = { [weak self] letter in self?.selectApp(startingWith: letter) }
w.contentView = v
view = v
return w
Expand Down
10 changes: 10 additions & 0 deletions Sources/cmdcmd/OverlayView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ final class OverlayView: NSView {
var onMouseDown: ((NSPoint) -> Void)?
var onMouseDragged: ((NSPoint) -> Void)?
var onMouseUp: ((NSPoint) -> Void)?
var onLetter: ((String) -> Void)?
private var momentaryPeek = false

override var acceptsFirstResponder: Bool { true }
Expand All @@ -25,6 +26,15 @@ final class OverlayView: NSView {
onSpaceDown?()
return
}
if bareMods == [.control],
let onLetter,
let chars = event.charactersIgnoringModifiers?.lowercased(),
chars.count == 1,
let scalar = chars.unicodeScalars.first,
CharacterSet.lowercaseLetters.contains(scalar) {
onLetter(chars)
return
}
if let action = keymap.action(for: event) {
onAction?(action)
return
Expand Down
18 changes: 16 additions & 2 deletions Sources/cmdcmd/SettingsWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class SettingsWindowController: NSWindowController {
init(config: Config) {
model = SettingsModel(config: config)
let window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 460, height: 400),
contentRect: NSRect(x: 0, y: 0, width: 460, height: 460),
styleMask: [.titled, .closable, .miniaturizable],
backing: .buffered,
defer: false
Expand All @@ -30,6 +30,7 @@ private final class SettingsModel: ObservableObject {
@Published var animations: Bool { didSet { save() } }
@Published var livePreviews: Bool { didSet { save() } }
@Published var displayMode: DisplayMode { didSet { save() } }
@Published var letterJump: Bool { didSet { save() } }
private var base: Config
@Published var status: String = ""
var onSave: ((Config) -> Void)?
Expand All @@ -38,6 +39,7 @@ private final class SettingsModel: ObservableObject {
animations = config.animations
livePreviews = config.livePreviewsEnabled
displayMode = config.displayModeOrDefault
letterJump = config.letterJumpEnabled
base = config
}

Expand All @@ -46,11 +48,13 @@ private final class SettingsModel: ObservableObject {
config.animations = animations
config.livePreviews = livePreviews
config.displayMode = displayMode
config.letterJump = letterJump
do {
try Config.patchOnDisk([
("animations", animations ? "true" : "false"),
("livePreviews", livePreviews ? "true" : "false"),
("displayMode", "\"\(displayMode.rawValue)\""),
("letterJump", letterJump ? "true" : "false"),
])
base = config
onSave?(config)
Expand Down Expand Up @@ -100,6 +104,16 @@ private struct SettingsRootView: View {
}
.toggleStyle(.switch)

Toggle(isOn: $model.letterJump) {
VStack(alignment: .leading, spacing: 2) {
Text("First-letter app jump").font(.system(size: 13, weight: .medium))
Text("Hold ⌃ (Control) + an app's first letter to select it; repeat to cycle matches.")
.font(.caption)
.foregroundStyle(.secondary)
}
}
.toggleStyle(.switch)

VStack(alignment: .leading, spacing: 6) {
Text("Show app in").font(.system(size: 13, weight: .medium))
Picker("", selection: $model.displayMode) {
Expand All @@ -126,6 +140,6 @@ private struct SettingsRootView: View {
}
}
.padding(24)
.frame(minWidth: 420, minHeight: 360)
.frame(minWidth: 420, minHeight: 420)
}
}
Loading