fix auto group selection status

This commit is contained in:
yicheng 2020-04-26 18:32:20 +08:00
parent 5eed7f9c85
commit 2cfec770af
4 changed files with 38 additions and 33 deletions

View File

@ -152,21 +152,17 @@ class MenuItemFactory {
}
let submenu = ProxyGroupMenu(title: proxyGroup.name)
let isSpeedtestAble = proxyGroup.speedtestAble.count > 0
for proxy in proxyGroup.all ?? [] {
guard let proxyModel = proxyMap[proxy] else { continue }
let proxyItem = ProxyMenuItem(proxy: proxyModel,
group: proxyGroup,
action: #selector(MenuItemFactory.actionSelectProxy(sender:)),
speedtestAble: isSpeedtestAble,
maxProxyNameLength: proxyGroup.maxProxyNameLength)
action: #selector(MenuItemFactory.actionSelectProxy(sender:)))
proxyItem.target = MenuItemFactory.self
submenu.add(delegate: proxyItem)
submenu.addItem(proxyItem)
}
if isSpeedtestAble && useViewToRenderProxy {
if proxyGroup.isSpeedTestable && useViewToRenderProxy {
submenu.minimumWidth = proxyGroup.maxProxyNameLength + ProxyItemView.fixedPlaceHolderWidth
}
@ -189,7 +185,7 @@ class MenuItemFactory {
for proxyName in proxyGroup.all ?? [] {
guard let proxy = proxyMap[proxyName] else { continue }
let proxyMenuItem = NSMenuItem(title: proxy.name, action: #selector(empty), keyEquivalent: "")
let proxyMenuItem = ProxyMenuItem(proxy: proxy, group: proxyGroup, action: #selector(empty), simpleItem: true)
proxyMenuItem.target = MenuItemFactory.self
if proxy.name == selectedName {
proxyMenuItem.state = .on
@ -224,19 +220,16 @@ class MenuItemFactory {
let menu = NSMenuItem(title: proxyGroup.name, action: nil, keyEquivalent: "")
let submenu = ProxyGroupMenu(title: proxyGroup.name)
let isSpeedTestAble = proxyGroup.speedtestAble.count > 0
for proxy in proxyGroup.all ?? [] {
guard let proxyModel = proxyMap[proxy] else { continue }
let proxyItem = ProxyMenuItem(proxy: proxyModel,
group: proxyGroup,
action: #selector(empty),
speedtestAble: isSpeedTestAble,
maxProxyNameLength: proxyGroup.maxProxyNameLength)
action: #selector(empty))
proxyItem.target = MenuItemFactory.self
submenu.add(delegate: proxyItem)
submenu.addItem(proxyItem)
}
if isSpeedTestAble && useViewToRenderProxy {
if proxyGroup.isSpeedTestable && useViewToRenderProxy {
submenu.minimumWidth = proxyGroup.maxProxyNameLength + ProxyItemView.fixedPlaceHolderWidth
}
addSpeedTestMenuItem(submenu, proxyGroup: proxyGroup)
@ -255,8 +248,7 @@ class MenuItemFactory {
let proxyItem = ProxyMenuItem(proxy: proxyModel,
group: proxyGroup,
action: #selector(empty),
speedtestAble: false,
maxProxyNameLength: proxyGroup.maxProxyNameLength)
simpleItem: true)
proxyItem.target = MenuItemFactory.self
submenu.add(delegate: proxyItem)
submenu.addItem(proxyItem)

View File

@ -114,6 +114,10 @@ class ClashProxy: Codable {
return proxys
}()
lazy var isSpeedTestable: Bool = {
return speedtestAble.count > 0
}()
private enum CodingKeys: String, CodingKey {
case type, all, history, now, name
}

View File

@ -15,7 +15,7 @@ class ProxyItemView: MenuItemBaseView {
static let fixedPlaceHolderWidth: CGFloat = 20 + 50 + 25
init(proxy: ClashProxy, selected: Bool) {
init(proxy: ClashProxy) {
nameLabel = VibrancyTextField(labelWithString: proxy.name)
delayLabel = VibrancyTextField(labelWithString: "").setup(allowsVibrancy: false)
let cell = PaddedNSTextFieldCell()
@ -23,7 +23,6 @@ class ProxyItemView: MenuItemBaseView {
cell.heightPadding = 1
delayLabel.cell = cell
super.init(autolayout: false)
update(selected: selected)
effectView.addSubview(nameLabel)
effectView.addSubview(delayLabel)

View File

@ -23,24 +23,27 @@ class ProxyMenuItem: NSMenuItem {
init(proxy: ClashProxy,
group: ClashProxy,
action selector: Selector?,
speedtestAble: Bool,
maxProxyNameLength: CGFloat) {
simpleItem: Bool = false) {
proxyName = proxy.name
let selected = group.now == proxy.name
self.maxProxyNameLength = maxProxyNameLength
super.init(title: proxyName, action: selector, keyEquivalent: "")
if speedtestAble && enableShowUsingView {
view = ProxyItemView(proxy: proxy, selected: selected)
} else {
if speedtestAble {
attributedTitle = getAttributedTitle(name: proxyName, delay: proxy.history.last?.delayDisplay)
}
state = selected ? .on : .off
}
NotificationCenter.default.addObserver(self, selector: #selector(updateDelayNotification(note:)), name: .speedTestFinishForProxy, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(proxyInfoUpdate(note:)), name: .proxyUpdate(for: proxy.name), object: nil)
maxProxyNameLength = simpleItem ? 0 : group.maxProxyNameLength
super.init(title: proxyName, action: selector, keyEquivalent: "")
if !simpleItem && enableShowUsingView && group.isSpeedTestable {
view = ProxyItemView(proxy: proxy)
} else if !simpleItem {
attributedTitle = getAttributedTitle(name: proxyName, delay: proxy.history.last?.delayDisplay)
}
let selected = group.now == proxy.name
updateSelected(selected)
NotificationCenter.default.addObserver(self, selector: #selector(proxyGroupInfoUpdate(note:)), name: .proxyUpdate(for: group.name), object: nil)
if !simpleItem {
NotificationCenter.default.addObserver(self, selector: #selector(updateDelayNotification(note:)), name: .speedTestFinishForProxy, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(proxyInfoUpdate(note:)), name: .proxyUpdate(for: proxy.name), object: nil)
}
}
required init(coder decoder: NSCoder) {
@ -74,8 +77,15 @@ class ProxyMenuItem: NSMenuItem {
@objc private func proxyGroupInfoUpdate(note: Notification) {
guard let group = note.object as? ClashProxy else { return }
let selected = group.now == proxyName
state = selected ? .on : .off
(view as? ProxyItemView)?.update(selected: selected)
updateSelected(selected)
}
private func updateSelected(_ selected: Bool) {
if let v = view as? ProxyItemView {
v.update(selected: selected)
} else {
state = selected ? .on : .off
}
}
private func updateDelay(_ delay: String?, rawValue: Int?) {