Merge pull request #391 from mitchellh/zoom-split

Zoom/Unzoom Split
This commit is contained in:
Mitchell Hashimoto
2023-09-02 16:55:49 -07:00
committed by GitHub
10 changed files with 266 additions and 86 deletions

View File

@ -262,6 +262,7 @@ typedef void (*ghostty_runtime_new_tab_cb)(void *, ghostty_surface_config_s);
typedef void (*ghostty_runtime_new_window_cb)(void *, ghostty_surface_config_s);
typedef void (*ghostty_runtime_close_surface_cb)(void *, bool);
typedef void (*ghostty_runtime_focus_split_cb)(void *, ghostty_split_focus_direction_e);
typedef void (*ghostty_runtime_toggle_split_zoom_cb)(void *);
typedef void (*ghostty_runtime_goto_tab_cb)(void *, int32_t);
typedef void (*ghostty_runtime_toggle_fullscreen_cb)(void *, ghostty_non_native_fullscreen_e);
@ -278,6 +279,7 @@ typedef struct {
ghostty_runtime_new_window_cb new_window_cb;
ghostty_runtime_close_surface_cb close_surface_cb;
ghostty_runtime_focus_split_cb focus_split_cb;
ghostty_runtime_toggle_split_zoom_cb toggle_split_zoom_cb;
ghostty_runtime_goto_tab_cb goto_tab_cb;
ghostty_runtime_toggle_fullscreen_cb toggle_fullscreen_cb;
} ghostty_runtime_config_s;

View File

