Exemplo n.º 1
0
            public WindowConductor(Window window, object viewModel)
            {
                this.window    = window;
                this.viewModel = viewModel;

                // They won't be able to request a close unless they implement IChild anyway...
                var viewModelAsChild = this.viewModel as IChild;

                if (viewModelAsChild != null)
                {
                    viewModelAsChild.Parent = this;
                }

                ScreenExtensions.TryActivate(this.viewModel);

                var viewModelAsScreenState = this.viewModel as IScreenState;

                if (viewModelAsScreenState != null)
                {
                    window.StateChanged += this.WindowStateChanged;
                    window.Closed       += this.WindowClosed;
                }

                if (this.viewModel is IGuardClose)
                {
                    window.Closing += this.WindowClosing;
                }
            }
Exemplo n.º 2
0
            /// <summary>
            /// Close was requested by the child
            /// </summary>
            /// <param name="item">Item to close</param>
            /// <param name="dialogResult">DialogResult to close with, if it's a dialog</param>
            async void IChildDelegate.CloseItem(object item, bool?dialogResult)
            {
                if (item != this.viewModel)
                {
                    logger.Warn("IChildDelegate.CloseItem called with item {0} which is _not_ our ViewModel {1}", item, this.viewModel);
                    return;
                }

                var guardClose = this.viewModel as IGuardClose;

                if (guardClose != null && !await guardClose.CanCloseAsync())
                {
                    logger.Info("Close of ViewModel {0} cancelled because CanCloseAsync returned false", this.viewModel);
                    return;
                }

                logger.Info("ViewModel {0} close requested with DialogResult {1} because it called RequestClose", this.viewModel, dialogResult);

                this.window.StateChanged -= this.WindowStateChanged;
                this.window.Closed       -= this.WindowClosed;
                this.window.Closing      -= this.WindowClosing;

                // Need to call this after unregistering the event handlers, as it causes the window
                // to be closed
                if (dialogResult != null)
                {
                    this.window.DialogResult = dialogResult;
                }

                ScreenExtensions.TryClose(this.viewModel);

                this.window.Close();
            }
