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.

Sidebar: on hover a tab's name runs to 13 pt before the close button, as in Arc (#243)

Victor12ab4cfd099c
diff --git a/Packages/CraCore/Sources/CraCore/TabRowTrailing.swift b/Packages/CraCore/Sources/CraCore/TabRowTrailing.swift
new file mode 100644
index 0000000..d02c1ea
--- /dev/null
+++ b/Packages/CraCore/Sources/CraCore/TabRowTrailing.swift
@@ -0,0 +1,53 @@
+import CoreGraphics
+
+/// The trailing end of a tab's sidebar row: its controls (the capture dot, a peek badge, the unread
+/// dot, the close button on hover) laid out right to left from the fill's end, and the width that
+/// leaves the name (and the subtitle under it, which shares it).
+///
+/// Arc 1.171 (GitHub #139): the close button is a 24 pt square ending 6 pt before the fill's end; the
+/// unread dot and a peek badge sit centred where it shows. Two controls are 11 pt apart.
+public enum TabRowTrailing {
+ /// The last control's slot ends this far before the row's end (the fill's end).
+ public static let endInset: CGFloat = 6
+ /// Between two trailing controls.
+ public static let controlGap: CGFloat = 11
+ /// The name's box (before `SidebarText.inset` widens the label) to the first control: 13 pt of
+ /// air from the label's box, as in Arc (GitHub #243). It was 26, a gap kept from when a 22 pt
+ /// "back to pinned page" button also sat before the close button.
+ public static let nameGap: CGFloat = 15
+ /// The name's box to the row's end with no control shown.
+ public static let restingTail: CGFloat = 21
+
+ public struct Control: Equatable, Sendable {
+ /// The control's own width.
+ public var width: CGFloat
+ /// The width it's centred in: its own, or the close button's for a dot or badge that sits
+ /// where the button shows on hover.
+ public var slot: CGFloat
+
+ public init(width: CGFloat, slot: CGFloat? = nil) {
+ self.width = width
+ self.slot = slot ?? width
+ }
+ }
+
+ public struct Layout: Equatable, Sendable {
+ /// Each control's minimum x, in the order given (leading to trailing).
+ public var controlX: [CGFloat]
+ /// The name's width from `textX`, never negative.
+ public var textWidth: CGFloat
+ }
+
+ /// `controls` leading to trailing; `textX` is where the name starts, in the row's coordinates.
+ public static func layout(rowWidth: CGFloat, textX: CGFloat, controls: [Control]) -> Layout {
+ var right = rowWidth - endInset
+ var xs = [CGFloat](repeating: 0, count: controls.count)
+ for i in controls.indices.reversed() {
+ let c = controls[i]
+ xs[i] = right - c.slot / 2 - c.width / 2
+ right = xs[i] - controlGap
+ }
+ let textEnd = xs.first.map { $0 - nameGap } ?? rowWidth - restingTail
+ return Layout(controlX: xs, textWidth: max(0, textEnd - textX))
+ }
+}
diff --git a/Packages/CraCore/Tests/CraCoreTests/TabRowTrailingTests.swift b/Packages/CraCore/Tests/CraCoreTests/TabRowTrailingTests.swift
new file mode 100644
index 0000000..998a8a7
--- /dev/null
+++ b/Packages/CraCore/Tests/CraCoreTests/TabRowTrailingTests.swift
@@ -0,0 +1,61 @@
+import CoreGraphics
+import CraCore
+import Testing
+
+struct TabRowTrailingTests {
+ // A 228.5 pt row (Arc's fill at a ≈236 pt sidebar, #243) whose name starts 46 pt in.
+ static let width: CGFloat = 228.5
+ static let textX: CGFloat = 46
+ static let close = TabRowTrailing.Control(width: 24)
+
+ /// On hover, the name runs to 15 pt before the close button (13 pt from the label's widened box),
+ /// as in Arc; it stopped 26 pt short, about two characters before Arc's (#243). The button stays
+ /// 6 pt from the row's end.
+ @Test(.bug("https://github.com/VictorKildahl/cra/issues/243"))
+ func hoveredNameRunsToTheCloseButton() {
+ let l = TabRowTrailing.layout(rowWidth: Self.width, textX: Self.textX, controls: [Self.close])
+ #expect(l.controlX == [Self.width - 6 - 24])
+ #expect(Self.textX + l.textWidth == l.controlX[0] - 15)
+ }
+
+ /// At rest, with nothing at the row's end, the name keeps its 21 pt tail (#243: Arc's rows match
+ /// there).
+ @Test(.bug("https://github.com/VictorKildahl/cra/issues/243"))
+ func restingTailIsUnchanged() {
+ let l = TabRowTrailing.layout(rowWidth: Self.width, textX: Self.textX, controls: [])
+ #expect(Self.textX + l.textWidth == Self.width - 21)
+ }
+
+ /// The unread dot and a peek badge sit centred where the close button shows, and the name stops
+ /// the same 15 pt before them.
+ @Test func dotSitsInTheCloseButtonsSlot() {
+ let dot = TabRowTrailing.Control(width: 6, slot: 24)
+ let l = TabRowTrailing.layout(rowWidth: Self.width, textX: Self.textX, controls: [dot])
+ #expect(l.controlX[0] + 3 == Self.width - 6 - 12)
+ #expect(Self.textX + l.textWidth == l.controlX[0] - TabRowTrailing.nameGap)
+ }
+
+ /// Several controls stay at least 11 pt apart (more where one sits centred in a wider slot), the
+ /// last in its place, the name 15 pt before the first:
+ /// on hover the capture dot and the close button, at rest the capture dot, a peek badge and the
+ /// unread dot (the badge and the dot give way to the button on hover).
+ @Test(arguments: [
+ [TabRowTrailing.Control(width: 8), TabRowTrailingTests.close],
+ [TabRowTrailing.Control(width: 8), TabRowTrailing.Control(width: 16, slot: 24), TabRowTrailing.Control(width: 6, slot: 24)],
+ ])
+ func controlsDoNotOverlap(_ controls: [TabRowTrailing.Control]) {
+ let l = TabRowTrailing.layout(rowWidth: Self.width, textX: Self.textX, controls: controls)
+ let last = controls[controls.count - 1]
+ #expect(l.controlX.last! + last.width / 2 == Self.width - 6 - last.slot / 2)
+ for i in 1..<controls.count {
+ #expect(l.controlX[i] - (l.controlX[i - 1] + controls[i - 1].width) >= 11)
+ }
+ #expect(Self.textX + l.textWidth == l.controlX[0] - 15)
+ }
+
+ /// A row too narrow for its name gives it no width rather than a negative one.
+ @Test func narrowRowGivesNoWidth() {
+ let l = TabRowTrailing.layout(rowWidth: 60, textX: Self.textX, controls: [Self.close])
+ #expect(l.textWidth == 0)
+ }
+}
diff --git a/Sources/Cra/App/AppDelegate.swift b/Sources/Cra/App/AppDelegate.swift
index d8b334a..177c287 100644
--- a/Sources/Cra/App/AppDelegate.swift
+++ b/Sources/Cra/App/AppDelegate.swift
@@ -938,6 +938,16 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuItemValidation {
}
if let v = dict["fullscreen"], let w = main.window, (v == "1") != w.styleMask.contains(.fullScreen) { w.toggleFullScreen(nil); return }
if let spec = dict["snapshot"], let out = dict["out"] { ViewSnapshot.write(spec, in: main.window, to: out); return }
+ // hover=TabRowView,1: that sidebar row hovered as if the pointer were on it (snapshots of
+ // hover-only controls, #243) until the pointer moves or the sidebar changes.
+ if let spec = dict["hover"] {
+ guard let row = ViewSnapshot.find(spec, in: main.window) as? SidebarRowView else {
+ CraLog.note("hover: no visible \(spec)")
+ return
+ }
+ row.list?.debugHover(row)
+ return
+ }
if let size = dict["windowSize"]?.split(separator: "x").compactMap({ Double($0) }), size.count == 2, let w = main.window {
w.setFrame(NSRect(x: w.frame.minX, y: w.frame.maxY - size[1], width: size[0], height: size[1]), display: true)
return
diff --git a/Sources/Cra/Services/ViewSnapshot.swift b/Sources/Cra/Services/ViewSnapshot.swift
index 3c8ffdd..ea1d003 100644
--- a/Sources/Cra/Services/ViewSnapshot.swift
+++ b/Sources/Cra/Services/ViewSnapshot.swift
@@ -8,12 +8,12 @@ enum ViewSnapshot {
/// "PaneToolbarView" or "PaneToolbarView,1": the first (or nth) view of that class on screen
/// in the window, left to right and top to bottom (the pager keeps other spaces' columns
/// laid out beside the visible one).
- static func write(_ spec: String, in window: NSWindow?, to path: String) {
+ static func find(_ spec: String, in window: NSWindow?) -> NSView? {
let parts = spec.split(separator: ",").map(String.init)
- guard let root = window?.contentView?.superview ?? window?.contentView else { return }
+ guard let name = parts.first, let root = window?.contentView?.superview ?? window?.contentView else { return nil }
var found: [NSView] = []
func walk(_ v: NSView) {
- if String(describing: type(of: v)) == parts[0], !v.isHiddenOrHasHiddenAncestor, !v.visibleRect.isEmpty,
+ if String(describing: type(of: v)) == name, !v.isHiddenOrHasHiddenAncestor, !v.visibleRect.isEmpty,
v.convert(v.bounds, to: nil).intersects(root.bounds) { found.append(v) }
v.subviews.forEach(walk)
}
@@ -23,7 +23,12 @@ enum ViewSnapshot {
return (a.minX, -a.maxY) < (b.minX, -b.maxY)
}
let index = parts.count > 1 ? Int(parts[1]) ?? 0 : 0
- guard index < found.count, let png = render(found[index]) else {
+ return index < found.count ? found[index] : nil
+ }
+
+ /// The view `find` picks, drawn to a PNG at `path`.
+ static func write(_ spec: String, in window: NSWindow?, to path: String) {
+ guard let view = find(spec, in: window), let png = render(view) else {
CraLog.note("snapshot: no visible \(spec)")
return
}
diff --git a/Sources/Cra/UI/Sidebar/List/SidebarRowViews.swift b/Sources/Cra/UI/Sidebar/List/SidebarRowViews.swift
index fd41ddf..bc26958 100644
--- a/Sources/Cra/UI/Sidebar/List/SidebarRowViews.swift
+++ b/Sources/Cra/UI/Sidebar/List/SidebarRowViews.swift
@@ -1213,17 +1213,16 @@ final class TabRowView: SidebarRowView, ArcTooltipTarget, NSTextFieldDelegate {
soundButton.frame = NSRect(x: x - (s.width - Self.soundGlyphWidth) / 2, y: (h - s.height) / 2, width: s.width, height: s.height)
x += Self.soundGlyphWidth + 8
}
- // HStack(spacing: 11) with a Spacer(minLength: 4) between the text and the trailing items.
- var right = bounds.width - 6
- for v in accessories.reversed() {
- // The unread dot is centred where the close button shows on hover, which is also
- // under a live folder's refresh button (FolderRowView, Arc lines them up too).
+ // The unread dot and a peek badge are centred where the close button shows on hover, which
+ // is also under a live folder's refresh button (FolderRowView, Arc lines them up too).
+ let trailing = TabRowTrailing.layout(rowWidth: bounds.width, textX: x, controls: accessories.map {
+ TabRowTrailing.Control(width: $0.frame.width, slot: $0 === unreadDot || $0 === peekBadge ? Self.closeSize : nil)
+ })
+ for (v, vx) in zip(accessories, trailing.controlX) {
let s = v.frame.size
- let slot = v === unreadDot || v === peekBadge ? Self.closeSize : s.width
- v.frame = NSRect(x: right - slot / 2 - s.width / 2, y: (h - s.height) / 2, width: s.width, height: s.height)
- right = v.frame.minX - 11
+ v.frame = NSRect(x: vx, y: (h - s.height) / 2, width: s.width, height: s.height)
}
- let textWidth = max(0, right - 4 - 11 - x)
+ let textWidth = trailing.textWidth
let th = titleLabel.intrinsicContentSize.height
if shownSubtitle != nil {
let sh = subtitleLabel.intrinsicContentSize.height
diff --git a/Sources/Cra/UI/Sidebar/List/SpaceListView.swift b/Sources/Cra/UI/Sidebar/List/SpaceListView.swift
index 2df4465..16b5800 100644
--- a/Sources/Cra/UI/Sidebar/List/SpaceListView.swift
+++ b/Sources/Cra/UI/Sidebar/List/SpaceListView.swift
@@ -819,6 +819,12 @@ final class SpaceListView: NSView, NSDraggingSource {
hoverAt(inside && document.isMouseInside ? p : nil)
}
+ /// Scenarios (cra://debug?hover=): `row` takes the hover as if the pointer were on its middle.
+ /// The pointer's next move, or the list's next sync, puts it back where the pointer is.
+ func debugHover(_ row: SidebarRowView) {
+ hoverAt(row.convert(NSPoint(x: row.bounds.midX, y: row.bounds.midY), to: nil))
+ }
+
private func hoverAt(_ windowPoint: NSPoint?) {
var target: SidebarRowView?
var local = NSPoint.zero
diff --git a/Tests/Scenarios/component-snapshots.scenario b/Tests/Scenarios/component-snapshots.scenario
index a26b942..5646e83 100644
--- a/Tests/Scenarios/component-snapshots.scenario
+++ b/Tests/Scenarios/component-snapshots.scenario
@@ -47,3 +47,10 @@ snapshot SplitRowView split-row
cra://action/renameTab # from a pane, the split's name is edited in its row
wait 1
snapshot SplitRowView split-row-renaming
+# A long name at rest keeps a 21 pt tail; on hover it runs to 13 pt before the close button, as in
+# Arc, where it stopped 24 pt before it (#243).
+cra://open?url=${PAGE}?n=with%20a%20name%20long%20enough%20to%20run%20under%20the%20close%20button
+wait 3
+snapshot TabRowView,0 today-tab-row-long
+cra://debug?hover=TabRowView,0
+snapshot TabRowView,0 today-tab-row-long-hover
diff --git a/Tests/Snapshots/today-tab-row-long-hover.png b/Tests/Snapshots/today-tab-row-long-hover.png
new file mode 100644
index 0000000..671783c
Binary files /dev/null and b/Tests/Snapshots/today-tab-row-long-hover.png differ
diff --git a/Tests/Snapshots/today-tab-row-long.png b/Tests/Snapshots/today-tab-row-long.png
new file mode 100644
index 0000000..2b14ec9
Binary files /dev/null and b/Tests/Snapshots/today-tab-row-long.png differ
diff --git a/Tests/promises.lock b/Tests/promises.lock
index ee7c24b..29f3312 100644
--- a/Tests/promises.lock
+++ b/Tests/promises.lock
@@ -364,3 +364,5 @@
#233 Packages/CraCore/Tests/CraCoreTests/MouseButtonSpaceSwitchTests.swift::backGoesToThePreviousSpaceAndForwardToTheNext 10b4dc8a4a65
#233 Packages/CraCore/Tests/CraCoreTests/MouseButtonSpaceSwitchTests.swift::otherButtonsDoNotSwitch 07b725a8e6f5
#233 Tests/Scenarios/mouse-buttons-switch-spaces.scenario 79148e8b3ebb
+#243 Packages/CraCore/Tests/CraCoreTests/TabRowTrailingTests.swift::hoveredNameRunsToTheCloseButton 0a8658ede567
+#243 Packages/CraCore/Tests/CraCoreTests/TabRowTrailingTests.swift::restingTailIsUnchanged f479053f4258
diff --git a/docs/arc-reverse-engineering.md b/docs/arc-reverse-engineering.md
index c6e111e..dda17af 100644
--- a/docs/arc-reverse-engineering.md
+++ b/docs/arc-reverse-engineering.md
@@ -1214,6 +1214,11 @@ so test pages must not use it). Live HID clicks and hovers at 1x, plus a static
| Today split, open | an ✕ inside every pane | Archive this item | that pane is archived, the split becomes a tab |
| Today split, background | one ✕ at the row's end | Archive this item | the whole split is archived |
+- [O reporter, 2x screenshots, GitHub #243] With the button shown, a long name's ink ends 20.5 pt before the ✕'s
+ ink (✕ centred 18 pt from the fill's end), so its box ends ~13.5 pt before the button. At rest the rows look the
+ same as Cra's 21 pt tail (not measured). Cra (`TabRowTrailing`): the name's box 15 pt before the first trailing
+ control (13 pt from the label's widened box), 21 pt before the row's end with none; it was 26 pt, kept from the
+ old trailing ↩ button.
- [O live] ⌘W on several picked tabs closes them all; [O static] a Today tab among them archives every one,
otherwise they all deselect. The ⌘W keycaps only show on the open row's tooltip.
- [O live] Split cells: 4 pt inside the fill, 4 pt apart, each favicon 6 pt into its cell. An open split draws