Skip to content

Commit

Permalink
added events for export && copy commands and virtual methods
Browse files Browse the repository at this point in the history
  • Loading branch information
w-ahmad committed Apr 20, 2024
1 parent bd08c0b commit 9798745
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 63 deletions.
98 changes: 97 additions & 1 deletion src/WinUI.TableView/TableView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel.DataTransfer;
using Windows.Storage;
using Windows.Storage.Pickers;
using WinRT.Interop;
using WinUI.TableView.Extensions;
using WinUIEx;

namespace WinUI.TableView;
public class TableView : ListView
Expand Down Expand Up @@ -55,13 +60,26 @@ private bool Filter(object obj)
return ActiveFilters.All(item => item.Value(obj));
}

internal void CopyToClipboard(bool includeHeaders)
internal void CopyToClipboardInternal(bool includeHeaders)
{
var args = new TableViewCopyToClipboardEventArgs(includeHeaders);
OnCopyToClipboard(args);

if (args.Handled)
{
return;
}

var package = new DataPackage();
package.SetText(GetSelectedRowsContent(includeHeaders));
Clipboard.SetContent(package);
}

protected virtual void OnCopyToClipboard(TableViewCopyToClipboardEventArgs args)
{
CopyToClipboard?.Invoke(this, args);
}

public string GetSelectedRowsContent(bool includeHeaders, char separator = '\t')
{
return GetRowsContent(SelectedItems, includeHeaders, separator);
Expand Down Expand Up @@ -240,6 +258,81 @@ private void RemoveAutoGeneratedColumns()
}
}

internal async void ExportSelectedToCSV()
{
var args = new TableViewExportRowsContentEventArgs();
OnExportSelectedRowsContent(args);

if (args.Handled)
{
return;
}

try
{
var hWnd = HwndExtensions.GetActiveWindow();
if (await GetStorageFile(hWnd) is not { } file)
{
return;
}

var content = GetSelectedRowsContent(true, ',');
using var stream = await file.OpenStreamForWriteAsync();
stream.SetLength(0);

using var tw = new StreamWriter(stream);
await tw.WriteAsync(content);
}
catch { }
}

protected virtual void OnExportSelectedRowsContent(TableViewExportRowsContentEventArgs args)
{
ExportSelectedRowsContent?.Invoke(this, args);
}

internal async void ExportAllToCSV()
{
var args = new TableViewExportRowsContentEventArgs();
OnExportAllRowsContent(args);

if (args.Handled)
{
return;
}

try
{
var hWnd = HwndExtensions.GetActiveWindow();
if (await GetStorageFile(hWnd) is not { } file)
{
return;
}

var content = GetAllRowsContent(true, ',');
using var stream = await file.OpenStreamForWriteAsync();
stream.SetLength(0);

using var tw = new StreamWriter(stream);
await tw.WriteAsync(content);
}
catch { }
}

protected virtual void OnExportAllRowsContent(TableViewExportRowsContentEventArgs args)
{
ExportAllRowsContent?.Invoke(this, args);
}

private static async Task<StorageFile> GetStorageFile(IntPtr hWnd)
{
var savePicker = new FileSavePicker();
InitializeWithWindow.Initialize(savePicker, hWnd);
savePicker.FileTypeChoices.Add("CSV (Comma delimited)", new List<string>() { ".csv" });

return await savePicker.PickSaveFileAsync();
}

private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is TableView tableView)
Expand Down Expand Up @@ -297,4 +390,7 @@ public bool AutoGenerateColumns
public static readonly DependencyProperty AutoGenerateColumnsProperty = DependencyProperty.Register(nameof(AutoGenerateColumns), typeof(bool), typeof(TableView), new PropertyMetadata(true));

public event EventHandler<TableViewAutoGeneratingColumnEventArgs>? AutoGeneratingColumn;
public event EventHandler<TableViewExportRowsContentEventArgs>? ExportAllRowsContent;
public event EventHandler<TableViewExportRowsContentEventArgs>? ExportSelectedRowsContent;
public event EventHandler<TableViewCopyToClipboardEventArgs>? CopyToClipboard;
}
13 changes: 13 additions & 0 deletions src/WinUI.TableView/TableViewCopyToClipboardEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.ComponentModel;

