LensStudio:App.AppState
EnumEnum in the Editor Scripting API.
Index
Example
import * as app from 'LensStudio:App';
import { CoreService, Descriptor } from 'LensStudio:CoreService';
const STATE_NAMES = ['Suspended', 'Hidden', 'Inactive', 'Active'] as const;
function stateName(state: app.AppState): string {
return STATE_NAMES.find((name) => app.AppState[name] === state) ?? 'Unknown';
}
// Pauses periodic background work while Lens Studio is not in front and resumes it when the user returns.
export class AppStateAwareService extends CoreService {
private connection: Editor.ScopedConnection | null;
private timer: ReturnType<typeof setInterval> | null;
static descriptor(): Descriptor {
const d = new Descriptor();
d.id = 'com.example.AppStateAwareService';
d.name = 'App State Aware Service';
d.description = 'Runs background work only while Lens Studio is active';
return d;
}
constructor(pluginSystem: Editor.PluginSystem) {
super(pluginSystem);
this.connection = null;
this.timer = null;
}
start(): void {
this.applyState(app.state());
this.connection = app.onStateChanged.connect((state: app.AppState) => this.applyState(state));
}
stop(): void {
this.connection?.disconnect();
this.connection = null;
this.stopWork();
}
private applyState(state: app.AppState): void {
console.log(`Lens Studio is now ${stateName(state)}`);
if (state === app.AppState.Active) {
this.startWork();
} else {
this.stopWork();
}
}
private startWork(): void {
if (this.timer === null) {
this.timer = setInterval(() => console.log('Background work tick'), 5000);
}
}
private stopWork(): void {
if (this.timer !== null) {
clearInterval(this.timer);
this.timer = null;
}
}
}
Enumeration Members
Active
Active:
number
A Lens Studio window, such as the main window, a floating panel, or the preview, is visible and in front.
Hidden
Hidden:
number
Lens Studio is running in the background and none of its windows are visible.
Inactive
Inactive:
number
Lens Studio is visible but not in front, for example after the user switched to another application.
Suspended
Suspended:
number
Lens Studio is about to be suspended; save state and stop activity, because execution may stop without further warning.