Skip to content

Commit ffb5e4f

Browse files
committed
[macos] Optimize sub-window feature
1 parent 859ba75 commit ffb5e4f

File tree

8 files changed

+57
-14
lines changed

8 files changed

+57
-14
lines changed

example/lib/pages/home.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,16 @@ class _HomePageState extends State<HomePage> with WindowListener {
447447
setState(() {});
448448
},
449449
),
450+
PreferenceListItem(
451+
title: Text('createSubWindow'),
452+
onTap: () async {
453+
SubWindow subWindow = await SubWindow.create(
454+
size: Size(800, 600),
455+
center: true,
456+
title: 'title',
457+
);
458+
},
459+
),
450460
],
451461
),
452462
],

example/macos/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
PODS:
22
- FlutterMacOS (1.0.0)
3-
- window_manager (0.1.0):
3+
- window_manager (0.1.2):
44
- FlutterMacOS
55

66
DEPENDENCIES:
@@ -15,7 +15,7 @@ EXTERNAL SOURCES:
1515

1616
SPEC CHECKSUMS:
1717
FlutterMacOS: 57701585bf7de1b3fc2bb61f6378d73bbdea8424
18-
window_manager: db713621c322d52e96064336a4ba2b7e4e30c94f
18+
window_manager: 8c227608c115dbd132510c023d3644274aaec8a8
1919

2020
PODFILE CHECKSUM: 6eac6b3292e5142cfc23bdeb71848a40ec51c14c
2121

example/macos/Runner/MainFlutterWindow.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class MainFlutterWindow: NSWindow {
1111

1212
RegisterGeneratedPlugins(registry: flutterViewController)
1313

14+
WindowManagerPlugin.RegisterGeneratedPlugins = RegisterGeneratedPlugins
15+
1416
super.awakeFromNib()
1517
}
1618

lib/src/sub_window.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import 'dart:ui';
2+
3+
import 'window_manager.dart';
4+
5+
class SubWindow {
6+
static Future<SubWindow> create({
7+
Size? size,
8+
Offset? position,
9+
bool center = true,
10+
required String title,
11+
}) async {
12+
SubWindow subWindow = SubWindow();
13+
windowManager.createSubWindow(
14+
size: size,
15+
position: position,
16+
center: center,
17+
title: title,
18+
);
19+
return subWindow;
20+
}
21+
}

lib/src/window_manager.dart

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -358,17 +358,19 @@ class WindowManager {
358358
}
359359

360360
Future<void> createSubWindow({
361+
Size? size,
362+
Offset? position,
363+
bool center = true,
361364
required String title,
362-
required Offset position,
363-
required Size size,
364365
}) async {
365366
final Map<String, dynamic> arguments = {
367+
'width': size?.width,
368+
'height': size?.height,
369+
'x': position?.dx,
370+
'y': position?.dy,
371+
'center': center,
366372
'title': title,
367-
'x': position.dx,
368-
'y': position.dy,
369-
'width': size.width,
370-
'height': size.height,
371-
};
373+
}..removeWhere((key, value) => value == null);
372374
await _channel.invokeMethod('createSubWindow', arguments);
373375
}
374376
}

lib/window_manager.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export 'src/drag_to_move_area.dart';
2+
export 'src/sub_window.dart';
23
export 'src/window_listener.dart';
34
export 'src/window_manager.dart';

macos/Classes/SubWindow.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@ public class SubWindow: NSWindow {
88

99
super.awakeFromNib()
1010
}
11-
1211
}

macos/Classes/WindowManager.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,16 +361,24 @@ public class WindowManager: NSObject, NSWindowDelegate {
361361
}
362362

363363
public func createSubWindow(_ args: [String: Any]) {
364-
let title: String = args["title"] as! String
364+
let visibleFrame = NSScreen.main!.visibleFrame
365365

366366
var frameRect: NSRect = NSRect.zero
367+
if (args["width"] != nil && args["width"] != nil) {
368+
frameRect.size.width = CGFloat(args["width"] as! Float)
369+
frameRect.size.height = CGFloat(args["height"] as! Float)
370+
}
367371
if (args["x"] != nil && args["y"] != nil) {
368372
frameRect.topLeft.x = CGFloat(args["x"] as! Float)
369373
frameRect.topLeft.y = CGFloat(args["y"] as! Float)
370374
}
371-
if (args["width"] != nil && args["width"] != nil) {
372-
frameRect.size.width = CGFloat(args["width"] as! Float)
373-
frameRect.size.height = CGFloat(args["height"] as! Float)
375+
376+
let center: Bool = args["center"] as! Bool
377+
let title: String = args["title"] as! String
378+
379+
if (center) {
380+
frameRect.origin.x = (visibleFrame.width / 2) - (frameRect.size.width / 2)
381+
frameRect.origin.y = (visibleFrame.height / 2) + (frameRect.size.height / 2)
374382
}
375383

376384
let flutterViewController = FlutterViewController.init()

0 commit comments

Comments
 (0)