コード例 #1
0
        internal override ConsoleControl GetContent()
        {
            if (Options == null)
            {
                throw new ArgumentException("Options cannot be null");
            }

            if (Mode == DialogButtonsPresentationMode.Grid && Options.Count == 0)
            {
                throw new ArgumentException("You need to specify at least one button for grid mode");
            }

            if (Mode == DialogButtonsPresentationMode.Buttons)
            {
                ConsolePanel    dialogContent = new ConsolePanel();
                ScrollablePanel messagePanel  = dialogContent.Add(new ScrollablePanel()).Fill(padding: new Thickness(0, 0, 1, 3));
                Label           messageLabel  = messagePanel.ScrollableContent.Add(new Label()
                {
                    Mode = LabelRenderMode.MultiLineSmartWrap, Text = Message
                }).FillHorizontally(padding: new Thickness(3, 3, 0, 0));
                if (Options.Count > 0)
                {
                    StackPanel buttonPanel = dialogContent.Add(new StackPanel()
                    {
                        Margin = 1, Height = 1, Orientation = Orientation.Horizontal
                    }).FillHorizontally(padding: new Thickness(1, 0, 0, 0)).DockToBottom(padding: 1);

                    foreach (var option in Options)
                    {
                        var    myOption = option;
                        Button b        = new Button()
                        {
                            Text = option.DisplayText
                        };
                        b.Pressed.SubscribeOnce(() =>
                        {
                            SelectedOption = myOption;
                            Dialog.Dismiss();
                        });
                        buttonPanel.Controls.Add(b);
                    }

                    buttonPanel.Controls.Last().AddedToVisualTree.SubscribeOnce(() => buttonPanel.Application.InvokeNextCycle(() => { buttonPanel.Controls.Last().TryFocus(); }));
                }
                return(dialogContent);
            }
            else
            {
                Grid optionsGrid = new Grid(Options.Select(o => o as object).ToList());
                optionsGrid.MoreDataMessage  = "More options below".ToYellow();
                optionsGrid.EndOfDataMessage = "End of menu";

                optionsGrid.VisibleColumns.Remove(optionsGrid.VisibleColumns.Where(v => v.ColumnName.ToString() == nameof(DialogOption.Id)).Single());
                optionsGrid.VisibleColumns[0].WidthPercentage   = 1;
                optionsGrid.VisibleColumns[0].ColumnDisplayName = Message.IsUnstyled ? Message.ToYellow() : Message;
                optionsGrid.VisibleColumns[0].OverflowBehavior  = new TruncateOverflowBehavior();
                (optionsGrid.VisibleColumns[0].OverflowBehavior as TruncateOverflowBehavior).ColumnWidth = 0;
                optionsGrid.SelectedItemActivated += () =>
                {
                    this.SelectedOption = optionsGrid.SelectedItem as DialogOption;
                    Dialog.Dismiss();
                };

                optionsGrid.AddedToVisualTree.SubscribeOnce(() => optionsGrid.Application.InvokeNextCycle(() => { optionsGrid.TryFocus(); }));

                return(optionsGrid);
            }
        }