emmental/emmental/sidebar/title.py

49 lines
1.8 KiB
Python

# Copyright 2022 (c) Anna Schumaker.
"""Helper widgets for sidebar rows titles."""
from gi.repository import GObject
from gi.repository import Pango
from gi.repository import Gtk
class Title(Gtk.Box):
"""Row Title and Subtitle."""
title = GObject.Property(type=str)
subtitle = GObject.Property(type=str)
def __init__(self, title: str = "", subtitle: str = "", **kwargs):
"""Initialize a row Title widget."""
super().__init__(orientation=Gtk.Orientation.VERTICAL,
title=title, subtitle=subtitle, tooltip_text=title,
hexpand=True, valign=Gtk.Align.CENTER,
margin_top=6, margin_bottom=6, **kwargs)
self._title = Gtk.Label(label=self.title, xalign=0,
ellipsize=Pango.EllipsizeMode.END)
self._subtitle = Gtk.Label(label=self.subtitle, xalign=0,
ellipsize=Pango.EllipsizeMode.END)
self._title.add_css_class("header")
self._subtitle.add_css_class("caption")
self.bind_property("title", self, "tooltip-text")
self.bind_property("title", self._title, "label")
self.bind_property("subtitle", self._subtitle, "label")
self.append(self._title)
self.append(self._subtitle)
class PlaylistTitle(Title):
"""A title widget for displaying playlist size."""
count = GObject.Property(type=int)
def __init__(self, title: str = "", **kwargs):
"""Initialize a Playlist Title widget."""
super().__init__(title=title, subtitle="-- tracks", **kwargs)
self.connect("notify::count", self.__update_subtitle)
def __update_subtitle(self, title: Title, param) -> None:
s_count = "s" if self.count != 1 else ""
self.subtitle = f"{self.count} track{s_count}"