flabbet 3 роки тому
батько
коміт
3a37aadb43

+ 5 - 3
PixiEditor/Models/DataProviders/LocalPalettesFetcher.cs

@@ -162,7 +162,7 @@ namespace PixiEditor.Models.DataProviders
                 if (foundParser != null)
                 {
                     var newPalette = await foundParser.Parse(file);
-                    if (newPalette != null)
+                    if (newPalette is { IsCorrupted: false })
                     {
                         Palette pal = CreatePalette(newPalette, file,
                             _cachedFavoritePalettes?.Contains(newPalette.Title) ?? false);
@@ -197,7 +197,8 @@ namespace PixiEditor.Models.DataProviders
 
         public async Task RefreshCacheAll()
         {
-            string[] files = DirectoryExtensions.GetFiles(PathToPalettesFolder,
+            string[] files = DirectoryExtensions.GetFiles(
+                PathToPalettesFolder,
                 string.Join("|", AvailableParsers.SelectMany(x => x.SupportedFileExtensions)),
                 SearchOption.TopDirectoryOnly);
             CachedPalettes = await ParseAll(files);
@@ -215,6 +216,7 @@ namespace PixiEditor.Models.DataProviders
                 var foundParser = AvailableParsers.First(x => x.SupportedFileExtensions.Contains(extension));
                 {
                     PaletteFileData fileData = await foundParser.Parse(file);
+                    if(fileData.IsCorrupted) continue;
                     var palette = CreatePalette(fileData, file, _cachedFavoritePalettes?.Contains(fileData.Title) ?? false);
 
                     result.Add(palette);
@@ -288,7 +290,7 @@ namespace PixiEditor.Models.DataProviders
                     
                     waitableExceptionOccured = false;
                 }
-                catch(IOException ex)
+                catch(IOException)
                 {
                     waitableExceptionOccured = true;
                     await Task.Delay(100);

+ 14 - 6
PixiEditor/Models/IO/ClsFile/ClsFileParser.cs

@@ -4,6 +4,7 @@ using System.IO;
 using System.Linq;
 using System.Threading.Tasks;
 using CLSEncoderDecoder;
+using PixiEditor.Models.DataHolders.Palettes;
 using SkiaSharp;
 
 namespace PixiEditor.Models.IO.ClsFile;
@@ -23,10 +24,11 @@ public class ClsFileParser : PaletteFileParser
             {
                 set = ClsColorSet.Load(path);
             }
-            catch (Exception e)
+            catch
             {
-                throw new FormatException("The provided file is invalid", e);
+                return new PaletteFileData("Corrupted", Array.Empty<SKColor>()) { IsCorrupted = true };
             }
+            
             PaletteFileData data = new(
                 set.Utf8Name,
                 set.Colors
@@ -38,19 +40,25 @@ public class ClsFileParser : PaletteFileParser
         });
     }
 
-    public override async Task Save(string path, PaletteFileData data)
-    {   
+    public override async Task<bool> Save(string path, PaletteFileData data)
+    {
+        if (data?.Colors == null || data.Colors.Length <= 0) return false;
+        
         string name = data.Title;
-        List<ClsColor> colors = data.Colors.Select(color => new ClsColor(color.Red, color.Green, color.Blue, color.Alpha)).ToList();
+        List<ClsColor> colors = data.Colors
+            .Select(color => new ClsColor(color.Red, color.Green, color.Blue, color.Alpha)).ToList();
         await Task.Run(() =>
         {
             if (name.Length == 0)
                 name = Path.GetFileNameWithoutExtension(path);
             if (name.Length > 64)
                 name = name.Substring(0, 64);
-            ClsColorSet set = new(colors, name); 
+            ClsColorSet set = new(colors, name);
             set.Save(path);
         });
+            
+        return true;
+
     }
 
 }

+ 18 - 4
PixiEditor/Models/IO/JascPalFile/JascFileParser.cs

@@ -13,8 +13,8 @@ public class JascFileParser : PaletteFileParser
     private static readonly string[] _supportedFileExtensions = new string[] { ".pal" };
     public override string[] SupportedFileExtensions => _supportedFileExtensions;
     public override string FileName => "Jasc Palette";
-
-    public static async Task<PaletteFileData> ParseFile(string path)
+    
+    private static async Task<PaletteFileData> ParseFile(string path)
     { 
         using var stream = File.OpenText(path);
         
@@ -39,8 +39,10 @@ public class JascFileParser : PaletteFileParser
         throw new JascFileException("Invalid JASC-PAL file.");
     }
 
-    public static async Task SaveFile(string path, PaletteFileData data)
+    public static async Task<bool> SaveFile(string path, PaletteFileData data)
     {
+        if (data is not { Colors.Length: > 0 }) return false;
+        
         string fileContent = "JASC-PAL\n0100\n" + data.Colors.Length;
         for (int i = 0; i < data.Colors.Length; i++)
         {
@@ -48,9 +50,21 @@ public class JascFileParser : PaletteFileParser
         }
 
         await File.WriteAllTextAsync(path, fileContent);
+        return true;
+
     }
 
-    public override async Task<PaletteFileData> Parse(string path) => await ParseFile(path);
+    public override async Task<PaletteFileData> Parse(string path)
+    {
+        try
+        {
+            return await ParseFile(path);
+        }
+        catch
+        {
+            return new PaletteFileData("Corrupted", Array.Empty<SKColor>()) { IsCorrupted = true };
+        }
+    }
 
     public override async Task Save(string path, PaletteFileData data) => await SaveFile(path, data);
 

+ 1 - 0
PixiEditor/Models/IO/PaletteFileData.cs

@@ -8,6 +8,7 @@ namespace PixiEditor.Models.IO
     {
         public string Title { get; set; }
         public SKColor[] Colors { get; set; }
+        public bool IsCorrupted { get; set; } = false;
 
         public PaletteFileData(SKColor[] colors)
         {

+ 8 - 7
PixiEditor/ViewModels/SubViewModels/Main/ColorsViewModel.cs

@@ -7,13 +7,9 @@ using PixiEditor.Models.Enums;
 using PixiEditor.Models.IO;
 using SkiaSharp;
 using System;
-using System.Collections;
 using System.Collections.Generic;
-using System.Collections.ObjectModel;
-using System.IO;
 using System.Linq;
 using System.Threading.Tasks;
-using System.Windows;
 using PixiEditor.Models.Controllers;
 using PixiEditor.Models.DataHolders.Palettes;
 using PixiEditor.Models.ExternalServices;
@@ -40,7 +36,7 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
         public WpfObservableRangeCollection<PaletteListDataSource> PaletteDataSources { get; private set; }
 
         public LocalPalettesFetcher LocalPaletteFetcher => _localPaletteFetcher ??=
-            (LocalPalettesFetcher)PaletteDataSources.FirstOrDefault(x => x is LocalPalettesFetcher);
+            (LocalPalettesFetcher)PaletteDataSources.FirstOrDefault(x => x is LocalPalettesFetcher)!;
 
         private SKColor primaryColor = SKColors.Black;
         private LocalPalettesFetcher _localPaletteFetcher;
@@ -81,11 +77,16 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             RemoveSwatchCommand = new RelayCommand(RemoveSwatch);
             SwapColorsCommand = new RelayCommand(SwapColors);
             SelectPaletteColorCommand = new RelayCommand<int>(SelectPaletteColor);
-            ImportPaletteCommand = new RelayCommand<List<string>>(ImportPalette, Owner.DocumentIsNotNull);
+            ImportPaletteCommand = new RelayCommand<List<string>>(ImportPalette, CanImportPalette);
             ReplaceColorsCommand = new RelayCommand<(SKColor oldColor, SKColor newColor)>(ReplaceColors, Owner.DocumentIsNotNull);
             Owner.OnStartupEvent += OwnerOnStartupEvent;
         }
 
+        private bool CanImportPalette(List<string> paletteColors)
+        {
+            return Owner.DocumentIsNotNull(paletteColors) && paletteColors.Count > 0;
+        }
+
         private void ReplaceColors((SKColor oldColor, SKColor newColor) colors)
         {
             Document activeDocument = Owner.BitmapManager?.ActiveDocument;
@@ -120,7 +121,7 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             }
         }
 
-        private async void OwnerOnStartupEvent(object? sender, EventArgs e)
+        private async void OwnerOnStartupEvent(object sender, EventArgs e)
         {
             await ImportLospecPalette();
         }

+ 2 - 8
PixiEditor/ViewModels/SubViewModels/Main/RegistryViewModel.cs

@@ -1,12 +1,6 @@
 using System;
-using System.Collections.Generic;
 using System.Diagnostics;
-using System.Linq;
 using System.Security.AccessControl;
-using System.Security.Claims;
-using System.Security.Principal;
-using System.Text;
-using System.Threading.Tasks;
 using System.Windows;
 using Microsoft.Win32;
 using PixiEditor.Models.Dialogs;
@@ -21,7 +15,7 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             Owner.OnStartupEvent += OwnerOnStartupEvent;
         }
 
-        private void OwnerOnStartupEvent(object? sender, EventArgs e)
+        private void OwnerOnStartupEvent(object sender, EventArgs e)
         {
             // Check if lospec-palette is associated in registry
 
@@ -63,7 +57,7 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
                 // Set the default value of the key
                 commandKey.SetValue("", $"\"{Process.GetCurrentProcess().MainModule?.FileName}\" \"%1\"");
             }
-            catch (Exception e)
+            catch
             {
                 NoticeDialog.Show("Failed to associate lospec-palette protocol", "Error");
             }

+ 14 - 4
PixiEditor/Views/Dialogs/PalettesBrowser.xaml.cs

@@ -157,15 +157,18 @@ namespace PixiEditor.Views.Dialogs
 
         private LocalPalettesFetcher _localPalettesFetcher;
 
-        private string[] _stopItTexts = new[] { "That's enough. Tidy up your file names.", 
-            "Can you stop copying these names please?", "No, really, stop it.", "Don't you have anything better to do?" };
+        private string[] _stopItTexts = new[]
+        {
+            "That's enough. Tidy up your file names.", 
+            "Can you stop copying these names please?", "No, really, stop it.", "Don't you have anything better to do?" 
+        };
 
         public PalettesBrowser()
         {
             InitializeComponent();
             Instance = this;
             DeletePaletteCommand = new RelayCommand<Palette>(DeletePalette);
-            ToggleFavouriteCommand = new RelayCommand<Palette>(ToggleFavourite);
+            ToggleFavouriteCommand = new RelayCommand<Palette>(ToggleFavourite, CanToggleFavourite);
             Loaded += async (sender, args) =>
             {
                 localPalettesFetcher.CacheUpdated += LocalCacheRefreshed;
@@ -413,6 +416,11 @@ namespace PixiEditor.Views.Dialogs
                 scrollViewer.ScrollToHome();
             }
         }
+        
+        private bool CanToggleFavourite(Palette palette)
+        {
+            return palette != null && palette.Colors.Count > 0;
+        }
 
         private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
         {
@@ -556,7 +564,8 @@ namespace PixiEditor.Views.Dialogs
 
         private static void UpdateRenamedFavourite(string old, string newName)
         {
-            var favourites = IPreferences.Current.GetLocalPreference(PreferencesConstants.FavouritePalettes,
+            var favourites = IPreferences.Current.GetLocalPreference(
+                PreferencesConstants.FavouritePalettes,
                 new List<string>());
 
             if (favourites.Contains(old))
@@ -598,6 +607,7 @@ namespace PixiEditor.Views.Dialogs
             {
                 var data = await parser.Parse(fileName);
 
+                if(data.IsCorrupted) return;
                 string name = LocalPalettesFetcher.GetNonExistingName(Path.GetFileName(fileName), true);
                 await localPalettesFetcher.SavePalette(name, data.Colors.ToArray());
             }

+ 0 - 299
PixiEditor/Views/UserControls/OutlinedTextBlock.cs

@@ -1,299 +0,0 @@
-using System;
-using System.ComponentModel;
-using System.Globalization;
-using System.Windows;
-using System.Windows.Documents;
-using System.Windows.Markup;
-using System.Windows.Media;
-
-namespace PixiEditor.Views.UserControls
-{
-
-    [ContentProperty("Text")]
-    public class OutlinedTextBlock : FrameworkElement
-    {
-        private void UpdatePen()
-        {
-            _Pen = new Pen(Stroke, StrokeThickness)
-            {
-                DashCap = PenLineCap.Round,
-                EndLineCap = PenLineCap.Round,
-                LineJoin = PenLineJoin.Round,
-                StartLineCap = PenLineCap.Round
-            };
-
-            InvalidateVisual();
-        }
-
-        public static readonly DependencyProperty FillProperty = DependencyProperty.Register(
-          "Fill",
-          typeof(Brush),
-          typeof(OutlinedTextBlock),
-          new FrameworkPropertyMetadata(Brushes.Black, FrameworkPropertyMetadataOptions.AffectsRender));
-
-        public static readonly DependencyProperty StrokeProperty = DependencyProperty.Register(
-          "Stroke",
-          typeof(Brush),
-          typeof(OutlinedTextBlock),
-          new FrameworkPropertyMetadata(Brushes.Black, FrameworkPropertyMetadataOptions.AffectsRender, StrokePropertyChangedCallback));
-
-        private static void StrokePropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
-        {
-            (dependencyObject as OutlinedTextBlock)?.UpdatePen();
-        }
-
-        public static readonly DependencyProperty StrokeThicknessProperty = DependencyProperty.Register(
-          "StrokeThickness",
-          typeof(double),
-          typeof(OutlinedTextBlock),
-          new FrameworkPropertyMetadata(1d, FrameworkPropertyMetadataOptions.AffectsRender, StrokePropertyChangedCallback));
-
-        public static readonly DependencyProperty FontFamilyProperty = TextElement.FontFamilyProperty.AddOwner(
-          typeof(OutlinedTextBlock),
-          new FrameworkPropertyMetadata(OnFormattedTextUpdated));
-
-        public static readonly DependencyProperty FontSizeProperty = TextElement.FontSizeProperty.AddOwner(
-          typeof(OutlinedTextBlock),
-          new FrameworkPropertyMetadata(OnFormattedTextUpdated));
-
-        public static readonly DependencyProperty FontStretchProperty = TextElement.FontStretchProperty.AddOwner(
-          typeof(OutlinedTextBlock),
-          new FrameworkPropertyMetadata(OnFormattedTextUpdated));
-
-        public static readonly DependencyProperty FontStyleProperty = TextElement.FontStyleProperty.AddOwner(
-          typeof(OutlinedTextBlock),
-          new FrameworkPropertyMetadata(OnFormattedTextUpdated));
-
-        public static readonly DependencyProperty FontWeightProperty = TextElement.FontWeightProperty.AddOwner(
-          typeof(OutlinedTextBlock),
-          new FrameworkPropertyMetadata(OnFormattedTextUpdated));
-
-        public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
-          "Text",
-          typeof(string),
-          typeof(OutlinedTextBlock),
-          new FrameworkPropertyMetadata(OnFormattedTextInvalidated));
-
-        public static readonly DependencyProperty TextAlignmentProperty = DependencyProperty.Register(
-          "TextAlignment",
-          typeof(TextAlignment),
-          typeof(OutlinedTextBlock),
-          new FrameworkPropertyMetadata(OnFormattedTextUpdated));
-
-        public static readonly DependencyProperty TextDecorationsProperty = DependencyProperty.Register(
-          "TextDecorations",
-          typeof(TextDecorationCollection),
-          typeof(OutlinedTextBlock),
-          new FrameworkPropertyMetadata(OnFormattedTextUpdated));
-
-        public static readonly DependencyProperty TextTrimmingProperty = DependencyProperty.Register(
-          "TextTrimming",
-          typeof(TextTrimming),
-          typeof(OutlinedTextBlock),
-          new FrameworkPropertyMetadata(OnFormattedTextUpdated));
-
-        public static readonly DependencyProperty TextWrappingProperty = DependencyProperty.Register(
-          "TextWrapping",
-          typeof(TextWrapping),
-          typeof(OutlinedTextBlock),
-          new FrameworkPropertyMetadata(TextWrapping.NoWrap, OnFormattedTextUpdated));
-
-        private FormattedText _FormattedText;
-        private Geometry _TextGeometry;
-        private Pen _Pen;
-
-        public Brush Fill
-        {
-            get { return (Brush)GetValue(FillProperty); }
-            set { SetValue(FillProperty, value); }
-        }
-
-        public FontFamily FontFamily
-        {
-            get { return (FontFamily)GetValue(FontFamilyProperty); }
-            set { SetValue(FontFamilyProperty, value); }
-        }
-
-        [TypeConverter(typeof(FontSizeConverter))]
-        public double FontSize
-        {
-            get { return (double)GetValue(FontSizeProperty); }
-            set { SetValue(FontSizeProperty, value); }
-        }
-
-        public FontStretch FontStretch
-        {
-            get { return (FontStretch)GetValue(FontStretchProperty); }
-            set { SetValue(FontStretchProperty, value); }
-        }
-
-        public FontStyle FontStyle
-        {
-            get { return (FontStyle)GetValue(FontStyleProperty); }
-            set { SetValue(FontStyleProperty, value); }
-        }
-
-        public FontWeight FontWeight
-        {
-            get { return (FontWeight)GetValue(FontWeightProperty); }
-            set { SetValue(FontWeightProperty, value); }
-        }
-
-        public Brush Stroke
-        {
-            get { return (Brush)GetValue(StrokeProperty); }
-            set { SetValue(StrokeProperty, value); }
-        }
-
-        public double StrokeThickness
-        {
-            get { return (double)GetValue(StrokeThicknessProperty); }
-            set { SetValue(StrokeThicknessProperty, value); }
-        }
-
-        public string Text
-        {
-            get { return (string)GetValue(TextProperty); }
-            set { SetValue(TextProperty, value); }
-        }
-
-        public TextAlignment TextAlignment
-        {
-            get { return (TextAlignment)GetValue(TextAlignmentProperty); }
-            set { SetValue(TextAlignmentProperty, value); }
-        }
-
-        public TextDecorationCollection TextDecorations
-        {
-            get { return (TextDecorationCollection)GetValue(TextDecorationsProperty); }
-            set { SetValue(TextDecorationsProperty, value); }
-        }
-
-        public TextTrimming TextTrimming
-        {
-            get { return (TextTrimming)GetValue(TextTrimmingProperty); }
-            set { SetValue(TextTrimmingProperty, value); }
-        }
-
-        public TextWrapping TextWrapping
-        {
-            get { return (TextWrapping)GetValue(TextWrappingProperty); }
-            set { SetValue(TextWrappingProperty, value); }
-        }
-
-        public OutlinedTextBlock()
-        {
-            UpdatePen();
-            TextDecorations = new TextDecorationCollection();
-        }
-
-        protected override void OnRender(DrawingContext drawingContext)
-        {
-            EnsureGeometry();
-
-            drawingContext.DrawGeometry(null, _Pen, _TextGeometry);
-            drawingContext.DrawGeometry(Fill, null, _TextGeometry);
-        }
-
-        protected override Size MeasureOverride(Size availableSize)
-        {
-            EnsureFormattedText();
-
-            // constrain the formatted text according to the available size
-
-            double w = availableSize.Width;
-            double h = availableSize.Height;
-
-            // the Math.Min call is important - without this constraint (which seems arbitrary, but is the maximum allowable text width), things blow up when availableSize is infinite in both directions
-            // the Math.Max call is to ensure we don't hit zero, which will cause MaxTextHeight to throw
-            _FormattedText.MaxTextWidth = Math.Min(3579139, w);
-            _FormattedText.MaxTextHeight = Math.Max(0.0001d, h);
-
-            // return the desired size
-            return new Size(Math.Ceiling(_FormattedText.Width), Math.Ceiling(_FormattedText.Height));
-        }
-
-        protected override Size ArrangeOverride(Size finalSize)
-        {
-            EnsureFormattedText();
-
-            // update the formatted text with the final size
-            _FormattedText.MaxTextWidth = finalSize.Width;
-            _FormattedText.MaxTextHeight = Math.Max(0.0001d, finalSize.Height);
-
-            // need to re-generate the geometry now that the dimensions have changed
-            _TextGeometry = null;
-
-            return finalSize;
-        }
-
-        private static void OnFormattedTextInvalidated(DependencyObject dependencyObject,
-          DependencyPropertyChangedEventArgs e)
-        {
-            var outlinedTextBlock = (OutlinedTextBlock)dependencyObject;
-            outlinedTextBlock._FormattedText = null;
-            outlinedTextBlock._TextGeometry = null;
-
-            outlinedTextBlock.InvalidateMeasure();
-            outlinedTextBlock.InvalidateVisual();
-        }
-
-        private static void OnFormattedTextUpdated(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
-        {
-            var outlinedTextBlock = (OutlinedTextBlock)dependencyObject;
-            outlinedTextBlock.UpdateFormattedText();
-            outlinedTextBlock._TextGeometry = null;
-
-            outlinedTextBlock.InvalidateMeasure();
-            outlinedTextBlock.InvalidateVisual();
-        }
-
-        private void EnsureFormattedText()
-        {
-            if (_FormattedText != null)
-            {
-                return;
-            }
-
-            _FormattedText = new FormattedText(
-              Text ?? "",
-              CultureInfo.CurrentUICulture,
-              FlowDirection,
-              new Typeface(FontFamily, FontStyle, FontWeight, FontStretch),
-              FontSize,
-              Brushes.Black);
-
-            UpdateFormattedText();
-        }
-
-        private void UpdateFormattedText()
-        {
-            if (_FormattedText == null)
-            {
-                return;
-            }
-
-            _FormattedText.MaxLineCount = TextWrapping == TextWrapping.NoWrap ? 1 : int.MaxValue;
-            _FormattedText.TextAlignment = TextAlignment;
-            _FormattedText.Trimming = TextTrimming;
-
-            _FormattedText.SetFontSize(FontSize);
-            _FormattedText.SetFontStyle(FontStyle);
-            _FormattedText.SetFontWeight(FontWeight);
-            _FormattedText.SetFontFamily(FontFamily);
-            _FormattedText.SetFontStretch(FontStretch);
-            _FormattedText.SetTextDecorations(TextDecorations);
-        }
-
-        private void EnsureGeometry()
-        {
-            if (_TextGeometry != null)
-            {
-                return;
-            }
-
-            EnsureFormattedText();
-            _TextGeometry = _FormattedText.BuildGeometry(new Point(0, 0));
-        }
-    }
-}

+ 2 - 2
PixiEditor/Views/UserControls/Palettes/ColorReplacer.xaml.cs

@@ -31,7 +31,7 @@ namespace PixiEditor.Views.UserControls.Palettes
 
         public Color NewColor
         {
-            get { return (Color) GetValue(NewColorProperty); }
+            get { return (Color)GetValue(NewColorProperty); }
             set { SetValue(NewColorProperty, value); }
         }
 
@@ -40,7 +40,7 @@ namespace PixiEditor.Views.UserControls.Palettes
 
         public ICommand ReplaceColorsCommand
         {
-            get { return (ICommand) GetValue(ReplaceColorsCommandProperty); }
+            get { return (ICommand)GetValue(ReplaceColorsCommandProperty); }
             set { SetValue(ReplaceColorsCommandProperty, value); }
         }
 

+ 0 - 1
PixiEditor/Views/UserControls/Palettes/PaletteColor.xaml.cs

@@ -40,7 +40,6 @@ public partial class PaletteColor : UserControl
     public static readonly DependencyProperty CornerRadiusProperty =
         DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(PaletteColor), new PropertyMetadata(new CornerRadius(5f)));
 
-    private Point prevPos;
     private Point clickPoint;
 
     public PaletteColor()

+ 3 - 14
PixiEditor/Views/UserControls/Palettes/PaletteColorAdder.xaml.cs

@@ -1,23 +1,12 @@
 using PixiEditor.Models.DataHolders;
 using SkiaSharp;
-using System;
-using System.Collections.Generic;
 using System.Collections.Specialized;
 using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
 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 PixiEditor.Views.UserControls.Palettes
-
 {
     /// <summary>
     /// Interaction logic for PaletteColorAdder.xaml
@@ -43,7 +32,7 @@ namespace PixiEditor.Views.UserControls.Palettes
         private static void OnHintColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
         {
             var adder = (PaletteColorAdder)d;
-            Color newColor = (Color) e.NewValue;
+            Color newColor = (Color)e.NewValue;
             if (newColor.A < 255)
             {
                 adder.HintColor = Color.FromArgb(255, newColor.R, newColor.G, newColor.B);
@@ -55,7 +44,7 @@ namespace PixiEditor.Views.UserControls.Palettes
 
         public WpfObservableRangeCollection<SKColor> Swatches
         {
-            get { return (WpfObservableRangeCollection<SKColor>) GetValue(SwatchesProperty); }
+            get { return (WpfObservableRangeCollection<SKColor>)GetValue(SwatchesProperty); }
             set { SetValue(SwatchesProperty, value); }
         }
 
@@ -116,7 +105,7 @@ namespace PixiEditor.Views.UserControls.Palettes
             }
         }
 
-        private void Swatches_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
+        private void Swatches_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
         {
             UpdateAddSwatchesButton();
         }

+ 6 - 11
PixiEditor/Views/UserControls/Palettes/PaletteViewer.xaml.cs

@@ -1,6 +1,4 @@
-using System.Collections;
-using System.Collections.Generic;
-using System.IO;
+using System.IO;
 using System.Linq;
 using System.Threading.Tasks;
 using System.Windows;
@@ -10,7 +8,6 @@ using System.Windows.Media;
 using Microsoft.Win32;
 using PixiEditor.Helpers;
 using PixiEditor.Models.DataHolders;
-using PixiEditor.Models.DataHolders.Palettes;
 using PixiEditor.Models.DataProviders;
 using PixiEditor.Models.IO;
 using PixiEditor.Views.Dialogs;
@@ -28,8 +25,8 @@ namespace PixiEditor.Views.UserControls.Palettes
 
         public WpfObservableRangeCollection<SKColor> Swatches
         {
-            get { return (WpfObservableRangeCollection<SKColor>) GetValue(SwatchesProperty); }
-            set { SetValue(SwatchesProperty, value); }
+            get => (WpfObservableRangeCollection<SKColor>)GetValue(SwatchesProperty);
+            set => SetValue(SwatchesProperty, value);
         }
         public static readonly DependencyProperty ColorsProperty = DependencyProperty.Register(
             "Colors", typeof(WpfObservableRangeCollection<SKColor>), typeof(PaletteViewer));
@@ -55,7 +52,7 @@ namespace PixiEditor.Views.UserControls.Palettes
 
         public ICommand ReplaceColorsCommand
         {
-            get { return (ICommand) GetValue(ReplaceColorsCommandProperty); }
+            get { return (ICommand)GetValue(ReplaceColorsCommandProperty); }
             set { SetValue(ReplaceColorsCommandProperty, value); }
         }
 
@@ -114,10 +111,7 @@ namespace PixiEditor.Views.UserControls.Palettes
             }
             set => _filesFilter = value;
         }
-
-
-        private PaletteList _cachedPaletteList;
-
+        
         public PaletteViewer()
         {
             InitializeComponent();
@@ -149,6 +143,7 @@ namespace PixiEditor.Views.UserControls.Palettes
         {
             var parser = FileParsers.FirstOrDefault(x => x.SupportedFileExtensions.Contains(Path.GetExtension(fileName)));
             var data = await parser.Parse(fileName);
+            if(data.IsCorrupted || data.Colors.Length == 0) return;
             Colors.Clear();
             Colors.AddRange(data.Colors);
         }

+ 1 - 1
PixiEditor/Views/UserControls/ToolSettingColorPicker.xaml.cs

@@ -17,7 +17,7 @@ namespace PixiEditor.Views
 
         public Color SelectedColor
         {
-            get => (Color) GetValue(SelectedColorProperty);
+            get => (Color)GetValue(SelectedColorProperty);
             set { SetValue(SelectedColorProperty, value); }
         }