Sign in Join
Wiki copytemporary

Using macOS Sheets: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11*(macOS only)*
22
33A sheet is a modal dialog that’s attached to a particular window—usually a document—and prevents further interaction with the window until the sheet is dismissed. It is the Apple recommended way to request user input before proceeding with a document-specific action, such as attaching files, exporting, saving, and printing. Sheets are also used to present information to the user before the next step, for example, a licensing agreement that requires acceptance.
44
55According to Apple's [Human Interface Guidelines](https://developer.apple.com/design/human-interface-guidelines/macos/windows-and-views/sheets/), you should only use a sheet when a window has a frame. Sheets should always emerge from a window’s frame above the body area. Here is an example:
66
7-<img src="macOSSheetExample.png" title="macOSSheetExample.png" width="440" />
7+![](/wiki-images/6/6b/MacOSSheetExample.png)
88
99The code below creates the sheet as shown in the above example (Tested working on Mojave and Catalina).
1010
1111``` pascal
1212unit unit1;
1313
1414{$modeswitch objectivec1}
1515
1616interface
1717
1818uses
1919 ...
2020 MacOSAll, CocoaAll, LCLType,
2121 ...
2222 ;
2323
2424type
2525
2626 { TForm1 }
2727
2828 TForm1 = class(TForm)
2929 ...
3030
3131 private
3232
3333 public
3434
3535 end;
3636
3737var
3838 Form1: TForm1;
3939
4040implementation
4141
4242{$R *.lfm}
4343
4444{ TForm1 }
4545
4646...
4747
4848procedure ShowAlertSheet(FormHandle: HWND; const TitleStr, MessageStr: string);
4949var
5050 tNSStr, mNSStr, okNSStr, fNSStr: NSString;
5151 theWindow : NSWindow;
5252 theID : id;
5353begin
5454 theID := NSView(FormHandle).window;
5555 theWindow := NSView(FormHandle).window;
5656
5757 tNSStr := NSString(CFStringCreateWithPascalString(kCFAllocatorDefault, TitleStr, kCFStringEncodingUTF8)); // title
5858 mNSStr := NSString(CFStringCreateWithPascalString(kCFAllocatorDefault, MessageStr, kCFStringEncodingUTF8));// message
5959 okNSStr:= NSString(CFStringCreateWithPascalString(kCFAllocatorDefault, 'OK', kCFStringEncodingUTF8)); // button caption
6060 fNSStr := NSString(CFStringCreateWithPascalString(kCFAllocatorDefault, '%@', kCFStringEncodingUTF8)); // format
6161
6262 NSBeginAlertSheet(tNSStr,okNSStr,nil,nil,theWindow,theID,nil,nil,nil,fNSStr, mNSStr);
6363end;
6464
6565...
6666
6767end.
6868```
6969
7070You can replace *NSBeginAlertSheet()* for *NSInformationalAlertSheet()* or *NSBeginCriticalAlertSheet()* which have different visual attributes. In particular, when using *NSBeginCriticalAlertSheet()*, the sheet presented to the user is badged with a caution icon. Critical alerts should be used only as specified in the "Alerts" section of the UI Element Guidelines Windows chapter of [macOS Human Interface Guidelines](https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/OSXHIGuidelines/index.html).
7171
7272To call the **ShowAlertSheet** procedure from, for example, a menu item:
7373
7474``` pascal
7575procedure TForm1.MenuItem3Click(Sender: TObject);
7676begin
7777 ShowAlertSheet(Form1.Handle, 'Test', 'Message text');
7878end;
7979```
8080
8181## See also
8282
8383- [[macOS Big Sur changes for developers|macOS Big Sur changes for developers - Sheets]]
8484
8585---
8686*From the Free Pascal wiki, page "Using macOS Sheets", as it stood on 2020-11-29 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history