public WindowMenuItemViewModel(WindowDefinition definition)
        {
            var windowManager = Ioc.Get <IWindowManager>();

            this.Definition = definition;

            if (definition.CanOpen == null)
            {
                Observable.Never <bool>().StartWith(true).ToProperty(this, x => x.CanOpen, out this.canOpen);
            }
            else
            {
                definition.CanOpen().ToProperty(this, x => x.CanOpen, out this.canOpen);
            }

            windowManager.WindowOpened += (o, e) =>
            {
                if (e.Value == definition.ViewModelType)
                {
                    this.IsOpen = true;
                }
            };
            windowManager.WindowClosed += (o, e) =>
            {
                if (e.Value == definition.ViewModelType)
                {
                    this.IsOpen = false;
                }
            };

            this.Command = windowManager.CreateOpenCommand(definition.ViewModelType);
        }
Beispiel #2
0
        /// <summary>
        /// Gets a tracked window definition from the given viewmodel.
        /// </summary>
        /// <param name="viewModel">The window viewmodel.</param>
        /// <returns>The tracked window definition for the viewmodel.</returns>
        private static WindowDefinition GetWindowDefinition(object viewModel)
        {
            Type             viewModelType = viewModel.GetType();
            WindowDefinition definition    = Definitions.FirstOrDefault(d => d.ViewModelType == viewModelType);

            if (definition != null)
            {
                return(definition);
            }

            return(null);
        }
Beispiel #3
0
        /// <summary>
        /// Prepares a window for opening.
        /// </summary>
        /// <param name="viewModel">The window viewmodel to use.</param>
        /// <param name="ownerViewModel">The owner viewmodel.</param>
        /// <param name="userInitiated">True if the user specifically asked this window to open,
        /// false if it being re-opened automatically on app start.</param>
        /// <param name="isDialog">True if the window is being opened as a dialog, false if it's being opened
        /// as a window.</param>
        /// <returns>The prepared window.</returns>
        private Window PrepareWindowForOpen(object viewModel, object ownerViewModel, bool userInitiated, bool isDialog)
        {
            Window windowToOpen = CreateWindow(viewModel.GetType());

            if (ownerViewModel != null)
            {
                windowToOpen.Owner = this.openWindows[ownerViewModel];
            }

            windowToOpen.DataContext = viewModel;
            windowToOpen.Closing    += this.OnClosingHandler;

            WindowDefinition windowDefinition = GetWindowDefinition(viewModel);

            if (windowDefinition != null && !windowDefinition.ManualPlacementRestore && windowDefinition.PlacementConfigKey != null)
            {
                windowToOpen.SourceInitialized += (o, e) =>
                {
                    string placementJson = Config.Get <string>(windowDefinition.PlacementConfigKey);
                    if (isDialog)
                    {
                        windowToOpen.SetPlacementJson(placementJson);
                    }
                    else
                    {
                        windowToOpen.PlaceDynamic(placementJson);
                    }
                };
            }

            this.openWindows.Add(viewModel, windowToOpen);

            if (userInitiated)
            {
                if (windowDefinition?.IsOpenConfigKey != null)
                {
                    Config.Set(windowDefinition.IsOpenConfigKey, true);
                }
            }

            this.WindowOpened?.Invoke(this, new EventArgs <Type>(viewModel.GetType()));

            if (!isDialog)
            {
                windowToOpen.RegisterGlobalHotkeys();
                windowToOpen.AllowDrop        = true;
                windowToOpen.PreviewDragOver += OnPreviewDragOver;
                windowToOpen.PreviewDrop     += OnPreviewDrop;
            }

            return(windowToOpen);
        }
Beispiel #4
0
        /// <summary>
        /// Fires when a window is closing.
        /// </summary>
        /// <param name="window">The window.</param>
        /// <param name="userInitiated">True if the close was initated by the user, false if this
        /// was initiated by the system as part of app shutdown.</param>
        private void OnClosing(Window window, bool userInitiated)
        {
            object viewModel      = window.DataContext;
            var    closableWindow = viewModel as IClosableWindow;

            if (closableWindow != null)
            {
                var dialogVM = closableWindow;
                dialogVM.OnClosing();
            }

            WindowDefinition windowDefinition = GetWindowDefinition(viewModel);

            if (windowDefinition != null)
            {
                if (windowDefinition.PlacementConfigKey != null)
                {
                    Config.Set(windowDefinition.PlacementConfigKey, window.GetPlacementJson());
                }

                if (userInitiated && windowDefinition.IsOpenConfigKey != null)
                {
                    Config.Set(windowDefinition.IsOpenConfigKey, false);
                }
            }

            this.openWindows.Remove(viewModel);

            if (userInitiated && window.Owner != null)
            {
                window.Owner.Activate();
            }

            var localWindowClosed = this.WindowClosed;

            if (localWindowClosed != null)
            {
                localWindowClosed(this, new EventArgs <Type>(viewModel.GetType()));
            }
        }
Beispiel #5
0
        /// <summary>
        /// Fires when a window is closing.
        /// </summary>
        /// <param name="window">The window.</param>
        /// <param name="userInitiated">True if the close was initated by the user, false if this
        /// was initiated by the system as part of app shutdown.</param>
        /// <returns>True if the window closed, false if it was stopped by the user.</returns>
        private bool OnClosing(Window window, bool userInitiated)
        {
            object viewModel      = window.DataContext;
            var    closableWindow = viewModel as IClosableWindow;

            if (closableWindow != null)
            {
                if (!closableWindow.OnClosing())
                {
                    return(false);
                }
            }

            WindowDefinition windowDefinition = GetWindowDefinition(viewModel);

            if (windowDefinition != null)
            {
                if (windowDefinition.PlacementConfigKey != null)
                {
                    Config.Set(windowDefinition.PlacementConfigKey, window.GetPlacementJson());
                }

                if (userInitiated && windowDefinition.IsOpenConfigKey != null)
                {
                    Config.Set(windowDefinition.IsOpenConfigKey, false);
                }
            }

            this.openWindows.Remove(viewModel);

            if (userInitiated)
            {
                window.Owner?.Activate();
            }

            this.WindowClosed?.Invoke(this, new EventArgs <Type>(viewModel.GetType()));

            return(true);
        }