Commit 9fe3fe36 authored by Taddeüs Kroes's avatar Taddeüs Kroes

Code cleanup.

parent a2f08297
...@@ -17,8 +17,8 @@ class Widget(Positionable, Logger): ...@@ -17,8 +17,8 @@ class Widget(Positionable, Logger):
def __init__(self, x=None, y=None): def __init__(self, x=None, y=None):
Positionable.__init__(self, x, y) Positionable.__init__(self, x, y)
# Map of gesture types to gesture trackers # List of all active trackers
self.trackers = {} self.trackers = []
# Map of gesture types to a list of handlers for that type # Map of gesture types to a list of handlers for that type
self.handlers = {} self.handlers = {}
...@@ -109,17 +109,27 @@ class Widget(Positionable, Logger): ...@@ -109,17 +109,27 @@ class Widget(Positionable, Logger):
# Remove handler list # Remove handler list
del self.handlers[gesture_type][:] del self.handlers[gesture_type][:]
tracker = self.find_tracker(gesture_type)
# Check if any other handlers need the tracker # Check if any other handlers need the tracker
for gtype in self.trackers[gesture_type].gesture_types: for gtype in tracker.gesture_types:
if gtype in self.handlers: if gtype in self.handlers:
return return
# No more handlers are bound, remove unused tracker and handlers # No more handlers are bound, remove unused tracker and handlers
del self.trackers[gesture_type] self.trackers.remove(gesture_type)
for gtype in self.trackers[gesture_type].gesture_types: for gtype in tracker.gesture_types:
del self.handlers[gtype] del self.handlers[gtype]
def find_tracker(self, gesture_type):
"""
Find a tracker that is tracking some gesture type.
"""
for tracker in self.trackers:
if gesture_type in tracker.gesture_types:
return tracker
def bind(self, gesture_type, handler, remove_existing=False, **kwargs): def bind(self, gesture_type, handler, remove_existing=False, **kwargs):
""" """
Bind a handler to the specified type of gesture. Create a tracker for Bind a handler to the specified type of gesture. Create a tracker for
...@@ -130,7 +140,7 @@ class Widget(Positionable, Logger): ...@@ -130,7 +140,7 @@ class Widget(Positionable, Logger):
if gesture_type not in self.handlers: if gesture_type not in self.handlers:
tracker = create_tracker(gesture_type, self) tracker = create_tracker(gesture_type, self)
tracker.configure(**kwargs) tracker.configure(**kwargs)
self.trackers[gesture_type] = tracker self.trackers.append(tracker)
self.handlers[gesture_type] = [] self.handlers[gesture_type] = []
# Create empty tracker lists for all supported gestures # Create empty tracker lists for all supported gestures
...@@ -163,14 +173,14 @@ class Widget(Positionable, Logger): ...@@ -163,14 +173,14 @@ class Widget(Positionable, Logger):
def delegate_event(self, event): def delegate_event(self, event):
""" """
Delegate a triggered event to child widgets. If a child stops Delegate a triggered event to all child widgets. If a child stops
propagation propagation, return so that its siblings and the parent widget will not
delegate the event to their trackers.
""" """
if not self.children: child_found = False
self.propagate_event(event)
else: if self.children:
event.set_offset(self.get_offset()) event.set_offset(self.get_offset())
child_found = False
# Delegate to children in reverse order because widgets that are # Delegate to children in reverse order because widgets that are
# added later, should be placed over previously added siblings # added later, should be placed over previously added siblings
...@@ -180,13 +190,13 @@ class Widget(Positionable, Logger): ...@@ -180,13 +190,13 @@ class Widget(Positionable, Logger):
child.delegate_event(event) child.delegate_event(event)
if event.is_propagation_stopped(): if event.is_propagation_stopped():
break return
if not child_found: if not child_found:
self.propagate_event(event) self.propagate_event(event)
def propagate_event(self, event): def propagate_event(self, event):
for tracker in set(self.trackers.itervalues()): for tracker in self.trackers:
tracker.handle_event(event) tracker.handle_event(event)
if event.is_immediate_propagation_stopped(): if event.is_immediate_propagation_stopped():
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment