UI Elements everywhere
One of the most certifiably useful things that the text editor can do is embed GUI elements inside the main flow of the text. A simple right-click is all you need to start setting variables equal to sliders, graphs and other more esoteric objects. But this means that, to change a parameter, you have to open up the code and look for it. Sometimes this gets tedious and a little error prone: how about using TheCanvas as a place to put sliders, graphs and other things?
The following code snipped does just that:
slider1= sheetSlider("slider1") xySlider2 = sheetXY("xySlider2") aGraph = sheetGraph("aGraph") print slider1.get() print xySlider2.get() print aGraph.get().get(0.2) slider1.set(0.5) xySlider2.set( (0.2, 0.6))
The first three lines, when executed, will make a slider (that lets you pick a single float between 0 and 1), an XYSlider (two floats) and a graph (a mapping from 0->1).
The latter two can be expanded, just like they can be expanded in the main editor:
Now these "important numbers" can be edited without opening up the body of the document
And they can also be resized by grabbing their right-most margin while holding down shift. More Canvas emebeddable widgets are coming soon (sheetCombo for example). Now that the framework is in place, anything that it makes sense to port over to the canvas will.
sheetX() conventions
The signatures for the sheetSlider (and sheetXYSlider &c} functions are as follows (note, you can always get these by using ⌘-period completion):
def sheetSlider(name, initialValue=None, onChange=None): ....
Specifically you can set initialValue to be a value that is used only when the widget is created for the first time. You can optionally pass in a function to be called when the slider is updated.
Other sheetX functions are being added. For example, sheetCombo 'prints' a Menu popup / ComboBox to the sheet:
From:
myCombo = sheetCombo("myCombo", (["apple", "peach", "pear"],0)).value() print myCombo # prints "apple", unless you have selected something else
It's important to note the convention in these functions — you pass in an "initial value" which is used only if that UI element is actually created anew by the call to sheetX. Otherwise it's largely ignored. A call to get or value returns what the actual UI element contains that this moment — this value is persisted across restarts of Field, it is stored with the sheet itself.
Finally there's a sheetDeleteUI(name) function that eliminates UI things that you have created.
Outputing elements
Now that we can have sliders in the text editor and in the canvas, one spot remains unconquered by text-embeddable GUI elements: the text-editor output window.
The range of things that it makes sense to "print" is quite small (although printing a slider with a callback might be useful, it's not different enough to have found itself coded-up yet).
But buttons can be very useful:
def printWorld(): print "world" printButton("hello?", printWorld)
Yields:
Followed by the inevitable:
Marc has used these to provide ad-hoc "wizards" that direct you through a series of operations in the canvas. In any case keep tabs on OutputInserts to track the development of this feature.
If you'd like to embed your own (or anybody else's) Swing components into the canvas, take a look at SwingIntegration.




