openPopupCall static method

bool openPopupCall(
  1. ChatId chatId, {
  2. bool withAudio = true,
  3. bool withVideo = false,
  4. bool withScreen = false,
})

Opens a new popup window at the Routes.call page with the provided chatId.

Implementation

static bool openPopupCall(
  ChatId chatId, {
  bool withAudio = true,
  bool withVideo = false,
  bool withScreen = false,
}) {
  Log.debug(
    'openPopupCall($chatId, withAudio: $withAudio, withVideo: $withVideo, withScreen: $withScreen)',
    'WebUtils',
  );

  final int screenW = web.window.screen.width;
  final int screenH = web.window.screen.height;

  final Rect? prefs = getCallRect(chatId);

  final width = min(prefs?.width ?? 500, screenW);
  final height = min(prefs?.height ?? 500, screenH);

  var left = prefs?.left ?? screenW - 50 - width;
  if (left < 0) {
    left = 0;
  } else if (left + width > screenW) {
    left = screenW - width;
  }

  var top = prefs?.top ?? 50;
  if (top < 0) {
    top = 0;
  } else if (top + height > screenH) {
    top = screenH.toDouble() - height;
  }

  final List<String> parameters = [
    if (withAudio != true) 'audio=$withAudio',
    if (withVideo != false) 'video=$withVideo',
    if (withScreen != false) 'screen=$withScreen',
  ];

  final String query = parameters.isEmpty ? '' : '?${parameters.join('&')}';

  final web.Window? window = web.window.open(
    '${Routes.call}/$chatId$query',
    'call_${const Uuid().v4()}',
    'popup=1,width=$width,height=$height,left=$left,top=$top',
  );

  try {
    final bool opened = window?.closed == false;
    Log.debug('openPopupCall($chatId) -> $opened, $window', 'WebUtils');
    return opened;
  } catch (e) {
    Log.debug('openPopupCall($chatId) -> failed due to $e', 'WebUtils');
    return false;
  }
}