Hey guys!
I'm trying to implement gestures handling on macOS. It's such an important topic...
I've created a component with purpose to provide a convenient way to handle gestures. Here is a simplified code of it (I've removed FreeNotification, etc. stuff for simplicity):
unit MagnificationGesture;
{$mode objfpc}{$H+}
{$modeswitch objectivec2}
interface
uses
Classes, Controls, CocoaAll;
type
TMagnificationGesture = class;
{ TCocoaMagnificationGesture }
TCocoaMagnificationGesture = objcclass(NSMagnificationGestureRecognizer)
public
FLCLGesture: TMagnificationGesture;
procedure HandleGesture; message 'handleGesture';
end;
{ TMagnificationGesture }
TMagnificationGesture = class(TComponent)
private
FCocoaGesture: TCocoaMagnificationGesture;
FControl: TWinControl;
FOnGesture: TNotifyEvent;
procedure SetControl(AValue: TWinControl);
public
constructor Create(AOwner: TComponent); override;
published
property Control: TWinControl read FControl write SetControl;
property OnGesture: TNotifyEvent read FOnGesture write FOnGesture;
end;
implementation
{ TCocoaMagnificationGesture }
procedure TCocoaMagnificationGesture.HandleGesture;
begin
if Assigned(FLCLGesture.OnGesture) then
FLCLGesture.OnGesture(FLCLGesture);
end;
{ TMagnificationGesture }
procedure TMagnificationGesture.SetControl(AValue: TWinControl);
begin
if FControl = AValue then Exit;
FControl := AValue;
NSView(FControl.Handle).addGestureRecognizer(FCocoaGesture);
end;
constructor TMagnificationGesture.Create(AOwner: TComponent);
begin
inherited;
FCocoaGesture := TCocoaMagnificationGesture
.alloc
.initWithTarget_action(FCocoaGesture, objcselector('handleGesture'))
.autorelease;
FCocoaGesture.FLCLGesture := Self;
end;
end.
Using it:
procedure TForm1.FormCreate(Sender: TObject);
begin
MagnificationGesture := TMagnificationGesture.Create(Self);
MagnificationGesture.Control := Panel1;
MagnificationGesture.OnGesture := @MagnificationGestureGesture;
end;
procedure TForm1.MagnificationGestureGesture(Sender: TObject);
begin
// Just to check if it works
Caption := Caption + '1';
end;
But unfortunately when I do a gesture on a panel the event doesn't fire.
If someone understands how Cocoa works could you give the cue what can be wrong here?