-
-
Save tib/183b2f5e4ac1d5afba5d69155ec11025 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() |
But Thanks---This is really cool and I am having fun with it!!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This wouldn't let me focus for text input. After some fooling around I changed the App stuff and it worked (for me)
let del = AppDelegate()
let application = NSApplication.shared
application.setActivationPolicy(NSApplication.ActivationPolicy.regular)
application.delegate = del
application.activate(ignoringOtherApps: true)
application.run()