IdleDetector
Know when the user walks away - the IdleDetector API reports user activity (active/idle) and screen lock state, ideal for pausing work, muting notifications or signing out.
@inject Bit.Butil.IdleDetector idleDetectorMDN reference
IsSupported.
idle-detection permission.
Call RequestPermission from a user-gesture handler (like the buttons below) -
the browser rejects prompts that don't originate from user interaction.
Returns true when the runtime exposes IdleDetector. During prerender/SSR the check returns false rather than throwing, so defer it to OnAfterRenderAsync.
@inject Bit.Butil.IdleDetector idleDetector
var supported = await idleDetector.IsSupported();Asks the browser for the idle-detection permission and returns the resulting PermissionState (Granted, Denied, Prompt or Unknown). Starting a detector without a granted permission fails.
var state = await idleDetector.RequestPermission();
if (state is PermissionState.Granted)
{
// safe to call Start
}Starts watching for idle changes with the given threshold in seconds (the spec minimum is 60 - smaller values are clamped). The handler receives an IdleState with UserState (active/idle) and ScreenState (locked/unlocked) on every change. Dispose the returned ButilSubscription to stop.
private ButilSubscription? _sub;
_sub = await idleDetector.Start(60, state =>
{
// state.UserState: "active" | "idle"
// state.ScreenState: "unlocked" | "locked"
});
// stop watching:
await _sub.DisposeAsync();API reference
ValueTask<bool> IsSupported()ValueTask<PermissionState> RequestPermission()Task<ButilSubscription> Start(int threshold, Action<IdleState> handler)ValueTask DisposeAsync()void InvokeIdleDetector(Guid id, IdleState state)string UserState { get; set; }string ScreenState { get; set; }