-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: FileSavePicker wrapper. Closes #13
- Added wrapper to set the Window that owns the picker - Added wrapper for FileSavePicker to allow saving a single file
- Loading branch information
1 parent
3890642
commit d31b5c8
Showing
7 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters