Skip to content

Commit

Permalink
Optimisation:
Browse files Browse the repository at this point in the history
Delay updating description incase it changes quickly, this means it bubbles up the tree slower, but also wastes less cpu time.
Only create context menus when they are needed. This makes it a ton faster.
  • Loading branch information
Lyeeedar committed Feb 6, 2017
1 parent 639af76 commit 3c463e6
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 52 deletions.
3 changes: 1 addition & 2 deletions StructuredXmlEditor/Data/CollectionChildItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ public override bool CanRemove
public CollectionChildItem(DataDefinition definition, UndoRedoManager undoRedo) : base(definition, undoRedo)
{
PropertyChanged += OnPropertyChanged;
IsContextMenuDynamic = true;
}

//-----------------------------------------------------------------------
Expand Down Expand Up @@ -235,7 +234,7 @@ public void WrappedItemPropertyChanged(object sender, PropertyChangedEventArgs a
{
if (args.PropertyName == "Description")
{
RaisePropertyChangedEvent("Description");
Future.Call(() => { RaisePropertyChangedEvent("Description"); }, 100, this);
}
else if (args.PropertyName == "Name")
{
Expand Down
1 change: 0 additions & 1 deletion StructuredXmlEditor/Data/CollectionItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public CollectionItem(DataDefinition definition, UndoRedoManager undoRedo) : bas
};

SelectedDefinition = CDef.ChildDefinitions.First();
IsContextMenuDynamic = true;
}

//-----------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion StructuredXmlEditor/Data/ComplexDataItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public override void ChildPropertyChanged(object sender, PropertyChangedEventArg

if (args.PropertyName == "Description")
{
RaisePropertyChangedEvent("Description");
Future.Call(() => { RaisePropertyChangedEvent("Description"); }, 100, this);
}
}

Expand Down
16 changes: 2 additions & 14 deletions StructuredXmlEditor/Data/DataItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -370,18 +370,6 @@ public XmlDataGrid Grid
}
private XmlDataGrid m_grid;

//-----------------------------------------------------------------------
public bool IsContextMenuDynamic;
public ContextMenu ContextMenu
{
get
{
if (m_menu == null || IsContextMenuDynamic) m_menu = CreateContextMenu();
return m_menu;
}
}
private ContextMenu m_menu;

//-----------------------------------------------------------------------
public DataDefinition Definition { get; set; }

Expand Down Expand Up @@ -550,7 +538,7 @@ public virtual DataItem DuplicateData(UndoRedoManager undoRedo)
public abstract void Paste();

