-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
AppButton.js
103 lines (88 loc) · 2.83 KB
/
AppButton.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import Gtk from "gi://Gtk";
import GLib from "gi://GLib";
import Gio from "gi://Gio";
import Gdk from "gi://Gdk";
export default function AppButton({ appInfo, content_type, entry, window }) {
const button = new Gtk.Button();
const icon = appInfo.get_icon();
if (icon) {
const image = Gtk.Image.new_from_gicon(appInfo.get_icon());
image.pixel_size = 92;
button.set_child(image);
} else {
button.label = appInfo.get_name();
}
button.set_tooltip_text(appInfo.get_name());
function open() {
try {
openWithApplication({
appInfo,
resource: entry.get_text(),
content_type,
});
return true;
} catch (err) {
logError(err);
return false;
}
}
// Ctrl+Click should not close Junction
// but it's not possible to do that with GtkButton
// I was told to implement my own widget and do the following
// const eventController = new Gtk.GestureClick();
// widget.add_controller(eventController);
// eventController.connect("released", () => {
// const evt = eventController.get_current_event();
// const modifier_state = evt.get_modifier_state();
// // Ctrl click
// if (modifier_state & Gdk.ModifierType.CONTROL_MASK) return;
// window.close();
// });
// but I'm too lazy ATM to implement a custom widget for this
button.connect("clicked", () => {
const success = open();
if (success) window.close();
});
const eventController = new Gtk.GestureSingle({
button: Gdk.BUTTON_MIDDLE,
});
button.add_controller(eventController);
eventController.connect("end", () => {
open();
});
return { button };
}
function openWithApplication({ appInfo, resource, content_type }) {
if (GLib.getenv("FLATPAK_ID")) {
appInfo = flatpakSpawnify(appInfo);
}
const success = appInfo.launch_uris([resource], null);
if (!success) {
throw new Error(`Could not launch ${resource} with ${appInfo.get_id()}`);
}
if (!GLib.getenv("FLATPAK_ID")) {
// On Flatpak fails with
// (re.sonny.Junction:3): Gjs-WARNING **: 18:35:39.427: JS ERROR: Gio.IOErrorEnum: Can’t create user desktop file /home/sonny/.var/app/re.sonny.Junction/data/applications/userapp-YOGA Image Optimizer-20X240.desktop
appInfo.set_as_last_used_for_type(content_type);
}
}
function flatpakSpawnify(appInfo) {
const filename = appInfo.get_filename();
if (!filename) {
return appInfo;
}
const keyFile = new GLib.KeyFile();
if (!keyFile.load_from_file(filename, GLib.KeyFileFlags.NONE)) {
return null;
}
const Exec = keyFile.get_value("Desktop Entry", "Exec");
if (Exec.startsWith("flatpak-spawn")) {
return null;
}
if (!Exec) {
logError(new Error(`No Exec for ${filename}`));
return null;
}
keyFile.set_value("Desktop Entry", "Exec", `flatpak-spawn --host ${Exec}`);
return Gio.DesktopAppInfo.new_from_keyfile(keyFile);
}