Difference between revisions of "Application disable resize over Dock"

From Lazarus wiki
Jump to navigationJump to search
m (→‎See also: Fix typo)
 
(9 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
{{Platform only|macOS}}
 +
 
On macOS it's often needed to disable app window's resize below OS Dock.
 
On macOS it's often needed to disable app window's resize below OS Dock.
 
But Lazarus forms always resize below it.
 
But Lazarus forms always resize below it.
Line 7: Line 9:
 
Create it by hands:
 
Create it by hands:
  
<syntaxhighlight>
+
<syntaxhighlight lang="pascal">
   TForm1 = class(TForm)
+
   TfmMain = class(TForm)
 
     ...  
 
     ...  
 
   private
 
   private
Line 15: Line 17:
 
       MaxWidth, MaxHeight: TConstraintSize);
 
       MaxWidth, MaxHeight: TConstraintSize);
 
     ...
 
     ...
 
+
  end;
  
 
procedure TfmMain.FormCreate(Sender: TObject);
 
procedure TfmMain.FormCreate(Sender: TObject);
 
begin
 
begin
   Self.OnConstrainedResize:= @FormConstrainedResize; //not published in LCL<1.7
+
  ..
 +
   Self.OnConstrainedResize:= @FormConstrainedResize;  
 +
  ..
 
end;
 
end;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
2) Write code in handler to change MaxHeight.
+
2) Write code in handler to change MaxHeight, value must be workarea height minus current form's Top.
Value must be "max workarea height" minus current form's Top.
 
  
<syntaxhighlight>
+
<syntaxhighlight lang="pascal">
 
procedure TfmMain.FormConstrainedResize(Sender: TObject; var MinWidth, MinHeight,
 
procedure TfmMain.FormConstrainedResize(Sender: TObject; var MinWidth, MinHeight,
 
   MaxWidth, MaxHeight: TConstraintSize);
 
   MaxWidth, MaxHeight: TConstraintSize);
 +
const
 +
  cMinHeight = 200;
 
var
 
var
 
   RWork: TRect;
 
   RWork: TRect;
 
begin
 
begin
 
   {$ifndef darwin} exit; {$endif}
 
   {$ifndef darwin} exit; {$endif}
   RWork:= Screen.Monitors[0].WorkareaRect;
+
   RWork:= Screen.PrimaryMonitor.WorkareaRect;
   MaxHeight:= RWork.Bottom - RWork.Top - Top;
+
   MaxHeight:= Max(cMinHeight, RWork.Bottom - RWork.Top - Top);
 
end;
 
end;
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
3) Note: handler has side effect. On dragging window by its caption, handler is called too, so window will be resized on moving it too low, near the Dock.
 +
 +
== See also ==
 +
 +
* [[Bounce Application Icon in Dock]]
 +
* [[Hiding a macOS app from the Dock]]
 +
* [[macOS Application Dock Menu]]
 +
* [[Show Badge on Application Icon in Dock]]
 +
 +
[[Category:macOS]]
 +
[[Category:Code Snippets]]

Latest revision as of 10:59, 15 December 2021

macOSlogo.png

This article applies to macOS only.

See also: Multiplatform Programming Guide

On macOS it's often needed to disable app window's resize below OS Dock. But Lazarus forms always resize below it. How to disable this resize?

1) Create TForm.OnConstrainedResize handler for your form. Note, it was not "published" in LCL until LCL 1.7. Create it by hands:

  TfmMain = class(TForm)
    ... 
  private
    ...
    procedure FormConstrainedResize(Sender: TObject; var MinWidth, MinHeight,
      MaxWidth, MaxHeight: TConstraintSize);
    ...
  end;

procedure TfmMain.FormCreate(Sender: TObject);
begin
  ..
  Self.OnConstrainedResize:= @FormConstrainedResize; 
  ..
end;

2) Write code in handler to change MaxHeight, value must be workarea height minus current form's Top.

procedure TfmMain.FormConstrainedResize(Sender: TObject; var MinWidth, MinHeight,
  MaxWidth, MaxHeight: TConstraintSize);
const
  cMinHeight = 200;
var
  RWork: TRect;
begin
  {$ifndef darwin} exit; {$endif}
  RWork:= Screen.PrimaryMonitor.WorkareaRect;
  MaxHeight:= Max(cMinHeight, RWork.Bottom - RWork.Top - Top);
end;

3) Note: handler has side effect. On dragging window by its caption, handler is called too, so window will be resized on moving it too low, near the Dock.

See also