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.
Rename a split view as in Arc: the row shows a pane count and the name (#179)
jake-dev-99290a40a4032e
diff --git a/Sources/Cra/App/AppDelegate.swift b/Sources/Cra/App/AppDelegate.swiftindex 52cf224..462ac06 100644--- a/Sources/Cra/App/AppDelegate.swift+++ b/Sources/Cra/App/AppDelegate.swift@@ -1013,7 +1013,8 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuItemValidation {case "pinTab": if let c = current { store.togglePinned(c) }case "favoriteTab":if let c = current { store.isFavorite(c) ? store.removeFromFavorites(c) : store.addToFavorites(c) }- case "renameTab": main.sidebarUI.renamingID = current+ // A pane has no row of its own: its split's name is edited.+ case "renameTab": main.sidebarUI.renamingID = current.map { store.splitContaining($0)?.id ?? $0 }case "duplicateTab": if let c = current { store.duplicateTab(c) }case "resetTab": if let c = current { main.resetPinnedTab(c) }case "muteTab":diff --git a/Sources/Cra/UI/Sidebar/List/SidebarMenus.swift b/Sources/Cra/UI/Sidebar/List/SidebarMenus.swiftindex 439cfa5..7b0e83d 100644--- a/Sources/Cra/UI/Sidebar/List/SidebarMenus.swift+++ b/Sources/Cra/UI/Sidebar/List/SidebarMenus.swift@@ -255,6 +255,8 @@ enum SidebarMenus {paneItems(submenu(menu, c.title), c.id, store: store, actions: actions)}menu.addItem(.separator())+ // Arc's way back to the panes from a renamed split.+ if store.item(id)?.title != nil { add(menu, "Reset Name and Icon") { store.rename(id, to: nil) } }add(menu, "Rename…") { ui.renamingID = id }menu.addItem(.separator())let spaces = submenu(menu, "Move to")diff --git a/Sources/Cra/UI/Sidebar/List/SidebarRowViews.swift b/Sources/Cra/UI/Sidebar/List/SidebarRowViews.swiftindex d904639..3f59b48 100644--- a/Sources/Cra/UI/Sidebar/List/SidebarRowViews.swift+++ b/Sources/Cra/UI/Sidebar/List/SidebarRowViews.swift@@ -16,6 +16,7 @@ struct SidebarColors: Equatable {var selectedFill = NSColor.clearvar separator = NSColor.separatorColorvar pillFill = NSColor.clear+ var countBadgeFill = NSColor.clearinit() {}@@ -28,6 +29,7 @@ struct SidebarColors: Equatable {selectedFill = NSColor(p.selectedFill)separator = NSColor(p.separator)pillFill = NSColor(p.pillFill)+ countBadgeFill = NSColor(p.countBadgeFill)}/// SidebarIconButton's glyph tone outside the header (no active state).@@ -295,7 +297,9 @@ final class SidebarGlyphButton: NSView {override var mouseDownCanMoveWindow: Bool { false }override func acceptsFirstMouse(for event: NSEvent?) -> Bool { takesClickFromChildWindow }- override func hitTest(_ point: NSPoint) -> NSView? { frame.contains(point) ? self : nil }+ // Rows hide their buttons rather than remove them: a hidden one must not take the row's+ // clicks or run the action it had when it last showed (AppKit's hitTest skips hidden views).+ override func hitTest(_ point: NSPoint) -> NSView? { !isHiddenOrHasHiddenAncestor && frame.contains(point) ? self : nil }override func updateTrackingAreas() {super.updateTrackingAreas()@@ -1535,22 +1539,32 @@ struct SplitRowModel: Equatable {var id: UUIDvar depth: Intvar today: Bool+ /// The name given with Rename…; the row shows it in place of the panes.+ var title: String?+ var renameText: String+ var editing: Boolvar selected: Bool/// Filled as the open row; not while the ⌘T bar's New Tab row stands in for it.var highlighted: Boolvar focusRing: Boolvar cells: [Cell]++ /// Arc's customInfo layout for a renamed split: the pane count and the name, one row.+ var named: Bool { title != nil || editing }}/// Arc's split cell (SplitViewCellView): the panes side by side, hairlines between them, each/// pane a pill while the split is open. Its close buttons follow SidebarClose.split: one at the/// row's end for a pinned split or a Today split in the background, one in every pane of an-/// open Today split (GitHub #139).-final class SplitRowView: SidebarRowView {+/// open Today split (GitHub #139). A renamed split shows its name instead of the panes.+final class SplitRowView: SidebarRowView, NSTextFieldDelegate, ArcTooltipTarget {private(set) var model: SplitRowModel?private var cells: [SplitCellView] = []private var separators: [NSView] = []private var focusRing: FocusRingLayer?+ private let countBadge = SplitCountBadgeView()+ private let titleLabel = NSTextField.sidebarLabel(size: 14)+ private var renameField: NSTextField?private lazy var closeButton: SidebarGlyphButton = {let b = SidebarGlyphButton(symbol: "xmark", size: SplitRowMetrics.button, iconSize: 13, help: "", colors: colors) {}b.setCloseMark(minus: false, help: "")@@ -1560,8 +1574,17 @@ final class SplitRowView: SidebarRowView {}()private var shownButtons: SidebarClose.SplitButtons?+ override init(key: SpaceListView.RowKey, list: SpaceListView, colors: SidebarColors) {+ super.init(key: key, list: list, colors: colors)+ addSubview(countBadge)+ addSubview(titleLabel)+ }++ required init?(coder: NSCoder) { fatalError() }+func apply(_ m: SplitRowModel) {guard m != model else { return }+ if m.named != model?.named { needsLayout = true }model = mif cells.map(\.cellID) != m.cells.map(\.id) {cells.forEach { $0.removeFromSuperview() }@@ -1575,11 +1598,20 @@ final class SplitRowView: SidebarRowView {cells.forEach { addSubview($0) }separators.forEach { addSubview($0) }}- for (c, cm) in zip(cells, m.cells) { c.apply(cm, colors: colors, pill: m.highlighted) }+ for (c, cm) in zip(cells, m.cells) {+ c.apply(cm, colors: colors, pill: m.highlighted)+ c.isHidden = m.named+ }for s in separators {s.layer?.backgroundColor = colors.separator.cgColor- s.isHidden = m.highlighted+ s.isHidden = m.highlighted || m.named}+ countBadge.show(m.cells.count, colors: colors)+ countBadge.isHidden = !m.named+ titleLabel.stringValue = m.title ?? ""+ titleLabel.textColor = colors.textPrimary+ titleLabel.isHidden = !m.named || m.editing+ if m.editing != (renameField != nil) { m.editing ? beginRename(m.renameText) : endRename() }if m.focusRing != (focusRing != nil) {if m.focusRing {let r = FocusRingLayer()@@ -1627,7 +1659,9 @@ final class SplitRowView: SidebarRowView {/// The buttons show while the row is hovered, as on a tab's row.private func refreshButtons() {guard let m = model, let list else { return }- let buttons: SidebarClose.SplitButtons? = isHovered ? SidebarClose.split(pinned: !m.today, current: m.selected) : nil+ var buttons: SidebarClose.SplitButtons? = isHovered && !m.editing ? SidebarClose.split(pinned: !m.today, current: m.selected) : nil+ // A renamed split shows no panes to put them on: one at the row's end archives the whole split.+ if m.named, case .perPane(let b)? = buttons { buttons = .row(b) }guard buttons != shownButtons else { return }shownButtons = buttonslet panes = m.cells.map(\.id)@@ -1656,7 +1690,7 @@ final class SplitRowView: SidebarRowView {override func layout() {super.layout()- guard model != nil, !cells.isEmpty else { return }+ guard let m = model, !cells.isEmpty else { return }let fill = fillRectlet gap = SplitRowMetrics.cellGaplet n = CGFloat(cells.count)@@ -1665,6 +1699,22 @@ final class SplitRowView: SidebarRowView {: NSRect(x: fill.maxX - SplitRowMetrics.buttonInset - SplitRowMetrics.button, y: (bounds.height - SplitRowMetrics.button) / 2,width: SplitRowMetrics.button, height: SplitRowMetrics.button)if let button { closeButton.frame = button }+ focusRing?.frame = fillRect+ if m.named {+ // Laid out as a tab's row: the badge where the favicon goes, the name 11 pt on.+ let h = bounds.height+ var x = CraMetrics.rowLeading + CGFloat(m.depth) * CraMetrics.indent+ countBadge.frame = NSRect(x: x, y: (h - CraMetrics.favicon) / 2, width: CraMetrics.favicon, height: CraMetrics.favicon)+ x += CraMetrics.favicon + 11+ let right = button?.minX ?? bounds.width - 6+ let th = titleLabel.intrinsicContentSize.height+ titleLabel.frame = NSRect(x: x - SidebarText.inset, y: (h - th) / 2, width: max(0, right - 4 - x) + 2 * SidebarText.inset, height: th)+ if let f = renameField {+ let fh = f.intrinsicContentSize.height+ f.frame = NSRect(x: x - SidebarText.inset, y: (h - fh) / 2, width: max(0, bounds.width - 6 - x + SidebarText.inset), height: fh)+ }+ return+ }var x = fill.minX + SplitRowMetrics.cellInsetfor (i, c) in cells.enumerated() {if i > 0 {@@ -1677,27 +1727,89 @@ final class SplitRowView: SidebarRowView {c.frame = NSRect(x: x, y: fill.minY, width: cw, height: fill.height)x += w + gap}- focusRing?.frame = fillRect}- private func cell(at point: NSPoint) -> SplitCellView? { cells.first { $0.frame.contains(point) } }+ private func cell(at point: NSPoint) -> SplitCellView? {+ model?.named == true ? nil : cells.first { $0.frame.contains(point) }+ }// The split moves as one (into Pinned, a folder, another space); a pane leaves it through// the menu or ⌥ on its close button, as in Arc.- override func dragItemID(at point: NSPoint) -> UUID? { model?.id }+ override func dragItemID(at point: NSPoint) -> UUID? { model?.editing == true ? nil : model?.id }override func click(_ event: NSEvent, at point: NSPoint) {- guard let c = cell(at: point), let list else { return }- list.store.selectTab(c.cellID, window: list.ui.windowID)+ guard let m = model, let list else { return }+ // A click on the row (not the field) ends a rename, so it can never get stuck.+ if m.editing { commitRename() }+ // A renamed split opens on the pane that had focus last.+ let panes = m.cells.map(\.id)+ let focus = list.store.item(m.id)?.split?.focusTabID.flatMap { panes.contains($0) ? $0 : nil }+ guard let pane = m.named ? focus ?? panes.first : cell(at: point)?.cellID else { return }+ list.store.selectTab(pane, window: list.ui.windowID)+ }++ // MARK: Rename (Arc: CustomInfo.isEditingTitle)++ private func beginRename(_ text: String) {+ let f = NSTextField(string: text)+ f.isBordered = false+ f.drawsBackground = false+ f.focusRingType = .none+ f.font = .systemFont(ofSize: 14)+ f.textColor = colors.textPrimary+ f.delegate = self+ f.cell?.isScrollable = true+ renameField = f+ addSubview(f)+ needsLayout = true+ DispatchQueue.main.async { [weak self, weak f] in+ guard let f, let w = self?.window else { return }+ w.makeFirstResponder(f)+ }}+ private func endRename() {+ guard let f = renameField else { return }+ renameField = nil+ f.delegate = nil+ if let w = window, let editor = f.currentEditor(), w.firstResponder === editor { w.makeFirstResponder(nil) }+ f.removeFromSuperview()+ }++ /// An empty name gives the split its panes back.+ private func commitRename() {+ guard let m = model, let list, list.ui.renamingID == m.id, let f = renameField else { return }+ list.store.rename(m.id, to: f.stringValue)+ list.ui.renamingID = nil+ }++ func control(_ control: NSControl, textView: NSTextView, doCommandBy sel: Selector) -> Bool {+ if sel == #selector(NSResponder.cancelOperation(_:)) { list?.ui.renamingID = nil; return true }+ if sel == #selector(NSResponder.insertNewline(_:)) { commitRename(); return true }+ return false+ }++ func controlTextDidEndEditing(_ obj: Notification) { commitRename() }+// Arc shows the split's menu over any pane; each pane's own items are a submenu in it.override func contextMenu(at point: NSPoint) -> NSMenu? {guard let m = model, let list else { return nil }return SidebarMenus.split(m, list: list)}- override func tooltipTarget(at point: NSPoint) -> (any ArcTooltipTarget)? { cell(at: point) }+ override func tooltipTarget(at point: NSPoint) -> (any ArcTooltipTarget)? {+ model?.named == true ? self : cell(at: point)+ }++ // ArcTooltipTarget, for a renamed split: its name by the pointer (Arc 1.171).+ var text: String { model.map { $0.editing ? "" : $0.title ?? "" } ?? "" }+ var keys: [String] { [] }+ var followsCursor: Bool { true }+ var subtitle: String? { nil }+ var optionText: String? { nil }+ var onShow: (() -> Void)? { nil }+ var screenFrame: NSRect { window?.convertToScreen(convert(bounds, to: nil)) ?? .zero }+ var hostWindow: NSWindow? { window }}/// Arc 1.171, measured at 1x (GitHub #139): panes 4 pt inside the fill and 4 pt apart, buttons 24 pt.@@ -1717,6 +1829,37 @@ enum SplitRowMetrics {static let favicon: CGFloat = 6}+/// A renamed split's icon: how many panes it holds (Arc: SplitViewCellItemCountIconView). Arc 1.171+/// live: a 16 pt rounded square in the favicon's place, the digit bold, 7.5 pt tall, centred.+final class SplitCountBadgeView: NSView {+ private let label = NSTextField.sidebarLabel(size: 10, weight: .bold)++ init() {+ super.init(frame: NSRect(x: 0, y: 0, width: CraMetrics.favicon, height: CraMetrics.favicon))+ wantsLayer = true+ layer?.cornerRadius = CraMetrics.favicon * 0.22+ layer?.cornerCurve = .continuous+ label.alignment = .center+ addSubview(label)+ }++ required init?(coder: NSCoder) { fatalError() }++ override func hitTest(_ point: NSPoint) -> NSView? { nil }++ func show(_ count: Int, colors: SidebarColors) {+ label.stringValue = "\(count)"+ label.textColor = colors.textPrimary+ layer?.backgroundColor = colors.countBadgeFill.cgColor+ }++ override func layout() {+ super.layout()+ let h = label.intrinsicContentSize.height+ label.frame = NSRect(x: 0, y: (bounds.height - h) / 2, width: bounds.width, height: h)+ }+}+/// One pane inside the split row (Arc: SplitViewCellChildItemView).final class SplitCellView: NSView, ArcTooltipTarget {let cellID: UUIDdiff --git a/Sources/Cra/UI/Sidebar/List/SpaceListView.swift b/Sources/Cra/UI/Sidebar/List/SpaceListView.swiftindex d8eb432..49893f3 100644--- a/Sources/Cra/UI/Sidebar/List/SpaceListView.swift+++ b/Sources/Cra/UI/Sidebar/List/SpaceListView.swift@@ -303,7 +303,10 @@ final class SpaceListView: NSView, NSDraggingSource {}let selected = children.contains { store.isSelected($0.id, window: window) }row.content = .split(SplitRowModel(- id: item.id, depth: depth, today: !pinned, selected: selected, highlighted: selected && !placeholder,+ id: item.id, depth: depth, today: !pinned,+ // Arc offers "2 Tabs" to a split with no name yet.+ title: item.title, renameText: item.title ?? "\(cells.count) Tabs", editing: renaming == item.id,+ selected: selected, highlighted: selected && !placeholder,focusRing: focusID.map { f in children.contains { $0.id == f } } ?? false,cells: cells))out.append(row)diff --git a/Sources/Cra/UI/Theme.swift b/Sources/Cra/UI/Theme.swiftindex 2721061..dbe6fd5 100644--- a/Sources/Cra/UI/Theme.swift+++ b/Sources/Cra/UI/Theme.swift@@ -41,6 +41,9 @@ struct SpacePalette {/// 85 % (outline) over the sidebar, flattened onto the sidebar's colour so an open folder is opaque.let folderFill: Colorlet folderBack: Color+ /// A renamed split's pane count (Arc: the palette's cutoutColor). Arc 1.171 live, 3 Nov: the theme+ /// colour at ~60% on a dark theme; a light theme takes tintedLight, as the folder front does.+ let countBadgeFill: Color/// Selected row in the command bar (Arc tints it with the space color).let commandHighlight: Colorlet noise: Double@@ -153,6 +156,7 @@ extension SpaceTheme {folderTint: dark ? ground.blendedToward(.white, 0.85) : Color(folder.outline),folderFill: dark ? darkFill : Color(folder.front),folderBack: dark ? darkFill : Color(folder.back),+ countBadgeFill: dark ? Color(hex: self.accent ?? base[0]).opacity(0.6) : Color(folder.front),commandHighlight: dark ? accent.opacity(0.55) : SpaceTheme.mix(base[0], towardWhite: false, amount: 0.45).opacity(0.75),noise: noise,// Calibrated against Arc's rendering of a 0.25-intensity vibrant theme.diff --git a/Tests/Scenarios/component-snapshots.scenario b/Tests/Scenarios/component-snapshots.scenarioindex d55bebb..54e32d4 100644--- a/Tests/Scenarios/component-snapshots.scenario+++ b/Tests/Scenarios/component-snapshots.scenario@@ -38,3 +38,6 @@ wait 3snapshot PaneToolbarView,0 toolbar-split-panesnapshot PaneToolbarView,1 toolbar-split-empty-panesnapshot SplitRowView split-row+cra://action/renameTab # from a pane, the split's name is edited in its row+wait 1+snapshot SplitRowView split-row-renamingdiff --git a/Tests/Snapshots/split-row-renaming.png b/Tests/Snapshots/split-row-renaming.pngnew file mode 100644index 0000000..19fe170Binary files /dev/null and b/Tests/Snapshots/split-row-renaming.png differdiff --git a/docs/testing.md b/docs/testing.mdindex 96edf39..54a1b63 100644--- a/docs/testing.md+++ b/docs/testing.md@@ -111,8 +111,8 @@ that breaks the private path fails a run instead of shipping.`component-snapshots` imports the made-up Arc profile, sizes the window and draws the spaceheader, favorites, sidebar rows, dock, URL pill, the toolbar in its normal, developer and-split-pane forms and the split row. A difference over 0.05 % of pixels fails, with a red diff-image next to the new snapshot.+split-pane forms and the split row, plain and being renamed. A difference over 0.05 % of+pixels fails, with a red diff image next to the new snapshot.A reference is Cra's own drawing, so it can only be as right as Cra was when it was recorded.Before recording one again (`CRA_SNAPSHOT_RECORD=1 scripts/scenario.sh component-snapshots`),