public static void ShowTextInput(ConsoleString message, Action <ConsoleString> resultCallback, Action cancelCallback = null, bool allowEscapeToCancel = true, int maxHeight = 12) { 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 }); TextBox inputBox = content.Add(new TextBox() { Y = 4, Foreground = ConsoleColor.Black, Background = ConsoleColor.White }).CenterHorizontally(); 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(); }
private void HardRefresh(ConsoleString outputValue = null) { Controls.Clear(); if (Width < 10 || Height < 5) { return; } def = CreateDefinition(); var gridLayout = Add(new GridLayout(new GridLayoutOptions() { Columns = new System.Collections.Generic.List <GridColumnDefinition>() { new GridColumnDefinition() { Type = GridValueType.Pixels, Width = 2 }, // 0 empty new GridColumnDefinition() { Type = GridValueType.RemainderValue, Width = 1 }, // 1 content new GridColumnDefinition() { Type = GridValueType.Pixels, Width = 2 }, // 2 empty }, Rows = new System.Collections.Generic.List <GridRowDefinition>() { new GridRowDefinition() { Type = GridValueType.Pixels, Height = 1, }, // 0 empty new GridRowDefinition() { Type = GridValueType.Pixels, Height = 1, }, // 1 welcome message new GridRowDefinition() { Type = GridValueType.Pixels, Height = 1, }, // 2 press escape message new GridRowDefinition() { Type = GridValueType.Pixels, Height = 1, }, // 3 empty new GridRowDefinition() { Type = GridValueType.Pixels, Height = 1, }, // 4 input new GridRowDefinition() { Type = GridValueType.RemainderValue, Height = 1, }, // 5 output new GridRowDefinition() { Type = GridValueType.Pixels, Height = 1, }, // 6 empty } })); gridLayout.Fill(); gridLayout.RefreshLayout(); var welcomePanel = gridLayout.Add(new ConsolePanel(), 1, 1); welcomePanel.Add(new Label() { Text = "Welcome to the console".ToWhite() }).CenterHorizontally(); var escapePanel = gridLayout.Add(new ConsolePanel(), 1, 2); escapePanel.Add(new Label() { Text = "Press escape to resume".ToGray() }).CenterHorizontally(); var inputPanel = gridLayout.Add(new ConsolePanel() { }, 1, 4); inputPanel.Add(new Label() { Text = "CMD> ".ToConsoleString() }); tb = inputPanel.Add(new TextBox() { X = "CMD> ".Length, Width = inputPanel.Width - "CMD> ".Length, Foreground = ConsoleColor.Gray, Background = ConsoleColor.Black }); tb.RichTextEditor.TabHandler.TabCompletionHandlers.Add(new PowerArgsRichCommandLineReader(def, new List <ConsoleString>(), false)); tb.TryFocus(); var outputPanel = gridLayout.Add(new ConsolePanel() { Background = ConsoleColor.Black }, 1, 5); outputLabel = outputPanel.Add(new Label() { Text = outputValue ?? UpdateAssistiveText(), Mode = LabelRenderMode.MultiLineSmartWrap }).Fill(); tb.KeyInputReceived.SubscribeForLifetime(async(keyInfo) => await OnHandleHey(keyInfo), tb); }
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); }
private void HardRefresh(ConsoleString outputValue = null) { refreshLt?.Dispose(); refreshLt = new Lifetime(); var myLt = refreshLt; Controls.Clear(); if (Width < 10 || Height < 5) { return; } def = CreateDefinition(); var gridLayout = Add(new GridLayout(new GridLayoutOptions() { Columns = new System.Collections.Generic.List <GridColumnDefinition>() { new GridColumnDefinition() { Type = GridValueType.Pixels, Width = 2 }, // 0 empty new GridColumnDefinition() { Type = GridValueType.RemainderValue, Width = 1 }, // 1 content new GridColumnDefinition() { Type = GridValueType.Pixels, Width = 2 }, // 2 empty }, Rows = new System.Collections.Generic.List <GridRowDefinition>() { new GridRowDefinition() { Type = GridValueType.Pixels, Height = 1, }, // 0 empty new GridRowDefinition() { Type = GridValueType.Pixels, Height = 1, }, // 1 welcome message new GridRowDefinition() { Type = GridValueType.Pixels, Height = 1, }, // 2 press escape message new GridRowDefinition() { Type = GridValueType.Pixels, Height = 1, }, // 3 empty new GridRowDefinition() { Type = GridValueType.Pixels, Height = 1, }, // 4 input new GridRowDefinition() { Type = GridValueType.RemainderValue, Height = 1, }, // 5 output new GridRowDefinition() { Type = GridValueType.Pixels, Height = 1, }, // 6 empty } })); gridLayout.Fill(); gridLayout.RefreshLayout(); var welcomePanel = gridLayout.Add(new ConsolePanel(), 1, 1); welcomePanel.Add(new Label() { Text = "Welcome to the console".ToWhite() }).CenterHorizontally(); var escapePanel = gridLayout.Add(new ConsolePanel(), 1, 2); escapePanel.Add(new Label() { Text = "Press escape to resume".ToGray() }).CenterHorizontally(); var inputPanel = gridLayout.Add(new ConsolePanel() { }, 1, 4); inputPanel.Add(new Label() { Text = "CMD> ".ToConsoleString() }); InputBox = inputPanel.Add(new TextBox() { X = "CMD> ".Length, Width = inputPanel.Width - "CMD> ".Length, Foreground = ConsoleColor.Gray, Background = ConsoleColor.Black }); InputBox.RichTextEditor.TabHandler.TabCompletionHandlers.Add(new PowerArgsRichCommandLineReader(def, new List <ConsoleString>(), false)); ConsoleApp.Current.QueueAction(() => { if (myLt == refreshLt) { InputBox.Focused.SubscribeForLifetime(() => { if (focusLt != null && focusLt.IsExpired == false && focusLt.IsExpiring == false) { focusLt.Dispose(); } focusLt = new Lifetime(); Application.FocusManager.GlobalKeyHandlers.PushForLifetime(ConsoleKey.Tab, null, () => { var forgotten = OnHandleHey(new ConsoleKeyInfo('\t', ConsoleKey.Tab, false, false, false)); }, focusLt); Application.FocusManager.GlobalKeyHandlers.PushForLifetime(ConsoleKey.Tab, ConsoleModifiers.Shift, () => { var forgotten = OnHandleHey(new ConsoleKeyInfo('\t', ConsoleKey.Tab, true, false, false)); }, focusLt); }, refreshLt); InputBox.Unfocused.SubscribeForLifetime(() => { if (focusLt != null && focusLt.IsExpired == false && focusLt.IsExpiring == false) { focusLt.Dispose(); } }, refreshLt); InputBox.TryFocus(); } }); var outputPanel = gridLayout.Add(new ConsolePanel() { Background = ConsoleColor.Black }, 1, 5); outputLabel = outputPanel.Add(new Label() { Text = outputValue ?? UpdateAssistiveText(), Mode = LabelRenderMode.MultiLineSmartWrap }).Fill(); InputBox.KeyInputReceived.SubscribeForLifetime(async(keyInfo) => await OnHandleHey(keyInfo), InputBox); }