UserGuide

Mac UI Tips

From Xojo Documentation

Cocoa is the current user interface framework supported by Macs. If you are moving an older project from Carbon (an older Mac UI framework) to Xojo, your existing applications should just work for the most part, but there are some differences to consider.

General UI Differences

  • Resizable windows can be resized from any window edge.
  • Spellchecking and grammar checking are now available in TextField and TextArea controls by setting AutomaticallyCheckSpelling property to ON in the Inspector.
  • PushButtons have a ButtonStyle property than can be used to change the look of the button.
  • Windows in your apps can be dragged around the screen while code is running.
  • Timers, threads and sockets continue to get events when menus or OS modal dialog boxes are displayed.
  • StyledText draws much faster.

Graphics

All drawing using a Canvas must be done from the Paint event or a method called from it. If you have older projects that use the Graphics property of a Canvas directly, you will have to update them to use the Graphics object from the Canvas.Paint event instead.

You should also use Canvas.Invalidate to refresh the Canvas display instead of Canvas.Refresh. The Refresh method tells the Canvas to redraw itself immediately, which can slow things down by redrawing too often. Invalidate tells the OS to redraw the Canvas when it is ready, which is more efficient.

All drawing now takes place in a ‘generic’ colorspace that is independent of the current output device. This means that colors will render more similarly between screens and printers.

Threads

You cannot have any code that is in a thread that modifies anything related to the user interface (which runs on what is called the main thread). If your app does this it will raise a ThreadAccessingUIException exception. If your user interface needs to be updated based on information in a thread, you should instead have the UI request the information from the thread.

One way to do this is to use a Timer that periodically updates the UI based on information that it gets from the thread. Or you could make use of the Task class that provides methods and events for this purpose.

MenuItems

Mac apps do not allow the same MenuItem to be used in two different places. To work around this, use the MenuItem.Clone method to create a copy of the MenuItem instead.

Font Display

Mac apps can only display font styles that are available. You cannot force a font to display in bold or italic if it does not have bold or italic variations available. In this situation, the Bold or Italic properties will not affect the font.

To check if the font you wish to use has the variation you need, use the system Font Book app in the Applications folder.

Keyboard Handling

Unhandled KeyDown event handlers cause a Beep instead of behaving silently. This is intentional and gives the user feedback that their action did not do anything. To eliminate the Beep, Return True from the KeyDown event handler.

Additionally, the KeyDown event handler literally means “a key was pressed” and does not attempt to interpret the key event for dead key sequences or input methods. For example, pressing Option+E and then E again results in two key presses sent to KeyDown, not a single key press with the value “é”.

Unsupported Items

These older Mac features are no longer supported: