/// <summary> /// Shows a dialog on top of the current ConsoleApp. /// </summary> /// <param name="contentFactory">A callback where you are given a handle that can be used to configure the dialog. /// It also has a method that lets you close the dialog. This callback should return the dialog content.</param> public static async void Show(Func <DialogHandle, ConsolePanel> contentFactory, ConsolePanel parent = null, bool pushPop = true) { parent = parent ?? ConsoleApp.Current.LayoutRoot; using (var dialogLt = new Lifetime()) { if (pushPop) { ConsoleApp.Current.FocusManager.Push(); dialogLt.OnDisposed(ConsoleApp.Current.FocusManager.Pop); } var handle = new DialogHandle(); var content = contentFactory(handle); content.IsVisible = false; var dialogContainer = parent.Add(new BorderPanel(content) { ZIndex = int.MaxValue, BorderColor = handle.BorderColor, Background = content.Background, Width = 1, Height = 1 }).CenterBoth(); await Forward(300, dialogLt, percentage => dialogContainer.Width = Math.Max(1, (int)Math.Round((4 + content.Width) * percentage))); await Forward(200, dialogLt, percentage => dialogContainer.Height = Math.Max(1, (int)Math.Round((2 + content.Height) * percentage))); content.IsVisible = true; await handle.CallerLifetime.AwaitEndOfLifetime(); content.IsVisible = false; await Reverse(150, dialogLt, percentage => dialogContainer.Height = Math.Max(1, (int)Math.Floor((2 + content.Height) * percentage))); await Task.Delay(200); await Reverse(200, dialogLt, percentage => dialogContainer.Width = Math.Max(1, (int)Math.Round((4 + content.Width) * percentage))); dialogContainer.Dispose(); } }
/// <summary> /// Shows a dialog on top of the current ConsoleApp. /// </summary> /// <param name="contentFactory">A callback where you are given a handle that can be used to configure the dialog. /// It also has a method that lets you close the dialog. This callback should return the dialog content.</param> public static async void Show(Func <DialogHandle, Container> contentFactory, AnimatedDialogOptions options = null) { options = options ?? new AnimatedDialogOptions(); options.Parent = options.Parent ?? ConsoleApp.Current.LayoutRoot; using (var dialogLt = new Lifetime()) { var handle = new DialogHandle(); if (options.PushPop) { ConsoleApp.Current.FocusManager.Push(); if (options.AllowEscapeToClose) { ConsoleApp.Current.FocusManager.GlobalKeyHandlers.PushForLifetime(ConsoleKey.Escape, null, () => { handle.CloseDialog(); }, dialogLt); } if (options.AllowEnterToClose) { ConsoleApp.Current.FocusManager.GlobalKeyHandlers.PushForLifetime(ConsoleKey.Enter, null, () => { handle.CloseDialog(); }, dialogLt); } dialogLt.OnDisposed(ConsoleApp.Current.FocusManager.Pop); } var content = contentFactory(handle); content.IsVisible = false; var dialogContainer = options.Parent.Add(new BorderPanel(content) { BorderColor = handle.BorderColor, Background = content.Background, Width = 1, Height = 1 }).CenterBoth(); dialogContainer.ZIndex = options.ZIndex; await Forward(300 *options.SpeedPercentage, dialogLt, percentage => dialogContainer.Width = Math.Max(1, Geometry.Round((4 + content.Width) * percentage))); await Forward(200 *options.SpeedPercentage, dialogLt, percentage => dialogContainer.Height = Math.Max(1, Geometry.Round((2 + content.Height) * percentage))); content.IsVisible = true; await handle.CallerLifetime.AwaitEndOfLifetime(); content.IsVisible = false; await Reverse(150 *options.SpeedPercentage, dialogLt, percentage => dialogContainer.Height = Math.Max(1, (int)Math.Floor((2 + content.Height) * percentage))); await Task.Delay((int)(200 * options.SpeedPercentage)); await Reverse(200 *options.SpeedPercentage, dialogLt, percentage => dialogContainer.Width = Math.Max(1, Geometry.Round((4 + content.Width) * percentage))); dialogContainer.Dispose(); } }