import 'package:flutter/material.dart'; import '../../core/servers/machine_access.dart'; import '../../core/servers/server_catalog.dart'; import '../../ui/adaptive/segmented_bar.dart'; import '../onboarding/discovery/discovered_server.dart'; import '../onboarding/discovery/discovery_panel.dart'; import '../onboarding/discovery/server_discovery.dart'; import 'saved_server_sign_in_step.dart'; /// When supplied, [onSave] must finish successfully before the route closes. /// Throwing keeps the draft open and displays the error for a retry. With an /// [authenticator], a new machine (or one whose addresses changed) is then /// asked for its own password before the route closes. Future editSavedServer( BuildContext context, { SavedServer? server, ServerDiscovery? discovery, Future Function(SavedServer)? onSave, MachineAuthenticator? authenticator, }) { final page = _ServerEditor( server: server, onSave: onSave, authenticator: authenticator, discovery: discovery ?? platformServerDiscovery(), ); return Navigator.of(context).push( MaterialPageRoute(builder: (_) => page), ); } class _ServerEditor extends StatefulWidget { const _ServerEditor({ this.server, this.onSave, this.discovery, this.authenticator, }); final ServerDiscovery? discovery; final SavedServer? server; final Future Function(SavedServer)? onSave; final MachineAuthenticator? authenticator; @override State<_ServerEditor> createState() => _ServerEditorState(); } class _ServerEditorState extends State<_ServerEditor> { // Keep the identity through retries, including a persisted profile whose // connection activation failed after the write succeeded. late final _id = widget.server?.id ?? 'server-${DateTime.now().microsecondsSinceEpoch}'; late final _name = TextEditingController(text: widget.server?.name ?? ''); late final _addresses = TextEditingController( text: widget.server?.endpoints.join('\n') ?? '', ); String? _error; bool _saving = false; /// Set once the profile is persisted and the machine must now be asked. SavedServer? _saved; void _useDiscovered(DiscoveredServer server) { setState(() { _name.text = server.name; _addresses.text = server.endpoints.join('\n'); _error = null; }); } Future _save() async { if (_saving) return; try { final server = SavedServer.validated( id: _id, name: _name.text, legacyEndpoint: widget.server?.legacyEndpoint, endpoints: _addresses.text .split('\n') .where((line) => line.trim().isNotEmpty), ); setState(() { _saving = true; _error = null; }); await widget.onSave?.call(server); if (!mounted) return; final authenticator = widget.authenticator; final changed = widget.server == null || !_sameEndpoints(widget.server!.endpoints, server.endpoints); if (authenticator == null || !changed) { Navigator.of(context).pop(server); return; } setState(() => _saved = server); } catch (error) { if (mounted) { setState(() { _saving = false; _error = '$error'; }); } } } static bool _sameEndpoints(List a, List b) => a.length == b.length && a.indexed.every((e) => b[e.$1] == e.$2); @override void dispose() { _name.dispose(); _addresses.dispose(); super.dispose(); } Widget _field( TextEditingController controller, String label, { required String key, bool multiline = false, }) { return TextField( key: Key(key), controller: controller, enabled: !_saving, decoration: InputDecoration( labelText: label, border: const OutlineInputBorder(), ), minLines: multiline ? 3 : 1, maxLines: multiline ? null : 1, keyboardType: multiline ? TextInputType.multiline : TextInputType.text, autocorrect: !multiline, ); } @override Widget build(BuildContext context) { final title = widget.server == null ? 'Add server' : 'Edit server'; final saveLabel = _saving ? 'Saving server…' : 'Save server'; final save = FilledButton( onPressed: _saving ? null : _save, child: Text(saveLabel), ); final saved = _saved; if (saved != null && widget.authenticator != null) { final step = SafeArea( child: ListView( padding: const EdgeInsets.all(16), children: [ SavedServerSignInStep( server: saved, authenticator: widget.authenticator!, onDone: () => Navigator.of(context).pop(saved), ), ], ), ); return _scaffold('Sign in to ${saved.name}', step); } final body = SafeArea( child: ListView( padding: const EdgeInsets.all(16), children: [ if (widget.server == null && widget.discovery != null) ...[ DiscoveryPanel( discovery: widget.discovery!, enabled: !_saving, onSelect: _useDiscovered, ), const SizedBox(height: 16), ], _field(_name, 'Server name', key: 'server-profile-name'), const SizedBox(height: 16), _field( _addresses, 'Addresses of this machine', key: 'server-profile-addresses', multiline: true, ), const SizedBox(height: 8), const Text( 'One ws:// or wss:// address per line, all for this one machine: ' 'for example its LAN address, its Tailscale IP and its Tailscale ' 'name. The first is tried first; the others are backups. A ' 'different machine is a separate entry with its own password.', ), const SizedBox(height: 8), const Text( 'Tailscale HTTPS (wss://) works with the machine\'s tailnet name ' 'and the web port, not with its IP. ws:// is unencrypted; use it ' 'only on a trusted LAN or VPN.', ), if (_error != null) Padding( padding: const EdgeInsets.only(top: 12), child: Semantics( liveRegion: true, child: Text( _error!, style: TextStyle( color: Theme.of(context).colorScheme.error, ), ), ), ), const SizedBox(height: 16), Align(alignment: AlignmentDirectional.centerStart, child: save), ], ), ); return PopScope(canPop: !_saving, child: _scaffold(title, body)); } Widget _scaffold(String title, Widget body) => Scaffold( extendBodyBehindAppBar: true, appBar: SegmentedBar(cupertino: false, title: title), body: body, ); }