Beispiel #1
0
        public override bool ProcessKey(KeyEvent kb)
        {
            switch (ShortcutHelper.GetModifiersKey(kb))
            {
            case Key.Enter:
                CommandEntered?.Invoke(this, Text.ToString());
                Text = string.Empty;
                return(true);

            default:
                return(base.ProcessKey(kb));
            }
        }
Beispiel #2
0
        public void SendKeys_Test()
        {
            Application.Init(new FakeDriver(), new FakeMainLoop(() => FakeConsole.ReadKey(true)));

            var        top = Application.Top;
            var        view = new View();
            var        shift = false; var alt = false; var control = false;
            Key        key            = default;
            Key        lastKey        = default;
            List <Key> keyEnums       = GetKeys();
            int        i              = 0;
            int        idxKey         = 0;
            var        PushIterations = 0;
            var        PopIterations  = 0;

            List <Key> GetKeys()
            {
                List <Key> keys = new List <Key> ();

                foreach (Key k in Enum.GetValues(typeof(Key)))
                {
                    if ((uint)k <= 0xff)
                    {
                        keys.Add(k);
                    }
                    else if ((uint)k > 0xff)
                    {
                        break;
                    }
                }

                return(keys);
            }

            view.KeyPress += (e) => {
                e.Handled = true;
                PopIterations++;
                var rMk = new KeyModifiers()
                {
                    Shift = e.KeyEvent.IsShift,
                    Alt   = e.KeyEvent.IsAlt,
                    Ctrl  = e.KeyEvent.IsCtrl
                };
                lastKey = ShortcutHelper.GetModifiersKey(new KeyEvent(e.KeyEvent.Key, rMk));
                Assert.Equal(key, lastKey);
            };
            top.Add(view);

            Application.Iteration += () => {
                switch (i)
                {
                case 0:
                    SendKeys();
                    break;

                case 1:
                    shift = true;
                    SendKeys();
                    break;

                case 2:
                    alt = true;
                    SendKeys();
                    break;

                case 3:
                    control = true;
                    SendKeys();
                    break;
                }
                if (PushIterations == keyEnums.Count * 4)
                {
                    Application.RequestStop();
                }
            };

            void SendKeys()
            {
                var k  = keyEnums [idxKey];
                var c  = (char)k;
                var ck = char.IsLetter(c) ? (ConsoleKey)char.ToUpper(c) : (ConsoleKey)c;
                var mk = new KeyModifiers()
                {
                    Shift = shift,
                    Alt   = alt,
                    Ctrl  = control
                };

                key = ShortcutHelper.GetModifiersKey(new KeyEvent(k, mk));
                Application.Driver.SendKeys(c, ck, shift, alt, control);
                PushIterations++;
                if (idxKey + 1 < keyEnums.Count)
                {
                    idxKey++;
                }
                else
                {
                    idxKey = 0;
                    i++;
                }
            }

            Application.Run();

            Assert.Equal(key, lastKey);
        }
