Skip to content

Commit 4bd4adc

Browse files
committed
optimize open links and remove deprecations
1 parent 018f2a3 commit 4bd4adc

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

devdocs_desktop.py

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@
2121
BUS_NAME = 'org.hardpixel.DevdocsDesktop'
2222
BUS_PATH = '/org/hardpixel/DevdocsDesktop'
2323

24+
CTX_MENU = [
25+
WebKit2.ContextMenuAction.GO_BACK,
26+
WebKit2.ContextMenuAction.GO_FORWARD,
27+
WebKit2.ContextMenuAction.STOP,
28+
WebKit2.ContextMenuAction.RELOAD,
29+
WebKit2.ContextMenuAction.COPY,
30+
WebKit2.ContextMenuAction.CUT,
31+
WebKit2.ContextMenuAction.PASTE,
32+
WebKit2.ContextMenuAction.DELETE,
33+
WebKit2.ContextMenuAction.SELECT_ALL,
34+
WebKit2.ContextMenuAction.OPEN_LINK,
35+
WebKit2.ContextMenuAction.COPY_LINK_TO_CLIPBOARD,
36+
WebKit2.ContextMenuAction.COPY_IMAGE_TO_CLIPBOARD,
37+
WebKit2.ContextMenuAction.COPY_IMAGE_URL_TO_CLIPBOARD,
38+
WebKit2.ContextMenuAction.COPY_VIDEO_LINK_TO_CLIPBOARD,
39+
WebKit2.ContextMenuAction.COPY_AUDIO_LINK_TO_CLIPBOARD
40+
]
41+
2442

2543
class DevdocsDesktop:
2644

@@ -31,13 +49,12 @@ def __init__(self):
3149
self.args = argparse.ArgumentParser(prog='devdocs-desktop')
3250
self.args.add_argument('s', metavar='STR', help='the string to search', nargs='?', default='')
3351

34-
self.app_url = 'https://devdocs.io'
35-
self.search = self.args.parse_args().s.strip()
36-
self.filter = ''
37-
self.open_link = False
38-
self.options = self.read_settings_json('cookies')
39-
self.prefs = self.read_settings_json('prefs')
40-
self.globals = Gtk.Settings.get_default()
52+
self.app_url = 'https://devdocs.io'
53+
self.search = self.args.parse_args().s.strip()
54+
self.filter = ''
55+
self.options = self.read_settings_json('cookies')
56+
self.prefs = self.read_settings_json('prefs')
57+
self.globals = Gtk.Settings.get_default()
4158

4259
self.main = Gtk.Builder()
4360
self.main.add_from_file(self.file_path('ui/main.ui'))
@@ -403,13 +420,11 @@ def on_finder_failed_to_find_text(self, _controller):
403420
self.finder_prev.set_sensitive(False)
404421

405422
def on_webview_decide_policy(self, _widget, decision, dtype):
406-
types = WebKit2.PolicyDecisionType
407-
408-
if self.open_link and dtype == types.NAVIGATION_ACTION:
409-
self.open_link = False
410-
uri = decision.get_request().get_uri()
423+
if dtype == WebKit2.PolicyDecisionType.NAVIGATION_ACTION:
424+
nav = decision.get_navigation_action()
425+
uri = nav.get_request().get_uri()
411426

412-
if not self.app_url in uri:
427+
if not uri.startswith(self.app_url):
413428
decision.ignore()
414429
webbrowser.open(uri)
415430

@@ -430,28 +445,13 @@ def on_history_changed(self, _list, _added, _removed):
430445
forward = self.webview.can_go_forward()
431446
self.header_forward.set_sensitive(forward)
432447

433-
def on_webview_open_link(self, action):
434-
self.open_link = True
435-
436448
def on_webview_context_menu(self, _widget, menu, _coords, _keyboard):
437-
actions = WebKit2.ContextMenuAction
438-
include = [
439-
actions.GO_BACK, actions.GO_FORWARD, actions.STOP, actions.RELOAD,
440-
actions.COPY, actions.CUT, actions.PASTE, actions.DELETE, actions.SELECT_ALL,
441-
actions.OPEN_LINK, actions.COPY_LINK_TO_CLIPBOARD,
442-
actions.COPY_IMAGE_TO_CLIPBOARD, actions.COPY_IMAGE_URL_TO_CLIPBOARD,
443-
actions.COPY_VIDEO_LINK_TO_CLIPBOARD, actions.COPY_AUDIO_LINK_TO_CLIPBOARD
444-
]
445-
446449
for item in menu.get_items():
447450
action = item.get_stock_action()
448451

449-
if not action in include:
452+
if not item.is_separator() and not action in CTX_MENU:
450453
menu.remove(item)
451454

452-
if action == actions.OPEN_LINK:
453-
item.get_action().connect('activate', self.on_webview_open_link)
454-
455455

456456
class DevdocsDesktopService(dbus.service.Object):
457457

0 commit comments

Comments
 (0)