macos: close surface works

This commit is contained in:
Mitchell Hashimoto
2023-03-10 14:44:33 -08:00
parent a265e7ce20
commit 0aadd19282
3 changed files with 28 additions and 17 deletions

View File

@ -7,10 +7,11 @@ extension Ghostty {
/// split direction by splitting the terminal. /// split direction by splitting the terminal.
struct TerminalSplit: View { struct TerminalSplit: View {
@Environment(\.ghosttyApp) private var app @Environment(\.ghosttyApp) private var app
let onClose: (() -> Void)?
var body: some View { var body: some View {
if let app = app { if let app = app {
TerminalSplitRoot(app: app) TerminalSplitRoot(app: app, onClose: onClose)
} }
} }
} }
@ -78,13 +79,13 @@ extension Ghostty {
/// one of these in a split tree. /// one of these in a split tree.
private struct TerminalSplitRoot: View { private struct TerminalSplitRoot: View {
@State private var node: SplitNode @State private var node: SplitNode
@State private var requestClose: Bool = false
/// This is an ignored value because at the root we can't close. let onClose: (() -> Void)?
@State private var ignoredRequestClose: Bool = false
@FocusedValue(\.ghosttySurfaceTitle) private var surfaceTitle: String? @FocusedValue(\.ghosttySurfaceTitle) private var surfaceTitle: String?
init(app: ghostty_app_t) { init(app: ghostty_app_t, onClose: (() ->Void)? = nil) {
self.onClose = onClose
_node = State(wrappedValue: SplitNode.noSplit(.init(app))) _node = State(wrappedValue: SplitNode.noSplit(.init(app)))
} }
@ -92,7 +93,12 @@ extension Ghostty {
ZStack { ZStack {
switch (node) { switch (node) {
case .noSplit(let leaf): case .noSplit(let leaf):
TerminalSplitLeaf(leaf: leaf, node: $node, requestClose: $ignoredRequestClose) TerminalSplitLeaf(leaf: leaf, node: $node, requestClose: $requestClose)
.onChange(of: requestClose) { value in
guard value else { return }
guard let onClose = self.onClose else { return }
onClose()
}
case .horizontal(let container): case .horizontal(let container):
TerminalSplitContainer(direction: .horizontal, node: $node, container: container) TerminalSplitContainer(direction: .horizontal, node: $node, container: container)

View File

@ -24,15 +24,15 @@ struct GhosttyApp: App {
case .error: case .error:
ErrorView() ErrorView()
case .ready: case .ready:
Ghostty.TerminalSplit() Ghostty.TerminalSplit(onClose: Self.closeWindow)
.ghosttyApp(ghostty.app!) .ghosttyApp(ghostty.app!)
} }
}.commands { }.commands {
CommandGroup(after: .newItem) { CommandGroup(after: .newItem) {
Button("New Tab", action: newTab).keyboardShortcut("t", modifiers: [.command]) Button("New Tab", action: Self.newTab).keyboardShortcut("t", modifiers: [.command])
Divider() Divider()
Button("Close", action: close).keyboardShortcut("w", modifiers: [.command]) Button("Close", action: close).keyboardShortcut("w", modifiers: [.command])
Button("Close Window", action: closeWindow).keyboardShortcut("w", modifiers: [.command, .shift]) Button("Close Window", action: Self.closeWindow).keyboardShortcut("w", modifiers: [.command, .shift])
} }
} }
@ -42,7 +42,7 @@ struct GhosttyApp: App {
} }
// Create a new tab in the currently active window // Create a new tab in the currently active window
func newTab() { static func newTab() {
guard let currentWindow = NSApp.keyWindow else { return } guard let currentWindow = NSApp.keyWindow else { return }
guard let windowController = currentWindow.windowController else { return } guard let windowController = currentWindow.windowController else { return }
windowController.newWindowForTab(nil) windowController.newWindowForTab(nil)
@ -51,16 +51,16 @@ struct GhosttyApp: App {
} }
} }
static func closeWindow() {
guard let currentWindow = NSApp.keyWindow else { return }
currentWindow.close()
}
func close() { func close() {
guard let surfaceView = focusedSurface else { return } guard let surfaceView = focusedSurface else { return }
guard let surface = surfaceView.surface else { return } guard let surface = surfaceView.surface else { return }
ghostty.requestClose(surface: surface) ghostty.requestClose(surface: surface)
} }
func closeWindow() {
guard let currentWindow = NSApp.keyWindow else { return }
currentWindow.close()
}
} }
class AppDelegate: NSObject, NSApplicationDelegate { class AppDelegate: NSObject, NSApplicationDelegate {

View File

@ -278,9 +278,14 @@ pub const Config = struct {
); );
try result.keybind.set.put( try result.keybind.set.put(
alloc, alloc,
.{ .key = .w, .mods = .{ .super = true, .shift = true } }, .{ .key = .w, .mods = .{ .super = true } },
.{ .close_surface = {} }, .{ .close_surface = {} },
); );
try result.keybind.set.put(
alloc,
.{ .key = .w, .mods = .{ .super = true, .shift = true } },
.{ .close_window = {} },
);
try result.keybind.set.put( try result.keybind.set.put(
alloc, alloc,
.{ .key = .t, .mods = .{ .super = true } }, .{ .key = .t, .mods = .{ .super = true } },