diff --git a/Sources/Timber/TimberApp.swift b/Sources/Timber/TimberApp.swift index d08a38b..428661b 100644 --- a/Sources/Timber/TimberApp.swift +++ b/Sources/Timber/TimberApp.swift @@ -10,6 +10,36 @@ import TimberModel // The status item is managed with AppKit rather than MenuBarExtra so // left-click opens the popover while right-click shows a menu with Quit. +/// Popover width bounds: the popover grows to fit long +/// `worktree@repo` names, clamped so short lists stay usable and very +/// long names truncate (from the middle) instead of stretching across +/// the screen. Bounds live in TimberModel so the math stays tested; +/// row widths inside the ScrollView do not reach the hosting view's +/// fitting size, so the width is measured from the longest row text +/// (including the icon cluster chrome) instead of the layout size. +enum PopoverSizing { + static var minWidth: CGFloat { + CGFloat(TimberModel.popoverMinWidth) + } + + static var maxWidth: CGFloat { + CGFloat(TimberModel.popoverMaxWidth) + } + + /// Display string for a row, mirroring TimberRow's text. + static func displayString(for item: TimberItem) -> String { + (item.kind == .create ? "+ " : "") + item.value + } + + static func textWidth(_ string: String) -> CGFloat { + (string as NSString).size(withAttributes: [.font: NSFont.systemFont(ofSize: NSFont.systemFontSize)]).width + } + + static func width(for items: [TimberItem]) -> CGFloat { + CGFloat(TimberModel.popoverWidth(textWidths: items.map { Double(textWidth(displayString(for: $0))) })) + } +} + /// NSHostingView that keeps its NSPopover sized to the SwiftUI content. /// Without this the popover opens at a pre-layout size (clipping rows) /// and only reaches full size on a later open, after layout has resolved. @@ -95,7 +125,6 @@ final class AppDelegate: NSObject, NSApplicationDelegate { private let state = TimberState() private var refreshTimer: Timer? - private let popoverWidth: CGFloat = 400 private let backgroundRefreshInterval: TimeInterval = 60 func applicationDidFinishLaunching(_ notification: Notification) { @@ -117,12 +146,19 @@ final class AppDelegate: NSObject, NSApplicationDelegate { let popover = NSPopover() popover.behavior = .transient popover.contentViewController = viewController - popover.contentSize = NSSize(width: popoverWidth, height: 160) - hostingView.onLayout = { [weak popover, popoverWidth] size in - guard let popover, popover.isShown, size.height > 0, - abs(popover.contentSize.height - size.height) > 0.5 + popover.contentSize = NSSize(width: PopoverSizing.minWidth, height: 160) + let state = state + hostingView.onLayout = { [weak popover, state] size in + guard let popover, popover.isShown, size.height > 0 else { return } + // Height follows the SwiftUI layout; width follows the + // longest measured row (ScrollView rows never widen the + // fitting size, so fittingSize.width stays near the minimum + // and truncates rows early if trusted). + let width = PopoverSizing.width(for: state.items) + guard abs(popover.contentSize.height - size.height) > 0.5 + || abs(popover.contentSize.width - width) > 0.5 else { return } - popover.contentSize = NSSize(width: popoverWidth, height: size.height) + popover.contentSize = NSSize(width: width, height: size.height) } self.popover = popover @@ -161,6 +197,10 @@ final class AppDelegate: NSObject, NSApplicationDelegate { // never blanks out or visibly reloads on open. state.presentFromCache() state.refreshInBackground() + // Pre-size to the cached rows so the first frame never + // flashes narrow; onLayout keeps height (and width, as the + // filter narrows the list) in sync afterwards. + popover.contentSize.width = PopoverSizing.width(for: state.items) popover.show(relativeTo: button.bounds, of: button, preferredEdge: .minY) popover.contentViewController?.view.window?.makeKey() } @@ -182,202 +222,3 @@ final class AppDelegate: NSObject, NSApplicationDelegate { // Note: there is deliberately no SwiftUI App scene here. The entry point // is main.swift (plain AppKit lifecycle); a Settings scene would open a // blank window at launch. - -// MARK: - Views - -struct TimberPopover: View { - @ObservedObject var state: TimberState - @FocusState private var filterFocused: Bool - @FocusState private var formFocused: Bool - - private let maxRows = 8 - private let rowHeight: CGFloat = 32 - - var body: some View { - VStack(alignment: .leading, spacing: 8) { - HStack(spacing: 8) { - TextField("type worktree@repo", text: $state.filter) - .textFieldStyle(.roundedBorder) - .focused($filterFocused) - .onSubmit { state.primaryAction() } - .onChange(of: state.filter) { _, _ in state.filterChanged() } - .disabled(state.busy) - - if state.busy { - ProgressView() - .scaleEffect(0.6) - .frame(width: 20, height: 20) - } - - Button(state.repoFormOpen ? "−" : "+") { - state.repoFormOpen.toggle() - } - .help(state.repoFormOpen ? "Close repository form" : "Add repository (timber repo add)") - .disabled(state.busy) - } - - if state.repoFormOpen { - TextField("Remote URL or path", text: $state.repoURL) - .textFieldStyle(.roundedBorder) - .focused($formFocused) - .onSubmit { state.submitRepoForm() } - .disabled(state.busy) - TextField("Name (optional, derived from URL)", text: $state.repoName) - .textFieldStyle(.roundedBorder) - .focused($formFocused) - .onSubmit { state.submitRepoForm() } - .disabled(state.busy) - HStack(spacing: 8) { - Button(state.busy ? "Adding…" : "Add repository") { - state.submitRepoForm() - } - .disabled(state.busy) - Button("Cancel") { - state.repoFormOpen = false - } - .disabled(state.busy) - } - } - - if state.listing { - Text("Loading worktrees…") - .foregroundStyle(.secondary) - .frame(maxWidth: .infinity, alignment: .center) - } else if state.items.isEmpty { - Text(state.worktrees.isEmpty - ? "No worktrees yet — type name@repo to create one" - : "No matches") - .foregroundStyle(.secondary) - .frame(maxWidth: .infinity, alignment: .center) - .multilineTextAlignment(.center) - } else { - ScrollView { - LazyVStack(spacing: 4) { - ForEach(state.items) { item in - TimberRow(state: state, item: item) - } - } - } - .frame(maxHeight: CGFloat(maxRows) * rowHeight) - } - } - .padding() - .frame(width: 400) - .onAppear { - filterFocused = true - } - .onKeyPress(.upArrow) { state.move(-1); return .handled } - .onKeyPress(.downArrow) { state.move(1); return .handled } - .onKeyPress(.escape) { - if formFocused { - return .ignored - } - if state.repoFormOpen { - state.repoFormOpen = false - } else if !state.filter.isEmpty { - state.clearFilter() - } - return .handled - } - .onKeyPress(characters: CharacterSet(charactersIn: "u"), phases: .down) { press in - guard press.modifiers == .control, !formFocused else { return .ignored } - state.clearFilter() - return .handled - } - } -} - -struct TimberRow: View { - @ObservedObject var state: TimberState - let item: TimberItem - - private var selected: Bool { - state.cursorActive && state.selectedID == item.id - } - - private var armed: Bool { - state.armedRemoveValue == item.value - } - - private let iconBox: CGFloat = 22 - private let iconSize: CGFloat = 14 - - var body: some View { - HStack(spacing: 6) { - RoundedRectangle(cornerRadius: 1.5) - .fill(Color.accentColor) - .frame(width: 3, height: 20) - .opacity(selected ? 1 : 0) - - Text((item.kind == .create ? "+ " : "") + item.value) - .lineLimit(1) - .truncationMode(.tail) - .opacity(item.kind == .create && !selected ? 0.72 : 1) - - Spacer(minLength: 8) - - // The action cluster is always in the layout (hidden when the - // row is not selected) so the popover width never jumps and - // the window never clips the row when buttons appear. - HStack(spacing: 4) { - Button { - state.openInZed(item) - } label: { - Image(nsImage: TimberIcons.zed) - .renderingMode(.template) - .resizable() - .frame(width: iconSize, height: iconSize) - .foregroundStyle(.primary) - } - .buttonStyle(.plain) - .frame(width: iconBox, height: iconBox) - .help(item.kind == .open ? "Open in Zed" : "Create and open in Zed") - - Button { - state.openInHerdr(item) - } label: { - Image(nsImage: TimberIcons.herdr) - .renderingMode(.template) - .resizable() - .frame(width: iconSize, height: iconSize) - .foregroundStyle(.primary) - } - .buttonStyle(.plain) - .frame(width: iconBox, height: iconBox) - .help(item.kind == .open ? "Open in Herdr" : "Create in Herdr") - - if item.kind == .open { - Button { - state.armOrRemove(item) - } label: { - Image(systemName: "trash") - .frame(width: iconSize, height: iconSize) - } - .buttonStyle(.plain) - .frame(width: iconBox, height: iconBox) - .foregroundStyle(armed ? .red : .primary) - .help(armed ? "Click again to remove \(item.value)" : "Remove \(item.value)") - } else { - // Invisible placeholder keeps the cluster width stable. - Image(systemName: "trash") - .frame(width: iconSize, height: iconSize) - .frame(width: iconBox, height: iconBox) - .opacity(0) - } - } - .layoutPriority(1) - .opacity(selected ? 1 : 0) - .disabled(!selected || state.busy) - } - .frame(height: 32) - .contentShape(Rectangle()) - .onHover { hovering in - if hovering { - state.select(id: item.id) - } - } - .onTapGesture { - state.clickActivate(item) - } - } -} diff --git a/Sources/Timber/TimberViews.swift b/Sources/Timber/TimberViews.swift new file mode 100644 index 0000000..4cad610 --- /dev/null +++ b/Sources/Timber/TimberViews.swift @@ -0,0 +1,206 @@ +import AppKit +import SwiftUI +import TimberModel + +// Popover views: fuzzy-filter list over worktrees plus the row layout. +// Kept apart from the AppKit lifecycle in TimberApp so neither file +// trips the line-count lint. + +// MARK: - Views + +struct TimberPopover: View { + @ObservedObject var state: TimberState + @FocusState private var filterFocused: Bool + @FocusState private var formFocused: Bool + + private let maxRows = 8 + private let rowHeight: CGFloat = 32 + + var body: some View { + VStack(alignment: .leading, spacing: 8) { + HStack(spacing: 8) { + TextField("type worktree@repo", text: $state.filter) + .textFieldStyle(.roundedBorder) + .focused($filterFocused) + .onSubmit { state.primaryAction() } + .onChange(of: state.filter) { _, _ in state.filterChanged() } + .disabled(state.busy) + + if state.busy { + ProgressView() + .scaleEffect(0.6) + .frame(width: 20, height: 20) + } + + Button(state.repoFormOpen ? "−" : "+") { + state.repoFormOpen.toggle() + } + .help(state.repoFormOpen ? "Close repository form" : "Add repository (timber repo add)") + .disabled(state.busy) + } + + if state.repoFormOpen { + TextField("Remote URL or path", text: $state.repoURL) + .textFieldStyle(.roundedBorder) + .focused($formFocused) + .onSubmit { state.submitRepoForm() } + .disabled(state.busy) + TextField("Name (optional, derived from URL)", text: $state.repoName) + .textFieldStyle(.roundedBorder) + .focused($formFocused) + .onSubmit { state.submitRepoForm() } + .disabled(state.busy) + HStack(spacing: 8) { + Button(state.busy ? "Adding…" : "Add repository") { + state.submitRepoForm() + } + .disabled(state.busy) + Button("Cancel") { + state.repoFormOpen = false + } + .disabled(state.busy) + } + } + + if state.listing { + Text("Loading worktrees…") + .foregroundStyle(.secondary) + .frame(maxWidth: .infinity, alignment: .center) + } else if state.items.isEmpty { + Text(state.worktrees.isEmpty + ? "No worktrees yet — type name@repo to create one" + : "No matches") + .foregroundStyle(.secondary) + .frame(maxWidth: .infinity, alignment: .center) + .multilineTextAlignment(.center) + } else { + ScrollView { + LazyVStack(spacing: 4) { + ForEach(state.items) { item in + TimberRow(state: state, item: item) + } + } + } + .frame(maxHeight: CGFloat(maxRows) * rowHeight) + } + } + .padding() + .frame(minWidth: PopoverSizing.minWidth, maxWidth: PopoverSizing.maxWidth) + .onAppear { + filterFocused = true + } + .onKeyPress(.upArrow) { state.move(-1); return .handled } + .onKeyPress(.downArrow) { state.move(1); return .handled } + .onKeyPress(.escape) { + if formFocused { + return .ignored + } + if state.repoFormOpen { + state.repoFormOpen = false + } else if !state.filter.isEmpty { + state.clearFilter() + } + return .handled + } + .onKeyPress(characters: CharacterSet(charactersIn: "u"), phases: .down) { press in + guard press.modifiers == .control, !formFocused else { return .ignored } + state.clearFilter() + return .handled + } + } +} + +struct TimberRow: View { + @ObservedObject var state: TimberState + let item: TimberItem + + private var selected: Bool { + state.cursorActive && state.selectedID == item.id + } + + private var armed: Bool { + state.armedRemoveValue == item.value + } + + private let iconBox: CGFloat = 22 + private let iconSize: CGFloat = 14 + + var body: some View { + HStack(spacing: 6) { + RoundedRectangle(cornerRadius: 1.5) + .fill(Color.accentColor) + .frame(width: 3, height: 20) + .opacity(selected ? 1 : 0) + + Text((item.kind == .create ? "+ " : "") + item.value) + .lineLimit(1) + .truncationMode(.middle) + .opacity(item.kind == .create && !selected ? 0.72 : 1) + + Spacer(minLength: 8) + + // The action cluster is always in the layout (hidden when the + // row is not selected) so the popover width never jumps and + // the window never clips the row when buttons appear. + HStack(spacing: 4) { + Button { + state.openInZed(item) + } label: { + Image(nsImage: TimberIcons.zed) + .renderingMode(.template) + .resizable() + .frame(width: iconSize, height: iconSize) + .foregroundStyle(.primary) + } + .buttonStyle(.plain) + .frame(width: iconBox, height: iconBox) + .help(item.kind == .open ? "Open in Zed" : "Create and open in Zed") + + Button { + state.openInHerdr(item) + } label: { + Image(nsImage: TimberIcons.herdr) + .renderingMode(.template) + .resizable() + .frame(width: iconSize, height: iconSize) + .foregroundStyle(.primary) + } + .buttonStyle(.plain) + .frame(width: iconBox, height: iconBox) + .help(item.kind == .open ? "Open in Herdr" : "Create in Herdr") + + if item.kind == .open { + Button { + state.armOrRemove(item) + } label: { + Image(systemName: "trash") + .frame(width: iconSize, height: iconSize) + } + .buttonStyle(.plain) + .frame(width: iconBox, height: iconBox) + .foregroundStyle(armed ? .red : .primary) + .help(armed ? "Click again to remove \(item.value)" : "Remove \(item.value)") + } else { + // Invisible placeholder keeps the cluster width stable. + Image(systemName: "trash") + .frame(width: iconSize, height: iconSize) + .frame(width: iconBox, height: iconBox) + .opacity(0) + } + } + .layoutPriority(1) + .opacity(selected ? 1 : 0) + .disabled(!selected || state.busy) + } + .frame(height: 32) + .contentShape(Rectangle()) + .onHover { hovering in + if hovering { + state.select(id: item.id) + } + } + .onTapGesture { + state.clickActivate(item) + } + } +} diff --git a/Sources/TimberModel/TimberModel.swift b/Sources/TimberModel/TimberModel.swift index ccb0dbf..2647626 100644 --- a/Sources/TimberModel/TimberModel.swift +++ b/Sources/TimberModel/TimberModel.swift @@ -202,6 +202,30 @@ public enum TimberModel { return args } + // MARK: - Popover sizing + + /// Width bounds for the popover. The popover grows to fit long + /// `worktree@repo` names so rows truncate only after hitting max. + public static let popoverMinWidth: Double = 300 + public static let popoverMaxWidth: Double = 600 + + /// Fixed chrome around the row text, kept next to the row layout it + /// mirrors (TimberRow + popover padding) so the two stay in sync: + /// selection bar (3) + row gaps (3 x 6) + spacer minimum (8) + + /// icon cluster (3 x 22 boxes + 2 x 4 spacing) + popover padding + /// (2 x 16) + slop for font-rendering differences (8). + public static let popoverChromeWidth: Double = 3 + 18 + 8 + 74 + 32 + 8 + + /// Content-driven popover width: the widest row text plus chrome, + /// clamped to the bounds. Widths inside a ScrollView do not + /// propagate to the hosting view's fitting size, so callers measure + /// the longest row text (e.g. via NSString sizing) and pass those + /// widths here instead of trusting the layout size. + public static func popoverWidth(textWidths: [Double]) -> Double { + guard let maxText = textWidths.max() else { return popoverMinWidth } + return min(max(maxText + popoverChromeWidth, popoverMinWidth), popoverMaxWidth) + } + // MARK: - Status-item clicks /// Left-click toggles the popover; right-click (or Ctrl-click) shows diff --git a/Tests/TimberModelTests/TimberModelTests.swift b/Tests/TimberModelTests/TimberModelTests.swift index dc318d6..296bfb5 100644 --- a/Tests/TimberModelTests/TimberModelTests.swift +++ b/Tests/TimberModelTests/TimberModelTests.swift @@ -115,6 +115,33 @@ final class TimberModelTests: XCTestCase { } } + // MARK: - popover sizing + + func testPopoverWidthEmptyStaysAtMin() { + XCTAssertEqual(TimberModel.popoverWidth(textWidths: []), TimberModel.popoverMinWidth) + } + + func testPopoverWidthShortTextStaysAtMin() { + // A short name plus chrome still fits inside the minimum. + XCTAssertEqual(TimberModel.popoverWidth(textWidths: [50]), TimberModel.popoverMinWidth) + } + + func testPopoverWidthGrowsWithLongestRow() { + // Widest row drives the width, including room for the icon + // cluster via the chrome constant. + let width = TimberModel.popoverWidth(textWidths: [100, 250, 180]) + XCTAssertEqual(width, 250 + TimberModel.popoverChromeWidth) + XCTAssertGreaterThan(width, TimberModel.popoverMinWidth) + XCTAssertLessThan(width, TimberModel.popoverMaxWidth) + } + + func testPopoverWidthClampsAtMax() { + XCTAssertEqual( + TimberModel.popoverWidth(textWidths: [10000]), + TimberModel.popoverMaxWidth + ) + } + // MARK: - status-item clicks func testSecondaryClickShowsContextMenu() { diff --git a/mise.toml b/mise.toml index 51d1f47..368fc84 100644 --- a/mise.toml +++ b/mise.toml @@ -9,6 +9,15 @@ _.path = [ "~/.local/bin", ] +[tasks.on-stop] +description = "Stop hook: format, lint, and test" +run = """ +set -euo pipefail +mise run format +mise run lint +mise run test +""" + [tasks.build] description = "Build dist/Timber.app via SwiftPM" run = """