From a44e2a4d5db5b4ebb1bba78a7bdb707637b9f277 Mon Sep 17 00:00:00 2001 From: dmitry Date: Fri, 21 Dec 2018 05:05:24 +0000 Subject: [PATCH] cocoa: adding a windowcontroller to NSAlert to process Escape key, even if there's no cancel button present on the dialog git-svn-id: trunk@59885 - --- lcl/interfaces/cocoa/cocoalclintf.inc | 33 +++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/lcl/interfaces/cocoa/cocoalclintf.inc b/lcl/interfaces/cocoa/cocoalclintf.inc index bff3895640..ed93508294 100644 --- a/lcl/interfaces/cocoa/cocoalclintf.inc +++ b/lcl/interfaces/cocoa/cocoalclintf.inc @@ -163,6 +163,14 @@ begin fClipboard.Sync; end; +type + + { TCocoaAlertController } + + TCocoaAlertController = objcclass(NSWindowController) + procedure keyUp(theEvent: NSEvent); override; + end; + {------------------------------------------------------------------------------ Func: CocoaPromptUser Params: DialogCaption - Dialog caption @@ -200,6 +208,7 @@ var I: Integer; aButton: NSButton; Str: string; + ctrl : TCocoaAlertController; begin {Str := 'TCocoaWidgetSet.PromptUser DialogCaption: ' + DialogCaption + ' DialogMessage: ' + DialogMessage + ' DialogType: ' + DbgS(DialogType) + @@ -272,7 +281,14 @@ begin Result := 0; end else - Result := AnAlert.runModal; + begin + ctrl := TCocoaAlertController(TCocoaAlertController.alloc).initWithWindow(AnAlert.window); + try + Result := AnAlert.runModal; + finally + ctrl.release; + end; + end; finally informativeText.release; messageText.release; @@ -285,7 +301,20 @@ begin {$IFDEF VerboseLCLIntf} DebugLn('TCocoaWidgetSet.PromptUser Result: ' + DbgS(Result)); {$ENDIF} -end; {TCocoaWidgetSet.PromptUser} +end; + +{TCocoaWidgetSet.PromptUser} + +{ TCocoaAlertController } + +procedure TCocoaAlertController.keyUp(theEvent: NSEvent); +const + NSModalResponseCancel = NSCancelButton; // NSCancelButton is deprecated +begin + inherited keyUp(theEvent); + if theEvent.keyCode = kVK_Escape then + NSApplication(NSApp).stopModalWithCode(NSModalResponseCancel); +end; {------------------------------------------------------------------------------ Method: PromptUser