mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-08-02 14:57:31 +03:00
macos: toggle fullscreen
This commit is contained in:
@ -1,7 +1,7 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import Cocoa
|
import Cocoa
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
import Combine
|
import GhosttyKit
|
||||||
|
|
||||||
class TerminalController: NSWindowController, NSWindowDelegate, TerminalViewDelegate {
|
class TerminalController: NSWindowController, NSWindowDelegate, TerminalViewDelegate {
|
||||||
override var windowNibName: NSNib.Name? { "Terminal" }
|
override var windowNibName: NSNib.Name? { "Terminal" }
|
||||||
@ -12,20 +12,31 @@ class TerminalController: NSWindowController, NSWindowDelegate, TerminalViewDele
|
|||||||
/// The currently focused surface.
|
/// The currently focused surface.
|
||||||
var focusedSurface: Ghostty.SurfaceView? = nil
|
var focusedSurface: Ghostty.SurfaceView? = nil
|
||||||
|
|
||||||
|
/// Fullscreen state management.
|
||||||
|
private let fullscreenHandler = FullScreenHandler()
|
||||||
|
|
||||||
init(_ ghostty: Ghostty.AppState) {
|
init(_ ghostty: Ghostty.AppState) {
|
||||||
self.ghostty = ghostty
|
self.ghostty = ghostty
|
||||||
super.init(window: nil)
|
super.init(window: nil)
|
||||||
|
|
||||||
// Register as observer for window-level manipulations that are best handled
|
|
||||||
// here at the controller layer rather than in the SwiftUI stack.
|
|
||||||
let center = NotificationCenter.default
|
let center = NotificationCenter.default
|
||||||
|
center.addObserver(
|
||||||
|
self,
|
||||||
|
selector: #selector(onToggleFullscreen),
|
||||||
|
name: Ghostty.Notification.ghosttyToggleFullscreen,
|
||||||
|
object: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
required init?(coder: NSCoder) {
|
required init?(coder: NSCoder) {
|
||||||
fatalError("init(coder:) is not supported for this view")
|
fatalError("init(coder:) is not supported for this view")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
deinit {
|
||||||
|
// Remove all of our notificationcenter subscriptions
|
||||||
|
let center = NotificationCenter.default
|
||||||
|
center.removeObserver(self)
|
||||||
|
}
|
||||||
|
|
||||||
//MARK: - NSWindowController
|
//MARK: - NSWindowController
|
||||||
|
|
||||||
override func windowWillLoad() {
|
override func windowWillLoad() {
|
||||||
@ -78,4 +89,24 @@ class TerminalController: NSWindowController, NSWindowDelegate, TerminalViewDele
|
|||||||
guard ghostty.windowStepResize else { return }
|
guard ghostty.windowStepResize else { return }
|
||||||
self.window?.contentResizeIncrements = to
|
self.window?.contentResizeIncrements = to
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//MARK: - Notifications
|
||||||
|
|
||||||
|
@objc private func onToggleFullscreen(notification: SwiftUI.Notification) {
|
||||||
|
guard let target = notification.object as? Ghostty.SurfaceView else { return }
|
||||||
|
guard target == self.focusedSurface else { return }
|
||||||
|
|
||||||
|
// We need a window to fullscreen
|
||||||
|
guard let window = self.window else { return }
|
||||||
|
|
||||||
|
// Check whether we use non-native fullscreen
|
||||||
|
guard let useNonNativeFullscreenAny = notification.userInfo?[Ghostty.Notification.NonNativeFullscreenKey] else { return }
|
||||||
|
guard let useNonNativeFullscreen = useNonNativeFullscreenAny as? ghostty_non_native_fullscreen_e else { return }
|
||||||
|
self.fullscreenHandler.toggleFullscreen(window: window, nonNativeFullscreen: useNonNativeFullscreen)
|
||||||
|
|
||||||
|
// For some reason focus always gets lost when we toggle fullscreen, so we set it back.
|
||||||
|
if let focusedSurface {
|
||||||
|
Ghostty.moveFocus(to: focusedSurface)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
import SwiftUI
|
import SwiftUI
|
||||||
import GhosttyKit
|
import GhosttyKit
|
||||||
|
|
||||||
class FullScreenHandler { var previousTabGroup: NSWindowTabGroup?
|
class FullScreenHandler {
|
||||||
|
var previousTabGroup: NSWindowTabGroup?
|
||||||
var previousTabGroupIndex: Int?
|
var previousTabGroupIndex: Int?
|
||||||
var previousContentFrame: NSRect?
|
var previousContentFrame: NSRect?
|
||||||
var previousStyleMask: NSWindow.StyleMask? = nil
|
var previousStyleMask: NSWindow.StyleMask? = nil
|
||||||
|
Reference in New Issue
Block a user