diff --git a/data/app.metainfo.xml b/data/app.metainfo.xml index de10fcc9d..888d14506 100644 --- a/data/app.metainfo.xml +++ b/data/app.metainfo.xml @@ -63,6 +63,7 @@
  • Library: Simplify "Column View" entry
  • Library: Simplify "Status Page" entry
  • Library: Modernize "HTTP Image" entry
  • +
  • Library: Make the "Save File" entry actually save a file
  • Library: Port "Welcome" entry to Python
  • Library: Port "Actions" entry to Python
  • Library: Port "Spinner" entry to Python
  • diff --git a/src/Library/demos/Save File/main.js b/src/Library/demos/Save File/main.js index cde42ccc2..fd15b797c 100644 --- a/src/Library/demos/Save File/main.js +++ b/src/Library/demos/Save File/main.js @@ -2,15 +2,31 @@ import Gio from "gi://Gio"; import Gtk from "gi://Gtk"; Gio._promisify(Gtk.FileDialog.prototype, "save", "save_finish"); +Gio._promisify( + Gio.File.prototype, + "replace_contents_async", + "replace_contents_finish", +); + const button = workbench.builder.get_object("button"); async function saveFile() { const dialog = new Gtk.FileDialog({ initial_name: "Workbench.txt", }); - // "save" returns a Gio.File you can write to + // "dialog.save" returns a Gio.File you can write to const file = await dialog.save(workbench.window, null); - console.log(`Save file to ${file.get_path()}`); + + const contents = new TextEncoder().encode("Hello from Workbench!"); + await file.replace_contents_async( + contents, + null, + false, + Gio.FileCreateFlags.NONE, + null, + ); + + console.log(`File ${file.get_basename()} saved`); } // Handle button click diff --git a/src/Library/demos/Save File/main.py b/src/Library/demos/Save File/main.py index dd42ff0af..c7f02a741 100644 --- a/src/Library/demos/Save File/main.py +++ b/src/Library/demos/Save File/main.py @@ -7,15 +7,28 @@ button = workbench.builder.get_object("button") -def on_output_path_selected(_dialog, result): - # "save_finish" returns a Gio.File you can write to - file = _dialog.save_finish(result) - print(f"Save file to {file.get_path()}") - - def save_file(button): dialog = Gtk.FileDialog(initial_name="Workbench.txt") - dialog.save(workbench.window, None, on_output_path_selected) + dialog.save(parent=workbench.window, cancellable=None, callback=on_save) + + +def on_save(dialog, result): + # "save_finish" returns a Gio.File you can write to + file = dialog.save_finish(result) + contents = "Hello from Workbench!".encode("UTF-8") + file.replace_contents_async( + contents, + etag=None, + make_backup=False, + flags=Gio.FileCreateFlags.NONE, + cancellable=None, + callback=on_replace_contents, + ) + + +def on_replace_contents(file, result): + file.replace_contents_finish(result) + print(f"File {file.get_basename()} saved") # Handle button click