Browse Source

Simplified CommandSearchViewModel.cs code and added invalid color notice

CPKreuz 3 years ago
parent
commit
7ff3869d53

+ 52 - 19
PixiEditor/ViewModels/CommandSearchViewModel.cs

@@ -5,6 +5,7 @@ using SkiaSharp;
 using System.Collections.ObjectModel;
 using System.Collections.ObjectModel;
 using System.IO;
 using System.IO;
 using System.Text.RegularExpressions;
 using System.Text.RegularExpressions;
+using PixiEditor.Models.DataHolders;
 
 
 namespace PixiEditor.ViewModels
 namespace PixiEditor.ViewModels
 {
 {
@@ -12,6 +13,7 @@ namespace PixiEditor.ViewModels
     {
     {
         private string searchTerm;
         private string searchTerm;
         private SearchResult selectedCommand;
         private SearchResult selectedCommand;
+        private bool invalidColor;
 
 
         public string SearchTerm
         public string SearchTerm
         {
         {
@@ -45,6 +47,12 @@ namespace PixiEditor.ViewModels
             }
             }
         }
         }
 
 
+        public bool InvalidColor
+        {
+            get => invalidColor;
+            set => SetProperty(ref invalidColor, value);
+        }
+        
         public ObservableCollection<SearchResult> Results { get; } = new();
         public ObservableCollection<SearchResult> Results { get; } = new();
 
 
         public CommandSearchViewModel()
         public CommandSearchViewModel()
@@ -54,21 +62,19 @@ namespace PixiEditor.ViewModels
 
 
         private void UpdateSearchResults()
         private void UpdateSearchResults()
         {
         {
-            CommandController controller = CommandController.Current;
             Results.Clear();
             Results.Clear();
 
 
+            var recentlyOpened = HandleRecentlyOpened();
+            
             if (string.IsNullOrWhiteSpace(SearchTerm))
             if (string.IsNullOrWhiteSpace(SearchTerm))
             {
             {
-                foreach (var file in ViewModelMain.Current.FileSubViewModel.RecentlyOpened)
+                foreach (var result in recentlyOpened)
                 {
                 {
-                    Results.Add(
-                        new FileSearchResult(file.FilePath)
-                        {
-                            SearchTerm = searchTerm
-                        });
+                    Results.Add(result);
                 }
                 }
 
 
                 SelectedResult = Results.FirstOrDefault(x => x.CanExecute);
                 SelectedResult = Results.FirstOrDefault(x => x.CanExecute);
+                InvalidColor = false;
                 return;
                 return;
             }
             }
 
 
@@ -80,10 +86,24 @@ namespace PixiEditor.ViewModels
             }
             }
 
 
             HandleFile(filePath);
             HandleFile(filePath);
+            HandleColor();
+            HandleCommands();
+
+            foreach (var result in recentlyOpened)
+            {
+                Results.Add(result);
+            }
+
+            SelectedResult = Results.FirstOrDefault(x => x.CanExecute);
+        }
 
 
+        private void HandleColor()
+        {
             if (SearchTerm.StartsWith('#'))
             if (SearchTerm.StartsWith('#'))
             {
             {
-                if (SKColor.TryParse(SearchTerm, out SKColor color))
+                InvalidColor = !SKColor.TryParse(SearchTerm, out var color);
+                
+                if (!InvalidColor)
                 {
                 {
                     Results.Add(new ColorSearchResult(color)
                     Results.Add(new ColorSearchResult(color)
                     {
                     {
@@ -91,8 +111,15 @@ namespace PixiEditor.ViewModels
                     });
                     });
                 }
                 }
             }
             }
+            else
+            {
+                InvalidColor = false;
+            }
+        }
 
 
-            foreach (var command in controller.Commands
+        private void HandleCommands()
+        {
+            foreach (var command in CommandController.Current.Commands
                          .Where(x => x.Description.Contains(SearchTerm, StringComparison.OrdinalIgnoreCase))
                          .Where(x => x.Description.Contains(SearchTerm, StringComparison.OrdinalIgnoreCase))
                          .OrderByDescending(x =>
                          .OrderByDescending(x =>
                              x.Description.Contains($" {SearchTerm} ", StringComparison.OrdinalIgnoreCase))
                              x.Description.Contains($" {SearchTerm} ", StringComparison.OrdinalIgnoreCase))
@@ -105,19 +132,25 @@ namespace PixiEditor.ViewModels
                         Match = Match(command.Description)
                         Match = Match(command.Description)
                     });
                     });
             }
             }