Exemplo n.º 3
0
 /// <summary>
 /// Deactive the given item
 /// </summary>
 /// <param name="item">Item to deactivate</param>
 public override void DeactivateItem(T item)
 {
     if (item != null && item.Equals(this.ActiveItem))
     {
         ScreenExtensions.TryDeactivate(this.ActiveItem);
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Switch the active item to the given item
        /// </summary>
        /// <param name="newItem">New item to activate</param>
        /// <param name="closePrevious">Whether the previously-active item should be closed</param>
        protected virtual void ChangeActiveItem(T newItem, bool closePrevious)
        {
            ScreenExtensions.TryDeactivate(this.ActiveItem);
            if (closePrevious)
            {
                this.CloseAndCleanUp(this.ActiveItem, this.DisposeChildren);
            }

            if (newItem != null)
            {
                this.EnsureItem(newItem);

                if (this.IsActive)
                {
                    ScreenExtensions.TryActivate(newItem);
                }
                else
                {
                    ScreenExtensions.TryDeactivate(newItem);
                }
            }

            this._activeItem = newItem;
            this.NotifyOfPropertyChange("ActiveItem");
        }
Exemplo n.º 5
0
            private void WindowClosed(object sender, EventArgs e)
            {
                // Logging was done in the Closing handler

                this.window.StateChanged -= this.WindowStateChanged;
                this.window.Closed       -= this.WindowClosed;
                this.window.Closing      -= this.WindowClosing; // Not sure this is required

                ScreenExtensions.TryClose(this.viewModel);
            }
Exemplo n.º 6
0
 /// <summary>
 /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
 /// </summary>
 public override void Dispose()
 {
     // Dispose the container last
     base.Dispose();
     // Don't create the root ViewModel if it doesn't already exist...
     ScreenExtensions.TryDispose(this._rootViewModel);
     if (this.Container != null)
     {
         this.Container.Dispose();
     }
 }
Exemplo n.º 7
0
 /// <summary>
 /// Activate the given item and set it as the ActiveItem, deactivating the previous ActiveItem
 /// </summary>
 /// <param name="item">Item to deactivate</param>
 public override void ActivateItem(T item)
 {
     if (item != null && item.Equals(this.ActiveItem))
     {
         if (this.IsActive)
         {
             ScreenExtensions.TryActivate(this.ActiveItem);
         }
     }
     else
     {
         this.ChangeActiveItem(item, false);
     }
 }
Exemplo n.º 8
0
            private void WindowStateChanged(object sender, EventArgs e)
            {
                switch (this.window.WindowState)
                {
                case WindowState.Maximized:
                case WindowState.Normal:
                    logger.Info("Window {0} maximized/restored: activating", this.window);
                    ScreenExtensions.TryActivate(this.viewModel);
                    break;

                case WindowState.Minimized:
                    logger.Info("Window {0} minimized: deactivating", this.window);
                    ScreenExtensions.TryDeactivate(this.viewModel);
                    break;
                }
            }
Exemplo n.º 9
0
        /// <summary>
        /// Activate the given item, discarding the previous ActiveItem
        /// </summary>
        /// <param name="item">Item to active</param>
        public override async void ActivateItem(T item)
        {
            if (item != null && item.Equals(this.ActiveItem))
            {
                if (this.IsActive)
                {
                    ScreenExtensions.TryActivate(item);
                }
            }
            else if (await this.CanCloseItem(this.ActiveItem))
            {
                // CanCloseItem is null-safe

                this.ChangeActiveItem(item, true);
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Close an item, and clear its parent if it's set to the current parent
        /// </summary>
        /// <typeparam name="T">Type of conductor</typeparam>
        /// <param name="parent">Parent</param>
        /// <param name="item">Item to close and clean up</param>
        /// <param name="dispose">True to dispose children as well as close them</param>
        public static void CloseAndCleanUp <T>(this IConductor <T> parent, T item, bool dispose)
        {
            ScreenExtensions.TryClose(item);

            var itemAsChild = item as IChild;

            if (itemAsChild != null && itemAsChild.Parent == parent)
            {
                itemAsChild.Parent = null;
            }

            if (dispose)
            {
                ScreenExtensions.TryDispose(item);
            }
        }
Exemplo n.º 11
0
                /// <summary>
                /// Deactive the given item, and choose another item to set as the ActiveItem
                /// </summary>
                /// <param name="item">Item to deactivate</param>
                public override void DeactivateItem(T item)
                {
                    if (item == null)
                    {
                        return;
                    }

                    if (item.Equals(this.ActiveItem))
                    {
                        var nextItem = this.DetermineNextItemToActivate(item);
                        this.ChangeActiveItem(nextItem, false);
                    }
                    else
                    {
                        ScreenExtensions.TryDeactivate(item);
                    }
                }
Exemplo n.º 12
0
                /// <summary>
                /// Activate the given item, and add it to the Items collection
                /// </summary>
                /// <param name="item">Item to activate</param>
                public override void ActivateItem(T item)
                {
                    if (item == null)
                    {
                        return;
                    }

                    this.EnsureItem(item);

                    if (this.IsActive)
                    {
                        ScreenExtensions.TryActivate(item);
                    }
                    else
                    {
                        ScreenExtensions.TryDeactivate(item);
                    }
                }
Exemplo n.º 13
0
        /// <summary>
        /// For each item in a list, set the parent to the current conductor
        /// </summary>
        /// <typeparam name="T">Type of conductor</typeparam>
        /// <param name="parent">Parent to set the items' parent to</param>
        /// <param name="items">Items to manipulate</param>
        /// <param name="active">True to active the item, false to deactive it</param>
        public static void SetParentAndSetActive <T>(this IConductor <T> parent, IEnumerable items, bool active)
        {
            foreach (var item in items)
            {
                var itemAsChild = item as IChild;
                if (itemAsChild != null)
                {
                    itemAsChild.Parent = parent;
                }

                if (active)
                {
                    ScreenExtensions.TryActivate(item);
                }
                else
                {
                    ScreenExtensions.TryDeactivate(item);
                }
            }
        }
Exemplo n.º 14
0
 /// <summary>
 /// Deactive the given item
 /// </summary>
 /// <param name="item">Item to deactivate</param>
 public override void DeactivateItem(T item)
 {
     ScreenExtensions.TryDeactivate(item);
 }
Exemplo n.º 15
0
 /// <summary>
 /// When we're deactivated, also deactivate the ActiveItem
 /// </summary>
 protected override void OnDeactivate()
 {
     ScreenExtensions.TryDeactivate(this.ActiveItem);
 }