Browse Source

Libraries: move TThrottledEvent to Common.UI

Herman Schoenfeld 7 years ago
parent
commit
de7c8fe0c3
2 changed files with 116 additions and 123 deletions
  1. 115 2
      src/libraries/sphere10/UCommon.UI.pas
  2. 1 121
      src/libraries/sphere10/UCommon.pas

+ 115 - 2
src/libraries/sphere10/UCommon.UI.pas

@@ -53,6 +53,37 @@ type
       property OnDestroyed : TNotifyEvent read FDestroyed write FDestroyed;
   end;
 
+  { TThrottledEvent }
+
+  TThrottledEvent = class(TComponent)
+    public const
+      CT_DEFAULT_DELAYEDREFRESH_MS = 1000;
+    public type
+      TThrottledEventMode = (temNone, temNotifyEveryInterval, temNotifyOnEventBurstFinished, temNotifyOnEventBurstStartAndFinished);
+    private
+      FHandler : TNotifyManyEvent;
+      FTimer: TTimer;
+      FMode : TThrottledEventMode;
+      FInterval : TTimeSpan;
+      FLastClientNotify : TDateTime;
+      FLastActualNotify : TDateTime;
+      FSuppressedInvocations : Integer;
+      procedure SetInterval(const ATimeSpan : TTimeSpan);
+      procedure OnTimer(Sender: TObject);
+      procedure NotifyNow;
+      procedure NotifyLater;
+    public
+      property Interval : TTimeSpan read FInterval write SetInterval;
+      property Mode : TThrottledEventMode read FMode write FMode;
+      property LastClientNotify : TDateTime read FLastClientNotify;
+      property LastActualNotify : TDateTime read FLastActualNotify;
+      property SuppressedInvocations : Integer read FSuppressedInvocations;
+      constructor Create(Owner:TComponent); override;
+      procedure Add(AListener : TNotifyEvent);
+      procedure Remove(AListener : TNotifyEvent);
+      procedure Notify;
+  end;
+
   { TWinControlHelper }
 
   TWinControlHelper = class helper for TWinControl
@@ -71,8 +102,6 @@ type
     procedure SetImageListPicture(AImageList: TImageList; AIndex : SizeInt);
   end;
 
-
-
 implementation
 
 uses LCLIntf;
@@ -138,6 +167,90 @@ end;
 
 {%endregion}
 
+{%region TThrottledEvent }
+
+constructor TThrottledEvent.Create(Owner:TComponent);
+begin
+  Inherited Create(Owner);
+  FTimer := TTimer.Create(Self);
+  FInterval := TTimeSpan.FromMilliseconds( CT_DEFAULT_DELAYEDREFRESH_MS );
+  FTimer.OnTimer := OnTimer;
+  FTimer.Enabled := false;
+  FSuppressedInvocations:=0;
+  FLastClientNotify := MinDateTime;
+  FLastActualNotify := MinDateTime;
+  FMode:=temNone;
+end;
+
+procedure TThrottledEvent.Add(AListener : TNotifyEvent);
+begin
+  FHandler.Add(AListener);
+end;
+
+procedure TThrottledEvent.Remove(AListener : TNotifyEvent);
+begin
+  FHandler.Remove(AListener);
+end;
+
+procedure TThrottledEvent.Notify;
+var LIdleDuration : TTimeSpan;
+begin
+  FLastClientNotify:=Now;
+  LIdleDuration := TTimeSpan.Subtract(Now, FLastActualNotify);
+  if (FMode = temNone) OR ((NOT FTimer.Enabled) AND (LIdleDuration > Interval) AND (FMode <> temNotifyOnEventBurstFinished)) then
+    NotifyNow
+  else
+    NotifyLater;
+end;
+
+procedure TThrottledEvent.NotifyNow;
+begin
+  FTimer.Enabled := false;
+  FLastActualNotify:=Now;
+  FHandler.Invoke(nil);
+  FSuppressedInvocations:=0;
+end;
+
+procedure TThrottledEvent.NotifyLater;
+begin
+  inc(FSuppressedInvocations);
+  if NOT FTimer.Enabled then begin
+    FTimer.Interval := ClipValue( Round( Abs( FInterval.TotalMilliseconds ) ), 10, High(integer)) ;
+    FTimer.Enabled:=true;
+  end;
+end;
+
+procedure TThrottledEvent.OnTimer(Sender: TObject);
+var LDuration : TTimeSpan;
+begin
+  case FMode of
+    temNone: NotifyNow;
+    temNotifyEveryInterval: begin
+      LDuration := TTimeSpan.Subtract(Now, FLastActualNotify);
+      if LDuration > FInterval then
+        NotifyNow
+      else
+        FTimer.Interval := ClipValue( Round( Abs ( (FInterval - LDuration).TotalMilliseconds)), 10, High(integer));
+    end;
+    temNotifyOnEventBurstStartAndFinished, temNotifyOnEventBurstFinished: begin
+      LDuration := TTimeSpan.Subtract(Now, FLastClientNotify);
+      if LDuration > FInterval then
+        NotifyNow
+      else
+        FTimer.Interval := ClipValue( Round( Abs ( (FInterval - LDuration).TotalMilliseconds)), 10, High(integer));
+    end;
+  end;
+end;
+
+procedure TThrottledEvent.SetInterval(const ATimeSpan : TTimeSpan);
+begin
+  if ATimeSpan.TotalMilliseconds = 0 then
+    raise EArgumentOutOfRangeException.Create('ATimeSpan was 0');
+  FInterval := ATimeSpan;
+end;
+
+{%endregion}
+
 {%region TWinControlHelper}
 
 procedure TWinControlHelper.RemoveAllControls(destroy : boolean);

