Remove legacy file dialog

Pre-Vista OS is not supported by .Net6 any ways
This commit is contained in:
Peter Kirmeier 2022-11-13 17:55:24 +01:00
parent 92e61ace23
commit 3b28cc168b
7 changed files with 21 additions and 108 deletions

View file

@ -8,7 +8,6 @@ namespace SystemTrayMenu
using System.Reflection;
using System.Windows;
using SystemTrayMenu.Utilities;
using static SystemTrayMenu.Utilities.FormsExtensions;
internal static class Program
{

View file

@ -14,7 +14,6 @@ namespace SystemTrayMenu
using SystemTrayMenu.Properties;
using SystemTrayMenu.UserInterface.FolderBrowseDialog;
using SystemTrayMenu.Utilities;
using static SystemTrayMenu.Utilities.FormsExtensions;
using static SystemTrayMenu.Utilities.IconReader;
public static class Config
@ -112,12 +111,12 @@ namespace SystemTrayMenu
}
}
public static void SetFolderByUser(bool save = true)
public static void SetFolderByUser(Window owner = null, bool save = true)
{
using FolderDialog dialog = new();
dialog.InitialFolder = Path;
if (dialog.ShowDialog() == DialogResult.OK)
if (dialog.ShowDialog(owner))
{
Settings.Default.PathDirectory = dialog.Folder;
if (save)
@ -127,12 +126,12 @@ namespace SystemTrayMenu
}
}
public static void SetFolderIcoByUser()
public static void SetFolderIcoByUser(Window owner)
{
using FolderDialog dialog = new();
dialog.InitialFolder = Settings.Default.PathIcoDirectory;
if (dialog.ShowDialog() == DialogResult.OK)
if (dialog.ShowDialog(owner))
{
Settings.Default.PathIcoDirectory = dialog.Folder;
}

View file

@ -5,11 +5,11 @@
namespace SystemTrayMenu.UserInterface.FolderBrowseDialog
{
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Windows;
using System.Windows.Interop;
using SystemTrayMenu.Utilities;
using static SystemTrayMenu.Utilities.FormsExtensions;
public class FolderDialog : IFolderDialog, IDisposable
{
@ -36,24 +36,13 @@ namespace SystemTrayMenu.UserInterface.FolderBrowseDialog
/// </summary>
public string Folder { get; set; }
public DialogResult ShowDialog()
{
return ShowDialog(owner: new WindowWrapper(IntPtr.Zero));
}
public DialogResult ShowDialog(IWin32Window owner)
{
if (Environment.OSVersion.Version.Major >= 6)
{
return ShowVistaDialog(owner);
}
else
{
return ShowLegacyDialog(owner);
}
}
public DialogResult ShowVistaDialog(IWin32Window owner)
/// <summary>
/// Shows the file dialog and requests user interaction.
/// </summary>
/// <param name="owner">The window the dialog is assigned to.</param>
/// <returns>True is returned on successful user interaction and when not cancelled by the user otherwise false is returned.</returns>
[SupportedOSPlatform("windows")]
public bool ShowDialog(Window owner)
{
NativeMethods.IFileDialog frm = (NativeMethods.IFileDialog)new NativeMethods.FileOpenDialogRCW();
frm.GetOptions(out uint options);
@ -89,7 +78,7 @@ namespace SystemTrayMenu.UserInterface.FolderBrowseDialog
}
}
if (owner != null && frm.Show(owner.Handle) == NativeMethods.S_OK)
if (frm.Show(owner == null ? IntPtr.Zero : new WindowInteropHelper(owner).Handle) == NativeMethods.S_OK)
{
try
{
@ -104,7 +93,7 @@ namespace SystemTrayMenu.UserInterface.FolderBrowseDialog
try
{
Folder = Marshal.PtrToStringAuto(pszString);
return DialogResult.OK;
return true;
}
finally
{
@ -120,40 +109,7 @@ namespace SystemTrayMenu.UserInterface.FolderBrowseDialog
}
}
return DialogResult.Cancel;
}
public DialogResult ShowLegacyDialog(IWin32Window owner)
{
#if TODO
using SaveFileDialog frm = new()
{
CheckFileExists = false,
CheckPathExists = true,
CreatePrompt = false,
Filter = "|" + Guid.Empty.ToString(),
FileName = "any",
};
if (InitialFolder != null)
{
frm.InitialDirectory = InitialFolder;
}
frm.OverwritePrompt = false;
frm.Title = Translator.GetText("Select directory");
frm.ValidateNames = false;
if (frm.ShowDialog(owner) == DialogResult.OK)
{
Folder = Path.GetDirectoryName(frm.FileName);
return DialogResult.OK;
}
else
{
return DialogResult.Cancel;
}
#else
return DialogResult.OK;
#endif
return false;
}
public void Dispose()

View file

@ -4,8 +4,7 @@
namespace SystemTrayMenu.UserInterface.FolderBrowseDialog
{
using System.Windows.Interop;
using static SystemTrayMenu.Utilities.FormsExtensions;
using System.Windows;
public interface IFolderDialog
{
@ -15,12 +14,6 @@ namespace SystemTrayMenu.UserInterface.FolderBrowseDialog
string Folder { get; set; }
DialogResult ShowDialog();
DialogResult ShowDialog(IWin32Window owner);
DialogResult ShowVistaDialog(IWin32Window owner);
DialogResult ShowLegacyDialog(IWin32Window owner);
bool ShowDialog(Window owner);
}
}

View file

@ -1,26 +0,0 @@
// <copyright file="WindowWrapper.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu.UserInterface.FolderBrowseDialog
{
using System;
using System.Windows.Interop;
public class WindowWrapper : IWin32Window
{
/// <summary>
/// Initializes a new instance of the <see cref="WindowWrapper"/> class.
/// </summary>
/// <param name="handle">Handle to wrap.</param>
public WindowWrapper(IntPtr handle)
{
Handle = handle;
}
/// <summary>
/// Gets original ptr.
/// </summary>
public IntPtr Handle { get; }
}
}

View file

@ -864,7 +864,7 @@ namespace SystemTrayMenu.UserInterface
private void ButtonChange_Click(object sender, RoutedEventArgs e)
{
Config.SetFolderByUser(false);
Config.SetFolderByUser(this, false);
textBoxFolder.Text = Config.Path;
}
@ -937,7 +937,7 @@ namespace SystemTrayMenu.UserInterface
private void ButtonChangeIcoFolder_Click(object sender, RoutedEventArgs e)
{
Config.SetFolderIcoByUser();
Config.SetFolderIcoByUser(this);
textBoxIcoFolder.Text = Settings.Default.PathIcoDirectory;
}
@ -962,7 +962,7 @@ namespace SystemTrayMenu.UserInterface
using FolderDialog dialog = new();
dialog.InitialFolder = Config.Path;
if (dialog.ShowDialog() == FormsExtensions.DialogResult.OK)
if (dialog.ShowDialog(this))
{
dataGridViewFolders.Items.Add(new ListViewItemData(dialog.Folder, false, true));
EnableButtonAddStartMenu();

View file

@ -9,14 +9,6 @@ namespace SystemTrayMenu.Utilities
public static class FormsExtensions
{
public enum DialogResult
{
OK,
Cancel,
Ignore,
Retry,
}
public class NativeWindow : HwndSource
{
private HwndSourceHook hook;