Skip to content

Commit ee9e4c0

Browse files
knoppmattkae
andauthored
[macOS] Implement dialog window (flutter#176893)
<!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> *Replace this paragraph with a description of what this PR is changing or adding, and why. Consider including before/after screenshots.* *List which issues are fixed by this PR. You must list at least one issue. An issue is not required if the PR fixes something trivial like a typo.* *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].* ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: Matthew Kosarek <matt.kosarek@canonical.com>
1 parent 0013b52 commit ee9e4c0

File tree

4 files changed

+336
-34
lines changed

4 files changed

+336
-34
lines changed

engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterWindowController.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ struct FlutterWindowCreationRequest {
4141
struct FlutterWindowSize size;
4242
bool has_constraints;
4343
struct FlutterWindowConstraints constraints;
44-
void (*on_close)();
44+
int64_t parent_view_id;
45+
void (*on_should_close)();
46+
void (*on_will_close)();
4547
void (*notify_listeners)();
4648
};
4749

@@ -54,6 +56,11 @@ int64_t InternalFlutter_WindowController_CreateRegularWindow(
5456
int64_t engine_id,
5557
const FlutterWindowCreationRequest* request);
5658

59+
FLUTTER_DARWIN_EXPORT
60+
int64_t InternalFlutter_WindowController_CreateDialogWindow(
61+
int64_t engine_id,
62+
const FlutterWindowCreationRequest* request);
63+
5764
FLUTTER_DARWIN_EXPORT
5865
void InternalFlutter_Window_Destroy(int64_t engine_id, void* window);
5966

engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterWindowController.mm

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,14 @@ - (void)windowDidResignKey:(NSNotification*)notification {
9090

9191
- (BOOL)windowShouldClose:(NSWindow*)sender {
9292
flutter::IsolateScope isolate_scope(*_isolate);
93-
_creationRequest.on_close();
93+
_creationRequest.on_should_close();
9494
return NO;
9595
}
9696

97+
- (void)windowWillClose {
98+
_creationRequest.on_will_close();
99+
}
100+
97101
- (void)windowDidResize:(NSNotification*)notification {
98102
flutter::IsolateScope isolate_scope(*_isolate);
99103
_creationRequest.notify_listeners();
@@ -141,6 +145,59 @@ - (instancetype)init {
141145
return self;
142146
}
143147

148+
- (FlutterViewIdentifier)createDialogWindow:(const FlutterWindowCreationRequest*)request {
149+
FlutterViewController* c = [[FlutterViewController alloc] initWithEngine:_engine
150+
nibName:nil
151+
bundle:nil];
152+
153+
NSWindow* window = [[NSWindow alloc] init];
154+
// If this is not set there will be double free on window close when
155+
// using ARC.
156+
[window setReleasedWhenClosed:NO];
157+
158+
window.contentViewController = c;
159+
window.styleMask =
160+
NSWindowStyleMaskResizable | NSWindowStyleMaskTitled | NSWindowStyleMaskClosable;
161+
window.collectionBehavior = NSWindowCollectionBehaviorFullScreenAuxiliary;
162+
if (request->has_size) {
163+
[window flutterSetContentSize:request->size];
164+
}
165+
if (request->has_constraints) {
166+
[window flutterSetConstraints:request->constraints];
167+
}
168+
169+
FlutterWindowOwner* w = [[FlutterWindowOwner alloc] initWithWindow:window
170+
flutterViewController:c
171+
creationRequest:*request];
172+
window.delegate = w;
173+
[_windows addObject:w];
174+
175+
NSWindow* parent = nil;
176+
177+
if (request->parent_view_id != 0) {
178+
for (FlutterWindowOwner* owner in _windows) {
179+
if (owner.flutterViewController.viewIdentifier == request->parent_view_id) {
180+
parent = owner.window;
181+
break;
182+
}
183+
}
184+
if (parent == nil) {
185+
FML_LOG(WARNING) << "Failed to find parent window for ID " << request->parent_view_id;
186+
}
187+
}
188+
189+
if (parent != nil) {
190+
[parent beginCriticalSheet:window
191+
completionHandler:^(NSModalResponse response){
192+
}];
193+
} else {
194+
[window setIsVisible:YES];
195+
[window makeKeyAndOrderFront:nil];
196+
}
197+
198+
return c.viewIdentifier;
199+
}
200+
144201
- (FlutterViewIdentifier)createRegularWindow:(const FlutterWindowCreationRequest*)request {
145202
FlutterViewController* c = [[FlutterViewController alloc] initWithEngine:_engine
146203
nibName:nil
@@ -182,11 +239,21 @@ - (void)destroyWindow:(NSWindow*)window {
182239
}
183240
if (owner != nil) {
184241
[_windows removeObject:owner];
242+
243+
for (NSWindow* win in owner.window.sheets) {
244+
[self destroyWindow:win];
245+
}
246+
247+
for (NSWindow* win in owner.window.childWindows) {
248+
[self destroyWindow:win];
249+
}
250+
185251
// Make sure to unregister the controller from the engine and remove the FlutterView
186252
// before destroying the window and Flutter NSView.
187253
[owner.flutterViewController dispose];
188254
owner.window.delegate = nil;
189255
[owner.window close];
256+
[owner windowWillClose];
190257
}
191258
}
192259

@@ -210,6 +277,14 @@ int64_t InternalFlutter_WindowController_CreateRegularWindow(
210277
return [engine.windowController createRegularWindow:request];
211278
}
212279

280+
int64_t InternalFlutter_WindowController_CreateDialogWindow(
281+
int64_t engine_id,
282+
const FlutterWindowCreationRequest* request) {
283+
FlutterEngine* engine = [FlutterEngine engineForIdentifier:engine_id];
284+
[engine enableMultiView];
285+
return [engine.windowController createDialogWindow:request];
286+
}
287+
213288
void InternalFlutter_Window_Destroy(int64_t engine_id, void* window) {
214289
NSWindow* w = (__bridge NSWindow*)window;
215290
FlutterEngine* engine = [FlutterEngine engineForIdentifier:engine_id];

engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterWindowControllerTest.mm

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ void TearDown() {
5959
FlutterWindowCreationRequest request{
6060
.has_size = true,
6161
.size = {.width = 800, .height = 600},
62-
.on_close = [] {},
62+
.on_should_close = [] {},
63+
.on_will_close = [] {},
6364
.notify_listeners = [] {},
6465
};
6566

@@ -83,7 +84,8 @@ void TearDown() {
8384
FlutterWindowCreationRequest request{
8485
.has_size = true,
8586
.size = {.width = 800, .height = 600},
86-
.on_close = [] {},
87+
.on_should_close = [] {},
88+
.on_will_close = [] {},
8789
.notify_listeners = [] {},
8890
};
8991

@@ -125,7 +127,8 @@ void TearDown() {
125127
FlutterWindowCreationRequest request{
126128
.has_size = true,
127129
.size = {.width = 800, .height = 600},
128-
.on_close = [] {},
130+
.on_should_close = [] {},
131+
.on_will_close = [] {},
129132
.notify_listeners = [] {},
130133
};
131134

@@ -145,7 +148,8 @@ void TearDown() {
145148
FlutterWindowCreationRequest request{
146149
.has_size = true,
147150
.size = {.width = 800, .height = 600},
148-
.on_close = [] {},
151+
.on_should_close = [] {},
152+
.on_will_close = [] {},
149153
.notify_listeners = [] {},
150154
};
151155

@@ -164,7 +168,8 @@ void TearDown() {
164168
FlutterWindowCreationRequest request{
165169
.has_size = true,
166170
.size = {.width = 800, .height = 600},
167-
.on_close = [] {},
171+
.on_should_close = [] {},
172+
.on_will_close = [] {},
168173
.notify_listeners = [] {},
169174
};
170175

0 commit comments

Comments
 (0)