Files
pipelines/app/lib/ui/theme/design_screen_overlay.dart
T
alan 7c42713be3 Retro CRT: scanlines as one solid path, no vignette
The full-screen repeating gradient and radial vignette doubled CPU raster
time per frame in the browser bench; solid non-AA bands cost about nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-04 00:12:44 +00:00

79 lines
2.6 KiB
Dart

import 'package:flutter/material.dart';
import 'app_design_tokens.dart';
/// [child] under the design's [AppDesignTokens.screen] pattern (Retro CRT's
/// scanlines), or [child] alone under a design without one.
///
/// The pattern is one [CustomPaint] in its own [RepaintBoundary]: it paints
/// once (again only on a resize, a pixel-ratio change or a design switch)
/// and the compositor reuses its picture every frame. It adds no saveLayer,
/// no filter and no animation, ignores the pointer and has no semantics.
/// [child] stays the Stack's first child either way, so switching to or
/// from a patterned design never remounts the app beneath.
class DesignScreenOverlay extends StatelessWidget {
const DesignScreenOverlay({super.key, required this.child});
final Widget child;
@override
Widget build(BuildContext context) {
final pattern = AppDesignTokens.of(context).screen;
return Stack(
fit: StackFit.passthrough,
children: [
child,
if (pattern != null)
Positioned.fill(
child: IgnorePointer(
child: RepaintBoundary(
key: const Key('design-screen-overlay'),
child: CustomPaint(
painter: ScreenPatternPainter(
pattern,
MediaQuery.devicePixelRatioOf(context),
),
),
),
),
),
],
);
}
}
/// Paints a [ScreenPattern]: its scanlines as one path of thin rectangles,
/// snapped to device pixels and filled solid without anti-aliasing. One draw
/// with no shader: a full-screen gradient (and a vignette tried with it)
/// doubled the CPU raster time per frame in the browser bench, while solid
/// bands touch only a third of the pixels and blend at memory speed.
class ScreenPatternPainter extends CustomPainter {
const ScreenPatternPainter(this.pattern, this.pixelRatio);
final ScreenPattern pattern;
final double pixelRatio;
@override
void paint(Canvas canvas, Size size) {
final period = pattern.period.clamp(2, 12);
final band = (period / 3).round().clamp(1, period - 1);
// In logical pixels, on device-pixel boundaries.
final step = period / pixelRatio;
final height = band / pixelRatio;
final lines = Path();
for (var y = step - height; y < size.height; y += step) {
lines.addRect(Rect.fromLTWH(0, y, size.width, height));
}
canvas.drawPath(
lines,
Paint()
..color = pattern.scanline
..isAntiAlias = false,
);
}
@override
bool shouldRepaint(ScreenPatternPainter old) =>
old.pattern != pattern || old.pixelRatio != pixelRatio;
}