Skip to content

Commit 6e9dce4

Browse files
vkarehflexiondotorg
authored andcommitted
Use GAppInfo to launch applications
This removes the need to rely on hacks like double-fork. It also fixes an issue where certain windows launched through mate-menu are not immediately in focus.
1 parent 88e846c commit 6e9dce4

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

mate_menu/execute.py

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
2020

2121
import os
22-
import shlex
23-
import subprocess
22+
23+
from gi.repository import Gdk, Gtk, Gio
2424

2525
def RemoveArgs(Execline):
2626
NewExecline = []
@@ -33,21 +33,34 @@ def RemoveArgs(Execline):
3333
NewExecline.append(elem)
3434
return NewExecline
3535

36-
# Actually execute the command
37-
def ExecuteCommand(cmd , commandCwd=None):
38-
if not commandCwd:
39-
cwd = os.path.expanduser( "~" );
40-
else:
36+
# Actually launch the application
37+
def Launch(cmd, cwd=None):
38+
if cwd:
39+
cmd = cwd + ' ' + cmd
40+
41+
app_info = Gio.AppInfo.create_from_commandline(cmd,
42+
None,
43+
Gio.AppInfoCreateFlags.SUPPORTS_STARTUP_NOTIFICATION)
44+
45+
display = Gdk.Display.get_default()
46+
context = display.get_app_launch_context()
47+
context.set_desktop(-1) # use default screen & desktop
48+
context.set_timestamp(Gtk.get_current_event_time())
49+
50+
app_info.launch(None, context)
51+
52+
def Execute(cmd , commandCwd=None):
53+
if commandCwd:
4154
tmpCwd = os.path.expanduser( commandCwd );
4255
if (os.path.exists(tmpCwd)):
4356
cwd = tmpCwd
57+
else:
58+
cwd = None
4459

4560
if isinstance( cmd, str ):
4661
if (cmd.find("/home/") >= 0) or (cmd.find("xdg-su") >= 0) or (cmd.find("\"") >= 0):
47-
print("running manually...")
4862
try:
49-
os.chdir(cwd)
50-
subprocess.Popen(shlex.split(cmd))
63+
Launch(cmd, cwd)
5164
return True
5265
except Exception as detail:
5366
print(detail)
@@ -56,19 +69,9 @@ def ExecuteCommand(cmd , commandCwd=None):
5669
cmd = RemoveArgs(cmd)
5770

5871
try:
59-
os.chdir( cwd )
6072
string = ' '.join(cmd)
61-
subprocess.Popen(shlex.split(string))
73+
Launch(string, cwd)
6274
return True
6375
except Exception as detail:
6476
print(detail)
6577
return False
66-
67-
# Execute cmd using the double fork method
68-
def Execute(cmd, commandCwd=None):
69-
child_pid = os.fork()
70-
if child_pid == 0:
71-
ExecuteCommand(cmd, commandCwd)
72-
os._exit(0)
73-
else:
74-
os.wait()

mate_menu/plugins/applications.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import subprocess
3333
import filecmp
3434
from mate_menu.easybuttons import *
35-
from mate_menu.execute import Execute
3635
from mate_menu.easygsettings import EasyGSettings
3736
from mate_menu.easyfiles import *
3837

0 commit comments

Comments
 (0)