Skip to content

Commit

Permalink
GetOpenFileName pinvoke..
Browse files Browse the repository at this point in the history
- unused currently.
- minor function rename.
  • Loading branch information
rocksdanister committed Jun 10, 2022
1 parent 37b4cc8 commit afcb392
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Lively/Lively.Common/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static class ApplicationType
public static bool IsMSIX { get; } = new DesktopBridge.Helpers().IsRunningAsUwp();
public static ClientType Client { get; } = ClientType.winui;
//todo: make compile-time flag.
public static bool IsTestBuild { get; } = true;
public static bool IsTestBuild { get; } = false;
}

public static class Weather
Expand Down
84 changes: 84 additions & 0 deletions src/Lively/Lively.UI.WinUI/Helpers/FilePickerUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage.Pickers;

namespace Lively.UI.WinUI.Helpers
{

//https://github.com/microsoft/WindowsAppSDK/issues/2504
public static class FilePickerUtil
{
[DllImport("comdlg32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern bool GetOpenFileName(ref OpenFileName ofn);

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct OpenFileName
{
public int lStructSize;
public IntPtr hwndOwner;
public IntPtr hInstance;
public string lpstrFilter;
public string lpstrCustomFilter;
public int nMaxCustFilter;
public int nFilterIndex;
public string lpstrFile;
public int nMaxFile;
public string lpstrFileTitle;
public int nMaxFileTitle;
public string lpstrInitialDir;
public string lpstrTitle;
public int Flags;
public short nFileOffset;
public short nFileExtension;
public string lpstrDefExt;
public IntPtr lCustData;
public IntPtr lpfnHook;
public string lpTemplateName;
public IntPtr pvReserved;
public int dwReserved;
public int flagsEx;
}

public static async Task<string> FilePicker(string[] filter) =>
false ? await FilePickerUwp(filter) : FilePickerCsWin32(filter);

private static async Task<string> FilePickerUwp(string[] filter)
{
var filePicker = new FileOpenPicker();
filePicker.SetOwnerWindow(App.Services.GetRequiredService<MainWindow>());
//filePicker.FileTypeFilter.Add("*");
foreach (var item in filter)
{
filePicker.FileTypeFilter.Add(item);
}
return (await filePicker.PickSingleFileAsync())?.Path;
}

private static string FilePickerCsWin32(string[] filters)
{
var ofn = new OpenFileName();
ofn.lStructSize = Marshal.SizeOf(ofn);
/*
ofn.lpstrFilter = "filterName";
foreach (string filter in filters)
{
ofn.lpstrFilter += $"*{filter};";
}
ofn.lpstrFilter += "\0\0";
*/
ofn.lpstrFile = new string(new char[256]);
ofn.nMaxFile = ofn.lpstrFile.Length;
ofn.lpstrFileTitle = new string(new char[64]);
ofn.nMaxFileTitle = ofn.lpstrFileTitle.Length;
//ofn.lpstrTitle = dialogTitle;
if (GetOpenFileName(ref ofn))
return ofn.lpstrFile;
return string.Empty;
}
}
}
2 changes: 1 addition & 1 deletion src/Lively/Lively.UI.WinUI/Helpers/LocalizationUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static string GetLocalizedWallpaperCategory(WallpaperType type)
/// </summary>
/// <param name="anyFile">Show any filetype.</param>
/// <returns></returns>
public static List<string> GetLocalizedSupportedFileDialogFilter(bool anyFile = false)
public static List<string> SupportedFileDialogFilter(bool anyFile = false)
{
var filterCollection = new List<string>();
if (anyFile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private async Task FileBrowseAction()
var filePicker = new FileOpenPicker();
filePicker.SetOwnerWindow(App.Services.GetRequiredService<MainWindow>());
//filePicker.FileTypeFilter.Add("*");
foreach (var item in LocalizationUtil.GetLocalizedSupportedFileDialogFilter(true))
foreach (var item in LocalizationUtil.SupportedFileDialogFilter(true))
{
filePicker.FileTypeFilter.Add(item);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Lively/Lively/Lively.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<StartupObject></StartupObject>
<ApplicationIcon>appicon.ico</ApplicationIcon>
<ApplicationManifest>app.manifest</ApplicationManifest>
<AssemblyVersion>1.9.7.0</AssemblyVersion>
<AssemblyVersion>2.0.2.1</AssemblyVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
Expand Down

0 comments on commit afcb392

Please sign in to comment.