Skip to content

Commit

Permalink
refactor: [StatusItemManager] shared singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
Caldis committed Sep 12, 2022
1 parent 32b44ad commit c90b495
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
21 changes: 13 additions & 8 deletions Mos/Managers/StatusItemManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ class StatusItemManager: NSMenu, NSMenuDelegate {
// 状态栏类型
let TYPE = STATUS_ITEM_TYPE.menu


// 单例
static let shared = StatusItemManager()

// 状态栏引用
static let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
let item = StatusItemManager.statusItem
let item = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)

// 初始化
override func awakeFromNib() {
NSLog("Module initialized: StatusItemManager")
// 设置图标
item.image = #imageLiteral(resourceName: "AppStatusBarIcon")
// 设置事件响应
Expand Down Expand Up @@ -135,19 +140,19 @@ extension StatusItemManager {
**/
extension StatusItemManager {
// 显示状态栏图标
class func showStatusItem() {
func showStatusItem() {
if #available(OSX 10.12, *) {
self.statusItem.isVisible = true
item.isVisible = true
} else {
self.statusItem.length = NSStatusItem.variableLength
item.length = NSStatusItem.variableLength
}
}
// 隐藏状态栏图标
class func hideStatusItem() {
func hideStatusItem() {
if #available(OSX 10.12, *) {
self.statusItem.isVisible = false
item.isVisible = false
} else {
self.statusItem.length = 0.0
item.length = 0.0
}
}
}
2 changes: 1 addition & 1 deletion Mos/Utils/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class OPTIONS_GENERAL_DEFAULT {
}
// 隐藏
var hideStatusItem = false {
willSet {newValue ? StatusItemManager.hideStatusItem() : StatusItemManager.showStatusItem()}
willSet {newValue ? StatusItemManager.shared.hideStatusItem() : StatusItemManager.shared.showStatusItem()}
didSet {Options.shared.saveOptions()}
}
// 例外
Expand Down

0 comments on commit c90b495

Please sign in to comment.