Beispiel #3
0
        public override void Init(Toplevel top, ColorScheme colorScheme)
        {
            Application.Init();
            Top = top;
            if (Top == null)
            {
                Top = Application.Top;
            }

            Win = new Window(_fileName ?? "Untitled")
            {
                X           = 0,
                Y           = 1,
                Width       = Dim.Fill(),
                Height      = Dim.Fill(),
                ColorScheme = colorScheme,
            };
            Top.Add(Win);

            _textView = new TextView()
            {
                X            = 0,
                Y            = 0,
                Width        = Dim.Fill(),
                Height       = Dim.Fill(),
                BottomOffset = 1,
                RightOffset  = 1
            };

            CreateDemoFile(_fileName);

            LoadFile();

            Win.Add(_textView);

            var menu = new MenuBar(new MenuBarItem [] {
                new MenuBarItem("_File", new MenuItem [] {
                    new MenuItem("_New", "", () => New()),
                    new MenuItem("_Open", "", () => Open()),
                    new MenuItem("_Save", "", () => Save()),
                    new MenuItem("_Save As", "", () => SaveAs()),
                    new MenuItem("_Close", "", () => CloseFile()),
                    null,
                    new MenuItem("_Quit", "", () => Quit()),
                }),
                new MenuBarItem("_Edit", new MenuItem [] {
                    new MenuItem("_Copy", "", () => Copy(), null, null, Key.CtrlMask | Key.C),
                    new MenuItem("C_ut", "", () => Cut(), null, null, Key.CtrlMask | Key.W),
                    new MenuItem("_Paste", "", () => Paste(), null, null, Key.CtrlMask | Key.Y),
                    null,
                    new MenuItem("_Find", "", () => Find(), null, null, Key.CtrlMask | Key.S),
                    new MenuItem("Find _Next", "", () => FindNext(), null, null, Key.CtrlMask | Key.ShiftMask | Key.S),
                    new MenuItem("Find P_revious", "", () => FindPrevious(), null, null, Key.CtrlMask | Key.ShiftMask | Key.AltMask | Key.S),
                    new MenuItem("_Replace", "", () => Replace(), null, null, Key.CtrlMask | Key.R),
                    new MenuItem("Replace Ne_xt", "", () => ReplaceNext(), null, null, Key.CtrlMask | Key.ShiftMask | Key.R),
                    new MenuItem("Replace Pre_vious", "", () => ReplacePrevious(), null, null, Key.CtrlMask | Key.ShiftMask | Key.AltMask | Key.R),
                    new MenuItem("Replace _All", "", () => ReplaceAll(), null, null, Key.CtrlMask | Key.ShiftMask | Key.AltMask | Key.A),
                    null,
                    new MenuItem("_Select All", "", () => SelectAll(), null, null, Key.CtrlMask | Key.T)
                }),
                new MenuBarItem("_ScrollBarView", CreateKeepChecked()),
                new MenuBarItem("_Cursor", CreateCursorRadio()),
                new MenuBarItem("Forma_t", new MenuItem [] {
                    CreateWrapChecked(),
                    CreateAutocomplete(),
                    CreateAllowsTabChecked()
                }),
                new MenuBarItem("_Responder", new MenuItem [] {
                    CreateCanFocusChecked(),
                    CreateEnabledChecked(),
                    CreateVisibleChecked()
                }),
                new MenuBarItem("Conte_xtMenu", new MenuItem [] {
                    miForceMinimumPosToZero = new MenuItem("ForceMinimumPosTo_Zero", "", () => {
                        miForceMinimumPosToZero.Checked             = forceMinimumPosToZero = !forceMinimumPosToZero;
                        _textView.ContextMenu.ForceMinimumPosToZero = forceMinimumPosToZero;
                    })
                    {
                        CheckType = MenuItemCheckStyle.Checked, Checked = forceMinimumPosToZero
                    },
                    new MenuBarItem("_Languages", GetSupportedCultures())
                })
            });

            Top.Add(menu);

            var statusBar = new StatusBar(new StatusItem [] {
                new StatusItem(Key.F2, "~F2~ Open", () => Open()),
                new StatusItem(Key.F3, "~F3~ Save", () => Save()),
                new StatusItem(Key.F4, "~F4~ Save As", () => SaveAs()),
                new StatusItem(Key.CtrlMask | Key.Q, "~^Q~ Quit", () => Quit()),
                new StatusItem(Key.Null, $"OS Clipboard IsSupported : {Clipboard.IsSupported}", null)
            });

            Top.Add(statusBar);

            _scrollBar = new ScrollBarView(_textView, true);

            _scrollBar.ChangedPosition += () => {
                _textView.TopRow = _scrollBar.Position;
                if (_textView.TopRow != _scrollBar.Position)
                {
                    _scrollBar.Position = _textView.TopRow;
                }
                _textView.SetNeedsDisplay();
            };

            _scrollBar.OtherScrollBarView.ChangedPosition += () => {
                _textView.LeftColumn = _scrollBar.OtherScrollBarView.Position;
                if (_textView.LeftColumn != _scrollBar.OtherScrollBarView.Position)
                {
                    _scrollBar.OtherScrollBarView.Position = _textView.LeftColumn;
                }
                _textView.SetNeedsDisplay();
            };

            _scrollBar.VisibleChanged += () => {
                if (_scrollBar.Visible && _textView.RightOffset == 0)
                {
                    _textView.RightOffset = 1;
                }
                else if (!_scrollBar.Visible && _textView.RightOffset == 1)
                {
                    _textView.RightOffset = 0;
                }
            };

            _scrollBar.OtherScrollBarView.VisibleChanged += () => {
                if (_scrollBar.OtherScrollBarView.Visible && _textView.BottomOffset == 0)
                {
                    _textView.BottomOffset = 1;
                }
                else if (!_scrollBar.OtherScrollBarView.Visible && _textView.BottomOffset == 1)
                {
                    _textView.BottomOffset = 0;
                }
            };

            _textView.DrawContent += (e) => {
                _scrollBar.Size     = _textView.Lines;
                _scrollBar.Position = _textView.TopRow;
                if (_scrollBar.OtherScrollBarView != null)
                {
                    _scrollBar.OtherScrollBarView.Size     = _textView.Maxlength;
                    _scrollBar.OtherScrollBarView.Position = _textView.LeftColumn;
                }
                _scrollBar.LayoutSubviews();
                _scrollBar.Refresh();
            };

            Win.KeyPress += (e) => {
                var keys = ShortcutHelper.GetModifiersKey(e.KeyEvent);
                if (winDialog != null && (e.KeyEvent.Key == Key.Esc ||
                                          e.KeyEvent.Key == (Key.Q | Key.CtrlMask)))
                {
                    DisposeWinDialog();
                }
                else if (e.KeyEvent.Key == (Key.Q | Key.CtrlMask))
                {
                    Quit();
                    e.Handled = true;
                }
                else if (winDialog != null && keys == (Key.Tab | Key.CtrlMask))
                {
                    if (_tabView.SelectedTab == _tabView.Tabs.ElementAt(_tabView.Tabs.Count - 1))
                    {
                        _tabView.SelectedTab = _tabView.Tabs.ElementAt(0);
                    }
                    else
                    {
                        _tabView.SwitchTabBy(1);
                    }
                    e.Handled = true;
                }
                else if (winDialog != null && keys == (Key.Tab | Key.CtrlMask | Key.ShiftMask))
                {
                    if (_tabView.SelectedTab == _tabView.Tabs.ElementAt(0))
                    {
                        _tabView.SelectedTab = _tabView.Tabs.ElementAt(_tabView.Tabs.Count - 1);
                    }
                    else
                    {
                        _tabView.SwitchTabBy(-1);
                    }
                    e.Handled = true;
                }
            };

            Top.Closed += (_) => Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
        }
