From 6ab8db2d2472abd545cb4873ae21ed11adc8d817 Mon Sep 17 00:00:00 2001 From: sowgro Date: Tue, 22 Jul 2025 02:22:52 -0400 Subject: Initial commit --- README.md | 10 ++++++++ nautilus-newfile.py | 37 ++++++++++++++++++++++++++++ new-file-dialog.ui | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 README.md create mode 100644 nautilus-newfile.py create mode 100644 new-file-dialog.ui diff --git a/README.md b/README.md new file mode 100644 index 0000000..fc697a1 --- /dev/null +++ b/README.md @@ -0,0 +1,10 @@ +# nautilus-newfile +Adds a "New File..." option to the context menu of nautilus + +### Installation +Copy both `nautilus-newfile.py` and `new-file-dialog.ui` to `~/.local/share/nautilus-python/extensions` + +### Limitations +- No filename validation is run (needs implementation) +- Modal is not attached to the nautilus window (limitation of the nautilus extensions api) +- The newly created file is not automatically selected (limitation of the nautilus extensions api) \ No newline at end of file diff --git a/nautilus-newfile.py b/nautilus-newfile.py new file mode 100644 index 0000000..0af4831 --- /dev/null +++ b/nautilus-newfile.py @@ -0,0 +1,37 @@ +import os, gi +gi.require_version('Adw', '1') +gi.require_version("Gtk", "4.0") +from gi.repository import Nautilus, GObject, Adw, Gtk, Gio +from typing import List + +@Gtk.Template(filename=os.path.join(__file__, "../new-file-dialog.ui")) +class NewFileDialog(Adw.Dialog): + fileinfo:Gio.FileInfo = None + __gtype_name__ = "NewFileDialog" + name_entry: Adw.EntryRow = Gtk.Template.Child() + + def __init__(self, fileinfo): + super().__init__() + self.fileinfo = fileinfo + + @Gtk.Template.Callback() + def on_create_clicked(self, _): + f: Gio.File = (self.fileinfo.get_location()) + f.get_child(self.name_entry.get_text()).create(Gio.FileCreateFlags(0), None) + self.close() + +class NautilusNewfile(GObject.GObject, Nautilus.MenuProvider): + def show_dialog(self, _, fileinfo): + + dialog = NewFileDialog(fileinfo) + dialog.present() + + def get_background_items(self, fileinfo: Gio.FileInfo) -> List[Nautilus.MenuItem]: + + menuitem = Nautilus.MenuItem( + name="ExampleMenuProvider::Foo2", + label="New File...", + ) + menuitem.connect('activate', self.show_dialog, fileinfo) + + return [menuitem,] \ No newline at end of file diff --git a/new-file-dialog.ui b/new-file-dialog.ui new file mode 100644 index 0000000..2553951 --- /dev/null +++ b/new-file-dialog.ui @@ -0,0 +1,69 @@ + + + + + + + + + + + + -- cgit v1.2.3