nucleic / enaml

Declarative User Interfaces for Python
http://enaml.readthedocs.io/en/latest/
Other
1.54k stars 132 forks source link

Allow out of order declaration of aliases #110

Open deepankarsharma opened 10 years ago

deepankarsharma commented 10 years ago

Code pasted inline breaks currently.

from enaml.widgets.api import Window, Container, PushButton

enamldef Content(Container): """ The primary application content.

This 'button_foreground' alias provides access to the internal
push button's foreground color.

"""
alias button_bar: button.bar
PushButton: button:
    attr bar = 1

enamldef Main(Window): """ The main application window.

This window uses the 'button_foreground' alias of the central
content to bind to its internal push button's foreground color.

"""
title = 'Simple Attribute Alias'
Content: pass
sccolbert commented 10 years ago

For my own personal notes:

Declaring the alias after the full definition of the PushButton would work, as would using a simple enamldef for the button to encapsulate the bar attr.

The Enaml compiler currently operates in two passes, the first pass builds an internal representation of the widget hierarchy, which is used to (among other things) instantiate the objects at runtime. The second pass adds the user-defined storage attributes and events, and wires up the aliases. This occurs in order of source code line number. The issue here is that the alias is being wired up before the storage attribute has been added to the push button.

I see two possible solutions 1) move the logic which adds the storage attributes to the first compiler pass, 2) move the alias wiring to a third compiler pass.

I would prefer solution number 1, but I can't recall at the moment if I had a good reason for adding the storage attributes during the second compiler pass.