emmental/emmental/tracklist/selection.py

65 lines
2.5 KiB
Python

# Copyright 2023 (c) Anna Schumaker.
"""An OSD to show when tracks are selected."""
from gi.repository import GObject
from gi.repository import Gtk
from gi.repository import Adw
from .. import playlist
class OSD(Gtk.Overlay):
"""An Overlay with extra controls for the Tracklist."""
playlist = GObject.Property(type=playlist.playlist.Playlist)
selection = GObject.Property(type=Gtk.SelectionModel)
have_selected = GObject.Property(type=bool, default=False)
n_selected = GObject.Property(type=int)
def __init__(self, selection: Gtk.SelectionModel, **kwargs):
"""Initialize an OSD."""
super().__init__(selection=selection, **kwargs)
self._remove = Gtk.Button(child=Adw.ButtonContent(label="Remove",
icon_name="list-remove-symbolic"),
halign=Gtk.Align.END, valign=Gtk.Align.END,
margin_end=16, margin_bottom=16,
visible=False)
self._remove.add_css_class("destructive-action")
self._remove.add_css_class("pill")
self.selection.connect("selection-changed", self.__selection_changed)
self._remove.connect("clicked", self.__remove_clicked)
self.add_overlay(self._remove)
def __get_selected_tracks(self) -> list:
selection = self.selection.get_selection()
return [self.selection.get_item(selection.get_nth(n))
for n in range(selection.get_size())]
def __remove_clicked(self, button: Gtk.Button) -> None:
if self.playlist is not None:
for track in self.__get_selected_tracks():
self.playlist.remove_track(track)
self.clear_selection()
def __selection_changed(self, selection: Gtk.SelectionModel,
position: int, n_items: int) -> None:
self.n_selected = selection.get_selection().get_size()
self.have_selected = self.n_selected > 0
self.__update_visibility()
def __update_visibility(self) -> None:
db_plist = None if self.playlist is None else self.playlist.playlist
user = False if db_plist is None else db_plist.user_tracks
self._remove.set_visible(user and self.have_selected)
def clear_selection(self, *args) -> None:
"""Clear the current selection."""
self.selection.unselect_all()
def reset(self) -> None:
"""Reset the OSD."""
self.selection.unselect_all()
self.__selection_changed(self.selection, 0, 0)