macOS Form Position Save and Restore

From Free Pascal wiki

English (en)

macOSlogo.png

This article applies to macOS only.

See also: Multiplatform Programming Guide

Overview

Here is a quick, almost code-free method to save the form's position and size using the Mac preferences system.

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.

A short example may be found below. Note that you need to give the form a unique name - here I have used Form_123

Code example

unit Unit1;

{$mode objfpc}{$H+}
{$modeswitch objectivec1}

interface

uses
  Forms,      // needed for main form
  CocoaAll;   // needed for NSView

type

  { TForm_123 }

  TForm_123 = class(TForm)
    procedure FormShow(Sender: TObject);
  private

  public

  end;

var
  Form_123: TForm_123;

implementation

{$R *.lfm}

{ TForm_123 }

procedure TForm_123.FormShow(Sender: TObject);
begin
  NSView(Form_123.Handle).window.setFrameAutosaveName(NSSTR(ClassName));
end;

end.

See also

External links