Skip to content

Make StreamColorSwatches.lerp and CodeBlockTextStyles.lerp static #759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 26, 2024
Merged
130 changes: 65 additions & 65 deletions lib/widgets/code_block.dart
Original file line number Diff line number Diff line change
Expand Up @@ -477,73 +477,73 @@ class CodeBlockTextStyles {
};
}

CodeBlockTextStyles lerp(CodeBlockTextStyles? other, double t) {
if (identical(this, other)) return this;
static CodeBlockTextStyles lerp(CodeBlockTextStyles a, CodeBlockTextStyles b, double t) {
if (identical(a, b)) return a;

return CodeBlockTextStyles._(
plain: TextStyle.lerp(plain, other?.plain, t)!,
hll: TextStyle.lerp(_hll, other?._hll, t)!,
c: TextStyle.lerp(_c, other?._c, t)!,
err: TextStyle.lerp(_err, other?._err, t)!,
k: TextStyle.lerp(_k, other?._k, t)!,
o: TextStyle.lerp(_o, other?._o, t)!,
cm: TextStyle.lerp(_cm, other?._cm, t)!,
cp: TextStyle.lerp(_cp, other?._cp, t)!,
c1: TextStyle.lerp(_c1, other?._c1, t)!,
cs: TextStyle.lerp(_cs, other?._cs, t)!,
gd: TextStyle.lerp(_gd, other?._gd, t)!,
ge: TextStyle.lerp(_ge, other?._ge, t)!,
gr: TextStyle.lerp(_gr, other?._gr, t)!,
gh: TextStyle.lerp(_gh, other?._gh, t)!,
gi: TextStyle.lerp(_gi, other?._gi, t)!,
go: TextStyle.lerp(_go, other?._go, t)!,
gp: TextStyle.lerp(_gp, other?._gp, t)!,
gs: TextStyle.lerp(_gs, other?._gs, t)!,
gu: TextStyle.lerp(_gu, other?._gu, t)!,
gt: TextStyle.lerp(_gt, other?._gt, t)!,
kc: TextStyle.lerp(_kc, other?._kc, t)!,
kd: TextStyle.lerp(_kd, other?._kd, t)!,
kn: TextStyle.lerp(_kn, other?._kn, t)!,
kp: TextStyle.lerp(_kp, other?._kp, t)!,
kr: TextStyle.lerp(_kr, other?._kr, t)!,
kt: TextStyle.lerp(_kt, other?._kt, t)!,
m: TextStyle.lerp(_m, other?._m, t)!,
s: TextStyle.lerp(_s, other?._s, t)!,
na: TextStyle.lerp(_na, other?._na, t)!,
nb: TextStyle.lerp(_nb, other?._nb, t)!,
nc: TextStyle.lerp(_nc, other?._nc, t)!,
no: TextStyle.lerp(_no, other?._no, t)!,
nd: TextStyle.lerp(_nd, other?._nd, t)!,
ni: TextStyle.lerp(_ni, other?._ni, t)!,
ne: TextStyle.lerp(_ne, other?._ne, t)!,
nf: TextStyle.lerp(_nf, other?._nf, t)!,
nl: TextStyle.lerp(_nl, other?._nl, t)!,
nn: TextStyle.lerp(_nn, other?._nn, t)!,
nt: TextStyle.lerp(_nt, other?._nt, t)!,
nv: TextStyle.lerp(_nv, other?._nv, t)!,
nx: TextStyle.lerp(_nx, other?._nx, t)!,
ow: TextStyle.lerp(_ow, other?._ow, t)!,
w: TextStyle.lerp(_w, other?._w, t)!,
mf: TextStyle.lerp(_mf, other?._mf, t)!,
mh: TextStyle.lerp(_mh, other?._mh, t)!,
mi: TextStyle.lerp(_mi, other?._mi, t)!,
mo: TextStyle.lerp(_mo, other?._mo, t)!,
sb: TextStyle.lerp(_sb, other?._sb, t)!,
sc: TextStyle.lerp(_sc, other?._sc, t)!,
sd: TextStyle.lerp(_sd, other?._sd, t)!,
s2: TextStyle.lerp(_s2, other?._s2, t)!,
se: TextStyle.lerp(_se, other?._se, t)!,
sh: TextStyle.lerp(_sh, other?._sh, t)!,
si: TextStyle.lerp(_si, other?._si, t)!,
sx: TextStyle.lerp(_sx, other?._sx, t)!,
sr: TextStyle.lerp(_sr, other?._sr, t)!,
s1: TextStyle.lerp(_s1, other?._s1, t)!,
ss: TextStyle.lerp(_ss, other?._ss, t)!,
bp: TextStyle.lerp(_bp, other?._bp, t)!,
vc: TextStyle.lerp(_vc, other?._vc, t)!,
vg: TextStyle.lerp(_vg, other?._vg, t)!,
vi: TextStyle.lerp(_vi, other?._vi, t)!,
il: TextStyle.lerp(_il, other?._il, t)!,
plain: TextStyle.lerp(a.plain, b.plain, t)!,
hll: TextStyle.lerp(a._hll, b._hll, t)!,
c: TextStyle.lerp(a._c, b._c, t)!,
err: TextStyle.lerp(a._err, b._err, t)!,
k: TextStyle.lerp(a._k, b._k, t)!,
o: TextStyle.lerp(a._o, b._o, t)!,
cm: TextStyle.lerp(a._cm, b._cm, t)!,
cp: TextStyle.lerp(a._cp, b._cp, t)!,
c1: TextStyle.lerp(a._c1, b._c1, t)!,
cs: TextStyle.lerp(a._cs, b._cs, t)!,
gd: TextStyle.lerp(a._gd, b._gd, t)!,
ge: TextStyle.lerp(a._ge, b._ge, t)!,
gr: TextStyle.lerp(a._gr, b._gr, t)!,
gh: TextStyle.lerp(a._gh, b._gh, t)!,
gi: TextStyle.lerp(a._gi, b._gi, t)!,
go: TextStyle.lerp(a._go, b._go, t)!,
gp: TextStyle.lerp(a._gp, b._gp, t)!,
gs: TextStyle.lerp(a._gs, b._gs, t)!,
gu: TextStyle.lerp(a._gu, b._gu, t)!,
gt: TextStyle.lerp(a._gt, b._gt, t)!,
kc: TextStyle.lerp(a._kc, b._kc, t)!,
kd: TextStyle.lerp(a._kd, b._kd, t)!,
kn: TextStyle.lerp(a._kn, b._kn, t)!,
kp: TextStyle.lerp(a._kp, b._kp, t)!,
kr: TextStyle.lerp(a._kr, b._kr, t)!,
kt: TextStyle.lerp(a._kt, b._kt, t)!,
m: TextStyle.lerp(a._m, b._m, t)!,
s: TextStyle.lerp(a._s, b._s, t)!,
na: TextStyle.lerp(a._na, b._na, t)!,
nb: TextStyle.lerp(a._nb, b._nb, t)!,
nc: TextStyle.lerp(a._nc, b._nc, t)!,
no: TextStyle.lerp(a._no, b._no, t)!,
nd: TextStyle.lerp(a._nd, b._nd, t)!,
ni: TextStyle.lerp(a._ni, b._ni, t)!,
ne: TextStyle.lerp(a._ne, b._ne, t)!,
nf: TextStyle.lerp(a._nf, b._nf, t)!,
nl: TextStyle.lerp(a._nl, b._nl, t)!,
nn: TextStyle.lerp(a._nn, b._nn, t)!,
nt: TextStyle.lerp(a._nt, b._nt, t)!,
nv: TextStyle.lerp(a._nv, b._nv, t)!,
nx: TextStyle.lerp(a._nx, b._nx, t)!,
ow: TextStyle.lerp(a._ow, b._ow, t)!,
w: TextStyle.lerp(a._w, b._w, t)!,
mf: TextStyle.lerp(a._mf, b._mf, t)!,
mh: TextStyle.lerp(a._mh, b._mh, t)!,
mi: TextStyle.lerp(a._mi, b._mi, t)!,
mo: TextStyle.lerp(a._mo, b._mo, t)!,
sb: TextStyle.lerp(a._sb, b._sb, t)!,
sc: TextStyle.lerp(a._sc, b._sc, t)!,
sd: TextStyle.lerp(a._sd, b._sd, t)!,
s2: TextStyle.lerp(a._s2, b._s2, t)!,
se: TextStyle.lerp(a._se, b._se, t)!,
sh: TextStyle.lerp(a._sh, b._sh, t)!,
si: TextStyle.lerp(a._si, b._si, t)!,
sx: TextStyle.lerp(a._sx, b._sx, t)!,
sr: TextStyle.lerp(a._sr, b._sr, t)!,
s1: TextStyle.lerp(a._s1, b._s1, t)!,
ss: TextStyle.lerp(a._ss, b._ss, t)!,
bp: TextStyle.lerp(a._bp, b._bp, t)!,
vc: TextStyle.lerp(a._vc, b._vc, t)!,
vg: TextStyle.lerp(a._vg, b._vg, t)!,
vi: TextStyle.lerp(a._vi, b._vi, t)!,
il: TextStyle.lerp(a._il, b._il, t)!,
);
}
}
10 changes: 5 additions & 5 deletions lib/widgets/content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ class ContentTheme extends ThemeExtension<ContentTheme> {
}

