Browse Source

Fixed PositionCursor

Tig 1 year ago
parent
commit
efe6f1ec60

+ 52 - 56
Terminal.Gui/View/ViewSubViews.cs

@@ -31,14 +31,14 @@ public partial class View
 
     /// <summary>Adds a subview (child) to this view.</summary>
     /// <remarks>
-    /// <para>
-    ///     The Views that have been added to this view can be retrieved via the <see cref="Subviews"/> property. See also
-    ///     <seealso cref="Remove(View)"/> <seealso cref="RemoveAll"/>
-    /// </para>
-    /// <para>
-    ///     Subviews will be disposed when this View is disposed. In other-words, calling this method causes
-    ///     the lifecycle of the subviews to be transferred to this View.
-    /// </para>
+    ///     <para>
+    ///         The Views that have been added to this view can be retrieved via the <see cref="Subviews"/> property. See also
+    ///         <seealso cref="Remove(View)"/> <seealso cref="RemoveAll"/>
+    ///     </para>
+    ///     <para>
+    ///         Subviews will be disposed when this View is disposed. In other-words, calling this method causes
+    ///         the lifecycle of the subviews to be transferred to this View.
+    ///     </para>
     /// </remarks>
     public virtual void Add (View view)
     {
@@ -49,12 +49,12 @@ public partial class View
 
         if (_subviews is null)
         {
-            _subviews = new List<View> ();
+            _subviews = new ();
         }
 
         if (_tabIndexes is null)
         {
-            _tabIndexes = new List<View> ();
+            _tabIndexes = new ();
         }
 
         _subviews.Add (view);
@@ -83,7 +83,7 @@ public partial class View
             view.Enabled = false;
         }
 
-        OnAdded (new SuperViewChangedEventArgs (this, view));
+        OnAdded (new (this, view));
 
         if (IsInitialized && !view.IsInitialized)
         {
@@ -99,14 +99,14 @@ public partial class View
     /// <summary>Adds the specified views (children) to the view.</summary>
     /// <param name="views">Array of one or more views (can be optional parameter).</param>
     /// <remarks>
-    /// <para>
-    ///     The Views that have been added to this view can be retrieved via the <see cref="Subviews"/> property. See also
-    ///     <seealso cref="Remove(View)"/> and <seealso cref="RemoveAll"/>.
-    /// </para>
-    /// <para>
-    ///     Subviews will be disposed when this View is disposed. In other-words, calling this method causes
-    ///     the lifecycle of the subviews to be transferred to this View.
-    /// </para>
+    ///     <para>
+    ///         The Views that have been added to this view can be retrieved via the <see cref="Subviews"/> property. See also
+    ///         <seealso cref="Remove(View)"/> and <seealso cref="RemoveAll"/>.
+    ///     </para>
+    ///     <para>
+    ///         Subviews will be disposed when this View is disposed. In other-words, calling this method causes
+    ///         the lifecycle of the subviews to be transferred to this View.
+    ///     </para>
     /// </remarks>
     public void Add (params View [] views)
     {
@@ -199,10 +199,11 @@ public partial class View
 
     /// <summary>Removes a subview added via <see cref="Add(View)"/> or <see cref="Add(View[])"/> from this View.</summary>
     /// <remarks>
-    /// <para>
-    ///     Normally Subviews will be disposed when this View is disposed. Removing a Subview causes ownership of the Subview's
-    ///     lifecycle to be transferred to the caller; the caller muse call <see cref="Dispose"/>.
-    /// </para>
+    ///     <para>
+    ///         Normally Subviews will be disposed when this View is disposed. Removing a Subview causes ownership of the
+    ///         Subview's
+    ///         lifecycle to be transferred to the caller; the caller muse call <see cref="Dispose"/>.
+    ///     </para>
     /// </remarks>
     public virtual void Remove (View view)
     {
@@ -227,7 +228,7 @@ public partial class View
             }
         }
 
-        OnRemoved (new SuperViewChangedEventArgs (this, view));
+        OnRemoved (new (this, view));
 
         if (Focused == view)
         {
@@ -236,13 +237,15 @@ public partial class View
     }
 
     /// <summary>
-    /// Removes all subviews (children) added via <see cref="Add(View)"/> or <see cref="Add(View[])"/> from this View.
+    ///     Removes all subviews (children) added via <see cref="Add(View)"/> or <see cref="Add(View[])"/> from this View.
     /// </summary>
     /// <remarks>
-    /// <para>
-    ///     Normally Subviews will be disposed when this View is disposed. Removing a Subview causes ownership of the Subview's
-    ///     lifecycle to be transferred to the caller; the caller must call <see cref="Dispose"/> on any Views that were added.
-    /// </para>
+    ///     <para>
+    ///         Normally Subviews will be disposed when this View is disposed. Removing a Subview causes ownership of the
+    ///         Subview's
+    ///         lifecycle to be transferred to the caller; the caller must call <see cref="Dispose"/> on any Views that were
+    ///         added.
+    ///     </para>
     /// </remarks>
     public virtual void RemoveAll ()
     {
@@ -379,7 +382,6 @@ public partial class View
         }
     }
 
