使用swift如何開發一個MacOS的狀態欄App,上一篇已經講了。裡面我忘記提如何修改狀態icon為餅型進度。比如App在處理什麼事情的時候,可以添加進度狀態提示用戶。如下圖所示:

/// 顯示狀態欄菜單餅型進度
private func showStatusItemProgress() {
if let button = statusItem.button {
// FIXME: it works, but obviously not good.
button.subviews.removeAll()
}
if let button = statusItem.button {
// FIXME: it works, but obviously not good.
let frame = NSRect(x: 6, y: 2, width: 18, height: 18)
let progressIndicator = NSProgressIndicator(frame: frame)
progressIndicator.style = .SpinningStyle
progressIndicator.indeterminate = false
progressIndicator.minValue = 0
progressIndicator.maxValue = 100
progressIndicator.doubleValue = 0
self.progressIndicator = progressIndicator
// 當添加進度後,發現狀態欄frame大小錯誤了,沒找到解決辦法,但是填充一個圖片可以解決這個尺寸錯誤問題
statusItem.image = NSImage(named: "EmptyIcon")
statusItem.image?.template = true
button.addSubview(progressIndicator)
}
}
請注意,
- 我們是把一個NSProgressIndicator添加到了NSStatusItem的button視圖上,不要添加到view視圖上,如果添加到view上,view會擋住狀態欄點擊事件,這樣NSStatusItem會不能響應用戶點擊了。(當然,如果你app恰好不需要用戶點擊,可以這樣做)
- 你應該留意到了,我在NSStatusItem的image裡放了一張透明圖片,因為這了是為了解決它frame大小錯誤的問題
當然,如果你知道如何解決的話,可以告訴我下。哈哈。