+ 1 - 121
src/libraries/sphere10/UCommon.pas

@@ -22,7 +22,7 @@ interface
 uses
   Classes, SysUtils, Generics.Collections, Generics.Defaults,
   {$IFNDEF FPC}System.Types, System.TimeSpan,{$ENDIF} Variants,
-  {$IFDEF FPC}LazUTF8,{$ENDIF} math, typinfo, UMemory, ExtCtrls;
+  {$IFDEF FPC}LazUTF8,{$ENDIF} math, typinfo, UMemory;
 
 { CONSTANTS }
 
@@ -271,37 +271,6 @@ type
     procedure Invoke(sender : TObject; const args: array of Pointer);
   end;
 
-  { TThrottledEvent }
-
-  TThrottledEvent = class(TComponent)
-    public const
-      CT_DEFAULT_DELAYEDREFRESH_MS = 1000;
-    public type
-      TThrottledEventMode = (temNone, temNotifyEveryInterval, temNotifyOnEventBurstFinished, temNotifyOnEventBurstStartAndFinished);
-    private
-      FHandler : TNotifyManyEvent;
-      FTimer: TTimer;
-      FMode : TThrottledEventMode;
-      FInterval : TTimeSpan;
-      FLastClientNotify : TDateTime;
-      FLastActualNotify : TDateTime;
-      FSuppressedInvocations : Integer;
-      procedure SetInterval(const ATimeSpan : TTimeSpan);
-      procedure OnTimer(Sender: TObject);
-      procedure NotifyNow;
-      procedure NotifyLater;
-    public
-      property Interval : TTimeSpan read FInterval write SetInterval;
-      property Mode : TThrottledEventMode read FMode write FMode;
-      property LastClientNotify : TDateTime read FLastClientNotify;
-      property LastActualNotify : TDateTime read FLastActualNotify;
-      property SuppressedInvocations : Integer read FSuppressedInvocations;
-      constructor Create(Owner:TComponent); override;
-      procedure Add(AListener : TNotifyEvent);
-      procedure Remove(AListener : TNotifyEvent);
-      procedure Notify;
-  end;
-
   { TArrayTool }
 
   TArrayTool<T> = class
@@ -610,7 +579,6 @@ begin
   Result := ZeroValue;
 end;
 
-
 constructor TTimeSpan.Create(Hours, Minutes, Seconds: Integer);
 begin
   Self.FMillis := (Hours*MillisPerHour) + (Minutes*MillisPerMinute) + (Seconds*MillisPerSecond);