-
     /// <summary>Event fired when the <see cref="CanFocus"/> value is being changed.</summary>
     public event EventHandler CanFocusChanged;
 
@@ -498,7 +500,6 @@ public partial class View
         return false;
     }
 
-
     /// <summary>Method invoked when a view loses focus.</summary>
     /// <param name="view">The view that is getting focus.</param>
     /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
@@ -855,36 +856,31 @@ public partial class View
     ///     Positions the cursor in the right position based on the currently focused view in the chain.
     /// </summary>
     /// <remarks>
-    /// Views that are focusable and want the cursor visible should override <see cref="PositionCursor"/> 
-    /// place the cursor in a location that makes sense. Unix terminals do not have
-    /// a way of hiding the cursor, so it can be distracting to have the cursor left at
-    /// the last focused view. Views should make sure that they place the cursor
-    /// in a visually sensible place.
+    ///     <para>
+    ///         Views that are focusable and want the cursor visible should override <see cref="PositionCursor"/>,
+    ///         use <see cref="Move"/> to place the cursor in a location that makes sense, use
+    ///         <see cref="ConsoleDriver.SetCursorVisibility"/>
+    ///         to make the cursor visible, and return the position where the cursor was placed.
+    ///     </para>
+    ///     <para>
+    ///         Unix terminals do not have  a way of hiding the cursor, so it can be distracting to have the cursor left at
+    ///         the last focused view. Views should make sure that they place the cursor in a visually sensible place.
+    ///     </para>
     /// </remarks>
-    /// <returns>Viewport-relative cursor position.</returns>
+    /// <returns>Viewport-relative cursor position. Return <see langword="null"/> to ensure the cursor is not visible.</returns>
     public virtual Point? PositionCursor ()
     {
-        //if (!IsInitialized)
-        //{
-        //    return null;
-        //}
-
-        //// TODO: v2 - This needs to support Subviews of Adornments too
-
-        //// By default we will position the cursor at the top left corner of the Viewport.
-        //// Overrides should return the position where the cursor has been placed.
-        //Point location = Viewport.Location;
-
-        //if (CanFocus && HasFocus && ContentSize != Size.Empty)
-        //{
-        //    location.X = TextFormatter.HotKeyPos == -1 ? 0 : TextFormatter.CursorPosition;
-        //    location.Y = 0;
-        //    Move (location.X, location.Y);
-        //    return location;
-        //}
+        if (CanFocus && HasFocus && ContentSize.HasValue)
+        {
+            // Base class will position the cursor at the end of the text.
+            Point location = Viewport.Location;
+            location.X = TextFormatter.HotKeyPos == -1 ? 0 : TextFormatter.CursorPosition;
+            location.Y = 0;
+            Move (location.X, location.Y);
+        }
 
+        // Returning null will hide the cursor.
         return null;
-
     }
 
     #endregion Focus

