mirror of
https://github.com/aclist/dztui.git
synced 2026-11-06 10:00:54 +01:00
61 lines
1.8 KiB
Python
61 lines
1.8 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,
|
|
parent=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) -> None:
|
|
super().__init__(
|
|
title=picker.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 not None:
|
|
self.destroy()
|
|
return Path(folder)
|
|
return None
|