@ -26,6 +26,7 @@ struct PrimaryView: View {
@FocusedValue(\.ghosttySurfaceView) private var focusedSurface
@FocusedValue(\.ghosttySurfaceTitle) private var surfaceTitle
@FocusedValue(\.ghosttySurfaceZoomed) private var zoomedSplit
// This is true if this view should be the one to show the quit confirmation.
var ownsQuitConfirmation: Bool {
@ -49,6 +50,25 @@ struct PrimaryView: View {
return window == firstWindow
}
// The title for our window
private var title: String {
var title = "👻"
if let surfaceTitle = surfaceTitle {
if (surfaceTitle.count > 0) {
title = surfaceTitle
}
}
if let zoomedSplit = zoomedSplit {
if zoomedSplit {
title = "🔍 " + title
}
}
return title
}
var body: some View {
switch ghostty.readiness {
case .loading:
@ -84,12 +104,11 @@ struct PrimaryView: View {
.onChange(of: focusedSurface) { newValue in
self.focusedSurfaceWrapper.surface = newValue?.surface
}
.onChange(of: surfaceTitle) { newValue in
.onChange(of: title) { newValue in
// We need to handle this manually because we are using AppKit lifecycle
// so navigationTitle no longer works.
guard let window = self.window else { return }
guard let title = newValue else { return }
window.title = title
window.title = newValue
}
.confirmationDialog(
"Quit Ghostty?",

View File

@ -73,6 +73,7 @@ extension Ghostty {
new_window_cb: { userdata, surfaceConfig in AppState.newWindow(userdata, config: surfaceConfig) },
close_surface_cb: { userdata, processAlive in AppState.closeSurface(userdata, processAlive: processAlive) },
focus_split_cb: { userdata, direction in AppState.focusSplit(userdata, direction: direction) },
toggle_split_zoom_cb: { userdata in AppState.toggleSplitZoom(userdata) },
goto_tab_cb: { userdata, n in AppState.gotoTab(userdata, n: n) },
toggle_fullscreen_cb: { userdata, nonNativeFullscreen in AppState.toggleFullscreen(userdata, nonNativeFullscreen: nonNativeFullscreen) }
)
@ -205,6 +206,15 @@ extension Ghostty {
)
}
static func toggleSplitZoom(_ userdata: UnsafeMutableRawPointer?) {
guard let surface = self.surfaceUserdata(from: userdata) else { return }
NotificationCenter.default.post(
name: Notification.didToggleSplitZoom,
object: surface
)
}
static func gotoTab(_ userdata: UnsafeMutableRawPointer?, n: Int32) {
guard let surface = self.surfaceUserdata(from: userdata) else { return }
NotificationCenter.default.post(

View File

@ -6,13 +6,34 @@ extension Ghostty {
/// view. The terminal starts in the unsplit state (a plain ol' TerminalView) but responds to changes to the
/// split direction by splitting the terminal.
struct TerminalSplit: View {
@Environment(\.ghosttyApp) private var app
let onClose: (() -> Void)?
let baseConfig: ghostty_surface_config_s?
@Environment(\.ghosttyApp) private var app
/// Non-nil if one of the surfaces in the split tree is currently "zoomed." A zoomed surface
/// becomes "full screen" on the split tree.
@State private var zoomedSurface: SurfaceView? = nil
var body: some View {
if let app = app {
TerminalSplitRoot(app: app, onClose: onClose, baseConfig: baseConfig)
ZStack {
TerminalSplitRoot(
app: app,
zoomedSurface: $zoomedSurface,
onClose: onClose,
baseConfig: baseConfig
)
// If we have a zoomed surface, we overlay that on top of our split
// root. Our split root will become clear when there is a zoomed
// surface. We need to keep the split root around so that we don't
// lose all of the surface state so this must be a ZStack.
if let surfaceView = zoomedSurface {
SurfaceWrapper(surfaceView: surfaceView)
}
}
.focusedValue(\.ghosttySurfaceZoomed, zoomedSurface != nil)
}
}
}
@ -63,6 +84,22 @@ extension Ghostty {
}
}
/// Returns true if the split tree contains the given view.
func contains(view: SurfaceView) -> Bool {
switch (self) {
case .noSplit(let leaf):
return leaf.surface == view
case .horizontal(let container):
return container.topLeft.contains(view: view) ||
container.bottomRight.contains(view: view)
case .vertical(let container):
return container.topLeft.contains(view: view) ||
container.bottomRight.contains(view: view)
}
}
class Leaf: ObservableObject {
let app: ghostty_app_t
@Published var surface: SurfaceView
@ -144,15 +181,32 @@ extension Ghostty {
let onClose: (() -> Void)?
let baseConfig: ghostty_surface_config_s?
/// Keeps track of whether we're in a zoomed split state or not. If one of the splits we own
/// is in the zoomed state, we clear our body since we expect a zoomed split to overlay
/// this one.
@Binding var zoomedSurface: SurfaceView?
@FocusedValue(\.ghosttySurfaceTitle) private var surfaceTitle: String?
init(app: ghostty_app_t, onClose: (() ->Void)? = nil, baseConfig: ghostty_surface_config_s? = nil) {
init(app: ghostty_app_t,
zoomedSurface: Binding<SurfaceView?>,
onClose: (() ->Void)? = nil,
baseConfig: ghostty_surface_config_s? = nil) {
self.onClose = onClose
self.baseConfig = baseConfig
self._zoomedSurface = zoomedSurface
_node = State(wrappedValue: SplitNode.noSplit(.init(app, baseConfig)))
}
var body: some View {
let center = NotificationCenter.default
let pubZoom = center.publisher(for: Notification.didToggleSplitZoom)
// If we're zoomed, we don't render anything, we are transparent. This
// ensures that the View stays around so we don't lose our state, but
// also that the zoomed view on top can see through if background transparency
// is enabled.
if (zoomedSurface == nil) {
ZStack {
switch (node) {
case .noSplit(let leaf):
@ -180,6 +234,7 @@ extension Ghostty {
node: $node,
container: container
)
.onReceive(pubZoom) { onZoom(notification: $0) }
case .vertical(let container):
TerminalSplitContainer(
@ -188,9 +243,65 @@ extension Ghostty {
node: $node,
container: container
)
.onReceive(pubZoom) { onZoom(notification: $0) }
}
}
.navigationTitle(surfaceTitle ?? "Ghostty")
} else {
// On these events we want to reset the split state and call it.
let pubSplit = center.publisher(for: Notification.ghosttyNewSplit, object: zoomedSurface!)
let pubClose = center.publisher(for: Notification.ghosttyCloseSurface, object: zoomedSurface!)
let pubFocus = center.publisher(for: Notification.ghosttyFocusSplit, object: zoomedSurface!)
ZStack {}
.onReceive(pubZoom) { onZoomReset(notification: $0) }
.onReceive(pubSplit) { onZoomReset(notification: $0) }
.onReceive(pubClose) { onZoomReset(notification: $0) }
.onReceive(pubFocus) { onZoomReset(notification: $0) }
}
}
func onZoom(notification: SwiftUI.Notification) {
// Our node must be split to receive zooms. You can't zoom an unsplit terminal.
if case .noSplit = node {
preconditionFailure("TerminalSplitRoom must not be zoom-able if no splits exist")
}
// Make sure the notification has a surface and that this window owns the surface.
guard let surfaceView = notification.object as? SurfaceView else { return }
guard node.contains(view: surfaceView) else { return }
// We are in the zoomed state.
zoomedSurface = surfaceView
// See onZoomReset, same logic.
DispatchQueue.main.async { Ghostty.moveFocus(to: surfaceView) }
}
func onZoomReset(notification: SwiftUI.Notification) {
// Make sure the notification has a surface and that this window owns the surface.
guard let surfaceView = notification.object as? SurfaceView else { return }
guard zoomedSurface == surfaceView else { return }
// We are now unzoomed
zoomedSurface = nil
// We need to stay focused on this view, but the view is going to change
// superviews. We need to do this async so it happens on the next event loop
// tick.
DispatchQueue.main.async {
Ghostty.moveFocus(to: surfaceView)
// If the notification is not a toggle zoom notification, we want to re-publish
// it after a short delay so that the split tree has a chance to re-establish
// so the proper view gets this notification.
if (notification.name != Notification.didToggleSplitZoom) {
// We have to wait ANOTHER tick since we just established.
DispatchQueue.main.async {
NotificationCenter.default.post(notification)
}
}
}
}
}
@ -285,7 +396,7 @@ extension Ghostty {
}
// See moveFocus comment, we have to run this whenever split changes.
Self.moveFocus(container.bottomRight, previous: node)
Ghostty.moveFocus(to: container.bottomRight.preferredFocus(), from: node.preferredFocus())
}
/// This handles the event to move the split focus (i.e. previous/next) from a keyboard event.
@ -294,48 +405,7 @@ extension Ghostty {
guard let directionAny = notification.userInfo?[Notification.SplitDirectionKey] else { return }
guard let direction = directionAny as? SplitFocusDirection else { return }
guard let next = neighbors.get(direction: direction) else { return }
Self.moveFocus(next, previous: node)
}
/// There is a bug I can't figure out where when changing the split state, the terminal view
/// will lose focus. There has to be some nice SwiftUI-native way to fix this but I can't
/// figure it out so we're going to do this hacky thing to bring focus back to the terminal
/// that should have it.
fileprivate static func moveFocus(_ target: SplitNode, previous: SplitNode) {
let view = target.preferredFocus()
DispatchQueue.main.async {
// If the callback runs before the surface is attached to a view
// then the window will be nil. We just reschedule in that case.
guard let window = view.window else {
self.moveFocus(target, previous: previous)
return
}
window.makeFirstResponder(view)
// If we had a previously focused node and its not where we're sending
// focus, make sure that we explicitly tell it to lose focus. In theory
// we should NOT have to do this but the focus callback isn't getting
// called for some reason.
let previous = previous.preferredFocus()
if previous != view {
_ = previous.resignFirstResponder()
}
// On newer versions of macOS everything above works great so we're done.
if #available(macOS 13, *) { return }
// On macOS 12, splits do not properly gain focus. I don't know why, but
// it seems like the `focused` SwiftUI method doesn't work. We use
// NotificationCenter as a blunt force instrument to make it work.
if #available(macOS 12, *) {
NotificationCenter.default.post(
name: Notification.didBecomeFocusedSurface,
object: view
)
}
}
Ghostty.moveFocus(to: next.preferredFocus(), from: node.preferredFocus())
}
}
@ -369,7 +439,7 @@ extension Ghostty {
// When closing the topLeft, our parent becomes the bottomRight.
node = container.bottomRight
TerminalSplitLeaf.moveFocus(node, previous: container.topLeft)
Ghostty.moveFocus(to: node.preferredFocus(), from: container.topLeft.preferredFocus())
}
}, right: {
let neighborKey: WritableKeyPath<SplitNode.Neighbors, SplitNode?> = direction == .horizontal ? \.left : \.top
@ -390,7 +460,7 @@ extension Ghostty {
// When closing the bottomRight, our parent becomes the topLeft.
node = container.topLeft
TerminalSplitLeaf.moveFocus(node, previous: container.bottomRight)
Ghostty.moveFocus(to: node.preferredFocus(), from: container.bottomRight.preferredFocus())
}
})
}
@ -431,4 +501,42 @@ extension Ghostty {
}
}
}
/// There is a bug I can't figure out where when changing the split state, the terminal view
/// will lose focus. There has to be some nice SwiftUI-native way to fix this but I can't
/// figure it out so we're going to do this hacky thing to bring focus back to the terminal
/// that should have it.
fileprivate static func moveFocus(to: SurfaceView, from: SurfaceView? = nil) {
DispatchQueue.main.async {
// If the callback runs before the surface is attached to a view
// then the window will be nil. We just reschedule in that case.
guard let window = to.window else {
moveFocus(to: to, from: from)
return
}
// If we had a previously focused node and its not where we're sending
// focus, make sure that we explicitly tell it to lose focus. In theory
// we should NOT have to do this but the focus callback isn't getting
// called for some reason.
if let from = from {
_ = from.resignFirstResponder()
}
window.makeFirstResponder(to)
// On newer versions of macOS everything above works great so we're done.
if #available(macOS 13, *) { return }
// On macOS 12, splits do not properly gain focus. I don't know why, but
// it seems like the `focused` SwiftUI method doesn't work. We use
// NotificationCenter as a blunt force instrument to make it work.
if #available(macOS 12, *) {
NotificationCenter.default.post(
name: Notification.didBecomeFocusedSurface,
object: to
)
}
}
}
}

View File

@ -95,6 +95,9 @@ extension Ghostty.Notification {
/// Notification that a surface is becoming focused. This is only sent on macOS 12 to
/// work around bugs. macOS 13+ should use the ".focused()" attribute.
static let didBecomeFocusedSurface = Notification.Name("com.mitchellh.ghostty.didBecomeFocusedSurface")
/// Notification sent to toggle split maximize/unmaximize.
static let didToggleSplitZoom = Notification.Name("com.mitchellh.ghostty.didToggleSplitZoom")
}
// Make the input enum hashable.

View File

@ -710,3 +710,13 @@ extension FocusedValues {
}
}
extension FocusedValues {
var ghosttySurfaceZoomed: Bool? {
get { self[FocusedGhosttySurfaceZoomed.self] }
set { self[FocusedGhosttySurfaceZoomed.self] = newValue }
}
struct FocusedGhosttySurfaceZoomed: FocusedValueKey {
typealias Value = Bool
}
}

