Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions data/app.metainfo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
<li>Library: Simplify "Column View" entry</li>
<li>Library: Simplify "Status Page" entry</li>
<li>Library: Modernize "HTTP Image" entry</li>
<li>Library: Make the "Save File" entry actually save a file</li>
<li>Library: Port "Welcome" entry to Python</li>
<li>Library: Port "Actions" entry to Python</li>
<li>Library: Port "Spinner" entry to Python</li>
Expand Down
20 changes: 18 additions & 2 deletions src/Library/demos/Save File/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 20 additions & 7 deletions src/Library/demos/Save File/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down