Exemplo n.º 1
0
                public override void CanClose(Action <bool> callback)
                {
                    CloseStrategy.Execute(items, (canClose, closable) =>
                    {
                        if (!canClose && closable.Any())
                        {
                            closable.OfType <IDeactivate>().Apply(x => x.Deactivate(true));
                            items.RemoveRange(closable);
                        }

                        callback(canClose);
                    });
                }
Exemplo n.º 2
0
        /// <summary>
        /// Deactivates the specified item.
        /// </summary>
        /// <param name="item">The item to close.</param>
        /// <param name="close">Indicates whether or not to close the item after deactivating it.</param>
        /// <param name="cancellationToken"></param>
        public override async Task DeactivateItem(T item, bool close, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (item == null || !item.Equals(ActiveItem))
            {
                return;
            }

            var closeResult = await CloseStrategy.Execute(new[] { ActiveItem });

            if (closeResult.CanClose)
            {
                await ChangeActiveItem(default(T), closeResult.CanClose, cancellationToken);
            }
        }
        /// <summary>
        /// Deactivates the specified item.
        /// </summary>
        /// <param name="item">The item to close.</param>
        /// <param name="close">Indicates whether or not to close the item after deactivating it.</param>
        public override void DeactivateItem(T item, bool close)
        {
            if (item == null || !item.Equals(ActiveItem))
            {
                return;
            }

            CloseStrategy.Execute(new[] { ActiveItem }, (canClose, items) => {
                if (canClose)
                {
                    ChangeActiveItem(default(T), close);
                }
            });
        }
Exemplo n.º 4
0
                /// <summary>
                /// Called to check whether or not this instance can close.
                /// </summary>
                public override async Task <bool> CanClose()
                {
                    var closeResult = await CloseStrategy.Execute(items.ToList());

                    if (!closeResult.CanClose && closeResult.Items.Length > 0)
                    {
                        // remove items that are already closed
                        var closeables       = closeResult.Items.OfType <IDeactivate>().ToArray();
                        var closeableResults = closeables.Select(c => c.Deactivate(true));
                        await Task.WhenAll(closeableResults);

                        items.RemoveRange(closeables.Cast <T>());
                    }
                    return(closeResult.CanClose);
                }
                /// <summary>
                /// Closes the specified item.
                /// </summary>
                /// <param name="item">The item to close.</param>
                public override void CloseItem(T item)
                {
                    if (item == null)
                    {
                        return;
                    }

                    CloseStrategy.Execute(new[] { item }, (canClose, closable) =>
                    {
                        if (canClose)
                        {
                            CloseItemCore(item);
                        }
                    });
                }
Exemplo n.º 6
0
        /// <summary>
        /// Activates the specified item.
        /// </summary>
        /// <param name="item">The item to activate.</param>
        public override void ActivateItem(T item)
        {
            if (item != null && item.Equals(ActiveItem))
            {
                OnActivationProcessed(item, true);
                return;
            }

            CloseStrategy.Execute(new[] { ActiveItem }, (canClose, items) =>
            {
                if (canClose)
                {
                    ChangeActiveItem(item, true);
                }
                else
                {
                    OnActivationProcessed(item, false);
                }
            });
        }
