Servers: name a machine after its hostname once it connects
A saved server still named by the app ('My server', 'This server',
'Original legacy server' or its address host/IP) takes the hostname the
machine reports in get_status the first time the app connects; typed
names are kept. New servers (legacy migration, Settings with a blank
name, onboarding) start with the address host. A browser on its page
origin with no saved profile shows the reported hostname too. No server
change: get_status already reports the hostname.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
|
||||
|
||||
import '../../core/servers/machine_access.dart';
|
||||
import '../../core/servers/server_catalog.dart';
|
||||
import '../../core/servers/server_naming.dart';
|
||||
import '../../ui/adaptive/segmented_bar.dart';
|
||||
import '../onboarding/discovery/discovered_server.dart';
|
||||
import '../onboarding/discovery/discovery_panel.dart';
|
||||
@@ -72,13 +73,19 @@ class _ServerEditorState extends State<_ServerEditor> {
|
||||
Future<void> _save() async {
|
||||
if (_saving) return;
|
||||
try {
|
||||
final endpoints = _addresses.text
|
||||
.split('\n')
|
||||
.where((line) => line.trim().isNotEmpty)
|
||||
.toList();
|
||||
// A blank name is the address's host until the machine reports its
|
||||
// hostname, which then names it.
|
||||
final server = SavedServer.validated(
|
||||
id: _id,
|
||||
name: _name.text,
|
||||
name: _name.text.trim().isEmpty && endpoints.isNotEmpty
|
||||
? temporaryServerName(endpoints.first)
|
||||
: _name.text,
|
||||
legacyEndpoint: widget.server?.legacyEndpoint,
|
||||
endpoints: _addresses.text
|
||||
.split('\n')
|
||||
.where((line) => line.trim().isNotEmpty),
|
||||
endpoints: endpoints,
|
||||
);
|
||||
setState(() {
|
||||
_saving = true;
|
||||
@@ -173,6 +180,10 @@ class _ServerEditorState extends State<_ServerEditor> {
|
||||
const SizedBox(height: 16),
|
||||
],
|
||||
_field(_name, 'Server name', key: 'server-profile-name'),
|
||||
const SizedBox(height: 8),
|
||||
const Text(
|
||||
'Leave blank to use the machine\'s own name once it connects.',
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
_field(
|
||||
_addresses,
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../../core/servers/machine_access.dart';
|
||||
import '../../core/servers/server_catalog.dart';
|
||||
import '../../core/servers/server_catalog_store.dart';
|
||||
import '../../core/servers/server_naming.dart';
|
||||
|
||||
/// Saved configurations are committed before switching the app's connection.
|
||||
/// A failed save leaves the old selection and its live socket unchanged.
|
||||
@@ -79,6 +82,47 @@ class ServerConnections extends ChangeNotifier {
|
||||
bool get busy => _busy;
|
||||
String? get error => _error;
|
||||
|
||||
String? _originHostname;
|
||||
|
||||
/// The hostname of the page-origin server a browser uses with no saved
|
||||
/// profile for it; it names that server, which has no catalog entry.
|
||||
String? get originHostname => _originHostname;
|
||||
|
||||
final _learned = <String, String>{};
|
||||
|
||||
/// Records the [hostname] the machine [id] reported. A saved machine still
|
||||
/// named by the app (see [hasPlaceholderName]) is renamed and saved; a name
|
||||
/// the operator typed is kept. A null [id] is the unsaved page origin.
|
||||
/// A change already in progress defers the rename until it finishes.
|
||||
Future<bool> learnHostname(String? id, String hostname) async {
|
||||
final name = hostname.trim();
|
||||
if (_disposed || name.isEmpty) return false;
|
||||
if (id == null) {
|
||||
if (_originHostname == name) return false;
|
||||
_originHostname = name;
|
||||
_notify();
|
||||
return true;
|
||||
}
|
||||
_learned[id] = name;
|
||||
return _applyLearnedNames();
|
||||
}
|
||||
|
||||
Future<bool> _applyLearnedNames() async {
|
||||
if (_busy || _disposed || _learned.isEmpty) return false;
|
||||
final learned = Map.of(_learned);
|
||||
_learned.clear();
|
||||
var next = _catalog;
|
||||
for (final server in _catalog.servers) {
|
||||
final hostname = learned[server.id];
|
||||
final renamed = hostname == null
|
||||
? null
|
||||
: renamedToHostname(server, hostname);
|
||||
if (renamed != null) next = next.upsert(renamed);
|
||||
}
|
||||
if (identical(next, _catalog)) return false;
|
||||
return _change(() => next, activate: false);
|
||||
}
|
||||
|
||||
/// Browsers always connect to their page origin, even after Back or a failed
|
||||
/// cross-origin navigation. Do not label another saved profile as active.
|
||||
void alignBrowserOrigin(Uri endpoint) {
|
||||
@@ -181,6 +225,7 @@ class ServerConnections extends ChangeNotifier {
|
||||
} finally {
|
||||
_busy = false;
|
||||
_notify();
|
||||
if (_learned.isNotEmpty) scheduleMicrotask(_applyLearnedNames);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user