Files
pipelines/app/lib/features/settings/saved_server_row.dart
T

135 lines
4.5 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.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 }
/// The key of a saved server's actions menu button.
ValueKey<String> savedServerMenuKey(String id) =>
ValueKey<String>('settings.server.$id.menu');
/// One saved server: a selection mark, the name, "Selected server" on the
/// active one and each address (preferred, then backups) as subtitle lines.
/// Tapping another server uses it; the trailing menu offers 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.allowSelection = true,
this.primaryContext = false,
});
final bool allowSelection, primaryContext;
final SavedServer server;
final bool selected;
final bool busy;
final ValueChanged<SavedServerAction> 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}',
};
List<SavedServerAction> get _actions => <SavedServerAction>[
if (allowSelection && !selected) SavedServerAction.use,
SavedServerAction.edit,
SavedServerAction.remove,
];
Future<void> _cupertinoMenu(BuildContext context) async {
final action = await showCupertinoModalPopup<SavedServerAction>(
context: context,
builder: (context) => CupertinoActionSheet(
title: Text(server.name),
actions: <Widget>[
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<SavedServerAction>(
key: savedServerMenuKey(server.id),
enabled: !busy,
tooltip: '${server.name} actions',
onSelected: onAction,
itemBuilder: (context) => <PopupMenuEntry<SavedServerAction>>[
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,
);
return SettingsRow(
leading: mark,
title: Text(server.name),
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
if (selected)
Text(
primaryContext ? 'Pipelines and runs server' : 'Selected server',
),
for (final (index, address) in server.endpoints.indexed)
Text('${index == 0 ? 'Preferred' : 'Backup $index'}: $address'),
],
),
trailing: menu,
onTap: !allowSelection || selected || busy
? null
: () => onAction(SavedServerAction.use),
);
}
}