This commit is contained in:
Frank Skare
2021-06-22 19:10:27 +02:00
parent 6634ef094c
commit bac8b2b96c
15 changed files with 363 additions and 271 deletions

View File

@@ -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<CommandPaletteItem> items = new List<CommandPaletteItem>();
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();
}
}
}

View File

@@ -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)

View File

@@ -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<CommandPaletteItem> GetItems()
{
return CommandItem.Items.Select(i => new CommandPaletteItem() { Text = i.Display, SecondaryText = i.Input });
}
}
}

View File

@@ -1,12 +1,69 @@
<UserControl x:Class="mpvnet.WPF.CommandPaletteControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
<UserControl
x:Class="mpvnet.CommandPaletteControl"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:mpvnet.WPF"
xmlns:local="clr-namespace:mpvnet"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
FontSize="13"
Loaded="OnLoaded"
Background="{Binding Theme.Background}"
>
<UserControl.InputBindings>
<KeyBinding Gesture="Esc" Command="{Binding EscapeCommand}"/>
</UserControl.InputBindings>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<local:SearchTextBoxUserControl
HintText="Search"
x:Name="SearchControl"
Grid.ColumnSpan="2"
Padding="1,1,1,0"
/>
<ListView
Name="MainListView"
Grid.Row="1"
Foreground="{Binding Theme.Foreground}"
Background="{Binding Theme.Background}"
BorderThickness="0"
MaxHeight="202"
SizeChanged="MainListView_SizeChanged" MouseUp="MainListView_MouseUp"
>
<ListView.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
<Setter Property="Height" Value="25"></Setter>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Text}"></TextBlock>
<TextBlock
Grid.Column="1"
Text="{Binding SecondaryText}"
HorizontalAlignment="Right"
/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</UserControl>

View File

@@ -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
{
/// <summary>
/// Interaction logic for CommandPaletteControl.xaml
/// </summary>
public partial class CommandPaletteControl : UserControl
{
public ICollectionView CollectionView { get; set; }
public ICommand EscapeCommand { get; }
public CollectionViewSource CollectionViewSource { get; }
public ObservableCollection<CommandPaletteItem> Items { get; } = new ObservableCollection<CommandPaletteItem>();
public CommandPaletteControl()
{
InitializeComponent();
DataContext = this;
CollectionViewSource = new CollectionViewSource() { Source = Items };
CollectionView = CollectionViewSource.View;
CollectionView.Filter = new Predicate<object>(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<CommandPaletteItem> 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();
}
}
}

View File

@@ -1,66 +0,0 @@
<Window
x:Class="mpvnet.CommandPaletteWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="Command Palette"
Height="295"
Width="400"
FontSize="13"
ResizeMode="NoResize"
ShowInTaskbar="False"
WindowStartupLocation="CenterOwner"
Loaded="Window_Loaded">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<TextBox
Name="FilterTextBox"
Foreground="{Binding Theme.Foreground}"
Background="{Binding Theme.Background}"
PreviewKeyDown="FilterTextBox_PreviewKeyDown"
TextChanged="FilterTextBox_TextChanged"
/>
<ListView
Name="ListView"
Grid.Row="1"
Foreground="{Binding Theme.Foreground}"
Background="{Binding Theme.Background}"
MouseUp="ListView_MouseUp"
>
<ListView.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Display}"></TextBlock>
<TextBlock
Grid.Column="1"
Text="{Binding Input}"
HorizontalAlignment="Right"
/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Window>

View File

@@ -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<object>(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();
}
}
}

View File

@@ -1,9 +1,11 @@
<Window
xmlns:Controls="clr-namespace:Controls" x:Name="ConfWindow1" x:Class="mpvnet.ConfWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="ConfWindow1"
x:Class="mpvnet.ConfWindow"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:mpvnet"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
mc:Ignorable="d"
Title="Config Editor"
@@ -27,7 +29,7 @@
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Controls:SearchTextBoxUserControl
<local:SearchTextBoxUserControl
x:Name="SearchControl"
HintText="Find a setting"
Width="250"

View File

@@ -1,9 +1,10 @@
<Window
xmlns:Controls="clr-namespace:Controls" x:Class="mpvnet.InputWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="mpvnet.InputWindow"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:mpvnet"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
mc:Ignorable="d"
Title="Input Editor"
@@ -50,7 +51,7 @@
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Controls:SearchTextBoxUserControl
<local:SearchTextBoxUserControl
HintText="Type ? to get help."
x:Name="SearchControl"
Width="300"

27
src/WPF/RelayCommand.cs Normal file
View File

@@ -0,0 +1,27 @@

using System;
using System.Windows.Input;
namespace mpvnet
{
public class RelayCommand : ICommand
{
public event EventHandler CanExecuteChanged;
Action<object> ExecuteAction;
Predicate<object> CanExecutePredicate;
public RelayCommand(Action<object> executeAction, Predicate<object> 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);
}
}

View File

@@ -36,6 +36,7 @@
</Setter.Value>
</Setter>
</Style>
<Style TargetType="RadioButton">
<Setter Property="Padding" Value="6 0 0 0" />
<Setter Property="Template">

View File

