dockToCorner static method
停靠窗口到指定角落(简单对齐,不隐藏)
corner 要停靠到的角落位置
返回 true 表示停靠成功,false 表示停靠失败
注意: 此方法仅将窗口对齐到角落位置,不会隐藏窗口或提供鼠标交互。 如需类似QQ的智能停靠功能(自动隐藏/显示),请使用 setSmartEdgeDocking。
此方法会自动检测屏幕工作区域,避开任务栏。
使用场景:
- 简单的窗口定位需求
- 不需要隐藏/显示交互的场景
- 一次性窗口位置调整
示例:
// 将窗口对齐到左上角
await MyApp.dockToCorner(WindowCorner.topLeft);
Implementation
static Future<bool> dockToCorner(WindowCorner corner) async {
if (!MyPlatform.isDesktop) return false;
try {
final display = await screenRetriever.getPrimaryDisplay();
if (display.visiblePosition == null || display.visibleSize == null) {
return false;
}
final windowSize = await windowManager.getSize();
final workArea = display.visibleSize!;
final workAreaPosition = display.visiblePosition!;
// 获取DPI缩放因子来修正位置精度
final scaleFactor = display.scaleFactor ?? 1.0;
// 计算边框偏移量(Windows窗口可能有不可见边框)
// 在高DPI环境下,通常需要微调更多像素来达到完美贴边
// 根据用户反馈,增加偏移量以获得更好的贴边效果
final edgeOffset = scaleFactor > 1.0 ? 16.0 / scaleFactor : 10.0;
// 顶部边缘需要额外的偏移,因为Windows标题栏区域的处理方式不同
// final topEdgeOffset = scaleFactor > 1.0 ? 20.0 / scaleFactor : 14.0;
XlyLogger.debug('scaleFactor: $scaleFactor, edgeOffset: $edgeOffset');
final topEdgeOffset = edgeOffset - 0.95 * edgeOffset;
late Offset position;
switch (corner) {
case WindowCorner.topLeft:
position = Offset(
workAreaPosition.dx - edgeOffset,
workAreaPosition.dy - topEdgeOffset,
);
break;
case WindowCorner.topRight:
position = Offset(
workAreaPosition.dx +
workArea.width -
windowSize.width +
edgeOffset,
workAreaPosition.dy - topEdgeOffset,
);
break;
case WindowCorner.bottomLeft:
position = Offset(
workAreaPosition.dx - edgeOffset,
workAreaPosition.dy +
workArea.height -
windowSize.height +
edgeOffset,
);
break;
case WindowCorner.bottomRight:
position = Offset(
workAreaPosition.dx +
workArea.width -
windowSize.width +
edgeOffset,
workAreaPosition.dy +
workArea.height -
windowSize.height +
edgeOffset,
);
break;
}
// 确保位置值是整数,避免亚像素定位问题
final adjustedPosition = Offset(
position.dx.roundToDouble(),
position.dy.roundToDouble(),
);
await windowManager.setPosition(adjustedPosition);
return true;
} catch (e) {
return false;
}
}