+        }
+        
+        private IEnumerable<FileSearchResult> HandleRecentlyOpened()
+        {
+            IEnumerable<RecentlyOpenedDocument> enumerable = ViewModelMain.Current.FileSubViewModel.RecentlyOpened;
 
 
-            foreach (var file in ViewModelMain.Current.FileSubViewModel.RecentlyOpened
-                         .Where(x => x.FilePath.Contains(searchTerm, StringComparison.OrdinalIgnoreCase)))
+            if (!string.IsNullOrWhiteSpace(SearchTerm))
             {
             {
-                Results.Add(
-                    new FileSearchResult(file.FilePath)
-                    {
-                        SearchTerm = searchTerm,
-                        Match = Match(file.FilePath)
-                    });
+                enumerable = enumerable.Where(x => x.FilePath.Contains(searchTerm, StringComparison.OrdinalIgnoreCase));
             }
             }
 
 
-            SelectedResult = Results.FirstOrDefault(x => x.CanExecute);
+            foreach (var file in enumerable)
+            {
+                yield return new FileSearchResult(file.FilePath)
+                {
+                    SearchTerm = searchTerm,
+                    Match = Match(file.FilePath)
+                };
+            }
         }
         }
 
 
         private void HandleFile(string filePath)
         private void HandleFile(string filePath)
@@ -150,7 +183,7 @@ namespace PixiEditor.ViewModels
         private Match Match(string text) => Match(text, SearchTerm);
         private Match Match(string text) => Match(text, SearchTerm);
 
 
         private Match Match(string text, string searchTerm) =>
         private Match Match(string text, string searchTerm) =>
