Skip to content
Closed
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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).

Expand Down
44 changes: 32 additions & 12 deletions lib/src/legacy/window_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -80,6 +81,13 @@ class WindowManager {
);
}

@visibleForTesting
void debugBindWindow(nativeapi.Window? window) {
_unwireNativeEvents();
_window = window;
if (_listeners.isNotEmpty || _isPreventClose) _wireNativeEvents();
}

List<WindowListener> get listeners {
return List<WindowListener>.from(_listeners);
}
Expand Down Expand Up @@ -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);
}
}

Expand Down
116 changes: 116 additions & 0 deletions test/legacy_geometry_test.dart
Original file line number Diff line number Diff line change
@@ -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);
},
);
}
Loading