-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🎨 refactor StatusItem into separate class
- Loading branch information
1 parent
6c4d75d
commit 21b383a
Showing
3 changed files
with
56 additions
and
25 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import Cocoa | ||
|
||
fileprivate func createMenu(_ itemCreators: [(_ menuItem: NSMenuItem) -> Void]) -> NSMenu { | ||
let menu = NSMenu() | ||
itemCreators.forEach { createItem in | ||
let item = NSMenuItem() | ||
createItem(item) | ||
menu.addItem(item) | ||
} | ||
return menu | ||
} | ||
|
||
class StatusItem { | ||
enum StateIcon { | ||
case idle | ||
case busy | ||
case error | ||
|
||
var image: NSImage? { | ||
switch self { | ||
case .idle: return NSImage(systemSymbolName: "mic", accessibilityDescription: nil) | ||
case .busy: return NSImage(systemSymbolName: "mic.fill", accessibilityDescription: nil) | ||
case .error: return NSImage(systemSymbolName: "mic.slash", accessibilityDescription: nil) | ||
} | ||
} | ||
} | ||
|
||
private let instance = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength) | ||
|
||
init() { | ||
instance.menu = createMenu([{ | ||
let appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleName") ?? "" | ||
$0.title = "About \(appName)" | ||
$0.action = #selector(AboutWindow.show) | ||
$0.target = AboutWindow.shared | ||
}, { | ||
$0.title = "Quit" | ||
$0.keyEquivalent = "q" | ||
$0.action = #selector(NSApp.terminate) | ||
$0.target = NSApp | ||
}]) | ||
} | ||
|
||
func setIcon(_ icon: StateIcon) { | ||
instance.button?.image = icon.image | ||
} | ||
} |