ClashX.Meta/ClashX/ViewControllers/StatusItemView.swift

110 lines
3.4 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 Foundation
import AppKit
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!
@IBOutlet weak var speedContainerView: NSView!
2018-06-23 20:17:05 +08:00
weak var statusItem:NSStatusItem?
var disposeBag = DisposeBag()
2018-08-07 23:01:03 +08:00
var isDarkMode = false
2018-06-23 20:17:05 +08:00
var onPopUpMenuAction:(()->())? = nil
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
view.setupView()
2018-06-23 20:17:05 +08:00
statusMenu.delegate = view
return view
}
return NSView() as! StatusItemView
}
func setupView() {
UserDefaults.standard
.rx.observe(String.self, "AppleInterfaceStyle").bind {
value in
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
}.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
}
}
func showSpeedContainer(show:Bool) {
self.speedContainerView.isHidden = !show
}
2018-06-23 20:17:05 +08:00
override func mouseDown(with event: NSEvent) {
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
}
}