-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathAppDelegate.swift
More file actions
89 lines (82 loc) · 2.28 KB
/
Copy pathAppDelegate.swift
File metadata and controls
89 lines (82 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import AppKit
import Dependencies
import SQLiteData
@MainActor
public final class AppDelegate: NSObject, NSApplicationDelegate {
private var windowControllers: [DemoWindowController] = []
public func applicationWillFinishLaunching(_ notification: Notification) {
let appMenu = NSMenuItem()
appMenu.submenu = NSMenu()
appMenu.submenu?.items = [
NSMenuItem(
title: "New Window",
action: #selector(AppDelegate.newDemoWindow),
keyEquivalent: "n"
),
NSMenuItem(
title: "Close Window",
action: #selector(NSWindow.performClose(_:)),
keyEquivalent: "w"
),
NSMenuItem(
title: "Quit",
action: #selector(NSApplication.terminate(_:)),
keyEquivalent: "q"
),
]
let mainMenu = NSMenu()
mainMenu.items = [appMenu]
NSApplication.shared.mainMenu = mainMenu
}
public func applicationDidFinishLaunching(_ notification: Notification) {
try! prepareDependencies {
try $0.bootstrapDatabase()
}
@Dependency(\.defaultDatabase) var database
try! database.write { db in
try db.seedSampleData()
}
newDemoWindow()
}
}
extension AppDelegate {
@objc func newDemoWindow() {
let windowController = DemoWindowController()
windowController.showWindow(nil)
windowControllers.append(windowController)
}
func removeWindowController(_ controller: DemoWindowController) {
windowControllers.removeAll { $0 === controller }
}
}
final class DemoWindowController: NSWindowController {
init() {
let window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 800, height: 600),
styleMask: [
.fullSizeContentView,
.closable,
.miniaturizable,
.resizable,
.titled,
],
backing: .buffered,
defer: false
)
window.titleVisibility = .visible
window.toolbarStyle = .unified
window.center()
super.init(window: window)
window.contentViewController = RootViewController(
model: AppModel()
)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public func windowWillClose(_ notification: Notification) {
if let appDelegate = NSApp.delegate as? AppDelegate {
appDelegate.removeWindowController(self)
}
}
}