diff --git a/macos/Sources/Ghostty/Surface View/SurfaceTitleBar.swift b/macos/Sources/Ghostty/Surface View/SurfaceTitleBar.swift index 01f6b1afa..8e0814ec4 100644 --- a/macos/Sources/Ghostty/Surface View/SurfaceTitleBar.swift +++ b/macos/Sources/Ghostty/Surface View/SurfaceTitleBar.swift @@ -7,13 +7,17 @@ struct SurfaceTitleBar: View { @State private var isEditing = false @State private var editText = "" + @State private var isCancelling = false @FocusState private var fieldFocused: Bool private var displayTitle: String { if surfaceView.isUserSetTitle { return surfaceView.title } - return URL(fileURLWithPath: surfaceView.pwd ?? "/").lastPathComponent + if let pwd = surfaceView.pwd, !pwd.isEmpty, pwd != "/" { + return URL(fileURLWithPath: pwd).lastPathComponent + } + return surfaceView.title.isEmpty ? "terminal" : surfaceView.title } var body: some View { @@ -26,28 +30,41 @@ struct SurfaceTitleBar: View { .textFieldStyle(.plain) .multilineTextAlignment(.center) .focused($fieldFocused) - .onSubmit { commit() } - .onExitCommand { cancel() } + .onSubmit { fieldFocused = false } + .onExitCommand { isCancelling = true; fieldFocused = false } .onChange(of: fieldFocused) { focused in - if !focused { commit() } + guard !focused else { return } + if isCancelling { + isCancelling = false + cancel() + } else { + commit() + } } + .onAppear { fieldFocused = true } .padding(.horizontal, 8) + .font(.system(size: 11)) } else { Text(displayTitle) .lineLimit(1) .truncationMode(.middle) .foregroundStyle(.secondary) + .font(.system(size: 11)) .onTapGesture(count: 2) { startEditing() } + .accessibilityLabel("Pane title: \(displayTitle)") } } .frame(height: 22) .frame(maxWidth: .infinity) + .accessibilityElement(children: .contain) + .accessibilityLabel(isEditing ? "Title editor" : "Pane title: \(displayTitle)") } private func startEditing() { editText = surfaceView.isUserSetTitle ? surfaceView.title : "" + isCancelling = false isEditing = true - fieldFocused = true + // Focus is set via .onAppear on the TextField } private func commit() {