Example #1
0
        /// <summary>
        /// Building block API: Prepares the provided <see cref="Toplevel"/>  for execution.
        /// </summary>
        /// <returns>The runstate handle that needs to be passed to the <see cref="End(RunState, bool)"/> method upon completion.</returns>
        /// <param name="toplevel">Toplevel to prepare execution for.</param>
        /// <remarks>
        ///  This method prepares the provided toplevel for running with the focus,
        ///  it adds this to the list of toplevels, sets up the mainloop to process the
        ///  event, lays out the subviews, focuses the first element, and draws the
        ///  toplevel in the screen. This is usually followed by executing
        ///  the <see cref="RunLoop"/> method, and then the <see cref="End(RunState, bool)"/> method upon termination which will
        ///   undo these changes.
        /// </remarks>
        public static RunState Begin(Toplevel toplevel)
        {
            if (toplevel == null)
            {
                throw new ArgumentNullException(nameof(toplevel));
            }
            var rs = new RunState(toplevel);

            Init();
            if (toplevel is ISupportInitializeNotification initializableNotification &&
                !initializableNotification.IsInitialized)
            {
                initializableNotification.BeginInit();
                initializableNotification.EndInit();
            }
        public void RunState_Dispose_Cleans_Up()
        {
            var rs = new Application.RunState(null);

            Assert.NotNull(rs);

            // Should not throw because Toplevel was null
            rs.Dispose();

            var top = new Toplevel();

            rs = new Application.RunState(top);
            Assert.NotNull(rs);

            // Should throw because there's no stack
            Assert.Throws <InvalidOperationException> (() => rs.Dispose());
        }
Example #3
0
        internal void EnsureVisibleBounds(Toplevel top, int x, int y, out int nx, out int ny)
        {
            nx = Math.Max(x, 0);
            nx = nx + top.Frame.Width > Driver.Cols ? Math.Max(Driver.Cols - top.Frame.Width, 0) : nx;
            bool m, s;

            if (SuperView == null || SuperView.GetType() != typeof(Toplevel))
            {
                m = Application.Top.MenuBar != null;
            }
            else
            {
                m = ((Toplevel)SuperView).MenuBar != null;
            }
            int l;

            if (SuperView == null || SuperView is Toplevel)
            {
                l = m ? 1 : 0;
            }
            else
            {
                l = 0;
            }
            ny = Math.Max(y, l);
            if (SuperView == null || SuperView.GetType() != typeof(Toplevel))
            {
                s = Application.Top.StatusBar != null && Application.Top.StatusBar.Visible;
            }
            else
            {
                s = ((Toplevel)SuperView).StatusBar != null && ((Toplevel)SuperView).StatusBar.Visible;
            }
            if (SuperView == null || SuperView is Toplevel)
            {
                l = s ? Driver.Rows - 1 : Driver.Rows;
            }
            else
            {
                l = s ? SuperView.Frame.Height - 1 : SuperView.Frame.Height;
            }
            ny = Math.Min(ny, l);
            ny = ny + top.Frame.Height > l?Math.Max(l - top.Frame.Height, m? 1 : 0) : ny;
        }
        public void RunningFalse_Stops()
        {
            // Setup Mock driver
            Init();

            var top = new Toplevel();
            var rs  = Application.Begin(top);

            Assert.NotNull(rs);
            Assert.Equal(top, Application.Current);

            Application.Iteration = () => {
                top.Running = false;
            };

            Application.Run(top);

            Application.Shutdown();
            Assert.Null(Application.Current);
            Assert.Null(Application.Top);
            Assert.Null(Application.MainLoop);
            Assert.Null(Application.Driver);
        }
Example #5
0
        public void Begin_End_Cleana_Up()
        {
            // Setup Mock driver
            Init();

            // Test null Toplevel
            Assert.Throws <ArgumentNullException> (() => Application.Begin(null));

            var top = new Toplevel();
            var rs  = Application.Begin(top);

            Assert.NotNull(rs);
            Assert.Equal(top, Application.Current);
            Application.End(rs, true);

            Assert.Null(Application.Current);
            Assert.Null(Application.CurrentView);
            Assert.Null(Application.Top);
            Assert.Null(Application.MainLoop);
            Assert.Null(Application.Driver);

            Shutdown();
        }
Example #6
0
        public void RequestStop_Stops()
        {
            // Setup Mock driver
            Init();

            var top = new Toplevel();
            var rs  = Application.Begin(top);

            Assert.NotNull(rs);
            Assert.Equal(top, Application.Current);

            Application.Iteration = () => {
                Application.RequestStop();
            };

            Application.Run(top, true);

            Application.Shutdown(true);
            Assert.Null(Application.Current);
            Assert.Null(Application.CurrentView);
            Assert.Null(Application.Top);
            Assert.Null(Application.MainLoop);
            Assert.Null(Application.Driver);
        }
Example #7
0
 private void PositionToplevel(Toplevel top)
 {
     EnsureVisibleBounds(top, top.Frame.X, top.Frame.Y, out int nx, out int ny);
     if ((nx != top.Frame.X || ny != top.Frame.Y) && top.LayoutStyle != LayoutStyle.Computed)
     {
         top.X = nx;
         top.Y = ny;
     }
     if (StatusBar != null)
     {
         if (ny + top.Frame.Height > Driver.Rows - 1)
         {
             if (top.Height is Dim.DimFill)
             {
                 top.Height = Dim.Fill() - 1;
             }
         }
         if (StatusBar.Frame.Y != Driver.Rows - 1)
         {
             StatusBar.Y = Driver.Rows - 1;
             SetNeedsDisplay();
         }
     }
 }
Example #8
0
 /// <summary>
 /// Initializes a new instance of <see cref="Terminal.Gui"/> Application.
 /// </summary>
 /// <remarks>
 /// <para>
 /// Call this method once per instance (or after <see cref="Shutdown"/> has been called).
 /// </para>
 /// <para>
 /// Loads the right <see cref="ConsoleDriver"/> for the platform.
 /// </para>
 /// <para>
 /// Creates a <see cref="Toplevel"/> and assigns it to <see cref="Top"/>
 /// </para>
 /// </remarks>
 public static void Init(ConsoleDriver driver = null, IMainLoopDriver mainLoopDriver = null) => Init(() => Toplevel.Create(), driver, mainLoopDriver);
Example #9
0
        public void Initialized_Event_Comparing_With_Added_Event()
        {
            Application.Init(new FakeDriver(), new NetMainLoop(() => FakeConsole.ReadKey(true)));

            var t = new Toplevel()
            {
                Id = "0",
            };

            var w = new Window()
            {
                Id = "t", Width = Dim.Fill(), Height = Dim.Fill()
            };
            var v1 = new View()
            {
                Id = "v1", Width = Dim.Fill(), Height = Dim.Fill()
            };
            var v2 = new View()
            {
                Id = "v2", Width = Dim.Fill(), Height = Dim.Fill()
            };
            var sv1 = new View()
            {
                Id = "sv1", Width = Dim.Fill(), Height = Dim.Fill()
            };

            int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0;

            w.Added += (e) => {
                Assert.Equal(e.Frame.Width, w.Frame.Width);
                Assert.Equal(e.Frame.Height, w.Frame.Height);
            };
            v1.Added += (e) => {
                Assert.Equal(e.Frame.Width, v1.Frame.Width);
                Assert.Equal(e.Frame.Height, v1.Frame.Height);
            };
            v2.Added += (e) => {
                Assert.Equal(e.Frame.Width, v2.Frame.Width);
                Assert.Equal(e.Frame.Height, v2.Frame.Height);
            };
            sv1.Added += (e) => {
                Assert.Equal(e.Frame.Width, sv1.Frame.Width);
                Assert.Equal(e.Frame.Height, sv1.Frame.Height);
            };

            t.Initialized += (s, e) => {
                tc++;
                Assert.Equal(1, tc);
                Assert.Equal(1, wc);
                Assert.Equal(1, v1c);
                Assert.Equal(1, v2c);
                Assert.Equal(1, sv1c);

                Assert.True(t.CanFocus);
                Assert.True(w.CanFocus);
                Assert.False(v1.CanFocus);
                Assert.False(v2.CanFocus);
                Assert.False(sv1.CanFocus);

                Application.Refresh();
            };
            w.Initialized += (s, e) => {
                wc++;
                Assert.Equal(t.Frame.Width, w.Frame.Width);
                Assert.Equal(t.Frame.Height, w.Frame.Height);
            };
            v1.Initialized += (s, e) => {
                v1c++;
                Assert.Equal(t.Frame.Width, v1.Frame.Width);
                Assert.Equal(t.Frame.Height, v1.Frame.Height);
            };
            v2.Initialized += (s, e) => {
                v2c++;
                Assert.Equal(t.Frame.Width, v2.Frame.Width);
                Assert.Equal(t.Frame.Height, v2.Frame.Height);
            };
            sv1.Initialized += (s, e) => {
                sv1c++;
                Assert.Equal(t.Frame.Width, sv1.Frame.Width);
                Assert.Equal(t.Frame.Height, sv1.Frame.Height);
                Assert.False(sv1.CanFocus);
                Assert.Throws <InvalidOperationException> (() => sv1.CanFocus = true);
                Assert.False(sv1.CanFocus);
            };

            v1.Add(sv1);
            w.Add(v1, v2);
            t.Add(w);

            Application.Iteration = () => {
                Application.Refresh();
                t.Running = false;
            };

            Application.Run(t);
            Application.Shutdown();

            Assert.Equal(1, tc);
            Assert.Equal(1, wc);
            Assert.Equal(1, v1c);
            Assert.Equal(1, v2c);
            Assert.Equal(1, sv1c);

            Assert.True(t.CanFocus);
            Assert.True(w.CanFocus);
            Assert.False(v1.CanFocus);
            Assert.False(v2.CanFocus);
            Assert.False(sv1.CanFocus);

            v1.CanFocus = true;
            Assert.False(sv1.CanFocus);              // False because sv1 was disposed and it isn't a subview of v1.
        }
Example #10
0
 internal virtual void OnActivate(Toplevel deactivated)
 {
     Activate?.Invoke(deactivated);
 }
Example #11
0
 internal virtual void OnDeactivate(Toplevel activated)
 {
     Deactivate?.Invoke(activated);
 }
Example #12
0
 internal virtual void OnClosed(Toplevel top)
 {
     Closed?.Invoke(top);
 }
Example #13
0
 /// <summary>
 /// Initializes the event arguments with the requesting toplevel.
 /// </summary>
 /// <param name="requestingTop">The <see cref="RequestingTop"/>.</param>
 public ToplevelClosingEventArgs(Toplevel requestingTop)
 {
     RequestingTop = requestingTop;
 }
Example #14
0
 /// <summary>
 /// Stops running the <paramref name="top"/> <see cref="Toplevel"/>.
 /// </summary>
 /// <param name="top">The toplevel to request stop.</param>
 public virtual void RequestStop(Toplevel top)
 {
     top.RequestStop();
 }
Example #15
0
        public void Initialized_Event_Will_Be_Invoked_When_Added_Dynamically()
        {
            Application.Init(new FakeDriver(), new NetMainLoop(() => FakeConsole.ReadKey(true)));

            var t = new Toplevel()
            {
                Id = "0",
            };

            var w = new Window()
            {
                Id = "t", Width = Dim.Fill(), Height = Dim.Fill()
            };
            var v1 = new View()
            {
                Id = "v1", Width = Dim.Fill(), Height = Dim.Fill()
            };
            var v2 = new View()
            {
                Id = "v2", Width = Dim.Fill(), Height = Dim.Fill()
            };

            int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0;

            t.Initialized += (s, e) => {
                tc++;
                Assert.Equal(1, tc);
                Assert.Equal(0, wc);
                Assert.Equal(0, v1c);
                Assert.Equal(0, v2c);
                Assert.Equal(0, sv1c);

                Assert.True(t.CanFocus);
                Assert.True(w.CanFocus);
                Assert.False(v1.CanFocus);
                Assert.False(v2.CanFocus);

                Application.Refresh();
            };
            w.Initialized += (s, e) => {
                wc++;
                Assert.Equal(t.Frame.Width, w.Frame.Width);
                Assert.Equal(t.Frame.Height, w.Frame.Height);
            };
            v1.Initialized += (s, e) => {
                v1c++;
                Assert.Equal(t.Frame.Width, v1.Frame.Width);
                Assert.Equal(t.Frame.Height, v1.Frame.Height);
            };
            v2.Initialized += (s, e) => {
                v2c++;
                Assert.Equal(t.Frame.Width, v2.Frame.Width);
                Assert.Equal(t.Frame.Height, v2.Frame.Height);
            };
            w.Add(v1, v2);
            t.Add(w);

            Application.Iteration = () => {
                var sv1 = new View()
                {
                    Id = "sv1", Width = Dim.Fill(), Height = Dim.Fill()
                };

                sv1.Initialized += (s, e) => {
                    sv1c++;
                    Assert.NotEqual(t.Frame.Width, sv1.Frame.Width);
                    Assert.NotEqual(t.Frame.Height, sv1.Frame.Height);
                    Assert.False(sv1.CanFocus);
                    sv1.CanFocus = true;
                    Assert.True(sv1.CanFocus);
                };

                v1.Add(sv1);

                Application.Refresh();
                t.Running = false;
            };

            Application.Run(t);
            Application.Shutdown();

            Assert.Equal(1, tc);
            Assert.Equal(1, wc);
            Assert.Equal(1, v1c);
            Assert.Equal(1, v2c);
            Assert.Equal(1, sv1c);

            Assert.True(t.CanFocus);
            Assert.True(w.CanFocus);
            Assert.False(v1.CanFocus);
            Assert.False(v2.CanFocus);
        }
Example #16
0
        /// <summary>
        /// Open the <see cref="MenuItems"/> menu items.
        /// </summary>
        public void Show()
        {
            if (menuBar != null)
            {
                Hide();
            }
            container          = Application.Current;
            container.Closing += Container_Closing;
            container.Resized += Container_Resized;
            var frame    = container.Frame;
            var position = Position;

            if (Host != null)
            {
                Host.ViewToScreen(container.Frame.X, container.Frame.Y, out int x, out int y);
                var pos = new Point(x, y);
                pos.Y += Host.Frame.Height - 1;
                if (position != pos)
                {
                    Position = position = pos;
                }
            }
            var rect = Menu.MakeFrame(position.X, position.Y, MenuItems.Children);

            if (rect.Right >= frame.Right)
            {
                if (frame.Right - rect.Width >= 0 || !ForceMinimumPosToZero)
                {
                    position.X = frame.Right - rect.Width;
                }
                else if (ForceMinimumPosToZero)
                {
                    position.X = 0;
                }
            }
            else if (ForceMinimumPosToZero && position.X < 0)
            {
                position.X = 0;
            }
            if (rect.Bottom >= frame.Bottom)
            {
                if (frame.Bottom - rect.Height - 1 >= 0 || !ForceMinimumPosToZero)
                {
                    if (Host == null)
                    {
                        position.Y = frame.Bottom - rect.Height - 1;
                    }
                    else
                    {
                        Host.ViewToScreen(container.Frame.X, container.Frame.Y, out int x, out int y);
                        var pos = new Point(x, y);
                        position.Y = pos.Y - rect.Height - 1;
                    }
                }
                else if (ForceMinimumPosToZero)
                {
                    position.Y = 0;
                }
            }
            else if (ForceMinimumPosToZero && position.Y < 0)
            {
                position.Y = 0;
            }

            menuBar = new MenuBar(new [] { MenuItems })
            {
                X      = position.X,
                Y      = position.Y,
                Width  = 0,
                Height = 0,
                UseSubMenusSingleFrame = UseSubMenusSingleFrame
            };

            menuBar.isContextMenuLoading = true;
            menuBar.MenuAllClosed       += MenuBar_MenuAllClosed;
            IsShow = true;
            menuBar.OpenMenu();
        }
Example #17
0
 /// <summary>
 /// Initializes a new <see cref="RunState"/> class.
 /// </summary>
 /// <param name="view"></param>
 public RunState(Toplevel view)
 {
     Toplevel = view;
 }
Example #18
0
 internal RunState(Toplevel view)
 {
     Toplevel = view;
 }
Example #19
0
        internal View EnsureVisibleBounds(Toplevel top, int x, int y,
                                          out int nx, out int ny, out View mb, out View sb)
        {
            int  l;
            View superView;

            if (top?.SuperView == null || top == Application.Top || top?.SuperView == Application.Top)
            {
                l         = Driver.Cols;
                superView = Application.Top;
            }
            else
            {
                l         = top.SuperView.Frame.Width;
                superView = top.SuperView;
            }
            nx = Math.Max(x, 0);
            nx = nx + top.Frame.Width > l?Math.Max(l - top.Frame.Width, 0) : nx;

            SetWidth(top.Frame.Width, out int rWidth);
            if (rWidth < 0 && nx >= top.Frame.X)
            {
                nx = Math.Max(top.Frame.Right - 2, 0);
            }
            //System.Diagnostics.Debug.WriteLine ($"nx:{nx}, rWidth:{rWidth}");
            bool m, s;

            if (top?.SuperView == null || top == Application.Top || top?.SuperView == Application.Top)
            {
                m  = Application.Top.MenuBar?.Visible == true;
                mb = Application.Top.MenuBar;
            }
            else
            {
                var t = top.SuperView;
                while (!(t is Toplevel))
                {
                    t = t.SuperView;
                }
                m  = ((Toplevel)t).MenuBar?.Visible == true;
                mb = ((Toplevel)t).MenuBar;
            }
            if (top?.SuperView == null || top == Application.Top || top?.SuperView == Application.Top)
            {
                l = m ? 1 : 0;
            }
            else
            {
                l = 0;
            }
            ny = Math.Max(y, l);
            if (top?.SuperView == null || top == Application.Top || top?.SuperView == Application.Top)
            {
                s  = Application.Top.StatusBar?.Visible == true;
                sb = Application.Top.StatusBar;
            }
            else
            {
                var t = top.SuperView;
                while (!(t is Toplevel))
                {
                    t = t.SuperView;
                }
                s  = ((Toplevel)t).StatusBar?.Visible == true;
                sb = ((Toplevel)t).StatusBar;
            }
            if (top?.SuperView == null || top == Application.Top || top?.SuperView == Application.Top)
            {
                l = s ? Driver.Rows - 1 : Driver.Rows;
            }
            else
            {
                l = s ? top.SuperView.Frame.Height - 1 : top.SuperView.Frame.Height;
            }
            ny = Math.Min(ny, l);
            ny = ny + top.Frame.Height >= l?Math.Max(l - top.Frame.Height, m? 1 : 0) : ny;

            SetHeight(top.Frame.Height, out int rHeight);
            if (rHeight < 0 && ny >= top.Frame.Y)
            {
                ny = Math.Max(top.Frame.Bottom - 2, 0);
            }
            //System.Diagnostics.Debug.WriteLine ($"ny:{ny}, rHeight:{rHeight}");

            return(superView);
        }
Example #20
0
 /// <summary>
 /// Initializes a new instance of <see cref="Terminal.Gui"/> Application.
 /// </summary>
 /// <remarks>
 /// <para>
 /// Call this method once per instance (or after <see cref="Shutdown"/> has been called).
 /// </para>
 /// <para>
 /// Loads the right <see cref="ConsoleDriver"/> for the platform.
 /// </para>
 /// <para>
 /// Creates a <see cref="Toplevel"/> and assigns it to <see cref="Top"/> and <see cref="CurrentView"/>
 /// </para>
 /// </remarks>
 public static void Init() => Init(() => Toplevel.Create());
Example #21
0
        internal void EnsureVisibleBounds(Toplevel top, int x, int y, out int nx, out int ny)
        {
            nx = Math.Max(x, 0);
            int l;

            if (SuperView == null || SuperView is Toplevel)
            {
                l = Driver.Cols;
            }
            else
            {
                l = SuperView.Frame.Width;
            }
            nx = nx + top.Frame.Width > l?Math.Max(l - top.Frame.Width, 0) : nx;

            SetWidth(top.Frame.Width, out int rWidth);
            if (rWidth < 0 && nx >= top.Frame.X)
            {
                nx = Math.Max(top.Frame.Right - 2, 0);
            }
            //System.Diagnostics.Debug.WriteLine ($"nx:{nx}, rWidth:{rWidth}");
            bool m, s;

            if (SuperView == null || SuperView.GetType() != typeof(Toplevel))
            {
                m = Application.Top.MenuBar != null;
            }
            else
            {
                m = ((Toplevel)SuperView).MenuBar != null;
            }
            if (SuperView == null || SuperView is Toplevel)
            {
                l = m ? 1 : 0;
            }
            else
            {
                l = 0;
            }
            ny = Math.Max(y, l);
            if (SuperView == null || SuperView.GetType() != typeof(Toplevel))
            {
                s = Application.Top.StatusBar != null && Application.Top.StatusBar.Visible;
            }
            else
            {
                s = ((Toplevel)SuperView).StatusBar != null && ((Toplevel)SuperView).StatusBar.Visible;
            }
            if (SuperView == null || SuperView is Toplevel)
            {
                l = s ? Driver.Rows - 1 : Driver.Rows;
            }
            else
            {
                l = s ? SuperView.Frame.Height - 1 : SuperView.Frame.Height;
            }
            ny = Math.Min(ny, l);
            ny = ny + top.Frame.Height > l?Math.Max(l - top.Frame.Height, m? 1 : 0) : ny;

            SetHeight(top.Frame.Height, out int rHeight);
            if (rHeight < 0 && ny >= top.Frame.Y)
            {
                ny = Math.Max(top.Frame.Bottom - 2, 0);
            }
            //System.Diagnostics.Debug.WriteLine ($"ny:{ny}, rHeight:{rHeight}");
        }
Example #22
0
 internal virtual void OnChildLoaded(Toplevel top)
 {
     ChildLoaded?.Invoke(top);
 }
Example #23
0
 static Window generateMainWindow(Terminal.Gui.Toplevel applicationTop)
 {
     return(new Window(new Rect(0, 1, Constants.WIDTH, Constants.HEIGHT), "KRATE OFFICIAL"));
 }