Skip to content

armgit / cra

Public Imported from GitHub
0

The open-source Arc replacement for macOS. Spaces, pinned tabs, split view, Little Windows and Arc's shortcuts on Chromium, with your Arc sidebar imported in one go.

Tab on the empty command bar lists actions, as Arc's does (#195)

Victor390cc0bf1d64
diff --git a/Packages/CraCore/Sources/CraCore/ActionSearchScope.swift b/Packages/CraCore/Sources/CraCore/ActionSearchScope.swift
new file mode 100644
index 0000000..356be16
--- /dev/null
+++ b/Packages/CraCore/Sources/CraCore/ActionSearchScope.swift
@@ -0,0 +1,45 @@
+import Foundation
+
+/// The command bar scoped to the browser's actions, Arc's `typeTravel` scope (#195): Tab on the
+/// empty field puts an "Actions" pill before it (placeholder "Search…") and the rows become
+/// commands, each with its shortcut; typing narrows them ("spl": Add Split View, Add Top Split…).
+/// Arc 1.171 shows no "Search Actions Tab" hint for it. Backspace on the empty field and Escape
+/// leave it as they leave a site's scope.
+public enum ActionSearchScope {
+ public static let name = "Actions"
+
+ /// Actions that only open the command bar the scope is already in (New Tab…, Open Command
+ /// Bar): Arc lists neither, and run from here they'd just reopen the bar.
+ public static let reopensTheBar: Set<String> = ["newTab", "editURL"]
+
+ /// Whether the scope lists the action with this id.
+ public static func lists(actionID: String) -> Bool { !reopensTheBar.contains(actionID) }
+
+ public struct Candidate<ID: Hashable>: Equatable {
+ public let id: ID
+ /// How well the typed text names the action (0 = not at all); ignored with nothing typed.
+ public let score: Double
+ /// Times the action was run from the bar.
+ public let uses: Int
+
+ public init(id: ID, score: Double, uses: Int) {
+ self.id = id
+ self.score = score
+ self.uses = uses
+ }
+ }
+
+ /// The rows, best first. With nothing typed every action, the most used first and the rest in
+ /// the order given (the menus'). Typed, only the actions the text names, the best match first,
+ /// use counts nudging it (capped, so a habit can't bury a name typed out in full).
+ public static func rank<ID>(_ actions: [Candidate<ID>], typed: Bool) -> [ID] {
+ let kept = typed ? actions.filter { $0.score > 0 } : actions
+ func value(_ c: Candidate<ID>) -> Double {
+ typed ? c.score + Double(min(c.uses, 10)) * 2 : Double(c.uses)
+ }
+ return kept.enumerated().sorted { a, b in
+ let x = value(a.element), y = value(b.element)
+ return x != y ? x > y : a.offset < b.offset
+ }.map(\.element.id)
+ }
+}
diff --git a/Packages/CraCore/Tests/CraCoreTests/ActionSearchScopeTests.swift b/Packages/CraCore/Tests/CraCoreTests/ActionSearchScopeTests.swift
new file mode 100644
index 0000000..cd91bf0
--- /dev/null
+++ b/Packages/CraCore/Tests/CraCoreTests/ActionSearchScopeTests.swift
@@ -0,0 +1,41 @@
+import CraCore
+import Testing
+
+/// The command bar's Actions scope (#195, Arc's typeTravel): which actions it lists and in what order.
+struct ActionSearchScopeTests {
+ typealias C = ActionSearchScope.Candidate<String>
+
+ /// New Tab… and Open Command Bar would only reopen the bar; everything else is listed.
+ @Test(arguments: [("newTab", false), ("editURL", false), ("archiveTab", true), ("toggleSidebar", true)])
+ func barOpenersAreLeftOut(id: String, listed: Bool) {
+ #expect(ActionSearchScope.lists(actionID: id) == listed)
+ }
+
+ /// Nothing typed: every action, the most used first, the others in menu order.
+ @Test func nothingTypedListsByUseThenMenuOrder() {
+ let actions = [
+ C(id: "new-tab", score: 0, uses: 0),
+ C(id: "archive", score: 0, uses: 4),
+ C(id: "reload", score: 0, uses: 0),
+ C(id: "sidebar", score: 9, uses: 1),
+ ]
+ #expect(ActionSearchScope.rank(actions, typed: false) == ["archive", "sidebar", "new-tab", "reload"])
+ }
+
+ /// Typed: actions the text doesn't name drop out, the best name leads, use breaks ties.
+ @Test func typedRanksByMatchThenUse() {
+ let actions = [
+ C(id: "unrelated", score: 0, uses: 10),
+ C(id: "add-top-split", score: 68, uses: 0),
+ C(id: "add-split-view", score: 68, uses: 2),
+ C(id: "split-prefix", score: 80, uses: 0),
+ ]
+ #expect(ActionSearchScope.rank(actions, typed: true) == ["split-prefix", "add-split-view", "add-top-split"])
+ }
+
+ /// Use counts are capped: a much-used action can't outrank one the text names better by far.
+ @Test func useCannotBuryABetterName() {
+ let actions = [C(id: "habit", score: 50, uses: 500), C(id: "named", score: 80, uses: 0)]
+ #expect(ActionSearchScope.rank(actions, typed: true) == ["named", "habit"])
+ }
+}
diff --git a/Sources/Cra/Services/SuggestionEngine.swift b/Sources/Cra/Services/SuggestionEngine.swift
index 303148b..0e70871 100644
--- a/Sources/Cra/Services/SuggestionEngine.swift
+++ b/Sources/Cra/Services/SuggestionEngine.swift
@@ -74,6 +74,8 @@ final class SuggestionEngine {
var result = SuggestionResult(query: query)
if q.isEmpty {
result.items = Array(recentSuggestions(excluding: currentTabID).prefix(Self.maxRows))
+ // Tab on the empty field lists the actions (Arc's typeTravel), with no hint at the end of it.
+ result.scope = .actions
return result
}
@@ -156,6 +158,23 @@ final class SuggestionEngine {
return result
}
+ /// The bar scoped to actions (Tab on the empty field, Arc's typeTravel): commands, space
+ /// switches and moves, the most used first, narrowed by what is typed.
+ func suggestions(forActionsMatching rawQuery: String, currentTabID: UUID?) -> SuggestionResult {
+ let query = rawQuery.trimmingCharacters(in: .whitespaces)
+ let q = query.lowercased()
+ var result = SuggestionResult(query: query)
+ let rows = actionRows(q, currentTabID: currentTabID).filter { row in
+ if case .command(let id) = row.suggestion.kind { return ActionSearchScope.lists(actionID: id) }
+ return true
+ }
+ let ranked = ActionSearchScope.rank(rows.indices.map {
+ ActionSearchScope.Candidate(id: $0, score: rows[$0].suggestion.score, uses: rows[$0].uses)
+ }, typed: !q.isEmpty)
+ result.items = ranked.prefix(Self.maxRows).map { rows[$0].suggestion }
+ return result
+ }
+
// MARK: Search index
/// What scoring needs from a title and a URL. Worked out per keystroke for every tab,
@@ -343,7 +362,17 @@ final class SuggestionEngine {
private func commandSuggestions(_ q: String, currentTabID: UUID?) -> [Suggestion] {
guard q.count >= 2 else { return [] }
- var out: [Suggestion] = []
+ return actionRows(q, currentTabID: currentTabID).map { row in
+ var s = row.suggestion
+ s.score += Double(min(row.uses, 10)) * 2
+ return s
+ }
+ }
+
+ /// Commands, space switches and moves the text names (all of them with nothing typed), each
+ /// scored by its name alone, with how often it was run from the bar.
+ private func actionRows(_ q: String, currentTabID: UUID?) -> [(suggestion: Suggestion, uses: Int)] {
+ var out: [(suggestion: Suggestion, uses: Int)] = []
func nameScore(_ name: String, triggers: [String]) -> Double {
let n = name.lowercased()
if n.hasPrefix(q) { return 80 }
@@ -353,23 +382,23 @@ final class SuggestionEngine {
return 0
}
for a in bindings.actions where a.inCommandBar {
- var s = nameScore(a.name, triggers: a.triggers)
+ let s = nameScore(a.name, triggers: a.triggers)
guard s > 0 else { continue }
- s += Double(min(store.ranking.commandUsage[a.id] ?? 0, 10)) * 2
- out.append(Suggestion(id: "cmd:\(a.id)", kind: .command(a.id), title: a.name, subtitle: "Command",
- url: nil, faviconKey: nil, symbol: a.symbol ?? "command", trailing: a.primary?.display, score: s))
+ out.append((Suggestion(id: "cmd:\(a.id)", kind: .command(a.id), title: a.name, subtitle: "Command",
+ url: nil, faviconKey: nil, symbol: a.symbol ?? "command", trailing: a.primary?.display, score: s),
+ store.ranking.commandUsage[a.id] ?? 0))
}
for space in incognito ? [] : store.orderedSpaces {
let s = nameScore("Switch to \(space.title)", triggers: [space.title.lowercased()])
if s > 0 && space.id != store.focusedSpaceID {
- out.append(Suggestion(id: "space:\(space.id)", kind: .switchSpace(space.id), title: "Switch to \(space.title)",
- subtitle: "Space", url: nil, faviconKey: nil, symbol: nil, trailing: space.emoji, score: s))
+ out.append((Suggestion(id: "space:\(space.id)", kind: .switchSpace(space.id), title: "Switch to \(space.title)",
+ subtitle: "Space", url: nil, faviconKey: nil, symbol: nil, trailing: space.emoji, score: s), 0))
}
if currentTabID != nil, space.id != store.focusedSpaceID {
let m = nameScore("Move Tab to \(space.title)", triggers: ["move"])
if m > 0 {
- out.append(Suggestion(id: "move:\(space.id)", kind: .moveToSpace(space.id), title: "Move Tab to \(space.title)",
- subtitle: "Space", url: nil, faviconKey: nil, symbol: "arrow.right.square", trailing: space.emoji, score: m - 5))
+ out.append((Suggestion(id: "move:\(space.id)", kind: .moveToSpace(space.id), title: "Move Tab to \(space.title)",
+ subtitle: "Space", url: nil, faviconKey: nil, symbol: "arrow.right.square", trailing: space.emoji, score: m - 5), 0))
}
}
}
@@ -378,8 +407,8 @@ final class SuggestionEngine {
let name = f.title ?? "Folder"
let m = nameScore("Move Tab to Folder \(name)", triggers: [name.lowercased()])
if m > 0 {
- out.append(Suggestion(id: "folder:\(f.id)", kind: .moveToFolder(f.id), title: "Move Tab to \(name)",
- subtitle: "Folder", url: nil, faviconKey: nil, symbol: "folder", trailing: nil, score: m - 5))
+ out.append((Suggestion(id: "folder:\(f.id)", kind: .moveToFolder(f.id), title: "Move Tab to \(name)",
+ subtitle: "Folder", url: nil, faviconKey: nil, symbol: "folder", trailing: nil, score: m - 5), 0))
}
}
}
@@ -525,11 +554,14 @@ enum CommandBarScope: Equatable {
case site(SiteSearch)
/// The open tabs of the window's space (#49).
case tabs
+ /// Arc's typeTravel: Tab on the empty field lists the browser's actions (#195).
+ case actions
var name: String {
switch self {
case .site(let site): return site.name
case .tabs: return TabSearchScope.name
+ case .actions: return ActionSearchScope.name
}
}
}
diff --git a/Sources/Cra/UI/CommandBar/CommandBarController.swift b/Sources/Cra/UI/CommandBar/CommandBarController.swift
index 440feb0..35809ba 100644
--- a/Sources/Cra/UI/CommandBar/CommandBarController.swift
+++ b/Sources/Cra/UI/CommandBar/CommandBarController.swift
@@ -29,7 +29,8 @@ final class CommandBarController {
var focusedSpaceID: UUID?
var spaceName: (UUID) -> String = { _ in "" }
/// Arc's scoped search: Tab on "youtube" scopes the bar to YouTube, on "%" to the space's open
- /// tabs (a pill before the field) until Backspace on an empty field or Escape.
+ /// tabs, on the empty field to the browser's actions (a pill before the field) until Backspace
+ /// on an empty field or Escape.
private(set) var scope: CommandBarScope?
/// What was typed when Tab entered the scope; Backspace puts it back.
private var scopeTyped = ""
@@ -91,6 +92,8 @@ final class CommandBarController {
result = engine.suggestions(for: newQuery, in: site)
case .tabs:
result = engine.suggestions(for: newQuery, inTabsOf: focusedSpaceID ?? store.focusedSpaceID, currentTabID: store.selectedTabID)
+ case .actions:
+ result = engine.suggestions(forActionsMatching: newQuery, currentTabID: store.selectedTabID)
}
selectedIndex = 0
previewText = nil
@@ -150,7 +153,7 @@ final class CommandBarController {
}
}
- /// Tab on text that starts a site search's host, or on "%": scope the bar to it.
+ /// Tab on text that starts a site search's host, on "%", or on the empty field: scope the bar to it.
func enterScope() -> Bool {
guard scope == nil, previewText == nil, let offered = result.scope else { return false }
scopeTyped = query
@@ -215,8 +218,8 @@ final class CommandBarController {
private func committed() -> (Suggestion?, String)? {
let selected = result.items.indices.contains(selectedIndex) ? result.items[selectedIndex] : nil
- // No tab matched: Return has nowhere to go, and the tabs scope never searches the web.
- if scope == .tabs, selected == nil { return nil }
+ // No tab or action matched: Return has nowhere to go, and neither scope searches the web.
+ if scope == .tabs || scope == .actions, selected == nil { return nil }
var typed = query
// Enter on the typed row with an inline completion goes to the completion.
if selectedIndex == 0, let c = result.inlineCompletion, selected?.id == "typed" { typed = c }
diff --git a/Sources/Cra/UI/CommandBar/CommandBarView.swift b/Sources/Cra/UI/CommandBar/CommandBarView.swift
index e09520f..73c6ea6 100644
--- a/Sources/Cra/UI/CommandBar/CommandBarView.swift
+++ b/Sources/Cra/UI/CommandBar/CommandBarView.swift
@@ -134,7 +134,8 @@ final class CommandBarView: NSView {
pill.layer?.shadowColor = color.cgColor
pillLabel.stringValue = scope.name
}
- let offered = scope == nil && c.previewText == nil ? c.result.scope : nil
+ // Arc shows no hint for the actions Tab offers on the empty field.
+ let offered = scope == nil && c.previewText == nil && c.result.scope != .actions ? c.result.scope : nil
hint.isHidden = offered == nil
keycap.isHidden = offered == nil
if let offered {
@@ -149,8 +150,9 @@ final class CommandBarView: NSView {
let row = previewing ? items[c.selectedIndex] : items.first
let typed = c.query.trimmingCharacters(in: .whitespaces)
let glyph = CommandBarGlyph.resolve(
- editingURL: c.mode == .editURL, untouched: !previewing && (typed.isEmpty || typed == c.openedPrefill),
- scoped: scope != nil, pageFaviconKey: c.openedPrefill.flatMap(FaviconKey.key(for:)),
+ editingURL: c.mode == .editURL && scope == nil, untouched: !previewing && (typed.isEmpty || typed == c.openedPrefill),
+ // Actions run the row, not a search: Arc shows the row's glyph once something is typed.
+ scoped: scope != nil && scope != .actions, pageFaviconKey: c.openedPrefill.flatMap(FaviconKey.key(for:)),
row: row.map { ($0.faviconKey, $0.symbol) }, hasFavicon: { [favicons] in favicons.image(forKey: $0) != nil })
if case .favicon(let key) = glyph, let img = favicons.image(forKey: key) {
leadingIcon.imageScaling = .scaleProportionallyUpOrDown
diff --git a/docs/arc-reverse-engineering.md b/docs/arc-reverse-engineering.md
index 5b85a1e..fd501f5 100644
--- a/docs/arc-reverse-engineering.md
+++ b/docs/arc-reverse-engineering.md
@@ -89,6 +89,14 @@ Legend: [O] observed in shipped artifact or data file, [I] inference, [U] unknow
`siteSearchSpaceTriggerEnabled` show Space was a trigger once and Tab is the only one now. Cra's "%" tab scope
(#49, Zen's idea) reuses all of it: "%" offers "Search Tabs Tab", the pill takes the space's colour (Arc has no
colour for a scope that isn't a site), rows are the bar's own tab rows. Cra: TabSearchScope, CommandBarScope.
+- [O] Actions scope (6 Nov, Arc 1.171, ⌘T then Tab posted to the main window, AX + window captures; GitHub #195):
+ Tab on the EMPTY field enters `typeTravel` ("title for scoped search of browser actions in Arc"): an "Actions"
+ pill in the space's colour before the field, placeholder "Search…", no "Search … Tab" hint before it. Rows are
+ actions only, each with its shortcut (Archive Tab ⌘W, an extension, Close Window ⇧⌘W, Hide Sidebar ⌘S, Next Tab
+ ⌥⌘↓: recently used, from typeTravelUsage); typing narrows them ("spl": Add Split View, Add Top/Left/Right/Bottom
+ Split) and the glyph before the field becomes the row's. Backspace on the empty field leaves it, Escape returns
+ to how the bar opened, a second Escape closes. Neither New Tab nor the command bar itself is listed. Cra:
+ ActionSearchScope, CommandBarScope.actions (most used first, then menu order: Cra keeps counts, not times).
- [O] Entering and leaving a scope (3 Nov, Arc 1.171, disassembly of `CommandBar.CommandBarContainer` + a 120 fps
recording of Little Arc, "youtube" Tab): the container's layer gets `arc_commandBarBounce`, a CASpringAnimation on
`transform` toward scale 0.95 about the centre (damping ratio 0.9, response 0.7: stiffness ≈ 80.57, damping ≈ 16.16,