From 4c4088b28aed6be95df1368e0d80f7006a667cd2 Mon Sep 17 00:00:00 2001 From: stax76 Date: Tue, 31 Oct 2023 09:44:20 +0100 Subject: [PATCH] environment variables and more --- docs/Changelog.md | 2 +- docs/Manual.md | 20 ++- src/MpvNet.Windows/GuiCommand.cs | 26 ++-- src/MpvNet.Windows/Help/WinMpvHelp.cs | 16 +++ src/MpvNet.Windows/Misc.cs | 15 --- src/MpvNet.Windows/WinForms/MainForm.cs | 161 +++++++++++++----------- src/MpvNet/Command.cs | 10 +- src/MpvNet/InputHelp.cs | 2 +- src/MpvNet/MpvNet.csproj | 1 + src/MpvNet/Player.cs | 6 +- 10 files changed, 140 insertions(+), 119 deletions(-) create mode 100644 src/MpvNet.Windows/Help/WinMpvHelp.cs delete mode 100644 src/MpvNet.Windows/Misc.cs diff --git a/docs/Changelog.md b/docs/Changelog.md index 37d1b19..6241db7 100644 --- a/docs/Changelog.md +++ b/docs/Changelog.md @@ -22,7 +22,7 @@ - Dark mode title bar enabled on Windows 10.0.18985 or higher. - The navigation bar on the left side of the config editor was changed from a simple list to a tree view. -- Support of the MPV_HOME environment variable that allows +- Support of the MPVNET_HOME environment variable that allows customizing the conf directory location. - Improved support for third party osc scripts like uosc. - Support of the mpv property `focused`. diff --git a/docs/Manual.md b/docs/Manual.md index 8bba706..11046cd 100644 --- a/docs/Manual.md +++ b/docs/Manual.md @@ -24,7 +24,7 @@ Table of contents * [Advanced Features](#advanced-features) * [Hidden Features](#hidden-features) * [Differences compared to mpv](#differences-compared-to-mpv) -* [Technical Overview](#technical-overview) +* [Environment Variables](#environment-variables) * [Context Menu Commands](#context-menu) @@ -614,22 +614,20 @@ mpv.net specific options are saved in the file mpvnet.conf and are just as mpv properties available on the command line. -Technical Overview ------------------- +Environment Variables +--------------------- -mpv.net is written in C# 7 and runs on the .NET Framework 4.8. +### MPVNET_HOME -The Extension implementation is based on the -[Managed Extensibility Framework](https://docs.microsoft.com/en-us/dotnet/framework/mef/). +Directory where mpv.net looks for user settings. -The main window is WinForms based because WinForms allows better libmpv integration -compared to WPF, all other windows are WPF based. +### MPVNET_VERSION -Third party components are: +Returns the version of mpv.net. -- [libmpv provides the core functionality](https://mpv.io/) -- [MediaInfo](https://mediaarea.net/en/MediaInfo) +Context Menu Commands +--------------------- ### Open > Open Files diff --git a/src/MpvNet.Windows/GuiCommand.cs b/src/MpvNet.Windows/GuiCommand.cs index f078f20..681d6fe 100644 --- a/src/MpvNet.Windows/GuiCommand.cs +++ b/src/MpvNet.Windows/GuiCommand.cs @@ -11,7 +11,6 @@ using MpvNet.Windows.WinForms; using MpvNet.Windows.WPF.Views; using MpvNet.Windows.WPF; using MpvNet.Windows.WPF.MsgBox; -using MpvNet.Windows.UI; namespace MpvNet; @@ -45,16 +44,16 @@ public class GuiCommand ["move-window"] = args => MoveWindow?.Invoke(args[0]), ["window-scale"] = args => WindowScaleNet?.Invoke(float.Parse(args[0], CultureInfo.InvariantCulture)), ["show-menu"] = args => ShowMenu?.Invoke(), - ["show-command-palette"] = args => ShowCommandPalette(), // deprecated ["show-info"] = args => ShowMediaInfo(new[] { "osd" }), // deprecated - ["playlist-random"] = args => PlaylistRandom(), // deprecated ["quick-bookmark"] = args => QuickBookmark(), // deprecated ["show-commands"] = args => ShowCommands(), // deprecated ["show-history"] = args => ShowHistory(), // deprecated ["show-playlist"] = args => ShowPlaylist(), // deprecated + + //["show-command-palette"] = args => ShowCommandPalette(), }; public void ShowDialog(Type winType) @@ -267,19 +266,14 @@ public class GuiCommand ProcessHelp.ShellExecute(file); } - public void ShowCommandPalette() - { - MainForm.Instance?.BeginInvoke(() => { - CommandPalette.Instance.SetItems(CommandPalette.GetItems()); - MainForm.Instance.ShowCommandPalette(); - CommandPalette.Instance.SelectFirst(); - }); - } - - // deprecated - public void PlaylistRandom() => - Msg.ShowInfo("This feature was moved to a user script,\nwhich can be found here:\n\n" + - "https://github.com/stax76/mpv-scripts/blob/main/misc.lua"); + //public void ShowCommandPalette() + //{ + // MainForm.Instance?.BeginInvoke(() => { + // CommandPalette.Instance.SetItems(CommandPalette.GetItems()); + // MainForm.Instance.ShowCommandPalette(); + // CommandPalette.Instance.SelectFirst(); + // }); + //} // deprecated public void QuickBookmark() => diff --git a/src/MpvNet.Windows/Help/WinMpvHelp.cs b/src/MpvNet.Windows/Help/WinMpvHelp.cs new file mode 100644 index 0000000..5125798 --- /dev/null +++ b/src/MpvNet.Windows/Help/WinMpvHelp.cs @@ -0,0 +1,16 @@ + +using MpvNet.ExtensionMethod; + +namespace MpvNet.Windows.Help; + +public class WinMpvHelp +{ + public static void CopyMpvNetCom() + { + string dir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData).AddSep() + + "Microsoft\\WindowsApps\\"; + + if (File.Exists(dir + "mpvnet.exe") && !File.Exists(dir + "mpvnet.com")) + File.Copy(Folder.Startup + "mpvnet.com", dir + "mpvnet.com"); + } +} diff --git a/src/MpvNet.Windows/Misc.cs b/src/MpvNet.Windows/Misc.cs deleted file mode 100644 index a49e896..0000000 --- a/src/MpvNet.Windows/Misc.cs +++ /dev/null @@ -1,15 +0,0 @@ -using MpvNet.ExtensionMethod; - -namespace MpvNet.Windows; - -public class Misc -{ - public static void CopyMpvnetCom() - { - string dir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData).AddSep() + - "Microsoft\\WindowsApps\\"; - - if (File.Exists(dir + "MpvNet.exe") && !File.Exists(dir + "MpvNet.com")) - File.Copy(Folder.Startup + "MpvNet.com", dir + "MpvNet.com"); - } -} diff --git a/src/MpvNet.Windows/WinForms/MainForm.cs b/src/MpvNet.Windows/WinForms/MainForm.cs index 063a070..30c2963 100644 --- a/src/MpvNet.Windows/WinForms/MainForm.cs +++ b/src/MpvNet.Windows/WinForms/MainForm.cs @@ -5,6 +5,7 @@ using System.Runtime.InteropServices; using System.Threading; using System.Windows.Forms; using System.Windows.Threading; +using System.Windows.Forms.Integration; using MpvNet.Windows.WPF; using MpvNet.Windows.UI; @@ -17,7 +18,7 @@ using WpfControls = System.Windows.Controls; using CommunityToolkit.Mvvm.Messaging; using static MpvNet.Windows.Native.WinApi; -using System.Windows.Forms.Integration; +using MpvNet.Windows.Help; namespace MpvNet.Windows.WinForms; @@ -25,7 +26,7 @@ public partial class MainForm : Form { public SnapManager SnapManager = new SnapManager(); public IntPtr MpvWindowHandle { get; set; } - public ElementHost CommandPaletteHost { get; set; } + public ElementHost? CommandPaletteHost { get; set; } public Dictionary MenuItemDuplicate = new Dictionary(); public bool WasShown { get; set; } public static MainForm? Instance { get; set; } @@ -39,6 +40,7 @@ public partial class MainForm : Form int _lastCycleFullscreen; int _taskbarButtonCreatedMessage; + bool _contextMenuIsReady; bool _wasMaximized; public MainForm() @@ -758,6 +760,8 @@ public partial class MainForm : Form menuItem.InputGestureText = tempBinding.Input; } } + + _contextMenuIsReady = true; } void Player_FileLoaded() @@ -901,11 +905,26 @@ public partial class MainForm : Form case 0x0290: // WM_IME_KEYDOWN case 0x0291: // WM_IME_KEYUP case 0x02a3: // WM_MOUSELEAVE - if (MpvWindowHandle == IntPtr.Zero) - MpvWindowHandle = FindWindowEx(Handle, IntPtr.Zero, "mpv", null); + { + bool ignore = false; - if (MpvWindowHandle != IntPtr.Zero) - m.Result = SendMessage(MpvWindowHandle, m.Msg, m.WParam, m.LParam); + if (m.Msg == 0x0100) // WM_KEYDOWN + { + Keys keyCode = (Keys)(int)m.WParam & Keys.KeyCode; + + if (keyCode == Keys.Escape && _contextMenuIsReady && ContextMenu!.IsOpen) + { + ignore = true; + ContextMenu!.IsOpen = false; + } + } + + if (MpvWindowHandle == IntPtr.Zero) + MpvWindowHandle = FindWindowEx(Handle, IntPtr.Zero, "mpv", null); + + if (MpvWindowHandle != IntPtr.Zero && !ignore) + m.Result = SendMessage(MpvWindowHandle, m.Msg, m.WParam, m.LParam); + } break; case 0x51: // WM_INPUTLANGCHANGE ActivateKeyboardLayout(m.LParam, 0x00000100u /*KLF_SETFORPROCESS*/); @@ -1187,7 +1206,7 @@ public partial class MainForm : Form InitAndBuildContextMenu(); Cursor.Position = new Point(Cursor.Position.X + 1, Cursor.Position.Y); GlobalHotkey.RegisterGlobalHotkeys(Handle); - TaskHelp.Run(Misc.CopyMpvnetCom); + TaskHelp.Run(WinMpvHelp.CopyMpvNetCom); WasShown = true; StrongReferenceMessenger.Default.Send(new MainWindowIsLoadedMessage()); //Player.Command("script-message-to mpvnet show-conf-editor"); @@ -1333,72 +1352,72 @@ public partial class MainForm : Form [DllImport("DwmApi")] static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, int[] attrValue, int attrSize); - protected override void OnLayout(LayoutEventArgs args) - { - base.OnLayout(args); - AdjustCommandPaletteLeftAndWidth(); - } + //protected override void OnLayout(LayoutEventArgs args) + //{ + // base.OnLayout(args); + // AdjustCommandPaletteLeftAndWidth(); + //} - class ElementHostEx : ElementHost - { - protected override void OnHandleCreated(EventArgs e) - { - base.OnHandleCreated(e); - const int LWA_ColorKey = 1; + //class ElementHostEx : ElementHost + //{ + // protected override void OnHandleCreated(EventArgs e) + // { + // base.OnHandleCreated(e); + // const int LWA_ColorKey = 1; - if (Environment.OSVersion.Version > new Version(10, 0)) - SetLayeredWindowAttributes(Handle, 0x111111, 255, LWA_ColorKey); - } + // if (Environment.OSVersion.Version > new Version(10, 0)) + // SetLayeredWindowAttributes(Handle, 0x111111, 255, LWA_ColorKey); + // } - protected override CreateParams CreateParams - { - get - { - CreateParams cp = base.CreateParams; + // protected override CreateParams CreateParams + // { + // get + // { + // CreateParams cp = base.CreateParams; - if (Environment.OSVersion.Version > new Version(10, 0)) - cp.ExStyle |= 0x00080000; // WS_EX_LAYERED + // if (Environment.OSVersion.Version > new Version(10, 0)) + // cp.ExStyle |= 0x00080000; // WS_EX_LAYERED - cp.ExStyle |= 0x00000008; // WS_EX_TOPMOST + // cp.ExStyle |= 0x00000008; // WS_EX_TOPMOST - cp.Style |= 0x04000000; //WS_CLIPSIBLINGS - cp.Style |= 0x02000000; //WS_CLIPCHILDREN + // cp.Style |= 0x04000000; //WS_CLIPSIBLINGS + // cp.Style |= 0x02000000; //WS_CLIPCHILDREN - return cp; - } - } + // return cp; + // } + // } - protected override bool ProcessCmdKey(ref Message msg, Keys keyData) - { - try - { - return base.ProcessCmdKey(ref msg, keyData); - } - catch (Exception) - { - return true; - } - } + // protected override bool ProcessCmdKey(ref Message msg, Keys keyData) + // { + // try + // { + // return base.ProcessCmdKey(ref msg, keyData); + // } + // catch (Exception) + // { + // return true; + // } + // } - [DllImport("user32.dll")] - public static extern bool SetLayeredWindowAttributes(IntPtr hWnd, int crKey, byte alpha, int dwFlags); - } + // [DllImport("user32.dll")] + // public static extern bool SetLayeredWindowAttributes(IntPtr hWnd, int crKey, byte alpha, int dwFlags); + //} - public void ShowCommandPalette() - { - if (CommandPaletteHost == null) - { - CommandPaletteHost = new ElementHostEx(); - CommandPaletteHost.Dock = DockStyle.Fill; - CommandPaletteHost.BackColor = Color.FromArgb(0x111111); + //public void ShowCommandPalette() + //{ + // if (CommandPaletteHost == null) + // { + // CommandPaletteHost = new ElementHostEx(); + // CommandPaletteHost.Dock = DockStyle.Fill; + // CommandPaletteHost.BackColor = Color.FromArgb(0x111111); - AdjustCommandPaletteLeftAndWidth(); - CommandPaletteHost.Child = CommandPalette.Instance; - CommandPalette.Instance.AdjustHeight(); - Controls.Add(CommandPaletteHost); - CommandPaletteHost.BringToFront(); - } - } + // AdjustCommandPaletteLeftAndWidth(); + // CommandPaletteHost.Child = CommandPalette.Instance; + // CommandPalette.Instance.AdjustHeight(); + // Controls.Add(CommandPaletteHost); + // CommandPaletteHost.BringToFront(); + // } + //} public void HideCommandPalette() { @@ -1419,16 +1438,16 @@ public partial class MainForm : Form } } - void AdjustCommandPaletteLeftAndWidth() - { - if (CommandPaletteHost == null) - return; + //void AdjustCommandPaletteLeftAndWidth() + //{ + // if (CommandPaletteHost == null) + // return; - CommandPaletteHost.Width = FontHeight * 26; + // CommandPaletteHost.Width = FontHeight * 26; - if (CommandPaletteHost.Width > ClientSize.Width) - CommandPaletteHost.Width = ClientSize.Width; + // if (CommandPaletteHost.Width > ClientSize.Width) + // CommandPaletteHost.Width = ClientSize.Width; - CommandPaletteHost.Left = (ClientSize.Width - CommandPaletteHost.Size.Width) / 2; - } + // CommandPaletteHost.Left = (ClientSize.Width - CommandPaletteHost.Size.Width) / 2; + //} } diff --git a/src/MpvNet/Command.cs b/src/MpvNet/Command.cs index 75f7632..c9492ce 100644 --- a/src/MpvNet/Command.cs +++ b/src/MpvNet/Command.cs @@ -25,7 +25,8 @@ public class Command ["cycle-audio"] = args => CycleAudio(), // deprecated ["cycle-subtitles"] = args => CycleSubtitles(), // deprecated ["playlist-first"] = args => PlaylistFirst(), // deprecated - ["playlist-last"] = args => PlaylistLast(), // deprecated + ["playlist-last"] = args => PlaylistLast(), // deprecated + ["playlist-random"] = args => PlaylistRandom(), // deprecated }; public string FormatTime(double value) => ((int)value).ToString("00"); @@ -175,4 +176,11 @@ public class Command if (Player.PlaylistPos < count - 1) Player.SetPropertyInt("playlist-pos", count - 1); } + + // deprecated + public static void PlaylistRandom() + { + int count = Player.GetPropertyInt("playlist-count"); + Player.SetPropertyInt("playlist-pos", new Random().Next(count)); + } } diff --git a/src/MpvNet/InputHelp.cs b/src/MpvNet/InputHelp.cs index 57e3d3c..5d0c3d6 100644 --- a/src/MpvNet/InputHelp.cs +++ b/src/MpvNet/InputHelp.cs @@ -69,7 +69,7 @@ public static class InputHelp new (_("Video"), _("Take Screenshot"), "async screenshot", "s"), new (_("Video"), _("Take Screenshot without subtitles"), "async screenshot video", "S"), new (_("Video"), _("Toggle Deinterlace"), "cycle deinterlace", "d"), - new (_("Video"), _("Cycle Aspect Ratio"), "cycle-values video-aspect 16:9 4:3 2.35:1 -1", "a"), + new (_("Video"), _("Cycle Aspect Ratio"), "cycle-values video-aspect-override 16:9 4:3 2.35:1 -1", "a"), new (_("Video"), _("Rotate Video"), "cycle-values video-rotate 90 180 270 0", "Ctrl+r"), new (_("Audio"), _("Cycle/Next"), "cycle audio", "KP7"), new (_("Audio"), "-"), diff --git a/src/MpvNet/MpvNet.csproj b/src/MpvNet/MpvNet.csproj index 1e16339..9bb79be 100644 --- a/src/MpvNet/MpvNet.csproj +++ b/src/MpvNet/MpvNet.csproj @@ -6,6 +6,7 @@ mpv.net enable MpvNet + false diff --git a/src/MpvNet/Player.cs b/src/MpvNet/Player.cs index 48d88ad..e4617fe 100644 --- a/src/MpvNet/Player.cs +++ b/src/MpvNet/Player.cs @@ -208,10 +208,10 @@ public class MainPlayer : MpvClient get { if (_configFolder == null) { - string? mpv_home = Environment.GetEnvironmentVariable("MPV_HOME"); + string? mpvnet_home = Environment.GetEnvironmentVariable("MPVNET_HOME"); - if (Directory.Exists(mpv_home)) - return _configFolder = mpv_home.AddSep(); + if (Directory.Exists(mpvnet_home)) + return _configFolder = mpvnet_home.AddSep(); _configFolder = Folder.Startup + "portable_config";