Class WakeLock
- All Implemented Interfaces:
Serializable
Reach the per-UI instance through Page.getWakeLock().
Example:
WakeLock wakeLock = UI.getCurrent().getPage().getWakeLock();
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. - Some browsers release the lock on low battery or when the device enters
power-saving mode;
activeSignal()reflects this immediately.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionReturns a read-only signal that reflects whether the browser is currently holding the wake lock on behalf of this UI.voidrelease()Releases the screen wake lock and stops re-acquiring it on subsequent visibility changes.voidrequest()Asks the browser to acquire a screen wake lock and keep re-acquiring it across tab visibility changes.
-
Method Details
-
request
public void request()Asks the browser to acquire a screen wake lock 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. -
release
public void release()Releases the screen wake lock and stops re-acquiring it on subsequent visibility changes. Idempotent — calling it when no lock is held is a no-op. -
activeSignal
Returns a read-only signal that reflects whether the browser is currently holding the wake lock on behalf of this 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
-