make ThermionFlutterPlugin static, remove dispose() and add internal listener for ThermionViewer.onDispose

This commit is contained in:
Nick Fisher
2024-06-19 12:57:16 +08:00
parent 8e75555540
commit 31e68df1c5
2 changed files with 90 additions and 89 deletions

View File

@@ -1,103 +1,100 @@
import 'dart:async';
import 'dart:ui';
import 'package:thermion_dart/thermion_dart.dart';
import 'package:flutter/widgets.dart';
import 'package:thermion_flutter_platform_interface/thermion_flutter_platform_interface.dart';
import 'package:thermion_flutter_platform_interface/thermion_flutter_texture.dart';
///
/// Handles all platform-specific initialization work necessary to create a
/// backing rendering surface in a Flutter application.
/// Instantiates/wraps a [ThermionViewer],
/// Handles all platform-specific initialization to create a backing rendering
/// surface in a Flutter application and lifecycle listeners to pause rendering
/// when the app is inactive or in the background.
/// Call [createViewer] to create an instance of [ThermionViewer].
/// This is a lightweight singleton that
///
class ThermionFlutterPlugin {
ThermionViewer get _viewer => ThermionFlutterPlatform.instance.viewer;
ThermionFlutterPlugin._();
bool _wasRenderingOnInactive = false;
static AppLifecycleListener? _appLifecycleListener;
void _handleStateChange(AppLifecycleState state) async {
await initialized;
static bool _initializing = false;
static ThermionViewer? _viewer;
static bool _wasRenderingOnInactive = false;
static void _handleStateChange(AppLifecycleState state) async {
if (_viewer == null) {
return;
}
await _viewer!.initialized;
switch (state) {
case AppLifecycleState.detached:
print("Detached");
if (!_wasRenderingOnInactive) {
_wasRenderingOnInactive = _viewer.rendering;
_wasRenderingOnInactive = _viewer!.rendering;
}
await _viewer.setRendering(false);
await _viewer!.setRendering(false);
break;
case AppLifecycleState.hidden:
print("Hidden");
if (!_wasRenderingOnInactive) {
_wasRenderingOnInactive = _viewer.rendering;
_wasRenderingOnInactive = _viewer!.rendering;
}
await _viewer.setRendering(false);
await _viewer!.setRendering(false);
break;
case AppLifecycleState.inactive:
print("Inactive");
if (!_wasRenderingOnInactive) {
_wasRenderingOnInactive = _viewer.rendering;
_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);
await _viewer!.setRendering(false);
break;
case AppLifecycleState.paused:
print("Paused");
if (!_wasRenderingOnInactive) {
_wasRenderingOnInactive = _viewer.rendering;
_wasRenderingOnInactive = _viewer!.rendering;
}
await _viewer.setRendering(false);
await _viewer!.setRendering(false);
break;
case AppLifecycleState.resumed:
print("Resumed");
await _viewer.setRendering(_wasRenderingOnInactive);
await _viewer!.setRendering(_wasRenderingOnInactive);
break;
}
}
AppLifecycleListener? _appLifecycleListener;
final _initialized = Completer<bool>();
Future<bool> get initialized => _initialized.future;
bool _initializing = false;
Future<ThermionViewer> initialize({String? uberArchivePath}) async {
_initializing = true;
if (_initialized.isCompleted) {
return ThermionFlutterPlatform.instance.viewer;
static Future<ThermionViewer> createViewer({String? uberArchivePath}) async {
if (_initializing) {
throw Exception("Existing call to createViewer has not completed.");
}
await ThermionFlutterPlatform.instance
.initialize(uberArchivePath: uberArchivePath);
_initializing = true;
_viewer = await ThermionFlutterPlatform.instance
.createViewer(uberArchivePath: uberArchivePath);
_appLifecycleListener = AppLifecycleListener(
onStateChange: _handleStateChange,
);
_viewer.initialized;
_initialized.complete(true);
_viewer!.onDispose(() async {
_viewer = null;
_appLifecycleListener?.dispose();
_appLifecycleListener = null;
});
_initializing = false;
return ThermionFlutterPlatform.instance.viewer;
return _viewer!;
}
Future<ThermionFlutterTexture?> createTexture(
static Future<ThermionFlutterTexture?> createTexture(
int width, int height, int offsetLeft, int offsetRight) async {
return ThermionFlutterPlatform.instance
.createTexture(width, height, offsetLeft, offsetRight);
}
Future destroyTexture(ThermionFlutterTexture texture) async {
static Future destroyTexture(ThermionFlutterTexture texture) async {
return ThermionFlutterPlatform.instance.destroyTexture(texture);
}
@override
Future<ThermionFlutterTexture?> resizeTexture(ThermionFlutterTexture texture,
static Future<ThermionFlutterTexture?> resizeTexture(ThermionFlutterTexture texture,
int width, int height, int offsetLeft, int offsetRight) async {
return ThermionFlutterPlatform.instance
.resizeTexture(texture, width, height, offsetLeft, offsetRight);
}
void dispose() {
ThermionFlutterPlatform.instance.dispose();
_appLifecycleListener?.dispose();
}
}