improved menu performance and folder browser fix

This commit is contained in:
Frank Skare
2021-09-05 08:23:42 +02:00
parent e4c65cd84e
commit 7a6b2011e4
7 changed files with 49 additions and 115 deletions

View File

@@ -1,15 +1,15 @@

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Interop;
using System.Windows;
using static mpvnet.Global;
using System.Collections.Generic;
using System.Text;
namespace mpvnet
{
@@ -88,7 +88,7 @@ namespace mpvnet
public static void Open_DVD_Or_BD_Folder()
{
App.InvokeOnMainThread(new Action(() => {
using (var dialog = new BetterFolderBrowser())
using (var dialog = new FolderBrowser())
if (dialog.ShowDialog() == DialogResult.OK)
Core.LoadDiskFolder(dialog.SelectedPath);
}));

View File

@@ -1,94 +1,48 @@

// https://github.com/Willy-Kimura/BetterFolderBrowser
using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Reflection;
using System.IO;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace mpvnet
{
public partial class BetterFolderBrowser : CommonDialog
public partial class FolderBrowser : CommonDialog
{
IContainer components = null;
BetterFolderBrowserDialog _dialog = new BetterFolderBrowserDialog();
public BetterFolderBrowser()
{
InitializeComponent();
SetDefaults();
}
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
components.Dispose();
base.Dispose(disposing);
}
void InitializeComponent() => components = new Container();
public string Title
{
get { return _dialog.Title; }
set { _dialog.Title = value; }
}
public string RootFolder
{
get { return _dialog.InitialDirectory; }
set { _dialog.InitialDirectory = value; }
}
public bool Multiselect
{
get { return _dialog.AllowMultiselect; }
set { _dialog.AllowMultiselect = value; }
}
public string SelectedPath => _dialog.FileName;
public string[] SelectedPaths => _dialog.FileNames;
public string SelectedFolder => _dialog.FileName;
public string[] SelectedFolders => _dialog.FileNames;
void SetDefaults()
{
_dialog.AllowMultiselect = false;
_dialog.Title = "Please select a folder...";
_dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
public string SelectedPath {
get => _dialog.FileName;
set => _dialog.FileName = value;
}
public new DialogResult ShowDialog()
{
DialogResult result;
if (_dialog.ShowDialog(IntPtr.Zero))
result = DialogResult.OK;
else
result = DialogResult.Cancel;
return result;
return _dialog.ShowDialog(GetOwnerHandle()) ? DialogResult.OK : DialogResult.Cancel;
}
public new DialogResult ShowDialog(IWin32Window owner)
public static IntPtr GetOwnerHandle()
{
DialogResult result;
IntPtr foregroundWindow = GetForegroundWindow();
GetWindowThreadProcessId(foregroundWindow, out var procID);
if (_dialog.ShowDialog(owner.Handle))
result = DialogResult.OK;
else
result = DialogResult.Cancel;
using (var proc = Process.GetCurrentProcess())
if (proc.Id == procID)
return foregroundWindow;
return result;
return IntPtr.Zero;
}
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[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() => SetDefaults();
public override void Reset() { }
class BetterFolderBrowserDialog
{
@@ -105,27 +59,14 @@ namespace mpvnet
ofd.Multiselect = false;
}
public bool AllowMultiselect {
get { return ofd.Multiselect; }
set { ofd.Multiselect = value; }
}
public string[] FileNames => ofd.FileNames;
public string InitialDirectory {
get { return ofd.InitialDirectory; }
public string FileName {
get => ofd.FileName;
set {
ofd.InitialDirectory = (value == null || value.Length == 0) ? Environment.CurrentDirectory : value;
if (Directory.Exists(value))
ofd.InitialDirectory = value;
}
}
public string Title {
get { return ofd.Title; }
set { ofd.Title = (value == null) ? "Select a folder" : value; }
}
public string FileName => ofd.FileName;
public bool ShowDialog() => ShowDialog(IntPtr.Zero);
public bool ShowDialog(IntPtr hWndOwner)
@@ -201,7 +142,7 @@ namespace mpvnet
string[] names = typeName.Split('.');
if (names.Length > 0)
type = m_asmb.GetType((m_ns + Convert.ToString(".")) + names[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);

View File

@@ -9,12 +9,13 @@ using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
using System.Windows.Threading;
using MsgBoxEx;
using WpfControls = System.Windows.Controls;
using static mpvnet.Native;
using static mpvnet.Global;
using MsgBoxEx;
namespace mpvnet
{
@@ -639,6 +640,8 @@ namespace mpvnet
try {
App.RunTask(() => {
MenuAutoResetEvent.WaitOne();
System.Windows.Application.Current.Dispatcher.Invoke(
DispatcherPriority.Background, new Action(delegate { }));
Core.Command(item.Command);
});
}

View File

@@ -7,6 +7,7 @@ using System.ComponentModel;
using System.Diagnostics;
using System.Drawing.Text;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
@@ -101,8 +102,8 @@ namespace MsgBoxEx
public static IntPtr GetOwnerHandle()
{
IntPtr foregroundWindow = Native.GetForegroundWindow();
Native.GetWindowThreadProcessId(foregroundWindow, out var procID);
IntPtr foregroundWindow = GetForegroundWindow();
GetWindowThreadProcessId(foregroundWindow, out var procID);
using (var proc = Process.GetCurrentProcess())
if (proc.Id == procID)
@@ -111,6 +112,12 @@ namespace MsgBoxEx
return IntPtr.Zero;
}
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
public static Color ColorFromString(string colorString)
{
Color wpfColor = Colors.Black;

View File

@@ -1,15 +0,0 @@

using System;
using System.Runtime.InteropServices;
namespace MsgBoxEx
{
class Native
{
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
}
}

View File

@@ -62,13 +62,12 @@ namespace DynamicGUI
switch (StringSetting.Type)
{
case "folder":
using (var d = new WinForms.FolderBrowserDialog())
using (FolderBrowser fb = new FolderBrowser())
{
d.Description = "Choose a folder.";
d.SelectedPath = ValueTextBox.Text;
fb.SelectedPath = ValueTextBox.Text;
if (d.ShowDialog() == WinForms.DialogResult.OK)
ValueTextBox.Text = d.SelectedPath;
if (fb.ShowDialog() == WinForms.DialogResult.OK)
ValueTextBox.Text = fb.SelectedPath;
}
break;
case "color":

View File

@@ -78,7 +78,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Misc\App.cs" />
<Compile Include="Misc\BetterFolderBrowser.cs">
<Compile Include="Misc\FolderBrowser.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Misc\Common.cs" />
@@ -112,7 +112,6 @@
<Compile Include="WPF\MsgBox\MsgBoxExtendedFunctionality.cs" />
<Compile Include="WPF\MsgBox\MsgBoxUrl.cs" />
<Compile Include="WPF\MsgBox\MsgEnumerators.cs" />
<Compile Include="WPF\MsgBox\Native.cs" />
<Compile Include="WPF\RelayCommand.cs" />
<Compile Include="Misc\CSharpScriptHost.cs" />
<Compile Include="Misc\Extension.cs" />