From 11d5ec7dc1187179f3dc2d20bbcb472d43b2785a Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 28 Sep 2024 18:42:13 -0700 Subject: [PATCH] config: support quick terminal position --- macos/Sources/App/macOS/AppDelegate.swift | 5 +- .../QuickTerminal/QuickTerminalPosition.swift | 62 +++++++++++++------ macos/Sources/Ghostty/Ghostty.Config.swift | 10 +++ src/config/Config.zig | 15 +++++ src/input/Binding.zig | 4 -- 5 files changed, 72 insertions(+), 24 deletions(-) diff --git a/macos/Sources/App/macOS/AppDelegate.swift b/macos/Sources/App/macOS/AppDelegate.swift index ad4c9bbda..5980b8d66 100644 --- a/macos/Sources/App/macOS/AppDelegate.swift +++ b/macos/Sources/App/macOS/AppDelegate.swift @@ -553,7 +553,10 @@ class AppDelegate: NSObject, @IBAction func toggleQuickTerminal(_ sender: Any) { if quickController == nil { - quickController = QuickTerminalController(ghostty, baseConfig: nil) + quickController = QuickTerminalController( + ghostty, + position: ghostty.config.quickTerminalPosition + ) } guard let quickController = self.quickController else { return } diff --git a/macos/Sources/Features/QuickTerminal/QuickTerminalPosition.swift b/macos/Sources/Features/QuickTerminal/QuickTerminalPosition.swift index c7509c465..559d7ef88 100644 --- a/macos/Sources/Features/QuickTerminal/QuickTerminalPosition.swift +++ b/macos/Sources/Features/QuickTerminal/QuickTerminalPosition.swift @@ -1,19 +1,30 @@ import Cocoa -enum QuickTerminalPosition { +enum QuickTerminalPosition : String { case top + case bottom + case left + case right /// Set the loaded state for a window. func setLoaded(_ window: NSWindow) { guard let screen = window.screen ?? NSScreen.main else { return } switch (self) { - case .top: + case .top, .bottom: window.setFrame(.init( origin: window.frame.origin, size: .init( width: screen.frame.width, height: screen.frame.height / 4) ), display: false) + + case .left, .right: + window.setFrame(.init( + origin: window.frame.origin, + size: .init( + width: screen.frame.width / 4, + height: screen.frame.height) + ), display: false) } } @@ -23,15 +34,10 @@ enum QuickTerminalPosition { window.alphaValue = 0 // Position depends - switch (self) { - case .top: - window.setFrame(.init( - origin: initialOrigin(for: window, on: screen), - size: .init( - width: screen.frame.width, - height: window.frame.height) - ), display: false) - } + window.setFrame(.init( + origin: initialOrigin(for: window, on: screen), + size: window.frame.size + ), display: false) } /// Set the final state for a window in this position. @@ -40,21 +46,21 @@ enum QuickTerminalPosition { window.alphaValue = 1 // Position depends - switch (self) { - case .top: - window.setFrame(.init( - origin: finalOrigin(for: window, on: screen), - size: window.frame.size - ), display: true) - } + window.setFrame(.init( + origin: finalOrigin(for: window, on: screen), + size: window.frame.size + ), display: true) } /// Restrict the frame size during resizing. func restrictFrameSize(_ size: NSSize, on screen: NSScreen) -> NSSize { var finalSize = size switch (self) { - case .top: + case .top, .bottom: finalSize.width = screen.frame.width + + case .left, .right: + finalSize.height = screen.frame.height } return finalSize @@ -65,6 +71,15 @@ enum QuickTerminalPosition { switch (self) { case .top: return .init(x: 0, y: screen.frame.maxY) + + case .bottom: + return .init(x: 0, y: -window.frame.height) + + case .left: + return .init(x: -window.frame.width, y: 0) + + case .right: + return .init(x: screen.frame.maxX, y: 0) } } @@ -73,6 +88,15 @@ enum QuickTerminalPosition { switch (self) { case .top: return .init(x: window.frame.origin.x, y: screen.visibleFrame.maxY - window.frame.height) + + case .bottom: + return .init(x: window.frame.origin.x, y: 0) + + case .left: + return .init(x: 0, y: window.frame.origin.y) + + case .right: + return .init(x: screen.visibleFrame.maxX - window.frame.width, y: window.frame.origin.y) } } } diff --git a/macos/Sources/Ghostty/Ghostty.Config.swift b/macos/Sources/Ghostty/Ghostty.Config.swift index 7ecd45cc4..441172ea0 100644 --- a/macos/Sources/Ghostty/Ghostty.Config.swift +++ b/macos/Sources/Ghostty/Ghostty.Config.swift @@ -332,6 +332,16 @@ extension Ghostty { return Color(newColor) } + var quickTerminalPosition: QuickTerminalPosition { + guard let config = self.config else { return .top } + var v: UnsafePointer? = nil + let key = "quick-terminal-position" + guard ghostty_config_get(config, &v, key, UInt(key.count)) else { return .top } + guard let ptr = v else { return .top } + let str = String(cString: ptr) + return QuickTerminalPosition(rawValue: str) ?? .top + } + var resizeOverlay: ResizeOverlay { guard let config = self.config else { return .after_first } var v: UnsafePointer? = nil diff --git a/src/config/Config.zig b/src/config/Config.zig index efa741307..6a0818095 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -1220,6 +1220,13 @@ keybind: Keybinds = .{}, /// window is ever created. Only implemented on Linux. @"initial-window": bool = true, +/// The position of the "quick" terminal window. To learn more about the +/// quick terminal, see the documentation for the `toggle_quick_terminal` +/// binding action. +/// +/// Changing this configuration requires restarting Ghostty completely. +@"quick-terminal-position": QuickTerminalPosition = .top, + /// Whether to enable shell integration auto-injection or not. Shell integration /// greatly enhances the terminal experience by enabling a number of features: /// @@ -4401,6 +4408,14 @@ pub const ResizeOverlayPosition = enum { @"bottom-right", }; +/// See quick-terminal-position +pub const QuickTerminalPosition = enum { + top, + bottom, + left, + right, +}; + /// See grapheme-width-method pub const GraphemeWidthMethod = enum { legacy, diff --git a/src/input/Binding.zig b/src/input/Binding.zig index 36d87ae3e..bef2e2209 100644 --- a/src/input/Binding.zig +++ b/src/input/Binding.zig @@ -402,10 +402,6 @@ pub const Action = union(enum) { /// crash: CrashThread, - pub const QuickTerminalPosition = enum { - top, - }; - pub const CrashThread = enum { main, io,