1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Profile editor - Fixed tree responsiveness while moving elements

This commit is contained in:
SpoinkyNL 2020-08-30 13:12:39 +02:00
parent ef9cfb9ce8
commit 8d88b14d78
2 changed files with 48 additions and 14 deletions

View File

@ -84,7 +84,9 @@ namespace Artemis.UI.Screens.ProfileEditor.ProfileTree
break; break;
} }
Unsubscribe();
_profileEditorService.UpdateSelectedProfile(); _profileEditorService.UpdateSelectedProfile();
Subscribe();
} }
// ReSharper disable once UnusedMember.Global - Called from view // ReSharper disable once UnusedMember.Global - Called from view
@ -152,15 +154,13 @@ namespace Artemis.UI.Screens.ProfileEditor.ProfileTree
protected override void OnInitialActivate() protected override void OnInitialActivate()
{ {
_profileEditorService.ProfileSelected += OnProfileSelected; Subscribe();
_profileEditorService.ProfileElementSelected += OnProfileElementSelected;
CreateRootFolderViewModel(); CreateRootFolderViewModel();
} }
protected override void OnClose() protected override void OnClose()
{ {
_profileEditorService.ProfileSelected -= OnProfileSelected; Unsubscribe();
_profileEditorService.ProfileElementSelected -= OnProfileElementSelected;
RootFolder?.Dispose(); RootFolder?.Dispose();
RootFolder = null; RootFolder = null;
@ -169,6 +169,18 @@ namespace Artemis.UI.Screens.ProfileEditor.ProfileTree
#region Event handlers #region Event handlers
private void Subscribe()
{
_profileEditorService.ProfileSelected += OnProfileSelected;
_profileEditorService.ProfileElementSelected += OnProfileElementSelected;
}
private void Unsubscribe()
{
_profileEditorService.ProfileSelected -= OnProfileSelected;
_profileEditorService.ProfileElementSelected -= OnProfileElementSelected;
}
private void OnProfileElementSelected(object sender, RenderProfileElementEventArgs e) private void OnProfileElementSelected(object sender, RenderProfileElementEventArgs e)
{ {
if (e.RenderProfileElement == SelectedTreeItem?.ProfileElement) if (e.RenderProfileElement == SelectedTreeItem?.ProfileElement)

View File

@ -41,9 +41,7 @@ namespace Artemis.UI.Screens.ProfileEditor.ProfileTree.TreeItem
Children = new BindableCollection<TreeItemViewModel>(); Children = new BindableCollection<TreeItemViewModel>();
ProfileElement.ChildAdded += ProfileElementOnChildAdded; Subscribe();
ProfileElement.ChildRemoved += ProfileElementOnChildRemoved;
UpdateProfileElements(); UpdateProfileElements();
} }
@ -65,9 +63,7 @@ namespace Artemis.UI.Screens.ProfileEditor.ProfileTree.TreeItem
public void Dispose() public void Dispose()
{ {
ProfileElement.ChildAdded -= ProfileElementOnChildAdded; Unsubscribe();
ProfileElement.ChildRemoved -= ProfileElementOnChildRemoved;
Dispose(true); Dispose(true);
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
} }
@ -94,8 +90,12 @@ namespace Artemis.UI.Screens.ProfileEditor.ProfileTree.TreeItem
Parent.AddExistingElement(source); Parent.AddExistingElement(source);
} }
Parent.Unsubscribe();
Parent.ProfileElement.RemoveChild(source.ProfileElement); Parent.ProfileElement.RemoveChild(source.ProfileElement);
Parent.ProfileElement.AddChild(source.ProfileElement, ProfileElement.Order); Parent.ProfileElement.AddChild(source.ProfileElement, ProfileElement.Order);
Parent.Subscribe();
Parent.UpdateProfileElements();
} }
public void SetElementBehind(TreeItemViewModel source) public void SetElementBehind(TreeItemViewModel source)
@ -106,8 +106,12 @@ namespace Artemis.UI.Screens.ProfileEditor.ProfileTree.TreeItem
Parent.AddExistingElement(source); Parent.AddExistingElement(source);
} }
Parent.Unsubscribe();
Parent.ProfileElement.RemoveChild(source.ProfileElement); Parent.ProfileElement.RemoveChild(source.ProfileElement);
Parent.ProfileElement.AddChild(source.ProfileElement, ProfileElement.Order + 1); Parent.ProfileElement.AddChild(source.ProfileElement, ProfileElement.Order + 1);
Parent.Subscribe();
Parent.UpdateProfileElements();
} }
public void RemoveExistingElement(TreeItemViewModel treeItem) public void RemoveExistingElement(TreeItemViewModel treeItem)
@ -116,7 +120,6 @@ namespace Artemis.UI.Screens.ProfileEditor.ProfileTree.TreeItem
throw new ArtemisUIException("Cannot remove a child from a profile element of type " + ProfileElement.GetType().Name); throw new ArtemisUIException("Cannot remove a child from a profile element of type " + ProfileElement.GetType().Name);
ProfileElement.RemoveChild(treeItem.ProfileElement); ProfileElement.RemoveChild(treeItem.ProfileElement);
Children.Remove(treeItem);
treeItem.Parent = null; treeItem.Parent = null;
treeItem.Dispose(); treeItem.Dispose();
} }
@ -127,7 +130,6 @@ namespace Artemis.UI.Screens.ProfileEditor.ProfileTree.TreeItem
throw new ArtemisUIException("Cannot add a child to a profile element of type " + ProfileElement.GetType().Name); throw new ArtemisUIException("Cannot add a child to a profile element of type " + ProfileElement.GetType().Name);
ProfileElement.AddChild(treeItem.ProfileElement); ProfileElement.AddChild(treeItem.ProfileElement);
Children.Add(treeItem);
treeItem.Parent = this; treeItem.Parent = this;
} }
@ -187,11 +189,17 @@ namespace Artemis.UI.Screens.ProfileEditor.ProfileTree.TreeItem
public void UpdateProfileElements() public void UpdateProfileElements()
{ {
// Remove VMs that are no longer a child
var toRemove = Children.Where(c => c.ProfileElement.Parent != ProfileElement).ToList();
foreach (var treeItemViewModel in toRemove)
Children.Remove(treeItemViewModel);
// Order the children // Order the children
var vmsList = Children.OrderBy(v => v.ProfileElement.Order).ToList(); var vmsList = Children.OrderBy(v => v.ProfileElement.Order).ToList();
for (var index = 0; index < vmsList.Count; index++) for (var index = 0; index < vmsList.Count; index++)
{ {
var profileElementViewModel = vmsList[index]; var profileElementViewModel = vmsList[index];
if (Children.IndexOf(profileElementViewModel) != index)
Children.Move(Children.IndexOf(profileElementViewModel), index); Children.Move(Children.IndexOf(profileElementViewModel), index);
} }
@ -218,9 +226,11 @@ namespace Artemis.UI.Screens.ProfileEditor.ProfileTree.TreeItem
return; return;
// Add the new children in one call, prevent extra UI events // Add the new children in one call, prevent extra UI events
Children.AddRange(newChildren);
foreach (var treeItemViewModel in newChildren) foreach (var treeItemViewModel in newChildren)
{
treeItemViewModel.UpdateProfileElements(); treeItemViewModel.UpdateProfileElements();
Children.Add(treeItemViewModel);
}
} }
public void EnableToggled() public void EnableToggled()
@ -235,6 +245,18 @@ namespace Artemis.UI.Screens.ProfileEditor.ProfileTree.TreeItem
} }
} }
private void Subscribe()
{
ProfileElement.ChildAdded += ProfileElementOnChildAdded;
ProfileElement.ChildRemoved += ProfileElementOnChildRemoved;
}
private void Unsubscribe()
{
ProfileElement.ChildAdded -= ProfileElementOnChildAdded;
ProfileElement.ChildRemoved -= ProfileElementOnChildRemoved;
}
private void ProfileElementOnChildRemoved(object sender, EventArgs e) private void ProfileElementOnChildRemoved(object sender, EventArgs e)
{ {
UpdateProfileElements(); UpdateProfileElements();