emmental/emmental/nowplaying/label.py

37 lines
1.3 KiB
Python

# Copyright 2022 (c) Anna Schumaker.
"""A Gtk.Label set up for displaying nowplaying tags."""
from gi.repository import GObject
from gi.repository import Gtk
from gi.repository import Pango
class Label(Gtk.Label):
"""A Gtk.Label wrapped in a ScrolledWindow for displaying tags."""
prefix = GObject.Property(type=str)
size = GObject.Property(type=int)
def __init__(self, prefix="", **kwargs):
"""Initialize our Label."""
super().__init__(prefix=prefix, xalign=0, **kwargs)
self.connect("notify::size", self.__set_size)
@GObject.Property(type=str)
def text(self) -> str:
"""Get the text set to the label (without any prefix)."""
return self.get_text()[len(self.prefix):]
@text.setter
def text(self, text: str) -> None:
"""Set text to the label (adding any prefix)."""
text = f"{self.prefix}{text}" if text else ""
self.set_text(text)
def __set_size(self, label: Gtk.Label, param) -> None:
# Note that we have to create a new Pango.AttrList here because
# Gtk.Label.get_attributes() returns a COPY of the AttrList, so
# any modifications won't take effect.
attrlist = Pango.AttrList.new()
attrlist.insert(Pango.attr_size_new_absolute(self.size * Pango.SCALE))
self.set_attributes(attrlist)