macos: add reload configuration to the Ghostty menu bar

This commit is contained in:
Mitchell Hashimoto
2023-09-11 13:49:20 -07:00
parent 4c0871b6dc
commit 711e3a5043
4 changed files with 37 additions and 15 deletions

View File

@@ -15,6 +15,7 @@ class AppDelegate: NSObject, ObservableObject, NSApplicationDelegate, GhosttyApp
@Published var confirmQuit: Bool = false
/// Various menu items so that we can programmatically sync the keyboard shortcut with the Ghostty config.
@IBOutlet private var menuReloadConfig: NSMenuItem?
@IBOutlet private var menuQuit: NSMenuItem?
@IBOutlet private var menuNewWindow: NSMenuItem?
@@ -127,6 +128,7 @@ class AppDelegate: NSObject, ObservableObject, NSApplicationDelegate, GhosttyApp
private func syncMenuShortcuts() {
guard ghostty.config != nil else { return }
syncMenuShortcut(action: "reload_config", menuItem: self.menuReloadConfig)
syncMenuShortcut(action: "quit", menuItem: self.menuQuit)
syncMenuShortcut(action: "new_window", menuItem: self.menuNewWindow)
@@ -186,6 +188,7 @@ class AppDelegate: NSObject, ObservableObject, NSApplicationDelegate, GhosttyApp
// If we have configuration errors, we need to show them.
let c = ConfigurationErrorsController.sharedInstance
c.model.errors = state.configErrors()
Self.logger.warning("TEST did reload, count=\(c.model.errors.count)")
if (c.model.errors.count > 0) { c.showWindow(self) }
}
@@ -202,6 +205,10 @@ class AppDelegate: NSObject, ObservableObject, NSApplicationDelegate, GhosttyApp
//MARK: - IB Actions
@IBAction func reloadConfig(_ sender: Any?) {
ghostty.reloadConfig()
}
@IBAction func newWindow(_ sender: Any?) {
windowManager.newWindow()

View File

@@ -171,6 +171,22 @@ extension Ghostty {
NSApplication.shared.terminate(nil)
}
func reloadConfig() {
guard let newConfig = Self.reloadConfig() else {
AppDelegate.logger.warning("failed to reload configuration")
return
}
// Assign the new config. This will automatically free the old config.
// It is safe to free the old config from within this function call.
config = newConfig
// If we have a delegate, notify.
if let delegate = delegate {
delegate.configDidReload(self)
}
}
/// Request that the given surface is closed. This will trigger the full normal surface close event
/// cycle which will call our close surface callback.
func requestClose(surface: ghostty_surface_t) {
@@ -286,22 +302,9 @@ extension Ghostty {
}
static func reloadConfig(_ userdata: UnsafeMutableRawPointer?) -> ghostty_config_t? {
guard let newConfig = AppState.reloadConfig() else {
AppDelegate.logger.warning("failed to reload configuration")
return nil
}
// Assign the new config. This will automatically free the old config.
// It is safe to free the old config from within this function call.
let state = Unmanaged<AppState>.fromOpaque(userdata!).takeUnretainedValue()
state.config = newConfig
// If we have a delegate, notify.
if let delegate = state.delegate {
delegate.configDidReload(state)
}
return newConfig
state.reloadConfig()
return state.config
}
static func wakeup(_ userdata: UnsafeMutableRawPointer?) {

View File

@@ -23,6 +23,7 @@
<outlet property="menuPaste" destination="i27-pK-umN" id="ICc-X2-gV3"/>
<outlet property="menuPreviousSplit" destination="Lic-px-1wg" id="Rto-CG-yRe"/>
<outlet property="menuQuit" destination="4sb-4s-VLi" id="qYN-S1-6UW"/>
<outlet property="menuReloadConfig" destination="KKH-XX-5py" id="Wvp-7J-wqX"/>
<outlet property="menuSelectSplitAbove" destination="0yU-hC-8xF" id="aPc-lS-own"/>
<outlet property="menuSelectSplitBelow" destination="QDz-d9-CBr" id="FsH-Dq-jij"/>
<outlet property="menuSelectSplitLeft" destination="cTK-oy-KuV" id="Jpr-5q-dqz"/>
@@ -47,6 +48,12 @@
</menuItem>
<menuItem isSeparatorItem="YES" id="VOq-y0-SEH"/>
<menuItem title="Preferences…" keyEquivalent="," id="BOF-NM-1cW"/>
<menuItem title="Reload Configuration" id="KKH-XX-5py">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="reloadConfig:" target="bbz-4X-AYv" id="h5x-tu-Izk"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="4je-JR-u6R"/>
<menuItem title="Hide Ghostty" keyEquivalent="h" id="Olw-nP-bQN">
<connections>

View File

@@ -588,6 +588,11 @@ pub fn default(alloc_gpa: Allocator) Allocator.Error!Config {
.{ .key = .q, .mods = .{ .super = true } },
.{ .quit = {} },
);
try result.keybind.set.put(
alloc,
.{ .key = .comma, .mods = .{ .super = true, .shift = true } },
.{ .reload_config = {} },
);
try result.keybind.set.put(
alloc,