More efficient undo link data storage

This commit is contained in:
Kovid Goyal 2023-04-12 18:07:21 +05:30
parent 019e9435fb
commit ebea37e7e3
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -267,6 +267,7 @@ class MessagePopup(QLabel):
def __init__(self, parent): def __init__(self, parent):
QLabel.__init__(self, parent) QLabel.__init__(self, parent)
self.setFocusPolicy(Qt.FocusPolicy.NoFocus) self.setFocusPolicy(Qt.FocusPolicy.NoFocus)
self.undo_data = None
if QApplication.instance().is_dark_theme: if QApplication.instance().is_dark_theme:
c = builtin_colors_dark['green'] c = builtin_colors_dark['green']
else: else:
@ -294,16 +295,13 @@ class MessagePopup(QLabel):
def link_activated(self, link): def link_activated(self, link):
self.hide() self.hide()
if link.startswith('undo://'): if link.startswith('undo://'):
import base64, json self.undo_requested.emit(self.undo_data)
data = base64.urlsafe_b64decode(link.rpartition('/')[-1])
self.undo_requested.emit(json.loads(data))
def __call__(self, text='Testing message popup', show_undo=True, timeout=5000, has_markup=False): def __call__(self, text='Testing message popup', show_undo=True, timeout=5000, has_markup=False):
text = '<p>' + (text if has_markup else prepare_string_for_xml(text)) text = '<p>' + (text if has_markup else prepare_string_for_xml(text))
if show_undo: if show_undo:
import base64, json self.undo_data = show_undo
data = base64.urlsafe_b64encode(json.dumps(show_undo).encode('utf-8')).decode('ascii') text += '\xa0\xa0<a style="text-decoration: none" href="undo://me.com">{}</a>'.format(_('Undo'))
text += '\xa0\xa0<a style="text-decoration: none" href="undo://me.com/{}">{}</a>'.format(data, _('Undo'))
text += f'\xa0\xa0<a style="text-decoration: none; color: {self.color}" href="close://me.com">✖</a>' text += f'\xa0\xa0<a style="text-decoration: none; color: {self.color}" href="close://me.com">✖</a>'
self.setText(text) self.setText(text)
self.resize(self.sizeHint()) self.resize(self.sizeHint())