onFocusChanged property

Stream<bool> get onFocusChanged

Returns a stream broadcasting the device's browser focus changes.

Implementation

static Stream<bool> get onFocusChanged {
  StreamController<bool>? controller;

  // Event listener reacting on window focus events.
  void focusListener(web.Event event) => controller!.add(true);

  // Event listener reacting on window unfocus events.
  void blurListener(web.Event event) => controller!.add(false);

  controller = StreamController(
    onListen: () {
      web.window.addEventListener('focus', focusListener.toJS);
      web.window.addEventListener('blur', blurListener.toJS);
    },
    onCancel: () {
      web.window.removeEventListener('focus', focusListener.toJS);
      web.window.removeEventListener('blur', blurListener.toJS);
    },
  );

  return controller.stream;
}