From e6562d82ea51aa134f3c422d9448ccd56ce8a8c4 Mon Sep 17 00:00:00 2001 From: rich2014 Date: Tue, 16 Jul 2024 21:46:13 +0800 Subject: [PATCH] Cocoa/ListView: enhanced robustness in special situations --- lcl/interfaces/cocoa/cocoacollectionview.pas | 18 +++++++--- lcl/interfaces/cocoa/cocoawscomctrls.pas | 37 +++++++++++++++++++- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/lcl/interfaces/cocoa/cocoacollectionview.pas b/lcl/interfaces/cocoa/cocoacollectionview.pas index 43e5bbc5e6..a3645ac9d6 100644 --- a/lcl/interfaces/cocoa/cocoacollectionview.pas +++ b/lcl/interfaces/cocoa/cocoacollectionview.pas @@ -229,12 +229,14 @@ begin lclListView:= TCustomListView( self.lclGetTarget ); if NOT Assigned(lclListView) then Exit; + if NOT Assigned(self.callback) then + Exit; lclItem:= lclListView.Items[indexPath.item]; - cocoaImage:= callback.GetImageFromIndex( lclItem.ImageIndex ); + cocoaImage:= self.callback.GetImageFromIndex( lclItem.ImageIndex ); cocoaItem.imageView.setImage( cocoaImage ); - callback.GetItemTextAt( indexPath.item, 0, lclText ); + self.callback.GetItemTextAt( indexPath.item, 0, lclText ); cocoaItem.textField.setStringValue( StrToNSString(lclText) ); isSelected:= self.callback.getItemStableSelection(indexPath.item); @@ -289,11 +291,15 @@ end; procedure TCocoaCollectionView.restoreFromStableSelection; begin - self.setSelectionIndexes( callback.selectionIndexSet ); + if Assigned(self.callback) then + self.setSelectionIndexes( self.callback.selectionIndexSet ); end; procedure TCocoaCollectionView.reloadData; begin + if NOT Assigned(self.callback) then + Exit; + if NOT TCocoaListView(self.callback.Owner).initializing then begin inherited reloadData; self.cancelPreviousPerformRequestsWithTarget_selector_object( @@ -400,7 +406,8 @@ begin item.textField.setToolTip( item.textField.stringValue ); item.view.setNeedsDisplay_(True); end; - callback.selectOne( indexPath.item, True ); + if Assigned(self.callback) then + self.callback.selectOne( indexPath.item, True ); end; end; @@ -423,7 +430,8 @@ begin item.textField.setToolTip( nil ); item.view.setNeedsDisplay_(True); end; - callback.selectOne( indexPath.item, False ); + if Assigned(self.callback) then + self.callback.selectOne( indexPath.item, False ); end; end; diff --git a/lcl/interfaces/cocoa/cocoawscomctrls.pas b/lcl/interfaces/cocoa/cocoawscomctrls.pas index 0f93dc5cb0..fb154341d9 100644 --- a/lcl/interfaces/cocoa/cocoawscomctrls.pas +++ b/lcl/interfaces/cocoa/cocoawscomctrls.pas @@ -719,6 +719,9 @@ var lclcb : TLCLListViewCallback; begin lclcb:= getCallback; + if NOT Assigned(lclcb) then + Exit; + lclcb.tempItemsCountDelta:= -1; lclcb.checkedIdx.shiftIndexesStartingAtIndex_by(AIndex, -1); _tableView.lclInsDelRow(AIndex, false); @@ -740,7 +743,11 @@ function TCocoaWSListView_TableViewHandler.ItemGetChecked( var lclcb : TLCLListViewCallback; begin + Result:= False; lclcb:= getCallback; + if NOT Assigned(lclcb) then + Exit; + Result := lclcb.checkedIdx.containsIndex(AIndex); end; @@ -773,6 +780,9 @@ var lclcb: TLCLListViewCallback; begin lclcb:= getCallback; + if NOT Assigned(lclcb) then + Exit; + lclcb.checkedIdx.shiftIndexesStartingAtIndex_by(AIndex, 1); _tableView.lclInsDelRow(AIndex, true); _tableView.sizeToFit(); @@ -784,6 +794,9 @@ var lclcb: TLCLListViewCallback; begin lclcb:= getCallback; + if NOT Assigned(lclcb) then + Exit; + if AChecked and not lclcb.checkedIdx.containsIndex(AIndex) then begin lclcb.checkedIdx.addIndex(AIndex); _tableView.reloadDataForRow_column(AIndex, 0); @@ -914,6 +927,9 @@ var lclcb: TLCLListViewCallback; begin lclcb:= getCallback; + if NOT Assigned(lclcb) then + Exit; + lclcb.ownerData := AValue; if lclcb.ownerData then lclcb.checkedIdx.removeAllIndexes; // releasing memory @@ -978,6 +994,7 @@ begin lclcb:= getCallback; if NOT Assigned(lclcb) then Exit; + if Assigned(lclcb.checkedIdx) then lclcb.checkedIdx.removeAllIndexes; _tableView.reloadData(); @@ -1081,6 +1098,9 @@ var lclcb : TLCLListViewCallback; begin lclcb:= getCallback; + if NOT Assigned(lclcb) then + Exit; + lclcb.selectionIndexSet.shiftIndexesStartingAtIndex_by( AIndex+1, -1 ); _collectionView.reloadData; end; @@ -1091,7 +1111,10 @@ var item: NSCollectionViewItem; frame: NSRect; begin + Result:= Rect( 0, 0, 0, 0 ); item:= _collectionView.itemAtIndex( AIndex ); + if NOT Assigned(item) then + Exit; case ACode of drLabel: @@ -1137,6 +1160,9 @@ var begin Result:= false; lclcb:= getCallback; + if NOT Assigned(lclcb) then + Exit; + case AState of lisSelected: begin Result:= (AIndex>=0) and (AIndex < _collectionView.numberOfItemsInSection(0)); @@ -1151,6 +1177,9 @@ var lclcb: TLCLListViewCallback; begin lclcb:= self.getCallback; + if NOT Assigned(lclcb) then + Exit; + if TCocoaListView(lclcb.Owner).initializing then Exit; @@ -1176,9 +1205,12 @@ procedure TCocoaWSListView_CollectionViewHandler.ItemSetState( var lclcb: TLCLListViewCallback; begin + lclcb:= self.getCallback; + if NOT Assigned(lclcb) then + Exit; + case AState of lisSelected: begin - lclcb:= self.getCallback; if lclcb.getItemStableSelection(AIndex) <> AIsSet then begin _collectionView.selectOneItemByIndex( AIndex, AIsSet ); _collectionView.redrawVisibleItems; @@ -1329,6 +1361,9 @@ var lclcb : TLCLListViewCallback; begin lclcb:= getCallback; + if NOT Assigned(lclcb) then + Exit; + lclcb.selectionIndexSet.removeAllIndexes; _collectionView.reloadData(); end;