Skip to content

Commit 1554767

Browse files
committed
create JS function inside gnome shell
this gives another ~23% performance increase for get_current_window_gnome_shell measured in wall clock time.
1 parent e9c4f3d commit 1554767

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

aw_watcher_window/lib.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def __init__(self):
1515
import gi.repository.GLib
1616
try:
1717
self.gnome_shell = self.bus.get("org.gnome.Shell")
18+
self._setup_gnome()
1819
except gi.repository.GLib.Error:
1920
self.gnome_shell = None
2021

@@ -23,20 +24,27 @@ def get_current_window(self) -> dict:
2324
return self.get_current_window_gnome_shell()
2425

2526
return self.get_current_window_x11()
27+
28+
def _setup_gnome(self) -> None:
29+
js_code = """
30+
global._aw_current_window = () => {
31+
var window_list = global.get_window_actors();
32+
var active_window_actor = window_list.find(window => window.meta_window.has_focus());
33+
var active_window = active_window_actor.get_meta_window()
34+
var vm_class = active_window.get_wm_class();
35+
var title = active_window.get_title()
36+
var result = {"title": title, "appname": vm_class};
37+
return result
38+
}
39+
"""
40+
ok, result = self.gnome_shell.Eval(js_code)
41+
if not ok:
42+
raise Error("failed seting up gnome-shell function: " + result)
2643

2744
def get_current_window_gnome_shell(self) -> dict:
2845
"""get current app from GNOME Shell via dbus"""
29-
js_code = """
30-
var window_list = global.get_window_actors();
31-
var active_window_actor = window_list.find(window => window.meta_window.has_focus());
32-
var active_window = active_window_actor.get_meta_window()
33-
var vm_class = active_window.get_wm_class();
34-
var title = active_window.get_title()
35-
var result = {"title": title, "appname": vm_class};
36-
result
37-
"""
3846

39-
ok, result = self.gnome_shell.Eval(js_code)
47+
ok, result = self.gnome_shell.Eval("global._aw_current_window()")
4048
if ok:
4149
result_data = json.loads(result)
4250
return result_data

0 commit comments

Comments
 (0)