atheorist (atheorist) wrote,

The relationship between graphical user interfaces and interfaces to libraries

A click on a button is analogous to a function or method call with void arguments.

Filling out a small form (comprising selecting from a dropdown, typing into a text box, checking a checkbox) and then clicking on a button like 'submit' is analogous to a function or method call with a few (enum, string, bool) arguments.

Dragging one (subpart of) an object onto another (subpart of) an object can be analogous to a binary function where both arguments are domain objects, or a method on a domain object with one argument which is a domain object.

In graphical user interfaces, there are lot of different ways to represent what the user is currently attending to. The tab key usually steps a keyboard focus through a form - this is state, but it's not state of the application, so much as state of the user's interaction with the application. The other sort of tabs (such as browsers universally now have) store state, at least which tab is currently active, and for browser tabs, the set of tabs and where they are pointing is also part of the state. Windows represent user attention even more richly.

If you have a cursor, such as the keyboard focus that the tab key increments, or the 'currently selected object' in many drawing programs, then many functions can take "the thing pointed to by such and such (global) cursor" as an implicit additional argument.

So even though 'cast fireball at mob' is conceptually a one-argument function, in combination with a 'currently selected mob' cursor and appropriate mutations such as 'select the mob in front of me', 'cycle the currently selected mob to the left', 'cycle the currently selected mob to the right', it can become a zero argument function.

In porting a GUI to an API, you want to find the cursors and modes and similar 'state of the interaction' concepts in the GUI, and replace them with concepts more convenient in programming languages, such as variables.

In HotDraw, a prototypical drawing application, there are two primary "cursors" - the currently selected tool, and the currently selection, which might be one or more objects. You might click the rectangle tool, then click and drag between two locations on the canvas to create a rectangle.

The first action, moving the currently selected tool cursor to the rectangle tool, we don't directly model in the API. The second action, clicking and dragging in the canvas, is reasonably modeled by a an argument taking four integers.

  rectangle_tool.create(x1, y1, x2, y2)

You might also click the rectangle, then press delete. If we replace the current selected object with a variable, then we could do.

  r = rectangle_tool.create(x1, y1, x2, y2)
  • Post a new comment


    default userpic

    Your IP address will be recorded