Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions windows/docs/decisions/0003-data-grid-control.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
- Off until the host sets `IsEditable`. F2, Enter or a double-click on a cell opens the editor; typing does not, as on macOS. `CellEditStarting` gives the host the cell, and the host sets the text or cancels. Text with NUL opens nothing, since a TextBox cannot hold it.
- F2 and Enter, and Tab in the editor, open the editor once the key is done, so the key's character does not reach the new TextBox.
- The editor is a stock TextBox in the cell's pane, the frozen one or the scrolling one, so it scrolls with the rows. It takes the cells' font, gets focus and selects its text. A value with a line break opens with AcceptsReturn and grows to four rows.
- Each edit opens a new TextBox. A TextBox with focus and no Height that wraps or takes returns animates its own Height for 100 ms when its size changes, and sets Height to NaN when that ends (`TextBox::OnSizeChanged`, `TextBox_Partial.cpp`). One reused for the next cell opened at the last cell's height, and a one-line editor lost the grid's Height a moment later (run 44783975649). Focus needs the box in the tree, not loaded (`CUIElement::IsFocusable`), so the new box takes focus as it opens and keys typed right after F2 or Tab reach it.
- Enter commits and stays. Tab and Shift+Tab commit and edit the next cell to the right or left, as NSTableView does. Escape cancels. Ctrl+Enter commits a value with line breaks.
- Losing focus commits, and a refused commit then cancels. Focus that moves to the TextBox's own menus does not. Unloading the grid and setting `FrozenColumnCount` do the same.
- On those paths the editor's ToolTip closes with it, so the host shows a refused commit's Error itself.
Expand Down
41 changes: 16 additions & 25 deletions windows/src/TablePro.DataGrid/DataGridView.Editing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -345,16 +345,15 @@ private void CommitOrCancel(bool refocus)
private void OpenEditor(int row, int column, string text)
{
ClearCellTip();
var editor = _editor ??= CreateEditor();
var window = column < _columns.FrozenCount ? _frozenWindow : _window;
if (!ReferenceEquals(editor.Parent, window))
// A TextBox animates its Height from the last size it showed and then sets it to NaN, so no edit reuses one.
if (_editor is { Parent: Panel pane } last)
{
(editor.Parent as Panel)?.Children.Remove(editor);
window.Children.Add(editor);
pane.Children.Remove(last);
}
var editor = _editor = CreateEditor();
(column < _columns.FrozenCount ? _frozenWindow : _window).Children.Add(editor);
_editSession++;
_edit = (row, column);
ClearEditError();
// Before the text: a TextBox that takes no returns keeps only the first line.
editor.AcceptsReturn = text.AsSpan().IndexOfAny('\r', '\n') >= 0;
_newline = text.Contains("\r\n", StringComparison.Ordinal) ? "\r\n"
Expand All @@ -364,25 +363,17 @@ private void OpenEditor(int row, int column, string text)
// What the TextBox holds, so a value with mixed line breaks opened and left is no change.
_editText = EditorText(editor);
AutomationProperties.SetName(editor, _names[column]);
editor.Visibility = Visibility.Visible;
PlaceEditor();
if (editor.IsLoaded)
{
FocusEditor(editor);
}
else
{
editor.Loaded -= OnEditorLoaded;
editor.Loaded += OnEditorLoaded;
}
// Focus needs the box in the tree, not loaded, so keys typed right after F2 or Tab reach it, not the grid.
editor.ApplyTemplate();
FocusEditor(editor);
}

// A new editor, or one moved to the other pane, takes focus once it is in the tree.
// A grid out of the tree could not focus the box; selecting again here would select over typed text.
private void OnEditorLoaded(object sender, RoutedEventArgs e)
{
var editor = (TextBox)sender;
editor.Loaded -= OnEditorLoaded;
if (IsEditing)
if (IsEditing && sender is TextBox editor && ReferenceEquals(editor, _editor)
&& editor.FocusState == FocusState.Unfocused)
{
FocusEditor(editor);
}
Expand Down Expand Up @@ -463,17 +454,17 @@ private TextBox CreateEditor()
MinWidth = 0,
MinHeight = 0,
TextWrapping = TextWrapping.NoWrap,
Visibility = Visibility.Collapsed,
};
ApplyEditorFont(editor);
editor.Loaded += OnEditorLoaded;
editor.PreviewKeyDown += OnEditorKeyDown;
// Arrows and paging the TextBox leaves would reach the ScrollViewer around it and scroll the grid.
editor.KeyDown += (_, e) => e.Handled |= e.Key is VirtualKey.Up or VirtualKey.Down or VirtualKey.Left
or VirtualKey.Right or VirtualKey.PageUp or VirtualKey.PageDown or VirtualKey.Home or VirtualKey.End;
editor.LostFocus += OnEditorLostFocus;
editor.TextChanged += (_, _) =>
editor.TextChanged += (sender, _) =>
{
if (IsEditing)
if (IsEditing && ReferenceEquals(sender, _editor))
{
PendingEditChanged?.Invoke(this, EventArgs.Empty);
}
Expand All @@ -498,10 +489,10 @@ private void OnEditorKeyDown(object sender, KeyRoutedEventArgs e)
}
}

