2018-06-23 20:17:05 +08:00
|
|
|
//
|
|
|
|
// StatusItemView.swift
|
|
|
|
// ClashX
|
|
|
|
//
|
2018-08-08 13:47:38 +08:00
|
|
|
// Created by CYC on 2018/6/23.
|
|
|
|
// Copyright © 2018年 yichengchen. All rights reserved.
|
2018-06-23 20:17:05 +08:00
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import AppKit
|
2018-08-07 13:52:18 +08:00
|
|
|
import RxCocoa
|
|
|
|
import RxSwift
|
|
|
|
|
2018-06-23 20:17:05 +08:00
|
|
|
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!
|
2018-08-05 01:17:27 +08:00
|
|
|
@IBOutlet weak var speedContainerView: NSView!
|
2018-11-30 22:14:20 +08:00
|
|
|
var updating = false
|
|
|
|
|
2018-06-23 20:17:05 +08:00
|
|
|
weak var statusItem:NSStatusItem?
|
2018-08-07 13:52:18 +08:00
|
|
|
var disposeBag = DisposeBag()
|
2018-06-23 20:17:05 +08:00
|
|
|
|
2018-08-05 19:45:37 +08:00
|
|
|
|
2018-11-04 00:36:24 +08:00
|
|
|
static func create(statusItem:NSStatusItem?)->StatusItemView{
|
2018-06-23 20:17:05 +08:00
|
|
|
var topLevelObjects : NSArray?
|
2018-09-27 23:07:05 +08:00
|
|
|
if Bundle.main.loadNibNamed("StatusItemView", owner: self, topLevelObjects: &topLevelObjects) {
|
2018-06-23 20:17:05 +08:00
|
|
|
let view = (topLevelObjects!.first(where: { $0 is NSView }) as? StatusItemView)!
|
|
|
|
view.statusItem = statusItem
|
2018-11-03 15:53:44 +08:00
|
|
|
view.setupView()
|
2018-06-23 20:17:05 +08:00
|
|
|
return view
|
|
|
|
}
|
|
|
|
return NSView() as! StatusItemView
|
|
|
|
}
|
|
|
|
|
2018-08-07 13:52:18 +08:00
|
|
|
func setupView() {
|
2018-10-02 14:05:56 +08:00
|
|
|
let proxySetObservable = ConfigManager.shared.proxyPortAutoSetObservable.map { $0 as AnyObject }
|
2018-11-04 00:36:24 +08:00
|
|
|
proxySetObservable
|
2018-10-02 14:05:56 +08:00
|
|
|
.bind { [weak self] _ in
|
|
|
|
guard let self = self else {return}
|
|
|
|
let enableProxy = ConfigManager.shared.proxyPortAutoSet;
|
|
|
|
|
2018-08-25 23:52:46 +08:00
|
|
|
let customImagePath = (NSHomeDirectory() as NSString).appendingPathComponent("/.config/clash/menuImage.png")
|
2018-10-02 14:05:56 +08:00
|
|
|
|
2018-11-03 15:53:44 +08:00
|
|
|
let selectedColor = NSColor.red
|
|
|
|
let unselectedColor: NSColor
|
|
|
|
if #available(OSX 10.14, *) {
|
|
|
|
unselectedColor = selectedColor.withSystemEffect(.disabled)
|
|
|
|
} else {
|
|
|
|
unselectedColor = selectedColor.withAlphaComponent(0.5)
|
|
|
|
}
|
|
|
|
|
2018-08-25 23:52:46 +08:00
|
|
|
let image = NSImage(contentsOfFile: customImagePath) ??
|
2018-10-02 14:05:56 +08:00
|
|
|
NSImage(named: "menu_icon")!.tint(color: enableProxy ? selectedColor : unselectedColor)
|
|
|
|
|
2018-08-25 23:52:46 +08:00
|
|
|
self.imageView.image = image
|
2018-10-02 12:30:22 +08:00
|
|
|
|
2018-11-03 15:53:44 +08:00
|
|
|
self.uploadSpeedLabel.textColor = NSColor.black
|
2018-10-02 12:30:22 +08:00
|
|
|
self.downloadSpeedLabel.textColor = self.uploadSpeedLabel.textColor
|
|
|
|
|
2018-11-03 15:53:44 +08:00
|
|
|
self.updateStatusItemView()
|
2018-08-07 13:52:18 +08:00
|
|
|
}.disposed(by: disposeBag)
|
2019-05-10 20:41:31 +08:00
|
|
|
|
|
|
|
if #available(OSX 10.11, *) {
|
2019-05-11 11:49:57 +08:00
|
|
|
let font = NSFont.systemFont(ofSize: 9, weight: .regular)
|
2019-05-10 20:41:31 +08:00
|
|
|
uploadSpeedLabel.font = font
|
|
|
|
downloadSpeedLabel.font = font
|
|
|
|
}
|
|
|
|
|
2018-08-07 13:52:18 +08:00
|
|
|
}
|
|
|
|
|
2018-06-23 21:43:33 +08:00
|
|
|
func updateSpeedLabel(up:Int,down:Int) {
|
2018-11-04 00:36:24 +08:00
|
|
|
guard !self.speedContainerView.isHidden else {return}
|
|
|
|
|
2018-06-23 21:43:33 +08:00
|
|
|
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
|
2019-09-29 21:28:43 +08:00
|
|
|
if self.updating {Logger.log("update during update"); return}
|
2018-11-30 22:14:20 +08:00
|
|
|
self.updating = true
|
|
|
|
self.updateStatusItemView()
|
|
|
|
self.updating = false
|
2018-06-23 21:43:33 +08:00
|
|
|
}
|
2018-11-03 15:53:44 +08:00
|
|
|
|
2018-06-23 21:43:33 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-08-05 01:17:27 +08:00
|
|
|
func showSpeedContainer(show:Bool) {
|
|
|
|
self.speedContainerView.isHidden = !show
|
2018-11-03 15:53:44 +08:00
|
|
|
updateStatusItemView()
|
2018-08-05 01:17:27 +08:00
|
|
|
}
|
|
|
|
|
2018-11-03 15:53:44 +08:00
|
|
|
func updateStatusItemView() {
|
2018-11-04 00:36:24 +08:00
|
|
|
statusItem?.updateImage(withView: self)
|
2018-06-23 20:17:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-03 15:53:44 +08:00
|
|
|
|
|
|
|
extension NSStatusItem {
|
|
|
|
func updateImage(withView: NSView) {
|
|
|
|
image = NSImage(data: withView.dataWithPDF(inside: withView.bounds))
|
|
|
|
image?.isTemplate = true
|
2018-06-23 20:17:05 +08:00
|
|
|
}
|
|
|
|
}
|