From 821479742d2ad419b32ac1503e76f6ec353c7ccf Mon Sep 17 00:00:00 2001 From: zjs81 Date: Sat, 31 Oct 2026 12:09:42 -0700 Subject: [PATCH] Fix repeater settings grey box, nameless discovered nodes and chip contrast Repeater settings: - The Advanced and Identity Keys cards store their expanded flag in PageStorage. Their TextFields' inner Scrollables shared that identifier, read the bool as a scroll offset and failed to build, which showed as a large grey box in release builds. Give each card's body its own storage scope. Regression test added. Discovered contacts: - Adverts parse the two optional 16-bit feature fields before the name, as MeshCore's AdvertDataParser does, so their bytes are no longer read as the name (a zero byte gave an empty name). - A nameless advert or removal no longer wipes a known name. - The discovery tile gives the name the full width (the type chip beside it squeezed it to nothing at large text sizes); the type moves to the key line as plain text, and an unnamed node shows its key prefix. - The node scan sheet's type chip can shrink instead of the name. Contrast: - StatusChip draws its label in the normal text color; the icon or dot keeps the accent, adjusted to 3:1 against its tint. Avatar icons and initials get the same adjustment. Amber on its tint in the light theme was about 2:1. Channel chat: - Hashtag channels drop the redundant "Hashtag" subtitle label, and the region action moves into the overflow menu (tapping the title still opens it) so the channel name keeps its room. --- lib/connector/meshcore_connector.dart | 51 ++++++++----- lib/screens/channel_chat_screen.dart | 39 ++++++---- lib/screens/discovery_screen.dart | 41 +++++----- lib/screens/repeater_settings_screen.dart | 25 +++++- lib/widgets/mesh_ui.dart | 60 +++++++++++++-- lib/widgets/node_scan_sheet.dart | 12 ++- .../repeater_settings_expansion_test.dart | 76 +++++++++++++++++++ test/widgets/readable_accent_test.dart | 29 +++++++ 8 files changed, 265 insertions(+), 68 deletions(-) create mode 100644 test/screens/repeater_settings_expansion_test.dart create mode 100644 test/widgets/readable_accent_test.dart diff --git a/lib/connector/meshcore_connector.dart b/lib/connector/meshcore_connector.dart index 02c3b3b3..e69b4f32 100644 --- a/lib/connector/meshcore_connector.dart +++ b/lib/connector/meshcore_connector.dart @@ -9127,14 +9127,19 @@ class MeshCoreConnector extends ChangeNotifier { final flags = packet.readByte(); type = flags & 0x0F; hasLocation = (flags & 0x10) != 0; - // For future use: - //final hasFeature1 = (flags & 0x20) != 0; - //final hasFeature2 = (flags & 0x40) != 0; hasName = (flags & 0x80) != 0; if (hasLocation && packet.remaining >= 8) { latitude = packet.readInt32LE() / 1e6; longitude = packet.readInt32LE() / 1e6; } + // Two optional 16-bit feature fields sit between the location and the + // name (AdvertDataParser in MeshCore AdvertDataHelpers.cpp). Skip them + // so their bytes aren't read as the start of the name. + for (final featureMask in const [0x20, 0x40]) { + if ((flags & featureMask) != 0 && packet.remaining >= 2) { + packet.skipBytes(2); + } + } if (hasName && packet.remaining > 0) { name = packet.readCString(); } @@ -9204,14 +9209,19 @@ class MeshCoreConnector extends ChangeNotifier { final flags = advert.readByte(); type = flags & 0x0F; hasLocation = (flags & 0x10) != 0; - // For future use: - //final hasFeature1 = (flags & 0x20) != 0; - //final hasFeature2 = (flags & 0x40) != 0; hasName = (flags & 0x80) != 0; if (hasLocation && advert.remaining >= 8) { latitude = advert.readInt32LE() / 1e6; longitude = advert.readInt32LE() / 1e6; } + // Two optional 16-bit feature fields sit between the location and the + // name (AdvertDataParser in MeshCore AdvertDataHelpers.cpp). Skip them + // so their bytes aren't read as the start of the name. + for (final featureMask in const [0x20, 0x40]) { + if ((flags & featureMask) != 0 && advert.remaining >= 2) { + advert.skipBytes(2); + } + } // Validate location values if present hasLocation = hasValidLocation(latitude, longitude); @@ -9489,20 +9499,21 @@ class MeshCoreConnector extends ChangeNotifier { // Update existing contact if (existingIndex >= 0) { - _discoveredContacts[existingIndex] = _discoveredContacts[existingIndex] - .copyWith( - rawPacket: rawPacket, - name: contact.name, - type: contact.type, - pathLength: contact.pathLength, - path: contact.path, - pathHashWidth: contact.pathHashWidth, - latitude: contact.latitude, - longitude: contact.longitude, - lastSeen: contact.lastSeen, - flags: 0, - isActive: addActive, - ); + final existing = _discoveredContacts[existingIndex]; + _discoveredContacts[existingIndex] = existing.copyWith( + rawPacket: rawPacket, + // A nameless advert or removal must not wipe a known name. + name: contact.name.isEmpty ? existing.name : contact.name, + type: contact.type, + pathLength: contact.pathLength, + path: contact.path, + pathHashWidth: contact.pathHashWidth, + latitude: contact.latitude, + longitude: contact.longitude, + lastSeen: contact.lastSeen, + flags: 0, + isActive: addActive, + ); notifyListeners(); _scheduleDiscoveredContactsPersist(); return; diff --git a/lib/screens/channel_chat_screen.dart b/lib/screens/channel_chat_screen.dart index 97a16208..dc737b95 100644 --- a/lib/screens/channel_chat_screen.dart +++ b/lib/screens/channel_chat_screen.dart @@ -357,17 +357,19 @@ class _ChannelChatScreenState extends State { ), Consumer( builder: (context, connector, _) { + // Hashtag channels get no type label: the # icon + // and name already say it, and the title needs the + // room. final privacy = switch (Channel.getChannelType( widget.channel, _communityIndex, )) { ChannelType.public => context.l10n.channels_public, - ChannelType.hashtag => context.l10n.channels_hashtag, + ChannelType.hashtag => null, ChannelType.private => context.l10n.channels_private, ChannelType.communityPublic => context.l10n.community_publicChannel, - ChannelType.communityHashtag => - context.l10n.community_hashtagChannel, + ChannelType.communityHashtag => null, }; final ownRegion = connector.getChannelRegion( widget.channel.index, @@ -380,15 +382,17 @@ class _ChannelChatScreenState extends State { : ''; final radioDefault = connector.defaultFloodScopeName; final regionText = region.isNotEmpty - ? ' • ${context.l10n.channels_regionSetTo(region)}$defaultSuffix' + ? '${context.l10n.channels_regionSetTo(region)}$defaultSuffix' : radioDefault != null && connector.channelUsesRadioDefaultScope( widget.channel.index, ) - ? ' • ${context.l10n.channels_regionSetTo(radioDefault)} ${context.l10n.channels_regionRadioDefaultSuffix}' - : ''; + ? '${context.l10n.channels_regionSetTo(radioDefault)} ${context.l10n.channels_regionRadioDefaultSuffix}' + : null; + final subtitle = [?privacy, ?regionText].join(' • '); + if (subtitle.isEmpty) return const SizedBox.shrink(); return Text( - '$privacy$regionText', + subtitle, overflow: TextOverflow.ellipsis, style: const TextStyle(fontSize: 12), ); @@ -425,20 +429,29 @@ class _ChannelChatScreenState extends State { ); }, ), - IconButton( - tooltip: context.l10n.channels_regionSelect_Title, - icon: const Icon(Icons.landscape), - onPressed: () => openRegionSelectDialog(widget.channel), - ), const RadioStatsIconButton(), PopupMenuButton( icon: const Icon(Icons.more_vert), onSelected: (value) { - if (value == 'clearChat') { + if (value == 'region') { + openRegionSelectDialog(widget.channel); + } else if (value == 'clearChat') { _confirmClearChat(); } }, + // Region lives here (and on a tap of the title) so the channel + // name keeps its room in the app bar. itemBuilder: (context) => [ + PopupMenuItem( + value: 'region', + child: Row( + children: [ + const Icon(Icons.landscape, size: 20), + const SizedBox(width: 12), + Text(context.l10n.channels_regionSelect_Title), + ], + ), + ), PopupMenuItem( value: 'clearChat', child: Row( diff --git a/lib/screens/discovery_screen.dart b/lib/screens/discovery_screen.dart index a7e3079e..d2494282 100644 --- a/lib/screens/discovery_screen.dart +++ b/lib/screens/discovery_screen.dart @@ -203,35 +203,30 @@ class _DiscoveryScreenState extends State { crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.min, children: [ - // Name + type chip - Row( - children: [ - Expanded( - child: Text( - contact.name, - maxLines: 1, - overflow: TextOverflow.ellipsis, - style: const TextStyle( - fontWeight: FontWeight.w500, - fontSize: 15, - ), - ), - ), - const SizedBox(width: 6), - StatusChip( - label: contact.typeLabel(context.l10n).toUpperCase(), - color: contactTypeColor(contact.type), - icon: contactTypeIcon(contact.type), - ), - ], + // The name gets the full width; a type chip beside it + // squeezed it to nothing on narrow screens or large text. + // The avatar icon already shows the type. + Text( + // Nodes can advertise without a name; show the key + // prefix rather than a blank title. + contact.name.isNotEmpty + ? contact.name + : contact.publicKeyHex.substring(0, 8), + maxLines: 1, + overflow: TextOverflow.ellipsis, + style: const TextStyle( + fontWeight: FontWeight.w500, + fontSize: 15, + ), ), const SizedBox(height: 3), - // Short pub key + // Type and short pub key Row( children: [ Expanded( child: Text( - contact.shortPubKeyHex, + '${contact.typeLabel(context.l10n)} · ' + '${contact.shortPubKeyHex}', maxLines: 1, overflow: TextOverflow.ellipsis, style: MeshTheme.mono( diff --git a/lib/screens/repeater_settings_screen.dart b/lib/screens/repeater_settings_screen.dart index f02b6bbf..37732cd1 100644 --- a/lib/screens/repeater_settings_screen.dart +++ b/lib/screens/repeater_settings_screen.dart @@ -1464,6 +1464,22 @@ class _RepeaterSettingsScreenState extends State { ); } + /// Wraps an [ExpansionTile]'s children in their own [PageStorage] scope. + /// + /// The tile saves its expanded flag (a bool) under its [PageStorageKey]. + /// A TextField's inner Scrollable has no key, so without this it shares + /// that identifier, reads the bool back as its scroll offset and throws + /// while building (a grey box in release builds). + List _ownPageStorage(String id, List children) => [ + KeyedSubtree( + key: PageStorageKey(id), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: children, + ), + ), + ]; + Widget _buildAdvancedCard() { final l10n = context.l10n; return MeshCard( @@ -1476,7 +1492,10 @@ class _RepeaterSettingsScreenState extends State { ), subtitle: Text(l10n.repeater_advancedSettingsSubtitle), childrenPadding: const EdgeInsets.fromLTRB(0, 8, 0, 4), - children: _buildSpecControls(RepeaterSettingSection.advanced), + children: _ownPageStorage( + 'repeater_settings_advanced_body', + _buildSpecControls(RepeaterSettingSection.advanced), + ), ), ); } @@ -1545,7 +1564,7 @@ class _RepeaterSettingsScreenState extends State { ), subtitle: Text(l10n.repeater_keySettingsSubtitle), childrenPadding: const EdgeInsets.fromLTRB(0, 8, 0, 4), - children: [ + children: _ownPageStorage('repeater_settings_keys_body', [ Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ @@ -1659,7 +1678,7 @@ class _RepeaterSettingsScreenState extends State { const SizedBox(width: 48), ], ), - ], + ]), ), ); } diff --git a/lib/widgets/mesh_ui.dart b/lib/widgets/mesh_ui.dart index a69b2cfa..0d1aca45 100644 --- a/lib/widgets/mesh_ui.dart +++ b/lib/widgets/mesh_ui.dart @@ -106,6 +106,46 @@ class MeshCard extends StatelessWidget { } /// Tinted pill chip for statuses: a dot or icon plus a short label. +/// Contrast ratio between two opaque colors (WCAG definition). +double meshContrastRatio(Color a, Color b) { + final la = a.computeLuminance(); + final lb = b.computeLuminance(); + return (math.max(la, lb) + 0.05) / (math.min(la, lb) + 0.05); +} + +/// [accent] darkened (on light backgrounds) or lightened (on dark ones) until +/// it reaches [minContrast] against [background]. WCAG asks 4.5 for text and +/// 3 for icons. Accents such as amber are bright enough for the dark theme but +/// unreadable on their own pale tint in the light theme. +Color readableAccent( + Color accent, + Color background, { + double minContrast = 4.5, +}) { + if (meshContrastRatio(accent, background) >= minContrast) return accent; + final darken = background.computeLuminance() > 0.18; + final hsl = HSLColor.fromColor(accent); + for (var step = 1; step <= 20; step++) { + final lightness = (hsl.lightness + (darken ? -0.05 : 0.05) * step).clamp( + 0.0, + 1.0, + ); + final candidate = hsl.withLightness(lightness).toColor(); + if (meshContrastRatio(candidate, background) >= minContrast) { + return candidate; + } + } + return darken ? Colors.black : Colors.white; +} + +/// The background of a [tint] alpha wash of [accent] drawn on a card. +Color _tintedCardBackground(BuildContext context, Color accent, double tint) { + return Color.alphaBlend( + accent.withValues(alpha: tint), + Theme.of(context).colorScheme.surfaceContainerLow, + ); +} + class StatusChip extends StatelessWidget { final String label; final Color color; @@ -124,6 +164,11 @@ class StatusChip extends StatelessWidget { @override Widget build(BuildContext context) { + // The label uses the normal text color: colored text on a tint of the + // same color is hard to read. The icon or dot carries the color, adjusted + // to stay visible on the tint. + final background = _tintedCardBackground(context, color, 0.12); + final markColor = readableAccent(color, background, minContrast: 3); return Container( padding: const EdgeInsets.symmetric(horizontal: 9, vertical: 4), decoration: BoxDecoration( @@ -135,9 +180,9 @@ class StatusChip extends StatelessWidget { mainAxisSize: MainAxisSize.min, children: [ if (icon != null) - Icon(icon, size: fontSize + 2, color: color) + Icon(icon, size: fontSize + 2, color: markColor) else - PulseDot(color: color, size: 7, animate: pulse), + PulseDot(color: markColor, size: 7, animate: pulse), const SizedBox(width: 5), Flexible( child: Text( @@ -147,7 +192,7 @@ class StatusChip extends StatelessWidget { style: MeshTheme.mono( fontSize: fontSize, fontWeight: FontWeight.w600, - color: color, + color: Theme.of(context).colorScheme.onSurface, ), ), ), @@ -274,6 +319,7 @@ class AvatarCircle extends StatelessWidget { Widget build(BuildContext context) { final accent = color ?? _colorFor(name); final initials = _initials(name); + final background = _tintedCardBackground(context, accent, 0.14); return Container( width: size, height: size, @@ -284,13 +330,17 @@ class AvatarCircle extends StatelessWidget { ), alignment: Alignment.center, child: icon != null - ? Icon(icon, size: size * 0.5, color: accent) + ? Icon( + icon, + size: size * 0.5, + color: readableAccent(accent, background, minContrast: 3), + ) : Text( initials, style: MeshTheme.mono( fontSize: size * 0.36, fontWeight: FontWeight.w700, - color: accent, + color: readableAccent(accent, background), ), ), ); diff --git a/lib/widgets/node_scan_sheet.dart b/lib/widgets/node_scan_sheet.dart index 5676d131..272eaa24 100644 --- a/lib/widgets/node_scan_sheet.dart +++ b/lib/widgets/node_scan_sheet.dart @@ -215,10 +215,14 @@ class _NodeScanSheetState extends State { ), ), const SizedBox(width: 6), - StatusChip( - label: contact.typeLabel(l10n).toUpperCase(), - color: color, - icon: contactTypeIcon(response.nodeType), + // Let the chip shrink too, so it can't squeeze the + // name to nothing on narrow screens or large text. + Flexible( + child: StatusChip( + label: contact.typeLabel(l10n).toUpperCase(), + color: color, + icon: contactTypeIcon(response.nodeType), + ), ), ], ), diff --git a/test/screens/repeater_settings_expansion_test.dart b/test/screens/repeater_settings_expansion_test.dart new file mode 100644 index 00000000..3ed2f22f --- /dev/null +++ b/test/screens/repeater_settings_expansion_test.dart @@ -0,0 +1,76 @@ +import 'dart:typed_data'; + +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:provider/provider.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +import 'package:meshcore_open/connector/meshcore_connector.dart'; +import 'package:meshcore_open/l10n/app_localizations.dart'; +import 'package:meshcore_open/models/contact.dart'; +import 'package:meshcore_open/screens/repeater_settings_screen.dart'; +import 'package:meshcore_open/services/app_settings_service.dart'; +import 'package:meshcore_open/storage/prefs_manager.dart'; + +void main() { + // Regression: the Advanced and Identity Keys cards keep their expanded flag + // in PageStorage. Their TextFields' inner Scrollables used to share that + // identifier, read the bool as a scroll offset and fail to build (a grey + // box in release builds). + testWidgets('expanding Advanced and Identity Keys builds their text fields', ( + tester, + ) async { + SharedPreferences.setMockInitialValues({}); + await PrefsManager.initialize(); + // Tall enough that both cards fit without scrolling. + tester.view.physicalSize = const Size(1080, 24000); + tester.view.devicePixelRatio = 2.6; + addTearDown(tester.view.reset); + + await tester.pumpWidget( + MultiProvider( + providers: [ + ChangeNotifierProvider(create: (_) => MeshCoreConnector()), + ChangeNotifierProvider(create: (_) => AppSettingsService()), + ], + child: MaterialApp( + localizationsDelegates: AppLocalizations.localizationsDelegates, + supportedLocales: AppLocalizations.supportedLocales, + home: RepeaterSettingsScreen( + repeater: Contact( + publicKey: Uint8List(32), + name: 'Repeater', + type: 2, + pathLength: 0, + path: Uint8List(0), + lastSeen: DateTime(2026), + ), + password: 'x', + ), + ), + ), + ); + await tester.pump(const Duration(milliseconds: 500)); + + final l10n = AppLocalizations.of( + tester.element(find.byType(RepeaterSettingsScreen)), + ); + for (final title in [ + l10n.repeater_advancedSettings, + l10n.repeater_keySettings, + ]) { + await tester.tap(find.text(title)); + await tester.pump(const Duration(seconds: 1)); + expect(tester.takeException(), isNull); + } + + expect( + find.widgetWithText(TextField, l10n.repeater_txDelay), + findsOneWidget, + ); + expect( + find.widgetWithText(TextField, l10n.repeater_prvKey), + findsOneWidget, + ); + }); +} diff --git a/test/widgets/readable_accent_test.dart b/test/widgets/readable_accent_test.dart new file mode 100644 index 00000000..349b4810 --- /dev/null +++ b/test/widgets/readable_accent_test.dart @@ -0,0 +1,29 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; + +import 'package:meshcore_open/theme/mesh_theme.dart'; +import 'package:meshcore_open/widgets/mesh_ui.dart'; + +void main() { + // Amber on its own pale tint in the light theme was about 2:1. + final lightTint = Color.alphaBlend( + MeshPalette.warn.withValues(alpha: 0.12), + MeshPalette.lightBg1, + ); + final darkTint = Color.alphaBlend( + MeshPalette.warn.withValues(alpha: 0.12), + MeshPalette.bg1, + ); + + test('darkens an accent on a light background until it is readable', () { + expect(meshContrastRatio(MeshPalette.warn, lightTint), lessThan(3)); + final text = readableAccent(MeshPalette.warn, lightTint); + expect(meshContrastRatio(text, lightTint), greaterThanOrEqualTo(4.5)); + final icon = readableAccent(MeshPalette.warn, lightTint, minContrast: 3); + expect(meshContrastRatio(icon, lightTint), greaterThanOrEqualTo(3)); + }); + + test('keeps an accent that is already readable', () { + expect(readableAccent(MeshPalette.warn, darkTint), MeshPalette.warn); + }); +}