Skip to content
98 changes: 76 additions & 22 deletions src/PerfView/StackViewer/PerfDataGrid.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,23 @@ public PerfDataGrid()
morphedContent = PadForColumn(morphedContent, i + e.StartColumnDisplayIndex);
}

// Add a leading | character to the first column to ensure GitHub renders the content as table
if (i == 0)
// Add markdown table formatting (| symbols) only when:
// - Multiple columns AND multiple rows selected
// Single column (any rows) or multiple columns in single row: no pipes
bool shouldAddPipes = (m_numSelectedColumns > 1 && m_numSelectedRows > 1);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do want to add pipe symbols for single rows when there are multiple columns selected. The one case where we don't want to add pipe symbols is when there is one row selected, two columns selected, and those cells are the last two columns (First and Last). We should detect this and adjust the value of shouldAddPipes.

Also, down below, we should update Grid.ClipboardCopyMode for all cases except the following:

  1. A single cell is selected.
  2. One row and two columns selected and the columns are the last two (First and Last).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit 079946b. Now:

  • Pipes added for all multiple column selections (including single rows)
  • Special case detected: 1 row + 2 columns = FirstColumn & LastColumn
  • First/Last special case: no pipes, no headers
  • All other multiple column selections: pipes and headers included

if (shouldAddPipes)
{
morphedContent = "| " + morphedContent;
}

// Add a trailing | character to the last column to complete the markdown table row
if (i == e.ClipboardRowContent.Count - 1)
{
morphedContent = morphedContent + " |";
// Add a leading | character to the first column to ensure GitHub renders the content as table

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove reference to GitHub - this is just a markdown table.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit 2b68cf2. Removed the GitHub reference and updated the comment to simply say "for markdown table format".

if (i == 0)
Comment thread
marklio marked this conversation as resolved.
Outdated
{
morphedContent = "| " + morphedContent;
}

// Add a trailing | character to the last column to complete the markdown table row
if (i == e.ClipboardRowContent.Count - 1)
{
morphedContent = morphedContent + " |";
}
}

// TODO Ugly, morph two cells on different rows into one line for the correct cut/paste experience
Expand Down Expand Up @@ -520,27 +527,71 @@ private void SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e
{
// We don't want the header for single values, or for 2 (for cutting and pasting ranges).
int numSelectedCells = window.SelectedCellsChanged(sender, e);
if (numSelectedCells <= 2)
m_numSelectedCells = numSelectedCells;

// Calculate the number of unique columns and rows selected
var dataGrid = sender as DataGrid;
if (dataGrid != null && dataGrid.SelectedCells.Count > 0)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SUGGESTION: if (sender is Datagrid dataGrid && dataGrid.SelectedCells.Count > 0)

While this is a personal preference, it does prevent duplicative checks and also scopes dataGrid to only scenarios in which it isn't null.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this, but didn't implement it for this particular one because we depend on the variable later in the method and I didn't want to duplicate the cast.

{
if (numSelectedCells == 2)
var uniqueColumns = new HashSet<DataGridColumn>();
var uniqueRows = new HashSet<object>();
foreach (var cell in dataGrid.SelectedCells)
{
var dataGrid = sender as DataGrid;
if (dataGrid != null)
{
var cells = dataGrid.SelectedCells;
if (cells != null)
{
m_clipboardRangeStart = GetCellStringValue(cells[0]);
m_clipboardRangeEnd = GetCellStringValue(cells[1]);
}
}
uniqueColumns.Add(cell.Column);
uniqueRows.Add(cell.Item);
}
Grid.ClipboardCopyMode = DataGridClipboardCopyMode.ExcludeHeader;
m_numSelectedColumns = uniqueColumns.Count;
m_numSelectedRows = uniqueRows.Count;
}
else
{
m_numSelectedColumns = 0;
m_numSelectedRows = 0;
}

// Determine whether to include headers based on selection:
// - Single cell: no header
// - 2 cells (range): no header
// - Single column, multiple cells: include header
// - Multiple columns, single row: no header
// - Multiple columns, multiple rows: include header
bool shouldIncludeHeader = false;
if (numSelectedCells > 2)
{
if (m_numSelectedColumns == 1)
{
// Single column, multiple rows: include header
shouldIncludeHeader = true;
}
else if (m_numSelectedRows > 1)
{
// Multiple columns, multiple rows: include header
shouldIncludeHeader = true;
}
// Multiple columns, single row: no header (shouldIncludeHeader stays false)
}

if (shouldIncludeHeader)
{
Grid.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;
}
else
{
Grid.ClipboardCopyMode = DataGridClipboardCopyMode.ExcludeHeader;
}

if (numSelectedCells == 2)
{
if (dataGrid != null)
{
var cells = dataGrid.SelectedCells;
if (cells != null)
{
m_clipboardRangeStart = GetCellStringValue(cells[0]);
m_clipboardRangeEnd = GetCellStringValue(cells[1]);
}
}
}
}
m_maxColumnInSelection = null;
}
Expand All @@ -559,6 +610,9 @@ private void DoHyperlinkHelp(object sender, System.Windows.RoutedEventArgs e)
/// </summary>
private string m_clipboardRangeStart;
private string m_clipboardRangeEnd;
private int m_numSelectedCells;
private int m_numSelectedColumns;
private int m_numSelectedRows;
private int[] m_maxColumnInSelection;
private int m_FindEnd;
private Regex m_findPat;
Expand Down
Loading