-
-
Save heckj/23fd64ea995e63d7c6d5e619f60781d0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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