Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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).

Expand Down
48 changes: 34 additions & 14 deletions lib/src/legacy/window_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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!);
}
Expand Down Expand Up @@ -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();
}
Expand Down
152 changes: 152 additions & 0 deletions test/legacy_api_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down Expand Up @@ -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<String> calls = <String>[];
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);
}
53 changes: 38 additions & 15 deletions test/legacy_set_bounds_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<nativeapi.Rectangle> animateRequests = [];

Expand Down Expand Up @@ -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(
Expand Down
Loading