<s> from atom . api import Atom , Event , Typed , ForwardTyped <EOL> from enaml . application import Application <EOL> from enaml . core . declarative import Declarative , d_ <EOL> from enaml . core . object import flag_generator , flag_property <EOL> class ProxyToolkitObject ( Atom ) : <EOL> """<STR_LIT>""" <EOL> declaration = ForwardTyped ( lambda : ToolkitObject ) <EOL> def activate_top_down ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def activate_bottom_up ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def destroy ( self ) : <EOL> """<STR_LIT>""" <EOL> del self . declaration <EOL> def parent ( self ) : <EOL> """<STR_LIT>""" <EOL> d = self . declaration . parent <EOL> if isinstance ( d , ToolkitObject ) : <EOL> return d . proxy <EOL> def children ( self ) : <EOL> """<STR_LIT>""" <EOL> for d in self . declaration . children : <EOL> if isinstance ( d , ToolkitObject ) : <EOL> yield d . proxy <EOL> def child_added ( self , child ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def child_removed ( self , child ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> ACTIVE_PROXY_FLAG = flag_generator . next ( ) <EOL> class ToolkitObject ( Declarative ) : <EOL> """<STR_LIT>""" <EOL> activated = d_ ( Event ( ) , writable = False ) <EOL> proxy = Typed ( ProxyToolkitObject ) <EOL> proxy_is_active = flag_property ( ACTIVE_PROXY_FLAG ) <EOL> def initialize ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . proxy : <EOL> app = Application . instance ( ) <EOL> if app is None : <EOL> msg = '<STR_LIT>' <EOL> raise RuntimeError ( msg ) <EOL> self . proxy = app . create_proxy ( self ) <EOL> super ( ToolkitObject , self ) . initialize ( ) <EOL> def destroy ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( ToolkitObject , self ) . destroy ( ) <EOL> self . proxy_is_active = False <EOL> if self . proxy : <EOL> self . proxy . destroy ( ) <EOL> del self . proxy <EOL> def child_added ( self , child ) : <EOL> """<STR_LIT>""" <EOL> super ( ToolkitObject , self ) . child_added ( child ) <EOL> if isinstance ( child , ToolkitObject ) and self . proxy_is_active : <EOL> if not child . proxy_is_active : <EOL> child . activate_proxy ( ) <EOL> self . proxy . child_added ( child . proxy ) <EOL> def child_removed ( self , child ) : <EOL> """<STR_LIT>""" <EOL> super ( ToolkitObject , self ) . child_removed ( child ) <EOL> if isinstance ( child , ToolkitObject ) and self . proxy_is_active : <EOL> self . proxy . child_removed ( child . proxy ) <EOL> def activate_proxy ( self ) : <EOL> """<STR_LIT>""" <EOL> self . activate_top_down ( ) <EOL> for child in self . children : <EOL> if isinstance ( child , ToolkitObject ) : <EOL> child . activate_proxy ( ) <EOL> self . activate_bottom_up ( ) <EOL> self . proxy_is_active = True <EOL> self . activated ( ) <EOL> def activate_top_down ( self ) : <EOL> """<STR_LIT>""" <EOL> self . proxy . activate_top_down ( ) <EOL> def activate_bottom_up ( self ) : <EOL> """<STR_LIT>""" <EOL> self . proxy . activate_bottom_up ( ) <EOL> def _update_proxy ( self , change ) : <EOL> """<STR_LIT>""" <EOL> if self . proxy_is_active and change [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> handler = getattr ( self . proxy , '<STR_LIT>' + change [ '<STR_LIT:name>' ] , None ) <EOL> if handler is not None : <EOL> handler ( change [ '<STR_LIT:value>' ] ) </s>
