diff --git a/thermion_flutter/thermion_flutter/lib/src/widgets/src/thermion_widget.dart b/thermion_flutter/thermion_flutter/lib/src/widgets/src/thermion_widget.dart index c7626476..6051a76b 100644 --- a/thermion_flutter/thermion_flutter/lib/src/widgets/src/thermion_widget.dart +++ b/thermion_flutter/thermion_flutter/lib/src/widgets/src/thermion_widget.dart @@ -3,7 +3,6 @@ import 'package:flutter/material.dart' hide View; import 'package:thermion_flutter/src/widgets/src/thermion_texture_widget.dart'; import 'package:thermion_flutter/src/widgets/src/thermion_widget_web.dart'; import 'package:thermion_flutter/thermion_flutter.dart'; -import 'package:thermion_flutter_web/thermion_flutter_web_options.dart'; Future kDefaultResizeCallback(Size size, View view, double pixelRatio) async { var camera = await view.getCamera(); @@ -67,8 +66,10 @@ class _ThermionWidgetState extends State { @override Widget build(BuildContext context) { if (kIsWeb) { + var options = ThermionFlutterPlatform.instance.options as ThermionFlutterWebOptions; return ThermionWidgetWeb( - viewer: widget.viewer, options: const ThermionFlutterWebOptions(importCanvasAsWidget: true)); + viewer: widget.viewer, + options: options); } return ThermionTextureWidget( diff --git a/thermion_flutter/thermion_flutter/lib/src/widgets/src/thermion_widget_web_impl.dart b/thermion_flutter/thermion_flutter/lib/src/widgets/src/thermion_widget_web_impl.dart index f13d63f6..a3f41df1 100644 --- a/thermion_flutter/thermion_flutter/lib/src/widgets/src/thermion_widget_web_impl.dart +++ b/thermion_flutter/thermion_flutter/lib/src/widgets/src/thermion_widget_web_impl.dart @@ -1,13 +1,11 @@ import 'dart:async'; import 'dart:ui' as ui; import 'dart:ui_web' as ui_web; +import 'package:flutter/material.dart'; import 'package:logging/logging.dart'; import 'package:thermion_flutter/thermion_flutter.dart'; import 'package:thermion_flutter_web/thermion_flutter_web.dart'; -import 'package:thermion_flutter_web/thermion_flutter_web_options.dart'; import 'package:web/web.dart' as web; -import 'package:flutter/widgets.dart'; - import 'resize_observer.dart'; class ThermionWidgetWeb extends StatefulWidget { @@ -52,15 +50,22 @@ class _ThermionWidgetWebState extends State { }); } + void _resize(Size oldSize, Size newSize) async { + var width = newSize.width.toInt(); + var height = newSize.height.toInt(); + ThermionFlutterWebPlugin.instance + .resizeCanvas(newSize.width, newSize.height); + await widget.viewer.setViewport(width, height); + } + @override Widget build(BuildContext context) { - if (widget.options.importCanvasAsWidget) { - return _ImageCopyingWidget(viewer: widget.viewer); - // return _PlatformView( - // viewer: widget.viewer, - // ); - } - return Container(color: const Color(0x00000000)); + return ResizeObserver( + onResized: _resize, + child: widget.options.importCanvasAsWidget + ? _ImageCopyingWidget(viewer: widget.viewer) + : SizedBox.expand( + child: Container(color: const Color(0x00000000)))); } } @@ -73,42 +78,27 @@ class _PlatformView extends StatefulWidget { } class _PlatformViewState extends State<_PlatformView> { + void initState() { super.initState(); ui_web.platformViewRegistry.registerViewFactory( 'imported-canvas', (int viewId, {Object? params}) { var canvas = web.document.getElementById("thermion_canvas"); - WidgetsBinding.instance.addPostFrameCallback((_) { - var renderBox = this.context.findRenderObject() as RenderBox?; - - _resize(Size(0, 0), renderBox!.size); - }); - return canvas! as Object; }, ); } - void _resize(Size oldSize, Size newSize) { - var width = newSize.width.toInt(); - var height = newSize.height.toInt(); - ThermionFlutterWebPlugin.instance - .resizeCanvas(newSize.width, newSize.height); - widget.viewer.setViewport(width, height); - } - @override Widget build(BuildContext context) { - return ResizeObserver( - onResized: _resize, - child: HtmlElementView( + return HtmlElementView( viewType: 'imported-canvas', onPlatformViewCreated: (i) {}, creationParams: { 'key': 'someValue', }, - )); + ); } } @@ -142,17 +132,27 @@ class _ImageCopyingWidgetState extends State<_ImageCopyingWidget> { try { final rb = this.context.findRenderObject() as RenderBox?; - if (_resizing || rb == null || rb.size.isEmpty) { + if (rb == null) { setState(() {}); return; } + if (rb.size.isEmpty) { + setState(() {}); + return; + } + + // if (_resizing) { + // setState(() {}); + // return; + // } + if (canvas.width != rb.size.width || canvas.height != rb.size.height) { - ThermionFlutterWebPlugin.instance - .resizeCanvas(rb.size.width, rb.size.height); - await widget.viewer - .setViewport(rb.size.width.ceil(), rb.size.height.ceil()) - .timeout(Duration(seconds: 1)); + // ThermionFlutterWebPlugin.instance + // .resizeCanvas(rb.size.width, rb.size.height); + // await widget.viewer + // .setViewport(rb.size.width.ceil(), rb.size.height.ceil()) + // .timeout(Duration(seconds: 1)); } width = canvas.width * web.window.devicePixelRatio; @@ -173,16 +173,16 @@ class _ImageCopyingWidgetState extends State<_ImageCopyingWidget> { int _request = 0; - bool _resizing = false; - Timer? _resizeTimer; + // bool _resizing = false; + // Timer? _resizeTimer; - void _resize(Size oldSize, Size newSize) { - _resizeTimer?.cancel(); - _resizing = true; - _resizeTimer = Timer(Duration(milliseconds: 100), () { - _resizing = false; - }); - } + // void _resize(Size oldSize, Size newSize) { + // _resizeTimer?.cancel(); + // _resizing = true; + // _resizeTimer = Timer(Duration(milliseconds: 100), () { + // _resizing = false; + // }); + // } @override Widget build(BuildContext context) { @@ -190,15 +190,13 @@ class _ImageCopyingWidgetState extends State<_ImageCopyingWidget> { return Container(); } - return ResizeObserver( - onResized: _resize, - child: RawImage( - key: Key(_request.toString()), - width: width, - height: height, - image: _img!, - filterQuality: FilterQuality.high, - isAntiAlias: false, - )); + return RawImage( + key: Key(_request.toString()), + width: width, + height: height, + image: _img!, + filterQuality: FilterQuality.high, + isAntiAlias: false, + ); } } diff --git a/thermion_flutter/thermion_flutter/lib/src/widgets/src/thermion_widget_web_stub.dart b/thermion_flutter/thermion_flutter/lib/src/widgets/src/thermion_widget_web_stub.dart index 6c1c109d..38413f81 100644 --- a/thermion_flutter/thermion_flutter/lib/src/widgets/src/thermion_widget_web_stub.dart +++ b/thermion_flutter/thermion_flutter/lib/src/widgets/src/thermion_widget_web_stub.dart @@ -1,7 +1,6 @@ import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; import 'package:thermion_flutter/thermion_flutter.dart'; -import 'package:thermion_flutter_web/thermion_flutter_web_options.dart'; class ThermionWidgetWeb extends StatelessWidget { final ThermionFlutterWebOptions? options; diff --git a/thermion_flutter/thermion_flutter_platform_interface/lib/thermion_flutter_platform_interface.dart b/thermion_flutter/thermion_flutter_platform_interface/lib/thermion_flutter_platform_interface.dart index abdcf41d..c1661467 100644 --- a/thermion_flutter/thermion_flutter_platform_interface/lib/thermion_flutter_platform_interface.dart +++ b/thermion_flutter/thermion_flutter_platform_interface/lib/thermion_flutter_platform_interface.dart @@ -12,6 +12,20 @@ class ThermionFlutterOptions { {this.uberarchivePath = null, this.backend = null}); } +class ThermionFlutterWebOptions extends ThermionFlutterOptions { + + final bool createCanvas; + final bool importCanvasAsWidget; + + const ThermionFlutterWebOptions( + {this.importCanvasAsWidget = false, + this.createCanvas = true, + String? uberarchivePath}) + : super(uberarchivePath: uberarchivePath); + + +} + abstract class ThermionFlutterPlatform extends PlatformInterface { ThermionFlutterPlatform() : super(token: _token); diff --git a/thermion_flutter/thermion_flutter_web/lib/thermion_flutter_web.dart b/thermion_flutter/thermion_flutter_web/lib/thermion_flutter_web.dart index 3b0c9c21..7263b310 100644 --- a/thermion_flutter/thermion_flutter_web/lib/thermion_flutter_web.dart +++ b/thermion_flutter/thermion_flutter_web/lib/thermion_flutter_web.dart @@ -6,9 +6,7 @@ import 'package:flutter/services.dart'; import 'package:thermion_dart/thermion_dart.dart'; import 'package:thermion_dart/src/filament/src/implementation/ffi_filament_app.dart'; import 'package:thermion_flutter_platform_interface/thermion_flutter_platform_interface.dart'; - import 'package:flutter_web_plugins/flutter_web_plugins.dart'; -import 'package:thermion_flutter_web/thermion_flutter_web_options.dart'; import 'package:web/web.dart'; class ThermionFlutterWebPlugin extends ThermionFlutterPlatform { diff --git a/thermion_flutter/thermion_flutter_web/lib/thermion_flutter_web_options.dart b/thermion_flutter/thermion_flutter_web/lib/thermion_flutter_web_options.dart deleted file mode 100644 index 2cc6d07c..00000000 --- a/thermion_flutter/thermion_flutter_web/lib/thermion_flutter_web_options.dart +++ /dev/null @@ -1,15 +0,0 @@ -import 'package:thermion_flutter_platform_interface/thermion_flutter_platform_interface.dart'; - -class ThermionFlutterWebOptions extends ThermionFlutterOptions { - - final bool createCanvas; - final bool importCanvasAsWidget; - - const ThermionFlutterWebOptions( - {this.importCanvasAsWidget = false, - this.createCanvas = true, - String? uberarchivePath}) - : super(uberarchivePath: uberarchivePath); - - -}