UPD: optimize trackpad wheel scrolling #2191

(cherry picked from commit 87a9ba1190)
This commit is contained in:
rich2014 2025-03-05 23:26:32 +08:00 committed by Alexander Koblov
commit d346429a1f
3 changed files with 77 additions and 3 deletions

View file

@ -18,6 +18,7 @@ uses
DCXmlConfig,
DCBasicTypes,
uTypes,
uSmoothScrollingGrid,
uFileViewWithGrid;
type
@ -28,7 +29,7 @@ type
{ TDrawGridEx }
TDrawGridEx = class(TDrawGrid)
TDrawGridEx = class(TSmoothScrollingGrid)
private
FMouseDownY: Integer;
FLastMouseMoveTime: QWord;

View file

@ -7,7 +7,8 @@ interface
uses
Classes, SysUtils, Controls, Grids, Graphics, StdCtrls, LCLVersion,
uDisplayFile, DCXmlConfig, uFileSorting, uFileProperty,
uFileViewWithMainCtrl, uFile, uFileViewHeader, uFileView, uFileSource;
uFileViewWithMainCtrl, uFile, uFileViewHeader, uFileView, uFileSource,
uSmoothScrollingGrid;
type
@ -15,7 +16,7 @@ type
{ TFileViewGrid }
TFileViewGrid = class(TDrawGrid)
TFileViewGrid = class(TSmoothScrollingGrid)
protected
FLastMouseMoveTime: QWord;
FLastMouseScrollTime: QWord;

View file

@ -0,0 +1,72 @@
unit uSmoothScrollingGrid;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils,
Grids;
type
{ TSmoothScrollingGrid }
TSmoothScrollingGrid = class( TDrawGrid )
private
_wheelDeltaAccumulator: Array [Boolean] of Integer;
private
function calcCurrentDelta( isVert: Boolean; WheelDelta: Integer ): Integer;
protected
function DoMouseWheel(Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint): Boolean; override;
function DoMouseWheelHorz(Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint): Boolean; override;
end;
implementation
const
WHEEL_DELTA_UNIT = 120;
{ TSmoothScrollingGrid }
function TSmoothScrollingGrid.calcCurrentDelta(isVert: Boolean; WheelDelta: Integer): Integer;
begin
if ((WheelDelta>0) and (_wheelDeltaAccumulator[isVert]<0)) or
((WheelDelta<0) and (_wheelDeltaAccumulator[isVert]>0)) then
_wheelDeltaAccumulator[isVert]:= 0;
inc( _wheelDeltaAccumulator[isVert], WheelDelta );
Result:= 0;
if _wheelDeltaAccumulator[isVert] >= WHEEL_DELTA_UNIT then
Result:= WHEEL_DELTA_UNIT
else if _wheelDeltaAccumulator[isVert] <= -WHEEL_DELTA_UNIT then
Result:= -WHEEL_DELTA_UNIT;
if Result <> 0 then
dec( _wheelDeltaAccumulator[isVert], Result );
end;
function TSmoothScrollingGrid.DoMouseWheel(Shift: TShiftState;
WheelDelta: Integer; MousePos: TPoint): Boolean;
var
currentDelta: Integer;
begin
Result:= True;
currentDelta:= calcCurrentDelta( True, WheelDelta );
if currentDelta <> 0 then
Result:= inherited DoMouseWheel(Shift, currentDelta, MousePos);
end;
function TSmoothScrollingGrid.DoMouseWheelHorz(Shift: TShiftState;
WheelDelta: Integer; MousePos: TPoint): Boolean;
var
currentDelta: Integer;
begin
Result:= True;
currentDelta:= calcCurrentDelta( False, WheelDelta );
if currentDelta <> 0 then
Result:= inherited DoMouseWheelHorz(Shift, currentDelta, MousePos);
end;
end.