diff --git a/src/.editorconfig b/src/.editorconfig index 480ec2a..82d0636 100644 --- a/src/.editorconfig +++ b/src/.editorconfig @@ -17,3 +17,6 @@ dotnet_diagnostic.IDE0011.severity = none # IDE0010: Add missing cases dotnet_diagnostic.IDE0010.severity = none + +# IDE0044: Add readonly modifier +dotnet_diagnostic.IDE0044.severity = silent diff --git a/src/Misc/Commands.cs b/src/Misc/Commands.cs index fd5bc13..60a3283 100644 --- a/src/Misc/Commands.cs +++ b/src/Misc/Commands.cs @@ -74,8 +74,11 @@ namespace mpvnet foreach (string arg in args) { - if (arg == "append") append = true; - if (arg == "no-folder") loadFolder = false; + if (arg == "append") + append = true; + + if (arg == "no-folder") + loadFolder = false; } App.InvokeOnMainThread(new Action(() => { @@ -88,9 +91,10 @@ namespace mpvnet public static void Open_DVD_Or_BD_Folder() { App.InvokeOnMainThread(new Action(() => { - using (var dialog = new FolderBrowser()) - if (dialog.ShowDialog() == DialogResult.OK) - Core.LoadDiskFolder(dialog.SelectedPath); + var dialog = new FolderBrowser(); + + if (dialog.Show()) + Core.LoadDiskFolder(dialog.SelectedPath); })); } diff --git a/src/Misc/FolderBrowser.cs b/src/Misc/FolderBrowser.cs index 6dd85c1..cf283d2 100644 --- a/src/Misc/FolderBrowser.cs +++ b/src/Misc/FolderBrowser.cs @@ -1,25 +1,43 @@  using System; -using System.Windows.Forms; -using System.Reflection; -using System.IO; -using System.Runtime.InteropServices; using System.Diagnostics; +using System.IO; +using System.Reflection; +using System.Runtime.InteropServices; +using System.Windows.Forms; namespace mpvnet { - public partial class FolderBrowser : CommonDialog + public class FolderBrowser { - BetterFolderBrowserDialog _dialog = new BetterFolderBrowserDialog(); + public string SelectedPath { get; set; } - public string SelectedPath { - get => _dialog.FileName; - set => _dialog.FileName = value; + string _initialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); + + public string InitialDirectory { + get => _initialDirectory; + set { + if (Directory.Exists(value)) + _initialDirectory = value; + } } - public new DialogResult ShowDialog() + public bool Show() => Show(GetOwnerHandle()); + + public bool Show(IntPtr hWndOwner) { - return _dialog.ShowDialog(GetOwnerHandle()) ? DialogResult.OK : DialogResult.Cancel; + ShowDialogResult result = VistaDialog.Show(hWndOwner, InitialDirectory); + + if (result.Result) + SelectedPath = result.FileName; + + return result.Result; + } + + struct ShowDialogResult + { + public bool Result { get; set; } + public string FileName { get; set; } } public static IntPtr GetOwnerHandle() @@ -40,168 +58,67 @@ namespace mpvnet [DllImport("user32.dll")] public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); - protected override bool RunDialog(IntPtr hwndOwner) => _dialog.ShowDialog(hwndOwner); - - public override void Reset() { } - - class BetterFolderBrowserDialog + static class VistaDialog { - OpenFileDialog ofd = null; + const string foldersFilter = "Folders|\n"; + const BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; + static Assembly windowsFormsAssembly = typeof(FileDialog).Assembly; + static Type iFileDialogType = windowsFormsAssembly.GetType("System.Windows.Forms.FileDialogNative+IFileDialog"); + static MethodInfo createVistaDialogMethodInfo = typeof(OpenFileDialog).GetMethod("CreateVistaDialog", flags); + static MethodInfo onBeforeVistaDialogMethodInfo = typeof(OpenFileDialog).GetMethod("OnBeforeVistaDialog", flags); + static MethodInfo getOptionsMethodInfo = typeof(FileDialog).GetMethod("GetOptions", flags); + static MethodInfo setOptionsMethodInfo = iFileDialogType.GetMethod("SetOptions", flags); + static uint fosPickFoldersBitFlag = (uint)windowsFormsAssembly + .GetType("System.Windows.Forms.FileDialogNative+FOS") + .GetField("FOS_PICKFOLDERS") + .GetValue(null); + static ConstructorInfo vistaDialogEventsConstructorInfo = windowsFormsAssembly + .GetType("System.Windows.Forms.FileDialog+VistaDialogEvents") + .GetConstructor(flags, null, new[] { typeof(FileDialog) }, null); + static MethodInfo adviseMethodInfo = iFileDialogType.GetMethod("Advise"); + static MethodInfo unAdviseMethodInfo = iFileDialogType.GetMethod("Unadvise"); + static MethodInfo showMethodInfo = iFileDialogType.GetMethod("Show"); - public BetterFolderBrowserDialog() + public static ShowDialogResult Show(IntPtr ownerHandle, string initialDirectory) { - ofd = new OpenFileDialog(); + var openFileDialog = new OpenFileDialog + { + AddExtension = false, + CheckFileExists = false, + DereferenceLinks = true, + Filter = foldersFilter, + InitialDirectory = initialDirectory, + Multiselect = false, + }; - ofd.Filter = "Folders|" + "\n"; - ofd.AddExtension = false; - ofd.CheckFileExists = false; - ofd.DereferenceLinks = true; - ofd.Multiselect = false; - } - - public string FileName { - get => ofd.FileName; - set { - if (Directory.Exists(value)) - ofd.InitialDirectory = value; - } - } - - public bool ShowDialog() => ShowDialog(IntPtr.Zero); - - public bool ShowDialog(IntPtr hWndOwner) - { - bool flag = false; - - var r = new Reflector("System.Windows.Forms"); - - uint num = 0; - Type typeIFileDialog = r.GetTypo("FileDialogNative.IFileDialog"); - object dialog = r.Call(ofd, "CreateVistaDialog"); - r.Call(ofd, "OnBeforeVistaDialog", dialog); - - uint options = Convert.ToUInt32(r.CallAs(typeof(FileDialog), ofd, "GetOptions")); - options |= Convert.ToUInt32(r.GetEnum("FileDialogNative.FOS", "FOS_PICKFOLDERS")); - r.CallAs(typeIFileDialog, dialog, "SetOptions", options); - - object pfde = r.New("FileDialog.VistaDialogEvents", ofd); - object[] parameters = new object[] { pfde, num }; - r.CallAs2(typeIFileDialog, dialog, "Advise", parameters); - - num = Convert.ToUInt32(parameters[1]); + var iFileDialog = createVistaDialogMethodInfo.Invoke(openFileDialog, new object[] { }); + onBeforeVistaDialogMethodInfo.Invoke(openFileDialog, new[] { iFileDialog }); + setOptionsMethodInfo.Invoke(iFileDialog, new object[] { (uint)getOptionsMethodInfo.Invoke(openFileDialog, new object[] { }) | fosPickFoldersBitFlag }); + var adviseParametersWithOutputConnectionToken = new[] { vistaDialogEventsConstructorInfo.Invoke(new object[] { openFileDialog }), 0U }; + adviseMethodInfo.Invoke(iFileDialog, adviseParametersWithOutputConnectionToken); try { - int num2 = Convert.ToInt32(r.CallAs(typeIFileDialog, dialog, "Show", hWndOwner)); - flag = 0 == num2; + int retVal = (int)showMethodInfo.Invoke(iFileDialog, new object[] { ownerHandle }); + + return new ShowDialogResult + { + Result = retVal == 0, + FileName = openFileDialog.FileName + }; } finally { - r.CallAs(typeIFileDialog, dialog, "Unadvise", num); - GC.KeepAlive(pfde); + unAdviseMethodInfo.Invoke(iFileDialog, new[] { adviseParametersWithOutputConnectionToken[1] }); } - - return flag; } } class WindowWrapper : IWin32Window { - IntPtr _hwnd; - - public IntPtr Handle => _hwnd; - - public WindowWrapper(IntPtr handle) => _hwnd = handle; - } - - class Reflector - { - string m_ns; - Assembly m_asmb; - - public Reflector(string ns) : this(ns, ns) { } - - public Reflector(string an__1, string ns) - { - m_ns = ns; - m_asmb = null; - - foreach (AssemblyName aN__2 in Assembly.GetExecutingAssembly().GetReferencedAssemblies()) - { - if (aN__2.FullName.StartsWith(an__1)) - { - m_asmb = Assembly.Load(aN__2); - break; - } - } - } - - public Type GetTypo(string typeName) - { - Type type = null; - string[] names = typeName.Split('.'); - - if (names.Length > 0) - type = m_asmb.GetType(m_ns + Convert.ToString(".") + names[0]); - - for (int i = 1; i < names.Length; i++) - type = type.GetNestedType(names[i], BindingFlags.NonPublic); - - return type; - } - - public object New(string name, params object[] parameters) - { - Type type = GetTypo(name); - ConstructorInfo[] ctorInfos = type.GetConstructors(); - - foreach (ConstructorInfo ci in ctorInfos) - { - try { - return ci.Invoke(parameters); - } catch { } - } - - return null; - } - - public object Call(object obj, string func, params object[] parameters) - { - return Call2(obj, func, parameters); - } - - public object Call2(object obj, string func, object[] parameters) - { - return CallAs2(obj.GetType(), obj, func, parameters); - } - - public object CallAs(Type type, object obj, string func, params object[] parameters) - { - return CallAs2(type, obj, func, parameters); - } - - public object CallAs2(Type type, object obj, string func, object[] parameters) - { - MethodInfo methInfo = type.GetMethod( - func, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); - return methInfo.Invoke(obj, parameters); - } - - public object Get(object obj, string prop) => GetAs(obj.GetType(), obj, prop); - - public object GetAs(Type type, object obj, string prop) - { - PropertyInfo propInfo = type.GetProperty( - prop, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); - return propInfo.GetValue(obj, null); - } - - public object GetEnum(string typeName, string name) - { - Type type = GetTypo(typeName); - FieldInfo fieldInfo = type.GetField(name); - return fieldInfo.GetValue(null); - } + IntPtr _handle; + public WindowWrapper(IntPtr handle) { _handle = handle; } + public IntPtr Handle => _handle; } } } diff --git a/src/WPF/StringSettingControl.xaml.cs b/src/WPF/StringSettingControl.xaml.cs index 8dda154..d6a03e1 100644 --- a/src/WPF/StringSettingControl.xaml.cs +++ b/src/WPF/StringSettingControl.xaml.cs @@ -62,13 +62,11 @@ namespace DynamicGUI switch (StringSetting.Type) { case "folder": - using (FolderBrowser fb = new FolderBrowser()) - { - fb.SelectedPath = ValueTextBox.Text; + FolderBrowser fb = new FolderBrowser(); + fb.InitialDirectory = ValueTextBox.Text; - if (fb.ShowDialog() == WinForms.DialogResult.OK) - ValueTextBox.Text = fb.SelectedPath; - } + if (fb.Show()) + ValueTextBox.Text = fb.SelectedPath; break; case "color": using (var dialog = new WinForms.ColorDialog()) diff --git a/src/mpv.net.csproj b/src/mpv.net.csproj index 47809eb..a8305ae 100644 --- a/src/mpv.net.csproj +++ b/src/mpv.net.csproj @@ -78,10 +78,8 @@ - - Component - +