Files
swiftcontrol/lib/widgets/controller/controller_canvas.dart
Jonas Acker 6e8d580989 fix(ui): second design review fixes
- Ride's activity preview (840-1199) renders the same ActivityRow as the
  Activity tab and the activity column, error fix links included.
- Connection methods under the "Recommended" header no longer repeat a
  Recommended pill, so the title gets the row beside the switch. The
  protocol announcement is a card in the new card language; its en/it/pl
  copy uses a proper dash.
- Pod buttons read "P", "SB", a power or zap icon instead of arrow
  initials like "P→"; a single right pod is framed on the art and its
  buttons so the picture sits centred in its card.
- The last-press strip keeps the action whole: it follows "Just pressed
  X →" on the same line when it fits, else on the next.
- Snapshot harness captures a beta controller in the light theme.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-29 12:43:45 +02:00

107 lines
4.4 KiB
Dart

import 'package:bike_control/utils/keymap/buttons.dart';
import 'package:bike_control/widgets/controller/controller_contour_painter.dart';
import 'package:bike_control/widgets/controller/controller_layout.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:shadcn_flutter/shadcn_flutter.dart';
typedef ControllerButtonBuilder = Widget Function(ControllerButton button);
class ControllerCanvas extends StatelessWidget {
final ControllerLayout layout;
final List<ControllerButton> availableButtons;
final ControllerButtonBuilder buttonBuilder;
final double buttonSize;
/// Tallest the picture may grow, before scaling. Defaults to 200 for a pill
/// and 250 for everything else; Ride's wider layouts give it more.
final double? maxHeight;
const ControllerCanvas({
super.key,
required this.layout,
required this.availableButtons,
required this.buttonBuilder,
this.buttonSize = 56,
this.maxHeight,
});
/// When a device sets `allowMultiple: true`, its `availableButtons` are
/// cloned with a per-device `sourceDeviceId` set, which breaks `==` against
/// the original `ControllerButton` stored in `layout.positions`. Match on
/// the `name` field instead — unique within any one device's button set.
Offset? _positionFor(ControllerButton btn) {
for (final entry in layout.positions.entries) {
if (entry.key.name == btn.name) return entry.value;
}
return null;
}
@override
Widget build(BuildContext context) {
final cs = Theme.of(context).colorScheme;
return Center(
child: Container(
width: double.infinity,
alignment: Alignment.center,
padding: EdgeInsets.symmetric(vertical: layout.padding),
constraints: BoxConstraints(
maxHeight: (maxHeight ?? (layout.shape == ContourShape.pill ? 200 : 250)) / Theme.of(context).scaling,
),
child: AspectRatio(
aspectRatio: layout.aspectRatio,
child: LayoutBuilder(
builder: (context, constraints) {
final w = constraints.maxWidth;
final h = constraints.maxHeight;
return Stack(
clipBehavior: Clip.none,
children: [
Positioned.fill(
child: IgnorePointer(
child: layout.svgAsset != null
? Transform.flip(
flipX: layout.mirrorX,
child: RotatedBox(
quarterTurns: ((layout.rotation / 90).round() % 4 + 4) % 4,
child: SvgPicture.asset(
layout.svgAsset!,
fit: BoxFit.contain,
// The flip mirrors the alignment too.
alignment: layout.mirrorX
? Alignment(-layout.svgAlignment.x, layout.svgAlignment.y)
: layout.svgAlignment,
colorFilter: Theme.of(context).brightness == Brightness.dark
? ColorFilter.mode(cs.primary.withValues(alpha: 1), BlendMode.srcIn)
: null,
),
),
)
: CustomPaint(
painter: ControllerContourPainter(
shape: layout.shape,
color: cs.border,
// Subtle darkening via the muted foreground at low alpha
// — visually distinct from the solid `cs.muted` button
// fill, so the buttons don't blend into the background.
fillColor: cs.mutedForeground.withValues(alpha: 0.08),
),
),
),
),
for (final btn in availableButtons)
if (_positionFor(btn) case final pos?)
Positioned(
left: (pos.dx * w) - buttonSize / 2,
top: (pos.dy * h) - buttonSize / 2,
child: buttonBuilder(btn),
),
],
);
},
),
),
),
);
}
}