//-----------------------------------------------------------------------
private ContextMenu CreateContextMenu()
public ContextMenu CreateContextMenu()
{
ContextMenu menu = new ContextMenu();

Expand Down Expand Up @@ -893,7 +881,7 @@ protected void DeferredRefreshChildren(DispatcherPriority priority = DispatcherP
m_deferredUpdateChildren = null;
}

m_deferredUpdateChildren = Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
m_deferredUpdateChildren = Application.Current.Dispatcher.BeginInvoke(priority, new Action(() =>
{
RefreshChildren();
m_deferredUpdateChildren = null;
Expand Down
3 changes: 1 addition & 2 deletions StructuredXmlEditor/Data/ReferenceItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ public override bool CanRemove
public ReferenceItem(DataDefinition definition, UndoRedoManager undoRedo) : base(definition, undoRedo)
{
SelectedDefinition = (Tuple<string, string>)(definition as ReferenceDefinition).ItemsSource.GetItemAt(0);
IsContextMenuDynamic = true;

PropertyChanged += (e, args) =>
{
Expand Down Expand Up @@ -218,7 +217,7 @@ public void WrappedItemPropertyChanged(object sender, PropertyChangedEventArgs a
{
if (args.PropertyName == "Description")
{
RaisePropertyChangedEvent("Description");
Future.Call(() => { RaisePropertyChangedEvent("Description"); }, 100, this);
}
}

Expand Down
4 changes: 4 additions & 0 deletions StructuredXmlEditor/StructuredXmlEditor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
<Compile Include="Util\DescendantPropertyChangedEventArgs.cs" />
<Compile Include="Util\Email.cs" />
<Compile Include="Util\Extensions.cs" />
<Compile Include="Util\Future.cs" />
<Compile Include="Util\PopupCloser.cs" />
<Compile Include="Util\SerializableDictionary.cs" />
<Compile Include="Util\UndoRedoManager.cs" />
Expand Down Expand Up @@ -170,6 +171,9 @@
</Compile>
<Compile Include="View\CustomControls\ZoomPan\ZoomPanCanvas.cs" />
<Compile Include="View\CustomControls\ZoomPan\ZoomPanItemsControl.cs" />
<Compile Include="View\Styles\DataGridView.xaml.cs">
<DependentUpon>DataGridView.xaml</DependentUpon>
</Compile>
<Compile Include="View\ValueConverters\ActiveDocumentConverter.cs" />
<Compile Include="View\ValueConverters\ColourMarkupConverter.cs" />
<Compile Include="View\ValueConverters\ConverterBase.cs" />
Expand Down
46 changes: 46 additions & 0 deletions StructuredXmlEditor/Util/Future.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace StructuredXmlEditor
{
public static class Future
{
private static object m_locker = new object();
private static Dictionary<object, Tuple<Action, Timer>> m_futures = new Dictionary<object, Tuple<Action, Timer>>();

public static void Call(Action func, int ms, object key = null)
{
if (key == null) key = new object();

lock (m_locker)
{
if (m_futures.ContainsKey(key))
{
m_futures[key].Item2.Change(ms, Timeout.Infinite);
}
else
{
var timer = new Timer(TimerElapsed, key, ms, Timeout.Infinite);
m_futures[key] = new Tuple<Action, Timer>(func, timer);
}
}
}

private static void TimerElapsed(object key)
{
lock (m_locker)
{
var data = m_futures[key];
m_futures.Remove(key);

Application.Current.Dispatcher.BeginInvoke(data.Item1);
data.Item2.Dispose();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -225,20 +225,15 @@ private void OnHeaderColumnWidthThumbDragDelta(object sender, DragDeltaEventArgs
}

//-----------------------------------------------------------------------
protected override void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
protected override DependencyObject GetContainerForItemOverride()
{
base.OnItemsSourceChanged(oldValue, newValue);

if (newValue != m_visibleItems)
{
throw new Exception("You cannot set 'ItemsSource' on 'DataGridView', use 'HierarchicalItemsSource', you fool!");
}
return new DataGridViewItem(this);
}

//-----------------------------------------------------------------------
protected override DependencyObject GetContainerForItemOverride()
protected override void ClearContainerForItemOverride(DependencyObject element, object item)
{
return new DataGridViewItem(this);
((DataGridViewItem)element).OnClearContainerForItemOverride();
}

//-----------------------------------------------------------------------
Expand All @@ -258,14 +253,6 @@ protected override void PrepareContainerForItemOverride(DependencyObject element
}
}

//-----------------------------------------------------------------------
protected override void ClearContainerForItemOverride(DependencyObject element, object item)
{
base.ClearContainerForItemOverride(element, item);

((DataGridViewItem)element).OnClearContainerForItemOverride();
}

//-----------------------------------------------------------------------
ItemData GetItemData(IDataGridItem item, out int index)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ public DataGridViewItem(DataGridView DataGrid)
//##############################################################################################################
#region Properties

public DataGridView DataGrid { get; set; }

#endregion Properties
//##############################################################################################################
#region Dependecy Properties

public DataGridView DataGrid { get; set; }

//##############################################################################################################
#region HeaderForeground
//-----------------------------------------------------------------------
public Brush HeaderForeground
Expand All @@ -69,7 +70,7 @@ public Brush HeaderForeground
public static readonly DependencyProperty HeaderForegroundProperty =
DependencyProperty.Register("HeaderForeground", typeof(Brush), typeof(DataGridViewItem), new PropertyMetadata(Brushes.Black));
#endregion HeaderForeground

//##############################################################################################################
#region CanSelect
//-----------------------------------------------------------------------
public bool CanSelect
Expand All @@ -82,7 +83,7 @@ public bool CanSelect
public static readonly DependencyProperty CanSelectProperty =
DependencyProperty.Register("CanSelect", typeof(bool), typeof(DataGridViewItem), new PropertyMetadata(true));
#endregion CanSelect

//##############################################################################################################
#region ListBox
//-----------------------------------------------------------------------
public DataGridView ListBox
Expand All @@ -95,7 +96,7 @@ public DataGridView ListBox
public static readonly DependencyProperty ListBoxProperty =
DependencyProperty.Register("ListBox", typeof(DataGridView), typeof(DataGridViewItem), new PropertyMetadata(null));
#endregion ListBox

//##############################################################################################################
#region Items
//-----------------------------------------------------------------------
private List<IDataGridItem> storedItems = new List<IDataGridItem>();
Expand Down Expand Up @@ -169,7 +170,7 @@ void OnChildPropertyChanged(object sender, PropertyChangedEventArgs args)
}

#endregion Items

//##############################################################################################################
#region HasItems
public bool HasItems
{
Expand All @@ -185,7 +186,7 @@ public bool HasItems
}
private bool m_hasItems;
#endregion HasItems

//##############################################################################################################
#region Level
//-----------------------------------------------------------------------
public int Level
Expand All @@ -198,7 +199,7 @@ public int Level
public static readonly DependencyProperty LevelProperty =
DependencyProperty.Register("Level", typeof(int), typeof(DataGridViewItem), new PropertyMetadata(0));
#endregion Level

//##############################################################################################################
#region IsExpandedChanged
//-----------------------------------------------------------------------
public static readonly RoutedEvent IsExpandedChangedEvent = EventManager.RegisterRoutedEvent(
Expand All @@ -217,7 +218,7 @@ void RaiseIsExpandedChangedEvent()
RaiseEvent(new RoutedEventArgs(DataGridViewItem.IsExpandedChangedEvent));
}
#endregion IsExpandedChanged

//##############################################################################################################
#region IsExpanded
//-----------------------------------------------------------------------
public bool IsExpanded
Expand All @@ -239,6 +240,7 @@ void OnIsExpandedChanged(bool oldValue, bool newValue)
RaiseIsExpandedChangedEvent();
}
#endregion IsExpanded
//##############################################################################################################

#endregion Dependency Properties
//##############################################################################################################
Expand All @@ -254,6 +256,11 @@ public void OnClearContainerForItemOverride()
{
incc.CollectionChanged -= OnItemsCollectionChanged;
}

foreach (var oldItem in storedItems)
{
oldItem.PropertyChanged -= OnChildPropertyChanged;
}
}
}

Expand Down Expand Up @@ -650,6 +657,8 @@ private static void ConvertElementToImage(FrameworkElement element)

#endregion Data
//##############################################################################################################
#region NotifyPropertyChanged

//--------------------------------------------------------------------------
public event PropertyChangedEventHandler PropertyChanged;

Expand All @@ -664,6 +673,9 @@ public void RaisePropertyChangedEvent
PropertyChanged(this, new PropertyChangedEventArgs(i_propertyName));
}
}

#endregion NotifyPropertyChanged
//##############################################################################################################
}

//##############################################################################################################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,5 @@ public interface IDataGridItem : INotifyPropertyChanged

//-----------------------------------------------------------------------
bool IsSelected { get; set; }

//-----------------------------------------------------------------------
ContextMenu ContextMenu { get; }
}
}
14 changes: 11 additions & 3 deletions StructuredXmlEditor/View/Styles/DataGridView.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<ResourceDictionary
x:Class="StructuredXmlEditor.View.DataGridViewStyle"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:l="clr-namespace:StructuredXmlEditor.View">
Expand Down Expand Up @@ -66,14 +67,21 @@
Property="KeyboardNavigation.IsTabStop"
Value="False" />
<Setter
Property="ContextMenu"
Value="{Binding ContextMenu}" />
Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem>Initial menu; this will be replaced ...</MenuItem>
</ContextMenu>
</Setter.Value>
</Setter>
<Setter
Property="Template">
<Setter.Value>
<ControlTemplate
TargetType="{x:Type l:DataGridViewItem}">
<Grid>
<Grid
ContextMenuOpening="Grid_ContextMenuOpening">

<Grid.ColumnDefinitions>
<ColumnDefinition
Width="{Binding ListBox.HeaderColumnWidth, RelativeSource={RelativeSource AncestorType={x:Type l:DataGridViewItem}}}" />
Expand Down
34 changes: 34 additions & 0 deletions StructuredXmlEditor/View/Styles/DataGridView.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using StructuredXmlEditor.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace StructuredXmlEditor.View
{
partial class DataGridViewStyle : ResourceDictionary
{
public DataGridViewStyle()
{
InitializeComponent();
}

private void Grid_ContextMenuOpening(object sender, System.Windows.Controls.ContextMenuEventArgs e)
{
FrameworkElement fe = e.Source as FrameworkElement;

if (fe.ContextMenu == null)
{
e.Handled = true;
fe.ContextMenu = ((DataItem)fe.DataContext).CreateContextMenu();
fe.ContextMenu.IsOpen = true;
}
else
{
fe.ContextMenu = ((DataItem)fe.DataContext).CreateContextMenu();
}
}
}
}

0 comments on commit 3c463e6

Please sign in to comment.