ClashX.Meta/ClashX/Views/StatusItemView.swift

107 lines
3.3 KiB
Swift
Raw Normal View History

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 AppKit
2019-10-20 13:40:50 +08:00
import Foundation
import RxCocoa
import RxSwift
2018-06-23 20:17:05 +08:00
class StatusItemView: NSView {
@IBOutlet var imageView: NSImageView!
2019-10-20 13:40:50 +08:00
2018-06-23 21:43:33 +08:00
@IBOutlet var uploadSpeedLabel: NSTextField!
2018-06-23 20:17:05 +08:00
@IBOutlet var downloadSpeedLabel: NSTextField!
2019-10-20 13:40:50 +08:00
@IBOutlet var speedContainerView: NSView!
2018-11-30 22:14:20 +08:00
var updating = false
2019-10-20 13:40:50 +08:00
weak var statusItem: NSStatusItem?
2019-10-15 22:40:02 +08:00
lazy var menuImage: NSImage = {
let customImagePath = (NSHomeDirectory() as NSString).appendingPathComponent("/.config/clash/menuImage.png")
return NSImage(contentsOfFile: customImagePath) ?? NSImage(named: "menu_icon")!
}()
2019-10-20 13:40:50 +08:00
static func create(statusItem: NSStatusItem?) -> StatusItemView {
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
view.setupView()
2018-06-23 20:17:05 +08:00
return view
}
return NSView() as! StatusItemView
}
2019-10-20 13:40:50 +08:00
func setupView() {
if #available(OSX 10.11, *) {
let font = NSFont.systemFont(ofSize: 9, weight: .regular)
uploadSpeedLabel.font = font
downloadSpeedLabel.font = font
}
}
2019-10-20 13:40:50 +08:00
2019-10-15 22:40:02 +08:00
func updateViewStatus(enableProxy: Bool) {
2019-10-20 13:40:50 +08:00
let selectedColor = NSColor.red
let unselectedColor: NSColor
if #available(OSX 10.14, *) {
unselectedColor = selectedColor.withSystemEffect(.disabled)
} else {
unselectedColor = selectedColor.withAlphaComponent(0.5)
}
imageView.image = menuImage.tint(color: enableProxy ? selectedColor : unselectedColor)
uploadSpeedLabel.textColor = NSColor.black
downloadSpeedLabel.textColor = uploadSpeedLabel.textColor
updateStatusItemView()
2019-10-15 22:40:02 +08:00
}
2019-10-20 13:40:50 +08:00
func updateSpeedLabel(up: Int, down: Int) {
guard !speedContainerView.isHidden else { return }
let kbup = up / 1024
let kbdown = down / 1024
var finalUpStr: String
var finalDownStr: String
2018-06-23 21:43:33 +08:00
if kbup < 1024 {
finalUpStr = "\(kbup)KB/s"
} else {
2019-10-20 13:40:50 +08:00
finalUpStr = String(format: "%.2fMB/s", Double(kbup) / 1024.0)
2018-06-23 21:43:33 +08:00
}
2019-10-20 13:40:50 +08:00
2018-06-23 21:43:33 +08:00
if kbdown < 1024 {
finalDownStr = "\(kbdown)KB/s"
} else {
2019-10-20 13:40:50 +08:00
finalDownStr = String(format: "%.2fMB/s", Double(kbdown) / 1024.0)
2018-06-23 21:43:33 +08:00
}
DispatchQueue.main.async {
self.downloadSpeedLabel.stringValue = finalDownStr
self.uploadSpeedLabel.stringValue = finalUpStr
2019-10-20 13:40:50 +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
}
}
2019-10-20 13:40:50 +08:00
func showSpeedContainer(show: Bool) {
speedContainerView.isHidden = !show
updateStatusItemView()
}
2019-10-20 13:40:50 +08:00
func updateStatusItemView() {
statusItem?.updateImage(withView: self)
2018-06-23 20:17:05 +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
}
}