Example #1
0
        private static void RemoveDialog(this CrystalWindow window, CrystalDialogBase dialog)
        {
            if (window.crystalActiveDialogContainer is null)
            {
                throw new InvalidOperationException("Active dialog container could not be found.");
            }

            if (window.crystalInactiveDialogContainer is null)
            {
                throw new InvalidOperationException("Inactive dialog container could not be found.");
            }

            if (window.crystalActiveDialogContainer.Children.Contains(dialog))
            {
                window.crystalActiveDialogContainer.Children.Remove(dialog); //remove the dialog from the container

                // if there's an inactive dialog, bring it to the front
                var dlg = window.crystalInactiveDialogContainer.Children.OfType <CrystalDialogBase>().LastOrDefault();
                if (dlg != null)
                {
                    window.crystalInactiveDialogContainer.Children.Remove(dlg);
                    window.crystalActiveDialogContainer.Children.Add(dlg);
                }
            }
            else
            {
                window.crystalInactiveDialogContainer.Children.Remove(dialog);
            }

            window.SetValue(CrystalWindow.IsAnyDialogOpenPropertyKey, BooleanBoxes.Box(window.crystalActiveDialogContainer.Children.Count > 0));
        }
Example #2
0
        private static void AddDialog(this CrystalWindow window, CrystalDialogBase dialog)
        {
            if (window.crystalActiveDialogContainer is null)
            {
                throw new InvalidOperationException("Active dialog container could not be found.");
            }

            if (window.crystalInactiveDialogContainer is null)
            {
                throw new InvalidOperationException("Inactive dialog container could not be found.");
            }

            window.StoreFocus();

            // if there's already an active dialog, move to the background
            var activeDialog = window.crystalActiveDialogContainer.Children.OfType <CrystalDialogBase>().SingleOrDefault();

            if (activeDialog != null)
            {
                window.crystalActiveDialogContainer.Children.Remove(activeDialog);
                window.crystalInactiveDialogContainer.Children.Add(activeDialog);
            }

            window.crystalActiveDialogContainer.Children.Add(dialog); //add the dialog to the container}

            window.SetValue(CrystalWindow.IsAnyDialogOpenPropertyKey, BooleanBoxes.TrueBox);
        }
Example #3
0
        /// <summary>
        /// Adds a Crystal Dialog instance to the specified window and makes it visible asynchronously.
        /// If you want to wait until the user has closed the dialog, use <see cref="CrystalDialogBase.WaitUntilUnloadedAsync"/>
        /// <para>You have to close the resulting dialog yourself with <see cref="HideCrystalDialogAsync"/>.</para>
        /// </summary>
        /// <param name="window">The owning window of the dialog.</param>
        /// <param name="dialog">The dialog instance itself.</param>
        /// <param name="settings">An optional pre-defined settings instance.</param>
        /// <returns>A task representing the operation.</returns>
        /// <exception cref="InvalidOperationException">The <paramref name="dialog"/> is already visible in the window.</exception>
        public static Task ShowCrystalDialogAsync(this CrystalWindow window, CrystalDialogBase dialog, CrystalDialogSettings?settings = null)
        {
            if (window is null)
            {
                throw new ArgumentNullException(nameof(window));
            }

            window.Dispatcher.VerifyAccess();

            if (dialog is null)
            {
                throw new ArgumentNullException(nameof(dialog));
            }

            if (window.crystalActiveDialogContainer is null)
            {
                throw new InvalidOperationException("Active dialog container could not be found.");
            }

            if (window.crystalInactiveDialogContainer is null)
            {
                throw new InvalidOperationException("Inactive dialog container could not be found.");
            }

            if (window.crystalActiveDialogContainer.Children.Contains(dialog) || window.crystalInactiveDialogContainer.Children.Contains(dialog))
            {
                throw new InvalidOperationException("The provided dialog is already visible in the specified window.");
            }

            settings ??= (dialog.DialogSettings ?? window.CrystalDialogOptions);

            return(HandleOverlayOnShow(settings, window).ContinueWith(z =>
            {
                return window.Dispatcher.Invoke(new Func <Task>(() =>
                {
                    SetDialogFontSizes(settings, dialog);

                    SizeChangedEventHandler sizeHandler = SetupAndOpenDialog(window, dialog);
                    dialog.SizeChangedHandler = sizeHandler;

                    return dialog.WaitForLoadAsync().ContinueWith(x =>
                    {
                        dialog.OnShown();

                        if (DialogOpened != null)
                        {
                            window.Dispatcher.BeginInvoke(new Action(() => DialogOpened(window, new DialogStateChangedEventArgs())));
                        }
                    });
                }));
            }).Unwrap());
        }
