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

View File

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