-
Notifications
You must be signed in to change notification settings - Fork 2
/
patch-electron-desktop-files
executable file
·107 lines (85 loc) · 2.59 KB
/
patch-electron-desktop-files
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
104
105
106
107
#!/usr/bin/env python3
import shutil
from pathlib import Path
from xdg.DesktopEntry import DesktopEntry
PATCH_APPLICATIONS = [
"1password",
"code",
"code-oss",
"com.ticktick.TickTick",
"discord",
"com.discordapp.Discord", # Flatpak variant
"mattermost-desktop",
"mattermost-desktop_mattermost-desktop", # Snap variant
"obsidian",
"slack",
"slack_slack", # Snap variant
"com.slack.Slack", # Flatpak variant
"spotify",
"spotify_spotify", # Snap variant
"teams",
]
SYSTEM_APPLICATION_DIRECTORIES = [
"/usr/share/applications/",
"/var/lib/snapd/desktop/applications/",
"/var/lib/flatpak/exports/share/applications/",
]
ENABLE_FEATURES = [
"UseOzonePlatform",
"WaylandWindowDecorations",
"WebRTCPipeWireCapturer",
]
def main():
for app in PATCH_APPLICATIONS:
patch_application(app)
def patch_application(app: str) -> None:
"""
Patch an application's .deskstop entry.
If needed, this will attempt to find a corresponding .desktop file in the
system-wide application directories in order to install a patched version
in the local home directory.
"""
basename = Path(f"{app}.desktop")
p = Path.home() / ".local/share/applications" / basename
for directory in SYSTEM_APPLICATION_DIRECTORIES:
found = copy_desktop_file_from_fallback_location(
src=directory / basename,
dest=p,
)
if found:
break
# If the application file still doesn't exist after the above steps,
# then it's likely not installed and safe to skip.
if p.exists():
patch_application_desktop_file(p)
def copy_desktop_file_from_fallback_location(src: Path, dest: Path) -> bool:
if dest.exists():
return True
try:
shutil.copy(src, dest)
return True
except FileNotFoundError:
return False
def patch_application_desktop_file(path: Path) -> None:
"""
Patch the .desktop entry at the given path.
"""
entry = DesktopEntry()
entry.parse(path)
exec = entry.getExec()
assert isinstance(
exec, str
), f"Expected exec entry to be a single string, got: {exec!r}"
try:
cmd, args = exec.split(" ", maxsplit=1)
except ValueError:
cmd, args = (exec, "")
if "-ozone-platform=wayland" not in args:
args = f"{args} --ozone-platform=wayland"
for feature in ENABLE_FEATURES:
if feature not in args:
args = f"{args} --enable-features={feature}"
entry.set("Exec", f"{cmd} {args}")
entry.write()
if __name__ == "__main__":
main()