Files
venera/macos/Runner/AppDelegate.swift
2024-10-28 17:41:46 +08:00

67 lines
2.5 KiB
Swift

import Cocoa
import FlutterMacOS
@main
class AppDelegate: FlutterAppDelegate {
var flutterResult: FlutterResult?
var directoryPath: URL!
override func applicationDidFinishLaunching(_ notification: Notification) {
let controller: FlutterViewController = mainFlutterWindow?.contentViewController as! FlutterViewController
let methodChannel = FlutterMethodChannel(name: "venera/method_channel", binaryMessenger: controller.engine.binaryMessenger)
methodChannel.setMethodCallHandler { (call, result) in
switch call.method {
case "getProxy":
if let proxySettings = CFNetworkCopySystemProxySettings()?.takeUnretainedValue() as NSDictionary? {
if let httpProxy = proxySettings[kCFNetworkProxiesHTTPProxy] as? String,
let httpPort = proxySettings[kCFNetworkProxiesHTTPPort] as? Int {
let proxyConfig = "\(httpProxy):\(httpPort)"
result(proxyConfig)
} else if let socksProxy = proxySettings[kCFNetworkProxiesSOCKSProxy] as? String,
let socksPort = proxySettings[kCFNetworkProxiesSOCKSPort] as? Int {
let proxyConfig = "\(socksProxy):\(socksPort)"
result(proxyConfig)
} else {
result("")
}
} else {
result("")
}
case "getDirectoryPath":
self.flutterResult = result
self.getDirectoryPath()
case "stopAccessingSecurityScopedResource":
self.directoryPath?.stopAccessingSecurityScopedResource()
result(nil)
default:
result(FlutterMethodNotImplemented)
}
}
}
func getDirectoryPath() {
let openPanel = NSOpenPanel()
openPanel.canChooseDirectories = true
openPanel.canChooseFiles = false
openPanel.allowsMultipleSelection = false
openPanel.begin { (result) in
if result == .OK {
self.directoryPath = openPanel.urls.first
if let directoryPath = self.directoryPath, !directoryPath.startAccessingSecurityScopedResource() {
self.flutterResult?(nil)
return
}
self.flutterResult?(self.directoryPath?.path)
} else {
self.flutterResult?(nil)
}
}
}
override func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return true
}
}