Browse Source

Refactoring for maintainabilty

Tig 3 weeks ago
parent
commit
9d060cee0b

+ 25 - 37
Terminal.Gui/Views/TextInput/TextView/TextView.Core.cs

@@ -56,6 +56,7 @@ public partial class TextView
         base.HotKeySpecifier = new ('\xffff');
         base.HotKeySpecifier = new ('\xffff');
 
 
         _model.LinesLoaded += Model_LinesLoaded!;
         _model.LinesLoaded += Model_LinesLoaded!;
+
         _historyText.ChangeText += HistoryText_ChangeText!;
         _historyText.ChangeText += HistoryText_ChangeText!;
 
 
         Initialized += TextView_Initialized!;
         Initialized += TextView_Initialized!;
@@ -572,20 +573,6 @@ public partial class TextView
 
 
     #region Initialization and Configuration
     #region Initialization and Configuration
 
 
-    /// <summary>
-    ///     Configures the ScrollBars to work with the modern View scrolling system.
-    /// </summary>
-    private void ConfigureScrollBars ()
-    {
-        // Subscribe to ViewportChanged to sync internal scroll fields
-        ViewportChanged += TextView_ViewportChanged;
-
-        // Vertical ScrollBar: AutoShow enabled by default as per requirements
-        VerticalScrollBar.AutoShow = true;
-
-        // Horizontal ScrollBar: AutoShow tracks WordWrap as per requirements
-        HorizontalScrollBar.AutoShow = !WordWrap;
-    }
 
 
     private void TextView_Initialized (object sender, EventArgs e)
     private void TextView_Initialized (object sender, EventArgs e)
     {
     {
@@ -599,7 +586,7 @@ public partial class TextView
         KeyBindings.Add (ContextMenu.Key, Command.Context);
         KeyBindings.Add (ContextMenu.Key, Command.Context);
 
 
         // Configure ScrollBars to use modern View scrolling infrastructure
         // Configure ScrollBars to use modern View scrolling infrastructure
-        ConfigureScrollBars ();
+        ConfigureLayout ();
 
 
         OnContentsChanged ();
         OnContentsChanged ();
     }
     }
@@ -619,28 +606,6 @@ public partial class TextView
         }
         }
     }
     }
 
 
-    private void TextView_ViewportChanged (object? sender, DrawEventArgs e)
-    {
-        // Sync internal scroll position fields with Viewport
-        // Only update if values actually changed to prevent infinite loops
-        if (_topRow != Viewport.Y)
-        {
-            _topRow = Viewport.Y;
-        }
-
-        if (_leftColumn != Viewport.X)
-        {
-            _leftColumn = Viewport.X;
-        }
-    }
-
-    private void TextView_LayoutComplete (object? sender, LayoutEventArgs e)
-    {
-        WrapTextModel ();
-        UpdateContentSize ();
-        Adjust ();
-    }
-
     private void Model_LinesLoaded (object sender, EventArgs e)
     private void Model_LinesLoaded (object sender, EventArgs e)
     {
     {
         // This call is not needed. Model_LinesLoaded gets invoked when
         // This call is not needed. Model_LinesLoaded gets invoked when
@@ -658,4 +623,27 @@ public partial class TextView
     }
     }
 
 
     #endregion
     #endregion
+
+    /// <summary>
+    ///     INTERNAL: Determines if a redraw is needed based on selection state, word wrap needs, and Used flag.
+    ///     If a redraw is needed, calls <see cref="AdjustScrollPosition"/>; otherwise positions the cursor and updates
+    ///     the unwrapped cursor position.
+    /// </summary>
+    private void DoNeededAction ()
+    {
+        if (!NeedsDraw && (IsSelecting || _wrapNeeded || !Used))
+        {
+            SetNeedsDraw ();
+        }
+
+        if (NeedsDraw)
+        {
+            AdjustScrollPosition ();
+        }
+        else
+        {
+            PositionCursor ();
+            OnUnwrappedCursorPosition ();
+        }
+    }
 }
 }

