1
1
mirror of https://github.com/bitgapp/eqMac.git synced 2024-12-12 16:24:12 +03:00
eqMac/native/app/Source/UI/StatusItem.swift
2020-04-04 19:15:38 +01:00

59 lines
1.3 KiB
Swift

//
// PopStatusItem.swift
// PopStatusItem
//
// Created by Adam Hartford on 4/24/15.
// Copyright (c) 2015 Adam Hartford. All rights reserved.
//
import Cocoa
import EmitterKit
class StatusItem {
// MARK: - Properties
var highlighted: Bool {
get {
return button.isHighlighted
}
set {
DispatchQueue.main.async {
self.button.isHighlighted = newValue
}
}
}
var clicked = Event<Void>()
var button: NSStatusBarButton
private let dummyMenu = NSMenu()
let item = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
// MARK - Initialization
public init(image: NSImage) {
self.button = item.button!
image.isTemplate = true
button.image = image
button.appearsDisabled = false
button.target = self
button.action = #selector(StatusItem.wasClicked)
NotificationCenter.default.addObserver(self, selector: #selector(NSApplicationDelegate.applicationWillResignActive(_:)), name: NSApplication.willResignActiveNotification, object: nil)
}
@IBAction private func wasClicked (event: Any?) {
item.popUpMenu(dummyMenu)
clicked.emit()
}
// MARK: - Listeners
@objc open func applicationWillResignActive(_ notification: Notification) {
highlighted = false
}
// MARK: - Deinit
deinit {
NotificationCenter.default.removeObserver(self)
}
}