@@ -1300,90 +1268,6 @@ end;
 
 {%endregion}
 
-{%region TThrottledEvent }
-
-constructor TThrottledEvent.Create(Owner:TComponent);
-begin
-  Inherited Create(Owner);
-  FTimer := TTimer.Create(Self);
-  FInterval := TTimeSpan.FromMilliseconds( CT_DEFAULT_DELAYEDREFRESH_MS );
-  FTimer.OnTimer := OnTimer;
-  FTimer.Enabled := false;
-  FSuppressedInvocations:=0;
-  FLastClientNotify := MinDateTime;
-  FLastActualNotify := MinDateTime;
-  FMode:=temNone;
-end;
-
-procedure TThrottledEvent.Add(AListener : TNotifyEvent);
-begin
-  FHandler.Add(AListener);
-end;
-
-procedure TThrottledEvent.Remove(AListener : TNotifyEvent);
-begin
-  FHandler.Remove(AListener);
-end;
-
-procedure TThrottledEvent.Notify;
-var LIdleDuration : TTimeSpan;
-begin
-  FLastClientNotify:=Now;
-  LIdleDuration := TTimeSpan.Subtract(Now, FLastActualNotify);
-  if (FMode = temNone) OR ((NOT FTimer.Enabled) AND (LIdleDuration > Interval) AND (FMode <> temNotifyOnEventBurstFinished)) then
-    NotifyNow
-  else
-    NotifyLater;
-end;
-
-procedure TThrottledEvent.NotifyNow;
-begin
-  FTimer.Enabled := false;
-  FLastActualNotify:=Now;
-  FHandler.Invoke(nil);
-  FSuppressedInvocations:=0;
-end;
-
-procedure TThrottledEvent.NotifyLater;
-begin
-  inc(FSuppressedInvocations);
-  if NOT FTimer.Enabled then begin
-    FTimer.Interval := ClipValue( Round( Abs( FInterval.TotalMilliseconds ) ), 10, High(integer)) ;
-    FTimer.Enabled:=true;
-  end;
-end;
-
-procedure TThrottledEvent.OnTimer(Sender: TObject);
-var LDuration : TTimeSpan;
-begin
-  case FMode of
-    temNone: NotifyNow;
-    temNotifyEveryInterval: begin
-      LDuration := TTimeSpan.Subtract(Now, FLastActualNotify);
-      if LDuration > FInterval then
-        NotifyNow
-      else
-        FTimer.Interval := ClipValue( Round( Abs ( (FInterval - LDuration).TotalMilliseconds)), 10, High(integer));
-    end;
-    temNotifyOnEventBurstStartAndFinished, temNotifyOnEventBurstFinished: begin
-      LDuration := TTimeSpan.Subtract(Now, FLastClientNotify);
-      if LDuration > FInterval then
-        NotifyNow
-      else
-        FTimer.Interval := ClipValue( Round( Abs ( (FInterval - LDuration).TotalMilliseconds)), 10, High(integer));
-    end;
-  end;
-end;
-
-procedure TThrottledEvent.SetInterval(const ATimeSpan : TTimeSpan);
-begin
-  if ATimeSpan.TotalMilliseconds = 0 then
-    raise EArgumentOutOfRangeException.Create('ATimeSpan was 0');
-  FInterval := ATimeSpan;
-end;
-
-{%endregion}
-
 {%region TArrayTool}
 
 class function TArrayTool<T>.Empty : TArray<T>;
@@ -1785,7 +1669,6 @@ end;
 
 {%endregion}
 
-
 { TFileStreamHelper }
 {$IFNDEF FPC}
 procedure TFileStreamHelper.WriteAnsiString(const AString : String);
@@ -1794,8 +1677,6 @@ begin
 end;
 {$ENDIF}
 
-
-
 {%region TFileTool }
 
 class procedure TFileTool.AppendText(const AFileName: string; const AText: string);
@@ -1818,7 +1699,6 @@ end;
 
 {%endregion}
 
-
 initialization
   MinTimeStampDateTime:= StrToDateTime('1980-01-01 00:00:000', IntlDateTimeFormat);
   VarTrue := True;