Something went wrong. Try again.
Rewild Your Web forked from me.webbeef.org/browser.html
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990// SPDX-License-Identifier: AGPL-3.0-or-later
// Blanks the screen after a period without user interaction, and brings it back// on the next interaction.
import { screenPower } from "beaver://shared/power/screen.js";import { cancelDusk, dusk, restoreBrightness,} from "beaver://shared/power/dusk.js";
// The lowest threshold a beaver:// page is allowed to ask for.const MINIMUM_MS = 10_000;
const PREF = "beaver.screen_timeout_ms";
let installed = false;
// Milliseconds of inactivity before blanking, or 0 when the feature is off.function configuredTimeout() { let value = 0; try { value = Number(navigator.servo?.getIntPreference(PREF) ?? 0); } catch { // The pref is missing: treat that as "off". return 0; } if (!Number.isFinite(value) || value <= 0) { console.warn(`[screen-timeout] invalid value for ${PREF} : ${value}`); return 0; } if (value < MINIMUM_MS) { console.warn( `[screen-timeout] ${PREF}=${value} is below the ${MINIMUM_MS}ms minimum, using ${MINIMUM_MS}`, ); return MINIMUM_MS; } return value;}
/** * Start watching for inactivity. Safe to call more than once; does nothing when * the pref is unset, the API is missing, or there is no screen to blank. */export async function bindScreenTimeout() { if (installed) { return; }
// Unset or zero means the feature is off, which is the default: not // something to warn about. const threshold = configuredTimeout(); if (!threshold) { return; } if (typeof globalThis.IdleDetector !== "function") { console.warn("[screen-timeout] IdleDetector is unavailable"); return; } if (!screenPower.available) { return; }
installed = true;
const detector = new IdleDetector(); detector.addEventListener("change", async () => { // Only `userState` matters. if (detector.userState === "idle") { // Fade first. If the user comes back mid-fade the `active` branch cancels // it, `dusk()` resolves false, and the panel is left alone. if (await dusk()) { screenPower.off(); restoreBrightness(); } } else if (detector.userState === "active") { cancelDusk(); } });
try { await detector.start({ threshold }); } catch (error) { // Should never happen... installed = false; console.warn("[screen-timeout] could not start idle detection:", error); }}