From add9623d9ebca3fcb3bc1dba93331cc1c4474938 Mon Sep 17 00:00:00 2001 From: LiJianying Date: Sun, 22 Nov 2026 07:25:36 +0800 Subject: [PATCH 1/2] Fix partial legacy geometry updates overwriting pending requests --- lib/src/legacy/window_manager.dart | 44 ++++++++--- test/legacy_geometry_test.dart | 116 +++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+), 12 deletions(-) create mode 100644 test/legacy_geometry_test.dart diff --git a/lib/src/legacy/window_manager.dart b/lib/src/legacy/window_manager.dart index 73396ab9..1b8645e6 100644 --- a/lib/src/legacy/window_manager.dart +++ b/lib/src/legacy/window_manager.dart @@ -4,7 +4,8 @@ import 'dart:async'; import 'dart:io'; import 'dart:ui' show Brightness, Color, Offset, PlatformDispatcher, Rect, Size; -import 'package:flutter/foundation.dart' show ObserverList, VoidCallback; +import 'package:flutter/foundation.dart' + show ObserverList, VoidCallback, visibleForTesting; import 'package:flutter/painting.dart' show Alignment; import 'package:nativeapi/nativeapi.dart' as nativeapi; import 'package:nativeapi_flutter/nativeapi_flutter.dart' @@ -80,6 +81,13 @@ class WindowManager { ); } + @visibleForTesting + void debugBindWindow(nativeapi.Window? window) { + _unwireNativeEvents(); + _window = window; + if (_listeners.isNotEmpty || _isPreventClose) _wireNativeEvents(); + } + List get listeners { return List.from(_listeners); } @@ -397,17 +405,29 @@ class WindowManager { final window = _target; if (window == null) return; if (bounds == null && position == null && size == null) return; - final current = window.bounds.toRect(); - final target = - bounds ?? - Rect.fromLTWH( - position?.dx ?? current.left, - position?.dy ?? current.top, - size?.width ?? current.width, - size?.height ?? current.height, - ); - if (!animate || !window.animateBounds(target.toNative())) { - window.bounds = target.toNative(); + if (animate) { + final current = window.bounds.toRect(); + final target = + bounds ?? + Rect.fromLTWH( + position?.dx ?? current.left, + position?.dy ?? current.top, + size?.width ?? current.width, + size?.height ?? current.height, + ); + if (window.animateBounds(target.toNative())) return; + } + // The window manager can apply geometry asynchronously. Writing a whole + // frame for a partial update would undo a pending resize or move with the + // stale values returned by window.bounds, including animation fallbacks. + if (bounds != null) { + window.bounds = bounds.toNative(); + } else if (position != null && size != null) { + window.bounds = (position & size).toNative(); + } else if (position != null) { + window.position = position.toNative(); + } else if (size != null) { + window.setSize(size.toNative(), false); } } diff --git a/test/legacy_geometry_test.dart b/test/legacy_geometry_test.dart new file mode 100644 index 00000000..0eb7f6dc --- /dev/null +++ b/test/legacy_geometry_test.dart @@ -0,0 +1,116 @@ +// ignore_for_file: deprecated_member_use + +import 'dart:ui'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:nativeapi/nativeapi.dart' as nativeapi; +import 'package:window_manager/legacy.dart'; + +// Like an X11 window manager, keep reporting the old geometry until the +// outstanding requests have been processed. A bounds write replaces both. +class _DeferredWindow extends Fake implements nativeapi.Window { + nativeapi.Rectangle reported = const nativeapi.Rectangle( + x: 100, + y: 100, + width: 1280, + height: 720, + ); + late nativeapi.Rectangle requested = reported; + bool supportsAnimation = false; + int boundsReads = 0; + + @override + nativeapi.Rectangle get bounds { + boundsReads++; + return reported; + } + + @override + set bounds(nativeapi.Rectangle value) => requested = value; + + @override + set position(nativeapi.Point value) { + requested = nativeapi.Rectangle( + x: value.x, + y: value.y, + width: requested.width, + height: requested.height, + ); + } + + @override + void setSize(nativeapi.Size value, bool animate) { + requested = nativeapi.Rectangle( + x: requested.x, + y: requested.y, + width: value.width, + height: value.height, + ); + } + + @override + bool animateBounds(nativeapi.Rectangle value) { + if (supportsAnimation) requested = value; + return supportsAnimation; + } +} + +void main() { + late _DeferredWindow window; + + setUp(() { + window = _DeferredWindow(); + windowManager.debugBindWindow(window); + }); + + tearDown(() => windowManager.debugBindWindow(null)); + + test( + 'moving preserves a resize that the window manager has not applied', + () async { + await windowManager.setSize(const Size(1920, 1080)); + await windowManager.setPosition(Offset.zero); + + expect(window.requested.width, 1920); + expect(window.requested.height, 1080); + expect(window.requested.x, 0); + expect(window.requested.y, 0); + expect(window.boundsReads, 0); + }, + ); + + test('size-only bounds preserve an outstanding position request', () async { + await windowManager.setPosition(const Offset(300, 400)); + await windowManager.setBounds(null, size: const Size(800, 600)); + + expect(window.requested.x, 300); + expect(window.requested.y, 400); + expect(window.requested.width, 800); + expect(window.requested.height, 600); + }); + + test('unsupported position animation preserves a pending resize', () async { + await windowManager.setSize(const Size(1920, 1080)); + await windowManager.setPosition(Offset.zero, animate: true); + + expect(window.requested.width, 1920); + expect(window.requested.height, 1080); + expect(window.requested.x, 0); + expect(window.requested.y, 0); + }); + + test( + 'full bounds and supported native animation still update the frame', + () async { + await windowManager.setBounds(const Rect.fromLTWH(20, 30, 800, 600)); + expect(window.requested.x, 20); + expect(window.requested.width, 800); + + window.supportsAnimation = true; + await windowManager.setPosition(const Offset(40, 50), animate: true); + expect(window.requested.x, 40); + expect(window.requested.y, 50); + expect(window.requested.width, window.reported.width); + }, + ); +} From 2ec666f97e48c1ebc99eeaa9dbd8923dfdab6fad Mon Sep 17 00:00:00 2001 From: LiJianying Date: Sun, 22 Nov 2026 07:25:46 +0800 Subject: [PATCH 2/2] Document legacy geometry fix --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ae3b291b..8e86f6c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## Unreleased +* Preserve pending geometry changes when legacy position-only or size-only calls + follow them, including when native frame animation is unavailable. * Document the RGBA visual required for transparent hidden-title-bar shadows on Linux X11/XWayland and select it in the example runner before realization (#594).