Skip to content

Commit

Permalink
Page events allow multiple subscribers (#38)
Browse files Browse the repository at this point in the history
* Update control itself when isolated

Re-implement control.build()

* Page events allow multiple subscribers
  • Loading branch information
FeodorFitsner authored Jun 27, 2022
1 parent c30be7a commit f743acb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
14 changes: 14 additions & 0 deletions sdk/python/flet/event_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class EventHandler:
def __init__(self) -> None:
self.__handlers = {}

def handler(self, e):
for h in self.__handlers.keys():
h(e)

def subscribe(self, handler):
self.__handlers[handler] = True

def unsubscribe(self, handler):
if handler in self.__handlers:
self.__handlers.pop(handler)
26 changes: 18 additions & 8 deletions sdk/python/flet/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
ScrollMode,
)
from flet.control_event import ControlEvent
from flet.event_handler import EventHandler
from flet.floating_action_button import FloatingActionButton
from flet.protocol import Command
from flet.pubsub import PubSub
Expand Down Expand Up @@ -57,6 +58,15 @@ def __init__(self, conn: Connection, session_id):
self.__dark_theme = None
self.__pubsub = PubSub(conn.pubsubhub, session_id)

self.__on_close = EventHandler()
self._add_event_handler("close", self.__on_close.handler)
self.__on_resize = EventHandler()
self._add_event_handler("resize", self.__on_resize.handler)
self.__on_connect = EventHandler()
self._add_event_handler("connect", self.__on_connect.handler)
self.__on_disconnect = EventHandler()
self._add_event_handler("disconnect", self.__on_disconnect.handler)

def __enter__(self):
return self

Expand Down Expand Up @@ -505,38 +515,38 @@ def window_height(self):
# on_close
@property
def on_close(self):
return self._get_event_handler("close")
return self.__on_close

@on_close.setter
def on_close(self, handler):
self._add_event_handler("close", handler)
self.__on_close.subscribe(handler)

# on_resize
@property
def on_resize(self):
return self._get_event_handler("resize")
return self.__on_resize

@on_resize.setter
def on_resize(self, handler):
self._add_event_handler("resize", handler)
self.__on_resize.subscribe(handler)

# on_connect
@property
def on_connect(self):
return self._get_event_handler("connect")
return self.__on_connect

@on_connect.setter
def on_connect(self, handler):
self._add_event_handler("connect", handler)
self.__on_connect.subscribe(handler)

# on_disconnect
@property
def on_disconnect(self):
return self._get_event_handler("disconnect")
return self.__on_disconnect

@on_disconnect.setter
def on_disconnect(self, handler):
self._add_event_handler("disconnect", handler)
self.__on_disconnect.subscribe(handler)


class Offstage(Control):
Expand Down

0 comments on commit f743acb

Please sign in to comment.