+ 15 - 15
Terminal.Gui/Views/TextField.cs

@@ -756,11 +756,11 @@ public class TextField : View
     {
         foreach (char ch in toAdd)
         {
-            KeyCode key;
+            Key key;
 
             try
             {
-                key = (KeyCode)ch;
+                key = ch;
             }
             catch (Exception)
             {
@@ -769,7 +769,7 @@ public class TextField : View
                                             );
             }
 
-            InsertText (new Key { KeyCode = key }, useOldCursorPos);
+            InsertText (key, useOldCursorPos);
         }
     }
 
@@ -819,7 +819,7 @@ public class TextField : View
     }
 
     /// <inheritdoc/>
-    protected internal override bool OnMouseEvent  (MouseEvent ev)
+    protected internal override bool OnMouseEvent (MouseEvent ev)
     {
         if (!ev.Flags.HasFlag (MouseFlags.Button1Pressed)
             && !ev.Flags.HasFlag (MouseFlags.ReportMousePosition)
@@ -1026,7 +1026,7 @@ public class TextField : View
             Driver.AddRune ((Rune)' ');
         }
 
-        //PositionCursor ();
+        PositionCursor ();
 
         RenderCaption ();
 
@@ -1035,16 +1035,16 @@ public class TextField : View
         _isDrawing = false;
     }
 
-    /// <inheritdoc/>
-    public override bool OnEnter (View view)
-    {
-        if (IsInitialized)
-        {
-            Application.Driver.SetCursorVisibility (CursorVisibility.Default);
-        }
+    ///// <inheritdoc/>
+    //public override bool OnEnter (View view)
+    //{
+    //    if (IsInitialized)
+    //    {
+    //        Application.Driver.SetCursorVisibility (CursorVisibility.Default);
+    //    }
 
-        return base.OnEnter (view);
-    }
+    //    return base.OnEnter (view);
+    //}
 
     /// <inheritdoc/>
     public override bool? OnInvokingKeyBindings (Key a)
@@ -1269,7 +1269,7 @@ public class TextField : View
         }
         else
         {
-            //PositionCursor ();
+            PositionCursor ();
         }
     }
 

+ 16 - 16
Terminal.Gui/Views/TextView.cs

@@ -3228,7 +3228,7 @@ public class TextView : View
             }
             else
             {
-                //PositionCursor ();
+                PositionCursor ();
             }
         }
     }
@@ -3353,7 +3353,7 @@ public class TextView : View
 
             if (Used)
             {
-                //PositionCursor ();
+                PositionCursor ();
             }
             else
             {
@@ -3390,7 +3390,7 @@ public class TextView : View
         else if (ev.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))
         {
             ProcessMouseClick (ev, out List<RuneCell> line);
-            //PositionCursor ();
+            PositionCursor ();
 
             if (_model.Count > 0 && _shiftSelecting && Selecting)
             {
@@ -3441,7 +3441,7 @@ public class TextView : View
             }
 
             ProcessMouseClick (ev, out _);
-            //PositionCursor ();
+            PositionCursor ();
             _lastWasKill = false;
             _columnTrack = CurrentColumn;
         }
@@ -3454,7 +3454,7 @@ public class TextView : View
             }
 
             ProcessMouseClick (ev, out _);
-           // PositionCursor ();
+            PositionCursor ();
 
             if (!Selecting)
             {
@@ -3514,7 +3514,7 @@ public class TextView : View
                 CurrentColumn = CurrentRow == newPos.Value.row ? newPos.Value.col : line.Count;
             }
 
-           // PositionCursor ();
+            PositionCursor ();
             _lastWasKill = false;
             _columnTrack = CurrentColumn;
         }
@@ -3534,7 +3534,7 @@ public class TextView : View
             }
 
             CurrentColumn = line.Count;
-            //PositionCursor ();
+            PositionCursor ();
             _lastWasKill = false;
             _columnTrack = CurrentColumn;
         }
@@ -3554,7 +3554,7 @@ public class TextView : View
         List<RuneCell> line = GetCurrentLine ();
         CurrentColumn = line.Count;
         TrackColumn ();
