Skip to content

Commit d7b8ba2

Browse files
authored
Release v2.0.0-rc.6 (#5092)
Release rc.6
1 parent ebb7404 commit d7b8ba2

3 files changed

Lines changed: 151 additions & 4 deletions

File tree

Terminal.Gui/Views/TextInput/TextField/TextField.Commands.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,14 @@ public bool Copy ()
522522

523523
App?.Clipboard?.SetClipboardData (SelectedText);
524524

525+
if (!ReadOnly)
526+
{
527+
return true;
528+
}
529+
_insertionPoint = 0;
530+
ScrollOffset = 0;
531+
ClearAllSelection ();
532+
525533
return true;
526534
}
527535

Terminal.Gui/Views/TextInput/TextField/TextField.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,12 @@ public TextField ()
112112

113113
private void TextField_Initialized (object? sender, EventArgs e)
114114
{
115-
_insertionPoint = GraphemeHelper.GetGraphemeCount (Text);
115+
if (!ReadOnly)
116+
{
117+
_insertionPoint = GraphemeHelper.GetGraphemeCount (Text);
118+
}
116119

117-
if (Viewport.Width > 0)
120+
if (!ReadOnly && Viewport.Width > 0)
118121
{
119122
int colsWidth = Text.GetColumns ();
120123
ScrollOffset = colsWidth > Viewport.Width + 1 ? colsWidth - Viewport.Width + 1 : 0;
@@ -145,12 +148,27 @@ protected override void OnHasFocusChanged (bool newHasFocus, View? previousFocus
145148
App.Mouse.UngrabMouse ();
146149
}
147150

148-
// If gaining focus via keyboard (not mouse), select all text
149-
if (newHasFocus && !_focusSetByMouse && _text.Count > 0)
151+
// If gaining focus via keyboard (not mouse), select all text if not ReadOnly
152+
if (newHasFocus && !ReadOnly && !_focusSetByMouse && _text.Count > 0)
150153
{
151154
SelectAll ();
152155
}
153156

157+
if (ReadOnly && InsertionPoint > 0)
158+
{
159+
_insertionPoint = 0;
160+
}
161+
162+
if (ReadOnly && ScrollOffset > 0)
163+
{
164+
ScrollOffset = 0;
165+
}
166+
167+
if (ReadOnly && SelectedLength > 0)
168+
{
169+
ClearAllSelection ();
170+
}
171+
154172
// Reset the flag after handling focus change
155173
_focusSetByMouse = false;
156174

Tests/UnitTestsParallelizable/Views/TextFieldTests.cs

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,4 +1539,125 @@ public void CtrlKey_With_AssociatedText_Does_Not_Insert_Into_TextField ()
15391539

15401540
Assert.Equal ("", tf.Text);
15411541
}
1542+
1543+
[Fact]
1544+
public void ReadOnly_ShouldNotAllowAutomaticallyScrolling_AndMustSetInsertionPointToZeroAtInitialization ()
1545+
{
1546+
TextField tf = new () { Width = 5, ReadOnly = true, Text = "hello world" };
1547+
tf.BeginInit ();
1548+
tf.EndInit ();
1549+
1550+
Assert.Equal (0, tf.InsertionPoint);
1551+
Assert.Equal (0, tf.ScrollOffset);
1552+
Assert.Null (tf.SelectedText);
1553+
}
1554+
1555+
[Fact]
1556+
public void ReadOnly_ShouldNotSelectAllText_OnFocus ()
1557+
{
1558+
TextField tf = new () { Width = 5, ReadOnly = true, Text = "hello world" };
1559+
tf.BeginInit ();
1560+
tf.EndInit ();
1561+
1562+
tf.SetFocus ();
1563+
1564+
Assert.Equal (0, tf.InsertionPoint);
1565+
Assert.Equal (0, tf.ScrollOffset);
1566+
Assert.Null (tf.SelectedText);
1567+
}
1568+
1569+
[Fact]
1570+
public void ReadOnly_ShouldSetInsertionPointAndScrollOffsetToZero_OnFocus ()
1571+
{
1572+
TextField tf = new () { Width = 5, ReadOnly = true, Text = "hello world" };
1573+
tf.BeginInit ();
1574+
tf.EndInit ();
1575+
1576+
// Move insertion point to end of text
1577+
tf.InsertionPoint = tf.Text.Length;
1578+
Assert.Equal (11, tf.InsertionPoint);
1579+
Assert.Equal (7, tf.ScrollOffset);
1580+
1581+
// Set focus and verify insertion point is still at zero
1582+
tf.SetFocus ();
1583+
Assert.Equal (0, tf.InsertionPoint);
1584+
Assert.Equal (0, tf.ScrollOffset);
1585+
}
1586+
1587+
[Fact]
1588+
public void ReadOnly_ShouldSetInsertionPointAndScrollOffsetToZero_LeavingFocus ()
1589+
{
1590+
TextField tf = new () { Width = 5, ReadOnly = true, Text = "hello world" };
1591+
1592+
// Create another view to take focus away
1593+
View otherView = new () { CanFocus = true };
1594+
1595+
// Create a container to hold both views
1596+
Runnable container = new () { Id = "container", Width = 20, Height = 5 };
1597+
container.Add (tf, otherView);
1598+
container.BeginInit ();
1599+
container.EndInit ();
1600+
1601+
// Set focus to TextField and verify insertion point and scroll offset are at zero
1602+
tf.SetFocus ();
1603+
Assert.Equal (0, tf.InsertionPoint);
1604+
Assert.Equal (0, tf.ScrollOffset);
1605+
1606+
// Move insertion point to end of text and then move focus away to verify insertion point is still at zero
1607+
tf.InsertionPoint = tf.Text.Length;
1608+
Assert.Equal (11, tf.InsertionPoint);
1609+
Assert.Equal (7, tf.ScrollOffset);
1610+
1611+
otherView.SetFocus ();
1612+
Assert.Equal (0, tf.InsertionPoint);
1613+
Assert.Equal (0, tf.ScrollOffset);
1614+
}
1615+
1616+
[Fact]
1617+
public void ReadOnly_ShouldSetInsertionPointAndScrollOffsetToZero_AfterCopyingText ()
1618+
{
1619+
TextField tf = new () { Width = 5, ReadOnly = true, Text = "hello world" };
1620+
tf.BeginInit ();
1621+
tf.EndInit ();
1622+
1623+
// Select all text
1624+
tf.SelectAll ();
1625+
Assert.Equal (11, tf.InsertionPoint);
1626+
Assert.Equal (7, tf.ScrollOffset);
1627+
Assert.Equal ("hello world", tf.SelectedText);
1628+
1629+
// Copy text and verify insertion point and scroll offset are still at zero
1630+
tf.Copy ();
1631+
Assert.Equal (0, tf.InsertionPoint);
1632+
Assert.Equal (0, tf.ScrollOffset);
1633+
Assert.Null (tf.SelectedText);
1634+
}
1635+
1636+
[Fact]
1637+
public void ReadOnly_ShouldSetInsertionPointAndScrollOffsetToZeroAndClearAllSelection_OnLeavingFocus ()
1638+
{
1639+
TextField tf = new () { Width = 5, ReadOnly = true, Text = "hello world" };
1640+
1641+
// Create another view to take focus away
1642+
View otherView = new () { CanFocus = true };
1643+
1644+
// Create a container to hold both views
1645+
Runnable container = new () { Id = "container", Width = 20, Height = 5 };
1646+
container.Add (tf, otherView);
1647+
container.BeginInit ();
1648+
container.EndInit ();
1649+
1650+
// Set focus to TextField and select all text
1651+
tf.SetFocus ();
1652+
tf.SelectAll ();
1653+
Assert.Equal (11, tf.InsertionPoint);
1654+
Assert.Equal (7, tf.ScrollOffset);
1655+
Assert.Equal ("hello world", tf.SelectedText);
1656+
1657+
// Move focus away and verify insertion point and scroll offset are at zero and selection is cleared
1658+
otherView.SetFocus ();
1659+
Assert.Equal (0, tf.InsertionPoint);
1660+
Assert.Equal (0, tf.ScrollOffset);
1661+
Assert.Null (tf.SelectedText);
1662+
}
15421663
}

0 commit comments

Comments
 (0)