Exemplo n.º 7
0
                /// <summary>
                /// Initializes a new instance of the <see cref="Conductor&lt;T&gt;.Collection.AllActive"/> class.
                /// </summary>
                public AllActive()
                {
                    items.CollectionChanged += (s, e) =>
                    {
                        switch (e.Action)
                        {
                        case NotifyCollectionChangedAction.Add:
                            e.NewItems.OfType <IChild>().Apply(x => x.Parent = this);
                            break;

                        case NotifyCollectionChangedAction.Remove:
                            e.OldItems.OfType <IChild>().Apply(x => x.Parent = null);
                            break;

                        case NotifyCollectionChangedAction.Replace:
                            e.NewItems.OfType <IChild>().Apply(x => x.Parent = this);
                            e.OldItems.OfType <IChild>().Apply(x => x.Parent = null);
                            break;

                        case NotifyCollectionChangedAction.Reset:
                            items.OfType <IChild>().Apply(x => x.Parent = this);
                            break;
                        }
                    };

                    CloseGuard = () =>
                    {
                        var tcs = new TaskCompletionSource <bool>();
                        CloseStrategy.Execute(items.ToList(), (canClose, closable) =>
                        {
                            if (!canClose && closable.Any())
                            {
                                closable.OfType <IDeactivate>().Apply(x => x.Deactivate(true));
                                items.RemoveRange(closable);
                            }

                            tcs.SetResult(canClose);
                        });
                        return(tcs.Task);
                    };
                }
                /// <summary>
                /// Deactivates the specified item.
                /// </summary>
                /// <param name="item">The item to close.</param>
                /// <param name="close">Indicates whether or not to close the item after deactivating it.</param>
                /// <param name="cancellationToken"></param>
                public override async Task DeactivateItem(T item, bool close, CancellationToken cancellationToken = default(CancellationToken))
                {
                    if (item == null)
                    {
                        return;
                    }

                    if (!close)
                    {
                        await ScreenExtensions.TryDeactivate(item, false, cancellationToken);
                    }
                    else
                    {
                        var closeResult = await CloseStrategy.Execute(new[] { item });

                        if (closeResult.CanClose)
                        {
                            await CloseItemCore(item);
                        }
                    }
                }
                /// <summary>
                /// Deactivates the specified item.
                /// </summary>
                /// <param name="item">The item to close.</param>
                /// <param name="close">Indicates whether or not to close the item after deactivating it.</param>
                public override void DeactivateItem(T item, bool close)
                {
                    if (item == null)
                    {
                        return;
                    }

                    if (!close)
                    {
                        ScreenExtensions.TryDeactivate(item, false);
                    }
                    else
                    {
                        CloseStrategy.Execute(new[] { item }, (canClose, closable) => {
                            if (canClose)
                            {
                                CloseItemCore(item);
                            }
                        });
                    }
                }
Exemplo n.º 10
0
        /// <summary>
        /// Activates the specified item.
        /// </summary>
        /// <param name="item">The item to activate.</param>
        public override void ActivateItem(object item)
        {
            if (item != null && item.Equals(ActiveItem))
            {
                if (IsActive)
                {
                    ScreenExtensions.TryActivate(item);
                    OnActivationProcessed(item, true);
                }

                return;
            }

            bool shouldClosePreviosly = true;
            var  screen = ActiveItem as PersitableScreen;

            if (screen != null)
            {
                shouldClosePreviosly = screen.ShouldClose;
            }

            if (shouldClosePreviosly)
            {
                CloseStrategy.Execute(new[] { ActiveItem }, (canClose, items) =>
                {
                    if (canClose)
                    {
                        ChangeActiveItem(item, true);
                    }
                    else
                    {
                        OnActivationProcessed(item, false);
                    }
                });
            }
            else
            {
                ChangeActiveItem(item, false);
            }
        }
Exemplo n.º 11
0
        /// <summary>
        ///     Activates the specified item.
        /// </summary>
        /// <param name="item">The item to activate.</param>
        public override void ActivateItem(T item)
        {
            if ((item != null) && item.Equals(ActiveItem))
            {
                if (IsActive)
                {
                    ScreenExtensions.TryActivate(item);
                    OnActivationProcessed(item, true);
                }
                return;
            }

            CloseStrategy.Execute(new[] { ActiveItem }, (canClose, items) => {
                if (canClose)
                {
                    ChangeActiveItem(item, true);
                }
                else
                {
                    OnActivationProcessed(item, false);
                }
            });
        }
                /// <summary>
                /// Called to check whether or not this instance can close.
                /// </summary>
                public override async Task <bool> CanClose()
                {
                    var closeResult = await CloseStrategy.Execute(items.ToList());

                    if (!closeResult.CanClose && closeResult.Items.Any())
                    {
                        var closables = closeResult.Items;
                        if (closables.Contains(ActiveItem))
                        {
                            var list = items.ToList();
                            var next = ActiveItem;
                            do
                            {
                                var previous = next;
                                next = DetermineNextItemToActivate(list, list.IndexOf(previous));
                                list.Remove(previous);
                            } while (closables.Contains(next));

                            var previousActive = ActiveItem;
                            await ChangeActiveItem(next, true);

                            items.Remove(previousActive);

                            var stillToClose = closeResult.Items.ToList();
                            stillToClose.Remove(previousActive);
                            closables = stillToClose.ToArray();
                        }

                        var closeableItems = closeResult.Items.OfType <IDeactivate>().ToArray();
                        var closeableTasks = closeableItems.Select(d => d.Deactivate(true));
                        await Task.WhenAll(closeableTasks);

                        items.RemoveRange((IEnumerable <T>)closeableItems);
                    }
                    return(closeResult.CanClose);
                }
