app.nit: events are propagated to parents of controls
authorAlexis Laferrière <alexis.laf@xymus.net>
Thu, 23 Jun 2016 22:09:44 +0000 (18:09 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Fri, 24 Jun 2016 01:21:16 +0000 (21:21 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/app/ui.nit

index cb45ac2..996629f 100644 (file)
@@ -107,11 +107,17 @@ class Control
 
        # Direct parent `Control` in the control tree
        #
+       # The parents (direct and indirect) receive all events from `self`,
+       # like the `observers`.
+       #
        # If `null` then `self` is at the root of the tree, or not yet attached.
        var parent: nullable CompositeControl = null is private writable(set_parent)
 
        # Direct parent `Control` in the control tree
        #
+       # The parents (direct and indirect) receive all events from `self`,
+       # like the `observers`.
+       #
        # Setting `parent` calls `remove` on the old parent and `add` on the new one.
        fun parent=(parent: nullable CompositeControl)
        is autoinit     do
@@ -124,12 +130,25 @@ class Control
 
                set_parent parent
        end
+
+       # Also notify the parents (both direct and indirect)
+       redef fun notify_observers(event)
+       do
+               super
+
+               var p = parent
+               while p != null do
+                       p.on_event event
+                       p = p.parent
+               end
+       end
 end
 
 # A `Control` grouping other controls
 class CompositeControl
        super Control
 
+       # Child controls composing this control
        protected var items = new Array[Control]
 
        # Add `item` as a child of `self`