Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion Sources/SwiftTerm/Mac/MacTerminalView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ open class TerminalView: NSView, NSTextInputClient, NSUserInterfaceValidations,
var cellDimension: CellDimension!
var caretView: CaretView!
public var terminal: Terminal!

/// The rendering backend used by this terminal view.
/// Set to a ``MetalTerminalRenderer`` instance to enable GPU-accelerated rendering.
/// Defaults to ``CoreGraphicsRenderer`` if not set before ``setup()`` completes.
public var renderer: TerminalRenderer? {
didSet {
renderer?.setup(view: self)
}
}
private var progressBarView: TerminalProgressBarView?
private var progressReportTimer: Timer?
private var lastProgressValue: UInt8?
Expand Down Expand Up @@ -175,6 +184,11 @@ open class TerminalView: NSView, NSTextInputClient, NSUserInterfaceValidations,
setupOptions()
setupProgressBar()
setupFocusNotification()
if renderer == nil {
let cg = CoreGraphicsRenderer()
cg.setup(view: self)
renderer = cg
}
}

func startDisplayUpdates ()
Expand Down Expand Up @@ -534,7 +548,17 @@ open class TerminalView: NSView, NSTextInputClient, NSUserInterfaceValidations,
guard let currentContext = getCurrentGraphicsContext() else {
return
}
drawTerminalContents (dirtyRect: dirtyRect, context: currentContext, bufferOffset: terminal.displayBuffer.yDisp)
let bufferOffset = terminal.displayBuffer.yDisp
if let renderer {
let dims = CellDimensions(
width: cellDimension.width,
height: cellDimension.height,
descent: CTFontGetDescent(fontSet.normal),
leading: CTFontGetLeading(fontSet.normal))
renderer.draw(in: currentContext, dirtyRect: dirtyRect, cellDimensions: dims, bufferOffset: bufferOffset)
} else {
drawTerminalContents(dirtyRect: dirtyRect, context: currentContext, bufferOffset: bufferOffset)
}
}

public override func cursorUpdate(with event: NSEvent)
Expand Down
44 changes: 44 additions & 0 deletions Sources/SwiftTerm/Rendering/CoreGraphicsRenderer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// CoreGraphicsRenderer.swift
//
// Default renderer that delegates to the existing CoreGraphics-based
// drawing code in AppleTerminalView.
//
#if os(macOS) || os(iOS) || os(visionOS)
import Foundation
import CoreGraphics

/// A thin wrapper around the existing CoreGraphics rendering in
/// ``TerminalView``. It delegates ``draw(in:dirtyRect:cellDimensions:bufferOffset:)``
/// back to the view's ``drawTerminalContents(dirtyRect:context:bufferOffset:)`` method.
public class CoreGraphicsRenderer: TerminalRenderer {
weak var view: TerminalView?

public init() {}

public func setup(view: TerminalView) {
self.view = view
}

public func draw(
in context: CGContext,
dirtyRect: CGRect,
cellDimensions: CellDimensions,
bufferOffset: Int
) {
view?.drawTerminalContents(dirtyRect: dirtyRect, context: context, bufferOffset: bufferOffset)
}

public func colorsChanged() {
view?.resetCaches()
}

public func fontChanged() {
view?.resetCaches()
}

public func invalidateAll() {
view?.resetCaches()
}
}
#endif
Loading