Skip to content
This repository has been archived by the owner on Oct 16, 2020. It is now read-only.

Commit

Permalink
BindingsUpdated and GesturesUpdated events.
Browse files Browse the repository at this point in the history
git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/shortcuts@4591 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
  • Loading branch information
sandrejev committed Aug 4, 2009
1 parent 07c2728 commit 7554e22
Show file tree
Hide file tree
Showing 21 changed files with 1,282 additions and 1,323 deletions.
394 changes: 197 additions & 197 deletions SharpDevelop.Tests.sln

Large diffs are not rendered by default.

Expand Up @@ -168,7 +168,7 @@ Keys[] GetKeyboardShortcut(string path, string id)

Keys[] GetKeyBoardShortcut(string routedCommandName)
{
var template = new BindingInfoTemplate { RoutedCommandName = routedCommandName};
var template = new BindingInfoTemplate { RoutedCommandName = routedCommandName };
var gestureCollection = SDCommandManager.FindInputGestures(BindingInfoMatchType.SuperSet, template);
var keyCollection = new Keys[gestureCollection.Count];

Expand Down
Expand Up @@ -6,6 +6,7 @@
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using SDCommandManager=ICSharpCode.Core.Presentation.CommandManager;


namespace ICSharpCode.Core.Presentation
Expand All @@ -17,7 +18,7 @@ public class BindingGroup
{
private HashSet<WeakReference> _attachedInstances = new HashSet<WeakReference>(new WeakReferenceEqualirtyComparer());

private List<BindingGroup> _nestedGroups = new List<BindingGroup>();
private BindingGroupCollection _nestedGroups = new BindingGroupCollection();

public string Name
{
Expand Down Expand Up @@ -67,44 +68,12 @@ public void DetachFrom(UIElement instance)

private void InvokeBindingUpdateHandlers(UIElement instance, bool attaching)
{
var type = instance.GetType();

// Invoke class wide and instance update handlers
var instanceNames = CommandManager.GetUIElementNameCollection(instance);
var typeNames = CommandManager.GetUITypeNameCollection(type);

var bindingInfoTemplates = new IBindingInfoTemplate[instanceNames.Count + typeNames.Count + 1];

var i = 0;

bindingInfoTemplates[i++] = new BindingInfoTemplate { Groups = new BindingGroupCollection { this } };

foreach(var instanceName in instanceNames) {
bindingInfoTemplates[i++] = new BindingInfoTemplate { OwnerInstanceName = instanceName};
}

foreach(var typeName in typeNames) {
bindingInfoTemplates[i++] = new BindingInfoTemplate { OwnerTypeName = typeName};
}

var args = new BindingsUpdatedHandlerArgs();
if(attaching) {
args.AddedInstances = new List<UIElement>{ instance };
} else {
args.RemovedInstances = new List<UIElement>{ instance };
}

CommandManager.InvokeCommandBindingUpdateHandlers(
this,
args,
BindingInfoMatchType.SubSet | BindingInfoMatchType.SuperSet,
bindingInfoTemplates);

CommandManager.InvokeInputBindingUpdateHandlers(
this,
args,
BindingInfoMatchType.SubSet | BindingInfoMatchType.SuperSet,
bindingInfoTemplates);
SDCommandManager.InvokeBindingsChanged(
this,
new NotifyBindingsChangedEventArgs(
NotifyBindingsChangedAction.GroupAttachmendsModified,
FlatNestedGroups,
new []{instance}));
}

public ICollection<UIElement> GetAttachedInstances(ICollection<Type> types)
Expand Down Expand Up @@ -137,20 +106,22 @@ public ICollection<UIElement> GetAttachedInstances(ICollection<UIElement> instan
return attachedInstances;
}

public List<BindingGroup> NestedGroups
public BindingGroupCollection NestedGroups
{
get {
return _nestedGroups;
}
}

public ICollection<BindingGroup> FlatNestedGroups
public BindingGroupCollection FlatNestedGroups
{
get {
var foundNestedGroups = new HashSet<BindingGroup>();
FlattenNestedGroups(this, foundNestedGroups);

return foundNestedGroups;
var groups = new BindingGroupCollection();
groups.AddRange(foundNestedGroups);
return groups;
}
}

Expand Down
Expand Up @@ -27,10 +27,36 @@ public BindingGroupCollection Groups
throw new ArgumentNullException("value");
}

var oldGroups = _groups;
_groups = value;

SetCollectionChanged<BindingGroup>(oldGroups, value, Groups_CollectionChanged);
}
}

