70 lines
2.0 KiB
Swift
70 lines
2.0 KiB
Swift
//
|
|
// AppDelegate.swift
|
|
// Gemini
|
|
//
|
|
// Created by Shadowfacts on 7/12/20.
|
|
//
|
|
|
|
import Cocoa
|
|
import SwiftUI
|
|
import GeminiProtocol
|
|
|
|
@NSApplicationMain
|
|
class AppDelegate: NSObject, NSApplicationDelegate {
|
|
|
|
var window: NSWindow!
|
|
|
|
var connection: GeminiConnection!
|
|
let url = URL(string: "gemini://localhost:1965/overview.gmi")!
|
|
|
|
func applicationDidFinishLaunching(_ aNotification: Notification) {
|
|
// Create the SwiftUI view that provides the window contents.
|
|
let contentView = ContentView()
|
|
|
|
// Create the window and set the content view.
|
|
window = NSWindow(
|
|
contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
|
|
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
|
|
backing: .buffered, defer: false)
|
|
window.isReleasedWhenClosed = false
|
|
window.center()
|
|
window.setFrameAutosaveName("Main Window")
|
|
window.contentView = NSHostingView(rootView: contentView)
|
|
window.title = "Gemini"
|
|
window.makeKeyAndOrderFront(nil)
|
|
|
|
connection = GeminiConnection(endpoint: .url(url), delegate: self)
|
|
}
|
|
|
|
func applicationWillTerminate(_ aNotification: Notification) {
|
|
// Insert code here to tear down your application
|
|
}
|
|
|
|
var alreadyReceived = false
|
|
|
|
}
|
|
|
|
extension AppDelegate: GeminiConnectionDelegate {
|
|
func connectionReady(_ connection: GeminiConnection) {
|
|
print("!! Ready")
|
|
let req = try! GeminiRequest(url: url)
|
|
connection.sendRequest(req)
|
|
}
|
|
|
|
func connection(_ connection: GeminiConnection, receivedData data: Data?, header: GeminiResponseHeader) {
|
|
if !alreadyReceived {
|
|
alreadyReceived = true
|
|
print("!! Status: \(header.status)")
|
|
print("!! Meta: '\(header.meta)'")
|
|
}
|
|
if let data = data {
|
|
print(String(data: data, encoding: .utf8)!)
|
|
}
|
|
}
|
|
|
|
func connectionCompleted(_ connection: GeminiConnection) {
|
|
print("!! completed")
|
|
}
|
|
}
|
|
|