lots of progress running a surface but still crashes

This commit is contained in:
Mitchell Hashimoto
2023-02-17 15:49:19 -08:00
parent 55b05b22bb
commit ff9af8a07b
12 changed files with 156 additions and 61 deletions

View File

@@ -0,0 +1,3 @@
enum AppError: Error {
case surfaceCreateError
}

View File

@@ -1,19 +0,0 @@
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundColor(.accentColor)
Text("Hello, world!")
}
.padding()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

View File

@@ -20,7 +20,7 @@ struct GhosttyApp: App {
case .error:
ErrorView()
case .ready:
TerminalSurfaceView()
TerminalSurfaceView(app: ghostty.app!)
}
}
}
@@ -37,6 +37,10 @@ class GhosttyState: ObservableObject {
/// The ghostty global configuration.
var config: ghostty_config_t? = nil
/// The ghostty app instance. We only have one of these for the entire app, although I guess
/// in theory you can have multiple... I don't know why you would...
var app: ghostty_app_t? = nil
init() {
// Initialize ghostty global state. This happens once per process.
guard ghostty_init() == GHOSTTY_SUCCESS else {
@@ -51,6 +55,7 @@ class GhosttyState: ObservableObject {
readiness = .error
return
}
self.config = cfg;
// TODO: we'd probably do some config loading here... for now we'd
// have to do this synchronously. When we support config updating we can do
@@ -59,11 +64,29 @@ class GhosttyState: ObservableObject {
// Finalize will make our defaults available.
ghostty_config_finalize(cfg)
config = cfg
readiness = .ready
// Create our "runtime" config. The "runtime" is the configuration that ghostty
// uses to interface with the application runtime environment.
var runtime_cfg = ghostty_runtime_config_s(
userdata: nil,
wakeup_cb: { userdata in GhosttyState.wakeup() })
// Create the ghostty app.
guard let app = ghostty_app_new(&runtime_cfg, cfg) else {
GhosttyApp.logger.critical("ghostty_app_new failed")
readiness = .error
return
}
self.app = app
self.readiness = .ready
}
static func wakeup() {
// TODO
}
deinit {
ghostty_app_free(app)
ghostty_config_free(config)
}
}

View File

@@ -1,4 +1,6 @@
import OSLog
import SwiftUI
import GhosttyKit
/// A surface is terminology in Ghostty for a terminal surface, or a place where a terminal is actually drawn
/// and interacted with. The word "surface" is used because a surface may represent a window, a tab,
@@ -8,7 +10,11 @@ import SwiftUI
/// since that is what the Metal renderer in Ghostty expects. In the future, it may make more sense to
/// wrap an MTKView and use that, but for legacy reasons we didn't do that to begin with.
struct TerminalSurfaceView: NSViewRepresentable {
@StateObject private var state = TerminalSurfaceState()
@StateObject private var state: TerminalSurfaceState
init(app: ghostty_app_t) {
self._state = StateObject(wrappedValue: TerminalSurfaceState(app))
}
func makeNSView(context: Context) -> TerminalSurfaceView_Real {
// We need the view as part of the state to be created previously because
@@ -24,10 +30,33 @@ struct TerminalSurfaceView: NSViewRepresentable {
/// The state for the terminal surface view.
class TerminalSurfaceState: ObservableObject {
var view: TerminalSurfaceView_Real;
static let logger = Logger(
subsystem: Bundle.main.bundleIdentifier!,
category: String(describing: TerminalSurfaceState.self)
)
init() {
var view: TerminalSurfaceView_Real
private var surface: ghostty_surface_t? = nil
private var error: Error? = nil
init(_ app: ghostty_app_t) {
view = TerminalSurfaceView_Real()
var surface_cfg = ghostty_surface_config_s(
nsview: Unmanaged.passUnretained(view).toOpaque(),
scale_factor: 2.0)
guard let surface = ghostty_surface_new(app, &surface_cfg) else {
self.error = AppError.surfaceCreateError
return
}
self.surface = surface;
}
deinit {
if let surface = self.surface {
ghostty_surface_free(surface)
}
}
}
@@ -52,8 +81,10 @@ class TerminalSurfaceView_Real: NSView {
}
}
struct TerminalSurfaceView_Previews: PreviewProvider {
static var previews: some View {
TerminalSurfaceView()
}
}
/*
struct TerminalSurfaceView_Previews: PreviewProvider {
static var previews: some View {
TerminalSurfaceView()
}
}
*/