<commit_before>namespace Macabresoft.Macabre2D.UI.Common.ViewModels.Scene {
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Reactive;
    using System.Reflection;
    using System.Runtime.Serialization;
    using System.Threading.Tasks;
    using System.Windows.Input;
    using Avalonia.Threading;
    using Macabresoft.Macabre2D.Framework;
    using Macabresoft.Macabre2D.UI.Common.Models;
    using Macabresoft.Macabre2D.UI.Common.Services;
    using ReactiveUI;
    using Unity;

    /// <summary>
    /// A view model for the scene tree.
    /// </summary>
    public class SceneTreeViewModel : ViewModelBase {
        private readonly IDialogService _dialogService;
        private readonly ISceneService _sceneService;
        private readonly ObservableCollection<IEntity> _treeRoot = new();
        private readonly IUndoService _undoService;

        /// <summary>
        /// Initializes a new instance of the <see cref="SceneTreeViewModel" /> class.
        /// </summary>
        /// <remarks>This constructor only exists for design time XAML.</remarks>
        public SceneTreeViewModel() {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SceneTreeViewModel" /> class.
        /// </summary>
        /// <param name="dialogService">The dialog service.</param>
        /// <param name="sceneService">The scene service.</param>
        /// <param name="entitySelectionService">The selection service.</param>
        /// <param name="undoService">The undo service.</param>
        [InjectionConstructor]
        public SceneTreeViewModel(IDialogService dialogService, ISceneService sceneService, IEntitySelectionService entitySelectionService, IUndoService undoService) {
            this._dialogService = dialogService;
            this._sceneService = sceneService;
            this.EntitySelectionService = entitySelectionService;
            this._undoService = undoService;
            this.ResetRoot();
            this._sceneService.PropertyChanged += this.SceneService_PropertyChanged;

            this.AddEntityCommand = ReactiveCommand.CreateFromTask<IEntity>(
                async x => await this.AddEntity(x),
                this.EntitySelectionService.WhenAny(x => x.SelectedEntity, y => y.Value != null));

            this.RemoveEntityCommand = ReactiveCommand.Create<IEntity, Unit>(
                this.RemoveEntity,
                this.EntitySelectionService.WhenAny(x => x.SelectedEntity, y => y.Value != null && y.Value.Parent != y.Value));
            
            this.RenameCommand = ReactiveCommand.Create<string, Unit>(
                this.RenameEntity,
                this.EntitySelectionService.WhenAny(x => x.SelectedEntity, y => y.Value != null));
        }

        /// <summary>
        /// Gets a command for renaming an entity.
        /// </summary>
        public ICommand RenameCommand { get; }
        
        /// <summary>
        /// Gets a command to add an entity.
        /// </summary>
        public ICommand AddEntityCommand { get; }

        /// <summary>
        /// Gets the selection service.
        /// </summary>
        public IEntitySelectionService EntitySelectionService { get; }

        /// <summary>
        /// Gets a command to remove an entity.
        /// </summary>
        public ICommand RemoveEntityCommand { get; }

        /// <summary>
        /// Gets the root of the scene tree.
        /// </summary>
        public IReadOnlyCollection<IEntity> Root => this._treeRoot;

        /// <summary>
        /// Moves the source entity to be a child of the target entity.
        /// </summary>
        /// <param name="sourceEntity">The source entity.</param>
        /// <param name="targetEntity">The target entity.</param>
        public void MoveEntity(IEntity sourceEntity, IEntity targetEntity) {
            if (CanMoveEntity(sourceEntity, targetEntity)) {
                var originalParent = sourceEntity.Parent;
                var originalHasChanges = this._sceneService.HasChanges;
                this._undoService.Do(() => {
                    targetEntity.AddChild(sourceEntity);
                    this._sceneService.HasChanges = true;
                }, () => {
                    originalParent.AddChild(sourceEntity);
                    this._sceneService.HasChanges = originalHasChanges;
                });
            }
        }
        private static bool CanMoveEntity(IEntity sourceEntity, IEntity targetEntity) {
            return sourceEntity != null && targetEntity != null && sourceEntity != targetEntity && !sourceEntity.IsDescendentOf(targetEntity);
        }

        private Unit RenameEntity(string updatedName) {
            if (this.EntitySelectionService.SelectedEntity is IEntity entity && entity.Name != updatedName) {
                var originalHasChanges = this._sceneService.HasChanges;
                var originalName = entity.Name;
                this._undoService.Do(
                    () => {
                        Dispatcher.UIThread.Post(() => {
                            entity.Name = updatedName;
                            this._sceneService.HasChanges = true;
                        });
                    }, () => {
                        Dispatcher.UIThread.Post(() => {
                            entity.Name = originalName;<commit_msg>Fixed logic for drag drop on entities.<commit_after>namespace Macabresoft.Macabre2D.UI.Common.ViewModels.Scene {
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Reactive;
    using System.Reflection;
    using System.Runtime.Serialization;
    using System.Threading.Tasks;
    using System.Windows.Input;
    using Avalonia.Threading;
    using Macabresoft.Macabre2D.Framework;
    using Macabresoft.Macabre2D.UI.Common.Models;
    using Macabresoft.Macabre2D.UI.Common.Services;
    using ReactiveUI;
    using Unity;

    /// <summary>
    /// A view model for the scene tree.
    /// </summary>
    public class SceneTreeViewModel : ViewModelBase {
        private readonly IDialogService _dialogService;
        private readonly ISceneService _sceneService;
        private readonly ObservableCollection<IEntity> _treeRoot = new();
        private readonly IUndoService _undoService;

        /// <summary>
        /// Initializes a new instance of the <see cref="SceneTreeViewModel" /> class.
        /// </summary>
        /// <remarks>This constructor only exists for design time XAML.</remarks>
        public SceneTreeViewModel() {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SceneTreeViewModel" /> class.
        /// </summary>
        /// <param name="dialogService">The dialog service.</param>
        /// <param name="sceneService">The scene service.</param>
        /// <param name="entitySelectionService">The selection service.</param>
        /// <param name="undoService">The undo service.</param>
        [InjectionConstructor]
        public SceneTreeViewModel(IDialogService dialogService, ISceneService sceneService, IEntitySelectionService entitySelectionService, IUndoService undoService) {
            this._dialogService = dialogService;
            this._sceneService = sceneService;
            this.EntitySelectionService = entitySelectionService;
            this._undoService = undoService;
            this.ResetRoot();
            this._sceneService.PropertyChanged += this.SceneService_PropertyChanged;

            this.AddEntityCommand = ReactiveCommand.CreateFromTask<IEntity>(
                async x => await this.AddEntity(x),
                this.EntitySelectionService.WhenAny(x => x.SelectedEntity, y => y.Value != null));

            this.RemoveEntityCommand = ReactiveCommand.Create<IEntity, Unit>(
                this.RemoveEntity,
                this.EntitySelectionService.WhenAny(x => x.SelectedEntity, y => y.Value != null && y.Value.Parent != y.Value));
            
            this.RenameCommand = ReactiveCommand.Create<string, Unit>(
                this.RenameEntity,
                this.EntitySelectionService.WhenAny(x => x.SelectedEntity, y => y.Value != null));
        }

        /// <summary>
        /// Gets a command for renaming an entity.
        /// </summary>
        public ICommand RenameCommand { get; }
        
        /// <summary>
        /// Gets a command to add an entity.
        /// </summary>
        public ICommand AddEntityCommand { get; }

        /// <summary>
        /// Gets the selection service.
        /// </summary>
        public IEntitySelectionService EntitySelectionService { get; }

        /// <summary>
        /// Gets a command to remove an entity.
        /// </summary>
        public ICommand RemoveEntityCommand { get; }

        /// <summary>
        /// Gets the root of the scene tree.
        /// </summary>
        public IReadOnlyCollection<IEntity> Root => this._treeRoot;

        /// <summary>
        /// Moves the source entity to be a child of the target entity.
        /// </summary>
        /// <param name="sourceEntity">The source entity.</param>
        /// <param name="targetEntity">The target entity.</param>
        public void MoveEntity(IEntity sourceEntity, IEntity targetEntity) {
            if (CanMoveEntity(sourceEntity, targetEntity)) {
                var originalParent = sourceEntity.Parent;
                var originalHasChanges = this._sceneService.HasChanges;
                this._undoService.Do(() => {
                    targetEntity.AddChild(sourceEntity);
                    this._sceneService.HasChanges = true;
                }, () => {
                    originalParent.AddChild(sourceEntity);
                    this._sceneService.HasChanges = originalHasChanges;
                });
            }
        }
        private static bool CanMoveEntity(IEntity sourceEntity, IEntity targetEntity) {
            return sourceEntity != null && 
                   targetEntity != null && 
                   sourceEntity != targetEntity && 
                   !targetEntity.IsDescendentOf(sourceEntity);
        }

        private Unit RenameEntity(string updatedName) {
            if (this.EntitySelectionService.SelectedEntity is IEntity entity && entity.Name != updatedName) {
                var originalHasChanges = this._sceneService.HasChanges;
                var originalName = entity.Name;
                this._undoService.Do(
                    () => {
                        Dispatcher.UIThread.Post(() => {
                            entity.Name = updatedName;
                            this._sceneService.HasChanges = true;
                        });
                    }, () => {
                        Dispatcher.UIThread.Post(() => {
                            entity.Name = originalName;
{'fraction_non_alphanumeric': 0.10247285336253308, 'fraction_numerical': 0.0007299936125558901, 'mean_word_length': 1.962162162162162, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 109, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5443710', 'n_tokens_mistral': 2691, 'n_tokens_neox': 2591, 'n_words': 623}