From 94deb03508358822c968ff343700134939128b2c Mon Sep 17 00:00:00 2001 From: mockturtl Date: Mon, 16 Nov 2026 22:48:39 -0500 Subject: [PATCH 1/2] fix(legacy): do not cancel a pending setSize in setPosition on Linux setPosition called setBounds(null, position: ...). setBounds reads window.bounds, replaces the position and writes the whole rectangle back. After a setSize call, the window manager applies the new size a few milliseconds later. Until then, window.bounds returns the old size, so setBounds wrote the old size back and cancelled the new one. On Linux (X11), this broke setAlignment, setPosition and center after setSize. When animate is false, move only the position. The animated path does not change. Add WindowManager.debugTarget (visible for testing), so that a test can replace the native window with a fake. Add tests for setPosition. With the old code, the three tests for animate false fail. With the fix, all five pass. debugTarget is a temporary test hook. The open PR leanflutter/window_manager#607 adds debugBindWindow(window) for the same job. When that PR merges, the tests will use debugBindWindow and debugTarget will go away. The open PR leanflutter/window_manager#606 also changes the _target getter, so a conflict with debugTarget is likely. Checked by hand on Linux (X11, Cinnamon 6.6.9 with Muffin 6.6.3). Not tested on macOS, Windows or Wayland. Refs libnativeapi/nativeapi#37 Co-Authored-By: Claude Sonnet 5.5 --- CHANGELOG.md | 4 + lib/src/legacy/window_manager.dart | 17 +++- test/legacy_set_position_test.dart | 150 +++++++++++++++++++++++++++++ 3 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 test/legacy_set_position_test.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index ae3b291b..1a9904cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ * 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). +* Fix the legacy `setPosition`, and with it `setAlignment` and `center`, on Linux X11. + A move right after `setSize` no longer cancels the new size: `setPosition` does not + write the old window size back with the new position. When `animate` is false, it + now moves only the position ([libnativeapi/nativeapi#37](https://github.com/libnativeapi/nativeapi/issues/37)). ## 0.7.0 diff --git a/lib/src/legacy/window_manager.dart b/lib/src/legacy/window_manager.dart index 73396ab9..d1e286a2 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,11 @@ class WindowManager { ); } + /// Replaces the window that every call addresses. Tests use it to run the + /// calls without the native library. Pass null to resolve the window again. + @visibleForTesting + set debugTarget(nativeapi.Window? window) => _window = window; + List get listeners { return List.from(_listeners); } @@ -428,6 +434,15 @@ class WindowManager { /// Moves window to position, with a native animation on macOS if requested. Future setPosition(Offset position, {bool animate = false}) async { + final window = _target; + if (window == null) return; + if (!animate) { + // Move only. `setBounds` reads the current size and writes it back with + // the new position, which cancels a `setSize` that the window manager has + // not applied yet. + window.position = position.toNative(); + return; + } await setBounds(null, position: position, animate: animate); } diff --git a/test/legacy_set_position_test.dart b/test/legacy_set_position_test.dart new file mode 100644 index 00000000..dff338bb --- /dev/null +++ b/test/legacy_set_position_test.dart @@ -0,0 +1,150 @@ +// Tests for `WindowManager.setPosition` of package:window_manager/legacy.dart. +// +// The native library is not available in a unit test. A fake window replaces +// it. The fake applies a size request later, like a window manager on Linux +// (X11): until then `bounds` returns the old size. +// ignore_for_file: deprecated_member_use, deprecated_member_use_from_same_package + +import 'dart:ui' show Offset, Size; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:nativeapi/nativeapi.dart' as nativeapi; +import 'package:window_manager/legacy.dart'; + +class _FakeWindow implements nativeapi.Window { + /// The size that the window manager applied. `bounds` returns this size. + final nativeapi.Size appliedSize = const nativeapi.Size( + width: 1280, + height: 720, + ); + + /// The size that the window gets when the window manager applies the last + /// request. A size request and a write to `bounds` both change it. + nativeapi.Size? requestedSize; + + nativeapi.Point? lastPosition; + nativeapi.Rectangle? lastBoundsWrite; + nativeapi.Rectangle? lastAnimatedBounds; + bool animationSupported = false; + + /// The native calls, in the order of the calls. + final List calls = []; + + @override + nativeapi.Rectangle get bounds { + calls.add('get bounds'); + return nativeapi.Rectangle( + x: 0, + y: 0, + width: appliedSize.width, + height: appliedSize.height, + ); + } + + @override + set bounds(nativeapi.Rectangle value) { + calls.add('set bounds'); + lastBoundsWrite = value; + requestedSize = nativeapi.Size(width: value.width, height: value.height); + } + + @override + bool animateBounds(nativeapi.Rectangle bounds) { + calls.add('animate bounds'); + if (!animationSupported) return false; + lastAnimatedBounds = bounds; + return true; + } + + @override + set position(nativeapi.Point value) { + calls.add('set position'); + lastPosition = value; + } + + @override + void setSize(nativeapi.Size size, bool animate) { + calls.add('set size'); + requestedSize = size; + } + + @override + dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); +} + +void main() { + late _FakeWindow window; + + setUp(() { + window = _FakeWindow(); + windowManager.debugTarget = window; + }); + + tearDown(() { + windowManager.debugTarget = null; + }); + + group('WindowManager.setPosition', () { + group('with animate false', () { + test('moves the window and does not read or write the bounds', () async { + await windowManager.setPosition(const Offset(10, 20)); + + expect(window.calls, ['set position']); + expect(window.lastPosition, const nativeapi.Point(x: 10, y: 20)); + }); + + test('does not cancel a size request that is not applied yet', () async { + await windowManager.setSize(const Size(1920, 1080)); + await windowManager.setPosition(Offset.zero); + + expect( + window.requestedSize, + const nativeapi.Size(width: 1920, height: 1080), + ); + expect(window.lastBoundsWrite, isNull); + }); + + test('keeps the size request after several moves', () async { + await windowManager.setSize(const Size(1920, 1080)); + await windowManager.setPosition(const Offset(1, 2)); + await windowManager.setPosition(const Offset(3, 4)); + + expect( + window.requestedSize, + const nativeapi.Size(width: 1920, height: 1080), + ); + expect(window.lastPosition, const nativeapi.Point(x: 3, y: 4)); + }); + }); + + group('with animate true', () { + test('animates to the new position with the current size', () async { + window.animationSupported = true; + + await windowManager.setPosition(const Offset(5, 6), animate: true); + + expect(window.calls, ['get bounds', 'animate bounds']); + final animated = window.lastAnimatedBounds!; + expect(animated.x, 5); + expect(animated.y, 6); + expect(animated.width, 1280); + expect(animated.height, 720); + }); + + test('writes the bounds when the platform has no animation', () async { + await windowManager.setPosition(const Offset(5, 6), animate: true); + + expect(window.calls, [ + 'get bounds', + 'animate bounds', + 'set bounds', + ]); + final written = window.lastBoundsWrite!; + expect(written.x, 5); + expect(written.y, 6); + expect(written.width, 1280); + expect(written.height, 720); + }); + }); + }); +} From c2df316d5aa1fb64cf225242e704498fb4407267 Mon Sep 17 00:00:00 2001 From: mockturtl Date: Mon, 16 Nov 2026 23:09:40 -0500 Subject: [PATCH 2/2] test(legacy): use debugBindWindow in the setPosition tests Replace the temporary WindowManager.debugTarget setter with debugBindWindow(window). The open PR leanflutter/window_manager#607 adds debugBindWindow for the same job. This commit needs #607. Without #607, the tests do not compile. Merge #607 before this commit. The previous commit stands alone, and its tests pass with debugTarget. The tests check the same behavior as before. I ran them with #607 applied on top of this branch. The five tests of legacy_set_position_test.dart pass, and so do the five tests of #607. With setPosition reverted, exactly the three tests for animate false fail. Tested on Linux (X11) only. Refs libnativeapi/nativeapi#37 Co-Authored-By: Claude Sonnet 5.5 --- lib/src/legacy/window_manager.dart | 8 +------- test/legacy_set_position_test.dart | 4 ++-- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/lib/src/legacy/window_manager.dart b/lib/src/legacy/window_manager.dart index d1e286a2..950ab1ef 100644 --- a/lib/src/legacy/window_manager.dart +++ b/lib/src/legacy/window_manager.dart @@ -4,8 +4,7 @@ import 'dart:async'; import 'dart:io'; import 'dart:ui' show Brightness, Color, Offset, PlatformDispatcher, Rect, Size; -import 'package:flutter/foundation.dart' - show ObserverList, VoidCallback, visibleForTesting; +import 'package:flutter/foundation.dart' show ObserverList, VoidCallback; import 'package:flutter/painting.dart' show Alignment; import 'package:nativeapi/nativeapi.dart' as nativeapi; import 'package:nativeapi_flutter/nativeapi_flutter.dart' @@ -81,11 +80,6 @@ class WindowManager { ); } - /// Replaces the window that every call addresses. Tests use it to run the - /// calls without the native library. Pass null to resolve the window again. - @visibleForTesting - set debugTarget(nativeapi.Window? window) => _window = window; - List get listeners { return List.from(_listeners); } diff --git a/test/legacy_set_position_test.dart b/test/legacy_set_position_test.dart index dff338bb..2bfef9b2 100644 --- a/test/legacy_set_position_test.dart +++ b/test/legacy_set_position_test.dart @@ -77,11 +77,11 @@ void main() { setUp(() { window = _FakeWindow(); - windowManager.debugTarget = window; + windowManager.debugBindWindow(window); }); tearDown(() { - windowManager.debugTarget = null; + windowManager.debugBindWindow(null); }); group('WindowManager.setPosition', () {