+ 13 - 0
Terminal.Gui/Views/TextInput/TextView/TextView.Delete.cs

@@ -638,4 +638,17 @@ public partial class TextView
         _lastWasKill = setLastWasKill;
         _lastWasKill = setLastWasKill;
         DoNeededAction ();
         DoNeededAction ();
     }
     }
+
+    /// <summary>
+    ///     INTERNAL: Resets the column tracking state and last kill operation flag.
+    ///     Column tracking is used to maintain the desired cursor column position when moving up/down
+    ///     through lines of different lengths.
+    /// </summary>
+    private void ResetColumnTrack ()
+    {
+        // Handle some state here - whether the last command was a kill
+        // operation and the column tracking (up/down)
+        _lastWasKill = false;
+        _columnTrack = -1;
+    }
 }
 }

+ 2 - 2
Terminal.Gui/Views/TextInput/TextView/TextView.Find.cs

@@ -150,7 +150,7 @@ public partial class TextView
 
 
             if (!_isReadOnly && replace)
             if (!_isReadOnly && replace)
             {
             {
-                Adjust ();
+                AdjustScrollPosition ();
                 ClearSelectedRegion ();
                 ClearSelectedRegion ();
                 InsertAllText (textToReplace!);
                 InsertAllText (textToReplace!);
                 StartSelecting ();
                 StartSelecting ();
@@ -160,7 +160,7 @@ public partial class TextView
             {
             {
                 UpdateWrapModel ();
                 UpdateWrapModel ();
                 SetNeedsDraw ();
                 SetNeedsDraw ();
-                Adjust ();
+                AdjustScrollPosition ();
             }
             }
 
 
             _continuousFind = true;
             _continuousFind = true;

+ 3 - 3
Terminal.Gui/Views/TextInput/TextView/TextView.Insert.cs

@@ -28,7 +28,7 @@ public partial class TextView
 
 
             if (NeedsDraw)
             if (NeedsDraw)
             {
             {
-                Adjust ();
+                AdjustScrollPosition ();
             }
             }
             else
             else
             {
             {
@@ -170,7 +170,7 @@ public partial class TextView
         // Now adjust column and row positions
         // Now adjust column and row positions
         CurrentRow += lines.Count - 1;
         CurrentRow += lines.Count - 1;
         CurrentColumn = rest is { } ? lastPosition : lines [^1].Count;
         CurrentColumn = rest is { } ? lastPosition : lines [^1].Count;
-        Adjust ();
+        AdjustScrollPosition ();
 
 
         _historyText.Add (
         _historyText.Add (
                           [new (line)],
                           [new (line)],
@@ -301,7 +301,7 @@ public partial class TextView
 
 
         UpdateWrapModel ();
         UpdateWrapModel ();
 
 
-        Adjust ();
+        AdjustScrollPosition ();
         OnContentsChanged ();
         OnContentsChanged ();
     }
     }
 
 

+ 42 - 48
Terminal.Gui/Views/TextInput/TextView/TextView.Viewport.cs → Terminal.Gui/Views/TextInput/TextView/TextView.Layout.cs

@@ -1,8 +1,48 @@
 namespace Terminal.Gui.Views;
 namespace Terminal.Gui.Views;
 
 
-/// <summary>Utility and helper methods for TextView</summary>
+/// <summary>Viewport, scrolling, and content area management methods for TextView</summary>
 public partial class TextView
 public partial class TextView
 {
 {
+
+    /// <summary>
+    ///     Configures the ScrollBars to work with the modern View scrolling system.
+    /// </summary>
+    private void ConfigureLayout ()
+    {
+        // Subscribe to ViewportChanged to sync internal scroll fields
+        ViewportChanged += TextView_ViewportChanged;
+
+        // Vertical ScrollBar: AutoShow enabled by default as per requirements
+        VerticalScrollBar.AutoShow = true;
+
+        // Horizontal ScrollBar: AutoShow tracks WordWrap as per requirements
+        HorizontalScrollBar.AutoShow = !WordWrap;
+    }
+
+    private void TextView_LayoutComplete (object? sender, LayoutEventArgs e)
+    {
+        WrapTextModel ();
+        UpdateContentSize ();
+        AdjustScrollPosition ();
+    }
+
+
+    private void TextView_ViewportChanged (object? sender, DrawEventArgs e)
+    {
+        // Sync internal scroll position fields with Viewport
+        // Only update if values actually changed to prevent infinite loops
+        if (_topRow != Viewport.Y)
+        {
+            _topRow = Viewport.Y;
+        }
+
+        if (_leftColumn != Viewport.X)
+        {
+            _leftColumn = Viewport.X;
+        }
+    }
+
+
     /// <summary>
     /// <summary>
     ///     INTERNAL: Adjusts the scroll position and cursor to ensure the cursor is visible in the viewport.
     ///     INTERNAL: Adjusts the scroll position and cursor to ensure the cursor is visible in the viewport.
     ///     This method handles both horizontal and vertical scrolling, word wrap considerations, and syncs
     ///     This method handles both horizontal and vertical scrolling, word wrap considerations, and syncs
@@ -85,29 +125,6 @@ public partial class TextView
         OnUnwrappedCursorPosition ();
         OnUnwrappedCursorPosition ();
     }
     }
 
 
-    /// <summary>
-    ///     INTERNAL: Determines if a redraw is needed based on selection state, word wrap needs, and Used flag.
-    ///     If a redraw is needed, calls <see cref="AdjustScrollPosition"/>; otherwise positions the cursor and updates
-    ///     the unwrapped cursor position.
-    /// </summary>
-    private void DoNeededAction ()
-    {
-        if (!NeedsDraw && (IsSelecting || _wrapNeeded || !Used))
-        {
-            SetNeedsDraw ();
-        }
-
-        if (NeedsDraw)
-        {
-            AdjustScrollPosition ();
-        }
-        else
-        {
-            PositionCursor ();
-            OnUnwrappedCursorPosition ();
-        }
-    }
-
     /// <summary>
     /// <summary>
     ///     INTERNAL: Calculates the viewport clipping caused by the view extending beyond the SuperView's boundaries.
     ///     INTERNAL: Calculates the viewport clipping caused by the view extending beyond the SuperView's boundaries.
     ///     Returns negative width and height offsets when the viewport extends beyond the SuperView, representing
     ///     Returns negative width and height offsets when the viewport extends beyond the SuperView, representing
@@ -143,7 +160,7 @@ public partial class TextView
         int contentHeight = Math.Max (_model.Count, 1);
         int contentHeight = Math.Max (_model.Count, 1);
 
 
         // For horizontal size: if word wrap is enabled, content width equals viewport width
         // For horizontal size: if word wrap is enabled, content width equals viewport width
-        // Otherwise, calculate the maximum line width (but only if we have a reasonable viewport)
+        // Otherwise, calculate the maximum line width from the entire text model
         int contentWidth;
         int contentWidth;
 
 
         if (_wordWrap)
         if (_wordWrap)
@@ -160,27 +177,4 @@ public partial class TextView
 
 
         SetContentSize (new Size (contentWidth, contentHeight));
         SetContentSize (new Size (contentWidth, contentHeight));
     }
     }
-
-    /// <summary>
-    ///     INTERNAL: Resets the cursor position and scroll offsets to the beginning of the document (0,0)
-    ///     and stops any active text selection.
-    /// </summary>
-    private void ResetPosition ()
-    {
-        _topRow = _leftColumn = CurrentRow = CurrentColumn = 0;
-        StopSelecting ();
-    }
-
-    /// <summary>
-    ///     INTERNAL: Resets the column tracking state and last kill operation flag.
-    ///     Column tracking is used to maintain the desired cursor column position when moving up/down
-    ///     through lines of different lengths.
-    /// </summary>
-    private void ResetColumnTrack ()
-    {
-        // Handle some state here - whether the last command was a kill
-        // operation and the column tracking (up/down)
-        _lastWasKill = false;
-        _columnTrack = -1;
-    }
 }
 }

