From 44faea9a22b4f9230434160f8c285afc87ad5648 Mon Sep 17 00:00:00 2001 From: ShlomoCode <78599753+ShlomoCode@users.noreply.github.com> Date: Sat, 17 Oct 2026 21:30:25 +0300 Subject: [PATCH] fix: report wakelock failures on Windows and macOS --- .../lib/src/wakelock_plus_windows_plugin.dart | 11 ++++- .../WakelockPlusMacosPlugin.swift | 43 +++++++++++++------ 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/wakelock_plus/lib/src/wakelock_plus_windows_plugin.dart b/wakelock_plus/lib/src/wakelock_plus_windows_plugin.dart index 67a3d6c..bb54431 100644 --- a/wakelock_plus/lib/src/wakelock_plus_windows_plugin.dart +++ b/wakelock_plus/lib/src/wakelock_plus_windows_plugin.dart @@ -1,5 +1,6 @@ import 'dart:async'; +import 'package:flutter/services.dart'; import 'package:wakelock_plus_platform_interface/wakelock_plus_platform_interface.dart'; import 'package:win32/win32.dart'; @@ -26,9 +27,15 @@ class WakelockPlusWindowsPlugin extends WakelockPlusPlatformInterface { } // SetThreadExecutionState returns 0 if the operation failed. - if (response != 0) { - _enabled = enable; + if (response == 0) { + final operation = enable ? 'enable' : 'disable'; + throw PlatformException( + code: 'wakelock-error', + message: 'Failed to $operation wakelock.', + details: {'operation': operation}, + ); } + _enabled = enable; } @override diff --git a/wakelock_plus/macos/wakelock_plus/Sources/wakelock_plus/WakelockPlusMacosPlugin.swift b/wakelock_plus/macos/wakelock_plus/Sources/wakelock_plus/WakelockPlusMacosPlugin.swift index ef435e4..d9ef3aa 100644 --- a/wakelock_plus/macos/wakelock_plus/Sources/wakelock_plus/WakelockPlusMacosPlugin.swift +++ b/wakelock_plus/macos/wakelock_plus/Sources/wakelock_plus/WakelockPlusMacosPlugin.swift @@ -17,12 +17,22 @@ public class WakelockPlusMacosPlugin: NSObject, FlutterPlugin { case "toggle": let args = call.arguments as? Dictionary let enable = args!["enable"] as! Bool - if enable { - enableWakelock() + let status = enable ? enableWakelock() : disableWakelock() + if status == kIOReturnSuccess { + result(true) } else { - disableWakelock(); + let operation = enable ? "enable" : "disable" + let description = String(cString: mach_error_string(status)) + result(FlutterError( + code: "wakelock-error", + message: "Failed to \(operation) wakelock: \(description)", + details: [ + "operation": operation, + "status": Int(status), + "description": description + ] + )) } - result(true) case "enabled": result(wakelockEnabled) default: @@ -30,19 +40,24 @@ public class WakelockPlusMacosPlugin: NSObject, FlutterPlugin { } } - func enableWakelock(reason: String = "Disabling display sleep") { - if !wakelockEnabled { - wakelockEnabled = IOPMAssertionCreateWithName( kIOPMAssertionTypeNoDisplaySleep as CFString, - IOPMAssertionLevel(kIOPMAssertionLevelOn), - reason as CFString, - &assertionID) == kIOReturnSuccess - } + func enableWakelock(reason: String = "Disabling display sleep") -> IOReturn { + guard !wakelockEnabled else { return kIOReturnSuccess } + let status = IOPMAssertionCreateWithName( + kIOPMAssertionTypeNoDisplaySleep as CFString, + IOPMAssertionLevel(kIOPMAssertionLevelOn), + reason as CFString, + &assertionID + ) + wakelockEnabled = status == kIOReturnSuccess + return status } - func disableWakelock() { - if wakelockEnabled { - IOPMAssertionRelease(assertionID) + func disableWakelock() -> IOReturn { + guard wakelockEnabled else { return kIOReturnSuccess } + let status = IOPMAssertionRelease(assertionID) + if status == kIOReturnSuccess { wakelockEnabled = false } + return status } }