Commits

Lars Brubaker  committed 0f391bb

Fixed a bug in textedit control that could happen when the mouse selected the
control and it had no content.
Wrote a unit test to be sure this never happens again.
Made the gcode visualizer actually select the text control on startup.

  • Participants
  • Parent commits c0a6877

Comments (0)

Files changed (3)

File Gui/TextEditWidget.cs

             if (mouseIsDown)
             {
                 selectionIndexToStartBefore = internalTextWidget.Printer.GetCharacterIndexToStartBefore(new Vector2(mouseEvent.X, mouseEvent.Y));
+                if (selectionIndexToStartBefore < 0)
+                {
+                    selectionIndexToStartBefore = 0;
+                }
                 if (selectionIndexToStartBefore != charIndexToInsertBefore)
                 {
                     selecting = true;
 
                 case Keys.End:
                     if (keyEvent.Control)
+                    {
                         charIndexToInsertBefore = internalTextWidget.Text.Length;
+                    }
                     else
+                    {
                         GotoEndOfCurrentLine();
+                    }
+
                     keyEvent.SuppressKeyPress = true;
                     keyEvent.Handled = true;
                     break;
 
                 case Keys.Home:
                     if (keyEvent.Control)
+                    {
                         charIndexToInsertBefore = 0;
+                    }
                     else
+                    {
                         GotoStartOfCurrentLine();
+                    }
+
                     keyEvent.SuppressKeyPress = true;
                     keyEvent.Handled = true;
                     break;

File Gui/UnitTests.cs

         }
 
         [Test]
-        public void TextEditGetsInput()
+        public void TextEditTextSelectionTests()
+        {
+            RectangleWidget container = new RectangleWidget();
+            container.LocalBounds = new rect_d(0, 0, 200, 200);
+            TextEditWidget editField1 = new TextEditWidget("", 0, 0, pixelWidth: 20);
+            container.AddChild(editField1);
+
+            container.OnLayout();
+
+            // mouse select on the control when it contains nothing
+            container.OnMouseDown(new MouseEventArgs(MouseButtons.Left, 1, 1, 1, 0));
+            container.OnMouseMove(new MouseEventArgs(MouseButtons.Left, 0, -1, 1, 0));
+            container.OnMouseUp(new MouseEventArgs(MouseButtons.Left, -1, 1, 1, 0));
+            SendKey(Keys.D, 'b', container);
+            Assert.IsTrue(editField1.Text == "b", "It should have b a in it.");
+        }
+
+        [Test]
+        public void TextEditGetsFocusTests()
         {
             RectangleWidget container = new RectangleWidget();
             container.LocalBounds = new rect_d(0, 0, 200, 200);
                 buttonTests.WindowBlocksButtonClick();
 
                 TextEditTests textEditTests = new TextEditTests();
-                textEditTests.TextEditGetsInput();
+                textEditTests.TextEditGetsFocusTests();
+                textEditTests.TextEditTextSelectionTests();
             }
         }
     }

File examples/GCodeVisualizer/GCodeVisualizer.cs

             currentLayerIndex.Multiline = false;
             keepOnTop.AddChild(currentLayerIndex);
             currentLayerIndex.TextChanged += new TextEditWidget.TextChangedEventHandler(layerCountTextWidget_TextChanged);
-            currentLayerIndex.Focus();
 
             layerCountTextWidget = new TextWidget("/1____", 12);
             keepOnTop.AddChild(layerCountTextWidget);
             ResumeLayout();
         }
 
-        public override void OnParentChanged()
-        {
-            SetAnchor(AnchorFlags.All);
+        public override void  OnLayout()
+        {
+            SetAnchor(AnchorFlags.All);
+            currentLayerIndex.Focus();
+
+            base.OnLayout();
         }
 
         void SetActiveLayer(int layer)