Beispiel #1
0
        public void ChangeWindow(Window newWindow)
        {
            if (_toplevel == null)
            {
                var errorMessage = "Couldn't change window. TopLevel view doesn't exist.";
                _logger.Fatal(errorMessage);
                throw new InvalidOperationException(errorMessage);
            }

            _toplevel.RemoveAll();
            _toplevel.Add(newWindow);
            _currentWindow = newWindow;
            _toplevel.LayoutSubviews();
            _toplevel.FocusFirst();
        }
Beispiel #2
0
        /// <summary>
        /// This shows the selection UI. Each time it is run, it calls Application.Init to reset everything.
        /// </summary>
        /// <returns></returns>
        private static Scenario GetScenarioToRun()
        {
            Application.UseSystemConsole = _useSystemConsole;
            Application.Init();
            Application.HeightAsBuffer    = _heightAsBuffer;
            Application.AlwaysSetPosition = _alwaysSetPosition;

            // Set this here because not initialized until driver is loaded
            _baseColorScheme = Colors.Base;

            StringBuilder aboutMessage = new StringBuilder();

            aboutMessage.AppendLine("UI Catalog is a comprehensive sample library for Terminal.Gui");
            aboutMessage.AppendLine(@"             _           ");
            aboutMessage.AppendLine(@"  __ _ _   _(_)  ___ ___ ");
            aboutMessage.AppendLine(@" / _` | | | | | / __/ __|");
            aboutMessage.AppendLine(@"| (_| | |_| | || (__\__ \");
            aboutMessage.AppendLine(@" \__, |\__,_|_(_)___|___/");
            aboutMessage.AppendLine(@" |___/                   ");
            aboutMessage.AppendLine("");
            aboutMessage.AppendLine($"Version: {typeof (UICatalogApp).Assembly.GetName ().Version}");
            aboutMessage.AppendLine($"Using Terminal.Gui Version: {FileVersionInfo.GetVersionInfo (typeof (Terminal.Gui.Application).Assembly.Location).ProductVersion}");
            aboutMessage.AppendLine("");

            _menu = new MenuBar(new MenuBarItem [] {
                new MenuBarItem("_File", new MenuItem [] {
                    new MenuItem("_Quit", "", () => Application.RequestStop(), null, null, Key.Q | Key.CtrlMask)
                }),
                new MenuBarItem("_Color Scheme", CreateColorSchemeMenuItems()),
                new MenuBarItem("Diag_nostics", CreateDiagnosticMenuItems()),
                new MenuBarItem("_Help", new MenuItem [] {
                    new MenuItem("_gui.cs API Overview", "", () => OpenUrl("https://migueldeicaza.github.io/gui.cs/articles/overview.html"), null, null, Key.F1),
                    new MenuItem("gui.cs _README", "", () => OpenUrl("https://github.com/migueldeicaza/gui.cs"), null, null, Key.F2),
                    new MenuItem("_About...", "About this app", () => MessageBox.Query("About UI Catalog", aboutMessage.ToString(), "_Ok"), null, null, Key.CtrlMask | Key.A),
                })
            });

            _leftPane = new FrameView("Categories")
            {
                X        = 0,
                Y        = 1,          // for menu
                Width    = 25,
                Height   = Dim.Fill(1),
                CanFocus = false,
                Shortcut = Key.CtrlMask | Key.C
            };
            _leftPane.Title          = $"{_leftPane.Title} ({_leftPane.ShortcutTag})";
            _leftPane.ShortcutAction = () => _leftPane.SetFocus();

            _categories       = Scenario.GetAllCategories().OrderBy(c => c).ToList();
            _categoryListView = new ListView(_categories)
            {
                X             = 0,
                Y             = 0,
                Width         = Dim.Fill(0),
                Height        = Dim.Fill(0),
                AllowsMarking = false,
                CanFocus      = true,
            };
            _categoryListView.OpenSelectedItem += (a) => {
                _rightPane.SetFocus();
            };
            _categoryListView.SelectedItemChanged += CategoryListView_SelectedChanged;
            _leftPane.Add(_categoryListView);

            _rightPane = new FrameView("Scenarios")
            {
                X        = 25,
                Y        = 1,          // for menu
                Width    = Dim.Fill(),
                Height   = Dim.Fill(1),
                CanFocus = true,
                Shortcut = Key.CtrlMask | Key.S
            };
            _rightPane.Title          = $"{_rightPane.Title} ({_rightPane.ShortcutTag})";
            _rightPane.ShortcutAction = () => _rightPane.SetFocus();

            _nameColumnWidth = Scenario.ScenarioMetadata.GetName(_scenarios.OrderByDescending(t => Scenario.ScenarioMetadata.GetName(t).Length).FirstOrDefault()).Length;

            _scenarioListView = new ListView()
            {
                X             = 0,
                Y             = 0,
                Width         = Dim.Fill(0),
                Height        = Dim.Fill(0),
                AllowsMarking = false,
                CanFocus      = true,
            };

            _scenarioListView.OpenSelectedItem += _scenarioListView_OpenSelectedItem;
            _rightPane.Add(_scenarioListView);

            _categoryListView.SelectedItem = _categoryListViewItem;
            _categoryListView.OnSelectedChanged();

            _capslock   = new StatusItem(Key.CharMask, "Caps", null);
            _numlock    = new StatusItem(Key.CharMask, "Num", null);
            _scrolllock = new StatusItem(Key.CharMask, "Scroll", null);

            _statusBar = new StatusBar()
            {
                Visible = true,
            };
            _statusBar.Items = new StatusItem [] {
                _capslock,
                _numlock,
                _scrolllock,
                new StatusItem(Key.Q | Key.CtrlMask, "~CTRL-Q~ Quit", () => {
                    if (_runningScenario is null)
                    {
                        // This causes GetScenarioToRun to return null
                        _runningScenario = null;
                        Application.RequestStop();
                    }
                    else
                    {
                        _runningScenario.RequestStop();
                    }
                }),
                new StatusItem(Key.F10, "~F10~ Hide/Show Status Bar", () => {
                    _statusBar.Visible = !_statusBar.Visible;
                    _leftPane.Height   = Dim.Fill(_statusBar.Visible ? 1 : 0);
                    _rightPane.Height  = Dim.Fill(_statusBar.Visible ? 1 : 0);
                    _top.LayoutSubviews();
                    _top.SetChildNeedsDisplay();
                }),
                new StatusItem(Key.CharMask, Application.Driver.GetType().Name, null),
            };
Beispiel #3
0
        public void Internal_Tests()
        {
            var top          = new Toplevel();
            var eventInvoked = "";

            top.ChildUnloaded += (e) => eventInvoked = "ChildUnloaded";
            top.OnChildUnloaded(top);
            Assert.Equal("ChildUnloaded", eventInvoked);
            top.ChildLoaded += (e) => eventInvoked = "ChildLoaded";
            top.OnChildLoaded(top);
            Assert.Equal("ChildLoaded", eventInvoked);
            top.Closed += (e) => eventInvoked = "Closed";
            top.OnClosed(top);
            Assert.Equal("Closed", eventInvoked);
            top.Closing += (e) => eventInvoked = "Closing";
            top.OnClosing(new ToplevelClosingEventArgs(top));
            Assert.Equal("Closing", eventInvoked);
            top.AllChildClosed += () => eventInvoked = "AllChildClosed";
            top.OnAllChildClosed();
            Assert.Equal("AllChildClosed", eventInvoked);
            top.ChildClosed += (e) => eventInvoked = "ChildClosed";
            top.OnChildClosed(top);
            Assert.Equal("ChildClosed", eventInvoked);
            top.Deactivate += (e) => eventInvoked = "Deactivate";
            top.OnDeactivate(top);
            Assert.Equal("Deactivate", eventInvoked);
            top.Activate += (e) => eventInvoked = "Activate";
            top.OnActivate(top);
            Assert.Equal("Activate", eventInvoked);
            top.Loaded += () => eventInvoked = "Loaded";
            top.OnLoaded();
            Assert.Equal("Loaded", eventInvoked);
            top.Ready += () => eventInvoked = "Ready";
            top.OnReady();
            Assert.Equal("Ready", eventInvoked);
            top.Unloaded += () => eventInvoked = "Unloaded";
            top.OnUnloaded();
            Assert.Equal("Unloaded", eventInvoked);

            top.AddMenuStatusBar(new MenuBar());
            Assert.NotNull(top.MenuBar);
            top.AddMenuStatusBar(new StatusBar());
            Assert.NotNull(top.StatusBar);
            top.RemoveMenuStatusBar(top.MenuBar);
            Assert.Null(top.MenuBar);
            top.RemoveMenuStatusBar(top.StatusBar);
            Assert.Null(top.StatusBar);

            Application.Begin(top);
            Assert.Equal(top, Application.Top);

            // top is Application.Top without menu and status bar.
            var supView = top.EnsureVisibleBounds(top, 2, 2, out int nx, out int ny, out View mb, out View sb);

            Assert.Equal(Application.Top, supView);
            Assert.Equal(0, nx);
            Assert.Equal(0, ny);
            Assert.Null(mb);
            Assert.Null(sb);

            top.AddMenuStatusBar(new MenuBar());
            Assert.NotNull(top.MenuBar);

            // top is Application.Top with a menu and without status bar.
            top.EnsureVisibleBounds(top, 2, 2, out nx, out ny, out mb, out sb);
            Assert.Equal(0, nx);
            Assert.Equal(1, ny);
            Assert.NotNull(mb);
            Assert.Null(sb);

            top.AddMenuStatusBar(new StatusBar());
            Assert.NotNull(top.StatusBar);

            // top is Application.Top with a menu and status bar.
            top.EnsureVisibleBounds(top, 2, 2, out nx, out ny, out mb, out sb);
            Assert.Equal(0, nx);
            Assert.Equal(1, ny);
            Assert.NotNull(mb);
            Assert.NotNull(sb);

            top.RemoveMenuStatusBar(top.MenuBar);
            Assert.Null(top.MenuBar);

            // top is Application.Top without a menu and with a status bar.
            top.EnsureVisibleBounds(top, 2, 2, out nx, out ny, out mb, out sb);
            Assert.Equal(0, nx);
            Assert.Equal(0, ny);
            Assert.Null(mb);
            Assert.NotNull(sb);

            top.RemoveMenuStatusBar(top.StatusBar);
            Assert.Null(top.StatusBar);
            Assert.Null(top.MenuBar);

            var win = new Window()
            {
                Width = Dim.Fill(), Height = Dim.Fill()
            };

            top.Add(win);
            top.LayoutSubviews();

            // The SuperView is always the same regardless of the caller.
            supView = top.EnsureVisibleBounds(win, 0, 0, out nx, out ny, out mb, out sb);
            Assert.Equal(Application.Top, supView);
            supView = win.EnsureVisibleBounds(win, 0, 0, out nx, out ny, out mb, out sb);
            Assert.Equal(Application.Top, supView);

            // top is Application.Top without menu and status bar.
            top.EnsureVisibleBounds(win, 0, 0, out nx, out ny, out mb, out sb);
            Assert.Equal(0, nx);
            Assert.Equal(0, ny);
            Assert.Null(mb);
            Assert.Null(sb);

            top.AddMenuStatusBar(new MenuBar());
            Assert.NotNull(top.MenuBar);

            // top is Application.Top with a menu and without status bar.
            top.EnsureVisibleBounds(win, 2, 2, out nx, out ny, out mb, out sb);
            Assert.Equal(0, nx);
            Assert.Equal(1, ny);
            Assert.NotNull(mb);
            Assert.Null(sb);

            top.AddMenuStatusBar(new StatusBar());
            Assert.NotNull(top.StatusBar);

            // top is Application.Top with a menu and status bar.
            top.EnsureVisibleBounds(win, 30, 20, out nx, out ny, out mb, out sb);
            Assert.Equal(0, nx);
            Assert.Equal(1, ny);
            Assert.NotNull(mb);
            Assert.NotNull(sb);

            top.RemoveMenuStatusBar(top.MenuBar);
            top.RemoveMenuStatusBar(top.StatusBar);
            Assert.Null(top.StatusBar);
            Assert.Null(top.MenuBar);

            top.Remove(win);

            win = new Window()
            {
                Width = 60, Height = 15
            };
            top.Add(win);

            // top is Application.Top without menu and status bar.
            top.EnsureVisibleBounds(win, 0, 0, out nx, out ny, out mb, out sb);
            Assert.Equal(0, nx);
            Assert.Equal(0, ny);
            Assert.Null(mb);
            Assert.Null(sb);

            top.AddMenuStatusBar(new MenuBar());
            Assert.NotNull(top.MenuBar);

            // top is Application.Top with a menu and without status bar.
            top.EnsureVisibleBounds(win, 2, 2, out nx, out ny, out mb, out sb);
            Assert.Equal(2, nx);
            Assert.Equal(2, ny);
            Assert.NotNull(mb);
            Assert.Null(sb);

            top.AddMenuStatusBar(new StatusBar());
            Assert.NotNull(top.StatusBar);

            // top is Application.Top with a menu and status bar.
            top.EnsureVisibleBounds(win, 30, 20, out nx, out ny, out mb, out sb);
            Assert.Equal(20, nx);             // 20+60=80
            Assert.Equal(9, ny);              // 9+15+1(mb)=25
            Assert.NotNull(mb);
            Assert.NotNull(sb);

            top.PositionToplevels();
            Assert.Equal(new Rect(0, 1, 60, 15), win.Frame);

            Assert.Null(Toplevel.dragPosition);
            win.MouseEvent(new MouseEvent()
            {
                X = 6, Y = 0, Flags = MouseFlags.Button1Pressed
            });
            Assert.Equal(new Point(6, 0), Toplevel.dragPosition);
            win.MouseEvent(new MouseEvent()
            {
                X = 6, Y = 0, Flags = MouseFlags.Button1Released
            });
            Assert.Null(Toplevel.dragPosition);
            win.CanFocus = false;
            win.MouseEvent(new MouseEvent()
            {
                X = 6, Y = 0, Flags = MouseFlags.Button1Pressed
            });
            Assert.Null(Toplevel.dragPosition);
        }