From bac8b2b96c0990f119dfe616a43ac7a9288bc8f2 Mon Sep 17 00:00:00 2001 From: Frank Skare Date: Tue, 22 Jun 2021 19:10:27 +0200 Subject: [PATCH] misc --- src/Misc/Commands.cs | 53 +++++---- src/Misc/Help.cs | 14 +++ src/Misc/Misc.cs | 17 +++ src/WPF/CommandPaletteControl.xaml | 75 ++++++++++-- src/WPF/CommandPaletteControl.xaml.cs | 138 ++++++++++++++++++++--- src/WPF/CommandPaletteWindow.xaml | 66 ----------- src/WPF/CommandPaletteWindow.xaml.cs | 128 --------------------- src/WPF/ConfWindow.xaml | 12 +- src/WPF/InputWindow.xaml | 11 +- src/WPF/RelayCommand.cs | 27 +++++ src/WPF/Resources.xaml | 7 +- src/WPF/SearchTextBoxUserControl.xaml | 12 +- src/WPF/SearchTextBoxUserControl.xaml.cs | 12 +- src/WinForms/MainForm.cs | 53 ++++++++- src/mpv.net.csproj | 9 +- 15 files changed, 363 insertions(+), 271 deletions(-) delete mode 100644 src/WPF/CommandPaletteWindow.xaml delete mode 100644 src/WPF/CommandPaletteWindow.xaml.cs create mode 100644 src/WPF/RelayCommand.cs diff --git a/src/Misc/Commands.cs b/src/Misc/Commands.cs index cc7fc90..f17b94b 100644 --- a/src/Misc/Commands.cs +++ b/src/Misc/Commands.cs @@ -9,6 +9,7 @@ using System.Windows.Interop; using System.Windows; using static mpvnet.Global; +using System.Collections.Generic; namespace mpvnet { @@ -44,7 +45,7 @@ namespace mpvnet case "show-keys": ShowTextWithEditor("input-key-list", Core.get_property_string("input-key-list").Replace(",", BR)); break; case "show-media-info": ShowMediaInfo(args); break; case "show-media-search": ShowDialog(typeof(EverythingWindow)); break; - case "show-playlist": ShowPlaylist(args); break; + case "show-playlist": ShowPlaylist(); break; case "show-profiles": ShowTextWithEditor("profile-list", mpvHelp.GetProfiles()); break; case "show-properties": ShowProperties(); break; case "show-protocols": ShowTextWithEditor("protocol-list", mpvHelp.GetProtocols()); break; @@ -360,23 +361,6 @@ namespace mpvnet "}${osd-ass-cc/1}" + text + "\" " + duration); } - public static void ShowPlaylist(string[] args) - { - int duration = 5000; - - if (args.Length == 1 && int.TryParse(args[0], out int result)) - duration = result; - - var size = Core.get_property_number("osd-font-size"); - Core.set_property_number("osd-font-size", 40); - Core.command("show-text ${playlist} " + duration); - - App.RunTask(() => { - Thread.Sleep(6000); - Core.set_property_number("osd-font-size", size); - }); - } - public static void ShowMediaInfo(string[] args) { string path = Core.GetPropertyString("path"); @@ -393,9 +377,38 @@ namespace mpvnet } } - public static void ShowCommandPalette() - { + public static void ShowCommandPalette() => App.InvokeOnMainThread(ShowCommandPaletteInternal); + static void ShowCommandPaletteInternal() + { + CommandPalette.Instance.SetItems(CommandPalette.GetItems()); + MainForm.Instance.ShowCommandPalette(); + } + + public static void ShowPlaylist() => App.InvokeOnMainThread(ShowPlaylistInternal); + + static void ShowPlaylistInternal() + { + int count = Core.get_property_int("playlist-count"); + + if (count <= 0) + return; + + List items = new List(); + + for (int i = 0; i < count; i++) + { + int index = i; + string file = Core.get_property_string($"playlist/{i}/filename"); + CommandPaletteItem item = new CommandPaletteItem() { + Text = PathHelp.GetFileName(file), + Action = () => Core.set_property_int("playlist-pos", index) + }; + items.Add(item); + } + + CommandPalette.Instance.SetItems(items); + MainForm.Instance.ShowCommandPalette(); } } } diff --git a/src/Misc/Help.cs b/src/Misc/Help.cs index 7e04cc3..ef1f6fa 100644 --- a/src/Misc/Help.cs +++ b/src/Misc/Help.cs @@ -14,6 +14,20 @@ using static mpvnet.Global; namespace mpvnet { + public static class PathHelp + { + public static string GetFileName(string path) + { + if (string.IsNullOrEmpty(path)) + return ""; + + if (path.Contains(Path.DirectorySeparatorChar)) + return path.Substring(path.LastIndexOf(Path.DirectorySeparatorChar) + 1); + + return path; + } + } + public static class StringHelp { public static string GetMD5Hash(string txt) diff --git a/src/Misc/Misc.cs b/src/Misc/Misc.cs index c004f37..60cd03d 100644 --- a/src/Misc/Misc.cs +++ b/src/Misc/Misc.cs @@ -228,4 +228,21 @@ namespace mpvnet } } } + + public class CommandPaletteItem + { + public string Text { get; set; } = ""; + public string SecondaryText { get; set; } = ""; + public Action Action { get; set; } + } + + public class CommandPalette + { + public static CommandPaletteControl Instance { get; } = new CommandPaletteControl(); + + public static IEnumerable GetItems() + { + return CommandItem.Items.Select(i => new CommandPaletteItem() { Text = i.Display, SecondaryText = i.Input }); + } + } } diff --git a/src/WPF/CommandPaletteControl.xaml b/src/WPF/CommandPaletteControl.xaml index f240d9a..3326325 100644 --- a/src/WPF/CommandPaletteControl.xaml +++ b/src/WPF/CommandPaletteControl.xaml @@ -1,12 +1,69 @@ - + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/WPF/CommandPaletteControl.xaml.cs b/src/WPF/CommandPaletteControl.xaml.cs index 8765383..67cb36a 100644 --- a/src/WPF/CommandPaletteControl.xaml.cs +++ b/src/WPF/CommandPaletteControl.xaml.cs @@ -1,28 +1,140 @@ -using System; + +using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Collections.ObjectModel; +using System.ComponentModel; using System.Windows; using System.Windows.Controls; using System.Windows.Data; -using System.Windows.Documents; using System.Windows.Input; -using System.Windows.Media; -using System.Windows.Media.Imaging; -using System.Windows.Navigation; -using System.Windows.Shapes; -namespace mpvnet.WPF +namespace mpvnet { - /// - /// Interaction logic for CommandPaletteControl.xaml - /// public partial class CommandPaletteControl : UserControl { + public ICollectionView CollectionView { get; set; } + public ICommand EscapeCommand { get; } + public CollectionViewSource CollectionViewSource { get; } + public ObservableCollection Items { get; } = new ObservableCollection(); + public CommandPaletteControl() { InitializeComponent(); + DataContext = this; + CollectionViewSource = new CollectionViewSource() { Source = Items }; + CollectionView = CollectionViewSource.View; + CollectionView.Filter = new Predicate(item => Filter((CommandPaletteItem)item)); + MainListView.ItemsSource = CollectionView; + + EscapeCommand = new RelayCommand(OnEscapeCommand); + SearchControl.SearchTextBox.PreviewKeyDown += SearchControl_PreviewKeyDown; + SearchControl.SearchTextBox.TextChanged += SearchTextBox_TextChanged; + SearchControl.SearchTextBox.BorderBrush = Theme.Background; + SearchControl.HideClearButton = true; + } + + void SearchTextBox_TextChanged(object sender, TextChangedEventArgs e) + { + CollectionView.Refresh(); + SelectFirst(); + } + + void SearchControl_PreviewKeyDown(object sender, KeyEventArgs e) + { + switch (e.Key) + { + case Key.Up: + { + int index = MainListView.SelectedIndex; + index -= 1; + + if (index < 0) + index = 0; + + MainListView.SelectedIndex = index; + MainListView.ScrollIntoView(MainListView.SelectedItem); + } + break; + case Key.Down: + { + int index = MainListView.SelectedIndex; + + if (++index > MainListView.Items.Count - 1) + index = MainListView.Items.Count - 1; + + MainListView.SelectedIndex = index; + MainListView.ScrollIntoView(MainListView.SelectedItem); + } + break; + case Key.Enter: + Execute(); + break; + } + } + + void OnEscapeCommand(object param) + { + MainForm.Instance.HideCommandPalette(); + } + + public Theme Theme => Theme.Current; + + bool Filter(CommandPaletteItem item) + { + string filter = SearchControl.SearchTextBox.Text.ToLower(); + + if (filter == "" || item.Text.ToLower().Contains(filter) || + item.SecondaryText.ToLower().Contains(filter)) + + return true; + + return false; + } + + public void SelectFirst() + { + if (MainListView.Items.Count > 0) + MainListView.SelectedIndex = 0; + } + + void Execute() + { + if (MainListView.SelectedItem != null) + { + CommandPaletteItem item = MainListView.SelectedItem as CommandPaletteItem; + MainForm.Instance.HideCommandPalette(); + item.Action.Invoke(); + } + } + + void OnLoaded(object sender, RoutedEventArgs e) + { + Keyboard.Focus(SearchControl.SearchTextBox); + } + + public void SetItems(IEnumerable items) + { + Items.Clear(); + + foreach (var i in items) + Items.Add(i); + } + + void MainListView_SizeChanged(object sender, SizeChangedEventArgs e) + { + AdjustHeight(); + } + + public void AdjustHeight() + { + double actualHeight = SearchControl.ActualHeight + MainListView.ActualHeight; + int dpi = Native.GetDPI(MainForm.Instance.Handle); + MainForm.Instance.CommandPaletteHost.Height = (int)(actualHeight / 96.0 * dpi); + } + + void MainListView_MouseUp(object sender, MouseButtonEventArgs e) + { + Execute(); } } } diff --git a/src/WPF/CommandPaletteWindow.xaml b/src/WPF/CommandPaletteWindow.xaml deleted file mode 100644 index ebe5be3..0000000 --- a/src/WPF/CommandPaletteWindow.xaml +++ /dev/null @@ -1,66 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/WPF/CommandPaletteWindow.xaml.cs b/src/WPF/CommandPaletteWindow.xaml.cs deleted file mode 100644 index 8782bf2..0000000 --- a/src/WPF/CommandPaletteWindow.xaml.cs +++ /dev/null @@ -1,128 +0,0 @@ - -using System; -using System.ComponentModel; -using System.Windows; -using System.Windows.Data; -using System.Windows.Input; -using System.Windows.Interop; - -using static mpvnet.Global; - -namespace mpvnet -{ - public partial class CommandPaletteWindow : Window - { - ICollectionView CollectionView; - - public CommandPaletteWindow() - { - InitializeComponent(); - DataContext = this; - CollectionViewSource collectionViewSource = new CollectionViewSource() { Source = CommandItem.Items }; - CollectionView = collectionViewSource.View; - var yourCostumFilter = new Predicate(item => Filter((CommandItem)item)); - CollectionView.Filter = yourCostumFilter; - ListView.ItemsSource = CollectionView; - } - - public Theme Theme => Theme.Current; - - bool Filter(CommandItem item) - { - if (item.Command == "") - return false; - - string filter = FilterTextBox.Text.ToLower(); - - if (filter == "") - return true; - - if (item.Command.ToLower().Contains(filter) || - item.Input.ToLower().Contains(filter) || - item.Path.ToLower().Contains(filter)) - - return true; - - return false; - } - - void Window_Loaded(object sender, RoutedEventArgs e) - { - HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle); - source.AddHook(new HwndSourceHook(WndProc)); - Keyboard.Focus(FilterTextBox); - SelectFirst(); - } - - void SelectFirst() - { - if (ListView.Items.Count > 0) - ListView.SelectedIndex = 0; - } - - IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) - { - if (msg == 0x200 /*WM_MOUSEMOVE*/ && Mouse.LeftButton != MouseButtonState.Pressed) - handled = true; - - return IntPtr.Zero; - } - - void FilterTextBox_PreviewKeyDown(object sender, KeyEventArgs e) - { - switch (e.Key) - { - case Key.Up: - { - int index = ListView.SelectedIndex; - index -= 1; - - if (index < 0) - index = 0; - - ListView.SelectedIndex = index; - ListView.ScrollIntoView(ListView.SelectedItem); - } - break; - case Key.Down: - { - int index = ListView.SelectedIndex; - - if (++index > ListView.Items.Count - 1) - index = ListView.Items.Count - 1; - - ListView.SelectedIndex = index; - ListView.ScrollIntoView(ListView.SelectedItem); - } - break; - case Key.Escape: - Close(); - break; - case Key.Enter: - Execute(); - break; - } - } - - void Execute() - { - if (ListView.SelectedItem != null) - { - CommandItem item = ListView.SelectedItem as CommandItem; - Close(); - Core.command(item.Command); - } - } - - void ListView_MouseUp(object sender, MouseButtonEventArgs e) - { - Execute(); - } - - void FilterTextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e) - { - CollectionView.Refresh(); - SelectFirst(); - } - } -} \ No newline at end of file diff --git a/src/WPF/ConfWindow.xaml b/src/WPF/ConfWindow.xaml index cd34d17..bf5ccfd 100644 --- a/src/WPF/ConfWindow.xaml +++ b/src/WPF/ConfWindow.xaml @@ -1,9 +1,11 @@  - - - - ExecuteAction; + + Predicate CanExecutePredicate; + + public RelayCommand(Action executeAction, Predicate canExecutePredicate = null) + { + ExecuteAction = executeAction; + CanExecutePredicate = canExecutePredicate; + } + + public bool CanExecute(object parameter) => CanExecutePredicate == null || CanExecutePredicate(parameter); + + public void Execute(object parameter) => ExecuteAction(parameter); + + public void OnCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty); + } +} diff --git a/src/WPF/Resources.xaml b/src/WPF/Resources.xaml index e61a3dd..ff430d9 100644 --- a/src/WPF/Resources.xaml +++ b/src/WPF/Resources.xaml @@ -36,6 +36,7 @@ +