Skip to content

Commit

Permalink
[macos] Fix incorrect centering when multi-screen
Browse files Browse the repository at this point in the history
  • Loading branch information
lijy91 committed Aug 27, 2023
1 parent 5807a96 commit 5d765c3
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README-ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@

```yaml
dependencies:
window_manager: ^0.3.5
window_manager: ^0.3.6
```
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ Add this to your package's `pubspec.yaml` file:

```yaml
dependencies:
window_manager: ^0.3.5
window_manager: ^0.3.6
```
Or
Expand Down
6 changes: 3 additions & 3 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,10 @@ packages:
dependency: transitive
description:
name: screen_retriever
sha256: "4931f226ca158123ccd765325e9fbf360bfed0af9b460a10f960f9bb13d58323"
sha256: "63694235c194d0d953f698fbb04471eb6c8d0e6bbb283a369b40414ed07ef83a"
url: "https://pub.dev"
source: hosted
version: "0.1.6"
version: "0.1.8"
shortid:
dependency: transitive
description:
Expand Down Expand Up @@ -301,7 +301,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.3.5"
version: "0.3.6"
sdks:
dart: ">=3.0.0 <4.0.0"
flutter: ">=3.3.0"
34 changes: 24 additions & 10 deletions lib/src/utils/calc_window_position.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,39 @@ Future<Offset> calcWindowPosition(
Alignment alignment,
) async {
Display primaryDisplay = await screenRetriever.getPrimaryDisplay();
List<Display> allDisplays = await screenRetriever.getAllDisplays();
Offset cursorScreenPoint = await screenRetriever.getCursorScreenPoint();

num visibleWidth = primaryDisplay.size.width;
num visibleHeight = primaryDisplay.size.height;
Display currentDisplay = allDisplays.firstWhere(
(display) => Rect.fromLTWH(
display.visiblePosition!.dx,
display.visiblePosition!.dy,
display.size.width,
display.size.height,
).contains(cursorScreenPoint),
orElse: () => primaryDisplay,
);

num visibleWidth = currentDisplay.size.width;
num visibleHeight = currentDisplay.size.height;
num visibleStartX = 0;
num visibleStartY = 0;

if (primaryDisplay.visibleSize != null) {
visibleWidth = primaryDisplay.visibleSize!.width;
visibleHeight = primaryDisplay.visibleSize!.height;
if (currentDisplay.visibleSize != null) {
visibleWidth = currentDisplay.visibleSize!.width;
visibleHeight = currentDisplay.visibleSize!.height;
}
if (primaryDisplay.visiblePosition != null) {
visibleStartX = primaryDisplay.visiblePosition!.dx;
visibleStartY = primaryDisplay.visiblePosition!.dy;
if (currentDisplay.visiblePosition != null) {
visibleStartX = currentDisplay.visiblePosition!.dx;
visibleStartY = currentDisplay.visiblePosition!.dy;
}
Offset position = const Offset(0, 0);

if (alignment == Alignment.topLeft) {
position = const Offset(0, 0);
position = Offset(
visibleStartX + 0,
visibleStartY + 0,
);
} else if (alignment == Alignment.topCenter) {
position = Offset(
visibleStartX + (visibleWidth / 2) - (windowSize.width / 2),
Expand Down Expand Up @@ -65,6 +80,5 @@ Future<Offset> calcWindowPosition(
visibleStartY + (visibleHeight - windowSize.height),
);
}

return position;
}
4 changes: 2 additions & 2 deletions macos/Classes/WindowManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ extension NSWindow {
extension NSRect {
var topLeft: CGPoint {
set {
let screenFrameRect = NSScreen.main!.frame
let screenFrameRect = NSScreen.screens[0].frame
origin.x = newValue.x
origin.y = screenFrameRect.height - newValue.y - size.height
}
get {
let screenFrameRect = NSScreen.main!.frame
let screenFrameRect = NSScreen.screens[0].frame
return CGPoint(x: origin.x, y: screenFrameRect.height - origin.y - size.height)
}
}
Expand Down
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,10 @@ packages:
dependency: "direct main"
description:
name: screen_retriever
sha256: "4931f226ca158123ccd765325e9fbf360bfed0af9b460a10f960f9bb13d58323"
sha256: "63694235c194d0d953f698fbb04471eb6c8d0e6bbb283a369b40414ed07ef83a"
url: "https://pub.dev"
source: hosted
version: "0.1.6"
version: "0.1.8"
sky_engine:
dependency: transitive
description: flutter
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: window_manager
description: This plugin allows Flutter desktop apps to resizing and repositioning the window.
version: 0.3.5
version: 0.3.6
homepage: https://github.com/leanflutter/window_manager

platforms:
Expand All @@ -23,7 +23,7 @@ dependencies:
flutter:
sdk: flutter
path: ^1.8.2
screen_retriever: ^0.1.6
screen_retriever: ^0.1.8

dev_dependencies:
dependency_validator: ^3.0.0
Expand Down

0 comments on commit 5d765c3

Please sign in to comment.