Files
pipelines/app/lib/ui/adaptive/glass_pill.dart
T
alan ca2f7eee32 App: add a selectable app design with a Terminal look
AppDesign builds each design's light and dark ThemeData with an
AppDesignTokens extension (corner radius, flat surfaces, frame colour,
ANSI accents, prompt glyph). Settings gains a Design picker stored per
device; the theme hosts rebuild live. Terminal is monospace (bundled
RobotoMono), near-black or paper, flat 1px frames with 3px corners, and
draws the chat transcript as prompt lines, tool dot/result lines and
bracketed follow-up lines. Default keeps its exact themes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 16:43:59 +00:00

152 lines
5.5 KiB
Dart

import 'package:flutter/material.dart';
import '../theme/app_design_tokens.dart';
import 'glass_surface.dart';
/// A frosted-glass piece floating over content: the content behind is
/// blurred and its colour lifted inside the piece's [shape], under a
/// translucent fill in Material `surfaceContainerHigh`, a soft highlight along
/// its top, a faint rim over a hairline border, and a soft shadow
/// outside it ([GlassLook], tuned for light and dark fills).
///
/// Under the system's high-contrast setting the fill is solid, nothing is
/// blurred and only the hairline border is drawn. A transparent [Material]
/// lets ink
/// responses inside it show.
///
/// Shared by the segmented top bar and any floating bar (a tab bar) that
/// wants the same pieces.
class GlassPill extends StatelessWidget {
const GlassPill({
super.key,
required this.child,
this.shape = const StadiumBorder(),
this.fill,
});
final Widget child;
final OutlinedBorder shape;
/// Overrides the fill (a dark viewer's pieces); used as given.
final Color? fill;
/// Blur radius of the content behind a piece.
///
/// Under the [fillOpacity] fill, 4 looks the same as a larger radius (the
/// content behind is already an indistinct tint) and stays on the
/// renderer's direct blur path: above about 4, Skia (CanvasKit) downscales,
/// blurs and upscales every piece on every frame content scrolls behind it.
static const double blurSigma = 4;
/// How much of the fill covers the blurred content.
static const double fillOpacity = 0.82;
@override
Widget build(BuildContext context) {
final reveal = GlassReveal.maybeOf(context);
if (reveal == null) return _build(context, 1, child);
// The fill, edge and blur fade in together, and the content with them:
// an opacity layer over the whole piece would hide the content behind
// it from the blur until the fade ends, and the blur would snap on.
final content = FadeTransition(
opacity: reveal,
alwaysIncludeSemantics: true,
child: child,
);
return AnimatedBuilder(
animation: reveal,
builder: (context, _) =>
_build(context, reveal.value.clamp(0.0, 1.0), content),
);
}
Widget _build(BuildContext context, double reveal, Widget child) {
// A flat design (Terminal) draws a solid piece in its own corners with
// its 1px frame: no blur, highlight, rim or shadow.
final tokens = AppDesignTokens.of(context);
final flat = tokens.flat;
final solid = flat || (MediaQuery.maybeHighContrastOf(context) ?? false);
final shape = tokens.shape(this.shape);
final textDirection = Directionality.maybeOf(context);
final Color base;
final Color edge;
final scheme = Theme.of(context).colorScheme;
base = flat ? scheme.surfaceContainerLow : scheme.surfaceContainerHigh;
edge = flat ? tokens.frame ?? scheme.outline : scheme.outlineVariant;
final opaque = fill ?? (solid ? base : base.withValues(alpha: fillOpacity));
final color = opaque.withValues(alpha: opaque.a * reveal);
final look = GlassLook.forFill(opaque);
Color faded(Color c, [double by = 1]) =>
c.withValues(alpha: c.a * by * reveal);
final border = shape.copyWith(
side: BorderSide(
color: faded(edge, solid ? 1 : 0.6),
width: flat ? 1 : 0.5,
),
);
Widget content = Material(type: MaterialType.transparency, child: child);
if (!solid) {
// The highlight lies on the fill under the content; the rim is drawn
// over the content (and its ink) so the edge stays clean.
content = CustomPaint(
painter: GlassHighlightPainter(
shape: shape,
color: faded(look.highlight),
textDirection: textDirection,
),
foregroundPainter: GlassRimPainter(
shape: shape,
top: faded(look.rimTop),
bottom: faded(look.rimBottom),
textDirection: textDirection,
),
child: content,
);
}
Widget surface = DecoratedBox(
decoration: ShapeDecoration(color: color, shape: border),
child: content,
);
if (!solid) {
// One filter per piece, clipped to it: the blur and the saturation
// boost are composed into one backdrop read. Pieces under a common
// [BackdropGroup] (a bar's row) share one read of the content behind
// them where the engine supports it (Impeller) instead of one each.
surface = BackdropFilter.grouped(
filter: GlassLook.backdrop(blurSigma * reveal),
child: surface,
);
}
surface = ClipPath(
clipper: ShapeBorderClipper(shape: shape, textDirection: textDirection),
child: surface,
);
if (solid) return surface;
// The shadow falls outside the clip, so it is painted beside it.
return CustomPaint(
painter: GlassShadowPainter(
shape: shape,
color: faded(look.shadow),
textDirection: textDirection,
),
child: surface,
);
}
}
/// Fades in every [GlassPill] below it by [opacity]: its fill, hairline,
/// blur and content together. A piece entering the screen uses it instead of
/// an opacity layer, under which the blur would read nothing behind it.
class GlassReveal extends InheritedWidget {
const GlassReveal({super.key, required this.opacity, required super.child});
final Animation<double> opacity;
static Animation<double>? maybeOf(BuildContext context) =>
context.dependOnInheritedWidgetOfExactType<GlassReveal>()?.opacity;
@override
bool updateShouldNotify(GlassReveal oldWidget) =>
opacity != oldWidget.opacity;
}