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-06-23 20:17:05 +08:00
|
|
|
weak var statusItem:NSStatusItem?
|
2018-08-07 13:52:18 +08:00
|
|
|
var disposeBag = DisposeBag()
|
2018-08-07 23:01:03 +08:00
|
|
|
var isDarkMode = false
|
2018-06-23 20:17:05 +08:00
|
|
|
|
2018-08-05 19:45:37 +08:00
|
|
|
var onPopUpMenuAction:(()->())? = nil
|
|
|
|
|
2018-08-05 01:17:27 +08:00
|
|
|
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
|
2018-08-07 13:52:18 +08:00
|
|
|
view.setupView()
|
2018-06-23 20:17:05 +08:00
|
|
|
statusMenu.delegate = view
|
|
|
|
return view
|
|
|
|
}
|
|
|
|
return NSView() as! StatusItemView
|
|
|
|
}
|
|
|
|
|
2018-08-07 13:52:18 +08:00
|
|
|
func setupView() {
|
|
|
|
UserDefaults.standard
|
|
|
|
.rx.observe(String.self, "AppleInterfaceStyle").bind {
|
|
|
|
value in
|
2018-08-25 23:52:46 +08:00
|
|
|
let darkMode = (value ?? "Light") == "Dark"
|
|
|
|
let customImagePath = (NSHomeDirectory() as NSString).appendingPathComponent("/.config/clash/menuImage.png")
|
|
|
|
let image = NSImage(contentsOfFile: customImagePath) ??
|
|
|
|
NSImage(named: NSImage.Name(rawValue: "menu_icon"))!.tint(color: darkMode ? NSColor.white : NSColor.black)
|
|
|
|
self.imageView.image = image
|
|
|
|
self.isDarkMode = darkMode
|
2018-08-07 13:52:18 +08:00
|
|
|
}.disposed(by: disposeBag)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-08-05 01:17:27 +08:00
|
|
|
func showSpeedContainer(show:Bool) {
|
|
|
|
self.speedContainerView.isHidden = !show
|
|
|
|
}
|
|
|
|
|
2018-06-23 20:17:05 +08:00
|
|
|
override func mouseDown(with event: NSEvent) {
|
2018-08-05 19:45:37 +08:00
|
|
|
onPopUpMenuAction?()
|
2018-06-23 20:17:05 +08:00
|
|
|
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
|
2018-08-07 23:01:03 +08:00
|
|
|
|
|
|
|
if !self.isDarkMode {
|
|
|
|
self.uploadSpeedLabel.textColor = highlight ? NSColor.white : NSColor.black
|
|
|
|
self.downloadSpeedLabel.textColor = highlight ? NSColor.white : NSColor.black
|
|
|
|
}
|
2018-06-23 20:17:05 +08:00
|
|
|
}
|
|
|
|
}
|