Example #1
0
        /// <summary>
        /// Adds a new batch to the stack.
        /// </summary>
        /// <param name="batch">The batch.</param>
        /// <param name="noInsertIfExecutingOperation">Do not insert record if currently running undo/redo.</param>
        /// <returns><c>true</c> if record inserted; otherwise <c>false</c>.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="batch"/> is <c>null</c>.</exception>
        public bool Add(IMementoBatch batch, bool noInsertIfExecutingOperation = true)
        {
            Argument.IsNotNull("batch", batch);

            if (!IsEnabled)
            {
                return(false);
            }

            if (noInsertIfExecutingOperation && _isUndoingOperation)
            {
                return(false);
            }

            lock (_lock)
            {
                _undoBatches.Insert(0, batch);

                while (_undoBatches.Count > MaximumSupportedBatches)
                {
                    _undoBatches.RemoveLast();
                }
            }

            return(true);
        }
        /// <summary>
        /// Adds a new batch to the stack.
        /// </summary>
        /// <param name="batch">The batch.</param>
        /// <param name="noInsertIfExecutingOperation">Do not insert record if currently running undo/redo.</param>
        /// <returns><c>true</c> if record inserted; otherwise <c>false</c>.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="batch"/> is <c>null</c>.</exception>
        public bool Add(IMementoBatch batch, bool noInsertIfExecutingOperation = true)
        {
            Argument.IsNotNull("batch", batch);

            if (!IsEnabled)
            {
                return(false);
            }

            if (noInsertIfExecutingOperation && _isUndoingOperation)
            {
                return(false);
            }

            lock (_lock)
            {
                _undoBatches.Insert(0, batch);

                while (_undoBatches.Count > MaximumSupportedBatches)
                {
                    _undoBatches.RemoveLast();
                }
            }

            Updated?.Invoke(this, new MementoEventArgs(MementoAction.ChangeRecorded, batch));

            return(true);
        }
        /// <summary>
        /// Executes the next undo operation.
        /// </summary>
        /// <returns><c>true</c> if an undo was executed; otherwise <c>false</c>.</returns>
        public bool Undo()
        {
            if (!CanUndo)
            {
                Log.Info("Cannot undo action");
                return(false);
            }

            Log.Debug("Undoing action");

            IMementoBatch undo = null;

            lock (_lock)
            {
                if (_undoBatches.Count > 0)
                {
                    undo = _undoBatches.First();
                    _undoBatches.RemoveFirst();
                }
            }

            if (undo is null)
            {
                Log.Info("Cannot undo because there is no undo batch for this action");
                return(false);
            }

            _isUndoingOperation = true;

            try
            {
                undo.Undo();

                if (undo.CanRedo)
                {
                    lock (_lock)
                    {
                        _redoBatches.Insert(0, undo);
                        while (_redoBatches.Count > _maximumSupportedOperations)
                        {
                            _redoBatches.RemoveLast();
                        }
                    }
                }

                Updated?.Invoke(this, new MementoEventArgs(MementoAction.Undo, undo));

                Log.Debug("Action is successfully undone");
                return(true);
            }
            finally
            {
                lock (_lock)
                {
                    _isUndoingOperation = false;
                }
            }
        }
Example #4
0
        /// <summary>
        /// Executes the next undo operation.
        /// </summary>
        /// <returns><c>true</c> if an undo was executed; otherwise <c>false</c>.</returns>
        public bool Undo()
        {
            if (CanUndo)
            {
                IMementoBatch undo = null;

                lock (_lock)
                {
                    if (_undoBatches.Count > 0)
                    {
                        _isUndoingOperation = true;

                        undo = _undoBatches.First();
                        _undoBatches.RemoveFirst();
                    }
                }

                if (undo != null)
                {
                    try
                    {
                        undo.Undo();
                        if (undo.CanRedo)
                        {
                            lock (_lock)
                            {
                                _redoBatches.Insert(0, undo);
                                while (_redoBatches.Count > _maximumSupportedOperations)
                                {
                                    _redoBatches.RemoveLast();
                                }
                            }
                        }

                        return(true);
                    }
                    finally
                    {
                        lock (_lock)
                        {
                            _isUndoingOperation = false;
                        }
                    }
                }
            }

            return(false);
        }
Example #5
0
        /// <summary>
        /// Adds a new batch to the stack.
        /// </summary>
        /// <param name="batch">The batch.</param>
        /// <param name="noInsertIfExecutingOperation">Do not insert record if currently running undo/redo.</param>
        /// <returns><c>true</c> if record inserted; otherwise <c>false</c>.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="batch"/> is <c>null</c>.</exception>
        public bool Add(IMementoBatch batch, bool noInsertIfExecutingOperation = true)
        {
            Argument.IsNotNull("batch", batch);

            if (!IsEnabled)
            {
                return false;
            }

            if (noInsertIfExecutingOperation && _isUndoingOperation)
            {
                return false;
            }

            lock (_lock)
            {
                _undoBatches.Insert(0, batch);

                while (_undoBatches.Count > MaximumSupportedBatches)
                {
                    _undoBatches.RemoveLast();
                }
            }

            return true;
        }
Example #6
0
 public MementoEventArgs(MementoAction mementoAction, IMementoBatch targetBatch)
 {
     MementoAction = mementoAction;
     TargetBatch   = targetBatch;
 }