Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 38 ยท Web and Applications

Morphic and Spec: User Interfaces

Learn the two Pharo GUI layers: Morphic, a direct-manipulation world of live morphs, and Spec, a declarative presenter-based toolkit for applications. Read-only Smalltalk and runnable JavaScript stand-ins.

Pharo's own tools (the browser, the inspector, the debugger) are graphical programs written in Smalltalk, and you can write programs in the same way. Two layers matter. Morphic is the old and very flexible graphics system where everything on screen is a live object called a morph. Spec is the modern, higher-level toolkit for building applications out of presenters, laid out declaratively, which can render through Morphic or another backend.

Neither runs on this site, so the Smalltalk is read-only. The stand-ins use plain JavaScript to model the two ideas: a tree of objects that draw and handle events, and a presenter that holds state and notifies listeners.

Morphic: the world is made of objects

A morph is an object with a bounds, a color and the ability to draw itself and react to the mouse. The screen is a world that contains morphs, which contain submorphs, forming a tree. You can create and manipulate morphs in the playground and see the result immediately (read-only):

SMALLTALK
| box |
box := Morph new
    color: Color orange;
    extent: 100 @ 60;
    position: 200 @ 200.
box openInWorld.

box color: Color blue.         "changes live"
box position: 300 @ 100.
box delete.

The same morph can respond to events by overriding handlesMouseDown: and mouseDown:, or by wrapping it with a button or text morph from the library. Because everything is an object, you can drag a morph, press the halo handles to inspect it, or send it messages from the debugger. This direct-manipulation style is why Morphic is a great teaching and prototyping tool.

Morphic uses a step loop: morphs implement step and a stepTime, and the world calls them regularly, which is how animations work (read-only):

SMALLTALK
Morph subclass: #Blinker
    instanceVariableNames: 'on'
    package: 'Demo-UI'.

Blinker >> step
    on := on not.
    self color: (on ifTrue: [ Color red ] ifFalse: [ Color gray ])

Blinker >> stepTime
    ^ 500    "milliseconds"

A morph tree you can run

This stand-in builds a small tree of "morphs" and does two things real Morphic does: draw (render the tree as indented text) and dispatch a click to the deepest morph at a point.

JS Stand-in: a morph tree with hit testing

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Real Morphic does exactly this: the event goes to the deepest morph that contains the point, which either handles it or lets its owner try.

Spec: presenters and layouts

Writing a full application directly in morphs is tedious. Spec describes an application with presenters (button, text input, list, table, tree), arranged by a layout, and wired by simple blocks. A presenter holds state and can be queried, which makes it easy to test (read-only):

SMALLTALK
SpPresenter subclass: #GreeterPresenter
    instanceVariableNames: 'nameInput greetButton output'
    package: 'Demo-UI'.

GreeterPresenter >> initializePresenters
    nameInput := self newTextInput placeholder: 'Your name'.
    greetButton := self newButton label: 'Greet'.
    output := self newLabel.
    greetButton action: [ output label: 'Hello, ', nameInput text, '!' ]

GreeterPresenter >> defaultLayout
    ^ SpBoxLayout newTopToBottom
        add: nameInput expand: false;
        add: greetButton expand: false;
        add: output;
        yourself

GreeterPresenter >> windowTitle
    ^ 'Greeter'

Open it with GreeterPresenter new open. The pattern separates three concerns: initializePresenters creates parts, defaultLayout positions them, and callbacks (here action:) connect behaviour. Because presenters are ordinary objects, a test can create one, set nameInput text: 'Ana', click the button programmatically and check output label, without any window. This is the same split of model, view and behaviour that announcements support.

A presenter you can test

The stand-in below models the three parts of a Spec presenter in JavaScript and tests it without any drawing.

JS Stand-in: a testable presenter

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Other options

Pharo also has Bloc, a newer scene-graph rendering layer that powers the GT (Glamorous Toolkit) tools, and Roassal for visualizations. Web interfaces use Seaside instead. For learning, start with Morphic in the playground to feel direct manipulation, then build real tools with Spec.

flowchart TB
  APP["Application"] --> SPEC["Spec presenters + layout"]
  SPEC --> MORPH["Morphic morphs<br/>(draw, events, step)"]
  SPEC -. "other backends" .-> OTHER["GTK / Bloc"]
  MORPH --> WORLD["World on screen"]

Spec is the declarative layer; Morphic is one of the engines that draws it.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is a morph?

    Everything on the Morphic screen is a morph object, organized in a tree.
  2. 2. Where does a mouse click go in Morphic?

    The hit test finds the topmost morph at the point.
  3. 3. What are the three jobs in a Spec presenter?

    initializePresenters, defaultLayout and callbacks separate those concerns.
  4. 4. Why are Spec presenters easy to test?

    A test sets inputs, triggers actions and checks labels programmatically.

Summary

  • Morphic is a world of live morph objects with step-based animation and event dispatch to the deepest morph.
  • Spec describes apps declaratively with presenters, layouts and callbacks; it renders through Morphic or another backend.
  • Presenters are objects, so GUI logic can be unit tested without opening windows.
  • The Smalltalk code is read-only; the JavaScript snippets are stand-ins.

Next: Project: Build a Mini SUnit