From b3aa1d3e4e6a6a3532771d225b95db7e2067c2c8 Mon Sep 17 00:00:00 2001 From: LiJianying Date: Fri, 20 Nov 2026 08:10:01 +0800 Subject: [PATCH] fix(legacy): avoid clobbering pending size in setPosition/setBounds --- CHANGELOG.md | 4 + lib/src/legacy/window_manager.dart | 48 ++++++--- test/legacy_api_test.dart | 152 +++++++++++++++++++++++++++++ test/legacy_set_bounds_test.dart | 53 +++++++--- 4 files changed, 228 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ae3b291..867bb76 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## Unreleased +* Avoid overwriting pending window geometry in legacy `setPosition`, `setBounds`, + and `waitUntilReadyToShow` when updating only position or size, and compute + centered startup placement from `options.size` directly + ([libnativeapi/nativeapi#37](https://github.com/libnativeapi/nativeapi/issues/37)). * 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). diff --git a/lib/src/legacy/window_manager.dart b/lib/src/legacy/window_manager.dart index abce9ae..860cd00 100644 --- a/lib/src/legacy/window_manager.dart +++ b/lib/src/legacy/window_manager.dart @@ -257,7 +257,17 @@ class WindowManager { if (await isMinimized()) await restore(); if (options?.size != null) await setSize(options!.size!); - if (options?.center == true) await setAlignment(Alignment.center); + if (options?.center == true) { + if (options?.size != null) { + final position = await calcWindowPosition( + options!.size!, + Alignment.center, + ); + await setPosition(position); + } else { + await setAlignment(Alignment.center); + } + } if (options?.minimumSize != null) { await setMinimumSize(options!.minimumSize!); } @@ -541,20 +551,30 @@ class WindowManager { final window = _target; if (window == null) return; if (bounds == null && position == null && size == null) return; - // Full bounds are already known: reading them again through nativeapi - // performs an unnecessary synchronous FFI call on every restore/move. - Rect target; - if (bounds != null) { - target = bounds; - } else { - final current = window.bounds.toRect(); - target = Rect.fromLTWH( - position?.dx ?? current.left, - position?.dy ?? current.top, - size?.width ?? current.width, - size?.height ?? current.height, - ); + // Write only the requested component for partial updates: asynchronous + // window managers (such as X11) can still report the old bounds after a + // recent resize or move, and writing back a full frame would clobber it. + if (bounds == null && position == null && size != null) { + window.setSize(size.toNative(), animate); + return; + } + if (bounds == null && size == null && position != null) { + if (animate) { + final current = window.bounds.toRect(); + final target = Rect.fromLTWH( + position.dx, + position.dy, + current.width, + current.height, + ); + if (window.animateBounds(target.toNative())) return; + } + window.position = position.toNative(); + return; } + final target = + bounds ?? + Rect.fromLTWH(position!.dx, position.dy, size!.width, size.height); if (!animate || !window.animateBounds(target.toNative())) { window.bounds = target.toNative(); } diff --git a/test/legacy_api_test.dart b/test/legacy_api_test.dart index 4ba93d9..ee8da1e 100644 --- a/test/legacy_api_test.dart +++ b/test/legacy_api_test.dart @@ -10,6 +10,7 @@ import 'dart:io'; import 'package:material_ui/material_ui.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:nativeapi/nativeapi.dart' as nativeapi; import 'package:window_manager/legacy.dart'; class _Mixed with WindowListener {} @@ -278,4 +279,155 @@ void main() { await windowManager.popUpWindowMenu(); await windowManager.ensureInitialized(); }); + + group('geometry updates do not clobber pending state', () { + late _FakeWindow fakeWindow; + + setUp(() { + fakeWindow = _FakeWindow(); + windowManager.bindWindow(fakeWindow); + }); + + tearDown(() { + windowManager.bindWindow(null); + }); + + test('setPosition does not write back stale size from bounds', () async { + await windowManager.setSize(const Size(1920, 1080)); + // Simulate X11 where window.bounds still returns the old size. + await windowManager.setPosition(const Offset(100, 200)); + + expect(fakeWindow.calls, [ + 'setSize(1920.0, 1080.0, false)', + 'position=(100.0, 200.0)', + ]); + }); + + test('setPosition(animate: true) falls back to position when animateBounds returns false', () async { + fakeWindow.animateBoundsResult = false; + await windowManager.setPosition(const Offset(50, 60), animate: true); + + expect(fakeWindow.calls, [ + 'bounds', + 'animateBounds(50.0, 60.0, 1280.0, 720.0)', + 'position=(50.0, 60.0)', + ]); + }); + + test( + 'setPosition(animate: true) stops after animateBounds succeeds', + () async { + fakeWindow.animateBoundsResult = true; + await windowManager.setPosition(const Offset(50, 60), animate: true); + + expect(fakeWindow.calls, [ + 'bounds', + 'animateBounds(50.0, 60.0, 1280.0, 720.0)', + ]); + }, + ); + + test('setBounds with only size calls setSize directly', () async { + await windowManager.setBounds( + null, + size: const Size(1024, 768), + animate: true, + ); + + expect(fakeWindow.calls, ['setSize(1024.0, 768.0, true)']); + }); + + test('setBounds with full rect sets bounds', () async { + await windowManager.setBounds(const Rect.fromLTWH(10, 20, 800, 600)); + + expect(fakeWindow.calls, ['bounds=(10.0, 20.0, 800.0, 600.0)']); + }); + + test( + 'waitUntilReadyToShow computes center from options.size without reading stale size', + () async { + const requestedSize = Size(600, 400); + final expectedPosition = await calcWindowPosition( + requestedSize, + Alignment.center, + ); + + await windowManager.waitUntilReadyToShow( + const WindowOptions(size: requestedSize, center: true), + ); + + expect(fakeWindow.calls, [ + 'setSize(600.0, 400.0, false)', + 'position=(${expectedPosition.dx}, ${expectedPosition.dy})', + ]); + }, + skip: + Platform.isLinux && + (Platform.environment['DISPLAY'] ?? '').isEmpty && + (Platform.environment['WAYLAND_DISPLAY'] ?? '').isEmpty, + ); + }); +} + +class _FakeWindow implements nativeapi.Window { + final List calls = []; + bool animateBoundsResult = false; + nativeapi.Rectangle currentBounds = const nativeapi.Rectangle( + x: 0, + y: 0, + width: 1280, + height: 720, + ); + + @override + bool get isFullScreen => false; + + @override + bool get isMaximized => false; + + @override + bool get isMinimized => false; + + @override + nativeapi.Size get size { + calls.add('size'); + return nativeapi.Size( + width: currentBounds.width, + height: currentBounds.height, + ); + } + + @override + nativeapi.Rectangle get bounds { + calls.add('bounds'); + return currentBounds; + } + + @override + set bounds(nativeapi.Rectangle value) { + calls.add( + 'bounds=(${value.x}, ${value.y}, ${value.width}, ${value.height})', + ); + } + + @override + set position(nativeapi.Point value) { + calls.add('position=(${value.x}, ${value.y})'); + } + + @override + void setSize(nativeapi.Size size, [bool animate = false]) { + calls.add('setSize(${size.width}, ${size.height}, $animate)'); + } + + @override + bool animateBounds(nativeapi.Rectangle bounds) { + calls.add( + 'animateBounds(${bounds.x}, ${bounds.y}, ${bounds.width}, ${bounds.height})', + ); + return animateBoundsResult; + } + + @override + dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); } diff --git a/test/legacy_set_bounds_test.dart b/test/legacy_set_bounds_test.dart index 063574c..ba80d56 100644 --- a/test/legacy_set_bounds_test.dart +++ b/test/legacy_set_bounds_test.dart @@ -29,6 +29,26 @@ class _FakeNativeWindow extends nativeapi.Window { currentBounds = value; } + @override + set position(nativeapi.Point value) { + currentBounds = nativeapi.Rectangle( + x: value.x, + y: value.y, + width: currentBounds.width, + height: currentBounds.height, + ); + } + + @override + void setSize(nativeapi.Size value, [bool animate = false]) { + currentBounds = nativeapi.Rectangle( + x: currentBounds.x, + y: currentBounds.y, + width: value.width, + height: value.height, + ); + } + // Simulates a platform without native bounds animation (Windows/Linux). final List animateRequests = []; @@ -58,21 +78,24 @@ void main() { ); }); - test('partial updates still read the omitted dimensions', () async { - await windowManager.setBounds(null, position: const Offset(25, 35)); - expect(native.boundsReads, 1); - expect( - native.currentBounds, - const nativeapi.Rectangle(x: 25, y: 35, width: 300, height: 200), - ); - - await windowManager.setBounds(null, size: const Size(640, 480)); - expect(native.boundsReads, 2); - expect( - native.currentBounds, - const nativeapi.Rectangle(x: 25, y: 35, width: 640, height: 480), - ); - }); + test( + 'partial updates write only the requested component without reading bounds', + () async { + await windowManager.setBounds(null, position: const Offset(25, 35)); + expect(native.boundsReads, 0); + expect( + native.currentBounds, + const nativeapi.Rectangle(x: 25, y: 35, width: 300, height: 200), + ); + + await windowManager.setBounds(null, size: const Size(640, 480)); + expect(native.boundsReads, 0); + expect( + native.currentBounds, + const nativeapi.Rectangle(x: 25, y: 35, width: 640, height: 480), + ); + }, + ); test('full bounds win over position and size', () async { await windowManager.setBounds(