(re)set rendering on all lifecycle changes

This commit is contained in:
Nick Fisher
2023-10-17 08:55:49 +08:00
parent d3f84f156a
commit 7f9c5a0f2d

View File

@@ -168,8 +168,9 @@ class _SizedFilamentWidgetState extends State<_SizedFilamentWidget> {
// debug mode does need a longer timeout. // debug mode does need a longer timeout.
_resizeTimer?.cancel(); _resizeTimer?.cancel();
_resizeTimer = Timer(const Duration(milliseconds: kReleaseMode ? 20 : 100), () async { _resizeTimer =
if(!mounted) { Timer(const Duration(milliseconds: kReleaseMode ? 20 : 100), () async {
if (!mounted) {
return; return;
} }
var size = ((context.findRenderObject()) as RenderBox).size; var size = ((context.findRenderObject()) as RenderBox).size;
@@ -209,41 +210,42 @@ class _SizedFilamentWidgetState extends State<_SizedFilamentWidget> {
switch (state) { switch (state) {
case AppLifecycleState.detached: case AppLifecycleState.detached:
print("Detached"); print("Detached");
if (!_wasRenderingOnInactive) {
if (widget.controller.textureDetails != null) { _wasRenderingOnInactive = widget.controller.rendering;
await widget.controller.destroyViewer();
await widget.controller.destroyTexture();
} }
await widget.controller.setRendering(false);
break; break;
case AppLifecycleState.hidden: case AppLifecycleState.hidden:
print("Hidden"); print("Hidden");
if (Platform.isIOS && widget.controller.textureDetails != null) { if (!_wasRenderingOnInactive) {
await widget.controller.destroyViewer(); _wasRenderingOnInactive = widget.controller.rendering;
await widget.controller.destroyTexture();
} }
await widget.controller.setRendering(false);
break; break;
case AppLifecycleState.inactive: case AppLifecycleState.inactive:
print("Inactive"); print("Inactive");
if (!_wasRenderingOnInactive) {
_wasRenderingOnInactive = widget.controller.rendering;
}
// on Windows in particular, restoring a window after minimizing stalls the renderer (and the whole application) for a considerable length of time. // 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). // 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).
_wasRenderingOnInactive = widget.controller.rendering;
await widget.controller.setRendering(false); await widget.controller.setRendering(false);
break; break;
case AppLifecycleState.paused: case AppLifecycleState.paused:
print("Paused"); print("Paused");
if (!_wasRenderingOnInactive) {
_wasRenderingOnInactive = widget.controller.rendering;
}
await widget.controller.setRendering(false);
break; break;
case AppLifecycleState.resumed: case AppLifecycleState.resumed:
print("Resumed"); print("Resumed");
if (!Platform.isWindows) {
if (widget.controller.textureDetails == null) {
var size = ((context.findRenderObject()) as RenderBox).size;
widget.controller
.createViewer(size.width.ceil(), size.height.ceil());
}
} else {
await _resize();
}
await widget.controller.setRendering(_wasRenderingOnInactive); await widget.controller.setRendering(_wasRenderingOnInactive);
await _resize();
WidgetsBinding.instance.addPostFrameCallback((_) {
setState(() {});
});
break; break;
} }
_lastState = state; _lastState = state;
@@ -274,7 +276,7 @@ class _SizedFilamentWidgetState extends State<_SizedFilamentWidget> {
return Stack(children: [ return Stack(children: [
Positioned.fill( Positioned.fill(
child: Platform.isLinux || Platform.isWindows child: Platform.isLinux || Platform.isWindows
? Transform( ? Transform(
alignment: Alignment.center, alignment: Alignment.center,
transform: Matrix4.rotationX( transform: Matrix4.rotationX(