Some cleanup. Also use the actual GLib function to escape markup.

It does escape control characters, which we didn't used to do, but I don't think that'll be a problem.
This commit is contained in:
Daniel Johnson 2023-11-04 07:08:02 -04:00
parent 43c23d7cdc
commit e84f78dcdf
2 changed files with 31 additions and 28 deletions

View file

@ -1,4 +1,6 @@
from gi.repository import GLib, Gtk, Pango
from gi.repository import Gtk, Pango
from lutris.util.strings import is_valid_pango_markup
class InstallerLabel(Gtk.Label):
@ -7,8 +9,6 @@ class InstallerLabel(Gtk.Label):
def __init__(self, text, wrap=True, selectable=False):
super().__init__()
is_valid_markup = InstallerLabel.is_valid_pango_markup(text)
if wrap:
self.set_line_wrap(True)
self.set_line_wrap_mode(Pango.WrapMode.WORD_CHAR)
@ -17,7 +17,7 @@ class InstallerLabel(Gtk.Label):
self.set_alignment(0, 0.5)
self.set_margin_right(12)
if is_valid_markup:
if is_valid_pango_markup(text):
self.set_markup(text)
else:
self.set_text(text)
@ -25,21 +25,3 @@ class InstallerLabel(Gtk.Label):
self.props.can_focus = False
self.set_tooltip_text(text)
self.set_selectable(selectable)
@staticmethod
def is_valid_pango_markup(text):
def destroy_func(_user_data):
pass # required by GLib, but we don't need this callback
if len(text) == 0:
return True # Trivial case - empty strings are always valid
try:
parser = GLib.MarkupParser()
context = GLib.MarkupParseContext(parser, GLib.MarkupParseFlags.DEFAULT_FLAGS, None, destroy_func)
markup = f"<markup>{text}</markup>"
context.parse(markup, len(markup))
return True
except GLib.GError:
return False

View file

@ -7,6 +7,8 @@ import uuid
from gettext import gettext as _
from typing import List, Union
from gi.repository import GLib
from lutris.util.log import logger
NO_PLAYTIME = "Never played"
@ -101,12 +103,31 @@ def unpack_dependencies(string: str) -> List[Union[str, tuple]]:
return [dep for dep in [_expand_dep(dep) for dep in string.split(",")] if dep]
def gtk_safe(string: str) -> str:
"""Return a string ready to used in Gtk widgets"""
if not string:
string = ""
string = str(string)
return string.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
def gtk_safe(text: str) -> str:
"""Return a string ready to used in Gtk widgets, with anything that could
be Pango markup escaped."""
if not text:
return ""
return GLib.markup_escape_text(str(text))
def is_valid_pango_markup(text):
def destroy_func(_user_data):
pass # required by GLib, but we don't need this callback
if len(text) == 0:
return True # Trivial case - empty strings are always valid
try:
parser = GLib.MarkupParser()
context = GLib.MarkupParseContext(parser, GLib.MarkupParseFlags.DEFAULT_FLAGS, None, destroy_func)
markup = f"<markup>{text}</markup>"
context.parse(markup, len(markup))
return True
except GLib.GError:
return False
def get_formatted_playtime(playtime) -> str: