mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-08-17 19:19:19 +02:00
Cocoa: cleans up some sloppiness with the way LCLCOCOA creates/interacts with NSTimer...
This commit is contained in:
parent
99d60446b7
commit
a3a16bf5b3
@ -42,8 +42,10 @@ type
|
|||||||
|
|
||||||
TCocoaTimerObject = objcclass(NSObject)
|
TCocoaTimerObject = objcclass(NSObject)
|
||||||
func: TWSTimerProc;
|
func: TWSTimerProc;
|
||||||
procedure timerEvent; message 'timerEvent';
|
timer: NSTimer;
|
||||||
class function newWithFunc(afunc: TWSTimerProc): TCocoaTimerObject; message 'newWithFunc:';
|
function initWithInterval_func(interval: integer; timerFunc: TWSTimerProc): id; message 'initWithInterval:func:';
|
||||||
|
procedure invalidate; message 'invalidate';
|
||||||
|
procedure timerFireMethod(atimer: NSTimer); message 'timerFireMethod:';
|
||||||
end;
|
end;
|
||||||
|
|
||||||
{ TAppDelegate }
|
{ TAppDelegate }
|
||||||
|
@ -364,28 +364,11 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
function TCocoaWidgetSet.CreateTimer(Interval: integer; TimerFunc: TWSTimerProc): TLCLHandle;
|
function TCocoaWidgetSet.CreateTimer(Interval: integer; TimerFunc: TWSTimerProc): TLCLHandle;
|
||||||
var
|
|
||||||
timer : NSTimer;
|
|
||||||
user : TCocoaTimerObject;
|
|
||||||
begin
|
begin
|
||||||
{$IFDEF VerboseObject}
|
{$IFDEF VerboseObject}
|
||||||
DebugLn('TCocoaWidgetSet.CreateTimer');
|
DebugLn('TCocoaWidgetSet.CreateTimer');
|
||||||
{$ENDIF}
|
{$ENDIF}
|
||||||
user:=TCocoaTimerObject.newWithFunc(TimerFunc);
|
Result:=TLCLHandle(TCocoaTimerObject.alloc.initWithInterval_func(Interval, TimerFunc));
|
||||||
|
|
||||||
timer:=NSTimer.timerWithTimeInterval_target_selector_userInfo_repeats(
|
|
||||||
Interval/1000, user, objcselector(user.timerEvent), user, True);
|
|
||||||
|
|
||||||
// adding timer to all "common" loop mode.
|
|
||||||
NSRunLoop.currentRunLoop.addTimer_forMode(timer, NSDefaultRunLoopMode);
|
|
||||||
NSRunLoop.currentRunLoop.addTimer_forMode(timer, NSModalPanelRunLoopMode);
|
|
||||||
NSRunLoop.currentRunLoop.addTimer_forMode(timer, NSEventTrackingRunLoopMode);
|
|
||||||
|
|
||||||
{user is retained (twice, because it's target), by the timer and }
|
|
||||||
{released (twice) on timer invalidation}
|
|
||||||
user.release;
|
|
||||||
|
|
||||||
Result:=TLCLHandle(timer);
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TCocoaWidgetSet.DestroyTimer(TimerHandle: TLCLHandle): boolean;
|
function TCocoaWidgetSet.DestroyTimer(TimerHandle: TLCLHandle): boolean;
|
||||||
@ -397,12 +380,13 @@ begin
|
|||||||
{$ENDIF}
|
{$ENDIF}
|
||||||
obj:=NSObject(TimerHandle);
|
obj:=NSObject(TimerHandle);
|
||||||
try
|
try
|
||||||
Result:= Assigned(obj) and obj.isKindOfClass_(NSTimer);
|
Result:= Assigned(obj) and obj.isKindOfClass_(TCocoaTimerObject);
|
||||||
except
|
except
|
||||||
Result:=false;
|
Result:=false;
|
||||||
end;
|
end;
|
||||||
if not Result then Exit;
|
if not Result then Exit;
|
||||||
NSTimer(obj).invalidate;
|
TCocoaTimerObject(obj).invalidate;
|
||||||
|
obj.release;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TCocoaWidgetSet.InitStockItems;
|
procedure TCocoaWidgetSet.InitStockItems;
|
||||||
@ -586,15 +570,36 @@ end;
|
|||||||
|
|
||||||
{ TCocoaTimerObject }
|
{ TCocoaTimerObject }
|
||||||
|
|
||||||
procedure TCocoaTimerObject.timerEvent;
|
function TCocoaTimerObject.initWithInterval_func(interval: integer;
|
||||||
|
timerFunc: TWSTimerProc): id;
|
||||||
begin
|
begin
|
||||||
if Assigned(@func) then func;
|
Self:=TCocoaTimerObject(inherited init);
|
||||||
|
Result:=Self;
|
||||||
|
if not Assigned(Result) then Exit;
|
||||||
|
func:=timerFunc;
|
||||||
|
// timer maintains a strong reference to Self until it's invalidate is called
|
||||||
|
timer:=NSTimer.timerWithTimeInterval_target_selector_userInfo_repeats(
|
||||||
|
interval/1000, Self, objcselector(timerFireMethod), nil, True);
|
||||||
|
if timer = nil then Exit;
|
||||||
|
timer.retain;
|
||||||
|
// adding timer to all "common" loop mode.
|
||||||
|
NSRunLoop.currentRunLoop.addTimer_forMode(timer, NSDefaultRunLoopMode);
|
||||||
|
NSRunLoop.currentRunLoop.addTimer_forMode(timer, NSModalPanelRunLoopMode);
|
||||||
|
NSRunLoop.currentRunLoop.addTimer_forMode(timer, NSEventTrackingRunLoopMode);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
class function TCocoaTimerObject.newWithFunc(afunc: TWSTimerProc): TCocoaTimerObject;
|
procedure TCocoaTimerObject.invalidate;
|
||||||
begin
|
begin
|
||||||
Result:=alloc;
|
if timer=nil then Exit;
|
||||||
Result.func:=afunc;
|
func:=nil;
|
||||||
|
timer.invalidate;
|
||||||
|
timer.release;
|
||||||
|
timer:=nil;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TCocoaTimerObject.timerFireMethod(atimer: NSTimer);
|
||||||
|
begin
|
||||||
|
if Assigned(func) then func;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TAppDelegate.application_openFiles(sender: NSApplication; filenames: NSArray);
|
procedure TAppDelegate.application_openFiles(sender: NSApplication; filenames: NSArray);
|
||||||
|
Loading…
Reference in New Issue
Block a user