Files
pipelines/app/lib/ui/adaptive/glass_surface.dart
T
alan 9283220c82 Glass: liquid-glass look for every glass surface
GlassPill (segmented bars, floating tabs, composer, viewer control bars,
top strips) now composes blur with a saturation boost in one backdrop
filter, paints a soft top highlight, a thin luminous gradient rim and an
outer-only depth shadow, tuned separately for light and dark fills
(GlassLook in glass_surface.dart). Fill opacity 0.84 -> 0.78; blur stays
at sigma 4 and one filter per piece. High contrast keeps the solid piece.
2026-11-03 16:11:24 +00:00

203 lines
6.4 KiB
Dart

import 'dart:ui' show ImageFilter;
import 'package:flutter/widgets.dart';
/// The look of a frosted [GlassPill]-style surface, tuned once for light and
/// dark fills: how the content behind is filtered, the soft specular
/// highlight along its top edge, its luminous rim and its depth shadow.
///
/// Every value is chosen for a surface that keeps text and icons readable at
/// WCAG AA over whatever scrolls behind it: the fill still covers most of the
/// backdrop, and the highlight only lifts the fill's top a little.
@immutable
class GlassLook {
const GlassLook._({
required this.highlight,
required this.rimTop,
required this.rimBottom,
required this.shadow,
});
/// The look over a light fill (the light theme's raised surface).
static const GlassLook light = GlassLook._(
highlight: Color(0xA6FFFFFF),
rimTop: Color(0xE6FFFFFF),
rimBottom: Color(0x33FFFFFF),
shadow: Color(0x24000000),
);
/// The look over a dark fill (the dark theme's raised surface, a dark
/// viewer's pieces).
static const GlassLook dark = GlassLook._(
highlight: Color(0x1FFFFFFF),
rimTop: Color(0x47FFFFFF),
rimBottom: Color(0x0DFFFFFF),
shadow: Color(0x59000000),
);
/// The look for a surface filled with [fill]: dark below mid luminance.
static GlassLook forFill(Color fill) =>
fill.computeLuminance() < 0.3 ? dark : light;
/// The highlight's colour at the very top of the surface; it fades out by
/// [highlightExtent] of the surface's height.
final Color highlight;
/// The rim's colour along the top edge and along the bottom edge.
final Color rimTop;
final Color rimBottom;
/// The colour of the soft shadow under the surface.
final Color shadow;
/// How far down the surface the top highlight reaches, as a fraction of
/// its height.
static const double highlightExtent = 0.55;
/// The width of the luminous rim, inside the surface's edge.
static const double rimWidth = 1;
/// The shadow's blur and how far below the surface it falls.
static const double shadowSigma = 3;
static const Offset shadowOffset = Offset(0, 1.5);
/// How much the backdrop's colour is boosted after blurring, so a colour
/// behind the glass still reads through its fill.
static const double saturation = 1.6;
/// The backdrop filter: a blur of [sigma], then the saturation boost. Both
/// are one filter (one backdrop read) on every renderer.
static ImageFilter backdrop(double sigma) => ImageFilter.compose(
outer: const ColorFilter.matrix(_saturationMatrix),
inner: ImageFilter.blur(sigmaX: sigma, sigmaY: sigma),
);
// A standard saturation matrix (Rec. 709 luma weights) for [saturation].
static const double _r = 0.2126 * (1 - saturation);
static const double _g = 0.7152 * (1 - saturation);
static const double _b = 0.0722 * (1 - saturation);
static const List<double> _saturationMatrix = [
_r + saturation, _g, _b, 0, 0, //
_r, _g + saturation, _b, 0, 0, //
_r, _g, _b + saturation, 0, 0, //
0, 0, 0, 1, 0, //
];
}
/// The soft specular highlight along the top of a glass surface of [shape],
/// painted over its fill and under its content.
class GlassHighlightPainter extends CustomPainter {
const GlassHighlightPainter({
required this.shape,
required this.color,
required this.textDirection,
});
final ShapeBorder shape;
final Color color;
final TextDirection? textDirection;
@override
void paint(Canvas canvas, Size size) {
if (color.a == 0 || size.isEmpty) return;
final rect = Offset.zero & size;
final paint = Paint()
..shader = LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [color, color.withValues(alpha: 0)],
stops: const [0, GlassLook.highlightExtent],
).createShader(rect);
canvas.drawPath(
shape.getOuterPath(rect, textDirection: textDirection),
paint,
);
}
@override
bool shouldRepaint(GlassHighlightPainter oldDelegate) =>
color != oldDelegate.color ||
shape != oldDelegate.shape ||
textDirection != oldDelegate.textDirection;
}
/// The thin luminous rim of a glass surface of [shape]: a stroke just inside
/// its edge, bright along the top and faint along the bottom, painted over
/// the surface's content (ink included) so it stays a clean edge.
class GlassRimPainter extends CustomPainter {
const GlassRimPainter({
required this.shape,
required this.top,
required this.bottom,
required this.textDirection,
});
final ShapeBorder shape;
final Color top;
final Color bottom;
final TextDirection? textDirection;
@override
void paint(Canvas canvas, Size size) {
if ((top.a == 0 && bottom.a == 0) || size.isEmpty) return;
const inset = GlassLook.rimWidth / 2;
final rect = (Offset.zero & size).deflate(inset);
final paint = Paint()
..style = PaintingStyle.stroke
..strokeWidth = GlassLook.rimWidth
..shader = LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [top, bottom, bottom],
stops: const [0, 0.5, 1],
).createShader(rect);
canvas.drawPath(
shape.getOuterPath(rect, textDirection: textDirection),
paint,
);
}
@override
bool shouldRepaint(GlassRimPainter oldDelegate) =>
top != oldDelegate.top ||
bottom != oldDelegate.bottom ||
shape != oldDelegate.shape ||
textDirection != oldDelegate.textDirection;
}
/// The soft depth shadow of a glass surface of [shape], blurred only outside
/// its outline ([BlurStyle.outer]): under a translucent fill a shadow would
/// darken the glass itself.
class GlassShadowPainter extends CustomPainter {
const GlassShadowPainter({
required this.shape,
required this.color,
required this.textDirection,
});
final ShapeBorder shape;
final Color color;
final TextDirection? textDirection;
@override
void paint(Canvas canvas, Size size) {
if (color.a == 0 || size.isEmpty) return;
final rect = (Offset.zero & size).shift(GlassLook.shadowOffset);
canvas.drawPath(
shape.getOuterPath(rect, textDirection: textDirection),
Paint()
..color = color
..maskFilter = const MaskFilter.blur(
BlurStyle.outer,
GlassLook.shadowSigma,
),
);
}
@override
bool shouldRepaint(GlassShadowPainter oldDelegate) =>
color != oldDelegate.color ||
shape != oldDelegate.shape ||
textDirection != oldDelegate.textDirection;
}