From 4d9143f634059d49d4749729cd614ded3bccec02 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 13 Nov 2024 12:55:43 -0800 Subject: [PATCH] macos: use the pwd apprt action to set the proxy icon URL --- .../Features/Terminal/TerminalView.swift | 18 +++++++-------- macos/Sources/Ghostty/Ghostty.App.swift | 23 +++++++++++++++++++ macos/Sources/Ghostty/SurfaceView.swift | 16 ++++++++----- .../Sources/Ghostty/SurfaceView_AppKit.swift | 15 ++++-------- 4 files changed, 45 insertions(+), 27 deletions(-) diff --git a/macos/Sources/Features/Terminal/TerminalView.swift b/macos/Sources/Features/Terminal/TerminalView.swift index 768f57d30..8fc1ae1ec 100644 --- a/macos/Sources/Features/Terminal/TerminalView.swift +++ b/macos/Sources/Features/Terminal/TerminalView.swift @@ -50,6 +50,7 @@ struct TerminalView: View { // Various state values sent back up from the currently focused terminals. @FocusedValue(\.ghosttySurfaceView) private var focusedSurface @FocusedValue(\.ghosttySurfaceTitle) private var surfaceTitle + @FocusedValue(\.ghosttySurfacePwd) private var surfacePwd @FocusedValue(\.ghosttySurfaceZoomed) private var zoomedSplit @FocusedValue(\.ghosttySurfaceCellSize) private var cellSize @@ -65,14 +66,11 @@ struct TerminalView: View { return title } - - // The proxy icon URL for our window - private var proxyIconURL: URL? { - guard let proxyURLString = focusedSurface?.pwd else { - return nil - } - // Use fileURLWithPath initializer for file paths - return URL(fileURLWithPath: proxyURLString) + + // The pwd of the focused surface as a URL + private var pwdURL: URL? { + guard let surfacePwd else { return nil } + return URL(fileURLWithPath: surfacePwd) } var body: some View { @@ -88,7 +86,7 @@ struct TerminalView: View { if (Ghostty.info.mode == GHOSTTY_BUILD_MODE_DEBUG || Ghostty.info.mode == GHOSTTY_BUILD_MODE_RELEASE_SAFE) { DebugBuildWarningView() } - + Ghostty.TerminalSplit(node: $viewModel.surfaceTree) .environmentObject(ghostty) .focused($focused) @@ -99,7 +97,7 @@ struct TerminalView: View { .onChange(of: title) { newValue in self.delegate?.titleDidChange(to: newValue) } - .onChange(of: proxyIconURL) { newValue in + .onChange(of: pwdURL) { newValue in self.delegate?.pwdDidChange(to: newValue) } .onChange(of: cellSize) { newValue in diff --git a/macos/Sources/Ghostty/Ghostty.App.swift b/macos/Sources/Ghostty/Ghostty.App.swift index 07acf0f91..489493ad3 100644 --- a/macos/Sources/Ghostty/Ghostty.App.swift +++ b/macos/Sources/Ghostty/Ghostty.App.swift @@ -488,6 +488,9 @@ extension Ghostty { case GHOSTTY_ACTION_SET_TITLE: setTitle(app, target: target, v: action.action.set_title) + case GHOSTTY_ACTION_PWD: + pwdChanged(app, target: target, v: action.action.pwd) + case GHOSTTY_ACTION_OPEN_CONFIG: ghostty_config_open() @@ -943,6 +946,26 @@ extension Ghostty { } } + private static func pwdChanged( + _ app: ghostty_app_t, + target: ghostty_target_s, + v: ghostty_action_pwd_s) { + switch (target.tag) { + case GHOSTTY_TARGET_APP: + Ghostty.logger.warning("pwd change does nothing with an app target") + return + + case GHOSTTY_TARGET_SURFACE: + guard let surface = target.target.surface else { return } + guard let surfaceView = self.surfaceView(from: surface) else { return } + guard let pwd = String(cString: v.pwd!, encoding: .utf8) else { return } + surfaceView.pwd = pwd + + default: + assertionFailure() + } + } + private static func setMouseShape( _ app: ghostty_app_t, target: ghostty_target_s, diff --git a/macos/Sources/Ghostty/SurfaceView.swift b/macos/Sources/Ghostty/SurfaceView.swift index 738c6331f..25fa99a2e 100644 --- a/macos/Sources/Ghostty/SurfaceView.swift +++ b/macos/Sources/Ghostty/SurfaceView.swift @@ -92,6 +92,7 @@ extension Ghostty { Surface(view: surfaceView, size: geo.size) .focused($surfaceFocus) .focusedValue(\.ghosttySurfaceTitle, surfaceView.title) + .focusedValue(\.ghosttySurfacePwd, surfaceView.pwd) .focusedValue(\.ghosttySurfaceView, surfaceView) .focusedValue(\.ghosttySurfaceCellSize, surfaceView.cellSize) #if canImport(AppKit) @@ -512,9 +513,7 @@ extension FocusedValues { struct FocusedGhosttySurface: FocusedValueKey { typealias Value = Ghostty.SurfaceView } -} -extension FocusedValues { var ghosttySurfaceTitle: String? { get { self[FocusedGhosttySurfaceTitle.self] } set { self[FocusedGhosttySurfaceTitle.self] = newValue } @@ -523,9 +522,16 @@ extension FocusedValues { struct FocusedGhosttySurfaceTitle: FocusedValueKey { typealias Value = String } -} -extension FocusedValues { + var ghosttySurfacePwd: String? { + get { self[FocusedGhosttySurfacePwd.self] } + set { self[FocusedGhosttySurfacePwd.self] = newValue } + } + + struct FocusedGhosttySurfacePwd: FocusedValueKey { + typealias Value = String + } + var ghosttySurfaceZoomed: Bool? { get { self[FocusedGhosttySurfaceZoomed.self] } set { self[FocusedGhosttySurfaceZoomed.self] = newValue } @@ -534,9 +540,7 @@ extension FocusedValues { struct FocusedGhosttySurfaceZoomed: FocusedValueKey { typealias Value = Bool } -} -extension FocusedValues { var ghosttySurfaceCellSize: OSSize? { get { self[FocusedGhosttySurfaceCellSize.self] } set { self[FocusedGhosttySurfaceCellSize.self] = newValue } diff --git a/macos/Sources/Ghostty/SurfaceView_AppKit.swift b/macos/Sources/Ghostty/SurfaceView_AppKit.swift index 512a5239b..6f72f74cc 100644 --- a/macos/Sources/Ghostty/SurfaceView_AppKit.swift +++ b/macos/Sources/Ghostty/SurfaceView_AppKit.swift @@ -14,6 +14,10 @@ extension Ghostty { // to the app level and it is set from there. @Published var title: String = "👻" + // The current pwd of the surface as defined by the pty. This can be + // changed with escape codes. + @Published var pwd: String? = nil + // The cell size of this surface. This is set by the core when the // surface is first created and any time the cell size changes (i.e. // when the font size changes). This is used to allow windows to be @@ -71,17 +75,6 @@ extension Ghostty { return ghostty_surface_needs_confirm_quit(surface) } - /// Returns the pwd of the surface if it has one. - var pwd: String? { - guard let surface = self.surface else { return nil } - let v = String(unsafeUninitializedCapacity: 1024) { - Int(ghostty_surface_pwd(surface, $0.baseAddress, UInt($0.count))) - } - - if (v.count == 0) { return nil } - return v - } - // Returns the inspector instance for this surface, or nil if the // surface has been closed. var inspector: ghostty_inspector_t? {