@@ -1,9 +1,9 @@
<UserControl
x:Class="Controls.SearchTextBoxUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Class="mpvnet.SearchTextBoxUserControl"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
mc:Ignorable="d"
d:DesignHeight="450"
d:DesignWidth="800"
@@ -16,7 +16,7 @@
<TextBlock
Name="HintTextBlock"
Margin="5,2"
Padding="6,1"
Text="Find a setting"
VerticalAlignment="Center"
Foreground="{Binding Theme.Foreground2}"
@@ -26,8 +26,8 @@
<TextBox
Name="SearchTextBox"
Height="25"
Padding="1,2,0,0"
BorderThickness="2"
Padding="2"
Background="Transparent"
TextChanged="SearchTextBox_TextChanged"
Foreground="{Binding Theme.Foreground}"

View File

@@ -3,21 +3,19 @@ using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using mpvnet;
namespace Controls
namespace mpvnet
{
public partial class SearchTextBoxUserControl : UserControl
{
public bool HideClearButton { get; set; }
public SearchTextBoxUserControl()
{
InitializeComponent();
DataContext = this;
}
public Theme Theme {
get => Theme.Current;
}
public Theme Theme => Theme.Current;
public string Text {
get => SearchTextBox.Text;
@@ -49,7 +47,7 @@ namespace Controls
{
HintTextBlock.Text = SearchTextBox.Text == "" ? HintText : "";
if (SearchTextBox.Text == "")
if (SearchTextBox.Text == "" || HideClearButton)
SearchClearButton.Visibility = Visibility.Hidden;
else
SearchClearButton.Visibility = Visibility.Visible;

View File

@@ -8,6 +8,7 @@ using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
using static mpvnet.Native;
using static mpvnet.Global;
@@ -16,6 +17,7 @@ namespace mpvnet
{
public partial class MainForm : Form
{
public ElementHost CommandPaletteHost { get; set; }
public static MainForm Instance { get; set; }
public static IntPtr Hwnd { get; set; }
public new ContextMenuStripEx ContextMenu { get; set; }
@@ -168,7 +170,7 @@ namespace mpvnet
bool IsMouseInOSC()
{
Point pos = PointToClient(Control.MousePosition);
Point pos = PointToClient(MousePosition);
float top = 0;
if (FormBorderStyle == FormBorderStyle.None)
@@ -177,6 +179,8 @@ namespace mpvnet
return pos.Y > ClientSize.Height * 0.85 || pos.Y < top;
}
bool IsCommandPaletteVissible() => CommandPaletteHost != null && CommandPaletteHost.Visible;
void ContextMenu_Opening(object sender, CancelEventArgs e)
{
lock (Core.MediaTracks)
@@ -815,7 +819,7 @@ namespace mpvnet
}
else if (Environment.TickCount - LastCursorChanged > 1500 &&
!IsMouseInOSC() && ClientRectangle.Contains(PointToClient(MousePosition)) &&
ActiveForm == this && !ContextMenu.Visible)
ActiveForm == this && !ContextMenu.Visible && !IsCommandPaletteVissible())
CursorHelp.Hide();
}
@@ -1039,5 +1043,50 @@ namespace mpvnet
base.OnKeyDown(e);
}
public void ShowCommandPalette()
{
if (CommandPaletteHost == null)
{
CommandPaletteHost = new ElementHost();
AdjustCommandPaletteLeftAndWidth();
CommandPaletteHost.Child = CommandPalette.Instance;
CommandPalette.Instance.AdjustHeight();
CommandPalette.Instance.SelectFirst();
Controls.Add(CommandPaletteHost);
}
}
public void HideCommandPalette()
{
if (CommandPaletteHost != null)
{
ActiveControl = null;
CommandPalette.Instance.SearchControl.SearchTextBox.Text = "";
Controls.Remove(CommandPaletteHost);
CommandPaletteHost.Child = null;
CommandPaletteHost.Dispose();
CommandPaletteHost = null;
}
}
void AdjustCommandPaletteLeftAndWidth()
{
if (CommandPaletteHost == null)
return;
CommandPaletteHost.Width = FontHeight * 25;
if (CommandPaletteHost.Width > ClientSize.Width)
CommandPaletteHost.Width = ClientSize.Width;
CommandPaletteHost.Left = (ClientSize.Width - CommandPaletteHost.Size.Width) / 2;
}
protected override void OnLayout(LayoutEventArgs args)
{
base.OnLayout(args);
AdjustCommandPaletteLeftAndWidth();
}
}
}

View File

@@ -73,9 +73,11 @@
<Reference Include="System.Xaml" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
<Reference Include="WindowsFormsIntegration" />
</ItemGroup>
<ItemGroup>
<Compile Include="Misc\App.cs" />
<Compile Include="WPF\RelayCommand.cs" />
<Compile Include="Misc\CSharpScriptHost.cs" />
<Compile Include="Misc\Extension.cs" />
<None Include="..\docs\Changelog.md">
@@ -106,10 +108,6 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="WPF\CommandPaletteWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="WPF\Resources.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
@@ -168,9 +166,6 @@
<Compile Include="WPF\EverythingWindow.xaml.cs">
<DependentUpon>EverythingWindow.xaml</DependentUpon>
</Compile>
<Compile Include="WPF\CommandPaletteWindow.xaml.cs">
<DependentUpon>CommandPaletteWindow.xaml</DependentUpon>
</Compile>
<Compile Include="WPF\ConfWindow.xaml.cs">
<DependentUpon>ConfWindow.xaml</DependentUpon>
</Compile>