Improve: fixing delay display in menu items

This commit is contained in:
yicheng 2018-11-04 17:20:25 +08:00 committed by Yicheng
parent b16efe35fa
commit dc7cf1642b
3 changed files with 66 additions and 25 deletions

View File

@ -46,35 +46,42 @@ class ProxyMenuItemFactory {
let selectedName = proxyGroup.value["now"].stringValue
let submenu = NSMenu(title: proxyGroup.key)
var hasSelected = false
submenu.minimumWidth = 20
for proxy in proxyGroup.value["all"].arrayValue {
if isGlobalMode {
if json[proxy.stringValue]["type"] == "Selector" {
continue
}
}
var speedText = ""
let placeHolder = "\t"
if let speedInfo = SpeedDataRecorder.shared.speedDict[proxy.stringValue] {
speedText = speedInfo < Int.max ?"\(placeHolder)\(speedInfo) ms" : "\(placeHolder)fail"
}
let proxyItem = ProxyMenuItem(title: proxy.stringValue + speedText, action: #selector(ProxyMenuItemFactory.actionSelectProxy(sender:)), keyEquivalent: "")
proxyItem.proxyName = proxy.stringValue
let proxyItem = NSMenuItem(title: proxy.stringValue, action: #selector(ProxyMenuItemFactory.actionSelectProxy(sender:)), keyEquivalent: "")
proxyItem.target = ProxyMenuItemFactory.self
let delay = SpeedDataRecorder.shared.speedDict[proxy.stringValue]
let selected = proxy.stringValue == selectedName
proxyItem.state = selected ? .on : .off
proxyItem.view = ProxyMenuItemView.create(proxy: proxy.stringValue, delay: delay)
let menuItemView = ProxyMenuItemView.create(proxy: proxy.stringValue, delay: delay)
menuItemView.isSelected = selected
menuItemView.onClick = { [weak proxyItem] in
guard let proxyItem = proxyItem else {return}
ProxyMenuItemFactory.actionSelectProxy(sender: proxyItem)
}
proxyItem.view = menuItemView
if selected {hasSelected = true}
submenu.addItem(proxyItem)
submenu.autoenablesItems = false
let fittitingWidth = menuItemView.fittingSize.width
if (fittitingWidth > submenu.minimumWidth) {
submenu.minimumWidth = fittitingWidth
}
}
for item in submenu.items {
item.view?.frame.size.width = submenu.minimumWidth
}
menu.submenu = submenu
if (!hasSelected && submenu.items.count>0) {
self.actionSelectProxy(sender: submenu.items[0] as! ProxyMenuItem)
self.actionSelectProxy(sender: submenu.items[0])
}
return menu
}
@ -92,9 +99,9 @@ class ProxyMenuItemFactory {
return menu
}
@objc static func actionSelectProxy(sender:ProxyMenuItem){
@objc static func actionSelectProxy(sender:NSMenuItem){
guard let proxyGroup = sender.menu?.title else {return}
let proxyName = sender.proxyName
let proxyName = sender.title
ApiRequest.updateProxyGroup(group: proxyGroup, selectProxy: proxyName) { (success) in
if (success) {
@ -111,6 +118,3 @@ class ProxyMenuItemFactory {
}
class ProxyMenuItem:NSMenuItem {
var proxyName:String = ""
}

View File

@ -12,14 +12,18 @@ class ProxyMenuItemView: NSView {
var topLevelObjects : NSArray?
if Bundle.main.loadNibNamed("ProxyMenuItemView", owner: self, topLevelObjects: &topLevelObjects) {
let view = (topLevelObjects!.first(where: { $0 is NSView }) as? ProxyMenuItemView)!
view.proxyNameLabel.stringValue = proxy
view.setupView(proxy:proxy,delay:delay)
return view;
}
return NSView() as! ProxyMenuItemView
}
@IBOutlet weak var proxyNameLabel: NSTextField!
var onClick:(()->())? = nil
@IBOutlet weak var proxyNameLabel: NSTextField!
@IBOutlet weak var selectedImageView: NSImageView!
@IBOutlet weak var delayLabel: NSTextField!
var highlighted : Bool = false {
@ -30,7 +34,26 @@ class ProxyMenuItemView: NSView {
}
}
var isSelected:Bool = false {
didSet {
self.selectedImageView.isHidden = !isSelected
}
}
func setupView(proxy:String,delay:Int?){
selectedImageView.image = NSImage(imageLiteralResourceName: NSImage.menuOnStateTemplateName)
proxyNameLabel.stringValue = proxy
if let delay = delay {
switch delay {
case Int.max:delayLabel.stringValue = "Fail"
case ..<0:delayLabel.stringValue = "Unknown"
default:delayLabel.stringValue = "\(delay)ms"
}
} else {
delayLabel.isHidden = true
}
}
override func draw(_ dirtyRect: NSRect) {
@ -38,10 +61,12 @@ class ProxyMenuItemView: NSView {
NSColor.selectedMenuItemColor.set()
self.proxyNameLabel.textColor = NSColor.white
self.delayLabel.textColor = NSColor.white
selectedImageView.image = selectedImageView.image?.tint(color: .white)
} else {
NSColor.clear.set()
self.proxyNameLabel.textColor = NSColor.labelColor
self.delayLabel.textColor = NSColor.labelColor
selectedImageView.image = selectedImageView.image?.tint(color: .labelColor)
}
NSBezierPath.fill(dirtyRect)
super.draw(dirtyRect)
@ -65,6 +90,9 @@ class ProxyMenuItemView: NSView {
override func mouseDown(with event: NSEvent) {
super.mouseDown(with: event)
print("11")
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.enclosingMenuItem?.menu?.cancelTracking()
}
onClick?()
}
}

View File

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="14313.18" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14313.18"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14460.31"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
@ -10,7 +10,7 @@
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<customView focusRingType="none" id="c22-O7-iKe" customClass="ProxyMenuItemView" customModule="ClashX" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="214" height="18"/>
<rect key="frame" x="0.0" y="0.0" width="130" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<subviews>
<textField autoresizesSubviews="NO" horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="2xI-Y4-PnW">
@ -25,18 +25,23 @@
</textFieldCell>
</textField>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="aYx-Wc-u4X">
<rect key="frame" x="171" y="0.0" width="37" height="18"/>
<textFieldCell key="cell" lineBreakMode="clipping" enabled="NO" title="Label" id="90L-CS-E5U">
<rect key="frame" x="120" y="0.0" width="4" height="18"/>
<textFieldCell key="cell" lineBreakMode="clipping" enabled="NO" id="90L-CS-E5U">
<font key="font" metaFont="system"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<imageView horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="G02-U6-KlU">
<rect key="frame" x="2" y="1" width="17" height="17"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<imageCell key="cell" refusesFirstResponder="YES" alignment="left" imageScaling="proportionallyDown" image="menuOnStateTemplateName" id="KJc-uZ-b6K"/>
</imageView>
</subviews>
<constraints>
<constraint firstItem="2xI-Y4-PnW" firstAttribute="leading" secondItem="c22-O7-iKe" secondAttribute="leading" constant="20" id="2Ez-3J-oJy"/>
<constraint firstAttribute="bottom" secondItem="aYx-Wc-u4X" secondAttribute="bottom" id="4cP-1x-iyV"/>
<constraint firstItem="aYx-Wc-u4X" firstAttribute="leading" relation="greaterThanOrEqual" secondItem="2xI-Y4-PnW" secondAttribute="trailing" constant="50" id="FaA-cy-GbD"/>
<constraint firstItem="aYx-Wc-u4X" firstAttribute="leading" relation="greaterThanOrEqual" secondItem="2xI-Y4-PnW" secondAttribute="trailing" constant="15" id="CR1-j3-Rh9"/>
<constraint firstItem="aYx-Wc-u4X" firstAttribute="top" secondItem="c22-O7-iKe" secondAttribute="top" id="M8d-FS-eL5"/>
<constraint firstItem="2xI-Y4-PnW" firstAttribute="top" secondItem="c22-O7-iKe" secondAttribute="top" id="Uyg-Eh-dS6"/>
<constraint firstAttribute="bottom" secondItem="2xI-Y4-PnW" secondAttribute="bottom" id="oRZ-eb-hPi"/>
@ -45,8 +50,12 @@
<connections>
<outlet property="delayLabel" destination="aYx-Wc-u4X" id="4ie-3u-ePM"/>
<outlet property="proxyNameLabel" destination="2xI-Y4-PnW" id="h8l-xP-rbC"/>
<outlet property="selectedImageView" destination="G02-U6-KlU" id="JxC-gB-yiD"/>
</connections>
<point key="canvasLocation" x="-96" y="202"/>
<point key="canvasLocation" x="-138" y="202"/>
</customView>
</objects>
<resources>
<image name="menuOnStateTemplateName" width="128" height="128"/>
</resources>
</document>