@override
ContentTheme lerp(ContentTheme? other, double t) {
ContentTheme lerp(ContentTheme other, double t) {
if (identical(this, other)) {
return this;
}
return ContentTheme._(
textStylePlainParagraph: TextStyle.lerp(textStylePlainParagraph, other?.textStylePlainParagraph, t)!,
codeBlockTextStyles: codeBlockTextStyles.lerp(other?.codeBlockTextStyles, t),
textStyleError: TextStyle.lerp(textStyleError, other?.textStyleError, t)!,
textStyleErrorCode: TextStyle.lerp(textStyleErrorCode, other?.textStyleErrorCode, t)!,
textStylePlainParagraph: TextStyle.lerp(textStylePlainParagraph, other.textStylePlainParagraph, t)!,
codeBlockTextStyles: CodeBlockTextStyles.lerp(codeBlockTextStyles, other.codeBlockTextStyles, t),
textStyleError: TextStyle.lerp(textStyleError, other.textStyleError, t)!,
textStyleErrorCode: TextStyle.lerp(textStyleErrorCode, other.textStyleErrorCode, t)!,
);
}
}
Expand Down
18 changes: 8 additions & 10 deletions lib/widgets/stream_colors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,25 @@ abstract class StreamColorSwatches {

StreamColorSwatch _computeForBaseColor(int base);

/// Gives a [StreamColorSwatches], lerped to [other] at [t].
/// Gives a [StreamColorSwatches], lerped between [a] and [b] at [t].
///
/// If [this] and [other] are [identical], returns [this].
/// If [a] and [b] are [identical], returns [this].
///
/// Else returns an instance whose [forBaseColor] will call
/// [this.forBaseColor] and [other.forBaseColor]
/// [a.forBaseColor] and [b.forBaseColor]
/// and return [StreamColorSwatch.lerp]'s result on those.
/// This computation is cached on the instance
/// in order to save work building [t]'s animation frame when there are
/// multiple UI elements using the same [subscription.color].
StreamColorSwatches lerp(StreamColorSwatches? other, double t) {
static StreamColorSwatches lerp(StreamColorSwatches a, StreamColorSwatches b, double t) {
// This short-circuit helps when [a] and [b]
// are both [StreamColorSwatches.light]
// or both [StreamColorSwatches.dark].
// Empirically, [lerp] is called even when the theme hasn't changed,
// so this is an important optimization.
if (identical(this, other)) {
return this;
}
if (identical(a, b)) return a;

return _StreamColorSwatchesLerped(this, other, t);
return _StreamColorSwatchesLerped(a, b, t);
}
}

Expand All @@ -65,12 +63,12 @@ class _StreamColorSwatchesLerped extends StreamColorSwatches {
_StreamColorSwatchesLerped(this.a, this.b, this.t);

final StreamColorSwatches a;
final StreamColorSwatches? b;
final StreamColorSwatches b;
final double t;

@override
StreamColorSwatch _computeForBaseColor(int base) =>
StreamColorSwatch.lerp(a.forBaseColor(base), b?.forBaseColor(base), t)!;
StreamColorSwatch.lerp(a.forBaseColor(base), b.forBaseColor(base), t)!;
}


Expand Down
14 changes: 7 additions & 7 deletions lib/widgets/theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,17 @@ class DesignVariables extends ThemeExtension<DesignVariables> {
}

@override
DesignVariables lerp(DesignVariables? other, double t) {
DesignVariables lerp(DesignVariables other, double t) {
if (identical(this, other)) {
return this;
}
return DesignVariables._(
bgMain: Color.lerp(bgMain, other?.bgMain, t)!,
bgTopBar: Color.lerp(bgTopBar, other?.bgTopBar, t)!,
borderBar: Color.lerp(borderBar, other?.borderBar, t)!,
icon: Color.lerp(icon, other?.icon, t)!,
title: Color.lerp(title, other?.title, t)!,
streamColorSwatches: streamColorSwatches.lerp(other?.streamColorSwatches, t),
bgMain: Color.lerp(bgMain, other.bgMain, t)!,
bgTopBar: Color.lerp(bgTopBar, other.bgTopBar, t)!,
borderBar: Color.lerp(borderBar, other.borderBar, t)!,
icon: Color.lerp(icon, other.icon, t)!,
title: Color.lerp(title, other.title, t)!,
streamColorSwatches: StreamColorSwatches.lerp(streamColorSwatches, other.streamColorSwatches, t),
);
}
}
Expand Down
13 changes: 6 additions & 7 deletions test/widgets/stream_colors_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,16 @@ void main() {

group('lerp', () {
test('on identical instances', () {
check(
StreamColorSwatches.light.lerp(StreamColorSwatches.light, 0.5)
).identicalTo(StreamColorSwatches.light);
final light = StreamColorSwatches.light;
check(StreamColorSwatches.lerp(light, light, 0.5)).identicalTo(light);

check(
StreamColorSwatches.dark.lerp(StreamColorSwatches.dark, 0.5)
).identicalTo(StreamColorSwatches.dark);
final dark = StreamColorSwatches.dark;
check(StreamColorSwatches.lerp(dark, dark, 0.5)).identicalTo(dark);
});

test('from light to dark', () {
final instance = StreamColorSwatches.light.lerp(StreamColorSwatches.dark, 0.4);
final instance = StreamColorSwatches
.lerp(StreamColorSwatches.light, StreamColorSwatches.dark, 0.4);

const base1 = 0xff76ce90;
final swatch1 = instance.forBaseColor(base1);
Expand Down
Loading