From 0399af60f40c36f6225f2fed72749e40569e6014 Mon Sep 17 00:00:00 2001 From: Chris Bobbe Date: Fri, 21 Jun 2024 16:05:07 -0700 Subject: [PATCH] stream_colors [nfc]: Make StreamColorSwatches.lerp static Discussion: https://github.com/zulip/zulip-flutter/pull/746#issuecomment-2183544362 --- lib/widgets/stream_colors.dart | 12 ++++++------ lib/widgets/theme.dart | 2 +- test/widgets/stream_colors_test.dart | 13 ++++++------- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/lib/widgets/stream_colors.dart b/lib/widgets/stream_colors.dart index 3686b0cf29..5c9a23c800 100644 --- a/lib/widgets/stream_colors.dart +++ b/lib/widgets/stream_colors.dart @@ -23,25 +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); } } diff --git a/lib/widgets/theme.dart b/lib/widgets/theme.dart index 6a0bf902d7..8a108d4be1 100644 --- a/lib/widgets/theme.dart +++ b/lib/widgets/theme.dart @@ -141,7 +141,7 @@ class DesignVariables extends ThemeExtension { 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), + streamColorSwatches: StreamColorSwatches.lerp(streamColorSwatches, other.streamColorSwatches, t), ); } } diff --git a/test/widgets/stream_colors_test.dart b/test/widgets/stream_colors_test.dart index b4ebbaa925..a3615c1919 100644 --- a/test/widgets/stream_colors_test.dart +++ b/test/widgets/stream_colors_test.dart @@ -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);