+ 22 - 2
Terminal.Gui/Views/TextInput/TextView/TextView.Navigation.cs

@@ -111,7 +111,7 @@ public partial class TextView
         }
         }
         else if (CurrentRow > Viewport.Height)
         else if (CurrentRow > Viewport.Height)
         {
         {
-            Adjust ();
+            AdjustScrollPosition ();
         }
         }
         else
         else
         {
         {
@@ -592,8 +592,28 @@ public partial class TextView
             CurrentColumn = line.Count;
             CurrentColumn = line.Count;
         }
         }
 
 
-        Adjust ();
+        AdjustScrollPosition ();
     }
     }
 
 
     #endregion
     #endregion
+
+
+    private void ResetAllTrack ()
+    {
+        // Handle some state here - whether the last command was a kill
+        // operation and the column tracking (up/down)
+        _lastWasKill = false;
+        _columnTrack = -1;
+        _continuousFind = false;
+    }
+
+    /// <summary>
+    ///     INTERNAL: Resets the cursor position and scroll offsets to the beginning of the document (0,0)
+    ///     and stops any active text selection.
+    /// </summary>
+    private void ResetPosition ()
+    {
+        _topRow = _leftColumn = CurrentRow = CurrentColumn = 0;
+        StopSelecting ();
+    }
 }
 }

