fix select status
This commit is contained in:
parent
631105bb9d
commit
5eed7f9c85
@ -9,7 +9,7 @@
|
||||
import Cocoa
|
||||
|
||||
class VibrancyTextField: NSTextField {
|
||||
private var _allowsVibrancy = false
|
||||
private var _allowsVibrancy = true
|
||||
override var allowsVibrancy: Bool {
|
||||
return _allowsVibrancy
|
||||
}
|
||||
|
@ -11,7 +11,6 @@ import RxCocoa
|
||||
import SwiftyJSON
|
||||
|
||||
class MenuItemFactory {
|
||||
private static var cachedProxyMenuItem: [NSMenuItem]?
|
||||
private static var cachedProxyData: ClashProxyResp?
|
||||
|
||||
private static var showSpeedTestItemAtTop: Bool = UserDefaults.standard.object(forKey: "kShowSpeedTestItemAtTop") as? Bool ?? AppDelegate.isAboveMacOS14 {
|
||||
@ -158,8 +157,8 @@ class MenuItemFactory {
|
||||
for proxy in proxyGroup.all ?? [] {
|
||||
guard let proxyModel = proxyMap[proxy] else { continue }
|
||||
let proxyItem = ProxyMenuItem(proxy: proxyModel,
|
||||
group: proxyGroup,
|
||||
action: #selector(MenuItemFactory.actionSelectProxy(sender:)),
|
||||
selected: proxy == selectedName,
|
||||
speedtestAble: isSpeedtestAble,
|
||||
maxProxyNameLength: proxyGroup.maxProxyNameLength)
|
||||
proxyItem.target = MenuItemFactory.self
|
||||
@ -229,8 +228,8 @@ class MenuItemFactory {
|
||||
for proxy in proxyGroup.all ?? [] {
|
||||
guard let proxyModel = proxyMap[proxy] else { continue }
|
||||
let proxyItem = ProxyMenuItem(proxy: proxyModel,
|
||||
group: proxyGroup,
|
||||
action: #selector(empty),
|
||||
selected: false,
|
||||
speedtestAble: isSpeedTestAble,
|
||||
maxProxyNameLength: proxyGroup.maxProxyNameLength)
|
||||
proxyItem.target = MenuItemFactory.self
|
||||
@ -254,8 +253,8 @@ class MenuItemFactory {
|
||||
for proxy in proxyGroup.all ?? [] {
|
||||
guard let proxyModel = proxyMap[proxy] else { continue }
|
||||
let proxyItem = ProxyMenuItem(proxy: proxyModel,
|
||||
group: proxyGroup,
|
||||
action: #selector(empty),
|
||||
selected: false,
|
||||
speedtestAble: false,
|
||||
maxProxyNameLength: proxyGroup.maxProxyNameLength)
|
||||
proxyItem.target = MenuItemFactory.self
|
||||
|
@ -11,30 +11,22 @@ import Cocoa
|
||||
class ProxyItemView: MenuItemBaseView {
|
||||
let nameLabel: NSTextField
|
||||
let delayLabel: NSTextField
|
||||
let imageView: NSImageView?
|
||||
var imageView: NSImageView?
|
||||
|
||||
static let fixedPlaceHolderWidth: CGFloat = 20 + 50 + 25
|
||||
|
||||
init(proxy: ClashProxy, selected: Bool) {
|
||||
nameLabel = VibrancyTextField(labelWithString: proxy.name)
|
||||
delayLabel = VibrancyTextField(labelWithString: "")
|
||||
delayLabel = VibrancyTextField(labelWithString: "").setup(allowsVibrancy: false)
|
||||
let cell = PaddedNSTextFieldCell()
|
||||
cell.widthPadding = 2
|
||||
cell.heightPadding = 1
|
||||
delayLabel.cell = cell
|
||||
if selected {
|
||||
imageView = NSImageView(image: NSImage(named: NSImage.menuOnStateTemplateName)!)
|
||||
} else {
|
||||
imageView = nil
|
||||
}
|
||||
super.init(autolayout: false)
|
||||
update(selected: selected)
|
||||
effectView.addSubview(nameLabel)
|
||||
effectView.addSubview(delayLabel)
|
||||
if let imageView = imageView {
|
||||
effectView.addSubview(imageView)
|
||||
}
|
||||
|
||||
imageView?.translatesAutoresizingMaskIntoConstraints = false
|
||||
nameLabel.translatesAutoresizingMaskIntoConstraints = false
|
||||
delayLabel.translatesAutoresizingMaskIntoConstraints = false
|
||||
|
||||
@ -83,6 +75,19 @@ class ProxyItemView: MenuItemBaseView {
|
||||
}
|
||||
}
|
||||
|
||||
func update(selected: Bool) {
|
||||
if selected {
|
||||
if imageView == nil {
|
||||
imageView = NSImageView(image: NSImage(named: NSImage.menuOnStateTemplateName)!)
|
||||
imageView?.translatesAutoresizingMaskIntoConstraints = false
|
||||
effectView.addSubview(imageView!)
|
||||
}
|
||||
} else {
|
||||
imageView?.removeFromSuperview()
|
||||
imageView = nil
|
||||
}
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
@ -21,11 +21,12 @@ class ProxyMenuItem: NSMenuItem {
|
||||
}
|
||||
|
||||
init(proxy: ClashProxy,
|
||||
group: ClashProxy,
|
||||
action selector: Selector?,
|
||||
selected: Bool,
|
||||
speedtestAble: Bool,
|
||||
maxProxyNameLength: CGFloat) {
|
||||
proxyName = proxy.name
|
||||
let selected = group.now == proxy.name
|
||||
self.maxProxyNameLength = maxProxyNameLength
|
||||
super.init(title: proxyName, action: selector, keyEquivalent: "")
|
||||
if speedtestAble && enableShowUsingView {
|
||||
@ -39,6 +40,7 @@ class ProxyMenuItem: NSMenuItem {
|
||||
|
||||
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)
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(proxyGroupInfoUpdate(note:)), name: .proxyUpdate(for: group.name), object: nil)
|
||||
}
|
||||
|
||||
required init(coder decoder: NSCoder) {
|
||||
@ -69,6 +71,13 @@ class ProxyMenuItem: NSMenuItem {
|
||||
updateDelay(info.history.last?.delayDisplay, rawValue: info.history.last?.delay)
|
||||
}
|
||||
|
||||
@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)
|
||||
}
|
||||
|
||||
private func updateDelay(_ delay: String?, rawValue: Int?) {
|
||||
if enableShowUsingView {
|
||||
(view as? ProxyItemView)?.update(str: delay, value: rawValue)
|
||||
|
Loading…
Reference in New Issue
Block a user