Skip to content

Commit

Permalink
feat: FileSavePicker wrapper. Closes #13
Browse files Browse the repository at this point in the history
- Added wrapper to set the Window that owns the picker
- Added wrapper for FileSavePicker to allow saving a single file
  • Loading branch information
DeadlyEmbrace committed Sep 18, 2021
1 parent 3890642 commit d31b5c8
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 1 deletion.
1 change: 1 addition & 0 deletions VaraniumSharp.WinUI.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
<s:Double x:Key="/Default/Environment/InjectedLayers/InjectedLayerCustomization/=File90684B96B34B974D8CCBEBB97FAE1F59/RelativePriority/@EntryValue">1</s:Double>
<s:Boolean x:Key="/Default/UserDictionary/Words/=flyout/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Fody/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=hwnd/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Readmodel/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Varanium/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
20 changes: 20 additions & 0 deletions VaraniumSharp.WinUI/Interfaces/Pickers/IOwnerWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.UI.Xaml;

namespace VaraniumSharp.WinUI.Interfaces.Pickers
{
/// <summary>
/// Assist with setting the main <see cref="Window"/> that Pickers will be associated with.
/// When using the <see cref="WinUI.TabWindow.TabWindow"/> this class will be set up automatically.
/// </summary>
public interface IOwnerWindow
{
#region Properties

/// <summary>
/// The main parent Window that the Pickers should be assigned to
/// </summary>
Window ParentWindow { get; set; }

#endregion
}
}
24 changes: 24 additions & 0 deletions VaraniumSharp.WinUI/Interfaces/Pickers/IPickerWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Windows.Storage;

namespace VaraniumSharp.WinUI.Interfaces.Pickers
{
/// <summary>
/// Wrapper for Pickers
/// </summary>
public interface IPickerWrapper
{
#region Public Methods

/// <summary>
/// Pick a single file with a specific type
/// </summary>
/// <param name="fileTypes">Dictionary containing file type name and extensions</param>
/// <param name="suggestedFilename">Suggested name for the file. Pass null or empty to leave empty</param>
/// <returns>StorageFile from the pick</returns>
Task<StorageFile> PickSaveFileAsync(KeyValuePair<string, List<string>> fileTypes, string? suggestedFilename);

#endregion
}
}
21 changes: 21 additions & 0 deletions VaraniumSharp.WinUI/Pickers/OwnerWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Microsoft.UI.Xaml;
using VaraniumSharp.Attributes;
using VaraniumSharp.Enumerations;
using VaraniumSharp.WinUI.Interfaces.Pickers;

namespace VaraniumSharp.WinUI.Pickers
{
/// <summary>
/// Assist with setting the main <see cref="Window"/> that Pickers will be associated with
/// </summary>
[AutomaticContainerRegistration(typeof(IOwnerWindow), ServiceReuse.Singleton)]
public class OwnerWindow : IOwnerWindow
{
#region Properties

/// <inheritdoc />
public Window ParentWindow { get; set; }

#endregion
}
}
60 changes: 60 additions & 0 deletions VaraniumSharp.WinUI/Pickers/PickerWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Pickers;
using VaraniumSharp.Attributes;
using VaraniumSharp.WinUI.Interfaces.Pickers;

namespace VaraniumSharp.WinUI.Pickers
{
/// <summary>
/// Wrapper class for Pickers
/// </summary>
[AutomaticContainerRegistration(typeof(IPickerWrapper))]
public class PickerWrapper : IPickerWrapper
{
#region Constructor

/// <summary>
/// DI Constructor
/// </summary>
public PickerWrapper(IOwnerWindow ownerWindow)
{
_ownerWindow = ownerWindow;
}

#endregion

#region Public Methods

/// <inheritdoc />
public async Task<StorageFile> PickSaveFileAsync(KeyValuePair<string, List<string>> fileTypes, string? suggestedFilename)
{
var savePicker = new FileSavePicker();
savePicker.FileTypeChoices.Add(fileTypes.Key, fileTypes.Value);

if (!string.IsNullOrEmpty(suggestedFilename))
{
savePicker.SuggestedFileName = suggestedFilename;
}

// See: https://github.com/microsoft/WindowsAppSDK/issues/467#issuecomment-901220636
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(_ownerWindow.ParentWindow);
WinRT.Interop.InitializeWithWindow.Initialize(savePicker, hwnd);

return await savePicker.PickSaveFileAsync();
}

#endregion

#region Variables

/// <summary>
/// OwnerWindow instance
/// </summary>
private readonly IOwnerWindow _ownerWindow;

#endregion
}
}
4 changes: 3 additions & 1 deletion VaraniumSharp.WinUI/TabWindow/TabWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using VaraniumSharp.Attributes;
using VaraniumSharp.Enumerations;
using VaraniumSharp.WinUI.Interfaces.CustomPaneBase;
using VaraniumSharp.WinUI.Interfaces.Pickers;
using VaraniumSharp.WinUI.Interfaces.TabWindow;

namespace VaraniumSharp.WinUI.TabWindow
Expand All @@ -18,7 +19,7 @@ public sealed partial class TabWindow
/// <summary>
/// DI Constructor
/// </summary>
public TabWindow(ITabWindowContext tabWindowContext, ICustomLayoutEventRouter customLayoutEventRouter)
public TabWindow(ITabWindowContext tabWindowContext, ICustomLayoutEventRouter customLayoutEventRouter, IOwnerWindow ownerWindow)
{
InitializeComponent();
ExtendsContentIntoTitleBar = true;
Expand All @@ -28,6 +29,7 @@ public TabWindow(ITabWindowContext tabWindowContext, ICustomLayoutEventRouter cu
LayoutPane.Children.Add(tabWindowContext.ContentPaneManager.BasePane as UIElement);
_customLayoutEventRouter = customLayoutEventRouter;
_customLayoutEventRouter.ControlDisplayChanged += CustomLayoutEventRouterOnControlDisplayChanged;
ownerWindow.ParentWindow = this;
}

#endregion
Expand Down
1 change: 1 addition & 0 deletions VaraniumSharp.WinUI/VaraniumSharp.WinUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<ItemGroup>
<Folder Include="Interfaces\SettingPane\" />
<Folder Include="Interfaces\VerticalPane\" />
<Folder Include="Pickers\" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit d31b5c8

Please sign in to comment.