Skip to content

Commit

Permalink
Introduce reset_window_size keybinding and apprt action (ghostty-or…
Browse files Browse the repository at this point in the history
…g#6038)

Related to ghostty-org#6035

This implements the keybind/action portion of ghostty-org#5974 so that this can
have a binding and so that other apprts can respond to this and
implement it this way.
  • Loading branch information
mitchellh authored Mar 1, 2025
2 parents c6485b9 + 17cae57 commit efc1b10
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 2 deletions.
1 change: 1 addition & 0 deletions include/ghostty.h
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,7 @@ typedef enum {
GHOSTTY_ACTION_TOGGLE_SPLIT_ZOOM,
GHOSTTY_ACTION_PRESENT_TERMINAL,
GHOSTTY_ACTION_SIZE_LIMIT,
GHOSTTY_ACTION_RESET_WINDOW_SIZE,
GHOSTTY_ACTION_INITIAL_SIZE,
GHOSTTY_ACTION_CELL_SIZE,
GHOSTTY_ACTION_INSPECTOR,
Expand Down
2 changes: 2 additions & 0 deletions macos/Sources/App/macOS/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class AppDelegate: NSObject,
@IBOutlet private var menuSelectSplitBelow: NSMenuItem?
@IBOutlet private var menuSelectSplitLeft: NSMenuItem?
@IBOutlet private var menuSelectSplitRight: NSMenuItem?
@IBOutlet private var menuReturnToDefaultSize: NSMenuItem?

@IBOutlet private var menuIncreaseFontSize: NSMenuItem?
@IBOutlet private var menuDecreaseFontSize: NSMenuItem?
Expand Down Expand Up @@ -386,6 +387,7 @@ class AppDelegate: NSObject,
syncMenuShortcut(config, action: "resize_split:right,10", menuItem: self.menuMoveSplitDividerRight)
syncMenuShortcut(config, action: "resize_split:left,10", menuItem: self.menuMoveSplitDividerLeft)
syncMenuShortcut(config, action: "equalize_splits", menuItem: self.menuEqualizeSplits)
syncMenuShortcut(config, action: "reset_window_size", menuItem: self.menuReturnToDefaultSize)

syncMenuShortcut(config, action: "increase_font_size:1", menuItem: self.menuIncreaseFontSize)
syncMenuShortcut(config, action: "decrease_font_size:1", menuItem: self.menuDecreaseFontSize)
Expand Down
1 change: 1 addition & 0 deletions macos/Sources/App/macOS/MainMenu.xib
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<outlet property="menuQuit" destination="4sb-4s-VLi" id="qYN-S1-6UW"/>
<outlet property="menuReloadConfig" destination="KKH-XX-5py" id="Wvp-7J-wqX"/>
<outlet property="menuResetFontSize" destination="Jah-MY-aLX" id="ger-qM-wrm"/>
<outlet property="menuReturnToDefaultSize" destination="Gbx-Vi-OGC" id="po9-qC-Iz6"/>
<outlet property="menuSecureInput" destination="oC6-w4-qI7" id="PCc-pe-Mda"/>
<outlet property="menuSelectAll" destination="q2h-lq-e4r" id="s98-r1-Jcv"/>
<outlet property="menuSelectSplitAbove" destination="0yU-hC-8xF" id="aPc-lS-own"/>
Expand Down
14 changes: 13 additions & 1 deletion macos/Sources/Features/Terminal/TerminalController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ class TerminalController: BaseTerminalController {
selector: #selector(onCloseTab),
name: .ghosttyCloseTab,
object: nil)
center.addObserver(
self,
selector: #selector(onResetWindowSize),
name: .ghosttyResetWindowSize,
object: nil
)
center.addObserver(
self,
selector: #selector(ghosttyConfigDidChange(_:)),
Expand Down Expand Up @@ -612,7 +618,7 @@ class TerminalController: BaseTerminalController {
window.close()
}

@IBAction func returnToDefaultSize(_ sender: Any) {
@IBAction func returnToDefaultSize(_ sender: Any?) {
guard let defaultSize else { return }
window?.setFrame(defaultSize, display: true)
}
Expand Down Expand Up @@ -830,6 +836,12 @@ class TerminalController: BaseTerminalController {
closeTab(self)
}

@objc private func onResetWindowSize(notification: SwiftUI.Notification) {
guard let target = notification.object as? Ghostty.SurfaceView else { return }
guard surfaceTree?.contains(view: target) ?? false else { return }
returnToDefaultSize(nil)
}

@objc private func onToggleFullscreen(notification: SwiftUI.Notification) {
guard let target = notification.object as? Ghostty.SurfaceView else { return }
guard target == self.focusedSurface else { return }
Expand Down
27 changes: 26 additions & 1 deletion macos/Sources/Ghostty/Ghostty.App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,9 @@ extension Ghostty {
case GHOSTTY_ACTION_INITIAL_SIZE:
setInitialSize(app, target: target, v: action.action.initial_size)

case GHOSTTY_ACTION_RESET_WINDOW_SIZE:
resetWindowSize(app, target: target)

case GHOSTTY_ACTION_CELL_SIZE:
setCellSize(app, target: target, v: action.action.cell_size)

Expand Down Expand Up @@ -1131,7 +1134,7 @@ extension Ghostty {
v: ghostty_action_initial_size_s) {
switch (target.tag) {
case GHOSTTY_TARGET_APP:
Ghostty.logger.warning("mouse over link does nothing with an app target")
Ghostty.logger.warning("initial size does nothing with an app target")
return

case GHOSTTY_TARGET_SURFACE:
Expand All @@ -1145,6 +1148,28 @@ extension Ghostty {
}
}

private static func resetWindowSize(
_ app: ghostty_app_t,
target: ghostty_target_s) {
switch (target.tag) {
case GHOSTTY_TARGET_APP:
Ghostty.logger.warning("reset window size 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 }
NotificationCenter.default.post(
name: .ghosttyResetWindowSize,
object: surfaceView
)


default:
assertionFailure()
}
}

private static func setCellSize(
_ app: ghostty_app_t,
target: ghostty_target_s,
Expand Down
3 changes: 3 additions & 0 deletions macos/Sources/Ghostty/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ extension Notification.Name {

/// Close tab
static let ghosttyCloseTab = Notification.Name("com.mitchellh.ghostty.closeTab")

/// Resize the window to a default size.
static let ghosttyResetWindowSize = Notification.Name("com.mitchellh.ghostty.resetWindowSize")
}

// NOTE: I am moving all of these to Notification.Name extensions over time. This
Expand Down
6 changes: 6 additions & 0 deletions src/Surface.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4228,6 +4228,12 @@ pub fn performBindingAction(self: *Surface, action: input.Binding.Action) !bool
{},
),

.reset_window_size => return try self.rt_app.performAction(
.{ .surface = self },
.reset_window_size,
{},
),

.toggle_maximize => return try self.rt_app.performAction(
.{ .surface = self },
.toggle_maximize,
Expand Down
5 changes: 5 additions & 0 deletions src/apprt/action.zig
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ pub const Action = union(Key) {
/// Sets a size limit (in pixels) for the target terminal.
size_limit: SizeLimit,

/// Resets the window size to the default size. See the
/// `reset_window_size` keybinding for more information.
reset_window_size,

/// Specifies the initial size of the target terminal.
///
/// This may be sent once during the initialization of a surface
Expand Down Expand Up @@ -259,6 +263,7 @@ pub const Action = union(Key) {
toggle_split_zoom,
present_terminal,
size_limit,
reset_window_size,
initial_size,
cell_size,
inspector,
Expand Down
1 change: 1 addition & 0 deletions src/apprt/glfw.zig
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ pub const App = struct {
.config_change,
.toggle_maximize,
.prompt_title,
.reset_window_size,
=> {
log.info("unimplemented action={}", .{action});
return false;
Expand Down
1 change: 1 addition & 0 deletions src/apprt/gtk/App.zig
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ pub fn performAction(
.render_inspector,
.renderer_health,
.color_change,
.reset_window_size,
=> {
log.warn("unimplemented action={}", .{action});
return false;
Expand Down
6 changes: 6 additions & 0 deletions src/input/Binding.zig
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,11 @@ pub const Action = union(enum) {
/// Equalize all splits in the current window
equalize_splits: void,

/// Reset the window to the default size. The "default size" is the
/// size that a new window would be created with. This has no effect
/// if the window is fullscreen.
reset_window_size: void,

/// Control the terminal inspector visibility.
///
/// Arguments:
Expand Down Expand Up @@ -772,6 +777,7 @@ pub const Action = union(enum) {
.toggle_fullscreen,
.toggle_window_decorations,
.toggle_secure_input,
.reset_window_size,
.crash,
=> .surface,

Expand Down

0 comments on commit efc1b10

Please sign in to comment.