Debian Codegraph @

src/pyj/qt.pyj

raw
1 # vim:fileencoding=utf-8
2 # License: GPL v3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net>
3 from __python__ import bound_methods, hash_literals
4
5 special_title = '__SPECIAL_TITLE__'
6 slots = {}
7
8
9 def ping(suffix):
10 document.title = special_title + suffix
11
12
13 def signal():
14 args = Array.from (arguments)
15 to_python._queue_message({'type': 'signal', 'name': this, 'args': args})
16
17
18 class ToPython:
19 def __init__(self):
20 self._messages = []
21 self._last_ping_value = 0
22 self._ping_timer_id = -1
23 self._queue_message({'type': 'qt-ready'})
24
25 def _ping(self):
26 if self._ping_timer_id < 0:
27 self._last_ping_value = 0 if self._last_ping_value else 1
28 self._ping_timer_id = setTimeout(def ():
29 ping(self._last_ping_value)
30 self._ping_timer_id = -1
31 , 0)
32
33 def _queue_message(self, msg):
34 self._messages.push(msg)
35 self._ping()
36
37 def _register_signals(self, signals):
38 for signal_name in signals:
39 self[signal_name] = signal.bind(signal_name)
40
41 def _poll(self):
42 ans = self._messages
43 self._messages = []
44 return ans
45
46 def _from_python(self, name, args):
47 callback = slots[name]
48 if callback:
49 callback.apply(None, args)
50 else:
51 console.warn(f'Attempt to call non-existent python-to-js slot named: {name}')
52
53
54 to_python = None # TODO: Implement this via message passing from sub-frames
55 if window is window.top:
56 window.python_comm = to_python = ToPython()
57
58
59 def from_python(func):
60 slots[func.name] = func
61 return func