Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cocoa] Add handler for javascript prompt/input. #1567

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions webview/localization.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@
'cocoa.menu.about': 'About',
'cocoa.menu.services': 'Services',
'cocoa.menu.view': 'View',
'cocoa.menu.edit': 'Edit',
'cocoa.menu.hide': 'Hide',
'cocoa.menu.hideOthers': 'Hide Others',
'cocoa.menu.showAll': 'Show All',
'cocoa.menu.quit': 'Quit',
'cocoa.menu.fullscreen': 'Enter Fullscreen',
'cocoa.menu.cut': 'Cut',
'cocoa.menu.copy': 'Copy',
'cocoa.menu.paste': 'Paste',
'cocoa.menu.selectAll': 'Select All',
'windows.fileFilter.allFiles': 'All files',
'windows.fileFilter.otherFiles': 'Other file types',
'linux.openFile': 'Open file',
Expand Down
63 changes: 63 additions & 0 deletions webview/platforms/cocoa.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,17 @@ def webView_runJavaScriptConfirmPanelWithMessage_initiatedByFrame_completionHand
result = BrowserView.display_confirmation_dialog(ok, cancel, message)
handler(result)

# Display a Javascript input panel
def webView_runJavaScriptTextInputPanelWithPrompt_defaultText_initiatedByFrame_completionHandler_(
self, webview, prompt, default_text, frame, handler
):
i = BrowserView.get_instance('webview', webview)
ok = i.localization['global.ok']
cancel = i.localization['global.cancel']

result = BrowserView.display_input_dialog(ok, cancel, prompt, default_text)
handler(result)

# Display an open panel for <input type="file"> element
def webView_runOpenPanelWithParameters_initiatedByFrame_completionHandler_(
self, webview, param, frame, handler
Expand Down Expand Up @@ -645,6 +656,7 @@ def first_show(self):
self._clear_main_menu()
self._add_app_menu()
self._add_view_menu()
self._add_edit_menu()
self._add_user_menu()

BrowserView.app.activateIgnoringOtherApps_(Foundation.YES)
Expand Down Expand Up @@ -1068,6 +1080,34 @@ def _add_view_menu(self):
AppKit.NSControlKeyMask | AppKit.NSCommandKeyMask
)

def _add_edit_menu(self):
"""
Create a default Edit menu that shows Copy/Paste/etc.
"""
mainMenu = BrowserView.app.mainMenu()

if not mainMenu:
mainMenu = AppKit.NSMenu.alloc().init()
BrowserView.app.setMainMenu_(mainMenu)

# Create an Edit menu and make it a submenu of the main menu
editMenu = AppKit.NSMenu.alloc().init()
editMenu.setTitle_(self.localization['cocoa.menu.edit'])
editMenuItem = AppKit.NSMenuItem.alloc().init()
editMenuItem.setSubmenu_(editMenu)
# Make the edit menu the first item after the application menu
mainMenu.insertItem_atIndex_(editMenuItem, 1)

for (title, action, keyEquivalent) in [
(self.localization['cocoa.menu.cut'], 'cut:', 'x'),
(self.localization['cocoa.menu.copy'], 'copy:', 'c'),
(self.localization['cocoa.menu.paste'], 'paste:', 'v'),
(self.localization['cocoa.menu.selectAll'], 'selectAll:', 'a'),
]:
menuItem = editMenu.addItemWithTitle_action_keyEquivalent_(
title, action, keyEquivalent
)

def _append_app_name(self, val):
"""
Append the application name to a string if it's available. If not, the
Expand Down Expand Up @@ -1132,6 +1172,29 @@ def display_confirmation_dialog(first_button, second_button, message):

return alert.runModal() == AppKit.NSAlertFirstButtonReturn

@staticmethod
def display_input_dialog(first_button, second_button, prompt, default_text):
AppKit.NSApplication.sharedApplication()
AppKit.NSRunningApplication.currentApplication().activateWithOptions_(
AppKit.NSApplicationActivateIgnoringOtherApps
)
alert = AppKit.NSAlert.alloc().init()
text_field = AppKit.NSTextField.alloc().initWithFrame_(
AppKit.NSMakeRect(0, 0, 240, 24)
)
text_field.cell().setScrollable_(True)
text_field.setStringValue_(default_text)
alert.setAccessoryView_(text_field)
alert.addButtonWithTitle_(first_button)
alert.addButtonWithTitle_(second_button)
alert.setMessageText_(prompt)
alert.setAlertStyle_(AppKit.NSWarningAlertStyle)

if alert.runModal() == AppKit.NSAlertFirstButtonReturn:
return text_field.stringValue()
else:
return None

@staticmethod
def should_close(window):
quit = window.localization['global.quit']
Expand Down