Chore: improve ProxyMenuItem code
This commit is contained in:
parent
9e3c4e6613
commit
538ea84e0e
@ -15,6 +15,7 @@
|
||||
4931969B21631E5E00A8E6E7 /* SpeedDataRecorder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4931969A21631E5D00A8E6E7 /* SpeedDataRecorder.swift */; };
|
||||
493196A2216331F400A8E6E7 /* NetworkChangeNotifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 493196A1216331F400A8E6E7 /* NetworkChangeNotifier.swift */; };
|
||||
493AEAE3221AE3420016FE98 /* AppVersionUtil.swift in Sources */ = {isa = PBXBuildFile; fileRef = 493AEAE2221AE3420016FE98 /* AppVersionUtil.swift */; };
|
||||
493AEAE5221AE7230016FE98 /* ProxyMenuItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 493AEAE4221AE7230016FE98 /* ProxyMenuItem.swift */; };
|
||||
4949D154213242F600EF85E6 /* Paths.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4949D153213242F600EF85E6 /* Paths.swift */; };
|
||||
4952C3BF2115C7CA004A4FA8 /* MenuItemFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4952C3BE2115C7CA004A4FA8 /* MenuItemFactory.swift */; };
|
||||
4952C3D02117027C004A4FA8 /* ConfigFileManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4952C3CF2117027C004A4FA8 /* ConfigFileManager.swift */; };
|
||||
@ -114,6 +115,7 @@
|
||||
4931969A21631E5D00A8E6E7 /* SpeedDataRecorder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpeedDataRecorder.swift; sourceTree = "<group>"; };
|
||||
493196A1216331F400A8E6E7 /* NetworkChangeNotifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkChangeNotifier.swift; sourceTree = "<group>"; };
|
||||
493AEAE2221AE3420016FE98 /* AppVersionUtil.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppVersionUtil.swift; sourceTree = "<group>"; };
|
||||
493AEAE4221AE7230016FE98 /* ProxyMenuItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProxyMenuItem.swift; sourceTree = "<group>"; };
|
||||
4949D153213242F600EF85E6 /* Paths.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Paths.swift; sourceTree = "<group>"; };
|
||||
4952C3BE2115C7CA004A4FA8 /* MenuItemFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuItemFactory.swift; sourceTree = "<group>"; };
|
||||
4952C3CF2117027C004A4FA8 /* ConfigFileManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfigFileManager.swift; sourceTree = "<group>"; };
|
||||
@ -254,6 +256,7 @@
|
||||
495340B220DE68C300B0D3FF /* StatusItemView.swift */,
|
||||
4975E120217A12210027708D /* ProxyMenuItemView.swift */,
|
||||
4975E122217A122E0027708D /* ProxyMenuItemView.xib */,
|
||||
493AEAE4221AE7230016FE98 /* ProxyMenuItem.swift */,
|
||||
);
|
||||
path = Views;
|
||||
sourceTree = "<group>";
|
||||
@ -672,6 +675,7 @@
|
||||
4975E121217A12210027708D /* ProxyMenuItemView.swift in Sources */,
|
||||
49B1086A216A356D0064FFCE /* String+Extension.swift in Sources */,
|
||||
4960A6DB2136529200B940C9 /* JSBridgeHandler.swift in Sources */,
|
||||
493AEAE5221AE7230016FE98 /* ProxyMenuItem.swift in Sources */,
|
||||
49862FA0218418C600A1D5EC /* ClashRule.swift in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
|
@ -57,27 +57,17 @@ class MenuItemFactory {
|
||||
}
|
||||
}
|
||||
|
||||
let proxyItem = ProxyMenuItem(title: proxy.stringValue, action: #selector(MenuItemFactory.actionSelectProxy(sender:)), keyEquivalent: "")
|
||||
let proxyItem = ProxyMenuItem(proxyName: proxy.stringValue, action: #selector(MenuItemFactory.actionSelectProxy(sender:)))
|
||||
|
||||
proxyItem.target = MenuItemFactory.self
|
||||
proxyItem.proxyName = proxy.stringValue
|
||||
let selected = proxy.stringValue == selectedName
|
||||
proxyItem.state = selected ? .on : .off
|
||||
proxyItem.isSelected = proxy.stringValue == selectedName
|
||||
|
||||
if let delay = SpeedDataRecorder.shared.getDelay(proxy.stringValue) {
|
||||
let menuItemView = ProxyMenuItemView.create(proxy: proxy.stringValue, delay: delay)
|
||||
menuItemView.isSelected = selected
|
||||
menuItemView.onClick = { [weak proxyItem] in
|
||||
guard let proxyItem = proxyItem else {return}
|
||||
MenuItemFactory.actionSelectProxy(sender: proxyItem)
|
||||
}
|
||||
let fittitingWidth = menuItemView.fittingSize.width
|
||||
if (fittitingWidth > submenu.minimumWidth) {
|
||||
submenu.minimumWidth = fittitingWidth
|
||||
}
|
||||
proxyItem.view = menuItemView
|
||||
let fittitingWidth = proxyItem.suggestWidth()
|
||||
if fittitingWidth > submenu.minimumWidth {
|
||||
submenu.minimumWidth = fittitingWidth
|
||||
}
|
||||
|
||||
if selected {hasSelected = true}
|
||||
if proxyItem.isSelected {hasSelected = true}
|
||||
submenu.addItem(proxyItem)
|
||||
submenu.autoenablesItems = false
|
||||
|
||||
@ -110,19 +100,11 @@ class MenuItemFactory {
|
||||
let submenu = NSMenu(title: proxyGroup.key)
|
||||
|
||||
for proxy in proxyGroup.value["all"].arrayValue {
|
||||
let proxyItem = ProxyMenuItem(title: proxy.stringValue, action:nil, keyEquivalent: "")
|
||||
proxyItem.proxyName = proxy.stringValue
|
||||
|
||||
if let delay = SpeedDataRecorder.shared.getDelay(proxy.stringValue) {
|
||||
let menuItemView = ProxyMenuItemView.create(proxy: proxy.stringValue, delay: delay)
|
||||
menuItemView.isSelected = false
|
||||
let fittitingWidth = menuItemView.fittingSize.width
|
||||
if (fittitingWidth > submenu.minimumWidth) {
|
||||
submenu.minimumWidth = fittitingWidth
|
||||
}
|
||||
proxyItem.view = menuItemView
|
||||
let proxyItem = ProxyMenuItem(proxyName: proxy.stringValue, action: #selector(MenuItemFactory.actionSelectProxy(sender:)))
|
||||
let fittitingWidth = proxyItem.suggestWidth()
|
||||
if fittitingWidth > submenu.minimumWidth {
|
||||
submenu.minimumWidth = fittitingWidth
|
||||
}
|
||||
|
||||
submenu.addItem(proxyItem)
|
||||
}
|
||||
|
||||
@ -172,6 +154,3 @@ extension MenuItemFactory {
|
||||
}
|
||||
}
|
||||
|
||||
class ProxyMenuItem:NSMenuItem {
|
||||
var proxyName:String = ""
|
||||
}
|
||||
|
45
ClashX/Views/ProxyMenuItem.swift
Normal file
45
ClashX/Views/ProxyMenuItem.swift
Normal file
@ -0,0 +1,45 @@
|
||||
//
|
||||
// ProxyMenuItem.swift
|
||||
// ClashX
|
||||
//
|
||||
// Created by CYC on 2019/2/18.
|
||||
// Copyright © 2019 west2online. All rights reserved.
|
||||
//
|
||||
|
||||
import Cocoa
|
||||
|
||||
class ProxyMenuItem:NSMenuItem {
|
||||
var proxyName:String = ""
|
||||
|
||||
init(proxyName string: String, action selector: Selector?) {
|
||||
super.init(title: string, action: selector, keyEquivalent: "")
|
||||
|
||||
if let delay = SpeedDataRecorder.shared.getDelay(string) {
|
||||
let menuItemView = ProxyMenuItemView.create(proxy: string, delay: delay)
|
||||
menuItemView.onClick = { [weak self] in
|
||||
guard let self = self else {return}
|
||||
MenuItemFactory.actionSelectProxy(sender: self)
|
||||
}
|
||||
self.view = menuItemView
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
required init(coder decoder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
var isSelected:Bool = false {
|
||||
didSet {
|
||||
self.state = isSelected ? .on : .off
|
||||
(self.view as? ProxyMenuItemView)?.isSelected = isSelected
|
||||
}
|
||||
}
|
||||
|
||||
func suggestWidth()->CGFloat {
|
||||
return self.view?.fittingSize.width ?? 0
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user