Exemplo n.º 13
0
        /// <summary>
        /// Activates the specified item.
        /// </summary>
        /// <param name="item">The item to activate.</param>
        /// <param name="cancellationToken"></param>
        public override async Task ActivateItem(T item, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (item != null && item.Equals(ActiveItem))
            {
                if (IsActive)
                {
                    await ScreenExtensions.TryActivate(item, cancellationToken);

                    OnActivationProcessed(item, true);
                }
                return;
            }

            var closeResult = await CloseStrategy.Execute(new[] { ActiveItem });

            if (closeResult.CanClose)
            {
                await ChangeActiveItem(item, true, cancellationToken);
            }
            else
            {
                OnActivationProcessed(item, false);
            }
        }
Exemplo n.º 14
0
 /// <summary>
 /// Called to check whether or not this instance can close.
 /// </summary>
 /// <param name="callback">The implementor calls this action with the result of the close check.</param>
 public override void CanClose(Action <bool> callback)
 {
     CloseStrategy.Execute(new[] { ActiveItem }, (canClose, items) => callback(canClose));
 }
Exemplo n.º 15
0
                /// <summary>
                /// Initializes a new instance of the <see cref="Conductor&lt;T&gt;.Collection.OneActive"/> class.
                /// </summary>
                public OneActive()
                {
                    items.CollectionChanged += (s, e) =>
                    {
                        switch (e.Action)
                        {
                        case NotifyCollectionChangedAction.Add:
                            e.NewItems.OfType <IChild>().Apply(x => x.Parent = this);
                            break;

                        case NotifyCollectionChangedAction.Remove:
                            e.OldItems.OfType <IChild>().Apply(x => x.Parent = null);
                            break;

                        case NotifyCollectionChangedAction.Replace:
                            e.NewItems.OfType <IChild>().Apply(x => x.Parent = this);
                            e.OldItems.OfType <IChild>().Apply(x => x.Parent = null);
                            break;

                        case NotifyCollectionChangedAction.Reset:
                            items.OfType <IChild>().Apply(x => x.Parent = this);
                            break;
                        }
                    };

                    CloseGuard = () =>
                    {
                        var tcs = new TaskCompletionSource <bool>();
                        CloseStrategy.Execute(items.ToList(), (canClose, closable) =>
                        {
                            if (!canClose && closable.Any())
                            {
                                if (closable.Contains(ActiveItem))
                                {
                                    var list = items.ToList();
                                    var next = ActiveItem;
                                    do
                                    {
                                        var previous = next;
                                        next         = DetermineNextItemToActivate(list, list.IndexOf(previous));
                                        list.Remove(previous);
                                    } while (closable.Contains(next));

                                    var previousActive = ActiveItem;
                                    ChangeActiveItem(next, true);
                                    items.Remove(previousActive);

                                    var stillToClose = closable.ToList();
                                    stillToClose.Remove(previousActive);
                                    closable = stillToClose;
                                }

                                closable.OfType <IDeactivate>().Apply(x => x.Deactivate(true));
                                items.RemoveRange(closable);
                            }

                            tcs.SetResult(canClose);
                        });
                        return(tcs.Task);
                    };
                }
Exemplo n.º 16
0
 /// <summary>
 /// Called to check whether or not this instance can close.
 /// </summary>
 /// <param name="callback">The implementer calls this action with the result of the close check.</param>
 public override void CanClose(Action <bool> callback)
 {
     CloseStrategy.Execute(items, (canClose, closable) => callback(canClose));
 }
Exemplo n.º 17
0
        /// <summary>
        /// Called to check whether or not this instance can close.
        /// </summary>
        public override async Task <bool> CanClose()
        {
            var closeResult = await CloseStrategy.Execute(new[] { ActiveItem });

            return(closeResult.CanClose);
        }