sidebar: Make the current playlist bold
Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
44ba5438e2
commit
981e98818d
|
@ -1,8 +1,13 @@
|
|||
# Copyright 2021 (c) Anna Schumaker.
|
||||
import audio
|
||||
from gi.repository import Gtk
|
||||
from gi.repository import Pango
|
||||
from . import widgets
|
||||
|
||||
BoldAttr = Pango.AttrList.new()
|
||||
BoldAttr.insert(Pango.attr_weight_new(Pango.Weight.BOLD))
|
||||
|
||||
|
||||
class Label(Gtk.Label):
|
||||
def __init__(self):
|
||||
Gtk.Label.__init__(self)
|
||||
|
@ -10,6 +15,9 @@ class Label(Gtk.Label):
|
|||
self.set_halign(Gtk.Align.START)
|
||||
self.set_hexpand(True)
|
||||
|
||||
def set_bold(self, newval):
|
||||
self.set_attributes(BoldAttr if newval else None)
|
||||
|
||||
|
||||
class Grid(Gtk.Grid):
|
||||
def __init__(self):
|
||||
|
@ -18,11 +26,16 @@ class Grid(Gtk.Grid):
|
|||
self.name = self.attach(Label(), 1, 0, 1, 1)
|
||||
self.count = self.attach(Label(), 1, 1, 1, 1)
|
||||
self.set_column_spacing(5)
|
||||
self.cbid = None
|
||||
|
||||
def attach(self, widget, x, y, w, h):
|
||||
super().attach(widget, x, y, w, h)
|
||||
return widget
|
||||
|
||||
def plist_changed(self, player, old, new, this):
|
||||
self.name.set_bold(new == this)
|
||||
self.count.set_bold(new == this)
|
||||
|
||||
def set_count(self, item, *args):
|
||||
n = item.get_n_tracks()
|
||||
self.count.set_text(f"{n} Track{'s' if n != 1 else ''}")
|
||||
|
@ -30,14 +43,18 @@ class Grid(Gtk.Grid):
|
|||
def set_item(self, item):
|
||||
self.icon.set_from_icon_name(item.get_property("icon-name"))
|
||||
self.name.set_text(item.get_property("name"))
|
||||
self.plist_changed(None, None, audio.Player.playlist, item)
|
||||
self.set_count(item)
|
||||
|
||||
item.connect("track-added", self.set_count)
|
||||
item.connect("track-removed", self.set_count)
|
||||
item.connect("refreshed", self.set_count)
|
||||
self.set_count(item)
|
||||
self.cbid = audio.Player.connect("playlist-changed",
|
||||
self.plist_changed, item)
|
||||
|
||||
def unset_item(self, item):
|
||||
if item:
|
||||
self.cbid = audio.Player.disconnect(self.cbid)
|
||||
item.disconnect_by_func(self.set_count)
|
||||
item.disconnect_by_func(self.set_count)
|
||||
item.disconnect_by_func(self.set_count)
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
# Copyright 2021 (c) Anna Schumaker.
|
||||
import audio
|
||||
import db
|
||||
import unittest
|
||||
from gi.repository import Gtk
|
||||
|
@ -14,6 +15,14 @@ class TestLabel(unittest.TestCase):
|
|||
self.assertEqual(label.get_halign(), Gtk.Align.START)
|
||||
self.assertTrue(label.get_hexpand())
|
||||
|
||||
def test_bold(self):
|
||||
label = row.Label()
|
||||
self.assertIsNone(label.get_attributes())
|
||||
label.set_bold(True)
|
||||
self.assertIsNotNone(label.get_attributes())
|
||||
label.set_bold(False)
|
||||
self.assertIsNone(label.get_attributes())
|
||||
|
||||
|
||||
class TestGrid(unittest.TestCase):
|
||||
def setUp(self): db.reset()
|
||||
|
@ -40,6 +49,12 @@ class TestGrid(unittest.TestCase):
|
|||
self.assertEqual(grid.icon.get_icon_name(), artist.icon_name)
|
||||
self.assertEqual(grid.name.get_text(), "Test Artist")
|
||||
self.assertEqual(grid.count.get_text(), "0 Tracks")
|
||||
self.assertIsNone(grid.name.get_attributes())
|
||||
self.assertIsNone(grid.count.get_attributes())
|
||||
|
||||
audio.Player.set_playlist(artist)
|
||||
self.assertIsNotNone(grid.name.get_attributes())
|
||||
self.assertIsNotNone(grid.count.get_attributes())
|
||||
|
||||
grid.unset_item(artist)
|
||||
track = db.make_fake_track(1, 1, "Test Track", "/a/b/c/1.ogg")
|
||||
|
|
Loading…
Reference in New Issue