-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPopUpButton.swift
executable file
·115 lines (87 loc) · 3.38 KB
/
PopUpButton.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/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)
let label = NSTextField(frame: NSRect(x: 0, y: 300, width: 600, height: 60))
let popUpButton = NSPopUpButton(frame: NSRect(x: 240, y: 150, width: 120, height: 60))
// 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")
///////////////
// Popup button
setupPopUpButton()
contentView.addSubview(popUpButton)
popUpButton.action = #selector(popUpButtonClicked)
// Label
setupLabel()
contentView.addSubview(label)
}
// Button actions
@objc func popUpButtonClicked(sender: AnyObject) {
let optionSelected = popUpButton.titleOfSelectedItem!
if popUpButton.indexOfSelectedItem == 0 {
label.stringValue = ""
return
}
print("\(optionSelected) selected")
label.stringValue = optionSelected
}
// 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.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
}
func setupLabel() {
label.isBezeled = false
label.isEditable = false
label.font = NSFont.systemFont(ofSize: 40)
label.alignment = .center
}
func setupPopUpButton() {
let options = ["--- Select ---", "Earth", "Wind", "Fire"]
let _ = options.map({ popUpButton.addItem(withTitle: $0) })
}
// 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()