mirror of
https://github.com/aclist/dztui.git
synced 2026-10-25 12:12:31 +01:00
69 lines
2.1 KiB
Python
69 lines
2.1 KiB
Python
from pathlib import Path
|
|
|
|
from dzgui.util.strings import picker
|
|
|
|
import gi # noqa E402
|
|
|
|
gi.require_version("Gtk", "3.0")
|
|
from gi.repository import Gtk # noqa E402
|
|
|
|
|
|
class FilePicker(Gtk.FileChooserDialog):
|
|
def __init__(self, parent: Gtk.Window) -> None:
|
|
super().__init__(
|
|
title=picker.title,
|
|
action=Gtk.FileChooserAction.SAVE,
|
|
transient_for=parent,
|
|
resizable=True,
|
|
)
|
|
self.add_buttons("_Cancel", Gtk.ResponseType.CANCEL)
|
|
self.add_buttons("_Save", Gtk.ResponseType.OK)
|
|
self.set_default_response(Gtk.ResponseType.OK)
|
|
|
|
self.set_current_name(picker.placeholder)
|
|
|
|
def pick_file(self) -> Path | None:
|
|
res = self.run()
|
|
if res in (Gtk.ResponseType.CANCEL, Gtk.ResponseType.DELETE_EVENT):
|
|
self.destroy()
|
|
return None
|
|
if res == Gtk.ResponseType.OK:
|
|
file = self.get_filename()
|
|
if file is not None:
|
|
self.destroy()
|
|
return Path(file)
|
|
return None
|
|
|
|
|
|
class FolderPicker(Gtk.FileChooserDialog):
|
|
def __init__(self, parent: Gtk.Window, title: str) -> None:
|
|
super().__init__(
|
|
title=title,
|
|
action=Gtk.FileChooserAction.SELECT_FOLDER,
|
|
parent=parent,
|
|
resizable=True,
|
|
)
|
|
self.add_buttons("_Cancel", Gtk.ResponseType.CANCEL)
|
|
self.add_buttons("_OK", Gtk.ResponseType.OK)
|
|
self.set_default_response(Gtk.ResponseType.OK)
|
|
|
|
def pick_folder(self) -> Path | None:
|
|
res = self.run()
|
|
if res in (Gtk.ResponseType.CANCEL, Gtk.ResponseType.DELETE_EVENT):
|
|
self.destroy()
|
|
return None
|
|
if res == Gtk.ResponseType.OK:
|
|
folder = self.get_current_folder()
|
|
if folder is None:
|
|
try:
|
|
uri = self.get_uri()
|
|
if uri is None:
|
|
return None
|
|
return Path.from_uri(uri)
|
|
except Exception:
|
|
# FIXME: edge cases
|
|
return Path("/")
|
|
self.destroy()
|
|
return Path(folder)
|
|
return None
|