import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../../core/servers/machine_access.dart'; import '../../core/servers/server_catalog.dart'; import 'settings_row.dart'; import 'settings_theme.dart'; /// What a saved server's menu can do. enum SavedServerAction { use, edit, remove, signIn } /// The key of a saved server's actions menu button. ValueKey savedServerMenuKey(String id) => ValueKey('settings.server.$id.menu'); /// One saved machine: a selection mark, the name, its live state (connected, /// sign-in required, unreachable), "Pipelines and runs server" on the primary /// and each address as a subtitle line. The trailing menu offers Sign in when /// the machine wants its own password, plus Use, Edit and Remove. class SavedServerRow extends StatelessWidget { const SavedServerRow({ super.key, required this.server, required this.selected, required this.busy, required this.onAction, this.status, this.allowSelection = true, this.primaryContext = false, }); final bool allowSelection, primaryContext; final SavedServer server; final bool selected; final bool busy; final MachineAccessResult? status; final ValueChanged onAction; String _label(SavedServerAction action) => switch (action) { SavedServerAction.use => primaryContext ? 'Use ${server.name} for pipelines and runs' : 'Use ${server.name}', SavedServerAction.edit => 'Edit ${server.name}', SavedServerAction.remove => 'Remove ${server.name}', SavedServerAction.signIn => 'Sign in to ${server.name}', }; List get _actions => [ if (status?.needsPassword ?? false) SavedServerAction.signIn, if (allowSelection && !selected) SavedServerAction.use, SavedServerAction.edit, SavedServerAction.remove, ]; /// The state line: a tap on it signs in when that is what is missing. String? get _stateText => switch (status?.access) { null => null, MachineAccess.connected => 'Connected', MachineAccess.needsPassword => 'Sign in required', MachineAccess.unreachable => status?.detail == null || status!.detail!.isEmpty ? 'Unreachable' : 'Unreachable: ${status!.detail}', }; Future _cupertinoMenu(BuildContext context) async { final action = await showCupertinoModalPopup( context: context, builder: (context) => CupertinoActionSheet( title: Text(server.name), actions: [ for (final action in _actions) CupertinoActionSheetAction( isDestructiveAction: action == SavedServerAction.remove, onPressed: () => Navigator.pop(context, action), child: Text(_label(action)), ), ], cancelButton: CupertinoActionSheetAction( onPressed: () => Navigator.pop(context), child: const Text('Cancel'), ), ), ); if (action != null) onAction(action); } @override Widget build(BuildContext context) { final cupertino = usesCupertinoControls(context); final Widget menu = cupertino ? CupertinoButton( key: savedServerMenuKey(server.id), padding: EdgeInsets.zero, onPressed: busy ? null : () => _cupertinoMenu(context), child: Icon( CupertinoIcons.ellipsis_circle, semanticLabel: '${server.name} actions', ), ) : PopupMenuButton( key: savedServerMenuKey(server.id), enabled: !busy, tooltip: '${server.name} actions', onSelected: onAction, itemBuilder: (context) => >[ for (final action in _actions) PopupMenuItem(value: action, child: Text(_label(action))), ], ); final Widget mark = !allowSelection ? Icon(cupertino ? CupertinoIcons.desktopcomputer : Icons.computer) : selected ? Icon( cupertino ? CupertinoIcons.checkmark_circle_fill : Icons.check_circle, semanticLabel: 'Selected', color: cupertino ? CupertinoTheme.of(context).primaryColor : Theme.of(context).colorScheme.primary, ) : Icon( cupertino ? CupertinoIcons.circle : Icons.radio_button_unchecked, color: cupertino ? CupertinoColors.tertiaryLabel.resolveFrom(context) : null, ); final needsPassword = status?.needsPassword ?? false; final state = _stateText; return SettingsRow( leading: mark, title: Text(server.name), subtitle: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ if (state != null) Text( state, key: Key('settings.server.${server.id}.state'), style: status?.access == MachineAccess.unreachable ? TextStyle(color: settingsErrorColor(context)) : null, ), if (selected) Text( primaryContext ? 'Pipelines and runs server' : 'Selected server', ), for (final (index, address) in server.endpoints.indexed) Text('${index == 0 ? 'Address' : 'Backup address'}: $address'), ], ), trailing: menu, onTap: busy ? null : needsPassword ? () => onAction(SavedServerAction.signIn) : !allowSelection || selected ? null : () => onAction(SavedServerAction.use), ); } }