import 'package:flutter/material.dart'; import 'package:flutter_hbb/common.dart'; import 'package:flutter_hbb/models/platform_model.dart'; const _kWindowsSystemSound = 'System Sound'; typedef AudioINputSetDevice = void Function(String device); typedef AudioInputBuilder = Widget Function( List devices, String currentDevice, AudioINputSetDevice setDevice); class AudioInput extends StatelessWidget { final AudioInputBuilder builder; final bool isCm; final bool isVoiceCall; const AudioInput( {Key? key, required this.builder, required this.isCm, required this.isVoiceCall}) : super(key: key); static String getDefault() { if (isWindows) return translate('System Sound'); return ''; } static Future getAudioInput(bool isCm, bool isVoiceCall) { if (isVoiceCall) { return bind.getVoiceCallInputDevice(isCm: isCm); } else { return bind.mainGetOption(key: 'audio-input'); } } static Future getValue(bool isCm, bool isVoiceCall) async { String device = await getAudioInput(isCm, isVoiceCall); if (device.isNotEmpty) { return device; } else { return getDefault(); } } static Future setDevice( String device, bool isCm, bool isVoiceCall) async { if (device == getDefault()) device = ''; if (isVoiceCall) { await bind.setVoiceCallInputDevice(isCm: isCm, device: device); } else { await bind.mainSetOption(key: 'audio-input', value: device); } } static Future> getDevicesInfo( bool isCm, bool isVoiceCall) async { List devices = (await bind.mainGetSoundInputs()).toList(); if (isWindows) { devices.insert(0, translate(_kWindowsSystemSound)); } String current = await getValue(isCm, isVoiceCall); return {'devices': devices, 'current': current}; } @override Widget build(BuildContext context) { return futureBuilder( future: getDevicesInfo(isCm, isVoiceCall), hasData: (data) { String currentDevice = data['current']; List devices = data['devices'] as List; if (devices.isEmpty) { return const Offstage(); } return builder(devices, currentDevice, (devices) { setDevice(devices, isCm, isVoiceCall); }); }, ); } }