Activate toggle on hotkey invocation

This commit is contained in:
Henrik Giesel 2021-01-10 01:10:23 +01:00
parent 59d0e8f036
commit 6afc495035

View file

@ -11,6 +11,7 @@ import urllib.error
import urllib.parse import urllib.parse
import urllib.request import urllib.request
import warnings import warnings
from random import randrange
from typing import Callable, List, Optional, Tuple from typing import Callable, List, Optional, Tuple
import bs4 import bs4
@ -252,10 +253,23 @@ class Editor:
if func: if func:
self._links[cmd] = func self._links[cmd] = func
if keys: if keys:
def on_activated():
func(self)
if toggleable:
# generate a random id for triggering toggle
id = id or str(randrange(1_000_000))
def on_hotkey():
on_activated()
self.web.eval(f'toggleEditorButton("#{id}");')
else:
on_hotkey = on_activated
QShortcut( # type: ignore QShortcut( # type: ignore
QKeySequence(keys), QKeySequence(keys),
self.widget, self.widget,
activated=lambda s=self: func(s), activated=on_hotkey,
) )
btn = self._addButton( btn = self._addButton(
icon, icon,