-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmptyWindow.swift
executable file
·77 lines (58 loc) · 2.25 KB
/
EmptyWindow.swift
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
#!/usr/bin/swift
import Cocoa
class AppDelegate: NSObject, NSApplicationDelegate {
let appName = "Demo App"
// UI elements
let window = NSWindow(contentRect: NSMakeRect(0, 0, 600, 400),
styleMask: .titled,
backing: .buffered,
defer: true)
// Methods
func setupUI() {
// Menu
setupMenu(appName: appName)
// Window
setupWindow()
// "Normal" window presence (activation & exit)
window.orderFrontRegardless()
NSApp.activate(ignoringOtherApps: true)
// Setup window's content view
// let contentView = window.contentView!
// Dock icon
setupDockIcon(path: "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/GenericApplicationIcon.icns")
}
// Helper methods
func setupMenu(appName: String) {
let mainMenu = NSMenu()
let subMenu = NSMenu()
let menuItem = mainMenu.addItem(withTitle: "Application", action: nil, keyEquivalent: "")
let titleQuit = NSLocalizedString("Quit", comment: "Quit menu item") + " " + appName
let menuItemQuit = subMenu.addItem(withTitle: titleQuit, action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q")
menuItemQuit.target = NSApp
mainMenu.setSubmenu(subMenu, for: menuItem)
NSApp.mainMenu = mainMenu
}
func setupWindow() {
window.center()
window.styleMask.insert(.closable)
window.styleMask.insert(.miniaturizable)
window.styleMask.insert(.resizable)
window.backgroundColor = NSColor.controlBackgroundColor
window.title = "Window"
}
func setupDockIcon(path: String) {
let icon = NSImage(byReferencingFile: path)!
icon.size = CGSize(width: 128, height: 128)
NSApp.applicationIconImage = icon
}
// Required app delegate method
func applicationDidFinishLaunching(_ aNotification: Notification) {
setupUI()
}
}
// Setup app delegate and run the app
let thisApp = NSApplication.shared
NSApp.setActivationPolicy(.regular)
let appDelegate = AppDelegate()
thisApp.delegate = appDelegate
thisApp.run()