Feature: Allow user switch NetSpeedIndicator status

This commit is contained in:
yicheng 2018-08-05 01:17:27 +08:00
parent 37537d82db
commit 7d6745e2f0
5 changed files with 49 additions and 10 deletions

View File

@ -15,7 +15,6 @@ import RxSwift
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var statusItem: NSStatusItem!
static let StatusItemIconWidth: CGFloat = NSStatusItem.variableLength * 2
@IBOutlet weak var statusMenu: NSMenu!
@IBOutlet weak var proxySettingMenuItem: NSMenuItem!
@ -25,12 +24,13 @@ class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var proxyModeDirectMenuItem: NSMenuItem!
@IBOutlet weak var proxyModeRuleMenuItem: NSMenuItem!
@IBOutlet weak var showNetSpeedIndicatorMenuItem: NSMenuItem!
@IBOutlet weak var separatorLineTop: NSMenuItem!
@IBOutlet weak var sepatatorLineEndProxySelect: NSMenuItem!
var disposeBag = DisposeBag()
let ssQueue = DispatchQueue(label: "com.w2fzu.ssqueue", attributes: .concurrent)
var statusItemView:StatusItemView!
func applicationDidFinishLaunching(_ aNotification: Notification) {
signal(SIGPIPE, SIG_IGN)
@ -38,10 +38,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
_ = ProxyConfigManager.install()
PFMoveToApplicationsFolderIfNecessary()
self.startProxy()
statusItem = NSStatusBar.system.statusItem(withLength: 57)
let view = StatusItemView.create(statusItem: statusItem,statusMenu: statusMenu)
statusItem.view = view
statusItemView = StatusItemView.create(statusItem: nil,statusMenu: statusMenu)
setupData()
}
@ -61,7 +58,19 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
}.disposed(by: disposeBag)
ConfigManager.shared.proxyPortAutoSetObservable
ConfigManager.shared
.showNetSpeedIndicatorObservable
.bind {[unowned self] (show) in
self.showNetSpeedIndicatorMenuItem.state = (show!) ? .on : .off
self.statusItem = NSStatusBar.system.statusItem(withLength: show! ? 57 : 22)
self.statusItem.view = self.statusItemView
self.statusItemView.showSpeedContainer(show: show!)
self.statusItemView.statusItem = self.statusItem
}.disposed(by: disposeBag)
ConfigManager.shared
.proxyPortAutoSetObservable
.distinctUntilChanged()
.bind{ [unowned self]
enable in
@ -72,7 +81,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
((self.statusItem.view) as! StatusItemView).imageView.image = image
}.disposed(by: disposeBag)
ConfigManager.shared.currentConfigVariable
ConfigManager.shared
.currentConfigVariable
.asObservable()
.filter{$0 != nil}
.bind {[unowned self] (config) in
@ -89,11 +99,14 @@ class AppDelegate: NSObject, NSApplicationDelegate {
self.updateProxyList()
}.disposed(by: disposeBag)
LaunchAtLogin.shared.isEnableVirable
LaunchAtLogin.shared
.isEnableVirable
.asObservable()
.subscribe(onNext: { (enable) in
self.autoStartMenuItem.state = enable ? .on : .off
}).disposed(by: disposeBag)
}
func updateProxyList() {
@ -223,6 +236,9 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
@IBAction func actionShowNetSpeedIndicator(_ sender: NSMenuItem) {
ConfigManager.shared.showNetSpeedIndicator = !ConfigManager.shared.showNetSpeedIndicator
}
}

View File

@ -24,6 +24,7 @@
<outlet property="proxySettingMenuItem" destination="8se-yr-wmp" id="yvm-QP-yoA"/>
<outlet property="separatorLineTop" destination="1fM-p6-bHd" id="NFb-qS-TKR"/>
<outlet property="sepatatorLineEndProxySelect" destination="n67-fU-hLx" id="Vgv-N6-SOH"/>
<outlet property="showNetSpeedIndicatorMenuItem" destination="YIO-Vj-64f" id="9a5-3N-Ylk"/>
<outlet property="statusMenu" destination="Afa-lD-EbR" id="cov-E9-WrO"/>
</connections>
</customObject>
@ -74,6 +75,12 @@
<action selector="actionStartAtLogin:" target="Voe-Tx-rLC" id="aie-1F-HjZ"/>
</connections>
</menuItem>
<menuItem title="Show network indicator" id="YIO-Vj-64f">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="actionShowNetSpeedIndicator:" target="Voe-Tx-rLC" id="iHm-b9-VSa"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="UpD-DI-Smv"/>
<menuItem title="Config" id="JMV-Dy-CI0">
<modifierMask key="keyEquivalentModifierMask"/>

View File

@ -35,6 +35,16 @@ class ConfigManager {
}
let proxyPortAutoSetObservable = UserDefaults.standard.rx.observe(Bool.self, "proxyPortAutoSet")
var showNetSpeedIndicator:Bool {
get{
return UserDefaults.standard.bool(forKey: "showNetSpeedIndicator")
}
set {
UserDefaults.standard.set(newValue, forKey: "showNetSpeedIndicator")
}
}
let showNetSpeedIndicatorObservable = UserDefaults.standard.rx.observe(Bool.self, "showNetSpeedIndicator")
static var apiUrl:String{
get {
return "http://127.0.0.1:8080"

View File

@ -14,9 +14,10 @@ class StatusItemView: NSView {
@IBOutlet var uploadSpeedLabel: NSTextField!
@IBOutlet var downloadSpeedLabel: NSTextField!
@IBOutlet weak var speedContainerView: NSView!
weak var statusItem:NSStatusItem?
static func create(statusItem:NSStatusItem,statusMenu:NSMenu)->StatusItemView{
static func create(statusItem:NSStatusItem?,statusMenu:NSMenu)->StatusItemView{
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)!
@ -51,6 +52,10 @@ class StatusItemView: NSView {
}
func showSpeedContainer(show:Bool) {
self.speedContainerView.isHidden = !show
}
override func mouseDown(with event: NSEvent) {
statusItem?.popUpMenu(self.menu!)
}

View File

@ -63,6 +63,7 @@
<connections>
<outlet property="downloadSpeedLabel" destination="Hlw-Vg-wLD" id="Cji-Uh-tet"/>
<outlet property="imageView" destination="7SZ-08-T0A" id="q3S-JL-4mH"/>
<outlet property="speedContainerView" destination="x4I-nn-92U" id="qlv-Jp-oB5"/>
<outlet property="uploadSpeedLabel" destination="srp-1R-o7n" id="s4y-hX-VbP"/>
</connections>
<point key="canvasLocation" x="25.5" y="98.5"/>