Skip to content

Commit 75ae9d1

Browse files
committed
[macos] Refactor setBounds method.
1 parent f14e71c commit 75ae9d1

15 files changed

+234
-195
lines changed

example/lib/pages/home.dart

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -324,14 +324,21 @@ class _HomePageState extends State<HomePage> with TrayListener, WindowListener {
324324
],
325325
onPressed: (int index) async {
326326
_size = _kSizes[index];
327-
Rect bounds = await windowManager.getBounds();
328-
windowManager.setBounds(
329-
Rect.fromLTWH(
330-
bounds.left,
331-
bounds.top,
332-
_size.width,
333-
_size.height,
334-
),
327+
Offset newPosition = await calcWindowPosition(
328+
_size,
329+
Alignment.center,
330+
);
331+
await windowManager.setBounds(
332+
// Rect.fromLTWH(
333+
// bounds.left + 10,
334+
// bounds.top + 10,
335+
// _size.width,
336+
// _size.height,
337+
// ),
338+
null,
339+
position: newPosition,
340+
size: _size,
341+
animate: true,
335342
);
336343
setState(() {});
337344
},
@@ -355,56 +362,82 @@ class _HomePageState extends State<HomePage> with TrayListener, WindowListener {
355362
CupertinoButton(
356363
child: Text('topLeft'),
357364
onPressed: () async {
358-
await windowManager.setAlignment(Alignment.topLeft);
365+
await windowManager.setAlignment(
366+
Alignment.topLeft,
367+
animate: true,
368+
);
359369
},
360370
),
361371
CupertinoButton(
362372
child: Text('topCenter'),
363373
onPressed: () async {
364-
await windowManager.setAlignment(Alignment.topCenter);
374+
await windowManager.setAlignment(
375+
Alignment.topCenter,
376+
animate: true,
377+
);
365378
},
366379
),
367380
CupertinoButton(
368381
child: Text('topRight'),
369382
onPressed: () async {
370-
await windowManager.setAlignment(Alignment.topRight);
383+
await windowManager.setAlignment(
384+
Alignment.topRight,
385+
animate: true,
386+
);
371387
},
372388
),
373389
CupertinoButton(
374390
child: Text('centerLeft'),
375391
onPressed: () async {
376-
await windowManager.setAlignment(Alignment.centerLeft);
392+
await windowManager.setAlignment(
393+
Alignment.centerLeft,
394+
animate: true,
395+
);
377396
},
378397
),
379398
CupertinoButton(
380399
child: Text('center'),
381400
onPressed: () async {
382-
await windowManager.setAlignment(Alignment.center);
401+
await windowManager.setAlignment(
402+
Alignment.center,
403+
animate: true,
404+
);
383405
},
384406
),
385407
CupertinoButton(
386408
child: Text('centerRight'),
387409
onPressed: () async {
388-
await windowManager.setAlignment(Alignment.centerRight);
410+
await windowManager.setAlignment(
411+
Alignment.centerRight,
412+
animate: true,
413+
);
389414
},
390415
),
391416
CupertinoButton(
392417
child: Text('bottomLeft'),
393418
onPressed: () async {
394-
await windowManager.setAlignment(Alignment.bottomLeft);
419+
await windowManager.setAlignment(
420+
Alignment.bottomLeft,
421+
animate: true,
422+
);
395423
},
396424
),
397425
CupertinoButton(
398426
child: Text('bottomCenter'),
399427
onPressed: () async {
400-
await windowManager
401-
.setAlignment(Alignment.bottomCenter);
428+
await windowManager.setAlignment(
429+
Alignment.bottomCenter,
430+
animate: true,
431+
);
402432
},
403433
),
404434
CupertinoButton(
405435
child: Text('bottomRight'),
406436
onPressed: () async {
407-
await windowManager.setAlignment(Alignment.bottomRight);
437+
await windowManager.setAlignment(
438+
Alignment.bottomRight,
439+
animate: true,
440+
);
408441
},
409442
),
410443
],

example/linux/flutter/generated_plugin_registrant.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@
66

77
#include "generated_plugin_registrant.h"
88

9+
#include <screen_retriever/screen_retriever_plugin.h>
910
#include <tray_manager/tray_manager_plugin.h>
1011
#include <window_manager/window_manager_plugin.h>
1112

1213
void fl_register_plugins(FlPluginRegistry* registry) {
14+
g_autoptr(FlPluginRegistrar) screen_retriever_registrar =
15+
fl_plugin_registry_get_registrar_for_plugin(registry, "ScreenRetrieverPlugin");
16+
screen_retriever_plugin_register_with_registrar(screen_retriever_registrar);
1317
g_autoptr(FlPluginRegistrar) tray_manager_registrar =
1418
fl_plugin_registry_get_registrar_for_plugin(registry, "TrayManagerPlugin");
1519
tray_manager_plugin_register_with_registrar(tray_manager_registrar);

example/linux/flutter/generated_plugins.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#
44

55
list(APPEND FLUTTER_PLUGIN_LIST
6+
screen_retriever
67
tray_manager
78
window_manager
89
)

example/macos/Flutter/GeneratedPluginRegistrant.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
import FlutterMacOS
66
import Foundation
77

8+
import screen_retriever
89
import tray_manager
910
import window_manager
1011

1112
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
13+
ScreenRetrieverPlugin.register(with: registry.registrar(forPlugin: "ScreenRetrieverPlugin"))
1214
TrayManagerPlugin.register(with: registry.registrar(forPlugin: "TrayManagerPlugin"))
1315
WindowManagerPlugin.register(with: registry.registrar(forPlugin: "WindowManagerPlugin"))
1416
}

example/macos/Podfile.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
PODS:
22
- FlutterMacOS (1.0.0)
3+
- screen_retriever (0.0.1):
4+
- FlutterMacOS
35
- tray_manager (0.0.1):
46
- FlutterMacOS
57
- window_manager (0.2.0):
68
- FlutterMacOS
79

810
DEPENDENCIES:
911
- FlutterMacOS (from `Flutter/ephemeral`)
12+
- screen_retriever (from `Flutter/ephemeral/.symlinks/plugins/screen_retriever/macos`)
1013
- tray_manager (from `Flutter/ephemeral/.symlinks/plugins/tray_manager/macos`)
1114
- window_manager (from `Flutter/ephemeral/.symlinks/plugins/window_manager/macos`)
1215

1316
EXTERNAL SOURCES:
1417
FlutterMacOS:
1518
:path: Flutter/ephemeral
19+
screen_retriever:
20+
:path: Flutter/ephemeral/.symlinks/plugins/screen_retriever/macos
1621
tray_manager:
1722
:path: Flutter/ephemeral/.symlinks/plugins/tray_manager/macos
1823
window_manager:
1924
:path: Flutter/ephemeral/.symlinks/plugins/window_manager/macos
2025

2126
SPEC CHECKSUMS:
2227
FlutterMacOS: 57701585bf7de1b3fc2bb61f6378d73bbdea8424
28+
screen_retriever: 59634572a57080243dd1bf715e55b6c54f241a38
2329
tray_manager: 9064e219c56d75c476e46b9a21182087930baf90
2430
window_manager: 3a1844359a6295ab1e47659b1a777e36773cd6e8
2531

example/pubspec.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ packages:
116116
url: "https://pub.dartlang.org"
117117
source: hosted
118118
version: "0.0.1"
119+
screen_retriever:
120+
dependency: transitive
121+
description:
122+
name: screen_retriever
123+
url: "https://pub.dartlang.org"
124+
source: hosted
125+
version: "0.1.1"
119126
shortid:
120127
dependency: transitive
121128
description:

example/windows/flutter/generated_plugin_registrant.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66

77
#include "generated_plugin_registrant.h"
88

9+
#include <screen_retriever/screen_retriever_plugin.h>
910
#include <tray_manager/tray_manager_plugin.h>
1011
#include <window_manager/window_manager_plugin.h>
1112

1213
void RegisterPlugins(flutter::PluginRegistry* registry) {
14+
ScreenRetrieverPluginRegisterWithRegistrar(
15+
registry->GetRegistrarForPlugin("ScreenRetrieverPlugin"));
1316
TrayManagerPluginRegisterWithRegistrar(
1417
registry->GetRegistrarForPlugin("TrayManagerPlugin"));
1518
WindowManagerPluginRegisterWithRegistrar(

example/windows/flutter/generated_plugins.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#
44

55
list(APPEND FLUTTER_PLUGIN_LIST
6+
screen_retriever
67
tray_manager
78
window_manager
89
)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:screen_retriever/screen_retriever.dart';
3+
4+
Future<Offset> calcWindowPosition(
5+
Size windowSize,
6+
Alignment alignment,
7+
) async {
8+
Display primaryDisplay = await screenRetriever.getPrimaryDisplay();
9+
10+
num visibleWidth = primaryDisplay.size.width;
11+
num visibleHeight = primaryDisplay.size.height;
12+
num visibleStartX = 0;
13+
num visibleStartY = 0;
14+
15+
if (primaryDisplay.visibleSize != null) {
16+
visibleWidth = primaryDisplay.visibleSize!.width;
17+
visibleHeight = primaryDisplay.visibleSize!.height;
18+
}
19+
if (primaryDisplay.visiblePosition != null) {
20+
visibleStartX = primaryDisplay.visiblePosition!.dx;
21+
visibleStartY = primaryDisplay.visiblePosition!.dy;
22+
}
23+
Offset position = Offset(0, 0);
24+
25+
if (alignment == Alignment.topLeft) {
26+
position = Offset(0, 0);
27+
} else if (alignment == Alignment.topCenter) {
28+
position = Offset(
29+
visibleStartX + (visibleWidth / 2) - (windowSize.width / 2),
30+
visibleStartY + 0,
31+
);
32+
} else if (alignment == Alignment.topRight) {
33+
position = Offset(
34+
visibleStartX + visibleWidth - windowSize.width,
35+
visibleStartY + 0,
36+
);
37+
} else if (alignment == Alignment.centerLeft) {
38+
position = Offset(
39+
visibleStartX + 0,
40+
visibleStartY + ((visibleHeight / 2) - (windowSize.height / 2)),
41+
);
42+
} else if (alignment == Alignment.center) {
43+
position = Offset(
44+
visibleStartX + (visibleWidth / 2) - (windowSize.width / 2),
45+
visibleStartY + ((visibleHeight / 2) - (windowSize.height / 2)),
46+
);
47+
} else if (alignment == Alignment.centerRight) {
48+
position = Offset(
49+
visibleStartX + visibleWidth - windowSize.width,
50+
visibleStartY + ((visibleHeight / 2) - (windowSize.height / 2)),
51+
);
52+
} else if (alignment == Alignment.bottomLeft) {
53+
position = Offset(
54+
visibleStartX + 0,
55+
visibleStartY + (visibleHeight - windowSize.height),
56+
);
57+
} else if (alignment == Alignment.bottomCenter) {
58+
position = Offset(
59+
visibleStartX + (visibleWidth / 2) - (windowSize.width / 2),
60+
visibleStartY + (visibleHeight - windowSize.height),
61+
);
62+
} else if (alignment == Alignment.bottomRight) {
63+
position = Offset(
64+
visibleStartX + visibleWidth - windowSize.width,
65+
visibleStartY + (visibleHeight - windowSize.height),
66+
);
67+
}
68+
69+
return position;
70+
}

0 commit comments

Comments
 (0)