fix incorrect pixelDeviceRatio

This commit is contained in:
Nick Fisher
2023-11-16 12:09:50 +08:00
parent f2a458b9ca
commit e67d4d7e1f
2 changed files with 70 additions and 80 deletions

View File

@@ -130,8 +130,11 @@ class FilamentControllerFFI extends FilamentController {
@override @override
Future setDimensions(Rect rect, double pixelRatio) async { Future setDimensions(Rect rect, double pixelRatio) async {
this.rect.value = Rect.fromLTWH(rect.left, rect.top, this.rect.value = Rect.fromLTWH(
rect.width * _pixelRatio, rect.height * _pixelRatio); (rect.left * _pixelRatio).floor().toDouble(),
rect.top * _pixelRatio.floor().toDouble(),
(rect.width * _pixelRatio).ceil().toDouble(),
(rect.height * _pixelRatio).ceil().toDouble());
_pixelRatio = pixelRatio; _pixelRatio = pixelRatio;
} }
@@ -318,10 +321,11 @@ class FilamentControllerFFI extends FilamentController {
throw Exception("Cannot resize without active viewer"); throw Exception("Cannot resize without active viewer");
} }
if (_resizing) { while (_resizing) {
throw Exception("Resize currently underway, ignoring"); await Future.delayed(Duration(milliseconds: 100));
} }
try {
_resizing = true; _resizing = true;
set_rendering_ffi(_viewer!, false); set_rendering_ffi(_viewer!, false);
@@ -374,9 +378,10 @@ class FilamentControllerFFI extends FilamentController {
_viewer!, rect.value!.width.toInt(), rect.value!.height.toInt(), 1.0); _viewer!, rect.value!.width.toInt(), rect.value!.height.toInt(), 1.0);
await setRendering(_rendering); await setRendering(_rendering);
} finally {
_resizing = false; _resizing = false;
} }
}
@override @override
Future clearBackgroundImage() async { Future clearBackgroundImage() async {

View File

@@ -125,8 +125,6 @@ class _SizedFilamentWidgetState extends State<_SizedFilamentWidget> {
late final AppLifecycleListener _appLifecycleListener; late final AppLifecycleListener _appLifecycleListener;
late double _pixelRatio;
Rect get _rect { Rect get _rect {
final renderBox = (context.findRenderObject()) as RenderBox; final renderBox = (context.findRenderObject()) as RenderBox;
final size = renderBox.size; final size = renderBox.size;
@@ -142,23 +140,21 @@ class _SizedFilamentWidgetState extends State<_SizedFilamentWidget> {
WidgetsBinding.instance.addPostFrameCallback((timeStamp) async { WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
try { try {
_pixelRatio = MediaQuery.of(context).devicePixelRatio; widget.controller.setDimensions(_rect, MediaQuery.of(context).devicePixelRatio);
widget.controller.setDimensions(_rect, _pixelRatio);
} catch (err) { } catch (err) {
dev.log("Fatal error : $err"); dev.log("Fatal error : $err");
_error = err.toString(); _error = err.toString();
} }
setState(() {});
}); });
super.initState(); super.initState();
} }
Timer? _resizeTimer; Timer? _resizeTimer;
bool _resizing = false; bool _resizing = false;
Future _resize() { Future _resize() async {
dev.log("Resizing widget");
final completer = Completer(); final completer = Completer();
// resizing the window can be sluggish (particular in debug mode), exacerbated when simultaneously recreating the swapchain and resize the window. // resizing the window can be sluggish (particular in debug mode), exacerbated when simultaneously recreating the swapchain and resize the window.
// to address this, whenever the widget is resized, we set a timer for Xms in the future. // to address this, whenever the widget is resized, we set a timer for Xms in the future.
@@ -166,21 +162,13 @@ class _SizedFilamentWidgetState extends State<_SizedFilamentWidget> {
// any subsequent widget resizes will cancel the timer and replace with a new one. // any subsequent widget resizes will cancel the timer and replace with a new one.
// debug mode does need a longer timeout. // debug mode does need a longer timeout.
_resizeTimer?.cancel(); _resizeTimer?.cancel();
_resizeTimer = Timer(Duration(milliseconds: (kReleaseMode || Platform.isWindows) ? 10 : 100), () async {
_resizeTimer = Timer(
Duration(milliseconds: (kReleaseMode || Platform.isWindows) ? 10 : 100),
() async {
if (!mounted) {
completer.complete();
return;
}
try { try {
while(_resizing) { while(_resizing) {
await Future.delayed(const Duration(milliseconds: 20)); await Future.delayed(const Duration(milliseconds: 20));
} }
_resizing = true; _resizing = true;
await widget.controller.setDimensions(_rect, _pixelRatio); await widget.controller.setDimensions(_rect, MediaQuery.of(context).devicePixelRatio);
await widget.controller.resize(); await widget.controller.resize();
_resizeTimer = null; _resizeTimer = null;
setState(() {}); setState(() {});
@@ -191,18 +179,15 @@ class _SizedFilamentWidgetState extends State<_SizedFilamentWidget> {
completer.complete(); completer.complete();
} }
}); });
return completer.future; return completer.future;
} }
@override @override
void didUpdateWidget(_SizedFilamentWidget oldWidget) { void didUpdateWidget(_SizedFilamentWidget oldWidget) {
super.didUpdateWidget(oldWidget); super.didUpdateWidget(oldWidget);
if (oldWidget.height != widget.height || oldWidget.width != widget.width) {
_resize(); _resize();
} }
}
@override @override
void dispose() { void dispose() {