private void Groups_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {
if(IsRegistered) {
var modifiedGroups = new BindingGroupCollection();
if(e.NewItems != null) {
modifiedGroups.AddRange(e.NewItems.Cast<BindingGroup>());
}

if(e.OldItems != null) {
modifiedGroups.AddRange(e.OldItems.Cast<BindingGroup>());
}

ICollection<UIElement> attachedInstances = null;
if(OwnerInstanceName != null) {
attachedInstances = modifiedGroups.FlatNesteGroups.GetAttachedInstances(OwnerInstances);
} else {
attachedInstances = modifiedGroups.FlatNesteGroups.GetAttachedInstances(OwnerTypes);
}

SDCommandManager.InvokeBindingsChanged(this, new NotifyBindingsChangedEventArgs(NotifyBindingsChangedAction.GroupAttachmendsModified, modifiedGroups, attachedInstances));
}
}


public string _ownerInstanceName;

/// <summary>
Expand Down Expand Up @@ -138,7 +164,7 @@ public virtual string OwnerTypeName
/// Routed command instance which will be invoked when this binding is triggered
/// </summary>
public RoutedUICommand RoutedCommand {
get {
get {
return SDCommandManager.GetRoutedUICommand(RoutedCommandName);
}
}
Expand All @@ -155,17 +181,6 @@ public bool IsRegistered
get; set;
}

public InputBindingIdentifier Identifier {
get {
var identifier = new InputBindingIdentifier();
identifier.OwnerInstanceName = OwnerInstanceName;
identifier.OwnerTypeName = OwnerTypeName;
identifier.RoutedCommandName = RoutedCommandName;

return identifier;
}
}

public void SetCollectionChanged<T>(IObservableCollection<T> oldObservableCollection, IObservableCollection<T> newObservableCollection, NotifyCollectionChangedEventHandler handler)
{
newObservableCollection.CollectionChanged += handler;
Expand All @@ -187,84 +202,48 @@ public void SetCollectionChanged<T>(IObservableCollection<T> oldObservableCollec
}
}

private BindingsUpdatedHandler defaultCommandBindingHandler;

/// <summary>
/// Updates owner bindings
/// </summary>
public BindingsUpdatedHandler DefaultBindingsUpdateHandler
public void BindingsChangedHandler(object sender, NotifyBindingsChangedEventArgs args)
{
get {
if(defaultCommandBindingHandler == null && OwnerTypeName != null) {
defaultCommandBindingHandler = delegate(object sender, BindingsUpdatedHandlerArgs args) {
var ownerTypes = OwnerTypes;

if(RoutedCommand != null && OwnerTypes != null && IsRegistered) {
GenerateBindings();

if(Groups.Count == 0) {
var groupInstances = Groups.GetAttachedInstances(ownerTypes);
SetInstanceBindings(groupInstances, null);

var removedOwnerTypes = new List<Type>(ownerTypes);
if(args.RemovedTypes != null) {
removedOwnerTypes.AddRange(args.RemovedTypes);
}

SetClassBindings(removedOwnerTypes, ownerTypes);
} else {
SetClassBindings(ownerTypes, null);

var groupInstances = Groups.GetAttachedInstances(ownerTypes);
var removedOwnerInstances = new List<UIElement>(groupInstances);
if(args.RemovedInstances != null) {
removedOwnerInstances.AddRange(args.RemovedInstances);
}

SetInstanceBindings(removedOwnerInstances, groupInstances);
}
}
};
} else if(defaultCommandBindingHandler == null && OwnerInstanceName != null) {
defaultCommandBindingHandler = delegate(object sender, BindingsUpdatedHandlerArgs args) {
if(RoutedCommand != null && OwnerInstances != null && IsRegistered) {
GenerateBindings();

if(Groups.Count == 0) {
SetInstanceBindings(OwnerInstances, OwnerInstances);
} else {
var removedInstances = new List<UIElement>(OwnerInstances);
if(args.RemovedInstances != null) {
removedInstances.AddRange(args.RemovedInstances);
}

SetInstanceBindings(removedInstances, Groups.GetAttachedInstances(OwnerInstances));
}
}
};
}
if(!IsRegistered || RoutedCommand == null || (OwnerTypes == null && OwnerInstances == null)) {
return;
}

if( (args.Action == NotifyBindingsChangedAction.BindingInfoModified && args.ModifiedBindingInfoTemplates.Contains(new BindingInfoTemplate(this, false)))
|| (args.Action == NotifyBindingsChangedAction.NamedInstanceModified && OwnerInstanceName == args.UIElementName)
|| (args.Action == NotifyBindingsChangedAction.RoutedUICommandModified && routedCommandName == args.RoutedCommandName)
|| (args.Action == NotifyBindingsChangedAction.NamedTypeModified && OwnerTypeName == args.TypeName)
|| (args.Action == NotifyBindingsChangedAction.GroupAttachmendsModified && ((OwnerTypeName != null && OwnerTypes.Any(t1 => args.AttachedInstances.Any(t2 => t1 == t2.GetType())))
|| (OwnerInstanceName != null && OwnerInstances.Any(t1 => args.AttachedInstances.Any(t2 => t1 == t2)))))
) {
GenerateBindings();

return defaultCommandBindingHandler;
if(OwnerInstanceName != null) {
SetInstanceBindings(Groups.Count == 0 ? OwnerInstances : Groups.GetAttachedInstances(OwnerInstances));
} else {
if(Groups.Count == 0) {
SetInstanceBindings(null);
SetClassBindings(OwnerTypes);
} else {
SetClassBindings(null);
SetInstanceBindings(Groups.GetAttachedInstances(OwnerTypes));
}
}
}
}

public void RemoveActiveBindings()
{
if(OwnerTypeName != null) {
if(Groups.Count > 0) {
SetInstanceBindings(Groups.GetAttachedInstances(OwnerTypes), null);
}

SetClassBindings(OwnerTypes, null);
} else if(OwnerInstanceName != null) {
SetInstanceBindings(OwnerInstances, null);
}
SetInstanceBindings(null);
SetClassBindings(null);
}

protected abstract void GenerateBindings();

protected abstract void SetInstanceBindings(ICollection<UIElement> oldInstances, ICollection<UIElement> newInstances);
protected abstract void SetInstanceBindings(ICollection<UIElement> newInstances);

protected abstract void SetClassBindings(ICollection<Type> oldTypes, ICollection<Type> newtTypes);
protected abstract void SetClassBindings(ICollection<Type> newtTypes);
}
}
Expand Up @@ -34,6 +34,14 @@ public BindingInfoTemplate()

}


