Skip to content

Commit

Permalink
Allow only one GUI instance on Linux
Browse files Browse the repository at this point in the history
Otherwise, users may be confused if they try to activate Diode
Transfer using the Applications menu: previously another instance
of Diode Transfer would be launched.

For #13
  • Loading branch information
peterstory committed Jun 10, 2024
1 parent 3ba30eb commit 260a522
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/pydiode/gui/linux.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import json
import subprocess
import sys


def activate():
"""
Check whether the GUI is already running. If so, activate the existing
window. Uses the [email protected] Gnome extension.
:returns: A boolean indicating whether an already running instance was
activated
"""
base_cmd = [
"gdbus",
"call",
"--session",
"--dest",
"org.gnome.Shell",
"--object-path",
"/org/gnome/Shell/Extensions/Windows",
"--method",
]
completed = subprocess.run(
base_cmd + ["org.gnome.Shell.Extensions.Windows.List"],
stdout=subprocess.PIPE,
)
# Trim non-JSON characters from the front and back
windows = json.loads(completed.stdout[2:-4])
windows = list(filter(lambda w: w["wm_class"] == "Diodetransfer", windows))
if windows:
subprocess.run(
base_cmd
+ [
"org.gnome.Shell.Extensions.Windows.Activate",
str(windows[0]["id"]),
],
)
return True
else:
return False
6 changes: 6 additions & 0 deletions src/pydiode/gui/main.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from tkinter import BooleanVar, IntVar, Listbox, StringVar, Tk, ttk

from pydiode.gui.common import USER_STUDY
from pydiode.gui.linux import activate
from pydiode.gui.receive import (
receive_or_cancel,
receive_test,
Expand All @@ -29,6 +30,11 @@


def gui_main():
# Ensure that only one instance of the application runs on Linux
if sys.platform == "linux" and activate():
print("Diode Transfer was already running. Activated its window.")
sys.exit(0)

# Load configuration
config = configparser.ConfigParser()
config.read(CONFIG)
Expand Down

0 comments on commit 260a522

Please sign in to comment.