arbitrary cleanup; add missing features to Variable
This commit is contained in:
parent
5a6284d697
commit
2ddb8a05cd
@ -4,8 +4,6 @@ gi.require_version("GObject", "2.0")
|
|||||||
|
|
||||||
from gi.repository import GObject
|
from gi.repository import GObject
|
||||||
|
|
||||||
# from variable import Variable
|
|
||||||
|
|
||||||
class Binding(GObject.Object):
|
class Binding(GObject.Object):
|
||||||
def __init__(self, emitter: GObject.GObject, property: str | None = None, transform_fn = lambda x: x):
|
def __init__(self, emitter: GObject.GObject, property: str | None = None, transform_fn = lambda x: x):
|
||||||
self.emitter = emitter
|
self.emitter = emitter
|
||||||
|
@ -12,7 +12,6 @@ class AstalPy(Astal.Application):
|
|||||||
request_handler: Optional[Callable[[str, Callable[[Any], None]], None]] = None
|
request_handler: Optional[Callable[[str, Callable[[Any], None]], None]] = None
|
||||||
|
|
||||||
def do_astal_application_request(self, msg: str, conn: Gio.SocketConnection):
|
def do_astal_application_request(self, msg: str, conn: Gio.SocketConnection):
|
||||||
print(msg)
|
|
||||||
if callable(self.request_handler):
|
if callable(self.request_handler):
|
||||||
def respond(response: Any):
|
def respond(response: Any):
|
||||||
AstalIO.write_sock(conn, str(response), None, None)
|
AstalIO.write_sock(conn, str(response), None, None)
|
||||||
|
@ -31,6 +31,9 @@ class Variable(AstalIO.VariableBase):
|
|||||||
def __del__(self):
|
def __del__(self):
|
||||||
self.emit_dropped()
|
self.emit_dropped()
|
||||||
|
|
||||||
|
def __call__(self, transform: Callable = lambda x: x):
|
||||||
|
return Binding(self).transform(transform)
|
||||||
|
|
||||||
def subscribe(self, callback):
|
def subscribe(self, callback):
|
||||||
id = self.emitter.connect(
|
id = self.emitter.connect(
|
||||||
'changed',
|
'changed',
|
||||||
@ -38,6 +41,7 @@ class Variable(AstalIO.VariableBase):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def unsubscribe(_=None):
|
def unsubscribe(_=None):
|
||||||
|
self.emit_dropped()
|
||||||
self.emitter.disconnect(id)
|
self.emitter.disconnect(id)
|
||||||
|
|
||||||
return unsubscribe
|
return unsubscribe
|
||||||
@ -126,9 +130,21 @@ class Variable(AstalIO.VariableBase):
|
|||||||
def is_watching(self):
|
def is_watching(self):
|
||||||
return self.watch_proc != None
|
return self.watch_proc != None
|
||||||
|
|
||||||
def observe(self, object, signal, callback=lambda _, x: x):
|
def observe(self, object, signal_or_callback, callback=lambda _, x: x):
|
||||||
# not sure about this
|
if isinstance(signal_or_callback, str):
|
||||||
object.connect(signal, lambda *args: self.set_value(callback(*args)))
|
f = callback
|
||||||
|
|
||||||
|
else:
|
||||||
|
f = signal_or_callback
|
||||||
|
|
||||||
|
set = lambda *args: self.set(f(*args))
|
||||||
|
|
||||||
|
if isinstance(signal_or_callback, str):
|
||||||
|
object.connect(signal_or_callback, set)
|
||||||
|
|
||||||
|
if isinstance(object, list):
|
||||||
|
for connectable, signal in object:
|
||||||
|
connectable.connect(signal, set)
|
||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user