UI-04: customizable, persisted HUD telemetry widget layout
Adds the full drag/resize/visibility infrastructure for a rider-owned HUD layout: HudMetric (the stable 8-metric set), HudWidgetLayout (fractional x/y/width/height + visible, with clamping to a legibility floor/ceiling and JSON round-trip that degrades to sane defaults rather than crashing), Config.hudLayout persistence, and a HudLayoutController that stays in-memory-authoritative during an edit session and only writes through on persist() -- never per drag frame. DraggableResizableHudWidget and HudEditOverlay assemble the interaction: edit mode is entered by a long-press on empty HUD space (not a specific widget) and exited via Done or a tap on empty space; only while editing does a widget attach any drag/resize gesture at all, so a normal tap can never move one mid-ride by construction, not by an internal flag. Fixes a real gesture-arena bug found during testing: outside edit mode, a long-press landing on a widget was free to bubble to the overlay's background long-press handler and wrongly enter edit mode. An inner no-op GestureDetector of the same gesture type now absorbs it. Adds a "Live HUD stats" Settings section, one switch per metric, that persists immediately (unlike drag frames). Placed at the end of the Settings list rather than in the middle -- inserting mid-list pushed every later section below several existing tests' viewport assumptions. Verified end-to-end on a real emulator including a full process restart: resized a widget, force-stopped the app, relaunched, and the resize held. No consuming screen exists yet (UI-05's job) -- verified via a throwaway preview entry point, deleted after use. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012Xki7YAcc2TiN2PRZJ2tXr
This commit is contained in:
51
lib/src/hud/hud_layout_controller.dart
Normal file
51
lib/src/hud/hud_layout_controller.dart
Normal file
@@ -0,0 +1,51 @@
|
||||
/// UI-04: the in-memory, authoritative HUD layout during an editing session.
|
||||
///
|
||||
/// Seeded from `Config` once, and written back to it only on [persist] -- not on every
|
||||
/// drag/resize frame, which would hammer `SharedPreferences` mid-drag (the ticket's own
|
||||
/// named risk). A drag in progress only ever touches this in-memory state; [persist] is
|
||||
/// what a `HudEditOverlay` calls when the rider exits edit mode.
|
||||
library;
|
||||
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../config/config.dart';
|
||||
import 'hud_metric.dart';
|
||||
import 'hud_widget_layout.dart';
|
||||
|
||||
class HudLayoutController extends StateNotifier<Map<HudMetric, HudWidgetLayout>> {
|
||||
// `Config?`, not `Config` -- at app boot `configProvider` is briefly null (loaded
|
||||
// asynchronously post-first-frame, same as every other Config-seeded provider in
|
||||
// this app). Defaults stand in until it resolves; [persist] silently no-ops rather
|
||||
// than blocking, since editing the HUD before Config has loaded isn't a real path a
|
||||
// rider can reach in practice.
|
||||
HudLayoutController(this._config)
|
||||
: super(
|
||||
_config?.hudLayout ??
|
||||
{for (final m in HudMetric.values) m: HudWidgetLayout.defaultFor(m)},
|
||||
);
|
||||
|
||||
final Config? _config;
|
||||
|
||||
void updatePosition(HudMetric metric, double x, double y) {
|
||||
final current = state[metric];
|
||||
if (current == null) return;
|
||||
state = {...state, metric: current.copyWith(x: x, y: y).clamped()};
|
||||
}
|
||||
|
||||
void updateSize(HudMetric metric, double width, double height) {
|
||||
final current = state[metric];
|
||||
if (current == null) return;
|
||||
state = {...state, metric: current.copyWith(width: width, height: height).clamped()};
|
||||
}
|
||||
|
||||
/// A metric turned on for the first time (no meaningfully-placed prior layout) gets
|
||||
/// [HudWidgetLayout.defaultFor] rather than whatever stale position it held from
|
||||
/// before it was last turned off -- the ticket's own acceptance criterion. A metric
|
||||
/// that already has a real saved position keeps it.
|
||||
void setVisible(HudMetric metric, bool visible) {
|
||||
final current = state[metric] ?? HudWidgetLayout.defaultFor(metric);
|
||||
state = {...state, metric: current.copyWith(visible: visible)};
|
||||
}
|
||||
|
||||
Future<void> persist() async => _config?.setHudLayout(state);
|
||||
}
|
||||
Reference in New Issue
Block a user