From dbbb4d71233f597acb7b3619a53e0d9d3c917c35 Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Tue, 22 Oct 2024 12:48:26 +0800 Subject: [PATCH] fix: add listener in ThermionFlutterTextureBackedPlatform to unset viewer on dispose --- ...rmion_flutter_texture_backed_platform.dart | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/thermion_flutter/thermion_flutter_ffi/lib/thermion_flutter_texture_backed_platform.dart b/thermion_flutter/thermion_flutter_ffi/lib/thermion_flutter_texture_backed_platform.dart index 17a4ca93..a28784e0 100644 --- a/thermion_flutter/thermion_flutter_ffi/lib/thermion_flutter_texture_backed_platform.dart +++ b/thermion_flutter/thermion_flutter_ffi/lib/thermion_flutter_texture_backed_platform.dart @@ -14,12 +14,11 @@ import 'platform_texture.dart'; /// /// An implementation of [ThermionFlutterPlatform] that uses /// Flutter platform channels to create a rendering context, -/// resource loaders, and a texture that will be used as a render target +/// resource loaders, and a texture that will be used as a render target /// for a headless swapchain. /// class ThermionFlutterTextureBackedPlatform extends ThermionFlutterMethodChannelInterface { - final _logger = Logger("ThermionFlutterTextureBackedPlatform"); static SwapChain? _swapChain; @@ -46,20 +45,27 @@ class ThermionFlutterTextureBackedPlatform if (Platform.isMacOS || Platform.isIOS) { _swapChain = await viewer.createHeadlessSwapChain(1, 1); } + + viewer.onDispose(() async { + _swapChain = null; + }); + return viewer; } // On desktop platforms, textures are always created - Future createTexture(t.View view, int width, int height) async { - var texture = FlutterPlatformTexture(channel, viewer!, view, (Platform.isMacOS || Platform.isIOS)? _swapChain : null); + Future createTexture( + t.View view, int width, int height) async { + var texture = FlutterPlatformTexture(channel, viewer!, view, + (Platform.isMacOS || Platform.isIOS) ? _swapChain : null); await texture.resize(width, height, 0, 0); return texture; } - + @override - Future createWindow(int width, int height, int offsetLeft, int offsetTop) { + Future createWindow( + int width, int height, int offsetLeft, int offsetTop) { // TODO: implement createWindow throw UnimplementedError(); } } -