emmental/sidebar/library.py

85 lines
2.3 KiB
Python

# Copyright 2021 (c) Anna Schumaker.
from . import tagbox
from . import tagrow
from gi.repository import Gtk
import pathlib
import tagdb
class LibraryTagRow(tagrow.TagRow):
def __init__(self, tag, icon):
super().__init__(tag, icon)
self.update = Gtk.Button.new_from_icon_name("view-refresh")
self.update.set_hexpand(True)
self.update.connect("clicked", self.update_clicked)
self.remove = Gtk.Button.new_from_icon_name("list-remove")
self.remove.set_hexpand(True)
self.remove.connect("clicked", self.remove_clicked)
self.box = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 0)
self.box.add_css_class("linked")
self.box.append(self.update)
self.box.append(self.remove)
self.grid.attach(self.box, 0, 1, 2, 1)
def update_clicked(self, button):
self.tag.scan()
def remove_clicked(self, button):
tagdb.Library.remove(self.tag)
class LibraryTagBox(tagbox.TagBox):
def __alloc_tagrow__(self, tag):
return LibraryTagRow(tag, self.icon)
TagBox = LibraryTagBox(tagdb.Library, "folder-music")
#
# Configure the directory chooser popover and menu button
#
DirFilter = Gtk.FileFilter()
DirFilter.add_mime_type("inode/directory")
DirChooser = Gtk.FileChooserWidget()
DirChooser.set_action(Gtk.FileChooserAction.SELECT_FOLDER)
DirChooser.set_create_folders(False)
DirChooser.set_filter(DirFilter)
def selected_path():
if (file := DirChooser.get_file()) == None:
file = DirChooser.get_current_folder()
return pathlib.Path(file.get_path())
def on_select(button):
if (path := selected_path()) != None:
tagdb.Library.add(path).scan()
Popover.popdown()
DirSelect = Gtk.Button.new_with_label("Select Folder")
DirSelect.add_css_class("suggested-action")
DirSelect.connect("clicked", on_select)
DirBox = Gtk.Box.new(Gtk.Orientation.VERTICAL, 0)
DirBox.append(DirChooser)
DirBox.append(DirSelect)
Popover = Gtk.Popover()
Popover.add_css_class("normal-icons")
Popover.set_child(DirBox)
Add = Gtk.MenuButton()
Add.add_css_class("large-icons")
Add.set_icon_name("folder-new")
Add.set_direction(Gtk.ArrowType.UP)
Add.set_popover(Popover)
Box = Gtk.Box.new(Gtk.Orientation.VERTICAL, 0)
Box.add_css_class("linked")
Box.set_hexpand(True)
Box.append(TagBox)
Box.append(Add)
Box.icon = TagBox.icon