From 8c40d00f3ca1c611ee92d1fc9f944e93fa75c753 Mon Sep 17 00:00:00 2001 From: yanue Date: Mon, 25 Sep 2023 23:27:46 +0800 Subject: [PATCH] makeToast --- V2rayU/MainMenu.swift | 30 +++++++++++------------------- V2rayU/ToastWindow.swift | 11 ----------- V2rayU/V2rayLaunch.swift | 12 +++++++----- V2rayU/V2rayServer.swift | 2 +- 4 files changed, 19 insertions(+), 36 deletions(-) diff --git a/V2rayU/MainMenu.swift b/V2rayU/MainMenu.swift index 96f0d73..31f0a4e 100644 --- a/V2rayU/MainMenu.swift +++ b/V2rayU/MainMenu.swift @@ -41,8 +41,11 @@ var qrcodeWindow = QrcodeWindowController() var toastWindowCtrl = ToastWindowController() func makeToast(message: String, displayDuration: Double? = 2) { + toastWindowCtrl.close() + toastWindowCtrl = ToastWindowController() toastWindowCtrl.message = message - toastWindowCtrl.showWindow(Any.self) + toastWindowCtrl.showWindow(nil) + toastWindowCtrl.becomeFirstResponder() toastWindowCtrl.fadeInHud(displayDuration) } @@ -50,17 +53,11 @@ func ToggleRunning(_ toast: Bool = true) { // turn off if UserDefaults.getBool(forKey: .v2rayTurnOn) { menuController.stopV2rayCore() - if toast { - makeToast(message: "v2ray-core: Off") - } return } // start menuController.startV2rayCore() - if toast { - makeToast(message: "v2ray-core: On") - } } func SwitchProxyMode() { @@ -69,15 +66,12 @@ func SwitchProxyMode() { switch runMode { case .pac: menuController.switchRunMode(runMode: .global) - makeToast(message: "V2rayU: global Mode") break case .global: menuController.switchRunMode(runMode: .manual) - makeToast(message: "V2rayU: manual Mode") break case .manual: menuController.switchRunMode(runMode: .pac) - makeToast(message: "V2rayU: pac Mode") break default: break @@ -276,13 +270,6 @@ class MenuController: NSObject, NSMenuDelegate { // create json file V2rayLaunch.createJsonFile(item: v2ray) - // set status - setStatusOn(runMode: runMode) - - // launch - V2rayLaunch.Start() - NSLog("start v2ray-core done.") - // switch run mode self.switchRunMode(runMode: runMode) } @@ -527,10 +514,15 @@ class MenuController: NSObject, NSMenuDelegate { self.pacMode.state = runMode == .pac ? .on : .off self.manualMode.state = runMode == .manual ? .on : .off + // launch + let started = V2rayLaunch.Start() + if !started { + return + } + // set icon setStatusOn(runMode: runMode) - // launch - V2rayLaunch.Start() + // manual mode if lastRunMode == RunMode.manual.rawValue { // backup first diff --git a/V2rayU/ToastWindow.swift b/V2rayU/ToastWindow.swift index 522ad02..cf1a51c 100644 --- a/V2rayU/ToastWindow.swift +++ b/V2rayU/ToastWindow.swift @@ -38,17 +38,6 @@ class ToastWindowController: NSWindowController { self.shouldCascadeWindows = false - // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file. - if let win = self.window { - win.isOpaque = false - win.backgroundColor = .clear - win.styleMask = NSWindow.StyleMask.borderless - win.hidesOnDeactivate = false - win.collectionBehavior = NSWindow.CollectionBehavior.canJoinAllSpaces - win.level = NSWindow.Level(rawValue: Int(CGWindowLevelForKey(.floatingWindow))) - win.orderFrontRegardless() - } - let viewLayer: CALayer = CALayer() viewLayer.backgroundColor = CGColor.init(red: 0.05, green: 0.05, blue: 0.05, alpha: kHudAlphaValue) viewLayer.cornerRadius = kHudCornerRadius diff --git a/V2rayU/V2rayLaunch.swift b/V2rayU/V2rayLaunch.swift index f2bdbda..c30a268 100644 --- a/V2rayU/V2rayLaunch.swift +++ b/V2rayU/V2rayLaunch.swift @@ -102,7 +102,7 @@ class V2rayLaunch: NSObject { } } - static func Start() { + static func Start() -> Bool { // start http server startHttpServer() @@ -128,7 +128,7 @@ class V2rayLaunch: NSObject { makeToast(message: toast, displayDuration: 10) menuController.stopV2rayCore() preferencesWindowController.show(preferencePane: .advanceTab) - return + return false } // port has been used @@ -142,7 +142,7 @@ class V2rayLaunch: NSObject { makeToast(message: toast, displayDuration: 10) menuController.stopV2rayCore() preferencesWindowController.show(preferencePane: .advanceTab) - return + return false } // reinstance @@ -160,7 +160,7 @@ class V2rayLaunch: NSObject { DispatchQueue.main.async { NSLog("V2rayLaunch process been killed, restart now") V2rayLaunch.Stop() - V2rayLaunch.Start() + _ = V2rayLaunch.Start() } } } @@ -171,10 +171,12 @@ class V2rayLaunch: NSObject { // ping and select server if let v2ray = V2rayServer.loadSelectedItem() { // ping and refresh - DispatchQueue.global().async { + DispatchQueue.global(qos: .background).async { PingCurrent(item: v2ray).doPing() } } + + return true } static func Stop() { diff --git a/V2rayU/V2rayServer.swift b/V2rayU/V2rayServer.swift index 0ddfd56..adb6d9f 100644 --- a/V2rayU/V2rayServer.swift +++ b/V2rayU/V2rayServer.swift @@ -185,7 +185,7 @@ class V2rayServer: NSObject { for item in V2rayServer.v2rayItemList { // print("remove item: ", subscribe, item.subscribe) - if item.subscribe == subscribe && item.url == "" { + if item.subscribe == subscribe && item.url == "" && subscribe == "" { V2rayItem.remove(name: item.name) // if cuerrent item is default if curName != nil && item.name == curName {