namespace WinUI.TableView;

public class TableViewCopyToClipboardEventArgs : HandledEventArgs
{
public TableViewCopyToClipboardEventArgs(bool includeHeaders)
{
IncludeHeaders = includeHeaders;
}

public bool IncludeHeaders { get; }
}
5 changes: 5 additions & 0 deletions src/WinUI.TableView/TableViewExportRowsContentEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using System.ComponentModel;

namespace WinUI.TableView;

public class TableViewExportRowsContentEventArgs : HandledEventArgs { }
67 changes: 5 additions & 62 deletions src/WinUI.TableView/TableViewHeaderRow.OptionsFlyoutViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
using CommunityToolkit.WinUI;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Input;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Pickers;
using WinRT.Interop;
using WinUIEx;

namespace WinUI.TableView;

Expand All @@ -35,78 +27,29 @@ private void InitializeCommands()
CopyCommand.ExecuteRequested += delegate
{
var focusedElement = FocusManager.GetFocusedElement(TableView.XamlRoot);
if(focusedElement is FrameworkElement { Parent: TableViewCell })
if (focusedElement is FrameworkElement { Parent: TableViewCell })
{
return;
}

TableView.CopyToClipboard(false);
TableView.CopyToClipboardInternal(false);
};

CopyWithHeadersCommand.Description = "Copy the selected row's content including column headers to clipboard.";
CopyWithHeadersCommand.ExecuteRequested += delegate { TableView.CopyToClipboard(true); };
CopyWithHeadersCommand.ExecuteRequested += delegate { TableView.CopyToClipboardInternal(true); };

ClearSortingCommand.ExecuteRequested += delegate { ClearSorting(); };
ClearSortingCommand.CanExecuteRequested += (_, e) => e.CanExecute = TableView.CollectionView.SortDescriptions.Count > 0;

ClearFilterCommand.ExecuteRequested += delegate { ClearFilters(); };
ClearFilterCommand.CanExecuteRequested += (_, e) => e.CanExecute = TableView.ActiveFilters.Count > 0;

ExportAllToCSVCommand.ExecuteRequested += delegate { ExportAllToCSV(); };
ExportAllToCSVCommand.ExecuteRequested += delegate { TableView.ExportAllToCSV(); };

ExportSelectedToCSVCommand.ExecuteRequested += delegate { ExportSelectedToCSV(); };
ExportSelectedToCSVCommand.ExecuteRequested += delegate { TableView.ExportSelectedToCSV(); };
ExportSelectedToCSVCommand.CanExecuteRequested += (_, e) => e.CanExecute = TableView.SelectedItems.Count > 0;
}

private async void ExportSelectedToCSV()
{
try
{
var hWnd = HwndExtensions.GetActiveWindow();
if (await GetStorageFile(hWnd) is not { } file)
{
return;
}

var content = TableView.GetSelectedRowsContent(true, ',');
using var stream = await file.OpenStreamForWriteAsync();
stream.SetLength(0);

using var tw = new StreamWriter(stream);
await tw.WriteAsync(content);
}
catch { }
}

private async void ExportAllToCSV()
{
try
{
var hWnd = HwndExtensions.GetActiveWindow();
if (await GetStorageFile(hWnd) is not { } file)
{
return;
}

var content = TableView.GetAllRowsContent(true, ',');
using var stream = await file.OpenStreamForWriteAsync();
stream.SetLength(0);

using var tw = new StreamWriter(stream);
await tw.WriteAsync(content);
}
catch { }
}

private static async Task<StorageFile> GetStorageFile(IntPtr hWnd)
{
var savePicker = new FileSavePicker();
InitializeWithWindow.Initialize(savePicker, hWnd);
savePicker.FileTypeChoices.Add("CSV (Comma delimited)", new List<string>() { ".csv" });

return await savePicker.PickSaveFileAsync();
}

private void ClearSorting()
{
TableView.CollectionView.SortDescriptions.Clear();
Expand Down

0 comments on commit 9798745

Please sign in to comment.