Introduction to platform-sensitive development
│
English (en) │
Platform-sensitive development is the next step in cross-platform development. It extends the process of porting an application by implementing platform-specific functions and user-interface style guides.
Thanks to the "Write Once - Compile Anywhere" principle of Lazarus and Free Pascal it is very easy to develop a program that runs out of the box on multiple platforms. To make a finished application that adheres to the respective GUI style guidelines (and even the requirements of app stores) warrants additional steps, however.
Platform-sensitive development is more than adherence to user-interface style guides. It also addresses aspects that are as different as memory management, file handling and interprocess communication.
This article provides the reader with essential information and hints for platform-sensitive development.
Prelude
This article applies to platform-sensitive development only.
See also: Multiplatform Programming Guide
Most adaptations for specific target platforms are performed automatically by the LCL, FCL, RTL and the respective widgetsets. Often, only minor modifications are needed to create a finished application. Using conditional compilation by means of compiler directives helps to keep your code portable.
The following example demonstrates this technique. The procedure AdaptMenus
adjusts the display of the menu bar according to the target platform. This is necessary as the positions of the "About" and "Preferences" items are in different locations in macOS than they are in Windows, Linux or FreeBSD. Additionally, the menu shortcuts are invoked by the command key on macOS (in the LCL referred to as Meta key), while they are triggered with the control key on most other operating systems.
The main menu of the form contains some platform-specific items, WinAboutItem
(it opens an info box) and WinPreferencesItem
(for program settings). They are intended for use on non-macOS operating systems. On the other side, it contains an Apple Menu for macOS. Depending from the target platform, the code hides unneeded entries (and corresponding dividers) and shows those that are expected by the users of the respective platform.
uses
LCLType;
procedure AdaptMenus;
{ Adapts Menus and Shortcuts to the interface style guidelines
of the respective operating system }
var
modifierKey: TShiftState;
begin
{$IFDEF Darwin}
MainForm.WinAboutItem.Visible := False;
MainForm.Divider_5_1.Visible := False;
MainForm.Divider_2_2.Visible := False;
MainForm.WinPreferencesItem.Visible := False;
MainForm.AppleMenu.Visible := True;
{$ELSE}
MainForm.WinAboutItem.Visible := True;
MainForm.Divider_5_1.Visible := True;
MainForm.Divider_2_2.Visible := True;
MainForm.WinPreferencesItem.Visible := True;
MainForm.AppleMenu.Visible := False;
{$ENDIF}
MainForm.NewMenuItem.ShortCut := ShortCut(VK_N, ssModifier); //ssModifier equals ssMeta on Mac, ssCtrl on other platforms
MainForm.OpenMenuItem.ShortCut := ShortCut(VK_O, ssModifier);
MainForm.CloseMenuItem.ShortCut := ShortCut(VK_W, ssModifier);
MainForm.SaveItem.ShortCut := ShortCut(VK_S, ssModifier);
MainForm.PrintItem.ShortCut := ShortCut(VK_P, ssModifier);
MainForm.QuitMenuItem.ShortCut := ShortCut(VK_Q, ssModifier);
MainForm.UndoMenuItem.ShortCut := ShortCut(VK_Z, ssModifier);
MainForm.CutMenuItem.ShortCut := ShortCut(VK_X, ssModifier);
MainForm.CopyMenuItem.ShortCut := ShortCut(VK_C, ssModifier);
MainForm.PasteMenuItem.ShortCut := ShortCut(VK_V, ssModifier);
MainForm.SelectAllMenuItem.ShortCut := ShortCut(VK_A, ssModifier);
end;
In older Lazarus versions (before 1.0) ssModifier is not supported. The following code is compatible with Lazarus 0.9.x, albeit more complex than the example above:
uses
LCLType;
procedure AdaptMenus;
{ Adapts Menus and Shortcuts to the interface style guidelines
of the respective operating system }
var
modifierKey: TShiftState;
begin
{$IFDEF LCLcarbon}
modifierKey := [ssMeta];
MainForm.WinAboutItem.Visible := False;
MainForm.Divider_5_1.Visible := False;
MainForm.Divider_2_2.Visible := False;
MainForm.WinPreferencesItem.Visible := False;
MainForm.AppleMenu.Visible := True;
{$ELSE}
modifierKey := [ssCtrl];
MainForm.WinAboutItem.Visible := True;
MainForm.Divider_5_1.Visible := True;
MainForm.Divider_2_2.Visible := True;
MainForm.WinPreferencesItem.Visible := True;
MainForm.AppleMenu.Visible := False;
{$ENDIF}
MainForm.NewMenuItem.ShortCut := ShortCut(VK_N, modifierKey);
MainForm.OpenMenuItem.ShortCut := ShortCut(VK_O, modifierKey);
MainForm.CloseMenuItem.ShortCut := ShortCut(VK_W, modifierKey);
MainForm.SaveItem.ShortCut := ShortCut(VK_S, modifierKey);
MainForm.PrintItem.ShortCut := ShortCut(VK_P, modifierKey);
MainForm.QuitMenuItem.ShortCut := ShortCut(VK_Q, modifierKey);
MainForm.UndoMenuItem.ShortCut := ShortCut(VK_Z, modifierKey);
MainForm.CutMenuItem.ShortCut := ShortCut(VK_X, modifierKey);
MainForm.CopyMenuItem.ShortCut := ShortCut(VK_C, modifierKey);
MainForm.PasteMenuItem.ShortCut := ShortCut(VK_V, modifierKey);
MainForm.SelectAllMenuItem.ShortCut := ShortCut(VK_A, modifierKey);
end;
Platform-specific hints
Android
Android is an atypical Linux variant, therefore many considerations for usual Linux distributions do not apply. Furthermore, development for Android is faced by the challenge that the software landscape is fragmented in a large field of different system versions and by a considerable heterogeneity of hardware among supported devices.
See also
External Links
FreeBSD
FreeBSD is an operating system for a variety of platforms which focuses on features, speed, and stability. It is derived from BSD, the version of UNIX® developed at the University of California, Berkeley. It is developed and maintained by a large community. It is used to power modern servers, desktops, and embedded platforms. A large community has continually developed it for more than thirty years. Its advanced networking, security, and storage features have made FreeBSD the platform of choice for many of the busiest web sites and most pervasive embedded networking and storage devices.
See also
- FreeBSD Portal for comprehensive lists of links for development techniques, programming hints and tips for creating FreeBSD programs.
External links
iOS
iOS is a pruned variant of macOS for mobile devices. Recent additions to the Lazarus evironment help to develop applications with Free Pascal for iOS now. The development for iOS differs in some sense from that for other platforms, however thanks to the iOS Designer it is possible to graphically design the GUI. For submission to Apple's App Store it is mandatory to implement the iOS Human Interface Guidelines.
See also
External Links
- Apple iOS Human Interface Guidelines
- Apple Style Guide
- App Distribution Guide]
- High Resolution Guidelines for macOS
Linux
Linux is less standardized than other operating systems. This is a consequence of both heterogeneity of distributions and the fact that there is no "standard" desktop environment for Linux. GNOME, KDE, LXDE and other desktop environments differ in their recommendations for GUI design.
See also
External links
- GNOME human interface guidelines
- KDE human interface guidelines
- Canonical Ubuntu App design guides
- LXDE Design principles for software
macOS
Among all operating systems the GUI of the Mac operating system relies on the richest tradition. It dates back to classical Mac OS from 1984 and its less successful predecessors Lisa OS (1983), Xerox Star (1981) and Xerox Alto (1973). Many regard the Mac operating system GUI as delivering the best user experience of all platforms. This is balanced by a more complex development process, however.
Compared with other operating systems many issues have been differently implemented on Mac computers. This covers design and location of the menu bar, the position of some standard menus and the style of buttons and icons. Also Mac computer non-GUI features differ, e.g. the architecture of programs, the location of preferences files and interprocess communication.
Many of these issues are automatically taken care of by the Carbon (32 bit only and now eliminated by Apple in the macOS 10.15 Catalina release in late 2019) and Cocoa (64 bit only) widgetsets, so that it is easy to develop a simple program for macOS, often without any change to the source code. Some fine-tuning is still necessary to develop a finished Mac application, however. In particular, see Apple-specific UI elements.
See also
- Mac Portal for comprehensive lists of links for development techniques, programming hints and tips for creating macOS applications.
External Links
- macOS Human Interface Guidelines
- Apple Style Guide
- App Distribution Guide]
- High Resolution Guidelines for macOS
Windows
Windows is one of the oldest platforms supported by Lazarus. Therefore, most applications generated by the IDE are automatically in good agreement with the requirements of usability engineering. However, the spectrum of Windows implementations is very wide today, covering mobile operating systems like Windows CE or Windows mobile, desktop environments like Windows 2000, Windows 7 and Windows 10, plus server platforms. Therefore, it may be advisable to fine-tune applications for special environments and their GUI guidelines.