Skip to content

Commit

Permalink
Merge pull request #2680 from cwensley/curtis/mac-add-gridview-null-c…
Browse files Browse the repository at this point in the history
…hecks

Mac: Check for null handler in Tree/GridView delegates and data sources
  • Loading branch information
cwensley authored Aug 30, 2024
2 parents a9d8016 + a9a0214 commit 9bbc8d4
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 33 deletions.
46 changes: 31 additions & 15 deletions src/Eto.Mac/Forms/Controls/GridViewHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,15 @@ public class EtoTableDelegate : NSTableViewDelegate

public override bool ShouldEditTableColumn(NSTableView tableView, NSTableColumn tableColumn, nint row)
{
var colHandler = Handler.GetColumn(tableColumn);
var item = Handler.collection.ElementAt((int)row);
var h = Handler;
if (h == null)
return false;

var colHandler = h.GetColumn(tableColumn);
var item = h.collection.ElementAt((int)row);
var args = new GridViewCellEventArgs(colHandler.Widget, (int)row, colHandler.Column, item);
Handler.Callback.OnCellEditing(Handler.Widget, args);
Handler.SetIsEditing(true);
h.Callback.OnCellEditing(h.Widget, args);
h.SetIsEditing(true);
return true;
}

Expand Down Expand Up @@ -362,23 +366,32 @@ public override void SelectionDidChange(NSNotification notification)

public override nfloat GetSizeToFitColumnWidth(NSTableView tableView, nint column)
{
var colHandler = Handler.GetColumn(tableView.TableColumns()[column]);
var h = Handler;
if (h == null)
return 20;
var columns = tableView.TableColumns();
if (column >= (int)columns.Length)
return 20;
var colHandler = h.GetColumn(columns[column]);
if (colHandler != null)
{
// turn on autosizing for this column again
colHandler.AutoSize = true;
Handler.DidSetAutoSizeColumn = true;
Application.Instance.AsyncInvoke(() => Handler.DidSetAutoSizeColumn = false);
h.DidSetAutoSizeColumn = true;
Application.Instance.AsyncInvoke(() => h.DidSetAutoSizeColumn = false);
return colHandler.GetPreferredWidth();
}
return 20;
}

public override void DidClickTableColumn(NSTableView tableView, NSTableColumn tableColumn)
{
var colHandler = Handler.GetColumn(tableColumn);
var h = Handler;
if (h == null)
return;
var colHandler = h.GetColumn(tableColumn);
if (colHandler.Sortable)
Handler.Callback.OnColumnHeaderClick(Handler.Widget, new GridColumnEventArgs(colHandler.Widget));
h.Callback.OnColumnHeaderClick(h.Widget, new GridColumnEventArgs(colHandler.Widget));
}

public override void ColumnDidResize(NSNotification notification)
Expand All @@ -388,17 +401,17 @@ public override void ColumnDidResize(NSNotification notification)

public override NSView GetViewForItem(NSTableView tableView, NSTableColumn tableColumn, nint row)
{
var colHandler = Handler.GetColumn(tableColumn);
if (colHandler != null)
var h = Handler;
if (h != null)
{
var cellHandler = colHandler.DataCellHandler;
var cellHandler = h.GetColumn(tableColumn)?.DataCellHandler;
if (cellHandler != null)
{
return cellHandler.GetViewForItem(tableView, tableColumn, (int)row, null, (obj, r) => Handler.GetItem(r));
return cellHandler.GetViewForItem(tableView, tableColumn, (int)row, null, (obj, r) => h.GetItem(r));
}
}

return tableView.MakeView(tableColumn.Identifier, this);
return tableView.MakeView(tableColumn?.Identifier ?? string.Empty, this);
}

