1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
|
from __future__ import annotations
import json
import logging
import os
import urllib
import uuid
import webbrowser
from threading import Semaphore, Thread, main_thread
import AppKit
import Foundation
import WebKit
from objc import nil, super
from PyObjCTools import AppHelper
from webview import FileDialog, _state, windows
from webview import settings as webview_settings
from webview.dom import _dnd_state
from webview.menu import Menu, MenuAction, MenuSeparator
from webview.models import Request, Response
from webview.screen import Screen
from webview.util import (
DEFAULT_HTML,
create_cookie,
inject_pywebview,
js_bridge_call,
parse_file_type,
stringify_headers,
)
from webview.window import FixPoint
# This lines allow to load non-HTTPS resources, like a local app as: http://127.0.0.1:5000
bundle = AppKit.NSBundle.mainBundle()
info = bundle.localizedInfoDictionary() or bundle.infoDictionary()
info['NSAppTransportSecurity'] = {'NSAllowsArbitraryLoads': Foundation.YES}
info['NSRequiresAquaSystemAppearance'] = Foundation.NO # Enable dark mode support for Mojave
# Fallbacks, in case these constants are not wrapped by PyObjC
try:
NSFullSizeContentViewWindowMask = AppKit.NSFullSizeContentViewWindowMask
except AttributeError:
NSFullSizeContentViewWindowMask = 1 << 15
try:
NSWindowTitleHidden = AppKit.NSWindowTitleHidden
except AttributeError:
NSWindowTitleHidden = 1
logger = logging.getLogger('pywebview')
logger.debug('Using Cocoa')
renderer = 'wkwebview'
class BrowserView:
instances = {}
app = AppKit.NSApplication.sharedApplication()
app.setActivationPolicy_(0)
current_menu = None
cascade_loc = Foundation.NSMakePoint(100.0, 0.0)
class AppDelegate(AppKit.NSObject):
def applicationShouldTerminate_(self, app):
should_close = True
for i in BrowserView.instances.values():
should_close = should_close and BrowserView.should_close(i.pywebview_window)
return Foundation.YES if should_close else Foundation.NO
def applicationSupportsSecureRestorableState_(self, app):
return Foundation.YES
class WindowHost(AppKit.NSWindow):
def canBecomeKeyWindow(self):
return self.focus
class WindowDelegate(AppKit.NSObject):
def windowDidBecomeKey_(self, notification):
i = BrowserView.get_instance('window', notification.object())
if i.menu and BrowserView.current_menu != i.menu:
BrowserView.current_menu = i.menu
new_menu = i._recreate_menus(BrowserView.current_menu)
BrowserView.app.setMainMenu_(new_menu)
def windowShouldClose_(self, window):
i = BrowserView.get_instance('window', window)
return BrowserView.should_close(i.pywebview_window)
def windowWillClose_(self, notification):
# Delete the closed instance from the dict
i = BrowserView.get_instance('window', notification.object())
del BrowserView.instances[i.uid]
if i.pywebview_window in windows:
windows.remove(i.pywebview_window)
i.webview.setNavigationDelegate_(None)
i.webview.setUIDelegate_(None)
# this seems to be a bug in WkWebView, so we need to load blank html
# see https://stackoverflow.com/questions/27410413/wkwebview-embed-video-keeps-playing-sound-after-release
i.webview.loadHTMLString_baseURL_('', None)
i.webview.removeFromSuperview()
i.webview = None
i.closed.set()
if BrowserView.instances == {}:
BrowserView.app.stop_(self)
BrowserView.app.abortModal()
def windowDidResize_(self, notification):
i = BrowserView.get_instance('window', notification.object())
if i:
size = i.window.frame().size
i.pywebview_window.events.resized.set(size.width, size.height)
def windowDidMiniaturize_(self, notification):
i = BrowserView.get_instance('window', notification.object())
if i:
i.pywebview_window.events.minimized.set()
def windowDidDeminiaturize_(self, notification):
i = BrowserView.get_instance('window', notification.object())
if i:
i.pywebview_window.events.restored.set()
def windowDidEnterFullScreen_(self, notification):
i = BrowserView.get_instance('window', notification.object())
if i:
i.pywebview_window.events.maximized.set()
def windowDidExitFullScreen_(self, notification):
i = BrowserView.get_instance('window', notification.object())
if i:
i.pywebview_window.events.restored.set()
def windowDidMove_(self, notification):
i = BrowserView.get_instance('window', notification.object())
if i:
frame = i.window.frame()
screen = i.window.screen().frame()
flipped_y = screen.size.height - frame.size.height - frame.origin.y
i.pywebview_window.events.moved.set(frame.origin.x, flipped_y)
class JSBridge(AppKit.NSObject):
def initWithObject_(self, window):
super(BrowserView.JSBridge, self).init()
self.window = window
return self
def userContentController_didReceiveScriptMessage_(self, controller, message):
body = json.loads(message.body())
if body['params'] is WebKit.WebUndefined.undefined():
body['params'] = None
js_bridge_call(self.window, body['funcName'], body['params'], body['id'])
class DownloadDelegate(AppKit.NSObject):
# Download delegate to handle links with download attribute set
def download_decideDestinationUsingResponse_suggestedFilename_completionHandler_(
self, download, decideDestinationUsingResponse, suggestedFilename, completionHandler
):
save_dlg = AppKit.NSSavePanel.savePanel()
directory = Foundation.NSSearchPathForDirectoriesInDomains(
Foundation.NSDownloadsDirectory, Foundation.NSUserDomainMask, True
)[0]
save_dlg.setDirectoryURL_(Foundation.NSURL.fileURLWithPath_(directory))
save_dlg.setNameFieldStringValue_(suggestedFilename)
if save_dlg.runModal() == AppKit.NSFileHandlingPanelOKButton:
filename = save_dlg.filename()
url = Foundation.NSURL.fileURLWithPath_(filename)
completionHandler(url)
else:
completionHandler(None)
class BrowserDelegate(AppKit.NSObject):
# Display a JavaScript alert panel containing the specified message
def webView_runJavaScriptAlertPanelWithMessage_initiatedByFrame_completionHandler_(
self, webview, message, frame, handler
):
AppKit.NSRunningApplication.currentApplication().activateWithOptions_(
AppKit.NSApplicationActivateIgnoringOtherApps
)
alert = AppKit.NSAlert.alloc().init()
alert.setInformativeText_(str(message))
alert.runModal()
handler()
def webView_didReceiveAuthenticationChallenge_completionHandler_(
self, webview, challenge, handler
):
# Prevent `ObjCPointerWarning: PyObjCPointer created: ... type ^{__SecTrust=}`
# this allows any server cert
if webview_settings['IGNORE_SSL_ERRORS'] or _state['ssl']:
credential = AppKit.NSURLCredential.credentialForTrust_(
challenge.protectionSpace().serverTrust()
)
handler(AppKit.NSURLSessionAuthChallengeUseCredential, credential)
else:
handler(AppKit.NSURLSessionAuthChallengePerformDefaultHandling, nil)
# Display a JavaScript confirm panel containing the specified message
def webView_runJavaScriptConfirmPanelWithMessage_initiatedByFrame_completionHandler_(
self, webview, message, frame, handler
):
i = BrowserView.get_instance('webview', webview)
ok = i.localization['global.ok']
cancel = i.localization['global.cancel']
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
):
i = list(BrowserView.instances.values())[0]
file_filter = param._acceptedMIMETypes()
files = i.create_file_dialog(
FileDialog.OPEN,
'',
param.allowsMultipleSelection(),
'',
file_filter,
main_thread=True,
)
if files:
urls = [Foundation.NSURL.fileURLWithPath_(i) for i in files]
handler(urls)
else:
handler(nil)
# Open target="_blank" links in external browser
def webView_createWebViewWithConfiguration_forNavigationAction_windowFeatures_(
self, webview, config, action, features
):
if action.navigationType() == getattr(WebKit, 'WKNavigationTypeLinkActivated', 0):
if webview_settings['OPEN_EXTERNAL_LINKS_IN_BROWSER']:
webbrowser.open(action.request().URL().absoluteString(), 2, True)
else:
webview.loadRequest_(action.request())
return nil
# WKNavigationDelegate method, invoked when a navigation decision needs to be made
def webView_decidePolicyForNavigationAction_decisionHandler_(
self, webview, action, handler
):
# The event that might have triggered the navigation
event = AppKit.NSApp.currentEvent()
# Handle links with the download attribute set to recommend a file name
if action.shouldPerformDownload() and webview_settings['ALLOW_DOWNLOADS']:
handler(getattr(WebKit, 'WKNavigationActionPolicyDownload', 2))
return
""" Disable back navigation on pressing the Delete key: """
# Check if the requested navigation action is Back/Forward
if action.navigationType() == getattr(WebKit, 'WKNavigationTypeBackForward', 2):
# Check if the event is a Delete key press (keyCode = 51)
if event and event.type() == AppKit.NSKeyDown and event.keyCode() == 51:
# If so, ignore the request and return
handler(getattr(WebKit, 'WKNavigationActionPolicyCancel', 0))
return
request = action.request()
url = request.URL()
original_headers = dict(request.allHTTPHeaderFields())
i = BrowserView.get_instance('webview', webview)
if (
len(i.pywebview_window.events.request_sent) > 0
and 'X-Handled' not in original_headers
and str(url) != 'about:blank'
):
request_ = Request(str(url), request.HTTPMethod(), original_headers)
i.pywebview_window.events.request_sent.set(request_)
request_.headers['X-Handled'] = 'true'
new_request = Foundation.NSMutableURLRequest.requestWithURL_(url)
new_request.setHTTPMethod_(request.HTTPMethod())
new_request.setAllHTTPHeaderFields_(
AppKit.NSDictionary(stringify_headers(request_.headers))
)
new_request.setHTTPBody_(request.HTTPBody())
new_request.setHTTPBodyStream_(request.HTTPBodyStream())
new_request.setHTTPShouldHandleCookies_(request.HTTPShouldHandleCookies())
new_request.setHTTPShouldUsePipelining_(request.HTTPShouldUsePipelining())
new_request.setMainDocumentURL_(request.mainDocumentURL())
new_request.setNetworkServiceType_(request.networkServiceType())
webview.loadRequest_(new_request)
handler(getattr(WebKit, 'WKNavigationActionPolicyCancel', 1))
else:
# Normal navigation, allow
handler(getattr(WebKit, 'WKNavigationActionPolicyAllow', 1))
def webView_navigationAction_didBecomeDownload_(self, webview, navigationAction, download):
download.setDelegate_(BrowserView.DownloadDelegate.alloc().init().retain())
def webView_decidePolicyForNavigationResponse_decisionHandler_(
self, webview, navigationResponse, decisionHandler
):
if navigationResponse.canShowMIMEType():
response_ = navigationResponse.response()
headers = dict(response_.allHeaderFields())
response = Response(
response_.URL().absoluteString(), response_.statusCode(), headers
)
webview.pywebview_window.events.response_received.set(response)
decisionHandler(WebKit.WKNavigationResponsePolicyAllow)
elif webview_settings['ALLOW_DOWNLOADS']:
decisionHandler(WebKit.WKNavigationResponsePolicyCancel)
save_filename = navigationResponse.response().suggestedFilename()
save_dlg = AppKit.NSSavePanel.savePanel()
save_dlg.setTitle_(webview.pywebview_window.localization['global.saveFile'])
directory = Foundation.NSSearchPathForDirectoriesInDomains(
Foundation.NSDownloadsDirectory, Foundation.NSUserDomainMask, True
)[0]
save_dlg.setDirectoryURL_(Foundation.NSURL.fileURLWithPath_(directory))
if save_filename: # set file name
save_dlg.setNameFieldStringValue_(save_filename)
if save_dlg.runModal() == AppKit.NSFileHandlingPanelOKButton:
self._file_name = save_dlg.filename()
dataTask = Foundation.NSURLSession.sharedSession().downloadTaskWithURL_completionHandler_(
navigationResponse.response().URL(), self.download_completionHandler_error_
)
dataTask.resume()
else:
self._file_name = None
else:
decisionHandler(WebKit.WKNavigationResponsePolicyCancel)
def download_completionHandler_error_(self, temporaryLocation, response, error):
if error is not None:
logger.error('Download error:', error)
else:
if temporaryLocation is not None:
destinationURL = Foundation.NSURL.fileURLWithPath_(self._file_name)
fileManager = Foundation.NSFileManager.defaultManager()
try:
fileManager.moveItemAtURL_toURL_error_(
temporaryLocation, destinationURL, None
)
except Foundation.NSError as moveError:
logger.exception(moveError)
# Show the webview when it finishes loading
def webView_didFinishNavigation_(self, webview, nav):
# Add the webview to the window if it's not yet the contentView
i = BrowserView.get_instance('webview', webview)
if i:
if not webview.window():
i.window.setContentView_(webview)
i.window.makeFirstResponder_(webview)
inject_pywebview('cocoa', i.js_bridge.window)
if _state['debug'] and webview_settings['OPEN_DEVTOOLS_IN_DEBUG']:
BrowserView._open_web_inspector(webview)
# Handle JavaScript window.print()
def userContentController_didReceiveScriptMessage_(self, controller, message):
if message.body() == 'print':
i = BrowserView.get_instance('_browserDelegate', self)
BrowserView.print_webview(i.webview)
class FileFilterChooser(AppKit.NSPopUpButton):
def initWithFilter_(self, file_filter):
super(BrowserView.FileFilterChooser, self).init()
self.filter = file_filter
self.addItemsWithTitles_([i[0] for i in self.filter])
self.setAction_('onChange:')
self.setTarget_(self)
return self
def setFileDialog_(self, file_dlg):
self.file_dlg = file_dlg
def onChange_(self, sender):
option = sender.indexOfSelectedItem()
self.file_dlg.setAllowedFileTypes_(self.filter[option][1])
class WebKitHost(WebKit.WKWebView):
def performDragOperation_(self, sender):
if sender.draggingSource() is None and _dnd_state['num_listeners'] > 0:
pboard = sender.draggingPasteboard()
classes = [AppKit.NSURL]
options = {
AppKit.NSPasteboardURLReadingFileURLsOnlyKey: AppKit.NSNumber.numberWithBool_(
True
)
}
urls = pboard.readObjectsForClasses_options_(classes, options) or []
files = [
(
os.path.basename(os.path.dirname(file_path))
if os.path.isdir(file_path)
else os.path.basename(file_path),
file_path,
)
for url in urls
for file_path in [
urllib.parse.unquote(
url.filePathURL().absoluteString().replace('file://', '')
)
]
if os.path.isdir(file_path) or os.path.isfile(file_path)
]
_dnd_state['paths'] += files
return super(BrowserView.WebKitHost, self).performDragOperation_(sender)
def addSubview_(self, event):
# translate the event from the web coordinate space into the window coordinate space
# basically flip the y axis since the WKWebView/WebKitHost implements a flipped view
# (but seems to miss this flip back when adding a subview)
event_frame = event.frame()
flipped_y = self.frame().size.height - (event_frame.origin.y + event_frame.size.height)
event.setFrameOrigin_(AppKit.NSPoint(event_frame.origin.x, flipped_y))
super(BrowserView.WebKitHost, self).addSubview_(event)
def mouseDown_(self, event):
i = BrowserView.get_instance('webview', self)
window = self.window()
if i.frameless and i.easy_drag:
windowFrame = window.frame()
if windowFrame is None:
raise RuntimeError('Failed to obtain screen')
self.initialLocation = window.convertBaseToScreen_(event.locationInWindow())
self.initialLocation.x -= windowFrame.origin.x
self.initialLocation.y -= windowFrame.origin.y
super(BrowserView.WebKitHost, self).mouseDown_(event)
def mouseDragged_(self, event):
i = BrowserView.get_instance('webview', self)
window = self.window()
if i.frameless and i.easy_drag:
screenFrame = i.screen
if screenFrame is None:
raise RuntimeError('Failed to obtain screen')
windowFrame = window.frame()
if windowFrame is None:
raise RuntimeError('Failed to obtain frame')
currentLocation = window.convertBaseToScreen_(
window.mouseLocationOutsideOfEventStream()
)
newOrigin = AppKit.NSMakePoint(
(currentLocation.x - self.initialLocation.x),
(currentLocation.y - self.initialLocation.y),
)
if (newOrigin.y + windowFrame.size.height) > (
screenFrame.origin.y + screenFrame.size.height
):
newOrigin.y = screenFrame.origin.y + (
screenFrame.size.height + windowFrame.size.height
)
window.setFrameOrigin_(newOrigin)
if event.modifierFlags() & getattr(AppKit, 'NSEventModifierFlagControl', 1 << 18):
i = BrowserView.get_instance('webview', self)
if not _state['debug']:
return
super(BrowserView.WebKitHost, self).mouseDown_(event)
def willOpenMenu_withEvent_(self, menu, event):
if not _state['debug']:
menu.removeAllItems()
def keyDown_(self, event):
if event.modifierFlags() & AppKit.NSCommandKeyMask:
responder = self.window().firstResponder()
if responder != None:
range_ = responder.selectedRange()
hasSelectedText = len(range_) > 0
char = event.characters()
if char == 'x' and hasSelectedText: # cut
responder.cut_(self)
return
elif char == 'c' and hasSelectedText: # copy
responder.copy_(self)
return
elif char == 'v': # paste
responder.paste_(self)
return
elif char == 'a': # select all
responder.selectAll_(self)
return
elif char == 'z': # undo
if responder.undoManager().canUndo():
responder.undoManager().undo()
return
elif char == 'q': # quit
BrowserView.app.stop_(self)
return
elif char == 'w': # close
self.window().performClose_(event)
return
super(BrowserView.WebKitHost, self).keyDown_(event)
def __init__(self, window):
BrowserView.instances[window.uid] = self
self.uid = window.uid
self.pywebview_window = window
self.js_bridge = None
self._file_name = None
self._file_name_semaphore = Semaphore(0)
self._current_url_semaphore = Semaphore(0)
self.closed = window.events.closed
self.closing = window.events.closing
self.shown = window.events.shown
self.loaded = window.events.loaded
self.confirm_close = window.confirm_close
self.title = window.title
self.is_fullscreen = False
self.hidden = window.hidden
self.minimized = window.minimized
self.maximized = window.maximized
self.localization = window.localization
if window.screen:
self.screen = window.screen.frame
else:
self.screen = AppKit.NSScreen.mainScreen().frame()
rect = AppKit.NSMakeRect(0.0, 0.0, window.initial_width, window.initial_height)
window_mask = (
AppKit.NSTitledWindowMask
| AppKit.NSClosableWindowMask
| AppKit.NSMiniaturizableWindowMask
)
if window.resizable:
window_mask = window_mask | AppKit.NSResizableWindowMask
if window.frameless:
window_mask = (
window_mask
| NSFullSizeContentViewWindowMask
| AppKit.NSTexturedBackgroundWindowMask
)
self.menu = window.menu or _state['menu']
# The allocated resources are retained because we would explicitly delete
# this instance when its window is closed
self.window = (
BrowserView.WindowHost.alloc()
.initWithContentRect_styleMask_backing_defer_(
rect, window_mask, AppKit.NSBackingStoreBuffered, False
)
.retain()
)
self.pywebview_window.native = self.window
self.window.focus = window.focus
self.window.setTitle_(window.title)
self.window.setMinSize_(AppKit.NSSize(window.min_size[0], window.min_size[1]))
self.window.setAnimationBehavior_(AppKit.NSWindowAnimationBehaviorDocumentWindow)
BrowserView.cascade_loc = self.window.cascadeTopLeftFromPoint_(BrowserView.cascade_loc)
frame = self.window.frame()
frame.size.width = window.initial_width
frame.size.height = window.initial_height
self.window.setFrame_display_(frame, True)
config = WebKit.WKWebViewConfiguration.alloc().init()
self.webview = (
BrowserView.WebKitHost.alloc().initWithFrame_configuration_(rect, config).retain()
)
self.webview.pywebview_window = window
self._browserDelegate = BrowserView.BrowserDelegate.alloc().init().retain()
self._windowDelegate = BrowserView.WindowDelegate.alloc().init().retain()
self._appDelegate = BrowserView.AppDelegate.alloc().init().retain()
BrowserView.app.setDelegate_(self._appDelegate)
self.webview.setUIDelegate_(self._browserDelegate)
self.webview.setNavigationDelegate_(self._browserDelegate)
self.window.setDelegate_(self._windowDelegate)
config.userContentController().addScriptMessageHandler_name_(
self._browserDelegate, 'browserDelegate'
)
self.datastore = WebKit.WKWebsiteDataStore.defaultDataStore()
if _state['private_mode']:
# nonPersisentDataStore preserves cookies for some unknown reason. For this reason we use default datastore
# and clear all the cookies beforehand
def dummy_completion_handler():
pass
data_types = WebKit.WKWebsiteDataStore.allWebsiteDataTypes()
from_start = WebKit.NSDate.dateWithTimeIntervalSince1970_(0)
config.setWebsiteDataStore_(self.datastore)
self.datastore.removeDataOfTypes_modifiedSince_completionHandler_(
data_types, from_start, dummy_completion_handler
)
else:
config.setWebsiteDataStore_(self.datastore)
try:
config.preferences().setValue_forKey_(False, 'backspaceKeyNavigationEnabled')
except KeyError:
pass # backspaceKeyNavigationEnabled does not exist prior to macOS Mojave
config.preferences().setValue_forKey_(
webview_settings['ALLOW_FILE_URLS'], 'allowFileAccessFromFileURLs'
)
if _state['debug']:
config.preferences().setValue_forKey_(True, 'developerExtrasEnabled')
self.js_bridge = BrowserView.JSBridge.alloc().initWithObject_(window)
config.userContentController().addScriptMessageHandler_name_(self.js_bridge, 'jsBridge')
user_agent = webview_settings.get('user_agent') or _state['user_agent']
if user_agent:
self.webview.setCustomUserAgent_(user_agent)
self.window.setFrameOrigin_(self.screen.origin)
if window.initial_x is not None and window.initial_y is not None:
self.move(window.initial_x, window.initial_y)
else:
self.center()
if window.transparent:
self.window.setOpaque_(False)
self.window.setHasShadow_(False)
self.window.setBackgroundColor_(
BrowserView.nscolor_from_hex(window.background_color, 0)
)
self.webview.setValue_forKey_(True, 'drawsTransparentBackground')
else:
self.window.setBackgroundColor_(BrowserView.nscolor_from_hex(window.background_color))
if window.vibrancy:
frame_vibrancy = AppKit.NSMakeRect(0, 0, frame.size.width, frame.size.height)
visualEffectView = AppKit.NSVisualEffectView.new()
visualEffectView.setAutoresizingMask_(
AppKit.NSViewWidthSizable | AppKit.NSViewHeightSizable
)
visualEffectView.setWantsLayer_(True)
visualEffectView.setFrame_(frame_vibrancy)
visualEffectView.setState_(AppKit.NSVisualEffectStateActive)
visualEffectView.setBlendingMode_(AppKit.NSVisualEffectBlendingModeBehindWindow)
self.webview.addSubview_positioned_relativeTo_(
visualEffectView, AppKit.NSWindowBelow, self.webview
)
self.frameless = window.frameless
self.easy_drag = window.easy_drag
if window.frameless:
# Make content full size and titlebar transparent
self.window.setTitlebarAppearsTransparent_(True)
self.window.setTitleVisibility_(NSWindowTitleHidden)
self.window.standardWindowButton_(AppKit.NSWindowCloseButton).setHidden_(True)
self.window.standardWindowButton_(AppKit.NSWindowMiniaturizeButton).setHidden_(True)
self.window.standardWindowButton_(AppKit.NSWindowZoomButton).setHidden_(True)
else:
# Set the titlebar color (so that it does not change with the window color)
self.window.contentView().superview().subviews().lastObject().setBackgroundColor_(
AppKit.NSColor.windowBackgroundColor()
)
if window.on_top:
self.window.setLevel_(AppKit.NSStatusWindowLevel)
self.pywebview_window.events.before_show.set()
if window.real_url:
self.url = window.real_url
self.load_url(window.real_url)
elif window.html:
self.load_html(window.html, '')
else:
self.load_html(DEFAULT_HTML, '')
if window.fullscreen:
self.toggle_fullscreen()
def first_show(self):
if not self.hidden:
self.window.makeKeyAndOrderFront_(self.window)
if self.maximized:
self.maximize()
elif self.minimized:
self.minimize()
self.shown.set()
if not BrowserView.app.isRunning():
new_menu = self._recreate_menus(self.menu)
BrowserView.app.setMainMenu_(new_menu)
BrowserView.app.activateIgnoringOtherApps_(Foundation.YES)
AppHelper.installMachInterrupt()
BrowserView.app.run()
def show(self):
def _show():
self.window.makeKeyAndOrderFront_(self.window)
BrowserView.app.activateIgnoringOtherApps_(Foundation.YES)
AppHelper.callAfter(_show)
def hide(self):
AppHelper.callAfter(self.window.orderOut_, self.window)
def destroy(self):
AppHelper.callAfter(self.window.close)
def set_title(self, title):
AppHelper.callAfter(self.window.setTitle_, title)
def toggle_fullscreen(self):
def toggle():
if self.is_fullscreen:
window_behaviour = 1 << 2 # NSWindowCollectionBehaviorManaged
else:
window_behaviour = 1 << 7 # NSWindowCollectionBehaviorFullScreenPrimary
self.window.setCollectionBehavior_(window_behaviour)
self.window.toggleFullScreen_(None)
AppHelper.callAfter(toggle)
self.is_fullscreen = not self.is_fullscreen
def resize(self, width, height, fix_point):
def _resize():
frame = self.window.frame()
if fix_point and fix_point & FixPoint.EAST:
# Keep the right of the window in the same place
frame.origin.x += frame.size.width - width
if fix_point and fix_point & FixPoint.NORTH:
# Keep the top of the window in the same place
frame.origin.y += frame.size.height - height
frame.size.width = width
frame.size.height = height
if not fix_point: # used in maximized
frame.origin.x = 0
frame.origin.y = 0
self.window.setFrame_display_(frame, True)
AppHelper.callAfter(_resize)
def maximize(self):
width = self.screen.size.width
height = self.screen.size.height
self.resize(width, height, None)
def minimize(self):
AppHelper.callAfter(self.window.miniaturize_, self)
def restore(self):
AppHelper.callAfter(self.window.deminiaturize_, self)
def move(self, x, y):
flipped_y = self.screen.size.height - y
self.window.setFrameTopLeftPoint_(
AppKit.NSPoint(self.screen.origin.x + x, self.screen.origin.y + flipped_y)
)
def center(self):
window_frame = self.window.frame()
window_frame.origin.x = (
self.screen.origin.x + (self.screen.size.width - window_frame.size.width) / 2
)
window_frame.origin.y = (
self.screen.origin.y + (self.screen.size.height - window_frame.size.height) / 2
)
self.window.setFrameOrigin_(window_frame.origin)
def clear_cookies(self):
def clear():
self.datastore.removeDataOfTypes_modifiedSince_completionHandler_(
WebKit.WKWebsiteDataStore.allWebsiteDataTypes(),
Foundation.NSDate.dateWithTimeIntervalSince1970_(0),
lambda: None,
)
AppHelper.callAfter(clear)
def get_cookies(self):
def handler(cookies):
for c in cookies:
domain = c.domain()[1:] if c.domain().startswith('.') else c.domain()
if domain not in self.url:
continue
data = {
'name': c.name(),
'value': c.value(),
'path': c.path(),
'domain': c.domain(),
'expires': c.expiresDate(),
'secure': c.isSecure(),
'httponly': c.isHTTPOnly(),
'samesite': c.SameSitePolicy(),
}
cookie = create_cookie(data)
_cookies.append(cookie)
cookie_semaphore.release()
_cookies = []
AppHelper.callAfter(self.datastore.httpCookieStore().getAllCookies_, handler)
cookie_semaphore = Semaphore(0)
cookie_semaphore.acquire()
return _cookies
def get_current_url(self):
def get():
self._current_url = str(self.webview.URL())
self._current_url_semaphore.release()
AppHelper.callAfter(get)
self._current_url_semaphore.acquire()
return None if self._current_url == 'about:blank' else self._current_url
def load_url(self, url):
def load(url):
page_url = Foundation.NSURL.URLWithString_(BrowserView.quote(url))
req = Foundation.NSURLRequest.requestWithURL_(page_url)
self.webview.loadRequest_(req)
self.url = url
AppHelper.callAfter(load, url)
def load_html(self, content, base_uri):
def load(content, url):
url = Foundation.NSURL.URLWithString_(BrowserView.quote(url))
self.webview.loadHTMLString_baseURL_(content, url)
AppHelper.callAfter(load, content, base_uri)
def evaluate_js(self, script, parse_json):
def eval():
self.webview.evaluateJavaScript_completionHandler_(script, handler)
def handler(result, error):
if parse_json and result:
try:
JSResult.result = json.loads(result)
except Exception:
logger.exception('Failed to parse JSON: ' + result)
JSResult.result = result
else:
JSResult.result = result
JSResult.result_semaphore.release()
class JSResult:
result = None
result_semaphore = Semaphore(0)
AppHelper.callAfter(eval)
JSResult.result_semaphore.acquire()
return JSResult.result
def create_file_dialog(
self, dialog_type, directory, allow_multiple, save_filename, file_filter, main_thread=False
):
def create_dialog(*args):
dialog_type = args[0]
if dialog_type == FileDialog.SAVE:
save_filename = args[2]
save_dlg = AppKit.NSSavePanel.savePanel()
save_dlg.setTitle_(self.localization['global.saveFile'])
if directory: # set initial directory
save_dlg.setDirectoryURL_(Foundation.NSURL.fileURLWithPath_(directory))
if save_filename: # set file name
save_dlg.setNameFieldStringValue_(save_filename)
if save_dlg.runModal() == AppKit.NSFileHandlingPanelOKButton:
self._file_name = save_dlg.filename()
else:
self._file_name = None
else:
allow_multiple = args[1]
open_dlg = AppKit.NSOpenPanel.openPanel()
# Enable the selection of files in the dialog.
open_dlg.setCanChooseFiles_(dialog_type != FileDialog.FOLDER)
# Enable the selection of directories in the dialog.
open_dlg.setCanChooseDirectories_(dialog_type == FileDialog.FOLDER)
# Enable creating new folders in folder dialogs (macOS)
if dialog_type == FileDialog.FOLDER:
open_dlg.setCanCreateDirectories_(True)
# Enable / disable multiple selection
open_dlg.setAllowsMultipleSelection_(allow_multiple)
# Set allowed file extensions
if file_filter:
if isinstance(file_filter, Foundation.WKNSArray):
try:
import UniformTypeIdentifiers
UTType = UniformTypeIdentifiers.UTType
except ImportError:
UTType = None # Fallback if UTType is not available
open_dlg.setAllowedContentTypes_([UTType.typeWithMIMEType_('image/jpg')])
else:
open_dlg.setAllowedFileTypes_(file_filter[0][1])
if len(file_filter) > 1:
filter_chooser = BrowserView.FileFilterChooser.alloc().initWithFilter_(
file_filter
)
filter_chooser.setFileDialog_(open_dlg)
open_dlg.setAccessoryView_(filter_chooser)
open_dlg.setAccessoryViewDisclosed_(True)
if directory: # set initial directory
open_dlg.setDirectoryURL_(Foundation.NSURL.fileURLWithPath_(directory))
if open_dlg.runModal() == AppKit.NSFileHandlingPanelOKButton:
files = open_dlg.filenames()
self._file_name = tuple(files)
else:
self._file_name = None
if not main_thread:
self._file_name_semaphore.release()
if main_thread:
create_dialog(dialog_type, allow_multiple, save_filename)
else:
AppHelper.callAfter(create_dialog, dialog_type, allow_multiple, save_filename)
self._file_name_semaphore.acquire()
return self._file_name
def _recreate_menus(self, user_menu):
main_menu = self._clear_main_menu()
# Filter out app menu items (menus with title '__app__')
app_menu_items = None
regular_menus = None
if user_menu:
app_menu_items = []
regular_menus = []
for menu in user_menu:
if isinstance(menu, Menu) and menu.title == '__app__':
# This is an app menu - extract its items
app_menu_items.extend(menu.items)
else:
# Regular menu
regular_menus.append(menu)
# If no app menu items were found, set to None
if not app_menu_items:
app_menu_items = None
# If no regular menus, set to None
if not regular_menus:
regular_menus = None
self._add_app_menu(main_menu, app_menu_items)
if webview_settings['SHOW_DEFAULT_MENUS']:
self._add_view_menu(main_menu)
self._add_edit_menu(main_menu)
self._add_custom_menu(main_menu, regular_menus)
return main_menu
def _clear_main_menu(self):
"""
Remove all items from the main menu or create a new one if it doesn't exist.
"""
mainMenu = BrowserView.app.mainMenu()
if mainMenu:
mainMenu.removeAllItems()
mainMenu = AppKit.NSMenu.alloc().init()
return mainMenu
def _add_app_menu(self, mainMenu, custom_items=None):
"""
Create a default Cocoa menu that shows 'Services', 'Hide',
'Hide Others', 'Show All', and 'Quit'. Will append the application name
to some menu items if it's available.
"""
# Create an application menu and make it a submenu of the main menu
mainAppMenuItem = AppKit.NSMenuItem.alloc().init()
# The application menu is the first item, so add this menu ast the first item
mainMenu.insertItem_atIndex_(mainAppMenuItem, 0)
appMenu = AppKit.NSMenu.alloc().init()
mainAppMenuItem.setSubmenu_(appMenu)
appMenu.addItemWithTitle_action_keyEquivalent_(
self._append_app_name(self.localization['cocoa.menu.about']),
'orderFrontStandardAboutPanel:',
'',
)
# Add custom app menu items if provided (between About and Services)
if custom_items:
appMenu.addItem_(AppKit.NSMenuItem.separatorItem())
self._process_menu_items(custom_items, appMenu)
appMenu.addItem_(AppKit.NSMenuItem.separatorItem())
# Set the 'Services' menu for the app and create an app menu item
appServicesMenu = AppKit.NSMenu.alloc().init()
BrowserView.app.setServicesMenu_(appServicesMenu)
servicesMenuItem = appMenu.addItemWithTitle_action_keyEquivalent_(
self.localization['cocoa.menu.services'], nil, ''
)
servicesMenuItem.setSubmenu_(appServicesMenu)
appMenu.addItem_(AppKit.NSMenuItem.separatorItem())
# Append the 'Hide', 'Hide Others', and 'Show All' menu items
appMenu.addItemWithTitle_action_keyEquivalent_(
self._append_app_name(self.localization['cocoa.menu.hide']), 'hide:', 'h'
)
hideOthersMenuItem = appMenu.addItemWithTitle_action_keyEquivalent_(
self.localization['cocoa.menu.hideOthers'], 'hideOtherApplications:', 'h'
)
hideOthersMenuItem.setKeyEquivalentModifierMask_(
AppKit.NSAlternateKeyMask | AppKit.NSCommandKeyMask
)
appMenu.addItemWithTitle_action_keyEquivalent_(
self.localization['cocoa.menu.showAll'], 'unhideAllApplications:', ''
)
appMenu.addItem_(AppKit.NSMenuItem.separatorItem())
# Append a 'Quit' menu item
appMenu.addItemWithTitle_action_keyEquivalent_(
self._append_app_name(self.localization['cocoa.menu.quit']), 'terminate:', 'q'
)
def _add_view_menu(self, mainMenu):
"""
Create a default View menu that shows 'Enter Full Screen'.
"""
# Create an View menu and make it a submenu of the main menu
viewMenu = AppKit.NSMenu.alloc().init()
viewMenu.setTitle_(self.localization['cocoa.menu.view'])
viewMenuItem = AppKit.NSMenuItem.alloc().init()
viewMenuItem.setSubmenu_(viewMenu)
# Make the view menu the first item after the application menu
mainMenu.insertItem_atIndex_(viewMenuItem, 1)
# TODO: localization of the Enter fullscreen string has no effect
fullScreenMenuItem = viewMenu.addItemWithTitle_action_keyEquivalent_(
self.localization['cocoa.menu.fullscreen'], 'toggleFullScreen:', 'f'
)
fullScreenMenuItem.setKeyEquivalentModifierMask_(
AppKit.NSControlKeyMask | AppKit.NSCommandKeyMask
)
def _add_edit_menu(self, mainMenu):
"""
Create a default Edit menu that shows Copy/Paste/etc.
"""
# 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 _process_menu_items(self, menu_items, parent_menu):
"""
Process menu items and add them to the parent menu.
Used for both custom menus and app menu items.
"""
for item in menu_items:
if isinstance(item, MenuSeparator):
parent_menu.addItem_(AppKit.NSMenuItem.separatorItem())
elif isinstance(item, MenuAction):
# Actions must be registered before application start. Otherwise they are disabled.
# Menu handler is a workaround to register actions after application start
random_id = str(uuid.uuid4())[:6]
# Handle functools.partial objects which don't have __name__ attribute
if hasattr(item.function, '__name__'):
func_name = item.function.__name__
elif hasattr(item.function, 'func') and hasattr(item.function.func, '__name__'):
func_name = item.function.func.__name__
else:
func_name = 'anonymous_function'
action_id = func_name + '.' + random_id
menu_handler.register_action(action_id, item.function)
menu_item = AppKit.NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
item.title, 'handleMenuAction:', ''
)
menu_item.setTarget_(menu_handler)
menu_item.setRepresentedObject_(action_id)
parent_menu.addItem_(menu_item)
elif isinstance(item, Menu):
submenu = AppKit.NSMenu.alloc().init()
submenu.setTitle_(item.title)
menu_item = AppKit.NSMenuItem.alloc().init()
menu_item.setTitle_(item.title)
menu_item.setSubmenu_(submenu)
parent_menu.addItem_(menu_item)
self._process_menu_items(item.items, submenu)
def _add_custom_menu(self, mainMenu, app_menu_list):
"""
Create a custom menu for the app menu (MacOS bar menu)
"""
if app_menu_list is None:
return
for app_menu in app_menu_list:
submenu = AppKit.NSMenu.alloc().init()
submenu.setTitle_(app_menu.title)
menu_item = AppKit.NSMenuItem.alloc().init()
menu_item.setTitle_(app_menu.title)
menu_item.setSubmenu_(submenu)
mainMenu.addItem_(menu_item)
self._process_menu_items(app_menu.items, submenu)
def _append_app_name(self, val):
"""
Append the application name to a string if it's available. If not, the
string is returned unchanged.
:param str val: The string to append to
:return: String with app name appended, or unchanged string
:rtype: str
"""
if 'CFBundleName' in info:
val += ' {}'.format(info['CFBundleName'])
return val
@staticmethod
def nscolor_from_hex(hex_string, alpha=1.0):
"""
Convert given hex color to NSColor.
:hex_string: Hex code of the color as #RGB or #RRGGBB
"""
hex_string = hex_string[1:] # Remove leading hash
if len(hex_string) == 3:
hex_string = ''.join([c * 2 for c in hex_string]) # 3-digit to 6-digit
hex_int = int(hex_string, 16)
rgb = (
(hex_int >> 16) & 0xFF, # Red byte
(hex_int >> 8) & 0xFF, # Blue byte
(hex_int) & 0xFF, # Green byte
)
rgb = [i / 255.0 for i in rgb] # Normalize to range(0.0, 1.0)
return AppKit.NSColor.colorWithSRGBRed_green_blue_alpha_(rgb[0], rgb[1], rgb[2], alpha)
@staticmethod
def get_instance(attr, value):
"""
Return a BrowserView instance by the :value of its given :attribute,
and None if no match is found.
"""
for i in list(BrowserView.instances.values()):
try:
if getattr(i, attr) == value:
return i
except AttributeError:
break
return None
@staticmethod
def display_confirmation_dialog(first_button, second_button, message):
AppKit.NSApplication.sharedApplication()
AppKit.NSRunningApplication.currentApplication().activateWithOptions_(
AppKit.NSApplicationActivateIgnoringOtherApps
)
alert = AppKit.NSAlert.alloc().init()
alert.addButtonWithTitle_(first_button)
alert.addButtonWithTitle_(second_button)
alert.setMessageText_(message)
alert.setAlertStyle_(AppKit.NSWarningAlertStyle)
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']
cancel = window.localization['global.cancel']
msg = window.localization['global.quitConfirmation']
should_cancel = window.events.closing.set()
if should_cancel:
return Foundation.NO
if not window.confirm_close or BrowserView.display_confirmation_dialog(quit, cancel, msg):
return Foundation.YES
return Foundation.NO
@staticmethod
def print_webview(webview):
info = AppKit.NSPrintInfo.sharedPrintInfo().copy()
# default print settings used by Safari
info.setHorizontalPagination_(AppKit.NSFitPagination)
info.setHorizontallyCentered_(Foundation.NO)
info.setVerticallyCentered_(Foundation.NO)
imageableBounds = info.imageablePageBounds()
paperSize = info.paperSize()
if Foundation.NSWidth(imageableBounds) > paperSize.width:
imageableBounds.origin.x = 0
imageableBounds.size.width = paperSize.width
if Foundation.NSHeight(imageableBounds) > paperSize.height:
imageableBounds.origin.y = 0
imageableBounds.size.height = paperSize.height
info.setBottomMargin_(Foundation.NSMinY(imageableBounds))
info.setTopMargin_(
paperSize.height
- Foundation.NSMinY(imageableBounds)
- Foundation.NSHeight(imageableBounds)
)
info.setLeftMargin_(Foundation.NSMinX(imageableBounds))
info.setRightMargin_(
paperSize.width
- Foundation.NSMinX(imageableBounds)
- Foundation.NSWidth(imageableBounds)
)
# show the print panel
print_op = webview._printOperationWithPrintInfo_(info)
print_op.runOperationModalForWindow_delegate_didRunSelector_contextInfo_(
webview.window(), nil, nil, nil
)
@staticmethod
def _open_web_inspector(webview):
"""
Programmatically open the Web Inspector for the given WKWebView.
Uses private WebKit APIs that may not work on all macOS versions.
"""
try:
if hasattr(webview, '_inspector'):
inspector = webview._inspector()
if inspector and hasattr(inspector, 'show'):
inspector.show()
return True
return False
except Exception:
return False
@staticmethod
def quote(string):
return string.replace(' ', '%20')
class MenuHandler:
def __init__(self):
self.actions = {}
def handleMenuAction_(self, sender):
action_id = sender.representedObject()
if action_id in self.actions:
Thread(target=self.actions[action_id]).start()
else:
logger.warning(f'Menu cction {action_id} not found')
def register_action(self, action_id, action_callable):
self.actions[action_id] = action_callable
menu_handler = MenuHandler()
def setup_app():
pass
def get_active_window():
active_window = BrowserView.app.keyWindow()
if active_window is None:
return None
active_window_number = active_window.windowNumber()
for uid, browser_view_instance in BrowserView.instances.items():
if browser_view_instance.window.windowNumber() == active_window_number:
return browser_view_instance.pywebview_window
return None
def create_window(window):
def create():
browser = BrowserView(window)
browser.first_show()
if window.uid == 'master':
main_thread().pydev_do_not_trace = True # vs code debugger hang fix
create()
else:
AppHelper.callAfter(create)
def set_title(title, uid):
i = BrowserView.instances.get(uid)
if i:
i.set_title(title)
def create_confirmation_dialog(title, message, uid):
def _confirm():
nonlocal result
i = BrowserView.instances.get(uid)
ok = i.localization['global.ok']
cancel = i.localization['global.cancel']
result = BrowserView.display_confirmation_dialog(ok, cancel, message)
semaphore.release()
result = False
semaphore = Semaphore(0)
AppHelper.callAfter(_confirm)
semaphore.acquire()
return result
def create_file_dialog(dialog_type, directory, allow_multiple, save_filename, file_types, uid):
file_filter = []
# Parse file_types to obtain allowed file extensions
for s in file_types:
description, extensions = parse_file_type(s)
file_extensions = [i.lstrip('*.') for i in extensions.split(';') if i != '*.*']
file_filter.append([description, file_extensions or []])
i = BrowserView.instances.get(uid)
return i.create_file_dialog(dialog_type, directory, allow_multiple, save_filename, file_filter)
def load_url(url, uid):
i = BrowserView.instances.get(uid)
if i:
i.load_url(url)
def load_html(content, base_uri, uid):
i = BrowserView.instances.get(uid)
if i:
i.load_html(content, base_uri)
def destroy_window(uid):
i = BrowserView.instances.get(uid)
if i:
i.destroy()
def hide(uid):
i = BrowserView.instances.get(uid)
if i:
i.hide()
def show(uid):
i = BrowserView.instances.get(uid)
if i:
i.show()
def toggle_fullscreen(uid):
i = BrowserView.instances.get(uid)
if i:
i.toggle_fullscreen()
def set_on_top(uid, top):
def _set_on_top():
level = AppKit.NSStatusWindowLevel if top else AppKit.NSNormalWindowLevel
i.window.setLevel_(level)
i = BrowserView.instances.get(uid)
if i:
AppHelper.callAfter(_set_on_top)
def resize(width, height, uid, fix_point):
i = BrowserView.instances.get(uid)
if i:
i.resize(width, height, fix_point)
def maximize(uid):
i = BrowserView.instances.get(uid)
if i:
i.maximize()
def minimize(uid):
i = BrowserView.instances.get(uid)
if i:
i.minimize()
def restore(uid):
i = BrowserView.instances.get(uid)
if i:
i.restore()
def move(x, y, uid):
i = BrowserView.instances.get(uid)
if i:
AppHelper.callAfter(i.move, x, y)
def get_current_url(uid):
i = BrowserView.instances.get(uid)
if i:
return i.get_current_url()
def clear_cookies(uid):
i = BrowserView.instances.get(uid)
if i:
i.clear_cookies()
def get_cookies(uid):
i = BrowserView.instances.get(uid)
if i:
return i.get_cookies()
def evaluate_js(script, uid, parse_json=True):
i = BrowserView.instances.get(uid)
if i:
return i.evaluate_js(script, parse_json)
def get_position(uid):
def _position(coordinates):
screen_frame = i.screen
if screen_frame is None:
raise RuntimeError('Failed to obtain screen')
window = i.window
frame = window.frame()
coordinates[0] = int(frame.origin.x)
coordinates[1] = int(screen_frame.size.height - frame.origin.y - frame.size.height)
semaphore.release()
coordinates = [None, None]
semaphore = Semaphore(0)
i = BrowserView.instances.get(uid)
if not i:
return None, None
try:
_position(coordinates)
except:
AppHelper.callAfter(_position, coordinates)
semaphore.acquire()
return coordinates
def get_size(uid):
def _size(dimensions):
size = i.window.frame().size
dimensions[0] = size.width
dimensions[1] = size.height
semaphore.release()
dimensions = [None, None]
semaphore = Semaphore(0)
i = BrowserView.instances.get(uid)
if not i:
return None, None
try:
_size(dimensions)
except:
AppHelper.callAfter(_size, dimensions)
semaphore.acquire()
return dimensions
def get_screens():
screens = [
Screen(
s.frame().origin.x,
s.frame().origin.y,
s.frame().size.width,
s.frame().size.height,
s.frame(),
)
for s in AppKit.NSScreen.screens()
]
return screens
def add_tls_cert(certfile):
# does not auth against the certfile
# see webView_didReceiveAuthenticationChallenge_completionHandler_
pass
|