From 88853f7edaba81d9b45eb033724b3ac8ff31e347 Mon Sep 17 00:00:00 2001 From: reuk Date: Thu, 20 Jan 2022 19:52:36 +0000 Subject: [PATCH] AlertWindow: Show AlertWindows as modal sheets on macOS when associated component is present --- .../native/juce_mac_Windowing.mm | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/modules/juce_gui_basics/native/juce_mac_Windowing.mm b/modules/juce_gui_basics/native/juce_mac_Windowing.mm index 2f9778cbb6..4027510601 100644 --- a/modules/juce_gui_basics/native/juce_mac_Windowing.mm +++ b/modules/juce_gui_basics/native/juce_mac_Windowing.mm @@ -43,7 +43,7 @@ public: int getResult() const { - switch (getRawResult()) + switch ([getAlert() runModal]) { case NSAlertFirstButtonReturn: return 0; case NSAlertSecondButtonReturn: return 1; @@ -60,8 +60,30 @@ public: private: void handleAsyncUpdate() override { - auto result = getResult(); + if (auto* comp = options.getAssociatedComponent()) + { + if (auto* peer = comp->getPeer()) + { + if (auto* view = static_cast (peer->getNativeHandle())) + { + if (auto* window = [view window]) + { + [getAlert() beginSheetModalForWindow: window completionHandler: ^(NSModalResponse result) + { + handleModalFinished ((int) result); + }]; + return; + } + } + } + } + + handleModalFinished ((int) [getAlert() runModal]); + } + + void handleModalFinished (int result) + { if (callback != nullptr) callback->modalStateFinished (result); @@ -74,7 +96,7 @@ private: [alert addButtonWithTitle: juceStringToNS (button)]; } - NSInteger getRawResult() const + NSAlert* getAlert() const { NSAlert* alert = [[[NSAlert alloc] init] autorelease]; @@ -90,7 +112,7 @@ private: addButton (alert, options.getButtonText (1)); addButton (alert, options.getButtonText (2)); - return [alert runModal]; + return alert; } MessageBoxOptions options;