Feature: add outbound mode selection remember.
This commit is contained in:
parent
d9a9e75081
commit
66572c19ff
@ -150,13 +150,20 @@ class AppDelegate: NSObject, NSApplicationDelegate {
|
||||
}
|
||||
}
|
||||
|
||||
func selectOutBoundModeWithMenory() {
|
||||
ApiRequest.updateOutBoundMode(mode: ConfigManager.selectOutBoundMode){
|
||||
_ in
|
||||
self.syncConfig()
|
||||
}
|
||||
}
|
||||
|
||||
func updateProxyList() {
|
||||
ProxyMenuItemFactory.menuItems { [unowned self] (menus) in
|
||||
let startIndex = self.statusMenu.items.index(of: self.separatorLineTop)! + 1
|
||||
let endIndex = self.statusMenu.items.index(of: self.sepatatorLineEndProxySelect)! - 1
|
||||
let startIndex = self.statusMenu.items.index(of: self.separatorLineTop)!+1
|
||||
let endIndex = self.statusMenu.items.index(of: self.sepatatorLineEndProxySelect)!
|
||||
var items = self.statusMenu.items
|
||||
|
||||
items.removeSubrange(ClosedRange(uncheckedBounds: (lower: startIndex, upper: endIndex)))
|
||||
|
||||
items.removeSubrange(Range(uncheckedBounds: (lower: startIndex, upper: endIndex)))
|
||||
|
||||
for each in menus {
|
||||
items.insert(each, at: startIndex)
|
||||
@ -173,8 +180,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
|
||||
ssQueue.async {
|
||||
run()
|
||||
}
|
||||
syncConfig()
|
||||
self.resetStreamApi()
|
||||
self.selectOutBoundModeWithMenory()
|
||||
}
|
||||
|
||||
func syncConfig(){
|
||||
@ -278,12 +285,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
|
||||
}
|
||||
let config = ConfigManager.shared.currentConfig?.copy()
|
||||
config?.mode = mode
|
||||
ApiRequest.requestUpdateConfig(newConfig: config) { (success) in
|
||||
if (success) {
|
||||
ConfigManager.shared.currentConfig = config
|
||||
}
|
||||
ApiRequest.updateOutBoundMode(mode: mode) { (success) in
|
||||
ConfigManager.shared.currentConfig = config
|
||||
ConfigManager.selectOutBoundMode = mode
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@IBAction func actionShowNetSpeedIndicator(_ sender: NSMenuItem) {
|
||||
|
@ -86,21 +86,9 @@ class ApiRequest{
|
||||
callback(success==0)
|
||||
}
|
||||
|
||||
static func requestUpdateConfig(newConfig:ClashConfig?, callback:@escaping ((Bool)->())) {
|
||||
guard (newConfig != nil) else {
|
||||
callback(false)
|
||||
return
|
||||
}
|
||||
let encoder = JSONEncoder()
|
||||
let jsonData = try! encoder.encode(newConfig)
|
||||
|
||||
let url = URL(string:ConfigManager.apiUrl + "/configs")!
|
||||
var req = URLRequest(url: url)
|
||||
req.httpMethod = HTTPMethod.put.rawValue
|
||||
req.setValue("application/json; charset=UTF-8", forHTTPHeaderField: "Content-Type")
|
||||
req.httpBody = jsonData
|
||||
|
||||
request(req).responseJSON { response in
|
||||
static func updateOutBoundMode(mode:ClashProxyMode, callback:@escaping ((Bool)->())) {
|
||||
request(ConfigManager.apiUrl + "/configs", method: .put, parameters: ["mode":mode.rawValue], encoding: JSONEncoding.default)
|
||||
.responseJSON{ response in
|
||||
switch response.result {
|
||||
case .success(_):
|
||||
callback(true)
|
||||
@ -110,6 +98,7 @@ class ApiRequest{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static func requestProxyGroupList(completeHandler:@escaping (([String:[String:Any]])->())){
|
||||
request(ConfigManager.apiUrl + "/proxies", method: .get).responseJSON{
|
||||
res in
|
||||
|
@ -45,6 +45,7 @@ class ProxyMenuItemFactory {
|
||||
let menu = NSMenuItem(title: proxyGroup.key, action: nil, keyEquivalent: "")
|
||||
let selectedName = proxyGroup.value["now"].stringValue
|
||||
let submenu = NSMenu(title: proxyGroup.key)
|
||||
var hasSelected = false
|
||||
for proxy in proxyGroup.value["all"].arrayValue {
|
||||
if isGlobalMode {
|
||||
if json[proxy.stringValue]["type"] == "Selector" {
|
||||
@ -53,10 +54,16 @@ class ProxyMenuItemFactory {
|
||||
}
|
||||
let proxyItem = NSMenuItem(title: proxy.stringValue, action: #selector(ProxyMenuItemFactory.actionSelectProxy(sender:)), keyEquivalent: "")
|
||||
proxyItem.target = ProxyMenuItemFactory.self
|
||||
proxyItem.state = proxy.stringValue == selectedName ? .on : .off
|
||||
let selected = proxy.stringValue == selectedName
|
||||
proxyItem.state = selected ? .on : .off
|
||||
if selected {hasSelected = true}
|
||||
submenu.addItem(proxyItem)
|
||||
}
|
||||
menu.submenu = submenu
|
||||
|
||||
if (!hasSelected && submenu.items.count>0) {
|
||||
self.actionSelectProxy(sender: submenu.items[0])
|
||||
}
|
||||
return menu
|
||||
}
|
||||
|
||||
|
@ -60,4 +60,13 @@ class ConfigManager {
|
||||
}
|
||||
}
|
||||
|
||||
static var selectOutBoundMode:ClashProxyMode {
|
||||
get{
|
||||
return ClashProxyMode(rawValue: UserDefaults.standard.string(forKey: "selectOutBoundMode") ?? "") ?? .rule
|
||||
}
|
||||
set {
|
||||
UserDefaults.standard.set(newValue.rawValue, forKey: "selectOutBoundMode")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user