public override void DidAddRowView(NSTableView tableView, NSTableRowView rowView, nint row)
Expand All @@ -408,7 +421,10 @@ public override void DidAddRowView(NSTableView tableView, NSTableRowView rowView

public override void DidRemoveRowView(NSTableView tableView, NSTableRowView rowView, nint row)
{
foreach (var col in Handler.ColumnHandlers)
var h = Handler;
if (h == null)
return;
foreach (var col in h.ColumnHandlers)
{
if (col.DisplayIndex != -1)
{
Expand Down
50 changes: 32 additions & 18 deletions src/Eto.Mac/Forms/Controls/TreeGridViewHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ public class EtoOutlineDelegate : NSOutlineViewDelegate

public override bool IsGroupItem(NSOutlineView outlineView, NSObject item)
{
return Handler.ShowGroups && item != null && outlineView.LevelForItem(item) == 0;
return Handler?.ShowGroups == true && item != null && outlineView.LevelForItem(item) == 0;
}

public override bool ShouldSelectItem(NSOutlineView outlineView, NSObject item)
{
return Handler.AllowGroupSelection || !IsGroupItem(outlineView, item);
return Handler?.AllowGroupSelection == true || !IsGroupItem(outlineView, item);
}

public override bool ShouldEditTableColumn(NSOutlineView outlineView, NSTableColumn tableColumn, NSObject item)
Expand Down Expand Up @@ -146,7 +146,7 @@ public override void SelectionDidChange(NSNotification notification)
public override void ItemDidCollapse(NSNotification notification)
{
var h = Handler;
if (h.suppressExpandCollapseEvents > 0)
if (h == null || h.suppressExpandCollapseEvents > 0)
return;
var myitem = h.GetEtoItem(notification.UserInfo[(NSString)"NSObject"]);
if (myitem != null)
Expand All @@ -165,7 +165,7 @@ public override void ItemDidCollapse(NSNotification notification)
public override bool ShouldExpandItem(NSOutlineView outlineView, NSObject item)
{
var h = Handler;
if (h.suppressExpandCollapseEvents > 0)
if (h == null || h.suppressExpandCollapseEvents > 0)
return true;
var myitem = h.GetEtoItem(item);
if (myitem != null)
Expand All @@ -180,7 +180,7 @@ public override bool ShouldExpandItem(NSOutlineView outlineView, NSObject item)
public override bool ShouldCollapseItem(NSOutlineView outlineView, NSObject item)
{
var h = Handler;
if (h.suppressExpandCollapseEvents > 0)
if (h == null || h.suppressExpandCollapseEvents > 0)
return true;
var myitem = h.GetEtoItem(item);
if (myitem != null)
Expand All @@ -204,7 +204,7 @@ public override bool ShouldCollapseItem(NSOutlineView outlineView, NSObject item
public override void ItemDidExpand(NSNotification notification)
{
var h = Handler;
if (h.suppressExpandCollapseEvents > 0)
if (h == null || h.suppressExpandCollapseEvents > 0)
return;
var item = notification.UserInfo[(NSString)"NSObject"];
var myitem = h.GetEtoItem(item);
Expand All @@ -221,25 +221,34 @@ public override void ItemDidExpand(NSNotification notification)

public override nfloat GetSizeToFitColumnWidth(NSOutlineView outlineView, nint column)
{
var colHandler = Handler.GetColumn(outlineView.TableColumns()[column]);
var h = Handler;
if (h == null)
return 20;
var columns = outlineView.TableColumns();
if (column >= (int)columns.Length)
return 20;
var colHandler = h.GetColumn(columns[column]);
if (colHandler != null)
{
// turn on autosizing for this column again
colHandler.AutoSize = true;
Handler.DidSetAutoSizeColumn = true;
Application.Instance.AsyncInvoke(() => Handler.DidSetAutoSizeColumn = false);
Application.Instance.AsyncInvoke(() => h.DidSetAutoSizeColumn = false);
return colHandler.GetPreferredWidth();
}
return 20;
}

public override void DidClickTableColumn(NSOutlineView outlineView, NSTableColumn tableColumn)
{
var column = Handler.GetColumn(tableColumn);
var h = Handler;
if (h == null)
return;
var column = h.GetColumn(tableColumn);
if (column.Sortable)
{
var args = new GridColumnEventArgs(column.Widget);
Handler.Callback.OnColumnHeaderClick(Handler.Widget, args);
h.Callback.OnColumnHeaderClick(h.Widget, args);
}
}

Expand All @@ -250,14 +259,14 @@ public override void ColumnDidResize(NSNotification notification)

public override NSView GetView(NSOutlineView outlineView, NSTableColumn tableColumn, NSObject item)
{
if (tableColumn == null && Handler.ShowGroups)
tableColumn = outlineView.TableColumns()[0];

var colHandler = Handler.GetColumn(tableColumn);
if (colHandler != null && colHandler.DataCell != null)
var h = Handler;
if (h != null)
{
var cellHandler = colHandler.DataCell.Handler as ICellHandler;
if (cellHandler != null)
if (tableColumn == null && h.ShowGroups)
tableColumn = outlineView.TableColumns()[0];

var colHandler = h.GetColumn(tableColumn);
if (colHandler?.DataCell?.Handler is ICellHandler cellHandler)
{
var row = (int)outlineView.RowForItem(item);
return cellHandler.GetViewForItem(outlineView, tableColumn, row, item, (obj, row) => obj != null ? ((EtoTreeItem)obj).Item : null);
Expand All @@ -273,7 +282,10 @@ public override void DidAddRowView(NSOutlineView outlineView, NSTableRowView row

public override void DidRemoveRowView(NSOutlineView outlineView, NSTableRowView rowView, nint row)
{
foreach (var col in Handler.ColumnHandlers)
var h = Handler;
if (h == null)
return;
foreach (var col in h.ColumnHandlers)
{
if (col.DisplayIndex != -1)
{
Expand Down Expand Up @@ -402,6 +414,8 @@ public override NSDragOperation ValidateDrop(NSOutlineView outlineView, NSDraggi
TreeGridViewDragInfo GetDragInfo(NSDraggingInfo info, NSObject item, nint index)
{
var h = Handler;
if (h == null)
return null;
var outlineView = h.Control;
var position = GridDragPosition.Over;
int? childIndex;
Expand Down

0 comments on commit 9bbc8d4

Please sign in to comment.