move AppLifecyleListener to Flutter plugin

This commit is contained in:
Nick Fisher
2024-05-21 10:18:35 +08:00
parent 90727e79c5
commit b33e7d04ab
2 changed files with 59 additions and 52 deletions

View File

@@ -1,5 +1,7 @@
import 'dart:async'; import 'dart:async';
import 'dart:ui';
import 'package:dart_filament/dart_filament/abstract_filament_viewer.dart'; import 'package:dart_filament/dart_filament/abstract_filament_viewer.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_filament_platform_interface/flutter_filament_platform_interface.dart'; import 'package:flutter_filament_platform_interface/flutter_filament_platform_interface.dart';
import 'package:flutter_filament_platform_interface/flutter_filament_texture.dart'; import 'package:flutter_filament_platform_interface/flutter_filament_texture.dart';
@@ -7,6 +9,50 @@ import 'package:flutter_filament_platform_interface/flutter_filament_texture.dar
/// A Flutter-only interface for creating an [AbstractFilamentViewer] . /// A Flutter-only interface for creating an [AbstractFilamentViewer] .
/// ///
class FlutterFilamentPlugin { class FlutterFilamentPlugin {
bool _wasRenderingOnInactive = false;
void _handleStateChange(AppLifecycleState state) async {
await initialized;
switch (state) {
case AppLifecycleState.detached:
print("Detached");
if (!_wasRenderingOnInactive) {
_wasRenderingOnInactive = viewer.rendering;
}
await viewer.setRendering(false);
break;
case AppLifecycleState.hidden:
print("Hidden");
if (!_wasRenderingOnInactive) {
_wasRenderingOnInactive = viewer.rendering;
}
await viewer.setRendering(false);
break;
case AppLifecycleState.inactive:
print("Inactive");
if (!_wasRenderingOnInactive) {
_wasRenderingOnInactive = viewer.rendering;
}
// on Windows in particular, restoring a window after minimizing stalls the renderer (and the whole application) for a considerable length of time.
// disabling rendering on minimize seems to fix the issue (so I wonder if there's some kind of command buffer that's filling up while the window is minimized).
await viewer.setRendering(false);
break;
case AppLifecycleState.paused:
print("Paused");
if (!_wasRenderingOnInactive) {
_wasRenderingOnInactive = viewer.rendering;
}
await viewer.setRendering(false);
break;
case AppLifecycleState.resumed:
print("Resumed");
await viewer.setRendering(_wasRenderingOnInactive);
break;
}
}
AppLifecycleListener? _appLifecycleListener;
AbstractFilamentViewer get viewer => FlutterFilamentPlatform.instance.viewer; AbstractFilamentViewer get viewer => FlutterFilamentPlatform.instance.viewer;
final _initialized = Completer<bool>(); final _initialized = Completer<bool>();
@@ -18,11 +64,13 @@ class FlutterFilamentPlugin {
} }
await FlutterFilamentPlatform.instance await FlutterFilamentPlatform.instance
.initialize(uberArchivePath: uberArchivePath); .initialize(uberArchivePath: uberArchivePath);
print("instance init completed");
_appLifecycleListener = AppLifecycleListener(
onStateChange: _handleStateChange,
);
_initialized.complete(true); _initialized.complete(true);
print("completed compelter");
await viewer.initialized; await viewer.initialized;
print("viewer init complete");
} }
Future<FlutterFilamentTexture?> createTexture( Future<FlutterFilamentTexture?> createTexture(
@@ -44,5 +92,6 @@ class FlutterFilamentPlugin {
void dispose() { void dispose() {
FlutterFilamentPlatform.instance.dispose(); FlutterFilamentPlatform.instance.dispose();
_appLifecycleListener?.dispose();
} }
} }

View File

@@ -28,7 +28,6 @@ class FilamentWidget extends StatefulWidget {
class _FilamentWidgetState extends State<FilamentWidget> { class _FilamentWidgetState extends State<FilamentWidget> {
FlutterFilamentTexture? _texture; FlutterFilamentTexture? _texture;
late final AppLifecycleListener _appLifecycleListener;
Rect get _rect { Rect get _rect {
final renderBox = (context.findRenderObject()) as RenderBox; final renderBox = (context.findRenderObject()) as RenderBox;
@@ -45,10 +44,10 @@ class _FilamentWidgetState extends State<FilamentWidget> {
var width = (dpr * size.width).ceil(); var width = (dpr * size.width).ceil();
var height = (dpr * size.height).ceil(); var height = (dpr * size.height).ceil();
_texture = await widget.plugin.createTexture(width, height, 0, 0); _texture = await widget.plugin.createTexture(width, height, 0, 0);
_appLifecycleListener = AppLifecycleListener(
onStateChange: _handleStateChange, if (mounted) {
); setState(() {});
setState(() {}); }
}); });
super.initState(); super.initState();
} }
@@ -59,52 +58,10 @@ class _FilamentWidgetState extends State<FilamentWidget> {
widget.plugin.destroyTexture(_texture!); widget.plugin.destroyTexture(_texture!);
} }
_appLifecycleListener.dispose();
super.dispose(); super.dispose();
} }
bool _wasRenderingOnInactive = false;
void _handleStateChange(AppLifecycleState state) async {
await widget.plugin.viewer.initialized;
switch (state) {
case AppLifecycleState.detached:
print("Detached");
if (!_wasRenderingOnInactive) {
_wasRenderingOnInactive = widget.plugin.viewer.rendering;
}
await widget.plugin.viewer.setRendering(false);
break;
case AppLifecycleState.hidden:
print("Hidden");
if (!_wasRenderingOnInactive) {
_wasRenderingOnInactive = widget.plugin.viewer.rendering;
}
await widget.plugin.viewer.setRendering(false);
break;
case AppLifecycleState.inactive:
print("Inactive");
if (!_wasRenderingOnInactive) {
_wasRenderingOnInactive = widget.plugin.viewer.rendering;
}
// on Windows in particular, restoring a window after minimizing stalls the renderer (and the whole application) for a considerable length of time.
// disabling rendering on minimize seems to fix the issue (so I wonder if there's some kind of command buffer that's filling up while the window is minimized).
await widget.plugin.viewer.setRendering(false);
break;
case AppLifecycleState.paused:
print("Paused");
if (!_wasRenderingOnInactive) {
_wasRenderingOnInactive = widget.plugin.viewer.rendering;
}
await widget.plugin.viewer.setRendering(false);
break;
case AppLifecycleState.resumed:
print("Resumed");
await widget.plugin.viewer.setRendering(_wasRenderingOnInactive);
break;
}
}
bool _resizing = false; bool _resizing = false;
Future _resizeTexture(Size newSize) async { Future _resizeTexture(Size newSize) async {
@@ -130,7 +87,8 @@ class _FilamentWidgetState extends State<FilamentWidget> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
if (_texture == null) { if (_texture == null) {
return widget.initial ?? Container(color: kIsWeb ? Colors.transparent : Colors.red); return widget.initial ??
Container(color: kIsWeb ? Colors.transparent : Colors.red);
} }
var textureWidget = Texture( var textureWidget = Texture(