Skip to content

Commit 624c62c

Browse files
authored
Library: Write to file in Save File entry (#809)
1 parent 180bf26 commit 624c62c

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

data/app.metainfo.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
<li>Library: Simplify "Column View" entry</li>
6464
<li>Library: Simplify "Status Page" entry</li>
6565
<li>Library: Modernize "HTTP Image" entry</li>
66+
<li>Library: Make the "Save File" entry actually save a file</li>
6667
<li>Library: Port "Welcome" entry to Python</li>
6768
<li>Library: Port "Actions" entry to Python</li>
6869
<li>Library: Port "Spinner" entry to Python</li>

src/Library/demos/Save File/main.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,31 @@ import Gio from "gi://Gio";
22
import Gtk from "gi://Gtk";
33

44
Gio._promisify(Gtk.FileDialog.prototype, "save", "save_finish");
5+
Gio._promisify(
6+
Gio.File.prototype,
7+
"replace_contents_async",
8+
"replace_contents_finish",
9+
);
10+
511
const button = workbench.builder.get_object("button");
612

713
async function saveFile() {
814
const dialog = new Gtk.FileDialog({
915
initial_name: "Workbench.txt",
1016
});
11-
// "save" returns a Gio.File you can write to
17+
// "dialog.save" returns a Gio.File you can write to
1218
const file = await dialog.save(workbench.window, null);
13-
console.log(`Save file to ${file.get_path()}`);
19+
20+
const contents = new TextEncoder().encode("Hello from Workbench!");
21+
await file.replace_contents_async(
22+
contents,
23+
null,
24+
false,
25+
Gio.FileCreateFlags.NONE,
26+
null,
27+
);
28+
29+
console.log(`File ${file.get_basename()} saved`);
1430
}
1531

1632
// Handle button click

src/Library/demos/Save File/main.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,28 @@
77
button = workbench.builder.get_object("button")
88

99

10-
def on_output_path_selected(_dialog, result):
11-
# "save_finish" returns a Gio.File you can write to
12-
file = _dialog.save_finish(result)
13-
print(f"Save file to {file.get_path()}")
14-
15-
1610
def save_file(button):
1711
dialog = Gtk.FileDialog(initial_name="Workbench.txt")
18-
dialog.save(workbench.window, None, on_output_path_selected)
12+
dialog.save(parent=workbench.window, cancellable=None, callback=on_save)
13+
14+
15+
def on_save(dialog, result):
16+
# "save_finish" returns a Gio.File you can write to
17+
file = dialog.save_finish(result)
18+
contents = "Hello from Workbench!".encode("UTF-8")
19+
file.replace_contents_async(
20+
contents,
21+
etag=None,
22+
make_backup=False,
23+
flags=Gio.FileCreateFlags.NONE,
24+
cancellable=None,
25+
callback=on_replace_contents,
26+
)
27+
28+
29+
def on_replace_contents(file, result):
30+
file.replace_contents_finish(result)
31+
print(f"File {file.get_basename()} saved")
1932

2033

2134
# Handle button click

0 commit comments

Comments
 (0)