close method

Future<void> close()

Implementation

Future<void> close() async {
  // Pinned: a kiosk window closes only through kiosk.exit satisfying the
  // policy's exit method, or with the application. A user close is
  // refused and said so at debug, as the spec has it.
  final DVWindowKioskHandle? pinned = kiosk;
  if (pinned != null && !pinned.exited) {
    _code('DV-WINDOW-012', 'close refused on a pinned kiosk window');
    return;
  }
  _lifecycle.value = DVWindowLifecycle.closing;

  // Owned windows go first, and in reverse open order: the last opened is
  // closest to the user, so a palette disappearing before the dialog sitting
  // on top of it would flash the wrong thing. An owned window cannot outlive
  // its owner.
  final List<DVWindow> ownedWindows = DVWindowManager.ownedBy(this);
  if (ownedWindows.isNotEmpty) {
    final int started = DVWindowManager.performance.mark();
    for (final DVWindow owned in ownedWindows.reversed) {
      await owned.close();
    }
    DVWindowManager.performance.recordOwnedCloseFrom(
      started,
      owner: route.path,
      owned: ownedWindows.length,
    );
  }
  // A space ends with the window that owns it: the camera and tracking stop
  // before the window is gone, never after.
  await spatial?.close();
  // A browser window closes through the handle the page opened it with: a
  // browser lets a page close only a window that page opened, and the
  // `window.close` binding it would otherwise ask for does not exist there.
  // False when this is not one, so every other target is unaffected.
  if (!isVirtual && !dvCloseBrowserWindow(nativeId)) {
    await DVNativeBridge.invoke<bool>(
      'window.close',
      <String, Object?>{'id': nativeId},
    );
  }
  _lifecycle.value = DVWindowLifecycle.closed;
  DVWindowManager.forget(this);
}