6 #include <Foundation/Foundation.h>
17 : NSObject <NSWindowDelegate, FlutterViewContentDelegate, FlutterViewSizingDelegate> {
29 @property(readonly, nonatomic) NSWindow*
window;
34 - (instancetype)initWithWindow:(NSWindow*)window
49 [
self setContentSize:NSMakeSize(contentSize.width, contentSize.height)];
53 NSSize size = [
self frameRectForContentRect:self.frame].size;
54 NSSize originalSize = size;
55 [
self setContentMinSize:NSMakeSize(constraints.min_width, constraints.min_height)];
56 size.width = std::max(size.width, constraints.
min_width);
57 size.height = std::max(size.height, constraints.
min_height);
59 [
self setContentMaxSize:NSMakeSize(constraints.max_width, constraints.max_height)];
60 size.width = std::min(size.width, constraints.
max_width);
61 size.height = std::min(size.height, constraints.
max_height);
63 [
self setContentMaxSize:NSMakeSize(CGFLOAT_MAX, CGFLOAT_MAX)];
65 if (!NSEqualSizes(originalSize, size)) {
66 [
self setContentSize:size];
83 @synthesize window = _window;
86 - (instancetype)initWithWindow:(NSWindow*)window
89 if (
self = [super init]) {
98 - (void)windowDidBecomeKey:(NSNotification*)notification {
99 [_flutterViewController.engine windowDidBecomeKey:_flutterViewController.viewIdentifier];
102 - (void)windowDidResignKey:(NSNotification*)notification {
103 [_flutterViewController.engine windowDidResignKey:_flutterViewController.viewIdentifier];
104 [[_flutterViewController.engine windowController] windowDidResignKey:self];
107 - (BOOL)windowShouldClose:(NSWindow*)sender {
113 - (void)windowWillClose {
117 - (void)windowDidResize:(NSNotification*)notification {
124 - (void)windowDidMiniaturize:(NSNotification*)notification {
131 - (void)windowDidDeminiaturize:(NSNotification*)notification {
136 - (void)windowWillEnterFullScreen:(NSNotification*)notification {
141 - (void)windowWillExitFullScreen:(NSNotification*)notification {
146 - (std::optional<NSSize>)minimumViewSize:(
FlutterView*)view {
155 - (std::optional<NSSize>)maximumViewSize:(
FlutterView*)view {
160 NSSize screenSize =
self.window.screen.visibleFrame.size;
161 double width = screenSize.width;
166 double height = screenSize.height;
171 return NSMakeSize(width, height);
176 static NSRect ComputeGlobalScreenFrame() {
177 NSRect frame = NSZeroRect;
178 for (NSScreen* screen in [NSScreen screens]) {
179 NSRect screenFrame = screen.frame;
180 if (NSIsEmptyRect(frame)) {
183 frame = NSUnionRect(frame, screenFrame);
189 static void FlipRect(NSRect& rect,
const NSRect& globalScreenFrame) {
191 rect.origin.y = (globalScreenFrame.origin.y + globalScreenFrame.size.height) -
192 (rect.origin.y + rect.size.height);
195 - (void)updatePosition {
196 [
self viewDidUpdateContents:self.flutterViewController.flutterView
197 withSize:self.flutterViewController.flutterView.bounds.size];
200 - (void)viewDidUpdateContents:(
FlutterView*)view withSize:(NSSize)newSize {
203 bool firstFrame =
self.onFirstFrame != nil;
207 [view.window setContentSize:newSize];
214 self.onFirstFrame = nil;
217 NSRect globalScreenFrame = ComputeGlobalScreenFrame();
220 [
self.window.parentWindow contentRectForFrameRect:self.window.parentWindow.frame];
221 FlipRect(parentRect, globalScreenFrame);
223 NSRect screenRect = [
self.window.screen visibleFrame];
224 FlipRect(screenRect, globalScreenFrame);
231 NSRect positionRect = position->
toNSRect();
232 FlipRect(positionRect, globalScreenFrame);
234 [
self.window setFrame:positionRect display:NO animate:NO];
241 (positionRect.size.width < newSize.width || positionRect.size.height < newSize.height)) {
251 self.onFirstFrame = nil;
258 self->_creationRequest.constraints = constraints;
259 [_flutterViewController.flutterView constraintsDidChange];
261 [
self.window flutterSetConstraints:constraints];
272 - (BOOL)canBecomeKeyWindow {
276 - (BOOL)acceptsFirstResponder {
280 - (BOOL)canBecomeMainWindow {
291 - (NSRect)windowWillUseStandardFrame:(NSWindow*)window defaultFrame:(NSRect)newFrame {
293 newFrame.size.width = 110;
294 newFrame.size.height = 110;
302 - (instancetype)init {
305 _windows = [NSMutableArray array];
311 if (_runLoopModeFixApplied) {
321 NSWindow* window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 100, 100)
322 styleMask:NSWindowStyleMaskResizable
323 backing:NSBackingStoreNonretained
327 window.releasedWhenClosed = NO;
328 window.delegate = delegate;
329 window.ignoresMouseEvents = YES;
330 window.alphaValue = 0;
331 [window orderFront:nil];
334 NSArray* modes = (__bridge_transfer NSArray*)CFRunLoopCopyAllModes(CFRunLoopGetCurrent());
335 for (NSString* mode in modes) {
336 if ([mode hasSuffix:
@"MoveTimerRunLoopMode"]) {
337 CFRunLoopAddCommonMode(CFRunLoopGetCurrent(), (__bridge CFStringRef)mode);
340 _runLoopModeFixApplied = YES;
348 NSWindow* window = [[NSWindow alloc] init];
351 [window setReleasedWhenClosed:NO];
353 window.contentViewController = controller;
354 window.styleMask = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable;
355 if (request->resizable) {
356 window.styleMask |= NSWindowStyleMaskResizable;
359 window.collectionBehavior = NSWindowCollectionBehaviorFullScreenAuxiliary;
360 if (request->has_size) {
361 [window flutterSetContentSize:request->size];
363 if (request->resizable && request->has_constraints) {
364 [window flutterSetConstraints:request->constraints];
368 flutterViewController:controller
369 creationRequest:*request];
370 window.delegate = owner;
371 [_windows addObject:owner];
373 if (!request->has_size) {
374 controller.flutterView.sizingDelegate = owner;
375 [controller.flutterView constraintsDidChange];
377 controller.flutterView.contentDelegate = owner;
379 NSWindow* parent = nil;
381 if (request->parent_view_id != 0) {
389 FML_LOG(WARNING) <<
"Failed to find parent window for ID " << request->parent_view_id;
396 dispatch_async(dispatch_get_main_queue(), ^{
397 [
self fixMoveRunLoopModeIfNeeded];
400 [parent beginCriticalSheet:window
401 completionHandler:^(NSModalResponse response){
406 [window setIsVisible:YES];
407 [window makeKeyAndOrderFront:nil];
419 NSWindow* window = [[NSWindow alloc] init];
422 [window setReleasedWhenClosed:NO];
424 window.contentViewController = controller;
425 window.styleMask = NSWindowStyleMaskBorderless;
426 window.hasShadow = NO;
431 flutterViewController:controller
432 creationRequest:*request];
434 controller.flutterView.sizingDelegate = owner;
435 controller.flutterView.contentDelegate = owner;
438 [controller.flutterView constraintsDidChange];
441 window.delegate = owner;
442 [_windows addObject:owner];
444 NSWindow* parent = nil;
446 FML_DCHECK(request->parent_view_id != 0);
454 NSAssert(parent != nil,
@"Tooltip window must have a parent window.");
456 window.ignoresMouseEvents = YES;
457 window.collectionBehavior = NSWindowCollectionBehaviorAuxiliary;
458 [parent addChildWindow:window ordered:NSWindowAbove];
459 window.alphaValue = 0.0;
463 window.alphaValue = 1.0;
480 [window setReleasedWhenClosed:NO];
482 window.contentViewController = controller;
483 window.styleMask = NSWindowStyleMaskBorderless;
484 window.hasShadow = NO;
489 flutterViewController:controller
490 creationRequest:*request];
492 controller.flutterView.sizingDelegate = owner;
493 controller.flutterView.contentDelegate = owner;
494 [controller.flutterView setBackgroundColor:[NSColor clearColor]];
496 [controller.flutterView constraintsDidChange];
499 window.delegate = owner;
500 [_windows addObject:owner];
502 NSWindow* parent = nil;
504 FML_DCHECK(request->parent_view_id != 0);
512 NSAssert(parent != nil,
@"Popup window must have a parent window.");
514 window.collectionBehavior = NSWindowCollectionBehaviorAuxiliary;
515 [parent addChildWindow:window ordered:NSWindowAbove];
516 window.alphaValue = 0.0;
520 window.alphaValue = 1.0;
531 NSWindow* window = [[NSWindow alloc] init];
534 [window setReleasedWhenClosed:NO];
537 NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable;
538 if (request->resizable) {
539 window.styleMask |= NSWindowStyleMaskResizable;
542 window.contentViewController = controller;
544 if (request->has_size) {
545 [window flutterSetContentSize:request->size];
547 if (request->resizable && request->has_constraints) {
548 [window flutterSetConstraints:request->constraints];
552 flutterViewController:controller
553 creationRequest:*request];
555 controller.flutterView.contentDelegate = owner;
557 if (!request->has_size) {
558 controller.flutterView.sizingDelegate = owner;
559 [controller.flutterView constraintsDidChange];
562 window.delegate = owner;
563 [_windows addObject:owner];
567 [window setIsVisible:YES];
568 [window makeKeyAndOrderFront:nil];
574 - (void)destroyWindow:(NSWindow*)window {
577 if (o.window == window) {
583 [_windows removeObject:owner];
584 for (NSWindow* win in owner.
window.sheets) {
585 [
self destroyWindow:win];
588 for (NSWindow* win in owner.
window.childWindows) {
589 [
self destroyWindow:win];
593 [owner.flutterViewController dispose];
594 owner.
window.delegate = nil;
595 [owner.window close];
596 [owner windowWillClose];
602 [owner.flutterViewController dispose];
603 [owner.window close];
605 [_windows removeAllObjects];
608 static BOOL IsChildAncestor(NSWindow* child, NSWindow* ancestor) {
609 NSWindow* current = child.parentWindow;
611 if (current == ancestor) {
614 current = current.parentWindow;
622 if (possibleChild.closeWhenParentResignsKey &&
623 IsChildAncestor(possibleChild.window, parent.window)) {
624 [possibleChild windowShouldClose:possibleChild.window];
637 [engine enableMultiView];
638 return [engine.windowController createRegularWindow:request];
645 [engine enableMultiView];
646 return [engine.windowController createDialogWindow:request];
653 [engine enableMultiView];
654 return [engine.windowController createTooltipWindow:request];
661 [engine enableMultiView];
662 return [engine.windowController createPopupWindow:request];
666 NSWindow* w = (__bridge NSWindow*)window;
668 [engine.windowController destroyWindow:w];
674 return (__bridge
void*)controller.view.window;
678 NSWindow* w = (__bridge NSWindow*)window;
679 NSRect contentRect = [w contentRectForFrameRect:w.frame];
681 .
width = contentRect.size.width,
682 .height = contentRect.size.height,
687 NSWindow* w = (__bridge NSWindow*)window;
688 [w flutterSetContentSize:*size];
694 NSWindow* w = (__bridge NSWindow*)window;
696 [owner setConstraints:*constraints];
700 NSWindow* w = (__bridge NSWindow*)window;
701 w.title = [NSString stringWithUTF8String:title];
705 NSWindow* w = (__bridge NSWindow*)window;
706 if (maximized & !w.isZoomed) {
708 }
else if (!maximized && w.isZoomed) {
714 NSWindow* w = (__bridge NSWindow*)window;
719 NSWindow* w = (__bridge NSWindow*)window;
724 NSWindow* w = (__bridge NSWindow*)window;
725 [w deminiaturize:nil];
729 NSWindow* w = (__bridge NSWindow*)window;
730 return w.isMiniaturized;
734 NSWindow* w = (__bridge NSWindow*)window;
735 bool isFullScreen = (w.styleMask & NSWindowStyleMaskFullScreen) != 0;
736 if (fullScreen && !isFullScreen) {
737 [w toggleFullScreen:nil];
738 }
else if (!fullScreen && isFullScreen) {
739 [w toggleFullScreen:nil];
744 NSWindow* w = (__bridge NSWindow*)window;
745 return (w.styleMask & NSWindowStyleMaskFullScreen) != 0;
749 NSWindow* w = (__bridge NSWindow*)window;
750 [NSApplication.sharedApplication activateIgnoringOtherApps:YES];
751 [w makeKeyAndOrderFront:nil];
755 NSWindow* w = (__bridge NSWindow*)window;
756 return strdup(w.title.UTF8String);
760 NSWindow* w = (__bridge NSWindow*)window;
761 return w.isKeyWindow;
765 NSWindow* w = (__bridge NSWindow*)window;
767 [owner updatePosition];
771 NSWindow* w = (__bridge NSWindow*)window;
772 NSWindow* parent = w.parentWindow;
776 NSRect globalScreenFrame = ComputeGlobalScreenFrame();
778 NSRect parentRect = [parent contentRectForFrameRect:parent.frame];
779 FlipRect(parentRect, globalScreenFrame);
781 NSRect childRect = w.frame;
782 FlipRect(childRect, globalScreenFrame);
785 .x = childRect.origin.x - parentRect.origin.x,
786 .y = childRect.origin.y - parentRect.origin.y,
#define FLUTTER_DARWIN_EXPORT
int64_t FlutterViewIdentifier
void InternalFlutter_Window_Activate(void *window)
void * InternalFlutter_Window_GetHandle(int64_t engine_id, FlutterViewIdentifier view_id)
bool InternalFlutter_Window_IsMaximized(void *window)
int64_t InternalFlutter_WindowController_CreateTooltipWindow(int64_t engine_id, const FlutterWindowCreationRequest *request)
int64_t InternalFlutter_WindowController_CreatePopupWindow(int64_t engine_id, const FlutterWindowCreationRequest *request)
FLUTTER_DARWIN_EXPORT void InternalFlutter_Window_SetConstraints(void *window, const FlutterWindowConstraints *constraints)
bool InternalFlutter_Window_IsActivated(void *window)
int64_t InternalFlutter_WindowController_CreateDialogWindow(int64_t engine_id, const FlutterWindowCreationRequest *request)
FlutterWindowOffset InternalFlutter_Window_GetOffsetInParent(void *window)
int64_t InternalFlutter_WindowController_CreateRegularWindow(int64_t engine_id, const FlutterWindowCreationRequest *request)
void InternalFlutter_Window_SetFullScreen(void *window, bool fullScreen)
void InternalFlutter_Window_UpdatePosition(void *window)
bool InternalFlutter_Window_IsFullScreen(void *window)
void InternalFlutter_Window_SetContentSize(void *window, const FlutterWindowSize *size)
void InternalFlutter_Window_Minimize(void *window)
char * InternalFlutter_Window_GetTitle(void *window)
void InternalFlutter_Window_SetTitle(void *window, const char *title)
bool InternalFlutter_Window_IsMinimized(void *window)
void InternalFlutter_Window_Destroy(int64_t engine_id, void *window)
void InternalFlutter_Window_SetMaximized(void *window, bool maximized)
FlutterWindowSize InternalFlutter_Window_GetContentSize(void *window)
void InternalFlutter_Window_Unminimize(void *window)
NSMutableArray< FlutterWindowOwner * > * _windows
BOOL _runLoopModeFixApplied
NSColor * backgroundColor
FlutterMouseTrackingMode mouseTrackingMode
FlutterViewIdentifier viewIdentifier
void constraintsDidChange()
id< FlutterViewSizingDelegate > sizingDelegate
void fixMoveRunLoopModeIfNeeded()
dispatch_block_t onFirstFrame
FlutterViewController * _flutterViewController
FlutterViewController * flutterViewController
std::optional< flutter::Isolate > _isolate
CGSize _positionerSizeConstraints
FlutterWindowCreationRequest _creationRequest
BOOL closeWhenParentResignsKey
void(* on_should_close)()
void(* notify_listeners)()
struct FlutterWindowConstraints constraints
FlutterWindowRect *(* on_get_window_position)(const FlutterWindowSize &child_size, const FlutterWindowRect &parent_rect, const FlutterWindowRect &output_rect)
static FlutterWindowRect fromNSRect(const NSRect &rect)
static FlutterWindowSize fromNSSize(const NSSize &size)