mirror of
https://github.com/HChaZZY/PiliPlus.git
synced 2025-12-21 09:37:02 +08:00
54
lib/utils/calc_window_position.dart
Normal file
54
lib/utils/calc_window_position.dart
Normal file
@@ -0,0 +1,54 @@
|
||||
import 'package:PiliPlus/utils/storage_pref.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:screen_retriever/screen_retriever.dart';
|
||||
import 'package:window_manager/window_manager.dart';
|
||||
|
||||
Future<Offset> get calcWindowPosition async {
|
||||
Display primaryDisplay = await screenRetriever.getPrimaryDisplay();
|
||||
List<Display> allDisplays = await screenRetriever.getAllDisplays();
|
||||
Offset cursorScreenPoint = await screenRetriever.getCursorScreenPoint();
|
||||
|
||||
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 (currentDisplay.visibleSize != null) {
|
||||
visibleWidth = currentDisplay.visibleSize!.width;
|
||||
visibleHeight = currentDisplay.visibleSize!.height;
|
||||
}
|
||||
if (currentDisplay.visiblePosition != null) {
|
||||
visibleStartX = currentDisplay.visiblePosition!.dx;
|
||||
visibleStartY = currentDisplay.visiblePosition!.dy;
|
||||
}
|
||||
|
||||
final windowPosition = Pref.windowPosition;
|
||||
if (windowPosition != null) {
|
||||
try {
|
||||
final dx = windowPosition[0];
|
||||
final dy = windowPosition[1];
|
||||
if (dx >= visibleStartX &&
|
||||
dy >= visibleStartY &&
|
||||
dx < (visibleWidth - 30) &&
|
||||
dy < (visibleHeight - 30)) {
|
||||
return Offset(dx, dy);
|
||||
}
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
final windowSize = await windowManager.getSize();
|
||||
return Offset(
|
||||
visibleStartX + (visibleWidth / 2) - (windowSize.width / 2),
|
||||
visibleStartY + ((visibleHeight / 2) - (windowSize.height / 2)),
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user