47 lines
1.7 KiB
Python
47 lines
1.7 KiB
Python
# Copyright 2023 (c) Anna Schumaker.
|
|
"""A custom Adw.ActionRow to select a file for playback."""
|
|
import pathlib
|
|
from gi.repository import GObject
|
|
from gi.repository import GLib
|
|
from gi.repository import Gio
|
|
from gi.repository import Gtk
|
|
from gi.repository import Adw
|
|
|
|
|
|
class OpenRow(Adw.ActionRow):
|
|
"""Our pre-configured open Adw.ActionRow."""
|
|
|
|
def __init__(self):
|
|
"""Initialize our open ActionRow."""
|
|
super().__init__(activatable=True, title="Open File",
|
|
subtitle="Select a file for playback")
|
|
self._prefix = Gtk.Image(icon_name="document-open-symbolic")
|
|
self._filters = Gio.ListStore()
|
|
self._filter = Gtk.FileFilter(name="Audio Files",
|
|
mime_types=["inode/directory",
|
|
"audio/*"])
|
|
self._dialog = Gtk.FileDialog(filters=self._filters,
|
|
title="Pick a Track")
|
|
|
|
self._filters.append(self._filter)
|
|
|
|
self.connect("activated", self.__on_activated)
|
|
self.add_prefix(self._prefix)
|
|
|
|
def __async_ready(self, dialog: Gtk.FileDialog, task: Gio.Task) -> None:
|
|
try:
|
|
file = dialog.open_finish(task)
|
|
self.emit("track-requested", pathlib.Path(file.get_path()))
|
|
except GLib.Error:
|
|
pass
|
|
|
|
def __on_activated(self, row: Adw.ActionRow) -> None:
|
|
"""Handle activating an OpenRow."""
|
|
self.get_ancestor(Gtk.Popover).popdown()
|
|
self._dialog.open(self.get_ancestor(Gtk.Window), None,
|
|
self.__async_ready)
|
|
|
|
@GObject.Signal(arg_types=(GObject.TYPE_PYOBJECT,))
|
|
def track_requested(self, file: pathlib.Path) -> None:
|
|
"""Signal that a track has been requested."""
|