internal override ConsoleControl GetContent() { ConsolePanel content = new ConsolePanel(); content.Width = ConsoleApp.Current.LayoutRoot.Width / 2; content.Height = ConsoleApp.Current.LayoutRoot.Height / 2; Label messageLabel = content.Add(new Label() { Text = Message, X = 2, Y = 2 }); TextBox = TextBox ?? new TextBox() { Foreground = ConsoleColor.Black, Background = ConsoleColor.White }; content.Add(TextBox).CenterHorizontally(); TextBox.Y = 4; content.SynchronizeForLifetime(nameof(content.Bounds), () => { TextBox.Width = content.Width - 4; }, content); TextBox.KeyInputReceived.SubscribeForLifetime((k) => { if (k.Key == ConsoleKey.Enter) { Dialog.Dismiss(); } }, TextBox); TextBox.AddedToVisualTree.SubscribeOnce(() => TextBox.Application.QueueAction(() => TextBox.TryFocus())); return(content); }
public static void ShowRichTextInput(ConsoleString message, Action <ConsoleString> resultCallback, Action cancelCallback = null, bool allowEscapeToCancel = true, int maxHeight = 12, TextBox inputBox = null, ConsoleString initialValue = null) { ConsoleApp.AssertAppThread(); if (ConsoleApp.Current == null) { throw new InvalidOperationException("There is no console app running"); } ConsolePanel content = new ConsolePanel(); content.Width = ConsoleApp.Current.LayoutRoot.Width / 2; content.Height = ConsoleApp.Current.LayoutRoot.Height / 2; var dialog = new Dialog(content); dialog.MaxHeight = maxHeight; dialog.AllowEscapeToCancel = allowEscapeToCancel; dialog.Cancelled.SubscribeForLifetime(() => { if (cancelCallback != null) { cancelCallback(); } }, dialog); Label messageLabel = content.Add(new Label() { Text = message, X = 2, Y = 2 }); if (inputBox == null) { inputBox = new TextBox() { Foreground = ConsoleColor.Black, Background = ConsoleColor.White }; } if (initialValue != null) { inputBox.Value = initialValue; } content.Add(inputBox).CenterHorizontally(); inputBox.Y = 4; content.SynchronizeForLifetime(nameof(Bounds), () => { inputBox.Width = content.Width - 4; }, content); inputBox.KeyInputReceived.SubscribeForLifetime((k) => { if (k.Key == ConsoleKey.Enter) { ConsoleApp.Current.LayoutRoot.Controls.Remove(dialog); resultCallback(inputBox.Value); } }, inputBox); ConsoleApp.Current.LayoutRoot.Controls.Add(dialog); inputBox.TryFocus(); }
public static void ShowRichTextInput(ConsoleString message, Action<ConsoleString> resultCallback, Action cancelCallback = null, bool allowEscapeToCancel = true, int maxHeight = 12, TextBox inputBox = null) { if (ConsoleApp.Current == null) { throw new InvalidOperationException("There is no console app running"); } ConsolePanel content = new ConsolePanel(); content.Width = ConsoleApp.Current.LayoutRoot.Width / 2; content.Height = ConsoleApp.Current.LayoutRoot.Height / 2; var dialog = new Dialog(content); dialog.MaxHeight = maxHeight; dialog.AllowEscapeToCancel = allowEscapeToCancel; dialog.Cancelled.SubscribeForLifetime(() => { if (cancelCallback != null) cancelCallback(); }, dialog.LifetimeManager); Label messageLabel = content.Add(new Label() { Text = message, X = 2, Y = 2 }); if (inputBox == null) { inputBox = new TextBox() { Foreground = ConsoleColor.Black, Background = ConsoleColor.White }; } content.Add(inputBox).CenterHorizontally(); inputBox.Y = 4; content.SynchronizeForLifetime(nameof(Bounds), () => { inputBox.Width = content.Width - 4; }, content.LifetimeManager); inputBox.KeyInputReceived.SubscribeForLifetime((k) => { if (k.Key == ConsoleKey.Enter) { resultCallback(inputBox.Value); ConsoleApp.Current.LayoutRoot.Controls.Remove(dialog); } }, inputBox.LifetimeManager); ConsoleApp.Current.LayoutRoot.Controls.Add(dialog); inputBox.TryFocus(); }