Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#! /usr/bin/swift
//
// - This is just some AppKit boilerplate to launch a window.
//
import AppKit
@available(OSX 10.15, *)
class AppDelegate: NSObject, NSApplicationDelegate {
let window = NSWindow()
let windowDelegate = WindowDelegate()
func applicationDidFinishLaunching(_ notification: Notification) {
let contentSize = NSSize(width:800, height:600)
window.setContentSize(contentSize)
window.styleMask = [.titled, .closable, .miniaturizable, .resizable]
window.level = .floating
window.delegate = windowDelegate
window.title = "TestView"
let graph = NSHostingView(rootView: DemoView())
graph.frame = NSRect(origin: NSPoint(x:0, y:0), size: contentSize)
graph.autoresizingMask = [.height, .width]
window.contentView!.addSubview(graph)
window.center()
window.makeKeyAndOrderFront(window)
}
class WindowDelegate: NSObject, NSWindowDelegate {
func windowWillClose(_ notification: Notification) {
NSApplication.shared.terminate(0)
}
}
}
//
// - This is the actual view.
//
import SwiftUI
struct DemoView: View {
@State private var value: Int = 0
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
func getNewNumber() {
value = .random(in: -500...500)
}
var body: some View {
VStack {
HStack {
Text("Demo view").font(.headline).colorScheme(.dark)
}.padding(10)
List {
Text("hello - \(value)")
}
}.onReceive(timer) { _ in self.getNewNumber() }
}
}
//
// - More AppKit boilerplate.
//
let app = NSApplication.shared
let del = AppDelegate()
app.delegate = del
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.