Add protection against DisconnectedItem as sender object in mouse event handlers

Workaround was added in early development stage already:
1f985c53aa
However, it was meant to be no longer occurring, so detection was removed here:
6ebfed5f78
3f09f40188
3153032110

Since it occurred again, we reintroduce this fix at least on all mouse events.
This time, we check for the expected object type rather looking up the item in the list.
This commit is contained in:
Peter Kirmeier 2023-09-17 18:29:51 +02:00
parent 4bb533ea7c
commit 569722a5b0

View file

@ -1146,14 +1146,18 @@ namespace SystemTrayMenu.UserInterface
{
if (!isShellContextMenuOpen)
{
CellMouseEnter?.Invoke((RowData)((ListViewItem)sender).Content);
// "DisconnectedItem" protection
if (((ListViewItem)sender).Content is RowData rowData)
{
CellMouseEnter?.Invoke(rowData);
}
}
}
private void ListViewItem_MouseLeave(object sender, MouseEventArgs e)
{
var content = ((ListViewItem)sender).Content;
if (content is RowData rowData)
// "DisconnectedItem" protection
if (((ListViewItem)sender).Content is RowData rowData)
{
rowData.IsClicked = false;
countLeftMouseButtonClicked = 0;
@ -1169,39 +1173,56 @@ namespace SystemTrayMenu.UserInterface
}
}
private void ListViewItem_PreviewMouseDown(object sender, MouseButtonEventArgs e) =>
CellMouseDown?.Invoke((RowData)((ListViewItem)sender).Content);
private void ListViewItem_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
// "DisconnectedItem" protection
if (((ListViewItem)sender).Content is RowData rowData)
{
CellMouseDown?.Invoke(rowData);
}
}
private void ListViewItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
RowData rowData = (RowData)((ListViewItem)sender).Content;
rowData.IsClicked = true;
countLeftMouseButtonClicked = e.ClickCount;
// "DisconnectedItem" protection
if (((ListViewItem)sender).Content is RowData rowData)
{
rowData.IsClicked = true;
countLeftMouseButtonClicked = e.ClickCount;
}
}
private void ListViewItem_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
RowData rowData = (RowData)((ListViewItem)sender).Content;
if (rowData.IsClicked)
// "DisconnectedItem" protection
if (((ListViewItem)sender).Content is RowData rowData)
{
// Same row has been called with PreviewMouseLeftButtonDown without leaving the item, so we can call it a "click".
// The click count is also taken from Down event as it seems not being correct in Up event.
rowData.OpenItem(countLeftMouseButtonClicked);
if (rowData.IsClicked)
{
// Same row has been called with PreviewMouseLeftButtonDown without leaving the item, so we can call it a "click".
// The click count is also taken from Down event as it seems not being correct in Up event.
rowData.OpenItem(countLeftMouseButtonClicked);
}
rowData.IsClicked = false;
}
countLeftMouseButtonClicked = 0;
rowData.IsClicked = false;
}
private void ListViewItem_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
// At mouse location
Point position = Mouse.GetPosition(this);
position.Offset(Left, Top);
// "DisconnectedItem" protection
if (((ListViewItem)sender).Content is RowData rowData)
{
// At mouse location
Point position = Mouse.GetPosition(this);
position.Offset(Left, Top);
isShellContextMenuOpen = true;
((RowData)((ListViewItem)sender).Content).OpenShellContextMenu(position);
isShellContextMenuOpen = false;
isShellContextMenuOpen = true;
rowData.OpenShellContextMenu(position);
isShellContextMenuOpen = false;
}
}
}
}