ClashX.Meta/ClashX/ViewControllers/StatusItemView.swift

83 lines
2.3 KiB
Swift
Raw Normal View History

2018-06-23 20:17:05 +08:00
//
// StatusItemView.swift
// ClashX
//
// Created by on 2018/6/23.
// Copyright © 2018 west2online. All rights reserved.
//
import Foundation
import AppKit
class StatusItemView: NSView {
@IBOutlet var imageView: NSImageView!
2018-06-23 21:43:33 +08:00
@IBOutlet var uploadSpeedLabel: NSTextField!
2018-06-23 20:17:05 +08:00
@IBOutlet var downloadSpeedLabel: NSTextField!
@IBOutlet weak var speedContainerView: NSView!
2018-06-23 20:17:05 +08:00
weak var statusItem:NSStatusItem?
static func create(statusItem:NSStatusItem?,statusMenu:NSMenu)->StatusItemView{
2018-06-23 20:17:05 +08:00
var topLevelObjects : NSArray?
if Bundle.main.loadNibNamed(NSNib.Name(rawValue: "StatusItemView"), owner: self, topLevelObjects: &topLevelObjects) {
let view = (topLevelObjects!.first(where: { $0 is NSView }) as? StatusItemView)!
view.statusItem = statusItem
view.menu = statusMenu
statusMenu.delegate = view
return view
}
return NSView() as! StatusItemView
}
2018-06-23 21:43:33 +08:00
func updateSpeedLabel(up:Int,down:Int) {
let kbup = up/1024
let kbdown = down/1024
var finalUpStr:String
var finalDownStr:String
if kbup < 1024 {
finalUpStr = "\(kbup)KB/s"
} else {
finalUpStr = String(format: "%.2fMB/s", (Double(kbup)/1024.0))
}
if kbdown < 1024 {
finalDownStr = "\(kbdown)KB/s"
} else {
finalDownStr = String(format: "%.2fMB/s", (Double(kbdown)/1024.0))
}
DispatchQueue.main.async {
self.downloadSpeedLabel.stringValue = finalDownStr
self.uploadSpeedLabel.stringValue = finalUpStr
}
}
func showSpeedContainer(show:Bool) {
self.speedContainerView.isHidden = !show
}
2018-06-23 20:17:05 +08:00
override func mouseDown(with event: NSEvent) {
statusItem?.popUpMenu(self.menu!)
}
}
extension StatusItemView:NSMenuDelegate {
func menuWillOpen(_ menu: NSMenu) {
drawHighlight(highlight: true)
}
func menuDidClose(_ menu: NSMenu) {
drawHighlight(highlight: false)
}
func drawHighlight(highlight:Bool) {
let image = NSImage(size: self.frame.size)
image.lockFocus()
statusItem?.drawStatusBarBackground(in: self.bounds, withHighlight: highlight)
image.unlockFocus()
self.layer?.contents = image
}
}