From 69a5ba44706263b4a93f4c3e6e0f811f686217cc Mon Sep 17 00:00:00 2001 From: stax76 Date: Sat, 25 Jun 2022 13:07:17 +0200 Subject: [PATCH] Fix #445 chapters that are script created after the media file is loaded --- docs/Changelog.md | 1 + src/Misc/Commands.cs | 2 +- src/Misc/Player.cs | 28 ++++++++++++++-------------- src/WinForms/MainForm.cs | 23 ++++++++++------------- 4 files changed, 26 insertions(+), 28 deletions(-) diff --git a/docs/Changelog.md b/docs/Changelog.md index c8913ed..673ee60 100644 --- a/docs/Changelog.md +++ b/docs/Changelog.md @@ -11,6 +11,7 @@ - Fix script compatibility with mordenx and mpv-osc-tethys. - Fix borderless window not resizable with mouse. - Fix start-size=session not working. +- Fix chapters that are script created after the media file is loaded. - Width of command palette slightly increased. - libmpv zhongfly 2022-06-19 diff --git a/src/Misc/Commands.cs b/src/Misc/Commands.cs index 19830fb..f3fced1 100644 --- a/src/Misc/Commands.cs +++ b/src/Misc/Commands.cs @@ -672,7 +672,7 @@ namespace mpvnet public static void ShowChapters() => App.InvokeOnMainThread(() => { - var items = Core.Chapters.Select(i => new CommandPaletteItem(i.Title, i.TimeDisplay, () => + var items = Core.GetChapters().Select(i => new CommandPaletteItem(i.Title, i.TimeDisplay, () => Core.CommandV("seek", i.Time.ToString(CultureInfo.InvariantCulture), "absolute"))); CommandPalette.Instance.SetItems(items); diff --git a/src/Misc/Player.cs b/src/Misc/Player.cs index 191245f..bb0f60b 100644 --- a/src/Misc/Player.cs +++ b/src/Misc/Player.cs @@ -74,7 +74,6 @@ namespace mpvnet public AutoResetEvent VideoSizeAutoResetEvent { get; } = new AutoResetEvent(false); public IntPtr Handle { get; set; } public IntPtr NamedHandle { get; set; } - public List Chapters { get; set; } = new List(); public List MediaTracks { get; set; } = new List(); public List BluRayTitles { get; } = new List(); public object MediaTracksLock { get; } = new object(); @@ -1450,25 +1449,26 @@ namespace mpvnet else MediaTracks = GetTracks(); } + } - lock (Chapters) + public List GetChapters() { + List chapters = new List(); + int count = GetPropertyInt("chapter-list/count"); + + for (int x = 0; x < count; x++) { - Chapters.Clear(); - int count = GetPropertyInt("chapter-list/count"); + string title = GetPropertyString($"chapter-list/{x}/title"); + double time = GetPropertyDouble($"chapter-list/{x}/time"); - for (int x = 0; x < count; x++) - { - string title = GetPropertyString($"chapter-list/{x}/title"); - double time = GetPropertyDouble($"chapter-list/{x}/time"); + if (string.IsNullOrEmpty(title) || + (title.Length == 12 && title.Contains(":") && title.Contains("."))) - if (string.IsNullOrEmpty(title) || - (title.Length == 12 && title.Contains(":") && title.Contains("."))) + title = "Chapter " + (x + 1); - title = "Chapter " + (x + 1); - - Chapters.Add(new Chapter() { Title = title, Time = time }); - } + chapters.Add(new Chapter() { Title = title, Time = time }); } + + return chapters; } public void UpdateExternalTracks() diff --git a/src/WinForms/MainForm.cs b/src/WinForms/MainForm.cs index 7a1fc8c..ffefd64 100644 --- a/src/WinForms/MainForm.cs +++ b/src/WinForms/MainForm.cs @@ -293,21 +293,18 @@ namespace mpvnet } } - lock (Core.Chapters) + var chaptersMenuItem = FindMenuItem("Chapters"); + + if (chaptersMenuItem != null) { - var chaptersMenuItem = FindMenuItem("Chapters"); + chaptersMenuItem.Items.Clear(); - if (chaptersMenuItem != null) + foreach (Chapter chapter in Core.GetChapters()) { - chaptersMenuItem.Items.Clear(); - - foreach (Chapter chapter in Core.Chapters) - { - var chapterMenuItem = new WpfControls.MenuItem() { Header = chapter.Title }; - chapterMenuItem.InputGestureText = chapter.TimeDisplay; - chapterMenuItem.Click += (sender, args) => Core.CommandV("seek", chapter.Time.ToString(CultureInfo.InvariantCulture), "absolute"); - chaptersMenuItem.Items.Add(chapterMenuItem); - } + var chapterMenuItem = new WpfControls.MenuItem() { Header = chapter.Title }; + chapterMenuItem.InputGestureText = chapter.TimeDisplay; + chapterMenuItem.Click += (sender, args) => Core.CommandV("seek", chapter.Time.ToString(CultureInfo.InvariantCulture), "absolute"); + chaptersMenuItem.Items.Add(chapterMenuItem); } } @@ -1333,7 +1330,7 @@ namespace mpvnet if (CommandPaletteHost == null) return; - CommandPaletteHost.Width = FontHeight * 27; + CommandPaletteHost.Width = FontHeight * 26; if (CommandPaletteHost.Width > ClientSize.Width) CommandPaletteHost.Width = ClientSize.Width;