-            Regex.Match(text, $"(.*)({Regex.Escape(searchTerm)})(.*)", RegexOptions.IgnoreCase);
+            Regex.Match(text, $"(.*)({Regex.Escape(searchTerm ?? string.Empty)})(.*)", RegexOptions.IgnoreCase);
 
 
         private bool GetDirectory(string path, out string directory, out string file)
         private bool GetDirectory(string path, out string directory, out string file)
         {
         {

+ 93 - 79
PixiEditor/Views/UserControls/CommandSearchControl.xaml

@@ -1,8 +1,8 @@
 <UserControl x:Class="PixiEditor.Views.UserControls.CommandSearchControl"
 <UserControl x:Class="PixiEditor.Views.UserControls.CommandSearchControl"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
              xmlns:local="clr-namespace:PixiEditor.Views.UserControls"
              xmlns:local="clr-namespace:PixiEditor.Views.UserControls"
              xmlns:vm="clr-namespace:PixiEditor.ViewModels"
              xmlns:vm="clr-namespace:PixiEditor.ViewModels"
              xmlns:behaves="clr-namespace:PixiEditor.Helpers.Behaviours"
              xmlns:behaves="clr-namespace:PixiEditor.Helpers.Behaviours"
@@ -16,26 +16,27 @@
     <Grid DataContext="{DynamicResource viewModel}" x:Name="mainGrid">
     <Grid DataContext="{DynamicResource viewModel}" x:Name="mainGrid">
         <Grid.Resources>
         <Grid.Resources>
             <ResourceDictionary>
             <ResourceDictionary>
-                <vm:CommandSearchViewModel x:Key="viewModel"/>
+                <vm:CommandSearchViewModel x:Key="viewModel" />
             </ResourceDictionary>
             </ResourceDictionary>
         </Grid.Resources>
         </Grid.Resources>
         <Grid.RowDefinitions>
         <Grid.RowDefinitions>
-            <RowDefinition Height="Auto"/>
-            <RowDefinition Height="*"/>
-            <RowDefinition Height="Auto"/>
+            <RowDefinition Height="Auto" />
+            <RowDefinition Height="*" />
+            <RowDefinition Height="Auto" />
         </Grid.RowDefinitions>
         </Grid.RowDefinitions>
 
 
-        <TextBox Text="{Binding SearchTerm, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" FontSize="18" Padding="5"
+        <TextBox Text="{Binding SearchTerm, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" FontSize="18"
+                 Padding="5"
                  x:Name="textBox">
                  x:Name="textBox">
             <i:Interaction.Behaviors>
             <i:Interaction.Behaviors>
                 <behaves:TextBoxFocusBehavior SelectOnMouseClick="True" />
                 <behaves:TextBoxFocusBehavior SelectOnMouseClick="True" />
-                <behaves:GlobalShortcutFocusBehavior/>
+                <behaves:GlobalShortcutFocusBehavior />
             </i:Interaction.Behaviors>
             </i:Interaction.Behaviors>
             <TextBox.Style>
             <TextBox.Style>
                 <Style TargetType="TextBox" BasedOn="{StaticResource DarkTextBoxStyle}">
                 <Style TargetType="TextBox" BasedOn="{StaticResource DarkTextBoxStyle}">
                     <Style.Resources>
                     <Style.Resources>
                         <Style TargetType="Border">
                         <Style TargetType="Border">
-                            <Setter Property="CornerRadius" Value="5,5,0,0"/>
+                            <Setter Property="CornerRadius" Value="5,5,0,0" />
                         </Style>
                         </Style>
                     </Style.Resources>
                     </Style.Resources>
                 </Style>
                 </Style>
@@ -43,78 +44,91 @@
         </TextBox>
         </TextBox>
         <Border Grid.Row="1" BorderThickness="1,0,1,0" BorderBrush="{StaticResource BrighterAccentColor}"
         <Border Grid.Row="1" BorderThickness="1,0,1,0" BorderBrush="{StaticResource BrighterAccentColor}"
                 Background="{StaticResource AccentColor}">
                 Background="{StaticResource AccentColor}">
-            <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
-                <ItemsControl ItemsSource="{Binding Results}" MinWidth="400">
-                    <ItemsControl.ItemTemplate>
-                        <DataTemplate DataType="cmdssearch:SearchTerm">
-                            <Button Padding="5" Height="40" BorderThickness="0" Background="Transparent"
-                                Command="{Binding ExecuteCommand}"
-                                CommandParameter="{Binding}"
-                                MouseMove="Button_MouseMove">
-                                <Button.Style>
-                                    <Style TargetType="Button">
-                                        <Setter Property="Template">
-                                            <Setter.Value>
-                                                <ControlTemplate TargetType="Button">
-                                                    <Border>
-                                                        <Border.Style>
-                                                            <Style TargetType="Border">
-                                                                <Style.Triggers>
-                                                                    <DataTrigger Binding="{Binding IsSelected, Mode=TwoWay}" Value="False">
-                                                                        <Setter Property="Background" Value="Transparent"/>
-                                                                    </DataTrigger>
-                                                                    <DataTrigger Binding="{Binding IsSelected, Mode=TwoWay}" Value="True">
-                                                                        <Setter Property="Background" Value="{StaticResource BrighterAccentColor}"/>
-                                                                    </DataTrigger>
-                                                                    <DataTrigger Binding="{Binding CanExecute}" Value="False">
-                                                                        <Setter Property="Background" Value="Transparent"/>
-                                                                    </DataTrigger>
-                                                                </Style.Triggers>
-                                                            </Style>
-                                                        </Border.Style>
-                                                        <ContentPresenter/>
-                                                    </Border>
-                                                </ControlTemplate>
-                                            </Setter.Value>
-                                        </Setter>
-                                    </Style>
-                                </Button.Style>
-                                <Button.Resources>
-                                    <Style TargetType="TextBlock">
-                                        <Setter Property="FontSize" Value="16"/>
-                                        <Style.Triggers>
-                                            <DataTrigger Binding="{Binding CanExecute}" Value="True">
-                                                <Setter Property="Foreground" Value="White"/>
-                                            </DataTrigger>
-                                            <DataTrigger Binding="{Binding CanExecute}" Value="False">
-                                                <Setter Property="Foreground" Value="Gray"/>
-                                            </DataTrigger>
-                                        </Style.Triggers>
-                                    </Style>
-                                </Button.Resources>
-                                <Grid VerticalAlignment="Center" x:Name="dp" Margin="5,0,10,0">
-                                    <Grid.ColumnDefinitions>
-                                        <ColumnDefinition/>
-                                        <ColumnDefinition Width="Auto"/>
-                                    </Grid.ColumnDefinitions>
-                                    <StackPanel Orientation="Horizontal">
-                                        <Border Width="25" Margin="0,0,5,0" Padding="1">
-                                            <Image HorizontalAlignment="Center" Source="{Binding Icon}"/>
-                                        </Border>
-                                        <TextBlock VerticalAlignment="Center"
-                                               behaves:TextBlockExtensions.BindableInlines="{Binding TextBlockContent}"/>
-                                    </StackPanel>
-                                    <TextBlock Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Right" Text="{Binding Shortcut}"/>
-                                </Grid>
-                            </Button>
-                        </DataTemplate>
-                    </ItemsControl.ItemTemplate>
-                </ItemsControl>
-            </ScrollViewer>
+            <Grid>
+                <TextBlock Text="Invalid color" TextAlignment="Center" Foreground="Gray" Margin="0,5,0,0"
+                           Visibility="{Binding InvalidColor, Converter={BoolToVisibilityConverter}}"/>
+                <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
+                    <ItemsControl ItemsSource="{Binding Results}" MinWidth="400">
+                        <ItemsControl.ItemTemplate>
+                            <DataTemplate DataType="cmdssearch:SearchResult">
+                                <Button Padding="5" Height="40" BorderThickness="0" Background="Transparent"
+                                        Command="{Binding ExecuteCommand}"
+                                        CommandParameter="{Binding}"
+                                        MouseMove="Button_MouseMove">
+                                    <Button.Style>
+                                        <Style TargetType="Button">
+                                            <Setter Property="Template">
+                                                <Setter.Value>
+                                                    <ControlTemplate TargetType="Button">
+                                                        <Border>
+                                                            <Border.Style>
+                                                                <Style TargetType="Border">
+                                                                    <Style.Triggers>
+                                                                        <DataTrigger
+                                                                            Binding="{Binding IsSelected, Mode=TwoWay}"
+                                                                            Value="False">
+                                                                            <Setter Property="Background"
+                                                                                Value="Transparent" />
+                                                                        </DataTrigger>
+                                                                        <DataTrigger
+                                                                            Binding="{Binding IsSelected, Mode=TwoWay}"
+                                                                            Value="True">
+                                                                            <Setter Property="Background"
+                                                                                Value="{StaticResource BrighterAccentColor}" />
+                                                                        </DataTrigger>
+                                                                        <DataTrigger Binding="{Binding CanExecute}"
+                                                                            Value="False">
+                                                                            <Setter Property="Background"
+                                                                                Value="Transparent" />
+                                                                        </DataTrigger>
+                                                                    </Style.Triggers>
+                                                                </Style>
+                                                            </Border.Style>
+                                                            <ContentPresenter />
+                                                        </Border>
+                                                    </ControlTemplate>
+                                                </Setter.Value>
+                                            </Setter>
+                                        </Style>
+                                    </Button.Style>
+                                    <Button.Resources>
+                                        <Style TargetType="TextBlock">
+                                            <Setter Property="FontSize" Value="16" />
+                                            <Style.Triggers>
+                                                <DataTrigger Binding="{Binding CanExecute}" Value="True">
+                                                    <Setter Property="Foreground" Value="White" />
+                                                </DataTrigger>
+                                                <DataTrigger Binding="{Binding CanExecute}" Value="False">
+                                                    <Setter Property="Foreground" Value="Gray" />
+                                                </DataTrigger>
+                                            </Style.Triggers>
+                                        </Style>
+                                    </Button.Resources>
+                                    <Grid VerticalAlignment="Center" x:Name="dp" Margin="5,0,10,0">
+                                        <Grid.ColumnDefinitions>
+                                            <ColumnDefinition />
+                                            <ColumnDefinition Width="Auto" />
+                                        </Grid.ColumnDefinitions>
+                                        <StackPanel Orientation="Horizontal">
+                                            <Border Width="25" Margin="0,0,5,0" Padding="1">
+                                                <Image HorizontalAlignment="Center" Source="{Binding Icon}" />
+                                            </Border>
+                                            <TextBlock VerticalAlignment="Center"
+                                                       behaves:TextBlockExtensions.BindableInlines="{Binding TextBlockContent}" />
+                                        </StackPanel>
+                                        <TextBlock Grid.Column="1" VerticalAlignment="Center"
+                                                   HorizontalAlignment="Right" Text="{Binding Shortcut}" />
+                                    </Grid>
+                                </Button>
+                            </DataTemplate>
+                        </ItemsControl.ItemTemplate>
+                    </ItemsControl>
+                </ScrollViewer>
+            </Grid>
         </Border>
         </Border>
         <Border Grid.Row="2" BorderThickness="1" BorderBrush="{StaticResource BrighterAccentColor}"
         <Border Grid.Row="2" BorderThickness="1" BorderBrush="{StaticResource BrighterAccentColor}"
                 CornerRadius="0,0,5,5" Background="{StaticResource AccentColor}">
                 CornerRadius="0,0,5,5" Background="{StaticResource AccentColor}">
-            <TextBlock Margin="5" FontSize="16" Text="{Binding SelectedResult.Description}"/>
+            <TextBlock Margin="5" FontSize="16" Text="{Binding SelectedResult.Description}" />
         </Border>
         </Border>
     </Grid>
     </Grid>
-</UserControl>
+</UserControl>