Skip to content

Commit fb94dc6

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 d3ee3c3 commit fb94dc6

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
@@ -16,6 +16,7 @@ def __init__(self):
1616
import gi.repository.GLib
1717
try:
1818
self.gnome_shell = self.bus.get("org.gnome.Shell")
19+
self._setup_gnome()
1920
except gi.repository.GLib.Error:
2021
self.gnome_shell = None
2122

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

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

2845
def get_current_window_gnome_shell(self) -> dict:
2946
"""get current app from GNOME Shell via dbus"""
30-
js_code = """
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-
result
38-
"""
3947

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

0 commit comments

Comments
 (0)