Skip to content

Commit

Permalink
Merge pull request #1775 from cwensley/curtis/customcell-selected-cel…
Browse files Browse the repository at this point in the history
…ls-leak

Reuse WpfCellEventArgs when possible
  • Loading branch information
cwensley committed Aug 20, 2020
2 parents 47271c1 + 160a89a commit 90c9f72
Showing 1 changed file with 25 additions and 33 deletions.
58 changes: 25 additions & 33 deletions src/Eto.Wpf/Forms/Cells/CustomCellHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ public class CustomCellHandler : CellHandler<swc.DataGridColumn, CustomCell, Cus
{
public static int ImageSize = 16;

object GetValue(object dataItem)
{
return dataItem;
}

public class WpfCellEventArgs : CellEventArgs
{
swc.DataGridColumn _gridColumn;
Expand Down Expand Up @@ -200,9 +195,11 @@ static void HandleControlDataContextChanged(object sender, sw.DependencyProperty
if (handler == null)
return;

var args = new WpfCellEventArgs(handler.ContainerHandler?.Grid, handler.Widget, -1, cell.Column, wpfctl.DataContext, CellStates.None);
var args = GetEditArgs(handler, cell, wpfctl);
var originalArgs = args;
args.SetSelected(cell);
args.SetRow(cell);
args.SetDataContext(wpfctl.DataContext);
var id = handler.Callback.OnGetIdentifier(handler.Widget, args);
var child = wpfctl.Control;
if (id != wpfctl.Identifier || child == null)
Expand All @@ -219,40 +216,33 @@ static void HandleControlDataContextChanged(object sender, sw.DependencyProperty
if (cache.Count > 0)
{
child = cache.Pop();
if (child.Properties.ContainsKey(CellEventArgs_Key))
{
args = child.Properties.Get<WpfCellEventArgs>(CellEventArgs_Key);
args.SetSelected(cell);
args.SetDataContext(wpfctl.DataContext);
}
else
child.Properties.Set(CellEventArgs_Key, args);
wpfctl.Control = child;
// get args for this control
args = GetEditArgs(handler, cell, wpfctl);
}
else
{
child = handler.Callback.OnCreateCell(handler.Widget, args);
child?.Properties.Set(CellEventArgs_Key, args);
wpfctl.Control = child;
// create args for this new control
args = GetEditArgs(handler, cell, wpfctl);
}

if (!ReferenceEquals(args, originalArgs))
{
args.SetSelected(cell);
args.SetRow(cell);
args.SetDataContext(wpfctl.DataContext);
}

if (wpfctl.IsLoaded && child?.Loaded == false)
{
child.GetWpfFrameworkElement()?.SetScale(true, true);
child.AttachNative();
}
wpfctl.Control = child;
wpfctl.Identifier = id;
wpfctl.Child = child.ToNative();
}
else
{
if (child.Properties.ContainsKey(CellEventArgs_Key))
{
args = child.Properties.Get<WpfCellEventArgs>(CellEventArgs_Key);
args.SetSelected(cell);
args.SetDataContext(wpfctl.DataContext);
}
else
child.Properties.Set(CellEventArgs_Key, args);
}
handler.Callback.OnConfigureCell(handler.Widget, args, child);

handler.FormatCell(wpfctl, cell, wpfctl.DataContext);
Expand Down Expand Up @@ -286,7 +276,8 @@ static void HandleControlUnloaded(object sender, sw.RoutedEventArgs e)
{
// Configure cell with null item to clear out data context and bindings.
// Bindings can cause memory leaks if they are bounds to long lived objects.
var args = new WpfCellEventArgs(handler.ContainerHandler?.Grid, handler.Widget, -1, cell.Column, null, CellStates.None);
var args = GetEditArgs(handler, cell, wpfctl);
args.SetDataContext(null);
handler.Callback.OnConfigureCell(handler.Widget, args, etoctl);
}
}
Expand Down Expand Up @@ -343,12 +334,13 @@ static WpfCellEventArgs GetEditArgs(CustomCellHandler handler, swc.DataGridCell
{
if (handler == null)
return null;
var ctl = editingElement as EtoBorder;
var args = ctl.Control.Properties.Get<WpfCellEventArgs>(CellEventArgs_Key);
var wpfctl = editingElement as EtoBorder;
var etoctl = wpfctl?.Control;
var args = etoctl?.Properties.Get<WpfCellEventArgs>(CellEventArgs_Key);
if (args == null)
{
args = new WpfCellEventArgs(handler.ContainerHandler?.Grid, handler.Widget, -1, cell.Column, ctl.DataContext, CellStates.None, ctl.Control);
ctl.Control.Properties.Set(CellEventArgs_Key, args);
args = new WpfCellEventArgs(handler.ContainerHandler?.Grid, handler.Widget, -1, cell.Column, wpfctl.IsLoaded ? wpfctl.DataContext : null, CellStates.None, etoctl);
etoctl?.Properties.Set(CellEventArgs_Key, args);
}
args.Handled = false;
return args;
Expand Down Expand Up @@ -401,4 +393,4 @@ public CustomCellHandler()

}
}
}
}

0 comments on commit 90c9f72

Please sign in to comment.