Exemplo n.º 1
0
        /// <summary>
        /// Initialize the Dialog
        /// </summary>
        /// <param name="m">Message to show</param>
        /// <param name="hdr">Text for column header of list box</param>
        /// <param name="c">List of objects to put in the list box</param>
        /// <param name="renderer">Function to generate text for each option</param>
        /// <param name="comparer">Optional function to sort the rows</param>
        public ConsoleChoiceDialog(string m, string hdr, List <ChoiceT> c, Func <ChoiceT, string> renderer, Comparison <ChoiceT> comparer = null)
            : base()
        {
            int l = GetLeft(),
                                                 r = GetRight();
            int w = r - l + 1;

            // Resize the window to fit the content
            List <string> msgLines = Formatting.WordWrap(m, w - 4);

            int h = 2 + msgLines.Count + 1 + 1 + c.Count + 2;
            int t = (Console.WindowHeight - h) / 2;
            int b = t + h - 1;

            SetDimensions(l, t, r, b);

            // Wrapped message at top
            ConsoleTextBox tb = new ConsoleTextBox(
                l + 2, t + 2, r - 2, t + 2 + msgLines.Count - 1,
                false,
                TextAlign.Left,
                () => ConsoleTheme.Current.PopupBg,
                () => ConsoleTheme.Current.PopupFg
                );

            AddObject(tb);
            tb.AddLine(m);

            // ConsoleListBox<ChoiceT> of choices at bottom
            choices = new ConsoleListBox <ChoiceT>(
                l + 2, t + 2 + msgLines.Count + 1, r - 2, b - 2,
                c,
                new List <ConsoleListBoxColumn <ChoiceT> >()
            {
                new ConsoleListBoxColumn <ChoiceT>()
                {
                    Header   = hdr,
                    Width    = w - 6,
                    Renderer = renderer,
                    Comparer = comparer
                }
            },
                0, 0, ListSortDirection.Ascending
                );

            choices.AddTip("Enter", "Accept");
            choices.AddBinding(Keys.Enter, (object sender) => {
                return(false);
            });

            choices.AddTip("Esc", "Cancel");
            choices.AddBinding(Keys.Escape, (object sender) => {
                cancelled = true;
                return(false);
            });
            AddObject(choices);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initialize a dialog
        /// </summary>
        /// <param name="m">Message to show</param>
        /// <param name="btns">List of captions for buttons</param>
        /// <param name="hdr">Function to generate the header</param>
        /// <param name="ta">Alignment of the contents</param>
        /// <param name="vertOffset">Pass non-zero to move popup vertically</param>
        public ConsoleMessageDialog(string m, List <string> btns, Func <string> hdr = null, TextAlign ta = TextAlign.Center, int vertOffset = 0)
            : base()
        {
            int maxLen = Formatting.MaxLineLength(m);
            int w      = Math.Max(minWidth, Math.Min(maxLen + 6, Console.WindowWidth - 4));
            int l      = (Console.WindowWidth - w) / 2;
            int r      = -l;

            if (hdr != null)
            {
                CenterHeader = hdr;
            }

            int btnW = btns.Count * buttonWidth + (btns.Count - 1) * buttonPadding;

            if (w < btnW + 4)
            {
                // Widen the window to fit the buttons
                // Buttons will NOT wrap - use ConsoleChoiceDialog
                // if you have many large options.
                w = btnW + 4;
                l = (Console.WindowWidth - w) / 2;
                r = Console.WindowWidth - l;
            }

            List <string> messageLines = Formatting.WordWrap(m, w - 4);
            int           h            = 2 + messageLines.Count + (btns.Count > 0 ? 2 : 0) + 2;

            if (h > Console.WindowHeight - 4)
            {
                h = Console.WindowHeight - 4;
            }

            // Calculate vertical position including offset
            int t, b;

            if (vertOffset <= 0)
            {
                t = (Console.WindowHeight - h) / 2 + vertOffset;
                if (t < 1)
                {
                    t = 2;
                }
                b = t + h - 1;
            }
            else
            {
                b = (Console.WindowHeight - h) / 2 + h - 1;
                if (b >= Console.WindowHeight - 1)
                {
                    b = Console.WindowHeight - 1;
                }
                t = b - h + 1;
            }

            SetDimensions(l, t, r, b);
            int btnRow = GetBottom() - 2;

            ConsoleTextBox tb = new ConsoleTextBox(
                GetLeft() + 2, GetTop() + 2, GetRight() - 2, GetBottom() - 2 - (btns.Count > 0 ? 2 : 0),
                false,
                ta,
                th => th.PopupBg,
                th => th.PopupFg
                );

            AddObject(tb);
            tb.AddLine(m);

            int boxH = GetBottom() - 2 - (btns.Count > 0 ? 2 : 0) - (GetTop() + 2) + 1;

            if (messageLines.Count > boxH)
            {
                // Scroll
                AddTip("Cursor keys", "Scroll");
                tb.AddScrollBindings(this);
            }

            int btnLeft = (Console.WindowWidth - btnW) / 2;

            for (int i = 0; i < btns.Count; ++i)
            {
                string cap = btns[i];
                int    j   = i;
                AddObject(new ConsoleButton(btnLeft, btnRow, btnLeft + buttonWidth - 1, cap, () => {
                    selectedButton = j;
                    Quit();
                }));
                btnLeft += buttonWidth + buttonPadding;
            }
        }