Skip to content

Commit

Permalink
makeToast
Browse files Browse the repository at this point in the history
  • Loading branch information
yanue committed Sep 25, 2023
1 parent 0e89f96 commit 8c40d00
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 36 deletions.
30 changes: 11 additions & 19 deletions V2rayU/MainMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,23 @@ 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)
}

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() {
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down
11 changes: 0 additions & 11 deletions V2rayU/ToastWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions V2rayU/V2rayLaunch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class V2rayLaunch: NSObject {
}
}

static func Start() {
static func Start() -> Bool {
// start http server
startHttpServer()

Expand All @@ -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
Expand All @@ -142,7 +142,7 @@ class V2rayLaunch: NSObject {
makeToast(message: toast, displayDuration: 10)
menuController.stopV2rayCore()
preferencesWindowController.show(preferencePane: .advanceTab)
return
return false
}

// reinstance
Expand All @@ -160,7 +160,7 @@ class V2rayLaunch: NSObject {
DispatchQueue.main.async {
NSLog("V2rayLaunch process been killed, restart now")
V2rayLaunch.Stop()
V2rayLaunch.Start()
_ = V2rayLaunch.Start()
}
}
}
Expand All @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion V2rayU/V2rayServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 8c40d00

Please sign in to comment.