Navaron Bracke

reimplement toggle torch on MacOS

... ... @@ -59,8 +59,8 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
requestPermission(call, result)
case "start":
start(call, result)
case "torch":
toggleTorch(call, result)
case "toggleTorch":
toggleTorch(result)
case "setScale":
setScale(call, result)
case "resetScale":
... ... @@ -288,12 +288,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
// Turn on the torch if requested.
if (torch) {
do {
try self.toggleTorchInternal(.on)
} catch {
// If the torch could not be turned on,
// continue the capture session.
}
self.turnTorchOn()
}
device.addObserver(self, forKeyPath: #keyPath(AVCaptureDevice.torchMode), options: .new, context: nil)
... ... @@ -336,12 +331,12 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
}
// TODO: this method should be removed when iOS and MacOS share their implementation.
private func toggleTorchInternal(_ torch: AVCaptureDevice.TorchMode) throws {
private func toggleTorchInternal() {
guard let device = self.device else {
return
}
if (!device.hasTorch || !device.isTorchModeSupported(torch)) {
if (!device.hasTorch) {
return
}
... ... @@ -350,12 +345,57 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
return
}
}
var newTorchMode: AVCaptureDevice.TorchMode = device.torchMode
switch(device.torchMode) {
case AVCaptureDevice.TorchMode.auto:
if #available(macOS 10.15, *) {
newTorchMode = device.isTorchActive ? AVCaptureDevice.TorchMode.off : AVCaptureDevice.TorchMode.on
}
break;
case AVCaptureDevice.TorchMode.off:
newTorchMode = AVCaptureDevice.TorchMode.on
break;
case AVCaptureDevice.TorchMode.on:
newTorchMode = AVCaptureDevice.TorchMode.off
break;
default:
return;
}
if (!device.isTorchModeSupported(newTorchMode) || device.torchMode == newTorchMode) {
return;
}
if (device.torchMode != torch) {
do {
try device.lockForConfiguration()
device.torchMode = torch
device.torchMode = newTorchMode
device.unlockForConfiguration()
} catch(_) {}
}
/// Turn the torch on.
private func turnTorchOn() {
guard let device = self.device else {
return
}
if (!device.hasTorch || !device.isTorchModeSupported(.on) || device.torchMode == .on) {
return
}
if #available(macOS 15.0, *) {
if(!device.isTorchAvailable) {
return
}
}
do {
try device.lockForConfiguration()
device.torchMode = .on
device.unlockForConfiguration()
} catch(_) {}
}
/// Reset the zoom scale.
... ... @@ -370,15 +410,9 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
result(nil)
}
private func toggleTorch(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
let requestedTorchMode: AVCaptureDevice.TorchMode = call.arguments as! Int == 1 ? .on : .off
do {
try self.toggleTorchInternal(requestedTorchMode)
result(nil)
} catch {
result(FlutterError(code: "MobileScanner", message: error.localizedDescription, details: nil))
}
private func toggleTorch(_ result: @escaping FlutterResult) {
self.toggleTorchInternal()
result(nil)
}
// func switchAnalyzeMode(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
... ...