Skip to content

Commit 37379c8

Browse files
authored
library: Port Launcher demo to Vala (#765)
* library: Port Launcher demo to Vala * Connect callbacks to async functions' `.begin`
1 parent a692ef9 commit 37379c8

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env -S vala workbench.vala --pkg libadwaita-1
2+
3+
private Gtk.FileLauncher file_launcher;
4+
private Gtk.Entry uri_details;
5+
6+
public void main () {
7+
var launch_file = (Gtk.Button) workbench.builder.get_object ("launch_file");
8+
var change_file = (Gtk.Button) workbench.builder.get_object ("change_file");
9+
var file_location = (Gtk.Button) workbench.builder.get_object ("file_location");
10+
var file_name = (Gtk.Label) workbench.builder.get_object ("file_name");
11+
uri_details = (Gtk.Entry) workbench.builder.get_object ("uri_details");
12+
var uri_launch = (Gtk.Button) workbench.builder.get_object ("uri_launch");
13+
14+
// File Launcher
15+
var file = File.new_for_uri (workbench.resolve ("workbench.txt"));
16+
file_launcher = new Gtk.FileLauncher (file) {
17+
always_ask = true
18+
};
19+
20+
launch_file.clicked.connect (on_launch_file.begin);
21+
file_launcher.notify["file"].connect (() => {
22+
try {
23+
FileInfo info = file_launcher.file.query_info ("standard::display-name", NONE, null);
24+
file_name.label = info.get_display_name ();
25+
} catch (Error e) {
26+
critical (e.message);
27+
}
28+
});
29+
30+
file_location.clicked.connect (open_file_location.begin);
31+
change_file.clicked.connect (select_file.begin);
32+
33+
// URI Launcher
34+
uri_launch.clicked.connect (launch_uri.begin);
35+
uri_details.changed.connect (() => {
36+
try {
37+
uri_launch.sensitive = Uri.is_valid (uri_details.text, NONE);
38+
} catch (Error e) {
39+
uri_launch.sensitive = false;
40+
}
41+
});
42+
}
43+
44+
private async void on_launch_file () {
45+
try {
46+
bool success = yield file_launcher.launch (workbench.window, null);
47+
if (!success) {
48+
critical (@"Failed to open $(file_launcher.file.get_uri ())");
49+
}
50+
} catch (Error e) {
51+
critical (e.message);
52+
}
53+
}
54+
55+
private async void open_file_location () {
56+
try {
57+
bool success = yield file_launcher.open_containing_folder (workbench.window, null);
58+
if (!success) {
59+
critical ("Failed to open containing folder");
60+
}
61+
} catch (Error e) {
62+
critical (e.message);
63+
}
64+
}
65+
66+
private async void select_file () {
67+
var file_dialog = new Gtk.FileDialog ();
68+
try {
69+
file_launcher.file = yield file_dialog.open (workbench.window, null);
70+
} catch (Error e) {
71+
critical (e.message);
72+
}
73+
}
74+
75+
private async void launch_uri () {
76+
var uri_launcher = new Gtk.UriLauncher (uri_details.text);
77+
try {
78+
bool success = yield uri_launcher.launch (workbench.window, null);
79+
if (!success) {
80+
critical (@"Failed to launch $(uri_launcher.uri)");
81+
}
82+
} catch (Error e) {
83+
critical (e.message);
84+
}
85+
}

0 commit comments

Comments
 (0)