Skip to content

Commit bf008b6

Browse files
committed
[macos] Implement center method #59
1 parent 83e061e commit bf008b6

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

example/lib/pages/home.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,12 @@ class _HomePageState extends State<HomePage> with WindowListener {
222222
);
223223
},
224224
),
225+
PreferenceListItem(
226+
title: Text('center'),
227+
onTap: () async {
228+
await windowManager.center();
229+
},
230+
),
225231
PreferenceListItem(
226232
title: Text('getPosition / setPosition'),
227233
accessoryView: Row(

lib/src/window_manager.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,23 @@ class WindowManager {
204204
return Offset(resultData['x'], resultData['y']);
205205
}
206206

207+
// Moves window to the center of the screen.
208+
Future<void> center() async {
209+
Size windowSize = await getSize();
210+
Map<String, dynamic> primaryDisplay = await _getPrimaryDisplay();
211+
print(primaryDisplay);
212+
213+
num visibleWidth = primaryDisplay['visibleSize']['width'];
214+
num visibleHeight = primaryDisplay['visibleSize']['height'];
215+
216+
Offset position = Offset(
217+
(visibleWidth / 2) - (windowSize.width / 2),
218+
(visibleHeight / 2) - (windowSize.height / 2),
219+
);
220+
221+
await this.setPosition(position);
222+
}
223+
207224
/// Moves window to position.
208225
Future<void> setPosition(Offset position, {animate = false}) async {
209226
final Map<String, dynamic> arguments = {
@@ -424,6 +441,15 @@ class WindowManager {
424441
await _channel.invokeMethod('startDragging');
425442
}
426443

444+
Future<Map<String, dynamic>> _getPrimaryDisplay() async {
445+
final Map<String, dynamic> arguments = {
446+
'devicePixelRatio': window.devicePixelRatio,
447+
};
448+
final Map<dynamic, dynamic> resultData =
449+
await _channel.invokeMethod('getPrimaryDisplay', arguments);
450+
return Map<String, dynamic>.from(resultData);
451+
}
452+
427453
Future<bool> isSubWindow() async {
428454
return await _channel.invokeMethod('isSubWindow');
429455
}

macos/Classes/WindowManager.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,32 @@ public class WindowManager: NSObject, NSWindowDelegate {
407407
}
408408
}
409409

410+
public func getPrimaryDisplay() -> NSDictionary {
411+
let screen = NSScreen.main!
412+
let size: NSDictionary = [
413+
"width": screen.frame.width,
414+
"height": screen.frame.height,
415+
]
416+
let visiblePosition: NSDictionary = [
417+
"x": screen.visibleFrame.topLeft.x,
418+
"y": screen.visibleFrame.topLeft.y,
419+
"width": screen.visibleFrame.width,
420+
"height": screen.visibleFrame.height,
421+
]
422+
let visibleSize: NSDictionary = [
423+
"x": screen.visibleFrame.topLeft.x,
424+
"y": screen.visibleFrame.topLeft.y,
425+
"width": screen.visibleFrame.width,
426+
"height": screen.visibleFrame.height,
427+
]
428+
let dict: NSDictionary = [
429+
"size": size,
430+
"visiblePosition": visiblePosition,
431+
"visibleSize": visibleSize,
432+
]
433+
return dict;
434+
}
435+
410436
public func isSubWindow() -> Bool {
411437
let identifier: String = mainWindow.identifier?.rawValue ?? "";
412438
return identifier == "subwindow"

macos/Classes/WindowManagerPlugin.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ public class WindowManagerPlugin: NSObject, FlutterPlugin {
209209
windowManager.startDragging()
210210
result(true)
211211
break
212+
case "getPrimaryDisplay":
213+
result(windowManager.getPrimaryDisplay())
214+
break
212215
case "isSubWindow":
213216
result(windowManager.isSubWindow())
214217
break

0 commit comments

Comments
 (0)