96 lines
3.2 KiB
Python
96 lines
3.2 KiB
Python
# Copyright 2022 (c) Anna Schumaker.
|
|
"""Widgets for sidebar rows."""
|
|
from gi.repository import GObject
|
|
from gi.repository import Gtk
|
|
from .title import PlaylistTitle
|
|
from .title import EditableTitle
|
|
from .icon import Icon
|
|
from .icon import Settable
|
|
from .. import db
|
|
|
|
|
|
class BaseRow(Gtk.Box):
|
|
"""Base class for Row widgets."""
|
|
|
|
playlist = GObject.Property(type=db.playlist.Playlist)
|
|
name = GObject.Property(type=str)
|
|
count = GObject.Property(type=int)
|
|
|
|
|
|
class Row(BaseRow):
|
|
"""A basic row representing a playlist without extra actions."""
|
|
|
|
image = GObject.Property(type=GObject.TYPE_PYOBJECT)
|
|
|
|
def __init__(self, **kwargs):
|
|
"""Initialize a sidebar Row."""
|
|
super().__init__(spacing=12, **kwargs)
|
|
self._icon = Icon(text=self.name, show_initials=True)
|
|
self._title = PlaylistTitle(title=self.name, count=self.count)
|
|
self._year = None
|
|
|
|
self.bind_property("image", self._icon, "filepath")
|
|
self.bind_property("name", self._icon, "text")
|
|
self.bind_property("name", self._title, "title")
|
|
self.bind_property("count", self._title, "count")
|
|
|
|
self.append(self._icon)
|
|
self.append(self._title)
|
|
|
|
@GObject.Property(type=int)
|
|
def year(self) -> int | None:
|
|
"""Set the year displayed in the icon."""
|
|
return self._year
|
|
|
|
@year.setter
|
|
def year(self, year: int) -> None:
|
|
digits = str(year)
|
|
self._year = year
|
|
self._icon.text = f"{digits[-2]} {digits[-1]}"
|
|
|
|
|
|
class PlaylistRow(BaseRow):
|
|
"""An advanced playlist row with extra actions for users."""
|
|
|
|
icon_name = GObject.Property(type=str)
|
|
filepath = GObject.Property(type=GObject.TYPE_PYOBJECT)
|
|
modifiable = GObject.Property(type=bool, default=False)
|
|
|
|
def __init__(self, **kwargs):
|
|
"""Initialize a PlaylistRow."""
|
|
super().__init__(**kwargs)
|
|
self._icon = Settable()
|
|
self._title = EditableTitle(margin_start=12, margin_end=12)
|
|
self._delete = Gtk.Button(icon_name="big-x-symbolic",
|
|
valign=Gtk.Align.CENTER,
|
|
has_frame=False, visible=False)
|
|
|
|
self.bind_property("name", self._icon, "text")
|
|
self.bind_property("icon-name", self._icon, "icon-name")
|
|
self.bind_property("filepath", self._icon, "filepath",
|
|
GObject.BindingFlags.BIDIRECTIONAL)
|
|
self.bind_property("modifiable", self._icon, "settable")
|
|
|
|
self.bind_property("name", self._title, "title")
|
|
self.bind_property("count", self._title, "count")
|
|
self.bind_property("modifiable", self._title, "editable")
|
|
|
|
self.bind_property("modifiable", self._delete, "visible")
|
|
|
|
self._title.connect("request-rename", self.__on_rename)
|
|
self._delete.connect("clicked", self.__on_delete)
|
|
|
|
self._delete.add_css_class("emmental-delete")
|
|
|
|
self.append(self._icon)
|
|
self.append(self._title)
|
|
self.append(self._delete)
|
|
|
|
def __on_rename(self, title: EditableTitle, new_name: str) -> None:
|
|
if self.playlist is not None:
|
|
self.playlist.rename(new_name)
|
|
|
|
def __on_delete(self, button: Gtk.Button) -> None:
|
|
if self.playlist is not None:
|
|
self.playlist.delete()
|