macos: improved comments

This commit is contained in:
Mitchell Hashimoto
2023-10-30 15:14:20 -07:00
parent b010875176
commit b4c973cd23
3 changed files with 19 additions and 14 deletions

View File

@ -3,6 +3,7 @@ import Cocoa
import SwiftUI import SwiftUI
import GhosttyKit import GhosttyKit
/// The terminal controller is an NSWindowController that maps 1:1 to a terminal window.
class TerminalController: NSWindowController, NSWindowDelegate, TerminalViewDelegate, TerminalViewModel { class TerminalController: NSWindowController, NSWindowDelegate, TerminalViewDelegate, TerminalViewModel {
override var windowNibName: NSNib.Name? { "Terminal" } override var windowNibName: NSNib.Name? { "Terminal" }

View File

@ -3,7 +3,8 @@ import SwiftUI
import GhosttyKit import GhosttyKit
import Combine import Combine
/// Manages a set of terminal windows. /// Manages a set of terminal windows. This is effectively an array of TerminalControllers.
/// This abstraction helps manage tabs and multi-window scenarios.
class TerminalManager { class TerminalManager {
struct Window { struct Window {
let controller: TerminalController let controller: TerminalController
@ -48,15 +49,8 @@ class TerminalManager {
} }
deinit { deinit {
let center = NotificationCenter.default; let center = NotificationCenter.default
center.removeObserver( center.removeObserver(self)
self,
name: Ghostty.Notification.ghosttyNewTab,
object: nil)
center.removeObserver(
self,
name: Ghostty.Notification.ghosttyNewWindow,
object: nil)
} }
// MARK: - Window Management // MARK: - Window Management

View File

@ -1,6 +1,9 @@
import SwiftUI import SwiftUI
import GhosttyKit import GhosttyKit
/// This delegate is notified of actions and property changes regarding the terminal view. This
/// delegate is optional and can be used by a TerminalView caller to react to changes such as
/// titles being set, cell sizes being changed, etc.
protocol TerminalViewDelegate: AnyObject, ObservableObject { protocol TerminalViewDelegate: AnyObject, ObservableObject {
/// Called when the currently focused surface changed. This can be nil. /// Called when the currently focused surface changed. This can be nil.
func focusedSurfaceDidChange(to: Ghostty.SurfaceView?) func focusedSurfaceDidChange(to: Ghostty.SurfaceView?)
@ -12,16 +15,23 @@ protocol TerminalViewDelegate: AnyObject, ObservableObject {
func cellSizeDidChange(to: NSSize) func cellSizeDidChange(to: NSSize)
} }
protocol TerminalViewModel: ObservableObject { // Default all the functions so they're optional
var surfaceTree: Ghostty.SplitNode? { get set }
}
extension TerminalViewDelegate { extension TerminalViewDelegate {
func focusedSurfaceDidChange(to: Ghostty.SurfaceView?) {} func focusedSurfaceDidChange(to: Ghostty.SurfaceView?) {}
func titleDidChange(to: String) {} func titleDidChange(to: String) {}
func cellSizeDidChange(to: NSSize) {} func cellSizeDidChange(to: NSSize) {}
} }
/// The view model is a required implementation for TerminalView callers. This contains
/// the main state between the TerminalView caller and SwiftUI. This abstraction is what
/// allows AppKit to own most of the data in SwiftUI.
protocol TerminalViewModel: ObservableObject {
/// The tree of terminal surfaces (splits) within the view. This is mutated by TerminalView
/// and children. This should be @Published.
var surfaceTree: Ghostty.SplitNode? { get set }
}
/// The main terminal view. This terminal view supports splits.
struct TerminalView<ViewModel: TerminalViewModel>: View { struct TerminalView<ViewModel: TerminalViewModel>: View {
@ObservedObject var ghostty: Ghostty.AppState @ObservedObject var ghostty: Ghostty.AppState