View File

@ -2118,6 +2118,12 @@ pub fn performBindingAction(self: *Surface, action: input.Binding.Action) !void
} else log.warn("runtime doesn't implement gotoSplit", .{});
},
.toggle_split_zoom => {
if (@hasDecl(apprt.Surface, "toggleSplitZoom")) {
self.rt_surface.toggleSplitZoom();
} else log.warn("runtime doesn't implement toggleSplitZoom", .{});
},
.toggle_fullscreen => {
if (@hasDecl(apprt.Surface, "toggleFullscreen")) {
self.rt_surface.toggleFullscreen(self.config.macos_non_native_fullscreen);

View File

@ -72,6 +72,9 @@ pub const App = struct {
/// Focus the previous/next split (if any).
focus_split: ?*const fn (SurfaceUD, input.SplitFocusDirection) callconv(.C) void = null,
/// Zoom the current split.
toggle_split_zoom: ?*const fn (SurfaceUD) callconv(.C) void = null,
/// Goto tab
goto_tab: ?*const fn (SurfaceUD, usize) callconv(.C) void = null,
@ -270,6 +273,15 @@ pub const Surface = struct {
func(self.opts.userdata, direction);
}
pub fn toggleSplitZoom(self: *const Surface) void {
const func = self.app.opts.toggle_split_zoom orelse {
log.info("runtime embedder does not support split zoom", .{});
return;
};
func(self.opts.userdata);
}
pub fn getContentScale(self: *const Surface) !apprt.ContentScale {
return self.content_scale;
}

View File

@ -563,6 +563,13 @@ pub const Config = struct {
.{ .toggle_fullscreen = {} },
);
// Toggle zoom a split
try result.keybind.set.put(
alloc,
.{ .key = .enter, .mods = ctrlOrSuper(.{ .shift = true }) },
.{ .toggle_split_zoom = {} },
);
// Mac-specific keyboard bindings.
if (comptime builtin.target.isDarwin()) {
try result.keybind.set.put(

View File

@ -158,6 +158,9 @@ pub const Action = union(enum) {
/// Focus on a split in a given direction.
goto_split: SplitFocusDirection,
/// zoom/unzoom the current split.
toggle_split_zoom: void,
/// Reload the configuration. The exact meaning depends on the app runtime
/// in use but this usually involves re-reading the configuration file
/// and applying any changes. Note that not all changes can be applied at