Example #4
0
        private static CrystalWindow SetupExternalDialogWindow(CrystalDialogBase dialog, Window?windowOwner = null)
        {
            var win = CreateExternalWindow(windowOwner ?? Application.Current?.MainWindow);

            // Remove the border on left and right side
            win.BeginInvoke(window =>
            {
                window.SetCurrentValue(Control.BorderThicknessProperty, new Thickness(0, window.BorderThickness.Top, 0, window.BorderThickness.Bottom));
                window.SetCurrentValue(CrystalWindow.ResizeBorderThicknessProperty, new Thickness(0, window.ResizeBorderThickness.Top, 0, window.ResizeBorderThickness.Bottom));
            },
                            DispatcherPriority.Loaded);

            // Get the monitor working area
            var monitorWorkingArea = win.Owner.GetMonitorWorkSize();

            if (monitorWorkingArea != default)
            {
                win.Width     = monitorWorkingArea.Width;
                win.MinHeight = monitorWorkingArea.Height / 4.0;
                win.MaxHeight = monitorWorkingArea.Height;
            }
            else
            {
                win.Width     = SystemParameters.PrimaryScreenWidth;
                win.MinHeight = SystemParameters.PrimaryScreenHeight / 4.0;
                win.MaxHeight = SystemParameters.PrimaryScreenHeight;
            }

            dialog.ParentDialogWindow = win; //THIS IS ONLY, I REPEAT, ONLY SET FOR EXTERNAL DIALOGS!

            win.Content = dialog;

            dialog.HandleThemeChange();

            EventHandler?closedHandler = null;

            closedHandler = (_, _) =>
            {
                win.Closed -= closedHandler;
                dialog.ParentDialogWindow = null;
                win.Content = null;
            };

            win.Closed += closedHandler;

            win.SizeToContent = SizeToContent.Height;

            return(win);
        }
Example #5
0
        /// <summary>
        /// Hides a visible Crystal Dialog instance.
        /// </summary>
        /// <param name="window">The window with the dialog that is visible.</param>
        /// <param name="dialog">The dialog instance to hide.</param>
        /// <param name="settings">An optional pre-defined settings instance.</param>
        /// <returns>A task representing the operation.</returns>
        /// <exception cref="InvalidOperationException">
        /// The <paramref name="dialog"/> is not visible in the window.
        /// This happens if <see cref="ShowCrystalDialogAsync"/> hasn't been called before.
        /// </exception>
        public static Task HideCrystalDialogAsync(this CrystalWindow window, CrystalDialogBase dialog, CrystalDialogSettings?settings = null)
        {
            window.Dispatcher.VerifyAccess();

            if (window.crystalActiveDialogContainer is null)
            {
                throw new InvalidOperationException("Active dialog container could not be found.");
            }

            if (window.crystalInactiveDialogContainer is null)
            {
                throw new InvalidOperationException("Inactive dialog container could not be found.");
            }

            if (!window.crystalActiveDialogContainer.Children.Contains(dialog) && !window.crystalInactiveDialogContainer.Children.Contains(dialog))
            {
                throw new InvalidOperationException("The provided dialog is not visible in the specified window.");
            }

            window.SizeChanged -= dialog.SizeChangedHandler;

            dialog.OnClose();

            Task closingTask = window.Dispatcher.Invoke(new Func <Task>(dialog.WaitForCloseAsync));

            return(closingTask.ContinueWith(a =>
            {
                if (DialogClosed != null)
                {
                    window.Dispatcher.BeginInvoke(new Action(() => DialogClosed(window, new DialogStateChangedEventArgs())));
                }

                return window.Dispatcher.Invoke(new Func <Task>(() =>
                {
                    window.RemoveDialog(dialog);

                    settings ??= (dialog.DialogSettings ?? window.CrystalDialogOptions);
                    return HandleOverlayOnHide(settings, window);
                }));
            }).Unwrap());
        }
Example #6
0
        private static void SetDialogFontSizes(CrystalDialogSettings?settings, CrystalDialogBase dialog)
        {
            if (settings is null)
            {
                return;
            }

            if (!double.IsNaN(settings.DialogTitleFontSize))
            {
                dialog.DialogTitleFontSize = settings.DialogTitleFontSize;
            }

            if (!double.IsNaN(settings.DialogMessageFontSize))
            {
                dialog.DialogMessageFontSize = settings.DialogMessageFontSize;
            }

            if (!double.IsNaN(settings.DialogButtonFontSize))
            {
                dialog.DialogButtonFontSize = settings.DialogButtonFontSize;
            }
        }
Example #7
0
        private static SizeChangedEventHandler SetupAndOpenDialog(CrystalWindow window, CrystalDialogBase dialog)
        {
            dialog.SetValue(Panel.ZIndexProperty, (int)(window.overlayBox?.GetValue(Panel.ZIndexProperty) ?? 0) + 1);

            var fixedMinHeight = dialog.MinHeight > 0;
            var fixedMaxHeight = dialog.MaxHeight is not double.PositiveInfinity && dialog.MaxHeight > 0;

            void CalculateMinAndMaxHeight()
            {
                if (!fixedMinHeight)
                {
                    dialog.SetCurrentValue(FrameworkElement.MinHeightProperty, window.ActualHeight / 4.0);
                }

                if (!fixedMaxHeight)
                {
                    dialog.SetCurrentValue(FrameworkElement.MaxHeightProperty, window.ActualHeight);
                }
                else
                {
                    dialog.SetCurrentValue(FrameworkElement.MinHeightProperty, Math.Min(dialog.MinHeight, dialog.MaxHeight));
                }
            }

            CalculateMinAndMaxHeight();

            void OnWindowSizeChanged(object sender, SizeChangedEventArgs args)
            {
                CalculateMinAndMaxHeight();
            }

            window.SizeChanged += OnWindowSizeChanged;

            window.AddDialog(dialog);

            dialog.OnShown();

            return(OnWindowSizeChanged);
        }