LCL/ShellCtrls: Optimize the new UpdateView method of the tree and listview.

This commit is contained in:
wp_xyz 2023-11-12 16:48:29 +01:00
parent 9b23abbad1
commit 01d11f00ee

View File

@ -85,6 +85,7 @@ type
FExpandCollapseMode: TExpandCollapseMode; FExpandCollapseMode: TExpandCollapseMode;
FFileSortType: TFileSortType; FFileSortType: TFileSortType;
FInitialRoot: String; FInitialRoot: String;
FUpdateLock: Integer;
FUseBuiltinIcons: Boolean; FUseBuiltinIcons: Boolean;
FOnAddItem: TAddItemEvent; FOnAddItem: TAddItemEvent;
FOnSortCompare: TFileItemCompareEvent; FOnSortCompare: TFileItemCompareEvent;
@ -126,7 +127,7 @@ type
function GetPathFromNode(ANode: TTreeNode): string; function GetPathFromNode(ANode: TTreeNode): string;
procedure PopulateWithBaseFiles; procedure PopulateWithBaseFiles;
procedure Refresh(ANode: TTreeNode); overload; procedure Refresh(ANode: TTreeNode); overload;
procedure UpdateView; procedure UpdateView(AStartDir: String = '');
property UseBuiltinIcons: Boolean read FUseBuiltinIcons write SetUseBuiltinIcons default true; property UseBuiltinIcons: Boolean read FUseBuiltinIcons write SetUseBuiltinIcons default true;
{ Properties } { Properties }
@ -1235,7 +1236,10 @@ begin
end; end;
end; end;
procedure TCustomShellTreeView.UpdateView; { Rebuilds the tree for all expanded nodes from the node corresponding to
AStartDir (or from root if AStartDir is empty) to react on changes in the
file system. Collapsed nodes will be updated anyway when they are expanded. }
procedure TCustomShellTreeView.UpdateView(AStartDir: String = '');
procedure RecordNodeState(const ANode: TTreeNode; const AExpandedPaths: TStringList); procedure RecordNodeState(const ANode: TTreeNode; const AExpandedPaths: TStringList);
var var
@ -1282,11 +1286,15 @@ procedure TCustomShellTreeView.UpdateView;
var var
node: TTreeNode; node: TTreeNode;
firstNode: TTreeNode;
topNodePath: String; topNodePath: String;
selectedPath: String; selectedPath: String;
selectedWasExpanded: Boolean = false; selectedWasExpanded: Boolean = false;
expandedPaths: TStringList; expandedPaths: TStringList;
begin begin
if FUpdateLock <> 0 then
exit;
expandedPaths := TStringList.Create; expandedPaths := TStringList.Create;
Items.BeginUpdate; Items.BeginUpdate;
try try
@ -1295,7 +1303,17 @@ begin
if Assigned(Selected) then if Assigned(Selected) then
selectedWasExpanded := Selected.Expanded; selectedWasExpanded := Selected.Expanded;
node := Items.GetFirstNode; firstNode := Items.GetFirstNode;
if AStartDir = '' then
node := firstNode
else
begin
node := Items.FindNodeWithTextPath(ChompPathDelim(AStartDir));
// Avoid starting at a non-existing folder
while not Exists(GetPathFromNode(node)) and (node <> firstNode) do
node := node.Parent;
end;
RecordNodeState(node, expandedPaths); RecordNodeState(node, expandedPaths);
RestoreNodeState(node, true, expandedPaths); RestoreNodeState(node, true, expandedPaths);
@ -1310,7 +1328,14 @@ begin
// Force synchronization of associated ShellListView // Force synchronization of associated ShellListView
if Assigned(FShellListView) then if Assigned(FShellListView) then
FShellListView.UpdateView; begin
inc(FUpdateLock);
try
FShellListView.UpdateView;
finally
dec(FUpdateLock);
end;
end;
finally finally
Items.EndUpdate; Items.EndUpdate;
expandedPaths.Free; expandedPaths.Free;
@ -1892,6 +1917,7 @@ begin
Result := IncludeTrailingPathDelimiter(FRoot) + ANode.Caption; Result := IncludeTrailingPathDelimiter(FRoot) + ANode.Caption;
end; end;
{ Re-reads the list to react on changes in the file system. }
procedure TCustomShellListView.UpdateView; procedure TCustomShellListView.UpdateView;
var var
selectedItem: String = ''; selectedItem: String = '';
@ -1905,11 +1931,14 @@ begin
if selectedItem <> '' then if selectedItem <> '' then
Selected := FindCaption(0, selectedItem, false, true, false); Selected := FindCaption(0, selectedItem, false, true, false);
inc(FLockUpdate); if Assigned(ShellTreeView) then
try begin
ShellTreeView.UpdateView; inc(FLockUpdate);
finally try
dec(FLockUpdate); ShellTreeView.UpdateView(FRoot);
finally
dec(FLockUpdate);
end;
end; end;
end; end;
end; end;