Beispiel #4
0
        public void StartApplication(GuiSettings settings)
        {
            Application.Init();
            var top = Application.Top;
            var win = GuiComponents.MainGui.MainWindow;

            #region LatencyLabel

            GuiComponents.MainGui.LatencyLabel.Text  = $"Latency: {_client.Latency.ToString()} ms";
            GuiComponents.MainGui.LatencyLabel.Width = GuiComponents.MainGui.LatencyLabel.Text.Length;

            _client.LatencyUpdated += (i, i1) =>
            {
                GuiComponents.MainGui.LatencyLabel.Text  = $"Latency: {_client.Latency.ToString()} ms";
                GuiComponents.MainGui.LatencyLabel.Width = GuiComponents.MainGui.LatencyLabel.Text.Length;
                return(Task.CompletedTask);
            };

            #endregion

            #region StatusLabel

            GuiComponents.MainGui.StatusLabel.Y     = Pos.Bottom(GuiComponents.MainGui.LatencyLabel);
            GuiComponents.MainGui.StatusLabel.Text  = $"Status: {_client.Status.ToString()}";
            GuiComponents.MainGui.StatusLabel.Width = GuiComponents.MainGui.StatusLabel.Text.Length;
            _client.CurrentUserUpdated += (user, selfUser) =>
            {
                GuiComponents.MainGui.StatusLabel.Text  = $"Status: {selfUser.Status.ToString()}";
                GuiComponents.MainGui.StatusLabel.Width = GuiComponents.MainGui.StatusLabel.Text.Length;
                return(Task.CompletedTask);
            };

            #endregion

            #region ServerCountLabel

            GuiComponents.MainGui.ServerCountLabel.Text   = $" In {_client.Guilds.Count} Servers";
            GuiComponents.MainGui.ServerCountLabel.Width  = GuiComponents.MainGui.ServerCountLabel.Text.Length;
            GuiComponents.MainGui.ServerCountLabel.Height = 1;

            #endregion

            #region UsernameLabel

            GuiComponents.MainGui.UsernameLabel.Text   = "Username: "******"Status: {_client.Status.ToString()}";
                    GuiComponents.MainGui.StatusLabel.Width = GuiComponents.MainGui.StatusLabel.Text.Length;

                    GuiComponents.MainGui.ServerCountLabel.Text  = $" In {_client.Guilds.Count} Servers";
                    GuiComponents.MainGui.ServerCountLabel.Width = GuiComponents.MainGui.ServerCountLabel.Text.Length;

                    _client.Log += message =>
                    {
                        _logs += $"[{DateTime.Now}]\t({message.Source})\t{message.Message}\n";
                        GuiComponents.MainGui.LogLabel.Text          = _logs;
                        GuiComponents.MainGui.UsernameLabel.Text     = $"Username: {_client.CurrentUser.Username}";
                        GuiComponents.MainGui.UsernameLabel.Width    = GuiComponents.MainGui.UsernameLabel.Text.Length;
                        GuiComponents.MainGui.ServerCountLabel.Text  = $" In {_client.Guilds.Count} Servers";
                        GuiComponents.MainGui.ServerCountLabel.Width = GuiComponents.MainGui.ServerCountLabel.Text.Length;
                        return(Task.CompletedTask);
                    };

                    #region Log Scroll View

                    GuiComponents.MainGui.LogScroll.ContentSize = new Size(100, 50);
                    GuiComponents.MainGui.LogScroll.ShowHorizontalScrollIndicator = true;
                    GuiComponents.MainGui.LogScroll.ShowVerticalScrollIndicator   = true;

                    GuiComponents.MainGui.LogScroll.Add(GuiComponents.MainGui.LogLabel);
                    GuiComponents.MainGui.LogWindow.Add(GuiComponents.MainGui.LogScroll);
                    win.Add(GuiComponents.MainGui.LogWindow);
                    #endregion
                    break;

                case Key.F2:     // Stops the bot
                    if (_client.ConnectionState == ConnectionState.Connecting ||
                        _client.ConnectionState == ConnectionState.Connected)
                    {
                        await _client.StopAsync();

                        GuiComponents.MainGui.StatusLabel.Text  = "Status: Offline";
                        GuiComponents.MainGui.StatusLabel.Width = GuiComponents.MainGui.StatusLabel.Text.Length;
                    }
                    break;

                case Key.F3:
                    var online    = new Button("_Online");
                    var idle      = new Button("_Idle");
                    var dnd       = new Button("_Do Not Disturb");
                    var invisible = new Button("_Invisible");

                    var dialog = new Dialog($"Change Status from {_client.Status.ToString()} to: ", 60, 7, online, idle, dnd, invisible);
                    online.Clicked += async() =>
                    {
                        await _client.SetStatusAsync(UserStatus.Online);

                        GuiComponents.MainGui.StatusLabel.Text  = "Status: Online";
                        GuiComponents.MainGui.StatusLabel.Width = GuiComponents.MainGui.StatusLabel.Text.Length;
                        Application.RequestStop();
                    };

                    idle.Clicked += async() =>
                    {
                        await _client.SetStatusAsync(UserStatus.Idle);

                        GuiComponents.MainGui.StatusLabel.Text  = "Status: Idle";
                        GuiComponents.MainGui.StatusLabel.Width = GuiComponents.MainGui.StatusLabel.Text.Length;
                        Application.RequestStop();
                    };

                    dnd.Clicked += async() =>
                    {
                        await _client.SetStatusAsync(UserStatus.DoNotDisturb);

                        GuiComponents.MainGui.StatusLabel.Text  = "Status: Do Not Disturb";
                        GuiComponents.MainGui.StatusLabel.Width = GuiComponents.MainGui.StatusLabel.Text.Length;
                        Application.RequestStop();
                    };

                    invisible.Clicked += async() =>
                    {
                        await _client.SetStatusAsync(UserStatus.Invisible);

                        GuiComponents.MainGui.StatusLabel.Text  = "Status: Online";
                        GuiComponents.MainGui.StatusLabel.Width = GuiComponents.MainGui.StatusLabel.Text.Length;
                        Application.RequestStop();
                    };

                    Application.Run(dialog);
                    break;

                case Key.Esc:     // Exits out of the program
                    Application.RequestStop();
                    break;
                }
            };

            #endregion

            #region Menu

            var menu = new MenuBar(new[]
            {
                new MenuBarItem("_Application", new[]
                {
                    new MenuItem("_Quit", "Exit out of the program.", () =>
                    {
                        Application.RequestStop();
                    })
                })
            });

            #endregion

            #region Win Add
            if (!settings.DisableTips)
            {
                GuiComponents.MainGui.InformationWindow.Add(GuiComponents.MainGui.TooltipsLabel);
            }
            win.Add(GuiComponents.MainGui.CommandWindow);
            GuiComponents.MainGui.StatsWindow.Add(GuiComponents.MainGui.ServerCountLabel);
            GuiComponents.MainGui.StatsWindow.Add(GuiComponents.MainGui.LatencyLabel);
            GuiComponents.MainGui.StatsWindow.Add(GuiComponents.MainGui.StatusLabel);
            GuiComponents.MainGui.StatsWindow.Add(GuiComponents.MainGui.UsernameLabel);
            win.Add(GuiComponents.MainGui.StatsWindow);
            win.Add(GuiComponents.MainGui.InformationWindow);
            #endregion

            win.ColorScheme = Colors.TopLevel;
            top.Add(win, menu);
            Application.Run(top);
        }
        public DynamicStatusBarDetails(ustring title) : base(title)
        {
            var _lblTitle = new Label("Title:")
            {
                Y = 1
            };

            Add(_lblTitle);

            _txtTitle = new TextField()
            {
                X     = Pos.Right(_lblTitle) + 4,
                Y     = Pos.Top(_lblTitle),
                Width = Dim.Fill()
            };
            Add(_txtTitle);

            var _lblAction = new Label("Action:")
            {
                X = Pos.Left(_lblTitle),
                Y = Pos.Bottom(_lblTitle) + 1
            };

            Add(_lblAction);

            _txtAction = new TextView()
            {
                ColorScheme = Colors.Dialog,
                X           = Pos.Left(_txtTitle),
                Y           = Pos.Top(_lblAction),
                Width       = Dim.Fill(),
                Height      = 5
            };
            Add(_txtAction);

            var _lblShortcut = new Label("Shortcut:")
            {
                X = Pos.Left(_lblTitle),
                Y = Pos.Bottom(_txtAction) + 1
            };

            Add(_lblShortcut);

            _txtShortcut = new TextField()
            {
                X        = Pos.X(_txtAction),
                Y        = Pos.Y(_lblShortcut),
                Width    = Dim.Fill(),
                ReadOnly = true
            };
            _txtShortcut.KeyDown += (e) => {
                if (!ProcessKey(e.KeyEvent))
                {
                    return;
                }

                var k = ShortcutHelper.GetModifiersKey(e.KeyEvent);
                if (CheckShortcut(k, true))
                {
                    e.Handled = true;
                }
            };

            bool ProcessKey(KeyEvent ev)
            {
                switch (ev.Key)
                {
                case Key.CursorUp:
                case Key.CursorDown:
                case Key.Tab:
                case Key.BackTab:
                    return(false);
                }

                return(true);
            }

            bool CheckShortcut(Key k, bool pre)
            {
                var m = _statusItem != null ? _statusItem : new StatusItem(k, "", null);

                if (pre && !ShortcutHelper.PreShortcutValidation(k))
                {
                    _txtShortcut.Text = "";
                    return(false);
                }
                if (!pre)
                {
                    if (!ShortcutHelper.PostShortcutValidation(ShortcutHelper.GetShortcutFromTag(
                                                                   _txtShortcut.Text, StatusBar.ShortcutDelimiter)))
                    {
                        _txtShortcut.Text = "";
                        return(false);
                    }
                    return(true);
                }
                _txtShortcut.Text = ShortcutHelper.GetShortcutTag(k, StatusBar.ShortcutDelimiter);

                return(true);
            }

            _txtShortcut.KeyUp += (e) => {
                var k = ShortcutHelper.GetModifiersKey(e.KeyEvent);
                if (CheckShortcut(k, false))
                {
                    e.Handled = true;
                }
            };
            Add(_txtShortcut);

            var _btnShortcut = new Button("Clear Shortcut")
            {
                X = Pos.X(_lblShortcut),
                Y = Pos.Bottom(_txtShortcut) + 1
            };

            _btnShortcut.Clicked += () => {
                _txtShortcut.Text = "";
            };
            Add(_btnShortcut);
        }