File tree Expand file tree Collapse file tree 3 files changed +39
-9
lines changed
src/Library/demos/Save File Expand file tree Collapse file tree 3 files changed +39
-9
lines changed Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change @@ -2,15 +2,31 @@ import Gio from "gi://Gio";
22import Gtk from "gi://Gtk" ;
33
44Gio . _promisify ( Gtk . FileDialog . prototype , "save" , "save_finish" ) ;
5+ Gio . _promisify (
6+ Gio . File . prototype ,
7+ "replace_contents_async" ,
8+ "replace_contents_finish" ,
9+ ) ;
10+
511const button = workbench . builder . get_object ( "button" ) ;
612
713async 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
Original file line number Diff line number Diff line change 77button = 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-
1610def 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
You can’t perform that action at this time.
0 commit comments