From b33e7d04abf6ab1ed792fd9d625801dccf3a60b2 Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Tue, 21 May 2024 10:18:35 +0800 Subject: [PATCH] move AppLifecyleListener to Flutter plugin --- .../lib/filament/flutter_filament_plugin.dart | 55 +++++++++++++++++- .../lib/filament/widgets/filament_widget.dart | 56 +++---------------- 2 files changed, 59 insertions(+), 52 deletions(-) diff --git a/flutter_filament_federated/flutter_filament/lib/filament/flutter_filament_plugin.dart b/flutter_filament_federated/flutter_filament/lib/filament/flutter_filament_plugin.dart index 03790fa3..5511b6ef 100644 --- a/flutter_filament_federated/flutter_filament/lib/filament/flutter_filament_plugin.dart +++ b/flutter_filament_federated/flutter_filament/lib/filament/flutter_filament_plugin.dart @@ -1,5 +1,7 @@ import 'dart:async'; +import 'dart:ui'; 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_texture.dart'; @@ -7,6 +9,50 @@ import 'package:flutter_filament_platform_interface/flutter_filament_texture.dar /// A Flutter-only interface for creating an [AbstractFilamentViewer] . /// 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; final _initialized = Completer(); @@ -18,11 +64,13 @@ class FlutterFilamentPlugin { } await FlutterFilamentPlatform.instance .initialize(uberArchivePath: uberArchivePath); - print("instance init completed"); + + _appLifecycleListener = AppLifecycleListener( + onStateChange: _handleStateChange, + ); _initialized.complete(true); - print("completed compelter"); + await viewer.initialized; - print("viewer init complete"); } Future createTexture( @@ -44,5 +92,6 @@ class FlutterFilamentPlugin { void dispose() { FlutterFilamentPlatform.instance.dispose(); + _appLifecycleListener?.dispose(); } } diff --git a/flutter_filament_federated/flutter_filament/lib/filament/widgets/filament_widget.dart b/flutter_filament_federated/flutter_filament/lib/filament/widgets/filament_widget.dart index 269b140d..bb209703 100644 --- a/flutter_filament_federated/flutter_filament/lib/filament/widgets/filament_widget.dart +++ b/flutter_filament_federated/flutter_filament/lib/filament/widgets/filament_widget.dart @@ -28,7 +28,6 @@ class FilamentWidget extends StatefulWidget { class _FilamentWidgetState extends State { FlutterFilamentTexture? _texture; - late final AppLifecycleListener _appLifecycleListener; Rect get _rect { final renderBox = (context.findRenderObject()) as RenderBox; @@ -45,10 +44,10 @@ class _FilamentWidgetState extends State { var width = (dpr * size.width).ceil(); var height = (dpr * size.height).ceil(); _texture = await widget.plugin.createTexture(width, height, 0, 0); - _appLifecycleListener = AppLifecycleListener( - onStateChange: _handleStateChange, - ); - setState(() {}); + + if (mounted) { + setState(() {}); + } }); super.initState(); } @@ -59,52 +58,10 @@ class _FilamentWidgetState extends State { widget.plugin.destroyTexture(_texture!); } - _appLifecycleListener.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; Future _resizeTexture(Size newSize) async { @@ -130,7 +87,8 @@ class _FilamentWidgetState extends State { @override Widget build(BuildContext context) { 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(