From f966a5a1633749e0e95eea09c4fe7ac620ad84bf Mon Sep 17 00:00:00 2001 From: Gregory Anders Date: Mon, 16 Oct 2023 20:17:29 -0500 Subject: [PATCH 1/2] macos: add menu items to modify font size Add a new "View" menu to the menu bar with items to increase, decrease, or reset the font size. --- macos/Sources/AppDelegate.swift | 27 +++++++++++++++++++++++++-- macos/Sources/Ghostty/AppState.swift | 20 ++++++++++++++++++++ macos/Sources/MainMenu.xib | 28 ++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/macos/Sources/AppDelegate.swift b/macos/Sources/AppDelegate.swift index e23214154..9bdd637bd 100644 --- a/macos/Sources/AppDelegate.swift +++ b/macos/Sources/AppDelegate.swift @@ -32,7 +32,11 @@ class AppDelegate: NSObject, ObservableObject, NSApplicationDelegate, GhosttyApp @IBOutlet private var menuSelectSplitBelow: NSMenuItem? @IBOutlet private var menuSelectSplitLeft: NSMenuItem? @IBOutlet private var menuSelectSplitRight: NSMenuItem? - + + @IBOutlet private var menuIncreaseFontSize: NSMenuItem? + @IBOutlet private var menuDecreaseFontSize: NSMenuItem? + @IBOutlet private var menuResetFontSize: NSMenuItem? + /// The dock menu private var dockMenu: NSMenu = NSMenu() @@ -204,7 +208,11 @@ class AppDelegate: NSObject, ObservableObject, NSApplicationDelegate, GhosttyApp syncMenuShortcut(action: "goto_split:bottom", menuItem: self.menuSelectSplitBelow) syncMenuShortcut(action: "goto_split:left", menuItem: self.menuSelectSplitLeft) syncMenuShortcut(action: "goto_split:right", menuItem: self.menuSelectSplitRight) - + + syncMenuShortcut(action: "increase_font_size:1", menuItem: self.menuIncreaseFontSize) + syncMenuShortcut(action: "decrease_font_size:1", menuItem: self.menuDecreaseFontSize) + syncMenuShortcut(action: "reset_font_size", menuItem: self.menuResetFontSize) + // Dock menu reloadDockMenu() } @@ -367,4 +375,19 @@ class AppDelegate: NSObject, ObservableObject, NSApplicationDelegate, GhosttyApp guard let surface = focusedSurface() else { return } ghostty.toggleFullscreen(surface: surface) } + + @IBAction func increaseFontSize(_ sender: Any) { + guard let surface = focusedSurface() else { return } + ghostty.changeFontSize(surface: surface, .increase(1)) + } + + @IBAction func decreaseFontSize(_ sender: Any) { + guard let surface = focusedSurface() else { return } + ghostty.changeFontSize(surface: surface, .decrease(1)) + } + + @IBAction func resetFontSize(_ sender: Any) { + guard let surface = focusedSurface() else { return } + ghostty.changeFontSize(surface: surface, .reset) + } } diff --git a/macos/Sources/Ghostty/AppState.swift b/macos/Sources/Ghostty/AppState.swift index d3a4ba7d1..728f6aa95 100644 --- a/macos/Sources/Ghostty/AppState.swift +++ b/macos/Sources/Ghostty/AppState.swift @@ -11,6 +11,12 @@ extension Ghostty { case loading, error, ready } + enum FontSizeModification { + case increase(Int) + case decrease(Int) + case reset + } + struct Info { var mode: ghostty_build_mode_e var version: String @@ -269,6 +275,20 @@ extension Ghostty { } } + func changeFontSize(surface: ghostty_surface_t, _ change: FontSizeModification) { + let action = switch change { + case .increase(let amount): + "increase_font_size:\(amount)" + case .decrease(let amount): + "decrease_font_size:\(amount)" + case .reset: + "reset_font_size" + } + if (!ghostty_surface_binding_action(surface, action, UInt(action.count))) { + AppDelegate.logger.warning("action failed action=\(action)") + } + } + // Called when the selected keyboard changes. We have to notify Ghostty so that // it can reload the keyboard mapping for input. @objc private func keyboardSelectionDidChange(notification: NSNotification) { diff --git a/macos/Sources/MainMenu.xib b/macos/Sources/MainMenu.xib index 3e20e8476..fa8beb2f0 100644 --- a/macos/Sources/MainMenu.xib +++ b/macos/Sources/MainMenu.xib @@ -17,6 +17,8 @@ + + @@ -24,6 +26,7 @@ + @@ -128,6 +131,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + From 4341428c187df539f4107c2da6ef4d55db3fc897 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 16 Oct 2023 20:05:32 -0700 Subject: [PATCH 2/2] macos: make xcode 14 compatible, do not use switch expr --- macos/Sources/Ghostty/AppState.swift | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/macos/Sources/Ghostty/AppState.swift b/macos/Sources/Ghostty/AppState.swift index 728f6aa95..43f2a5dbf 100644 --- a/macos/Sources/Ghostty/AppState.swift +++ b/macos/Sources/Ghostty/AppState.swift @@ -276,13 +276,14 @@ extension Ghostty { } func changeFontSize(surface: ghostty_surface_t, _ change: FontSizeModification) { - let action = switch change { + let action: String + switch change { case .increase(let amount): - "increase_font_size:\(amount)" + action = "increase_font_size:\(amount)" case .decrease(let amount): - "decrease_font_size:\(amount)" + action = "decrease_font_size:\(amount)" case .reset: - "reset_font_size" + action = "reset_font_size" } if (!ghostty_surface_binding_action(surface, action, UInt(action.count))) { AppDelegate.logger.warning("action failed action=\(action)")