-        //PositionCursor ();
+        PositionCursor ();
     }
 
     /// <summary>Will scroll the <see cref="TextView"/> to the first line and position the cursor there.</summary>
@@ -3565,7 +3565,7 @@ public class TextView : View
         CurrentColumn = 0;
         _leftColumn = 0;
         TrackColumn ();
-        //PositionCursor ();
+        PositionCursor ();
         SetNeedsDisplay ();
     }
 
@@ -3671,7 +3671,7 @@ public class TextView : View
             ClearRegion (viewport.Left, row, right, bottom);
         }
 
-        //PositionCursor ();
+        PositionCursor ();
 
         _isDrawing = false;
     }
@@ -4174,7 +4174,7 @@ public class TextView : View
         }
         else
         {
-            //PositionCursor ();
+            PositionCursor ();
         }
 
         OnUnwrappedCursorPosition ();
@@ -4525,7 +4525,7 @@ public class TextView : View
         }
         else
         {
-            //PositionCursor ();
+            PositionCursor ();
         }
     }
 
@@ -5357,7 +5357,7 @@ public class TextView : View
             }
 
             TrackColumn ();
-            //PositionCursor ();
+            PositionCursor ();
         }
         else if (CurrentRow > Frame.Height)
         {
@@ -5436,7 +5436,7 @@ public class TextView : View
             }
 
             TrackColumn ();
-            //PositionCursor ();
+            PositionCursor ();
         }
 
         DoNeededAction ();
@@ -5462,7 +5462,7 @@ public class TextView : View
             }
 
             TrackColumn ();
-           // PositionCursor ();
+            PositionCursor ();
         }
 
         DoNeededAction ();
@@ -5555,7 +5555,7 @@ public class TextView : View
             }
 
             TrackColumn ();
-            //PositionCursor ();
+            PositionCursor ();
         }
 
         DoNeededAction ();

+ 6 - 6
Terminal.Gui/Views/TileView.cs

@@ -979,13 +979,13 @@ public class TileView : View
             DrawSplitterSymbol ();
         }
 
-        public override bool OnEnter (View view)
-        {
-            Driver.SetCursorVisibility (CursorVisibility.Default);
-            PositionCursor ();
+        //public override bool OnEnter (View view)
+        //{
+        //    Driver.SetCursorVisibility (CursorVisibility.Default);
+        //    PositionCursor ();
 
-            return base.OnEnter (view);
-        }
+        //    return base.OnEnter (view);
+        //}
 
         public override Point? PositionCursor ()
         {

+ 4 - 3
Terminal.Gui/Views/TreeView/TreeView.cs

@@ -468,7 +468,7 @@ public class TreeView<T> : View, ITreeView where T : class
             // TODO: Should this be cancelable?
             ObjectActivatedEventArgs<T> e = new (this, o);
             OnObjectActivated (e);
-            PositionCursor ();
+            //PositionCursor ();
             return true;
         }
         return false;
@@ -676,7 +676,7 @@ public class TreeView<T> : View, ITreeView where T : class
         var characterAsStr = character.ToString ();
         AdjustSelectionToNext (b => AspectGetter (b.Model).StartsWith (characterAsStr, caseSensitivity));
 
-        PositionCursor ();
+        //PositionCursor ();
     }
 
     /// <summary>
@@ -1231,7 +1231,7 @@ public class TreeView<T> : View, ITreeView where T : class
         {
             if (IsInitialized)
             {
-                PositionCursor ();
+                //PositionCursor ();
             }
         }
 
@@ -1250,6 +1250,7 @@ public class TreeView<T> : View, ITreeView where T : class
             if (idx - ScrollOffsetVertical >= 0 && idx - ScrollOffsetVertical < Viewport.Height)
             {
                 Move (0, idx - ScrollOffsetVertical);
+                Application.Driver.SetCursorVisibility (DesiredCursorVisibility);
 
                 return null;//new Point (0, idx - ScrollOffsetVertical);
             }