Class WakeLock
Example:
WakeLock.request();
Signal.effect(this, () -> {
statusLabel.setText(
WakeLock.activeSignal().get() ? "Screen will stay on" : "Idle");
});
Lifecycle. request() fires the browser request asynchronously
and activeSignal() flips to true once the browser confirms.
The browser releases the lock automatically when the tab is hidden; the
client transparently re-acquires it when the tab becomes visible again, so a
single request() is enough for the lifetime of the view. Call
release() to stop re-acquiring and drop the current lock.
Reliability caveats.
- Safari only ships the Screen Wake Lock API from version 16.4.
- The browser requires a secure context (HTTPS or
localhost); on an insecure origin the call fails silently andactiveSignal()staysfalse. UseavailabilitySignal()to gate UI controls on this up front. - Some browsers release the lock on low battery or when the device enters
power-saving mode;
activeSignal()reflects this immediately.
- Since:
- 25.2
-
Method Summary
Modifier and TypeMethodDescriptionReturns a read-only signal that reflects whether the browser is currently holding the wake lock on behalf of the current UI.activeSignal(UI ui) Returns the read-only active-state signal for the given UI.static Signal<WakeLockAvailability> Returns a read-only signal hinting at whether the Screen Wake Lock API is usable in the current UI's page context.static Signal<WakeLockAvailability> availabilitySignal(UI ui) Returns the availability hint signal for the given UI.static voidrelease()Releases the screen wake lock on the current UI and stops re-acquiring it on subsequent visibility changes.static voidReleases the screen wake lock on the given UI and stops re-acquiring it on subsequent visibility changes.static voidrequest()Asks the browser to acquire a screen wake lock for the current UI and keep re-acquiring it across tab visibility changes.static voidAsks the browser to acquire a screen wake lock for the given UI and keep re-acquiring it across tab visibility changes.static voidrequest(SerializableConsumer<WakeLockError> onError) Asks the browser to acquire a screen wake lock for the current UI, with an error callback for persistent failures.static voidrequest(SerializableConsumer<WakeLockError> onError, UI ui) Same asrequest(SerializableConsumer)on the given UI.
-
Method Details
-
request
public static void request()Asks the browser to acquire a screen wake lock for the current UI and keep re-acquiring it across tab visibility changes.The call is asynchronous and fire-and-forget: by the time this method returns the browser has not necessarily granted the lock yet. Observe
activeSignal()to react to the actual state. Callingrequest()when a lock is already held is a no-op.- Throws:
IllegalStateException- if there is no current UI
-
request
Asks the browser to acquire a screen wake lock for the given UI and keep re-acquiring it across tab visibility changes. Use this overload from background threads or anywhereUI.getCurrent()is unreliable.- Parameters:
ui- the UI to dispatch the request through, nevernull- Throws:
NullPointerException- ifuiisnull
-
request
Asks the browser to acquire a screen wake lock for the current UI, with an error callback for persistent failures. Same lifecycle semantics asrequest(), but if the browser permanently refuses the request (feature unavailable, insecure context,NotAllowedError) the consumer is invoked on the UI thread with aWakeLockErrordescribing the cause.The error callback fires only for persistent failures the application may want to surface ? typically by re-enabling a "Keep screen on" toggle and showing a message. It is not called when the lock is simply deferred (page hidden until the user returns to the tab) or when the browser temporarily releases the lock for power-management reasons; for those, observe
activeSignal().- Parameters:
onError- consumer invoked on the UI thread when the request fails, nevernull- Throws:
NullPointerException- ifonErrorisnullIllegalStateException- if there is no current UI
-
request
Same asrequest(SerializableConsumer)on the given UI. Use this overload from background threads or anywhereUI.getCurrent()is unreliable.- Parameters:
onError- consumer invoked on the UI thread when the request fails, nevernullui- the UI to dispatch the request through, nevernull- Throws:
NullPointerException- if either argument isnull
-
release
public static void release()Releases the screen wake lock on the current UI and stops re-acquiring it on subsequent visibility changes. Idempotent ? calling it when no lock is held is a no-op.- Throws:
IllegalStateException- if there is no current UI
-
release
Releases the screen wake lock on the given UI and stops re-acquiring it on subsequent visibility changes. Use this overload from background threads or anywhereUI.getCurrent()is unreliable.- Parameters:
ui- the UI to dispatch the release through, nevernull- Throws:
NullPointerException- ifuiisnull
-
activeSignal
Returns a read-only signal that reflects whether the browser is currently holding the wake lock on behalf of the current UI.Starts as
false, flips totruewhen the browser confirms a request, and flips back tofalsewhenever the browser releases the lock ? explicitly throughrelease(), automatically when the tab becomes hidden, or because the browser dropped it (power saving, low battery). When the tab is shown again the client re-requests the lock ifrelease()has not been called, so the signal flips back totrueshortly after.Use
Signal.effect(owner, ...)to react to changes and.peek()for a snapshot outside a reactive context.- Returns:
- the read-only active-state signal
- Throws:
IllegalStateException- if there is no current UI
-
activeSignal
Returns the read-only active-state signal for the given UI. Same semantics asactiveSignal(); use this overload from background threads or anywhereUI.getCurrent()is unreliable.- Parameters:
ui- the UI to read the signal from, nevernull- Returns:
- the read-only active-state signal
- Throws:
NullPointerException- ifuiisnull
-
availabilitySignal
Returns a read-only signal hinting at whether the Screen Wake Lock API is usable in the current UI's page context. Useful for hiding a "keep screen on" toggle entirely on insecure origins or browsers that do not implement the API.The value is seeded during bootstrap; the brief window between UI attach and bootstrap completion surfaces as
WakeLockAvailability.UNKNOWN.- Returns:
- the availability hint signal
- Throws:
IllegalStateException- if there is no current UI
-
availabilitySignal
Returns the availability hint signal for the given UI. Same semantics asavailabilitySignal(); use this overload from background threads or anywhereUI.getCurrent()is unreliable.- Parameters:
ui- the UI to read the signal from, nevernull- Returns:
- the availability hint signal
- Throws:
NullPointerException- ifuiisnull
-