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
61 changes: 59 additions & 2 deletions macos/Sources/ConchApp/CommitsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,13 @@ final class CommitsModel: ObservableObject {
}
}

/// Picks a commit, or with shift a second one to compare with it.
/// Picks a commit, or with shift a second one to compare with it; the
/// picked one clicked again closes its panel.
func pick(_ hash: String, shift: Bool) {
if pickCloses(hash, selected: selected, shift: shift, comparing: compareWith != nil) {
closeCommit()
return
}
if shift, let s = selected, s != hash {
compareWith = hash
} else {
Expand All @@ -395,6 +400,29 @@ final class CommitsModel: ObservableObject {
loadDetail()
}

/// Closes the commit's panel: the graph is whole again.
func closeCommit() {
selected = nil
compareWith = nil
detail = nil
detailError = ""
peekFile = nil
peek = []
}

/// One step back (Escape): the newest thing opened closes first.
func back() {
switch graphBack(showCI: showCI, showAutopilot: showAutopilot, comparing: compareWith != nil,
selected: selected != nil, focused: focusOn != nil) {
case .closeCI: showCI = false
case .closeAutopilot: showAutopilot = false
case .endCompare: compareWith = nil; loadDetail()
case .closeCommit: closeCommit()
case .clearFocus: focusOn = nil
case .nothing: break
}
}

/// The range being compared, the older commit first, by the graph's order.
var range: (from: String, to: String)? {
guard let a = selected, let b = compareWith else { return nil }
Expand Down Expand Up @@ -496,6 +524,9 @@ struct CommitsView: View {
.sheet(isPresented: Binding(get: { model.reviewing != nil }, set: { if !$0 { model.reviewing = nil } })) {
RebaseReviewSheet(model: model)
}
// Only while the graph has the keyboard: Escape belongs to the
// terminals and agents in the other splits.
.onExitCommand { model.back() }
}

var graph: some View {
Expand Down Expand Up @@ -804,12 +835,20 @@ struct CommitDetailPanel: View {
var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: 10) {
// Still reading, or it failed: the × has no header to sit in.
if model.detail == nil {
HStack { Spacer(); CloseCommitButton(model: model) }
}
if !model.detailError.isEmpty {
Text(model.detailError).font(.ui(size: 12)).foregroundStyle(Theme.err)
}
if let d = model.detail {
if let r = model.range {
Text("Compare").font(.ui(size: 11, weight: .semibold)).foregroundStyle(Theme.merged)
HStack {
Text("Compare").font(.ui(size: 11, weight: .semibold)).foregroundStyle(Theme.merged)
Spacer()
CloseCommitButton(model: model)
}
Text("\(d.commits ?? 0) commit\((d.commits ?? 0) == 1 ? "" : "s"), \(d.files?.count ?? 0) file\((d.files?.count ?? 0) == 1 ? "" : "s")")
.font(.ui(size: 14, weight: .semibold))
Text("\(String(r.from.prefix(7))) → \(String(r.to.prefix(7)))").font(.ui(size: 11.5, design: .monospaced)).foregroundStyle(Theme.muted)
Expand All @@ -822,6 +861,7 @@ struct CommitDetailPanel: View {
NSPasteboard.general.clearContents()
NSPasteboard.general.setString(d.hash, forType: .string)
}.controlSize(.small)
CloseCommitButton(model: model)
}
Text(d.subject ?? "").font(.ui(size: 14, weight: .semibold)).fixedSize(horizontal: false, vertical: true)
HStack(spacing: 6) {
Expand Down Expand Up @@ -1274,3 +1314,20 @@ struct ConflictCard: View {
.overlay(RoundedRectangle(cornerRadius: 8).stroke(tone.opacity(0.35)))
}
}


/// The commit panel's ×: back to the whole graph (Escape does it too).
struct CloseCommitButton: View {
@ObservedObject var model: CommitsModel
/// Read so the × is drawn again when the theme changes.
@Environment(\.conchTheme) private var look

var body: some View {
Button { model.closeCommit() } label: {
LucideIcon("x", color: Theme.muted, size: 13).padding(3).contentShape(Rectangle())
}
.buttonStyle(.plain)
.help("Close — back to the whole graph (Esc)")
.accessibilityLabel("Close the commit")
}
}
23 changes: 23 additions & 0 deletions macos/Sources/ConchKit/CommitGraph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,26 @@ public func lanePalette(_ t: ConchTheme) -> [RGB] {
}
return out
}

/// What one step back does in the commit graph — Escape, or the panel's
/// close button — the newest thing opened first, until the graph is whole
/// again with nothing picked.
public enum GraphBack: Equatable, Sendable {
case closeCI, closeAutopilot, endCompare, closeCommit, clearFocus, nothing
}

public func graphBack(showCI: Bool, showAutopilot: Bool, comparing: Bool, selected: Bool, focused: Bool) -> GraphBack {
if showCI { return .closeCI }
if showAutopilot { return .closeAutopilot }
if comparing { return .endCompare }
if selected { return .closeCommit }
if focused { return .clearFocus }
return .nothing
}

/// Whether a click on a commit closes its panel rather than opening one:
/// the commit already picked, clicked again without shift and with no
/// comparison open.
public func pickCloses(_ hash: String, selected: String?, shift: Bool, comparing: Bool) -> Bool {
!shift && !comparing && selected == hash
}
19 changes: 19 additions & 0 deletions macos/Tests/ConchKitTests/BranchGraphTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,22 @@ func history() -> [LogCommit] {
#expect(by["merging"]?.stopped == false)
#expect(branchWorktree(project, "stopped")?.path == "/w/a")
}

// One step back at a time, the newest first, until nothing is open (#250
// left the commit panel with no way to close it).
@Test func graphBackSteps() {
#expect(graphBack(showCI: true, showAutopilot: false, comparing: true, selected: true, focused: true) == .closeCI)
#expect(graphBack(showCI: false, showAutopilot: true, comparing: true, selected: true, focused: true) == .closeAutopilot)
#expect(graphBack(showCI: false, showAutopilot: false, comparing: true, selected: true, focused: true) == .endCompare)
#expect(graphBack(showCI: false, showAutopilot: false, comparing: false, selected: true, focused: true) == .closeCommit)
#expect(graphBack(showCI: false, showAutopilot: false, comparing: false, selected: false, focused: true) == .clearFocus)
#expect(graphBack(showCI: false, showAutopilot: false, comparing: false, selected: false, focused: false) == .nothing)
}

@Test func clickingThePickedCommitClosesIt() {
#expect(pickCloses("a", selected: "a", shift: false, comparing: false))
#expect(!pickCloses("b", selected: "a", shift: false, comparing: false))
#expect(!pickCloses("a", selected: nil, shift: false, comparing: false))
#expect(!pickCloses("a", selected: "a", shift: true, comparing: false)) // shift compares
#expect(!pickCloses("a", selected: "a", shift: false, comparing: true)) // ends a comparison instead
}
Loading