Thursday, 07 September, 2006

When modal isn't really modal

The Flash application framework, MX, provides a powerful and easy to use platform for creating user interface applications.  In many ways it's similar to the .NET Framework, and similar packages that I've used in the past.  However, there are key differences that will surprise a newcomer.  I ran into one this morning when I wanted to create a modal dialog box that would gather information and return it to the caller.

When you create a modal dialog box in Windows, execution of your main program is halted until the dialog box is closed.  The simplest way to illustrate this is with a message box:

public int SomeFunction()
{
    MessageBox.Show("Hello, world");
    // next statement will not be executed
    // until the messagebox is dismissed.
    return 1;
}

When the message box is displayed, execution is transferred to the code that handles the message box and the user cannot interact with the main program.  The same holds true for modal dialog boxes.  In Windows.

Under Flash, things are a bit different.  As with Windows, a modal dialog box in Flash prevents the user from interacting with the window or form that spawned the dialog box and execution of the main program is suspended until the user dismisses the dialog box.  The difference is when that suspension takes place.  Under Windows, it's immediate.  In Flash, program execution continues until your program returns control to the Flash virtual machine.  Consider this method that is executed when the user presses the "New Game" button:

    private function startNewGame() : void
    {

        var dlg : NewGameDialog;
        dlg = NewGameDialog(PopUpManager.createPopUp(this, NewGameDialog, true));
        // do some stuff
        return 1;
    }

In this function, execution continues in the function after the popup is created.  Flash displays the dialog and disables the caller's user interface until after the dialog box is dismissed.

What does it mean?  A program can't just display a dialog box and in the next line of code get the results from that dialog box.  Instead, the program has to display the dialog box, return, and then wait for a message or event to indicate that the dialog box has been dismissed so that the program can harvest the results.  It's a much different programming model than Windows.

I'm not saying that either way is better than the other--just that Flash is different from other systems and it's going to take some time getting used to the new model.