public BindingInfoTemplate(InputBindingIdentifier identifier)
{
OwnerInstanceName = identifier.OwnerInstanceName;
OwnerTypeName = identifier.OwnerTypeName;
RoutedCommandName = identifier.RoutedCommandName;
}

public BindingInfoTemplate(IBindingInfoTemplate template, bool includeGroup)
{
OwnerInstanceName = template.OwnerInstanceName;
Expand Down
Expand Up @@ -26,38 +26,6 @@ public CommandBindingInfo()
Groups = new BindingGroupCollection();
}

public BindingGroupCollection Groups
{
get {
return base.Groups;
}
set {
var oldGroups = base.Groups;
base.Groups = value;

SetCollectionChanged<BindingGroup>(oldGroups, value, Groups_CollectionChanged);
}
}

private void Groups_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {
if(IsRegistered) {
var modifiedGroups = new BindingGroupCollection();
if(e.NewItems != null) {
modifiedGroups.AddRange(e.NewItems.Cast<BindingGroup>());
}

if(e.OldItems != null) {
modifiedGroups.AddRange(e.OldItems.Cast<BindingGroup>());
}

SDCommandManager.InvokeCommandBindingUpdateHandlers(
this,
new BindingsUpdatedHandlerArgs(),
BindingInfoMatchType.SubSet,
new BindingInfoTemplate(this, false) { Groups = modifiedGroups });
}
}

private string commandTypeName;

/// <summary>
Expand Down Expand Up @@ -164,8 +132,10 @@ public CanExecuteRoutedEventHandler CanExecuteEventHandler
}
}

protected override void SetInstanceBindings(ICollection<UIElement> newInstances, ICollection<UIElement> oldInstances)
{
List<UIElement> oldInstances;

protected override void SetInstanceBindings(ICollection<UIElement> newInstances)
{
if(oldInstances != null) {
foreach(var ownerInstance in oldInstances) {
foreach(CommandBinding binding in OldCommandBindings) {
Expand All @@ -174,14 +144,19 @@ protected override void SetInstanceBindings(ICollection<UIElement> newInstances,
}
}

oldInstances = new List<UIElement>();

if(newInstances != null) {
foreach(var ownerInstance in newInstances) {
ownerInstance.CommandBindings.AddRange(ActiveCommandBindings);
oldInstances.Add(ownerInstance);
}
}
}

protected override void SetClassBindings(ICollection<Type> oldTypes, ICollection<Type> newTypes)
List<Type> oldTypes;

protected override void SetClassBindings(ICollection<Type> newTypes)
{
if(oldTypes != null) {
foreach(var ownerType in oldTypes) {
Expand All @@ -191,10 +166,13 @@ protected override void SetClassBindings(ICollection<Type> oldTypes, ICollection
}
}

oldTypes = new List<Type>();

if(newTypes != null) {
foreach(var ownerType in newTypes) {
foreach(CommandBinding binding in ActiveCommandBindings) {
System.Windows.Input.CommandManager.RegisterClassCommandBinding(ownerType, binding);
oldTypes.Add(ownerType);
}
}
}
Expand Down

0 comments on commit 7554e22

Please sign in to comment.