Set Minimum and Maximum Sizes for an Application

Minimum Size

Add two new constants to the "constants" file:

constant kMinHeight := 200;
constant kMinWidth := 250;
For the time being, the values will be set so that the application will run in landscape mode, but won't work in portrait mode (that way, we'll be able to test our code). A real application might have much smaller minimum widths and heights.

Add the following code to the end of the viewSetupFormScript of the protoApp:

   local msg := nil;
   if size.bottom < kMinHeight then
      msg := "too short"
   else if size.right < kMinWidth then
      msg := "too narrow";
   if msg then begin
      :Notify(kNotifyAlert, kAppName, msg);
   end;
This code puts up a notification slip as shown in FIGURE 6.7. (The Notify method, which puts up a notification slip, is documented in Newton Programmer's Guide.) However, the application is still open. We need to make sure that the application closes itself automatically.

You can't just close the application from the viewSetupFormScript with:

:Close();

FIGURE 6.7 : Notification slip when view is less than kMinWidth.


The problem with this is that the view system is in the middle of opening your application. Trying to close it during that process annoys the view system. The solution is to close the application after the application has successfully opened by adding a deferred call to close the application. Put a call to AddDeferredCall in the viewSetupFormScript:

if msg then begin
   :Notify(kNotifyAlert, kAppName, msg);
   AddDeferredCall(
      func()
      begin
         :Close();
      end,
      nil);
end;
This code will close the application; an odd visual effect occurs, though. The application opens, a notification slip appears, and then the application closes behind the notification slip. It would be better if the application never appeared. We can do that by adding a viewJustify and viewBounds slot to the protoApp view to make it appear small and offscreen (we'll have to make sure to remove those slots eventually).

Modify the viewSetupFormScript:

:Notify(kNotifyAlert, kAppName, msg);
self.viewBounds := SetBounds(-10, -10, -5, -5);
self.viewJustify := vjParentLeftH + vjParentTopV;
AddDeferredCall(
   func()
   begin
      :Close();
      RemoveSlot(self, 'viewBounds);
      RemoveSlot(self, 'viewJustify);
   end,
   nil);

An online version of Programming for the Newton using Macintosh, 2nd ed. ©1996, 1994, Julie McKeehan and Neil Rhodes.

Last modified: 1 DEC 1996