// Focus moving to the editor's own menus comes back.
// Focus moving to the editor's own menus comes back. The event comes late, so it may be from the last edit's box.
private void OnEditorLostFocus(object sender, RoutedEventArgs e)
{
if (_editor is null || !IsEditing || EditorFocused
if (_editor is null || !ReferenceEquals(sender, _editor) || !IsEditing || EditorFocused
|| _editor.ContextFlyout is { IsOpen: true } || _editor.SelectionFlyout is { IsOpen: true })
{
return;
Expand Down
66 changes: 59 additions & 7 deletions windows/tests/TablePro.DataGrid.Tests/EditingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,27 @@ public Task F2OpensATextBoxOverTheCellWithTheHostsText() => WinUiHost.RunAsync(a
grid.CancelEdit();
});

// Keys typed right after F2 or Tab go to whatever has focus, before the new box has loaded.
[Fact]
public Task TheEditorHasFocusAsItOpensAndKeepsWhatIsTypedBeforeItLoads() => WinUiHost.RunAsync(async () =>
{
var grid = await ShowEditableAsync(new SyntheticData());
grid.SelectCell(4, 1, extend: false);

grid.BeginEdit();

var editor = grid.Editor!;
Assert.NotEqual(FocusState.Unfocused, editor.FocusState);
Assert.Equal("r4c1", editor.SelectedText);
editor.SelectedText = "typed";
editor.Select(editor.Text.Length, 0);
await SettleAsync(grid);
Assert.Equal("typed", editor.Text);
Assert.Equal((5, 0), (editor.SelectionStart, editor.SelectionLength));
Assert.NotEqual(FocusState.Unfocused, editor.FocusState);
grid.CancelEdit();
});

[Fact]
public Task TheEditorCoversAWrappedRowAndWrapsItsTextAndWithWrapOffIsOneRowAgain() => WinUiHost.RunAsync(async () =>
{
Expand Down Expand Up @@ -81,6 +102,39 @@ public Task TheEditorCoversAWrappedRowAndWrapsItsTextAndWithWrapOffIsOneRowAgain
grid.CancelEdit();
});

// A TextBox with focus animates its own Height from the last size it showed, then sets Height to NaN.
[Fact]
public Task EachEditOpensAtItsOwnRowsHeightAfterATallerOne() => WinUiHost.RunAsync(async () =>
{
var grid = await WrapChecks.ShowAsync(new WrapData());
grid.IsEditable = true;
grid.SelectCell(0, 1, extend: false);
grid.BeginEdit();
await SettleAsync(grid);
Assert.True(grid.Editor!.ActualHeight >= grid.RowHeightOf(0) - 1);
Assert.True(grid.RowHeightOf(0) > grid.RowHeightOf(2) + 1);
grid.CancelEdit();

grid.SelectCell(2, 1, extend: false);
grid.BeginEdit();
await SettleAsync(grid);
Assert.Equal(grid.RowHeightOf(2), grid.Editor!.ActualHeight, 1.0);
grid.CancelEdit();

grid.WrapsText = false;
await SettleAsync(grid);
grid.BeginEdit();
await SettleAsync(grid);
var clock = System.Diagnostics.Stopwatch.StartNew();
while (clock.ElapsedMilliseconds < 300)
{
await WinUiHost.NextFrameAsync();
}
Assert.Equal(grid.RowHeight, grid.Editor!.Height);
Assert.Equal(grid.RowHeight, grid.Editor!.ActualHeight, 1.0);
grid.CancelEdit();
});

[Fact]
public Task TurningWrapOnKeepsWhatTheOpenEditorHolds() => WinUiHost.RunAsync(async () =>
{
Expand Down Expand Up @@ -235,22 +289,21 @@ public Task TabCommitsAndEditsTheNextCell() => WinUiHost.RunAsync(async () =>
grid.BeginEdit();
await SettleAsync(grid);

var editor = grid.Editor!;
editor.Text = "four";
grid.Editor!.Text = "four";
Assert.True(grid.HandleEditorKey(VirtualKey.Tab, control: false, shift: false));
await SettleAsync(grid);

Assert.Equal((3, 4, "four"), Assert.Single(commits));
Assert.True(grid.IsEditing);
Assert.Equal((3, 5), (grid.FocusedRow, grid.FocusedColumn));
Assert.Equal(SyntheticData.Value(3, 5), editor.Text);
Assert.Equal(SyntheticData.Value(3, 5), grid.Editor!.Text);
Assert.True(grid.HandleEditorKey(VirtualKey.Tab, control: false, shift: true));
Assert.Equal((3, 4), (grid.FocusedRow, grid.FocusedColumn));
Assert.False(grid.IsEditing);
await SettleAsync(grid);

Assert.True(grid.IsEditing);
Assert.Equal("r3c4", editor.Text);
Assert.Equal("r3c4", grid.Editor!.Text);
Assert.Single(commits);
grid.CancelEdit();
});
Expand All @@ -277,15 +330,14 @@ public Task AValueWithMixedLineBreaksOpenedAndLeftRaisesNothing() => WinUiHost.R
grid.BeginEdit();
await SettleAsync(grid);

var editor = grid.Editor!;
Assert.True(editor.AcceptsReturn);
Assert.True(grid.Editor!.AcceptsReturn);
Assert.True(grid.HandleEditorKey(VirtualKey.Enter, control: true, shift: false));
Assert.Empty(commits);
Assert.False(grid.IsEditing);

grid.BeginEdit();
await SettleAsync(grid);
editor.Text = "a\rb";
grid.Editor!.Text = "a\rb";
Assert.True(grid.HandleEditorKey(VirtualKey.Enter, control: true, shift: false));
Assert.Equal("a\r\nb", Assert.Single(commits));
});
Expand Down
Loading