mirror of
https://github.com/zarzet/SpotiFLAC-Mobile.git
synced 2026-11-28 07:04:12 +01:00
perf(theme): build the first frame with the system palette
DynamicColorBuilder asked for the wallpaper palette (or the desktop accent) in its first build, so the first frame used the seed scheme and MaterialApp then animated the whole theme for 300 ms into the palette, rebuilding every Theme.of dependent about 18 times during the busiest part of startup. The owner approved building the first frame with the final theme, which removes that launch fade. main now asks once the Activity attaches, alongside the other startup calls, and waits at most 100 ms for the answer before runApp; it is normally in by then. The wrapper builds its first frame with that answer through a provider override; a late answer still applies with the fade, and without a launch request the wrapper asks itself as before. The request keeps DynamicColorBuilder's order and schemes. Every Material launch also ran a second, invisible 300 ms fade: the custom font provider's first answer rebuilt the wrapper, and two builds of the same settings never give equal ThemeData. The wrapper now watches only the font family it reads. Host test with MaterialApp's 300 ms theme animation: the first frame now has the palette, with 1 theme build and 1 Theme.of reader build instead of 2 and 20 over 21 frames; with no dynamic color 1 and 1 instead of 2 and 20. Theme changes after launch still animate.
This commit is contained in:
1 parent
65a50d87f1
commit
6fa5e52beb
4 files changed
+331
-4
No files matched your search
@@ -82,6 +82,9 @@ void main() {
|
||||
}
|
||||
await waitForActivityAttachment(binding);
|
||||
}
|
||||
// Asked after attachment, so an app a media browser started long
|
||||
// before it opens still reads the current wallpaper colors.
|
||||
final dynamicColors = DynamicColors.request();
|
||||
final orientation = _initializeOrientation();
|
||||
// Desktop: log files and the unclean-exit marker, before the error
|
||||
// hooks below start writing to them.
|
||||
@@ -171,6 +174,9 @@ void main() {
|
||||
final runtimeProfile = await _resolveRuntimeProfile(prefs);
|
||||
_configureImageCache(runtimeProfile);
|
||||
await orientation;
|
||||
// Usually answered by now. The first frame then has the final theme,
|
||||
// with no fade from the seed colors; a late answer still applies.
|
||||
await dynamicColors.settle(const Duration(milliseconds: 100));
|
||||
|
||||
runApp(
|
||||
ProviderScope(
|
||||
@@ -191,6 +197,7 @@ void main() {
|
||||
initialSafAccessLost,
|
||||
),
|
||||
initialThemeSettingsProvider.overrideWithValue(bootstrapTheme),
|
||||
initialDynamicColorsProvider.overrideWithValue(dynamicColors),
|
||||
// While the paired desktop plays in the phone's place, the
|
||||
// player screens show and drive it; on the desktop, the phone
|
||||
// it follows.
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import 'package:dynamic_color/dynamic_color.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:material_ui/material_ui.dart' as material_ui;
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:mornye/models/theme_settings.dart';
|
||||
|
||||
@@ -7,6 +10,71 @@ final initialThemeSettingsProvider = Provider<ThemeSettings>(
|
||||
(ref) => ThemeSettings(style: defaultAppThemeStyle()),
|
||||
);
|
||||
|
||||
/// The launch request for the system's colors. Without one the theme asks
|
||||
/// when it first builds.
|
||||
final initialDynamicColorsProvider = Provider<DynamicColors?>((ref) => null);
|
||||
|
||||
typedef DynamicColorSchemes = ({
|
||||
material_ui.ColorScheme light,
|
||||
material_ui.ColorScheme dark,
|
||||
});
|
||||
|
||||
/// The system's colors: the Android 12+ wallpaper palette, else the desktop
|
||||
/// accent color. Asked once, in the same order as `DynamicColorBuilder`.
|
||||
class DynamicColors {
|
||||
DynamicColors.request() : future = _read() {
|
||||
future.then<void>((schemes) {
|
||||
_schemes = schemes;
|
||||
_settled = true;
|
||||
});
|
||||
}
|
||||
|
||||
/// Null when the platform has no dynamic colors.
|
||||
final Future<DynamicColorSchemes?> future;
|
||||
DynamicColorSchemes? _schemes;
|
||||
bool _settled = false;
|
||||
|
||||
bool get isSettled => _settled;
|
||||
DynamicColorSchemes? get schemes => _schemes;
|
||||
|
||||
/// Waits at most [timeout] for the answer; never throws.
|
||||
Future<void> settle(Duration timeout) => future
|
||||
.timeout(timeout, onTimeout: () => null)
|
||||
.then<void>((_) {}, onError: (Object _) {});
|
||||
|
||||
static Future<DynamicColorSchemes?> _read() async {
|
||||
try {
|
||||
final palette = await DynamicColorPlugin.getCorePalette();
|
||||
if (palette != null) {
|
||||
return (
|
||||
light: palette.toColorScheme(),
|
||||
dark: palette.toColorScheme(brightness: Brightness.dark),
|
||||
);
|
||||
}
|
||||
} on PlatformException {
|
||||
// Falls back to the accent color.
|
||||
}
|
||||
try {
|
||||
final accent = await DynamicColorPlugin.getAccentColor();
|
||||
if (accent != null) {
|
||||
return (
|
||||
light: material_ui.ColorScheme.fromSeed(
|
||||
seedColor: accent,
|
||||
brightness: Brightness.light,
|
||||
),
|
||||
dark: material_ui.ColorScheme.fromSeed(
|
||||
seedColor: accent,
|
||||
brightness: Brightness.dark,
|
||||
),
|
||||
);
|
||||
}
|
||||
} on PlatformException {
|
||||
// No dynamic colors on this platform.
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
ThemeSettings loadBootstrapThemeSettings(SharedPreferences prefs) {
|
||||
return ThemeSettings(
|
||||
themeMode: themeModeFromString(prefs.getString(kThemeModeKey)),
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:dynamic_color/dynamic_color.dart';
|
||||
import 'package:material_ui/material_ui.dart' as material_ui;
|
||||
import 'package:mornye/providers/theme_provider.dart';
|
||||
import 'package:mornye/providers/custom_font_provider.dart';
|
||||
@@ -32,12 +31,16 @@ class DynamicColorWrapper extends ConsumerWidget {
|
||||
),
|
||||
),
|
||||
);
|
||||
final fontFamily = ref.watch(customFontFamilyProvider).asData?.value;
|
||||
// Only the family: a rebuild while it loads would animate an equal theme.
|
||||
final fontFamily = ref.watch(
|
||||
customFontFamilyProvider.select((font) => font.asData?.value),
|
||||
);
|
||||
|
||||
// Both styles build below the same DynamicColorBuilder. Switching styles
|
||||
// Both styles build below the same _DynamicColorBuilder. Switching styles
|
||||
// must update the app in place: a different parent remounted the whole
|
||||
// app, losing its state and briefly mounting a second macOS menu bar.
|
||||
return DynamicColorBuilder(
|
||||
return _DynamicColorBuilder(
|
||||
colors: ref.watch(initialDynamicColorsProvider),
|
||||
builder: (lightDynamic, darkDynamic) {
|
||||
if (themeSettings.style == AppThemeStyle.mornye) {
|
||||
return builder(
|
||||
@@ -161,3 +164,40 @@ class DynamicColorWrapper extends ConsumerWidget {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Builds with the system's colors. A launch request answered before the
|
||||
/// first frame builds it with them, so the theme does not fade into them.
|
||||
class _DynamicColorBuilder extends StatefulWidget {
|
||||
const _DynamicColorBuilder({required this.colors, required this.builder});
|
||||
|
||||
final DynamicColors? colors;
|
||||
final Widget Function(
|
||||
material_ui.ColorScheme? lightDynamic,
|
||||
material_ui.ColorScheme? darkDynamic,
|
||||
)
|
||||
builder;
|
||||
|
||||
@override
|
||||
State<_DynamicColorBuilder> createState() => _DynamicColorBuilderState();
|
||||
}
|
||||
|
||||
class _DynamicColorBuilderState extends State<_DynamicColorBuilder> {
|
||||
DynamicColorSchemes? _schemes;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
final colors = widget.colors ?? DynamicColors.request();
|
||||
if (colors.isSettled) {
|
||||
_schemes = colors.schemes;
|
||||
return;
|
||||
}
|
||||
colors.future.then<void>((schemes) {
|
||||
if (mounted && schemes != null) setState(() => _schemes = schemes);
|
||||
}, onError: (Object _) {});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) =>
|
||||
widget.builder(_schemes?.light, _schemes?.dark);
|
||||
}
|
||||
@@ -0,0 +1,212 @@
|
||||
import 'dart:async';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:dynamic_color/dynamic_color.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:material_color_utilities/material_color_utilities.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:mornye/models/theme_settings.dart';
|
||||
import 'package:mornye/providers/theme_provider.dart';
|
||||
import 'package:mornye/theme/dynamic_color_wrapper.dart';
|
||||
|
||||
class _ThemeReader extends StatelessWidget {
|
||||
const _ThemeReader(this.builds);
|
||||
|
||||
final List<ThemeData> builds;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
builds.add(Theme.of(context));
|
||||
return const SizedBox();
|
||||
}
|
||||
}
|
||||
|
||||
// A wallpaper palette as Android sends it: five tonal palettes, 13 tones each.
|
||||
final _primary = TonalPalette.of(340, 48);
|
||||
final _secondary = TonalPalette.of(340, 16);
|
||||
final _palette = Int32List.fromList([
|
||||
..._primary.asList,
|
||||
..._secondary.asList,
|
||||
...TonalPalette.of(20, 24).asList,
|
||||
...TonalPalette.of(340, 4).asList,
|
||||
...TonalPalette.of(340, 8).asList,
|
||||
]);
|
||||
// The light scheme takes its primary and secondary from tone 40.
|
||||
final _palettePrimary = Color(_primary.get(40));
|
||||
|
||||
/// Answers the dynamic color channel as Android 12+ does, or as a platform
|
||||
/// without dynamic colors; counts the requests.
|
||||
class _SystemColors {
|
||||
_SystemColors(WidgetTester tester, {this.palette = true}) {
|
||||
final messenger = tester.binding.defaultBinaryMessenger;
|
||||
messenger.setMockMethodCallHandler(DynamicColorPlugin.channel, (call) {
|
||||
requests.add(call.method);
|
||||
if (!palette || call.method != DynamicColorPlugin.methodName) {
|
||||
return Future.value();
|
||||
}
|
||||
return (gate?.future ?? Future.value()).then((_) => _palette);
|
||||
});
|
||||
addTearDown(
|
||||
() =>
|
||||
messenger.setMockMethodCallHandler(DynamicColorPlugin.channel, null),
|
||||
);
|
||||
}
|
||||
|
||||
final bool palette;
|
||||
final requests = <String>[];
|
||||
Completer<void>? gate;
|
||||
}
|
||||
|
||||
class _Launch {
|
||||
final builds = <ThemeData>[];
|
||||
int themeBuilds = 0;
|
||||
late ProviderContainer container;
|
||||
|
||||
Future<void> pump(WidgetTester tester, {DynamicColors? colors}) async {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
container = ProviderContainer(
|
||||
overrides: [
|
||||
initialThemeSettingsProvider.overrideWithValue(
|
||||
loadBootstrapThemeSettings(prefs),
|
||||
),
|
||||
initialDynamicColorsProvider.overrideWithValue(colors),
|
||||
],
|
||||
);
|
||||
addTearDown(container.dispose);
|
||||
final reader = _ThemeReader(builds);
|
||||
await tester.pumpWidget(
|
||||
UncontrolledProviderScope(
|
||||
container: container,
|
||||
child: DynamicColorWrapper(
|
||||
builder: (light, dark, mode) {
|
||||
themeBuilds++;
|
||||
// As MornyeApp animates theme changes.
|
||||
return MaterialApp(
|
||||
theme: light,
|
||||
darkTheme: dark,
|
||||
themeMode: mode,
|
||||
themeAnimationDuration: const Duration(milliseconds: 300),
|
||||
themeAnimationCurve: Curves.easeInOut,
|
||||
home: reader,
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// Pumps frames until none is scheduled; returns how many it pumped.
|
||||
Future<int> settleFrames(WidgetTester tester) async {
|
||||
var frames = 0;
|
||||
while (tester.binding.hasScheduledFrame && frames < 100) {
|
||||
await tester.pump(const Duration(milliseconds: 16));
|
||||
frames++;
|
||||
}
|
||||
return frames;
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
testWidgets('a launch answer builds the first frame with the palette', (
|
||||
tester,
|
||||
) async {
|
||||
final system = _SystemColors(tester);
|
||||
final colors = DynamicColors.request();
|
||||
await colors.settle(const Duration(milliseconds: 100));
|
||||
expect(colors.isSettled, isTrue);
|
||||
|
||||
final launch = _Launch();
|
||||
await launch.pump(tester, colors: colors);
|
||||
expect(launch.builds.single.colorScheme.primary, _palettePrimary);
|
||||
expect(
|
||||
launch.builds.single.colorScheme.secondary,
|
||||
Color(_secondary.get(40)),
|
||||
);
|
||||
|
||||
// Before: the seed first, then 20 theme reader builds over 21 frames
|
||||
// while the palette faded in.
|
||||
expect(await launch.settleFrames(tester), lessThanOrEqualTo(1));
|
||||
expect(launch.builds, hasLength(1));
|
||||
expect(launch.themeBuilds, 1);
|
||||
expect(system.requests, [DynamicColorPlugin.methodName]);
|
||||
});
|
||||
|
||||
testWidgets('the theme does not rebuild or fade while the font loads', (
|
||||
tester,
|
||||
) async {
|
||||
final system = _SystemColors(tester, palette: false);
|
||||
final launch = _Launch();
|
||||
await launch.pump(tester);
|
||||
await launch.settleFrames(tester);
|
||||
|
||||
// Before: the font provider's first answer rebuilt an equal theme and
|
||||
// ran the 300 ms fade (20 reader builds).
|
||||
expect(launch.builds, hasLength(1));
|
||||
expect(launch.themeBuilds, 1);
|
||||
expect(system.requests, [
|
||||
DynamicColorPlugin.methodName,
|
||||
DynamicColorPlugin.accentColorMethodName,
|
||||
]);
|
||||
});
|
||||
|
||||
testWidgets('a late answer still applies the palette with the fade', (
|
||||
tester,
|
||||
) async {
|
||||
final system = _SystemColors(tester)..gate = Completer<void>();
|
||||
final colors = DynamicColors.request();
|
||||
final settling = colors.settle(const Duration(milliseconds: 100));
|
||||
await tester.pump(const Duration(milliseconds: 100));
|
||||
await settling;
|
||||
expect(colors.isSettled, isFalse);
|
||||
|
||||
final launch = _Launch();
|
||||
await launch.pump(tester, colors: colors);
|
||||
final seed = launch.builds.single.colorScheme.primary;
|
||||
expect(seed, isNot(_palettePrimary));
|
||||
|
||||
system.gate!.complete();
|
||||
await tester.pump();
|
||||
expect(await launch.settleFrames(tester), greaterThan(10));
|
||||
expect(launch.builds.last.colorScheme.primary, _palettePrimary);
|
||||
expect(launch.themeBuilds, 2);
|
||||
// The theme reuses the launch request instead of asking again.
|
||||
expect(system.requests, [DynamicColorPlugin.methodName]);
|
||||
});
|
||||
|
||||
testWidgets('without a launch request the theme asks, as before', (
|
||||
tester,
|
||||
) async {
|
||||
final system = _SystemColors(tester);
|
||||
final launch = _Launch();
|
||||
await launch.pump(tester);
|
||||
expect(launch.builds.single.colorScheme.primary, isNot(_palettePrimary));
|
||||
await launch.settleFrames(tester);
|
||||
expect(launch.builds.last.colorScheme.primary, _palettePrimary);
|
||||
expect(system.requests, [DynamicColorPlugin.methodName]);
|
||||
});
|
||||
|
||||
testWidgets('theme changes after launch still fade', (tester) async {
|
||||
_SystemColors(tester);
|
||||
final colors = DynamicColors.request();
|
||||
await colors.settle(const Duration(milliseconds: 100));
|
||||
final launch = _Launch();
|
||||
await launch.pump(tester, colors: colors);
|
||||
await launch.settleFrames(tester);
|
||||
|
||||
await launch.container
|
||||
.read(themeProvider.notifier)
|
||||
.setUseDynamicColor(false);
|
||||
await tester.pump();
|
||||
expect(await launch.settleFrames(tester), greaterThan(10));
|
||||
expect(launch.builds.length, greaterThan(10));
|
||||
expect(
|
||||
launch.builds.last.colorScheme.primary,
|
||||
ColorScheme.fromSeed(seedColor: const Color(kDefaultSeedColor)).primary,
|
||||
);
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user