41 lines
1.5 KiB
Python
41 lines
1.5 KiB
Python
# Copyright 2023 (c) Anna Schumaker.
|
|
"""A custom Button that opens a FileDialog 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
|
|
|
|
|
|
class Button(Gtk.Button):
|
|
"""Our pre-configured open button."""
|
|
|
|
def __init__(self):
|
|
"""Initialize our open button."""
|
|
super().__init__(icon_name="document-open-symbolic",
|
|
tooltip_text="open a file for playback")
|
|
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)
|
|
|
|
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 do_clicked(self) -> None:
|
|
"""Handle a click event."""
|
|
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."""
|