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

@@ -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"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:mpvnet.WPF"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl
x:Class="mpvnet.CommandPaletteControl"
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"
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"
@@ -26,8 +28,8 @@
<ColumnDefinition Width="100" />
<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"
@@ -49,8 +50,8 @@
<RowDefinition Height="Auto" />
<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">
@@ -46,7 +47,7 @@
<ColumnDefinition x:Name="LeftCol" Width="18" />
<ColumnDefinition x:Name="RightCol" Width="*" />
</Grid.ColumnDefinitions>
<Grid x:Name="PART_CHECKBOX">
<Ellipse
x:Name="normal"
@@ -72,7 +73,7 @@
StrokeThickness="{TemplateBinding BorderThickness}"
/>
</Grid>
<ContentPresenter
x:Name="contentPresenter"
Grid.Column="1"
@@ -85,7 +86,7 @@
ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}"
RecognizesAccessKey="True"
/>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />

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;