Optimize menu & formate
This commit is contained in:
parent
d61a051901
commit
d5d94c49ba
@ -74,7 +74,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
|
||||
ConfigFileManager.copySampleConfigIfNeed()
|
||||
|
||||
PFMoveToApplicationsFolderIfNecessary()
|
||||
|
||||
|
||||
// claer not existed selected model
|
||||
removeUnExistProxyGroups()
|
||||
|
||||
@ -564,7 +564,7 @@ extension AppDelegate {
|
||||
func selectProxyGroupWithMemory() {
|
||||
let copy = [SavedProxyModel](ConfigManager.selectedProxyRecords)
|
||||
for item in copy {
|
||||
guard item.config == ConfigManager.selectConfigName else {continue}
|
||||
guard item.config == ConfigManager.selectConfigName else { continue }
|
||||
ApiRequest.updateProxyGroup(group: item.group, selectProxy: item.selected) { success in
|
||||
if !success {
|
||||
ConfigManager.selectedProxyRecords.removeAll { model -> Bool in
|
||||
@ -574,7 +574,7 @@ extension AppDelegate {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func removeUnExistProxyGroups() {
|
||||
let list = ConfigManager.getConfigFilesList()
|
||||
let unexists = ConfigManager.selectedProxyRecords.filter {
|
||||
|
@ -191,7 +191,7 @@ class ApiRequest {
|
||||
requestProxyGroupList {
|
||||
proxyInfo in
|
||||
let lists: [ClashProxyName] = proxyInfo.proxies
|
||||
.filter { $0.name == "GLOBAL"}
|
||||
.filter { $0.name == "GLOBAL" }
|
||||
.first?.all ?? []
|
||||
callback(lists)
|
||||
}
|
||||
|
@ -107,7 +107,7 @@ class ConfigManager {
|
||||
SavedProxyModel.save(selectedProxyRecords)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static var selectOutBoundMode: ClashProxyMode {
|
||||
get {
|
||||
return ClashProxyMode(rawValue: UserDefaults.standard.string(forKey: "selectOutBoundMode") ?? "") ?? .rule
|
||||
|
@ -94,16 +94,6 @@ class MenuItemFactory {
|
||||
}
|
||||
let submenu = NSMenu(title: proxyGroup.name)
|
||||
|
||||
let nowMenuItem = NSMenuItem(title: "now:\(selectedName)", action: #selector(empty), keyEquivalent: "")
|
||||
nowMenuItem.target = MenuItemFactory.self
|
||||
|
||||
if let nowProxy = proxyMap[selectedName], let historyMenu = generateHistoryMenu(nowProxy) {
|
||||
nowMenuItem.submenu = historyMenu
|
||||
}
|
||||
|
||||
submenu.addItem(nowMenuItem)
|
||||
submenu.addItem(NSMenuItem.separator())
|
||||
|
||||
for proxyName in proxyGroup.all ?? [] {
|
||||
guard let proxy = proxyMap[proxyName] else { continue }
|
||||
let proxyMenuItem = NSMenuItem(title: proxy.name, action: #selector(empty), keyEquivalent: "")
|
||||
|
@ -95,7 +95,7 @@ class ClashProxy: Codable {
|
||||
|
||||
lazy var maxProxyNameLength: CGFloat = {
|
||||
let rect = CGSize(width: CGFloat.greatestFiniteMagnitude, height: 20)
|
||||
let attr = [NSAttributedString.Key.font: NSFont.menuBarFont(ofSize: 0)]
|
||||
let attr = [NSAttributedString.Key.font: NSFont.menuBarFont(ofSize: 14)]
|
||||
return (self.maxProxyName as NSString)
|
||||
.boundingRect(with: rect,
|
||||
options: .usesLineFragmentOrigin,
|
||||
|
@ -14,7 +14,7 @@ struct SavedProxyModel: Codable {
|
||||
let config: String
|
||||
|
||||
static let key = "SavedProxyModels"
|
||||
|
||||
|
||||
static func migrate() -> [SavedProxyModel]? {
|
||||
if let mapping = UserDefaults.standard.dictionary(forKey: "selectedProxyMap") as? [ClashProxyName: ClashProxyName] {
|
||||
var models = [SavedProxyModel]()
|
||||
@ -25,9 +25,8 @@ struct SavedProxyModel: Codable {
|
||||
return models
|
||||
}
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
|
||||
static func loadsFromUserDefault() -> [SavedProxyModel] {
|
||||
if let data = UserDefaults.standard.object(forKey: key) as? Data,
|
||||
let models = try? JSONDecoder().decode([SavedProxyModel].self, from: data) {
|
||||
@ -39,7 +38,7 @@ struct SavedProxyModel: Codable {
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
|
||||
static func save(_ models: [SavedProxyModel]) {
|
||||
if let data = try? JSONEncoder().encode(models) {
|
||||
UserDefaults.standard.set(data, forKey: key)
|
||||
@ -47,6 +46,4 @@ struct SavedProxyModel: Codable {
|
||||
}
|
||||
}
|
||||
|
||||
extension SavedProxyModel: Equatable {
|
||||
|
||||
}
|
||||
extension SavedProxyModel: Equatable {}
|
||||
|
@ -6,18 +6,18 @@
|
||||
// Copyright © 2019 west2online. All rights reserved.
|
||||
//
|
||||
|
||||
import Cocoa
|
||||
import Carbon
|
||||
import Cocoa
|
||||
|
||||
class MenuItemBaseView: NSView {
|
||||
|
||||
private var isMouseInsideView = false
|
||||
private var isMenuOpen = false
|
||||
private var eventHandler: EventHandlerRef?
|
||||
private let handleClick: Bool
|
||||
private let autolayout: Bool
|
||||
|
||||
|
||||
// MARK: Public
|
||||
|
||||
var isHighlighted: Bool {
|
||||
if #available(macOS 10.15.1, *) {
|
||||
return isMouseInsideView || isMenuOpen
|
||||
@ -25,7 +25,7 @@ class MenuItemBaseView: NSView {
|
||||
return enclosingMenuItem?.isHighlighted ?? false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
let effectView: NSVisualEffectView = {
|
||||
let effectView = NSVisualEffectView()
|
||||
effectView.material = .popover
|
||||
@ -34,33 +34,39 @@ class MenuItemBaseView: NSView {
|
||||
effectView.blendingMode = .behindWindow
|
||||
return effectView
|
||||
}()
|
||||
|
||||
|
||||
var labels: [NSTextField] {
|
||||
assertionFailure("Please override")
|
||||
return []
|
||||
}
|
||||
|
||||
static let labelFont = NSFont.menuFont(ofSize: 14)
|
||||
|
||||
init(frame frameRect: NSRect = .zero, handleClick:Bool, autolayout:Bool) {
|
||||
|
||||
init(frame frameRect: NSRect = .zero, handleClick: Bool, autolayout: Bool) {
|
||||
self.handleClick = handleClick
|
||||
self.autolayout = autolayout
|
||||
super.init(frame: frameRect)
|
||||
setupView()
|
||||
}
|
||||
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
|
||||
func setNeedsDisplay() {
|
||||
setNeedsDisplay(bounds)
|
||||
}
|
||||
|
||||
|
||||
func didClickView() {
|
||||
assertionFailure("Please override this method")
|
||||
}
|
||||
|
||||
|
||||
func updateBackground(_ label: NSTextField) {
|
||||
label.cell?.backgroundStyle = isHighlighted ? .emphasized : .normal
|
||||
}
|
||||
|
||||
|
||||
// MARK: Private
|
||||
|
||||
private func setupView() {
|
||||
translatesAutoresizingMaskIntoConstraints = false
|
||||
heightAnchor.constraint(equalToConstant: 20).isActive = true
|
||||
@ -71,9 +77,8 @@ class MenuItemBaseView: NSView {
|
||||
effectView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
|
||||
effectView.topAnchor.constraint(equalTo: topAnchor).isActive = true
|
||||
effectView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
|
||||
|
||||
}
|
||||
|
||||
|
||||
private func updateCarbon() {
|
||||
if window != nil {
|
||||
if let dispatcher = GetEventDispatcherTarget() {
|
||||
@ -92,23 +97,22 @@ class MenuItemBaseView: NSView {
|
||||
RemoveEventHandler(eventHandler)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// MARK: Override
|
||||
|
||||
|
||||
override func draw(_ dirtyRect: NSRect) {
|
||||
super.draw(dirtyRect)
|
||||
effectView.material = isHighlighted ? .selection : .popover
|
||||
labels.forEach { updateBackground($0) }
|
||||
}
|
||||
|
||||
|
||||
override func viewDidMoveToWindow() {
|
||||
super.viewDidMoveToWindow()
|
||||
if handleClick {
|
||||
updateCarbon()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
override func viewDidMoveToSuperview() {
|
||||
super.viewDidMoveToSuperview()
|
||||
guard autolayout else { return }
|
||||
@ -118,7 +122,7 @@ class MenuItemBaseView: NSView {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
override func updateTrackingAreas() {
|
||||
super.updateTrackingAreas()
|
||||
if #available(macOS 10.15.1, *) {
|
||||
@ -127,20 +131,20 @@ class MenuItemBaseView: NSView {
|
||||
addTrackingArea(NSTrackingArea(rect: bounds, options: [.mouseEnteredAndExited, .activeAlways], owner: self, userInfo: nil))
|
||||
}
|
||||
}
|
||||
|
||||
override func mouseEntered(with event: NSEvent) {
|
||||
if #available(macOS 10.15.1, *) {
|
||||
isMouseInsideView = true
|
||||
setNeedsDisplay()
|
||||
}
|
||||
}
|
||||
|
||||
override func mouseExited(with event: NSEvent) {
|
||||
if #available(macOS 10.15.1, *) {
|
||||
isMouseInsideView = false
|
||||
setNeedsDisplay()
|
||||
}
|
||||
}
|
||||
override func mouseEntered(with event: NSEvent) {
|
||||
if #available(macOS 10.15.1, *) {
|
||||
isMouseInsideView = true
|
||||
setNeedsDisplay()
|
||||
}
|
||||
}
|
||||
|
||||
override func mouseExited(with event: NSEvent) {
|
||||
if #available(macOS 10.15.1, *) {
|
||||
isMouseInsideView = false
|
||||
setNeedsDisplay()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension MenuItemBaseView: NSMenuDelegate {
|
||||
@ -158,4 +162,3 @@ extension MenuItemBaseView: NSMenuDelegate {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -13,11 +13,15 @@ class ProxyGroupMenuItemView: MenuItemBaseView {
|
||||
let selectProxyLabel: NSTextField
|
||||
let arrowLabel = NSTextField(labelWithString: "▶")
|
||||
|
||||
override var labels: [NSTextField] {
|
||||
return [groupNameLabel, selectProxyLabel, arrowLabel]
|
||||
}
|
||||
|
||||
init(group: ClashProxyName, targetProxy: ClashProxyName) {
|
||||
groupNameLabel = VibrancyTextField(labelWithString: group)
|
||||
selectProxyLabel = VibrancyTextField(labelWithString: targetProxy)
|
||||
let rect = NSRect(x: 0, y: 0, width: 0, height: 20) // requeie for system before 10.15
|
||||
super.init(frame: rect, handleClick:false, autolayout: true)
|
||||
super.init(frame: rect, handleClick: false, autolayout: true)
|
||||
|
||||
// arrow
|
||||
effectView.addSubview(arrowLabel)
|
||||
@ -30,7 +34,7 @@ class ProxyGroupMenuItemView: MenuItemBaseView {
|
||||
effectView.addSubview(groupNameLabel)
|
||||
groupNameLabel.leftAnchor.constraint(equalTo: effectView.leftAnchor, constant: 20).isActive = true
|
||||
groupNameLabel.centerYAnchor.constraint(equalTo: effectView.centerYAnchor).isActive = true
|
||||
|
||||
|
||||
// select
|
||||
selectProxyLabel.translatesAutoresizingMaskIntoConstraints = false
|
||||
effectView.addSubview(selectProxyLabel)
|
||||
@ -44,21 +48,11 @@ class ProxyGroupMenuItemView: MenuItemBaseView {
|
||||
groupNameLabel.font = type(of: self).labelFont
|
||||
selectProxyLabel.font = type(of: self).labelFont
|
||||
groupNameLabel.textColor = NSColor.labelColor
|
||||
selectProxyLabel.textColor = NSColor.tertiaryLabelColor
|
||||
selectProxyLabel.textColor = NSColor.secondaryLabelColor
|
||||
arrowLabel.textColor = NSColor.labelColor
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
override func draw(_ dirtyRect: NSRect) {
|
||||
super.draw(dirtyRect)
|
||||
updateBackground(groupNameLabel)
|
||||
updateBackground(selectProxyLabel)
|
||||
updateBackground(arrowLabel)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -48,7 +48,7 @@ fileprivate class ProxyGroupSpeedTestMenuItemView: MenuItemBaseView {
|
||||
label = NSTextField(labelWithString: title)
|
||||
label.font = Self.labelFont
|
||||
label.sizeToFit()
|
||||
let rect = NSRect(x: 0, y: 0, width: label.bounds.width + 40, height: 20)
|
||||
let rect = NSRect(x: 0, y: 0, width: label.bounds.width + 40, height: 20)
|
||||
super.init(frame: rect, handleClick: true, autolayout: false)
|
||||
addSubview(label)
|
||||
label.frame = NSRect(x: 20, y: 0, width: label.bounds.width, height: 20)
|
||||
@ -58,7 +58,11 @@ fileprivate class ProxyGroupSpeedTestMenuItemView: MenuItemBaseView {
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
|
||||
override var labels: [NSTextField] {
|
||||
return [label]
|
||||
}
|
||||
|
||||
override func didClickView() {
|
||||
startBenchmark()
|
||||
}
|
||||
@ -89,7 +93,7 @@ fileprivate class ProxyGroupSpeedTestMenuItemView: MenuItemBaseView {
|
||||
self.setNeedsDisplay()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
override func draw(_ dirtyRect: NSRect) {
|
||||
super.draw(dirtyRect)
|
||||
label.textColor = (enclosingMenuItem?.isEnabled ?? true) ? NSColor.labelColor : NSColor.placeholderTextColor
|
||||
|
@ -42,7 +42,7 @@ class ProxyMenuItem: NSMenuItem {
|
||||
func getAttributedTitle(name: String, delay: String) -> NSAttributedString {
|
||||
let paragraph = NSMutableParagraphStyle()
|
||||
paragraph.tabStops = [
|
||||
NSTextTab(textAlignment: .right, location: maxProxyNameLength + 80, options: [:]),
|
||||
NSTextTab(textAlignment: .right, location: maxProxyNameLength + 100, options: [:]),
|
||||
]
|
||||
|
||||
let str = "\(name)\t\(delay)"
|
||||
|
Loading…
Reference in New Issue
Block a user