Merge upstream/dev (2 commits) [upstream-sync]
This commit is contained in:
commit
b984e1d9a3
60 files changed
+989
-1472
No files matched your search
@@ -0,0 +1,216 @@
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../connector/meshcore_connector.dart';
|
||||
import '../l10n/l10n.dart';
|
||||
import '../review_mode/review_mode_entry.dart';
|
||||
import '../theme/mesh_theme.dart';
|
||||
import '../utils/platform_info.dart';
|
||||
import '../widgets/adaptive_app_bar_title.dart';
|
||||
import '../widgets/get_node_banner.dart';
|
||||
import '../widgets/mesh_ui.dart';
|
||||
import 'scanner_screen.dart';
|
||||
import 'tcp_screen.dart';
|
||||
import 'usb_screen.dart';
|
||||
|
||||
/// Connects a radio over Bluetooth, USB or TCP, one tab each. Only the
|
||||
/// transports the platform offers get a tab: no USB on iOS, no TCP on web.
|
||||
class ConnectScreen extends StatefulWidget {
|
||||
/// The tab to open on; the first tab when the platform lacks it.
|
||||
final MeshCoreTransportType initialTransport;
|
||||
|
||||
const ConnectScreen({
|
||||
super.key,
|
||||
this.initialTransport = MeshCoreTransportType.bluetooth,
|
||||
});
|
||||
|
||||
@override
|
||||
State<ConnectScreen> createState() => _ConnectScreenState();
|
||||
}
|
||||
|
||||
class _ConnectScreenState extends State<ConnectScreen>
|
||||
with SingleTickerProviderStateMixin {
|
||||
late final List<MeshCoreTransportType> _transports;
|
||||
late final TabController _tabController;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_transports = [
|
||||
MeshCoreTransportType.bluetooth,
|
||||
if (PlatformInfo.supportsUsbSerial) MeshCoreTransportType.usb,
|
||||
if (!PlatformInfo.isWeb) MeshCoreTransportType.tcp,
|
||||
];
|
||||
_tabController = TabController(
|
||||
length: _transports.length,
|
||||
initialIndex: math.max(0, _transports.indexOf(widget.initialTransport)),
|
||||
vsync: this,
|
||||
)..addListener(_closeKeyboardLeavingTcp);
|
||||
}
|
||||
|
||||
/// A focused field keeps the TCP tab alive and the keyboard open.
|
||||
void _closeKeyboardLeavingTcp() {
|
||||
final focus = FocusManager.instance.primaryFocus;
|
||||
if (_transports[_tabController.previousIndex] ==
|
||||
MeshCoreTransportType.tcp &&
|
||||
focus?.context?.findAncestorWidgetOfExactType<EditableText>() != null) {
|
||||
focus!.unfocus();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_tabController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final l10n = context.l10n;
|
||||
final state = context.select<MeshCoreConnector, MeshCoreConnectionState>(
|
||||
(connector) => connector.state,
|
||||
);
|
||||
// On a short screen, an open keyboard leaves room only for the TCP form.
|
||||
final keyboard = MediaQuery.viewInsetsOf(context).bottom;
|
||||
final cramped =
|
||||
keyboard > 0 && MediaQuery.sizeOf(context).height - keyboard < 400;
|
||||
// Leaving a tab cancels its connection, so tabs stay put meanwhile.
|
||||
final locked =
|
||||
state == MeshCoreConnectionState.connecting ||
|
||||
state == MeshCoreConnectionState.disconnecting;
|
||||
final tabBar = TabBar(
|
||||
controller: _tabController,
|
||||
tabs: [
|
||||
for (final transport in _transports)
|
||||
Tab(
|
||||
icon: Icon(switch (transport) {
|
||||
MeshCoreTransportType.usb => Icons.usb,
|
||||
MeshCoreTransportType.tcp => Icons.lan,
|
||||
_ => Icons.bluetooth,
|
||||
}),
|
||||
child: FittedBox(
|
||||
fit: BoxFit.scaleDown,
|
||||
child: Text(switch (transport) {
|
||||
MeshCoreTransportType.usb => l10n.connectionChoiceUsbLabel,
|
||||
MeshCoreTransportType.tcp => l10n.connectionChoiceTcpLabel,
|
||||
_ => l10n.connectionChoiceBluetoothLabel,
|
||||
}),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: ReviewModeLongPress(
|
||||
child: AdaptiveAppBarTitle(l10n.connectSheet_title),
|
||||
),
|
||||
centerTitle: true,
|
||||
bottom: cramped
|
||||
? null
|
||||
: PreferredSize(
|
||||
preferredSize: tabBar.preferredSize,
|
||||
child: ExcludeFocus(
|
||||
excluding: locked,
|
||||
child: IgnorePointer(
|
||||
ignoring: locked,
|
||||
child: Opacity(opacity: locked ? 0.5 : 1, child: tabBar),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
body: SafeArea(
|
||||
top: false,
|
||||
child: Column(
|
||||
children: [
|
||||
if (!cramped)
|
||||
Consumer<MeshCoreConnector>(
|
||||
builder: (context, connector, _) =>
|
||||
_ConnectionStatusHeader(connector: connector),
|
||||
),
|
||||
Expanded(
|
||||
child: TabBarView(
|
||||
controller: _tabController,
|
||||
physics: locked ? const NeverScrollableScrollPhysics() : null,
|
||||
children: [
|
||||
// Its own messenger lets a tab's snackbar lift its FAB.
|
||||
for (final transport in _transports)
|
||||
ScaffoldMessenger(
|
||||
child: switch (transport) {
|
||||
MeshCoreTransportType.usb => const UsbScreen(),
|
||||
MeshCoreTransportType.tcp => const TcpScreen(),
|
||||
_ => const ScannerScreen(),
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
bottomNavigationBar: const GetNodeBanner(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Connection status header with AnimatedSwitcher between states.
|
||||
class _ConnectionStatusHeader extends StatelessWidget {
|
||||
final MeshCoreConnector connector;
|
||||
|
||||
const _ConnectionStatusHeader({required this.connector});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final l10n = context.l10n;
|
||||
final scheme = Theme.of(context).colorScheme;
|
||||
final connectedName = switch (connector.activeTransport) {
|
||||
MeshCoreTransportType.usb =>
|
||||
connector.activeUsbPortDisplayLabel ?? l10n.connectionChoiceUsbLabel,
|
||||
MeshCoreTransportType.tcp =>
|
||||
connector.activeTcpEndpoint ?? l10n.connectionChoiceTcpLabel,
|
||||
_ => connector.deviceDisplayName,
|
||||
};
|
||||
|
||||
final (String label, Color color, bool pulse) = switch (connector.state) {
|
||||
MeshCoreConnectionState.scanning => (
|
||||
l10n.scanner_scanning,
|
||||
MeshPalette.blue,
|
||||
true,
|
||||
),
|
||||
MeshCoreConnectionState.connecting => (
|
||||
l10n.scanner_connecting,
|
||||
MeshPalette.warn,
|
||||
true,
|
||||
),
|
||||
MeshCoreConnectionState.connected => (
|
||||
l10n.scanner_connectedTo(connectedName),
|
||||
MeshPalette.signal,
|
||||
false,
|
||||
),
|
||||
MeshCoreConnectionState.disconnecting => (
|
||||
l10n.scanner_disconnecting,
|
||||
MeshPalette.warn,
|
||||
true,
|
||||
),
|
||||
MeshCoreConnectionState.disconnected => (
|
||||
l10n.scanner_notConnected,
|
||||
scheme.onSurfaceVariant,
|
||||
false,
|
||||
),
|
||||
};
|
||||
|
||||
return Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 12, 16, 4),
|
||||
child: AnimatedSwitcher(
|
||||
duration: const Duration(milliseconds: 300),
|
||||
child: Align(
|
||||
key: ValueKey(connector.state),
|
||||
alignment: Alignment.centerLeft,
|
||||
child: StatusChip(label: label, color: color, pulse: pulse),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
+18
-108
@@ -8,20 +8,16 @@ import 'package:provider/provider.dart';
|
||||
import '../connector/meshcore_connector.dart';
|
||||
import '../l10n/l10n.dart';
|
||||
import '../services/linux_ble_error_classifier.dart';
|
||||
import '../theme/mesh_theme.dart';
|
||||
import '../utils/app_logger.dart';
|
||||
import '../widgets/adaptive_app_bar_title.dart';
|
||||
import '../widgets/device_tile.dart';
|
||||
import '../widgets/empty_state.dart';
|
||||
import '../widgets/mesh_ui.dart';
|
||||
import '../widgets/settings_reveal.dart';
|
||||
import '../helpers/snack_bar_builder.dart';
|
||||
import '../review_mode/review_mode_entry.dart';
|
||||
import '../utils/main_navigation.dart';
|
||||
import 'tcp_screen.dart';
|
||||
import 'usb_screen.dart';
|
||||
|
||||
/// Screen for scanning and connecting to MeshCore devices
|
||||
/// Bluetooth tab of the connect screen: scans for and connects to MeshCore
|
||||
/// devices.
|
||||
class ScannerScreen extends StatefulWidget {
|
||||
const ScannerScreen({super.key});
|
||||
|
||||
@@ -49,8 +45,7 @@ class _ScannerScreenState extends State<ScannerScreen> {
|
||||
if (_connector.state == MeshCoreConnectionState.disconnected) {
|
||||
_changedNavigation = false;
|
||||
} else if (_connector.state == MeshCoreConnectionState.connected &&
|
||||
// TCP lands here after an auto-reconnect: a dropped link popped
|
||||
// back to this screen, and the TCP screen is no longer on the stack.
|
||||
// A TCP auto-reconnect can finish while this tab is shown.
|
||||
(_connector.activeTransport == MeshCoreTransportType.bluetooth ||
|
||||
_connector.activeTransport == MeshCoreTransportType.tcp) &&
|
||||
isCurrentRoute &&
|
||||
@@ -96,55 +91,20 @@ class _ScannerScreenState extends State<ScannerScreen> {
|
||||
unawaited(_connector.disconnect(manual: true));
|
||||
});
|
||||
}
|
||||
// A scan would keep the other tabs from connecting until it times out.
|
||||
if (state == MeshCoreConnectionState.scanning) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (_connector.state == MeshCoreConnectionState.scanning) {
|
||||
unawaited(_connector.stopScan());
|
||||
}
|
||||
});
|
||||
}
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final canPop = Navigator.of(context).canPop();
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
leading: canPop
|
||||
? IconButton(
|
||||
icon: const Icon(Icons.arrow_back),
|
||||
onPressed: () {
|
||||
appLogger.info('Back button pressed', tag: 'ScannerScreen');
|
||||
Navigator.of(context).maybePop();
|
||||
},
|
||||
)
|
||||
: null,
|
||||
title: ReviewModeLongPress(
|
||||
child: AdaptiveAppBarTitle(context.l10n.scanner_title),
|
||||
),
|
||||
centerTitle: true,
|
||||
automaticallyImplyLeading: false,
|
||||
actions: [
|
||||
if (PlatformInfo.supportsUsbSerial)
|
||||
IconButton(
|
||||
icon: const Icon(Icons.usb),
|
||||
tooltip: context.l10n.connectionChoiceUsbLabel,
|
||||
onPressed: () {
|
||||
appLogger.info(
|
||||
'USB selected, opening UsbScreen',
|
||||
tag: 'ScannerScreen',
|
||||
);
|
||||
Navigator.of(
|
||||
context,
|
||||
).push(MaterialPageRoute(builder: (_) => const UsbScreen()));
|
||||
},
|
||||
),
|
||||
if (!PlatformInfo.isWeb)
|
||||
IconButton(
|
||||
icon: const Icon(Icons.lan),
|
||||
tooltip: context.l10n.connectionChoiceTcpLabel,
|
||||
onPressed: () {
|
||||
Navigator.of(
|
||||
context,
|
||||
).push(MaterialPageRoute(builder: (_) => const TcpScreen()));
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
body: SafeArea(
|
||||
top: false,
|
||||
child: Consumer<MeshCoreConnector>(
|
||||
@@ -160,9 +120,6 @@ class _ScannerScreenState extends State<ScannerScreen> {
|
||||
),
|
||||
),
|
||||
|
||||
// Connection status header
|
||||
_ConnectionStatusHeader(connector: connector),
|
||||
|
||||
// Device list
|
||||
Expanded(child: _buildDeviceList(context, connector)),
|
||||
],
|
||||
@@ -259,10 +216,16 @@ class _ScannerScreenState extends State<ScannerScreen> {
|
||||
itemBuilder: (context, index) {
|
||||
final result = connector.scanResults[index];
|
||||
final deviceId = result.device.remoteId.toString();
|
||||
final name = result.device.platformName.isNotEmpty
|
||||
? result.device.platformName
|
||||
: result.advertisementData.advName;
|
||||
return ListEntrance(
|
||||
index: index,
|
||||
child: DeviceTile(
|
||||
scanResult: result,
|
||||
name: name.isNotEmpty ? name : context.l10n.common_unknownDevice,
|
||||
id: deviceId,
|
||||
icon: Icons.router,
|
||||
rssi: result.rssi,
|
||||
isConnecting: isConnecting && _connectingDeviceId == deviceId,
|
||||
onTap: isConnecting
|
||||
? null
|
||||
@@ -463,56 +426,3 @@ class _BluetoothOffBanner extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Connection status header with AnimatedSwitcher between states.
|
||||
class _ConnectionStatusHeader extends StatelessWidget {
|
||||
final MeshCoreConnector connector;
|
||||
|
||||
const _ConnectionStatusHeader({required this.connector});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final l10n = context.l10n;
|
||||
final scheme = Theme.of(context).colorScheme;
|
||||
|
||||
final (String label, Color color, bool pulse) = switch (connector.state) {
|
||||
MeshCoreConnectionState.scanning => (
|
||||
l10n.scanner_scanning,
|
||||
MeshPalette.blue,
|
||||
true,
|
||||
),
|
||||
MeshCoreConnectionState.connecting => (
|
||||
l10n.scanner_connecting,
|
||||
MeshPalette.warn,
|
||||
true,
|
||||
),
|
||||
MeshCoreConnectionState.connected => (
|
||||
l10n.scanner_connectedTo(connector.deviceDisplayName),
|
||||
MeshPalette.signal,
|
||||
false,
|
||||
),
|
||||
MeshCoreConnectionState.disconnecting => (
|
||||
l10n.scanner_disconnecting,
|
||||
MeshPalette.warn,
|
||||
true,
|
||||
),
|
||||
MeshCoreConnectionState.disconnected => (
|
||||
l10n.scanner_notConnected,
|
||||
scheme.onSurfaceVariant,
|
||||
false,
|
||||
),
|
||||
};
|
||||
|
||||
return Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 12, 16, 4),
|
||||
child: AnimatedSwitcher(
|
||||
duration: const Duration(milliseconds: 300),
|
||||
child: Align(
|
||||
key: ValueKey(connector.state),
|
||||
alignment: Alignment.centerLeft,
|
||||
child: StatusChip(label: label, color: color, pulse: pulse),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -8,12 +8,9 @@ import '../connector/meshcore_connector.dart';
|
||||
import '../l10n/l10n.dart';
|
||||
import '../services/app_settings_service.dart';
|
||||
import '../theme/mesh_theme.dart';
|
||||
import '../utils/platform_info.dart';
|
||||
import '../widgets/adaptive_app_bar_title.dart';
|
||||
import '../widgets/mesh_ui.dart';
|
||||
import '../helpers/snack_bar_builder.dart';
|
||||
import '../utils/main_navigation.dart';
|
||||
import 'usb_screen.dart';
|
||||
|
||||
class TcpScreen extends StatefulWidget {
|
||||
const TcpScreen({super.key});
|
||||
@@ -28,6 +25,7 @@ class _TcpScreenState extends State<TcpScreen> {
|
||||
late final MeshCoreConnector _connector;
|
||||
late final VoidCallback _connectionListener;
|
||||
bool _navigatedToChannels = false;
|
||||
bool _startedConnect = false;
|
||||
String? _hostError;
|
||||
String? _portError;
|
||||
|
||||
@@ -74,7 +72,10 @@ class _TcpScreenState extends State<TcpScreen> {
|
||||
_hostController.dispose();
|
||||
_portController.dispose();
|
||||
_connector.removeListener(_connectionListener);
|
||||
if (!_navigatedToChannels &&
|
||||
// Only cancel a connection this tab started; an automatic reconnect is
|
||||
// left alone.
|
||||
if (_startedConnect &&
|
||||
!_navigatedToChannels &&
|
||||
_connector.activeTransport == MeshCoreTransportType.tcp &&
|
||||
_connector.state != MeshCoreConnectionState.disconnected) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
@@ -87,14 +88,6 @@ class _TcpScreenState extends State<TcpScreen> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
leading: IconButton(
|
||||
icon: const Icon(Icons.arrow_back),
|
||||
onPressed: () => Navigator.of(context).maybePop(),
|
||||
),
|
||||
title: AdaptiveAppBarTitle(context.l10n.tcpScreenTitle),
|
||||
centerTitle: true,
|
||||
),
|
||||
body: SafeArea(
|
||||
top: false,
|
||||
child: Consumer<MeshCoreConnector>(
|
||||
@@ -107,26 +100,9 @@ class _TcpScreenState extends State<TcpScreen> {
|
||||
final isButtonDisabled =
|
||||
connector.state != MeshCoreConnectionState.disconnected;
|
||||
return ListView(
|
||||
padding: const EdgeInsets.only(bottom: 32),
|
||||
padding: const EdgeInsets.only(top: 8, bottom: 32),
|
||||
children: [
|
||||
// Status header
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 12, 16, 4),
|
||||
child: AnimatedSwitcher(
|
||||
duration: const Duration(milliseconds: 300),
|
||||
child: Align(
|
||||
key: ValueKey(connector.state),
|
||||
alignment: Alignment.centerLeft,
|
||||
child: _buildStatusChip(context, connector),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
// Transport switcher
|
||||
_buildTransportLinks(context),
|
||||
|
||||
// Connection form
|
||||
SectionHeader(context.l10n.tcpSectionTitle),
|
||||
MeshCard(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
@@ -233,65 +209,6 @@ class _TcpScreenState extends State<TcpScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStatusChip(BuildContext context, MeshCoreConnector connector) {
|
||||
final l10n = context.l10n;
|
||||
|
||||
if (connector.isTcpTransportConnected) {
|
||||
return StatusChip(
|
||||
label: l10n.scanner_connectedTo(connector.activeTcpEndpoint ?? 'TCP'),
|
||||
color: MeshPalette.signal,
|
||||
);
|
||||
} else if (connector.state == MeshCoreConnectionState.connecting &&
|
||||
connector.activeTransport == MeshCoreTransportType.tcp) {
|
||||
return StatusChip(
|
||||
label: l10n.tcpStatus_connectingTo(
|
||||
'${_hostController.text}:${_portController.text}',
|
||||
),
|
||||
color: MeshPalette.warn,
|
||||
pulse: true,
|
||||
);
|
||||
} else if (connector.state == MeshCoreConnectionState.disconnecting &&
|
||||
connector.activeTransport == MeshCoreTransportType.tcp) {
|
||||
return StatusChip(
|
||||
label: l10n.scanner_disconnecting,
|
||||
color: MeshPalette.warn,
|
||||
pulse: true,
|
||||
);
|
||||
} else {
|
||||
return StatusChip(
|
||||
label: l10n.tcpStatus_notConnected,
|
||||
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Widget _buildTransportLinks(BuildContext context) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
child: Wrap(
|
||||
spacing: 12,
|
||||
runSpacing: 8,
|
||||
children: [
|
||||
if (PlatformInfo.supportsUsbSerial)
|
||||
OutlinedButton.icon(
|
||||
onPressed: () {
|
||||
Navigator.of(context).pushReplacement(
|
||||
MaterialPageRoute(builder: (_) => const UsbScreen()),
|
||||
);
|
||||
},
|
||||
icon: const Icon(Icons.usb),
|
||||
label: Text(context.l10n.connectionChoiceUsbLabel),
|
||||
),
|
||||
OutlinedButton.icon(
|
||||
onPressed: () => Navigator.of(context).maybePop(),
|
||||
icon: const Icon(Icons.bluetooth),
|
||||
label: Text(context.l10n.connectionChoiceBluetoothLabel),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _connectTcp() async {
|
||||
if (_connector.state == MeshCoreConnectionState.connecting ||
|
||||
_connector.state == MeshCoreConnectionState.connected ||
|
||||
@@ -313,6 +230,7 @@ class _TcpScreenState extends State<TcpScreen> {
|
||||
return;
|
||||
}
|
||||
|
||||
_startedConnect = true;
|
||||
try {
|
||||
await _connector.connectTcp(host: host, port: parsedPort);
|
||||
} catch (error) {
|
||||
|
||||
+39
-177
@@ -6,16 +6,14 @@ import 'package:provider/provider.dart';
|
||||
|
||||
import '../connector/meshcore_connector.dart';
|
||||
import '../l10n/l10n.dart';
|
||||
import '../theme/mesh_theme.dart';
|
||||
import '../utils/app_logger.dart';
|
||||
import '../utils/platform_info.dart';
|
||||
import '../utils/usb_port_labels.dart';
|
||||
import '../widgets/adaptive_app_bar_title.dart';
|
||||
import '../widgets/device_tile.dart';
|
||||
import '../widgets/empty_state.dart';
|
||||
import '../widgets/mesh_ui.dart';
|
||||
import '../helpers/snack_bar_builder.dart';
|
||||
import '../utils/main_navigation.dart';
|
||||
import 'tcp_screen.dart';
|
||||
|
||||
class UsbScreen extends StatefulWidget {
|
||||
const UsbScreen({super.key});
|
||||
@@ -27,6 +25,7 @@ class UsbScreen extends StatefulWidget {
|
||||
class _UsbScreenState extends State<UsbScreen> with WidgetsBindingObserver {
|
||||
final List<String> _ports = <String>[];
|
||||
bool _isLoadingPorts = true;
|
||||
String? _connectingPort;
|
||||
bool _navigatedToChannels = false;
|
||||
bool _didScheduleInitialLoad = false;
|
||||
Timer? _hotPlugTimer;
|
||||
@@ -36,6 +35,9 @@ class _UsbScreenState extends State<UsbScreen> with WidgetsBindingObserver {
|
||||
bool get _supportsHotPlug =>
|
||||
PlatformInfo.isWindows || PlatformInfo.isLinux || PlatformInfo.isMacOS;
|
||||
|
||||
/// Desktop hot-plugs automatically; web uses the port-picker button.
|
||||
bool get _showRefreshButton => !_supportsHotPlug && !PlatformInfo.isWeb;
|
||||
|
||||
/// Web Serial lists a synthetic "request a port" entry plus ports the user
|
||||
/// already authorised; the request entry opens the browser's port picker.
|
||||
static const String _webRequestPortName = 'web:request';
|
||||
@@ -109,141 +111,27 @@ class _UsbScreenState extends State<UsbScreen> with WidgetsBindingObserver {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
leading: IconButton(
|
||||
icon: const Icon(Icons.arrow_back),
|
||||
onPressed: () => Navigator.of(context).maybePop(),
|
||||
),
|
||||
title: AdaptiveAppBarTitle(context.l10n.usbScreenTitle),
|
||||
centerTitle: true,
|
||||
),
|
||||
body: SafeArea(
|
||||
top: false,
|
||||
child: Consumer<MeshCoreConnector>(
|
||||
builder: (context, connector, child) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
// Status header
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 12, 16, 4),
|
||||
child: AnimatedSwitcher(
|
||||
duration: const Duration(milliseconds: 300),
|
||||
child: Align(
|
||||
key: ValueKey('${connector.state}_$_isLoadingPorts'),
|
||||
alignment: Alignment.centerLeft,
|
||||
child: _buildStatusChip(context, connector),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
// Transport switcher
|
||||
_buildTransportLinks(context),
|
||||
|
||||
// Port list
|
||||
Expanded(child: _buildPortList(context, connector)),
|
||||
],
|
||||
);
|
||||
},
|
||||
builder: (context, connector, child) =>
|
||||
_buildPortList(context, connector),
|
||||
),
|
||||
),
|
||||
// Desktop hot-plugs automatically; web uses the port-picker button.
|
||||
bottomNavigationBar: _supportsHotPlug || PlatformInfo.isWeb
|
||||
? null
|
||||
: SafeArea(
|
||||
top: false,
|
||||
minimum: const EdgeInsets.fromLTRB(16, 8, 16, 16),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
FloatingActionButton.extended(
|
||||
onPressed: _isLoadingPorts ? null : _loadPorts,
|
||||
heroTag: 'usb_refresh_action',
|
||||
icon: _isLoadingPorts
|
||||
? const SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: const Icon(Icons.usb),
|
||||
label: Text(context.l10n.scanner_scan),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStatusChip(BuildContext context, MeshCoreConnector connector) {
|
||||
final l10n = context.l10n;
|
||||
final scheme = Theme.of(context).colorScheme;
|
||||
|
||||
if (_isLoadingPorts) {
|
||||
return StatusChip(
|
||||
label: l10n.usbStatus_searching,
|
||||
color: scheme.primary,
|
||||
pulse: true,
|
||||
);
|
||||
} else if (connector.isUsbTransportConnected) {
|
||||
switch (connector.state) {
|
||||
case MeshCoreConnectionState.connected:
|
||||
return StatusChip(
|
||||
label: l10n.scanner_connectedTo(
|
||||
connector.activeUsbPortDisplayLabel ?? 'USB',
|
||||
),
|
||||
color: MeshPalette.signal,
|
||||
);
|
||||
case MeshCoreConnectionState.disconnecting:
|
||||
return StatusChip(
|
||||
label: l10n.scanner_disconnecting,
|
||||
color: MeshPalette.warn,
|
||||
pulse: true,
|
||||
);
|
||||
default:
|
||||
return StatusChip(
|
||||
label: l10n.usbStatus_notConnected,
|
||||
color: scheme.onSurfaceVariant,
|
||||
);
|
||||
}
|
||||
} else if (connector.state == MeshCoreConnectionState.connecting &&
|
||||
connector.activeTransport == MeshCoreTransportType.usb) {
|
||||
return StatusChip(
|
||||
label: l10n.usbStatus_connecting,
|
||||
color: MeshPalette.warn,
|
||||
pulse: true,
|
||||
);
|
||||
} else {
|
||||
return StatusChip(
|
||||
label: l10n.usbStatus_notConnected,
|
||||
color: scheme.onSurfaceVariant,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Widget _buildTransportLinks(BuildContext context) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
child: Wrap(
|
||||
spacing: 12,
|
||||
runSpacing: 8,
|
||||
children: [
|
||||
if (!PlatformInfo.isWeb)
|
||||
OutlinedButton.icon(
|
||||
onPressed: () {
|
||||
Navigator.of(context).pushReplacement(
|
||||
MaterialPageRoute(builder: (_) => const TcpScreen()),
|
||||
);
|
||||
},
|
||||
icon: const Icon(Icons.lan),
|
||||
label: Text(context.l10n.connectionChoiceTcpLabel),
|
||||
),
|
||||
OutlinedButton.icon(
|
||||
onPressed: () => Navigator.of(context).maybePop(),
|
||||
icon: const Icon(Icons.bluetooth),
|
||||
label: Text(context.l10n.connectionChoiceBluetoothLabel),
|
||||
),
|
||||
],
|
||||
),
|
||||
floatingActionButton: _showRefreshButton
|
||||
? FloatingActionButton.extended(
|
||||
onPressed: _isLoadingPorts ? null : _loadPorts,
|
||||
heroTag: 'usb_refresh_action',
|
||||
icon: _isLoadingPorts
|
||||
? const SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: const Icon(Icons.refresh),
|
||||
label: Text(context.l10n.repeater_refresh),
|
||||
)
|
||||
: null,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -263,7 +151,12 @@ class _UsbScreenState extends State<UsbScreen> with WidgetsBindingObserver {
|
||||
}
|
||||
|
||||
if (_ports.isEmpty) {
|
||||
return EmptyState(icon: Icons.usb, title: l10n.usbScreenEmptyState);
|
||||
return EmptyState(
|
||||
icon: Icons.usb,
|
||||
title: _supportsHotPlug
|
||||
? l10n.usbScreenEmptyStateAuto
|
||||
: l10n.usbScreenEmptyState,
|
||||
);
|
||||
}
|
||||
|
||||
return _buildPortListView(context, _ports, isConnecting);
|
||||
@@ -296,7 +189,7 @@ class _UsbScreenState extends State<UsbScreen> with WidgetsBindingObserver {
|
||||
_connectPort(_webRequestPortName);
|
||||
}
|
||||
: null,
|
||||
icon: isConnecting
|
||||
icon: isConnecting && _connectingPort == _webRequestPortName
|
||||
? const SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
@@ -323,7 +216,7 @@ class _UsbScreenState extends State<UsbScreen> with WidgetsBindingObserver {
|
||||
bool isConnecting,
|
||||
) {
|
||||
return ListView.builder(
|
||||
padding: const EdgeInsets.only(bottom: 32),
|
||||
padding: EdgeInsets.only(top: 8, bottom: _showRefreshButton ? 88 : 32),
|
||||
itemCount: ports.length,
|
||||
itemBuilder: (context, index) {
|
||||
final port = ports[index];
|
||||
@@ -334,47 +227,13 @@ class _UsbScreenState extends State<UsbScreen> with WidgetsBindingObserver {
|
||||
|
||||
return ListEntrance(
|
||||
index: index,
|
||||
child: MeshCard(
|
||||
padding: EdgeInsets.zero,
|
||||
child: ListTile(
|
||||
onTap: isConnecting
|
||||
? null
|
||||
: () {
|
||||
HapticFeedback.selectionClick();
|
||||
_connectPort(port);
|
||||
},
|
||||
leading: AvatarCircle(
|
||||
name: displayName,
|
||||
size: 40,
|
||||
icon: Icons.usb,
|
||||
color: Theme.of(context).colorScheme.primary,
|
||||
),
|
||||
title: Text(
|
||||
displayName,
|
||||
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||||
fontWeight: FontWeight.w600,
|
||||
color: Theme.of(context).colorScheme.onSurface,
|
||||
),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
subtitle: showRawName
|
||||
? Text(
|
||||
rawName,
|
||||
style: MeshTheme.mono(
|
||||
fontSize: 13,
|
||||
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
||||
),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
)
|
||||
: null,
|
||||
trailing: Icon(
|
||||
Icons.chevron_right,
|
||||
size: 24,
|
||||
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
child: DeviceTile(
|
||||
name: displayName,
|
||||
id: showRawName ? rawName : null,
|
||||
icon: Icons.usb,
|
||||
color: Theme.of(context).colorScheme.primary,
|
||||
isConnecting: isConnecting && _connectingPort == port,
|
||||
onTap: isConnecting ? null : () => _connectPort(port),
|
||||
),
|
||||
);
|
||||
},
|
||||
@@ -448,6 +307,7 @@ class _UsbScreenState extends State<UsbScreen> with WidgetsBindingObserver {
|
||||
tag: 'UsbScreen',
|
||||
);
|
||||
|
||||
setState(() => _connectingPort = port);
|
||||
try {
|
||||
await _connector.connectUsb(portName: rawPortName);
|
||||
} catch (error, stackTrace) {
|
||||
@@ -458,6 +318,8 @@ class _UsbScreenState extends State<UsbScreen> with WidgetsBindingObserver {
|
||||
if (!mounted) return;
|
||||
_showError(error);
|
||||
unawaited(_loadPorts());
|
||||
} finally {
|
||||
if (mounted) setState(() => _connectingPort = null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user