Sign in Join
Wiki copytemporary

macOS Form Position Save and Restore: what changed

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

Title: macOS Form Position Save and Restore

OldNewLine
1+*(macOS only)*
2+
3+## Overview
4+
5+Here is a quick, almost code-free method to save the form's position and size using the Mac preferences system.
6+
7+The *setFrameAutosaveName* method sets the preferences key name used and loads any previously-saved state. As a result, it is best to call it in a Form's OnShow event handler. There is no need to do anything else and the state of the form's position and size will be saved automatically when the form closes.
8+
9+A short example may be found below. Note that you need to give the form a unique name - here I have used Form_123
10+
11+## Code example
12+
13+``` pascal
14+unit Unit1;
15+
16+{$mode objfpc}{$H+}
17+{$modeswitch objectivec1}
18+
19+interface
20+
21+uses
22+ Forms, // needed for main form
23+ CocoaAll; // needed for NSView
24+
25+type
26+
27+ { TForm_123 }
28+
29+ TForm_123 = class(TForm)
30+ procedure FormShow(Sender: TObject);
31+ private
32+
33+ public
34+
35+ end;
36+
37+var
38+ Form_123: TForm_123;
39+
40+implementation
41+
42+{$R *.lfm}
43+
44+{ TForm_123 }
45+
46+procedure TForm_123.FormShow(Sender: TObject);
47+begin
48+ NSView(Form_123.Handle).window.setFrameAutosaveName(NSSTR(ClassName));
49+end;
50+
51+end.
52+```
53+
54+## See also
55+
56+- [[Mac Preferences Read and Write]]
57+- [[Mac Preferences and About Menu]]
58+- [[macOS Programming Tips]]
59+
60+## External links
61+
62+- [Apple: setFrameAutosaveName](https://developer.apple.com/documentation/appkit/nswindow/1419509-setframeautosavename)
63+
64+---
65+*From the Free Pascal wiki, page "macOS Form Position Save and Restore", as it stood on 2021-12-28 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).*
66+Category: Code Snippets
67+Category: Platform-sensitive development
68+Category: macOS

Back to history