Skip to content

Commit d4c31c3

Browse files
committed
[Cocoa] Add handler for javascript prompt/input.
Cocoa was missing handler for calling prompt("...") This adds the popup and input handling
1 parent 0424777 commit d4c31c3

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

webview/platforms/cocoa.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,17 @@ def webView_runJavaScriptConfirmPanelWithMessage_initiatedByFrame_completionHand
206206
result = BrowserView.display_confirmation_dialog(ok, cancel, message)
207207
handler(result)
208208

209+
# Display a Javascript input panel
210+
def webView_runJavaScriptTextInputPanelWithPrompt_defaultText_initiatedByFrame_completionHandler_(
211+
self, webview, prompt, default_text, frame, handler
212+
):
213+
i = BrowserView.get_instance('webview', webview)
214+
ok = i.localization['global.ok']
215+
cancel = i.localization['global.cancel']
216+
217+
result = BrowserView.display_input_dialog(ok, cancel, prompt, default_text)
218+
handler(result)
219+
209220
# Display an open panel for <input type="file"> element
210221
def webView_runOpenPanelWithParameters_initiatedByFrame_completionHandler_(
211222
self, webview, param, frame, handler
@@ -1132,6 +1143,27 @@ def display_confirmation_dialog(first_button, second_button, message):
11321143

11331144
return alert.runModal() == AppKit.NSAlertFirstButtonReturn
11341145

1146+
@staticmethod
1147+
def display_input_dialog(first_button, second_button, prompt, default_text):
1148+
AppKit.NSApplication.sharedApplication()
1149+
AppKit.NSRunningApplication.currentApplication().activateWithOptions_(
1150+
AppKit.NSApplicationActivateIgnoringOtherApps
1151+
)
1152+
alert = AppKit.NSAlert.alloc().init()
1153+
text_field = AppKit.NSTextField.alloc().init()
1154+
text_field.setStringValue_(default_text)
1155+
text_field.sizeToFit()
1156+
alert.setAccessoryView_(text_field)
1157+
alert.addButtonWithTitle_(first_button)
1158+
alert.addButtonWithTitle_(second_button)
1159+
alert.setMessageText_(prompt)
1160+
alert.setAlertStyle_(AppKit.NSWarningAlertStyle)
1161+
1162+
if alert.runModal() == AppKit.NSAlertFirstButtonReturn:
1163+
return text_field.stringValue()
1164+
else:
1165+
return None
1166+
11351167
@staticmethod
11361168
def should_close(window):
11371169
quit = window.localization['global.quit']

0 commit comments

Comments
 (0)