From 5d765c36210e891b88472debb3627410cf524467 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=81=A5=E8=8E=B9?= Date: Sun, 27 Aug 2023 21:11:02 +0800 Subject: [PATCH] [macos] Fix incorrect centering when multi-screen --- README-ZH.md | 2 +- README.md | 2 +- example/pubspec.lock | 6 ++--- lib/src/utils/calc_window_position.dart | 34 +++++++++++++++++-------- macos/Classes/WindowManager.swift | 4 +-- pubspec.lock | 4 +-- pubspec.yaml | 4 +-- 7 files changed, 35 insertions(+), 21 deletions(-) diff --git a/README-ZH.md b/README-ZH.md index 2d1a44b9..554a458a 100644 --- a/README-ZH.md +++ b/README-ZH.md @@ -145,7 +145,7 @@ ```yaml dependencies: - window_manager: ^0.3.5 + window_manager: ^0.3.6 ``` 或 diff --git a/README.md b/README.md index 7dc97d94..05af0caa 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/example/pubspec.lock b/example/pubspec.lock index b2170392..7bf7d1fd 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -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: @@ -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" diff --git a/lib/src/utils/calc_window_position.dart b/lib/src/utils/calc_window_position.dart index 4773956a..1a3ca7af 100644 --- a/lib/src/utils/calc_window_position.dart +++ b/lib/src/utils/calc_window_position.dart @@ -6,24 +6,39 @@ Future calcWindowPosition( Alignment alignment, ) async { Display primaryDisplay = await screenRetriever.getPrimaryDisplay(); + List 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), @@ -65,6 +80,5 @@ Future calcWindowPosition( visibleStartY + (visibleHeight - windowSize.height), ); } - return position; } diff --git a/macos/Classes/WindowManager.swift b/macos/Classes/WindowManager.swift index 87948df8..5c69abcb 100644 --- a/macos/Classes/WindowManager.swift +++ b/macos/Classes/WindowManager.swift @@ -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) } } diff --git a/pubspec.lock b/pubspec.lock index 705107a9..c292744e 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -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 diff --git a/pubspec.yaml b/pubspec.yaml index 7a612f9b..93a6a626 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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: @@ -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