[meld/pathlabel] ui.filebutton: Simple GtkButton subclass for handling file selection



commit 9c067f89f133933ed6b60879d8593451f7fbc4e7
Author: Kai Willadsen <kai willadsen gmail com>
Date:   Sat Jan 16 11:26:33 2021 +1000

    ui.filebutton: Simple GtkButton subclass for handling file selection
    
    This is essentially a simpler version of GtkFileChooserButton, but
    without the specific folder handling or the display style. This is
    partially to fit in better with our newer UI style, and partly because
    GtkFileChooserButton is now going away in GTK 4.0.

 meld/ui/filebutton.py   | 77 +++++++++++++++++++++++++++++++++++++++++++++++++
 meld/ui/gladesupport.py |  1 +
 2 files changed, 78 insertions(+)
---
diff --git a/meld/ui/filebutton.py b/meld/ui/filebutton.py
new file mode 100644
index 00000000..665ec986
--- /dev/null
+++ b/meld/ui/filebutton.py
@@ -0,0 +1,77 @@
+
+from typing import Optional
+
+from gi.repository import Gio, GObject, Gtk
+
+
+class MeldFileButton(Gtk.Button):
+    __gtype_name__ = "MeldFileButton"
+
+    file: Optional[Gio.File] = GObject.Property(
+        type=Gio.File,
+        nick="Most recently selected file",
+    )
+
+    pane: int = GObject.Property(
+        type=int,
+        nick="Index of pane associated with this file selector",
+        flags=(
+            GObject.ParamFlags.READWRITE |
+            GObject.ParamFlags.CONSTRUCT_ONLY
+        ),
+    )
+
+    action: Gtk.FileChooserAction = GObject.Property(
+        type=Gtk.FileChooserAction,
+        nick="File selector action",
+        flags=(
+            GObject.ParamFlags.READWRITE |
+            GObject.ParamFlags.CONSTRUCT_ONLY
+        ),
+        default=Gtk.FileChooserAction.OPEN,
+    )
+
+    dialog_label: str = GObject.Property(
+        type=str,
+        nick="Label for the file selector dialog",
+        flags=(
+            GObject.ParamFlags.READWRITE |
+            GObject.ParamFlags.CONSTRUCT_ONLY
+        ),
+    )
+
+    @GObject.Signal('file-selected')
+    def file_selected_signal(self, pane: int, file: Gio.File) -> None:
+        ...
+
+    icon_action_map = {
+        Gtk.FileChooserAction.OPEN: "document-open-symbolic",
+        Gtk.FileChooserAction.SELECT_FOLDER: "folder-open-symbolic",
+    }
+
+    def do_realize(self) -> None:
+        Gtk.Button.do_realize(self)
+
+        image = Gtk.Image.new_from_icon_name(
+            self.icon_action_map[self.action], Gtk.IconSize.BUTTON)
+        self.set_image(image)
+
+    def do_clicked(self) -> None:
+        dialog = Gtk.FileChooserNative(
+            title=self.dialog_label,
+            transient_for=self.get_toplevel(),
+            action=self.action,
+        )
+
+        if self.file:
+            dialog.set_file(self.file)
+
+        response = dialog.run()
+        gfile = dialog.get_file()
+        dialog.destroy()
+
+        if response != Gtk.ResponseType.ACCEPT or not gfile:
+            return
+
+        self.file = gfile
+        self.file_selected_signal.emit(self.pane, self.file)
diff --git a/meld/ui/gladesupport.py b/meld/ui/gladesupport.py
index 30a1680e..ee23ccd7 100644
--- a/meld/ui/gladesupport.py
+++ b/meld/ui/gladesupport.py
@@ -10,6 +10,7 @@ from meld import linkmap  # noqa: F401
 from meld import preferences  # noqa: F401
 from meld import sourceview  # noqa: F401
 from meld.ui import emblemcellrenderer  # noqa: F401
+from meld.ui import filebutton  # noqa: F401
 from meld.ui import historyentry  # noqa: F401
 from meld.ui import msgarea  # noqa: F401
 from meld.ui import notebook  # noqa: F401


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]