GT Inspector views for files

Glamorous Toolkit contains thousands of tiny contextual tools to help make the system itself more explainable. Many of these tools consists of reusable extensions that work across multiple contexts. A good example of reusable extensions within GT is the collection of contextual views that have been introduced for inspecting files . Consider that by default, an object inspector will only display this basic, “raw” view of a file:

This generic, raw view just shows the values of the instance variables of an object, and provides no particular insight into the nature of a file.

Instead, if we inspect this same file within GT, we obtain a much richer collection of views:

The Raw view is still there, but it appears as a tab further to the right. The first view we see is a nicely formatted view of the JSON code in the file. Obviously not all files are JSON files, so this view is designed to only appear if the file object recognizes that it contains JSON (i.e., from the .json file extension). How does this work? It's just an extension method consisting of a few lines of code: AbstractFileReference>>#gtJsonFor: gtJsonFor: aView <gtView> (self isFile and: [ (GtFileUtility mimeTypeOfExtension: self extension) = ZnMimeType applicationJson ]) ifFalse: [ ^ aView empty ]. ^ aView textEditor title: 'JSON'; priority: 1; actionButtonIcon: BrGlamorousVectorIcons tree tooltip: 'Inspect parsed JSON' action: [ :aButton | aButton phlow spawnObject: ( STONJSON fromString: self contents) ]; aptitude: [ BrGlamorousCodeEditorAptitude ]; styler: [ JSONParser gtStyler ]; text: [ (STONJSON toStringPretty: ( STONJSON fromString: self contents)) ]

Many contextual tools are just short methods annotated with a “pragma”, in this case it is <gtView>, appearing in the first line of the gtJsonFor: method. Don't pay too much attention to the details of the method for now, but notice that, right after the pragma, there is a guard condition that activates the view only if the file has the right JSON extension. In the following lines we give the view a title , “JSON”, we add a “tree” button to the right, with a tootip, and we display the text of the file by parsing and oretty-printing the contents of the file. That's pretty much it! Most (not all) contextual tools are tiny, like this, which means it can be very fast to create them and modify them.

Another example is the Store on view, which generates a snippet to open that file. We can see the source code of any view by Primary -clicking on the tab:

Note that the view code is very similar to the previous example.

With an image file, on the other hand, the Picture view is automatically enabled if the file name is recognized as having a typical image file extension. Note that the view code is similarly compact.

Have a look at the code behind other inspector views by Primary -clicking on their tabs.

To learn more about how to create such views, read moldable development patterns, Contextual View and Simple View.