What would something like jQuery look like in Smalltalk?
What follows is a sort of experience report and overview of what I came up with. It took me about 3 iterations to get to what's described below. I'm unsure at this point whether it's really pretty good, completely wrong, or sorta there but sorta not.
Motivation
My motivation for putting this together was some disturbing patterns I see all to often in VisualWorks (and for many years). The first is the reliance on the builder variable. When the VW UIBuilder assembles an interface from specs, it hangs on to the widgets that had an ID associated with them. You can then use the componentAt: message to retrieve that in application code. Here's an example from CompiledCodeInspector.
changeSelectedField
"..."
self builder == nil ifTrue: [^self].
w := self builder componentAt: #text.
w == nil ifTrue: [^self].
"..."
There are a couple of different problems I have with this. First of all, there's the need to first check if we have a builder. Much of this type of code is writen with a "if the builders there, do this, otherwise who cares" approach. Then we turn around and do a similar check on the result to make sure it's not nil. Later on in this method, not shown, we have to send things like widget and widget controller to it to further manipulate it. #controllerAt:, #wrapperAt:, and #widgetAt: all exist to try and help smooth this out.
We really shouldn't be having code needed to retain artifacts of the assembling process to get at widgets. That information ought to be encapsulated with the widgets themselves. But instead, we have to worry about whether we have enough state yet in two different places to determine whether we can act or not. And what we get back is... well it's a Wrapper. Usually. Of some sort. But usually we want the view object. And so we have to become intimately familiar with what was constructed and how it is related to be able to get at it.
We also may have to worry about where it's actually stored at. In the case of subcanvasing, where a different builder is used to assemble subsets of UI, we end up with cases like found in the ClassCreationDialog, where we have to add an extra instance variable to keep track of those subbuilders, just so we can use the componentAt: methods. The widgets are there in the view tree, but alas, the only way to get at them is from the builder.
To see examples of the second pattern that bothers me, do this.
- Open a System Browser
- Select all Packages using Ctrl-a
- Select the Rewrite tool (in the mid page tab control)
- Enter ``@r container container in the Search for: box
- Click the Search... button and wait for the result
One of the more egregious examples is found in SpinButtonController
pressAction
| compositeView inputBoxController |
"..."
compositeView := view container container.
inputBoxController := compositeView controller.
(view spin: inputBoxController view model value) ifFalse: [^self].
compositeView container container takeKeyboardFocus
Two times in the same method, the code walks up the view tree. Not just with one container send, but with two. There's explicit assumptions being made here about the structure of the view tree. Needing to have one widget reach out and communicate with another is not terrible. But exploiting the particular structure of the view tree violates encapsulation. This kind of a code is a huge headache for us as we consider how to move beyond and reduce the amount of Wrappers in our view trees. Getting rid of WidgetStateWrappers is actually probably achievable, but every site like this will need to be fixed. The only information we should realistically be exploiting is that "somewhere above me is an object that fulfills a role I know about and I'd like to talk to it."
Three Interesting Aspect of jQuery
First of all, I am not a jQuery expert. I've played a little with it. Read about it. Asked questions about it. In those forays, there were three basic properties that intrigued me about it:
- Ability to specify a criteria to match against widgets in a DOM.
- The idea that it is always a collectionish thing that is returned, whether it has 0, 1, or many matches.
- Terseness, which allows the programmer to see more code that has to do with manipulating the matches, rather than lots of code finding matches.
Building a Query Model in Smalltalk
The first thing I set out to do was build a Query model for view trees. The jQuery selectors have at least 4 kinds of general queries.
- Type (e.g. $("div") )
- Id (e.g. $("#cancelButton") )
- Class (e.g. $(".demoGroup") )
- Attribute (e.g. $("[checked ='false']") )
I started with a basic Query object. A query can be asked if it matches: aViewTreeElement. Different subclasses capture different kinds of matches.
Type Queries
These are probably the easiest to map over. DOM's have things like div and table elements. VisualWorks view trees also have different types of elements, embodied as different Class behaviors. The only real difference, is that you have inheritance in the Smalltalk world. So you might want to match all subtypes of Wrapper. This is why I called it a TypeQuery instead of a ClassQuery. You can make one like
Query type: Wrapper
Id Queries
The skin work we've been doing in the next version of VisualWorks adds an id/id: API to VisualPart. It turns out that if we insert a leading line in the following UILookPolicy method
widgetWrapperWrapping: aWrapper decorator: aDecorator component: aComponent state: aWidgetState spec: aSpec colors: colors isOpaque: opaqueFlag inBuilder: aBuilder
| sw dt |
aComponent id: aSpec name.
"...."
Once that happens, any name of a widget given in the UIPainter is applied as the id of the actual view object (not one wrapper or another). You can make an IdQuery with
Query id: #okButton
Class Queries (or Tag Queries)
In css, which jQuery leverages, you can attach one or more arbitrary "class" attributes to an element. It's like being able to keyword tag subsets of widgets. One might, for example, have a set of widgets that all close a dialog: "Overwrite All", "Overwrite Older", "Overwrite None", and "Cancel". One could set the class of these buttons to be ".dialogAction".
We currently have nothing like this in the VisualWorks view tree. But it might be nice to have. Because the word "Class" already has a very canonized and entrenched meaning in Smalltalk, I chose to use a different name for these. I called them tags. Three methods are added to VisualPart to support this:
- tag: anObject "adds anObject as a tag to the receiver"
- hasTag: anObject "returns whether the receiver has anObject attached to it as a tag"
- untag: anObject "removes anObject as a tag from the receiver, if it is there"
I think my thought for these, like Id's, is that they would usually be Symbol objects, but there's actually nothing that says they couldn't be more complex objects. A TagQuery would be created with an expression like
Query tag: #dialogActions
Attribute Queries (or Block Queries)
I did not create something like Attribute Queries. The VisualWorks view tree elements don't have rigorously defined attributes. But they do respond to messages of course. So I just generalized this so that you could use a BlockClosure (actually any object that responds to cull:) to select elements. For example
Query block: [:element | element isEnabled]
But one could do weirder things with it
"all widgets in the lower half of a window"
Query block: [:each | (each localPointToGlobal: Point zero) y >= aWindow bounds center y]
would be used to select view tree elements that currently respond true to isEnabled. The astute reader will note that the previous three query's could all be emulated with a BlockQuery.
Logical Combinators
For full expression's sake, I also added a NotQuery, and AndQuery and an OrQuery.
(Query type: Button) | (Query id: #okField) "Either inherits from Button or has id of #okButton"
(Query type: Wrapper) & (Query block: [:each | each component isNil]) "All wrappers with no component set yet"
(Query tag: #header) not "Everything that is not tagged #header"
View Trees as Collections
a jQuery results in a collection of 0 or more elements that you can operate on. We already have a Collection API in Smalltalk. I wasn't interested in rolling a new or specific one of those. I'd rather leverage what I can do already with existing Collections. So the other half of this is the ability to create a collection facade to a view tree, rooted in one or more existing tree elements.
ViewTreeSet is a subclass of Collection, it implements the necessary messages and inherits all of the other services that Collection already provides. jQuery (I believe) always searches the whole DOM tree, top down. Where our focus is at the specific widget level, I did not want to restrict it to that. Two specific classes currently exist for ViewTreeSet. One goes down the view tree, traversing the child elements of the roots, and the other goes up, traversing the parents up to the topmost element. Putting these two pieces together (the Query and the Collection), these also have a slot for a query. We can ask ApplicationModels, VisualParts, and Windows for either their childViewTree or their parentViewTree. Now we can do things like compute just how many Wrappers are in a current UI?
(self childViewTree query: (Query type: Wrapper)) size
Or we could enable a bunch of widgets
(self childViewTree query: (Query tag: #dialogAnswer))
do: [:each | each enable]
Or programmatically drive the OK button
(self childViewTree query: (Query id: #okButton)) do: [:each | each simulateMousePress]
Or tell a view's appropriate parent to takeKeyboardFocus without worrying how many layers away it is
(self parentViewTree query: (Query type: WidgetWrapper))
do: [:each | each takeKeyboardFocus]
Since a ViewTreeSet is actually derived from a collection of objects, we can ask them for a childViewTree or parentViewTree as well, and then we can stack queries
((self parentViewTree query: (Query type: BorderDecorator)) childViewTree
query: (Query type: Scrollbar)) do: [:each | each invalidate]
In that example, we're first querying upwards to the nearest BorderDecorator, and then querying downwards to find all Scrollbars and forcing them to invalidate (redraw).
I found myself struggling to come up with interesting and fun examples, so I apologize if these seem too esoteric. There's a sort of Chicken-and-Egg problem here. Without the utility, you don't think about what to do with it. And then you have it, and then you begin looking around to see if it's just a solution in search of a problem, if there's real value here. I think at this point, I'm not entirely sure yet.
Trying to Sweeten Things Up
jQuery uses CSS for its selectors, which uses lots of infix/punctation characters to mean individual things and keep things pretty tight and terse. We could do something like that in Smalltalk too. But I was interested in seeing how much mileage I could get out of sticking to Smalltalk legal syntax.
This is a tricky proposition I found. Basically, I anticipate that if the code I had to input to use these were terser, I'd be more likely to use them. But without having used them much, I'm left trying to guess and anticipate exactly what the syntactic sugar I might use would look like.
I basically used 3 tricks to try and get things short and sweet (or bitter and sour maybe).
Binary Selectors
I chose to reduce the common childViewTree query: part of my expressions to the single ? character. And to use ?? for the parent counterpart. The examples from above now become
(self ? (Query type: Wrapper)) size
self ? (Query tag: #dialogAnswer) do: [:each | each enable]
self ? (Query id: #okButton) do: [:each | each simulateMousePress]
self ?? (Query type: WidgetWrapper) do: [:each | each takeKeyboardFocus]
self ?? (Query type: BorderDecorator) ? (Query type: Scrollbar)
do: [:each | each invalidate]
I've toyed with implementing ?! and ??! as well which automatically send not to the argument. Note that the do: Blocks could be shortened by taking advantage of the fact that unary selectors can be cull:'ed in place of those blocks, but we're concentrating on the query part here, not what we do with the results.
Overloading Literals
The second technique to shorten and simplify these, was to make assumptions about certain literal Smalltalk expressions. By having an asUIQuery method sent to arguments of these messages, we can coerce certain common Smalltalk objects into their Query counter part objects. I've added it to Symbol, Class, and BlockClosure. This allows us to shorten 4 of the above examples to
(self ? Wrapper) size
self ? #okButton do: [:each | each simulateMousePress]
self ?? WidgetWrapper do: [:each | each takeKeyboardFocus]
self ?? BorderDecorator ? Scrollbar do: [:each | each invalidate]
We can do blocks too
self ? TextEditorView ? [:each | each displayContents isEmpty]
The problem I run into with this part is that I haven't devised a way to be able to use Symbols directly as standins for both Id's and Tags. Since we can commute spec names to id's easily, using Symbols directly as Id matches makes the most sense right now. I've toyed with ideas like letting the first character of the Symbol mean something. And generalizing it to CharacterArray. But I could go farther and make String arguments a whole embedded syntax similar to the CSS style that jQuery uses.
Another area where this only goes so far, is that we can't put together complex queries this way. I'm not sure how often those would exist anyway. One could borrow more from the whole ? theme and add more binary selector sugar: ?& to and the argument with the receiver. But we wouldn't be able to do ?|; Smalltalk doesn't allow | as a binary selector, except for by itself.
We could abuse things like Collections too, so that one could easily assemble or lists, something like
self ? #(#okButton #cancelButton) do: [:each | each flash]
From my little playing around, I like the basic techniques here, but I'm not sure how far it scales. Or how far it needs to scale.
Adding Common Actions
Assuming that 90% of the time, we might use something like this to do some common widget behavior, I've added some of those directly to the ViewTreeSet. So then some of the examples from above can be simplified as
(self ? (Query tag: #dialogAnswer)) enable
(self ?? BorderDecorator ? Scrollbar) invalidate
Beyond Cheesy Examples
I did sit down and try to use this on some actual real existing code in the system. The ClassCreationDialog was a good case. It tries to be relatively interactive, and was written by a programmer whose skills I esteem highly: Vassili Bykov. To accomplish what he wanted with VisualWorks standard approaches (plus some technique he added), he had to not only work thru a builder, but retain a reference to intermediate sub builders, so that he could hunt down and update widgets. And a number of helper methods had to be written. I was able to get rid of an instance variable and a family of helper methods, replaced with one longer method, but more expressive (IMO). Here's a screenshot of some of the differences made:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgSO5tyWEZZ4b1LsF4ejdZgz9nM31RkMZ6cBl5GtaiSiItjSbVhESNsLnf-9xqHrnnLpVpeaNtn80divhc39nmoY2i0JPYwCgZr3Q-SWM0o__gAC57cskctTj6yBUPowFL3NcZMlz3vcEo/s1600/ClassCreationDialogOne.png)
Followed by a screenshot that shows a sample of the kind of change that is made to the longer validateEverything method
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwLbHzQFRrBhdxmAPW7vn_vnBAg2DbhWW7ajd9OJayLmXLwDianc5pZnovPYHYpYeI-BrGyAAfPxU4zrnbKQW6FkCpPmaY3jOkBoDnFXoUZDuKZBiMdwHYTQjElvUD2MNeumL19MaHXp4/s1600/ClassCreationDialogTwo.png)
The code on the right is longer, but it is no longer so long and involved that it needs to use a helper method to do what it does.
Michael Lucas-Smith suggested I take a look at some of the methods he put together for xPressly, which was used to give his Smalltalk Solutions presentations on xTreams. He had some methods that were forced to do lots of container container container types of things. So I modified it to use this query stuff. It improves the container container nonsense, but it brings into that much more relief how silly it is that you have to go running around in other object to essentially manipulate the one you intended. Fixing that is a problem for another day unfortunately.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhSeG3HSq3NfxZEAZdAxA-U2yCgQJLPxPYxMoUXeB4BdhItgh9raLV2s9i4-gmIiXu2CM7lpnpT-UwS7iaiBbOP-QllHla5yTEtj6EIHSX6P693iowHNU1BMppPHHFnhiZRWkg6u8JK0J4/s1600/xPresslyOne.png)
Current Conclusions
I don't have any yet. I need more data. There are parts of this I feel right about, other parts I'm not so sure. I'd love to get some feedback. Does this just all sound like a bad idea? Or maybe good idea in spirit, but misguided in implementation? Generally warm to it, but issues with certain details?
If you have some code in the Open Repository that you'd like to see how this would change, I'd love to take a crack at modifying it as a way of taking this stuff for a spin, if your stuff isn't too involved.
Also, I'm still looking for a good name for this stuff. I've currently called it WidgetPath, which I think is a dumb name.