+ 10 - 2
Terminal.Gui/Views/TextInput/TextView/TextView.Selection.cs

@@ -23,7 +23,7 @@ public partial class TextView
                                  value > _model.Count - 1 ? Math.Max (_model.Count - 1, 0) : value;
                                  value > _model.Count - 1 ? Math.Max (_model.Count - 1, 0) : value;
             IsSelecting = true;
             IsSelecting = true;
             SetNeedsDraw ();
             SetNeedsDraw ();
-            Adjust ();
+            AdjustScrollPosition ();
         }
         }
     }
     }
 
 
@@ -39,7 +39,7 @@ public partial class TextView
                                     value > line.Count ? line.Count : value;
                                     value > line.Count ? line.Count : value;
             IsSelecting = true;
             IsSelecting = true;
             SetNeedsDraw ();
             SetNeedsDraw ();
-            Adjust ();
+            AdjustScrollPosition ();
         }
         }
     }
     }
 
 
@@ -396,4 +396,12 @@ public partial class TextView
 
 
         return q >= start && q <= end - 1;
         return q >= start && q <= end - 1;
     }
     }
+
+    private void ToggleSelecting ()
+    {
+        ResetColumnTrack ();
+        IsSelecting = !IsSelecting;
+        _selectionStartColumn = CurrentColumn;
+        _selectionStartRow = CurrentRow;
+    }
 }
 }

+ 3 - 3
Terminal.Gui/Views/TextInput/TextView/TextView.cs

@@ -178,7 +178,7 @@ public partial class TextView : View, IDesignable
             CurrentRow = value.Y < 0 ? 0 :
             CurrentRow = value.Y < 0 ? 0 :
                          value.Y > _model.Count - 1 ? Math.Max (_model.Count - 1, 0) : value.Y;
                          value.Y > _model.Count - 1 ? Math.Max (_model.Count - 1, 0) : value.Y;
             SetNeedsDraw ();
             SetNeedsDraw ();
-            Adjust ();
+            AdjustScrollPosition ();
         }
         }
     }
     }
 
 
@@ -292,7 +292,7 @@ public partial class TextView : View, IDesignable
 
 
                 SetNeedsDraw ();
                 SetNeedsDraw ();
                 WrapTextModel ();
                 WrapTextModel ();
-                Adjust ();
+                AdjustScrollPosition ();
             }
             }
         }
         }
     }
     }
@@ -499,7 +499,7 @@ public partial class TextView : View, IDesignable
         {
         {
             UpdateWrapModel ();
             UpdateWrapModel ();
             SetNeedsDraw ();
             SetNeedsDraw ();
-            Adjust ();
+            AdjustScrollPosition ();
         }
         }
 
 
         UpdateWrapModel ();
         UpdateWrapModel ();