Browse Source

Added Open Window to window provider and fixed browser documentless

Krzysztof Krysiński 2 years ago
parent
commit
9f0b6a9cce

+ 1 - 1
src/PixiEditor.Extensions/PixiEditor.Extensions.csproj

@@ -6,7 +6,7 @@
         <Nullable>enable</Nullable>
         <Nullable>enable</Nullable>
       <UseWPF>true</UseWPF>
       <UseWPF>true</UseWPF>
       <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
       <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
-      <Version>0.0.3</Version>
+      <Version>0.0.5</Version>
       <Title>PixiEditor Extensions</Title>
       <Title>PixiEditor Extensions</Title>
       <Authors>PixiEditor Organization</Authors>
       <Authors>PixiEditor Organization</Authors>
       <Copyright>PixiEditor Organization</Copyright>
       <Copyright>PixiEditor Organization</Copyright>

+ 2 - 0
src/PixiEditor.Extensions/Windowing/IPopupWindow.cs

@@ -2,8 +2,10 @@
 
 
 public interface IPopupWindow
 public interface IPopupWindow
 {
 {
+    public string UniqueId { get; }
     public string Title { get; set; }
     public string Title { get; set; }
     public void Show();
     public void Show();
+    public void Close();
     public bool? ShowDialog();
     public bool? ShowDialog();
     public double Width { get; set; }
     public double Width { get; set; }
     public double Height { get; set; }
     public double Height { get; set; }

+ 7 - 0
src/PixiEditor.Extensions/Windowing/IWindowProvider.cs

@@ -5,4 +5,11 @@ namespace PixiEditor.Extensions.Windowing;
 public interface IWindowProvider
 public interface IWindowProvider
 {
 {
     public PopupWindow CreatePopupWindow(string title, object body);
     public PopupWindow CreatePopupWindow(string title, object body);
+    public PopupWindow OpenWindow(WindowType type);
+    public PopupWindow OpenWindow(string windowId);
+}
+
+public enum WindowType
+{
+    BrowserPalette
 }
 }

+ 3 - 0
src/PixiEditor.Extensions/Windowing/PopupWindow.cs

@@ -2,6 +2,8 @@
 
 
 public class PopupWindow : IPopupWindow
 public class PopupWindow : IPopupWindow
 {
 {
+    public string UniqueId => _underlyingWindow.UniqueId;
+
     private IPopupWindow _underlyingWindow;
     private IPopupWindow _underlyingWindow;
 
 
     public PopupWindow(IPopupWindow basicPopup)
     public PopupWindow(IPopupWindow basicPopup)
@@ -16,6 +18,7 @@ public class PopupWindow : IPopupWindow
     }
     }
 
 
     public void Show() => _underlyingWindow.Show();
     public void Show() => _underlyingWindow.Show();
+    public void Close() => _underlyingWindow.Close();
 
 
     public bool? ShowDialog() => _underlyingWindow.ShowDialog();
     public bool? ShowDialog() => _underlyingWindow.ShowDialog();
     public double Width
     public double Width

+ 32 - 0
src/PixiEditor/Models/AppExtensions/Services/WindowProvider.cs

@@ -7,8 +7,40 @@ namespace PixiEditor.Models.AppExtensions.Services;
 
 
 public class WindowProvider : IWindowProvider
 public class WindowProvider : IWindowProvider
 {
 {
+    private Dictionary<string, Func<IPopupWindow>> _openHandlers = new();
+
+    public WindowProvider RegisterHandler(string id, Func<IPopupWindow> handler)
+    {
+        if (_openHandlers.ContainsKey(id))
+        {
+            _openHandlers[id] = handler;
+            throw new ArgumentException($"Window with id {id} already has a handler");
+        }
+
+        _openHandlers.Add(id, handler);
+        return this;
+    }
+
     public PopupWindow CreatePopupWindow(string title, object body)
     public PopupWindow CreatePopupWindow(string title, object body)
     {
     {
         return new PopupWindow(new BasicPopup { Title = title, Body = body });
         return new PopupWindow(new BasicPopup { Title = title, Body = body });
     }
     }
+
+    public PopupWindow OpenWindow(WindowType type)
+    {
+        return OpenWindow($"PixiEditor.{type}");
+    }
+
+    public PopupWindow OpenWindow(string windowId)
+    {
+        var handler = _openHandlers.FirstOrDefault(x => x.Key == windowId);
+        if (handler.Key != null)
+        {
+            return new PopupWindow(handler.Value());
+        }
+        else
+        {
+            throw new ArgumentException($"Window with id {windowId} does not exist");
+        }
+    }
 }
 }

+ 10 - 0
src/PixiEditor/ViewModels/SubViewModels/Main/ExtensionsViewModel.cs

@@ -1,8 +1,11 @@
 using Microsoft.Extensions.DependencyInjection;
 using Microsoft.Extensions.DependencyInjection;
 using PixiEditor.Extensions;
 using PixiEditor.Extensions;
 using PixiEditor.Extensions.Palettes;
 using PixiEditor.Extensions.Palettes;
+using PixiEditor.Extensions.Windowing;
 using PixiEditor.Models.AppExtensions;
 using PixiEditor.Models.AppExtensions;
+using PixiEditor.Models.AppExtensions.Services;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.DataHolders;
+using PixiEditor.Views.Dialogs;
 
 
 namespace PixiEditor.ViewModels.SubViewModels.Main;
 namespace PixiEditor.ViewModels.SubViewModels.Main;
 
 
@@ -12,6 +15,13 @@ internal class ExtensionsViewModel : SubViewModel<ViewModelMain>
     public ExtensionsViewModel(ViewModelMain owner, ExtensionLoader loader) : base(owner)
     public ExtensionsViewModel(ViewModelMain owner, ExtensionLoader loader) : base(owner)
     {
     {
         ExtensionLoader = loader;
         ExtensionLoader = loader;
+        ((WindowProvider)Owner.Services.GetService<IWindowProvider>()).RegisterHandler(PalettesBrowser.UniqueId, () =>
+        {
+            return PalettesBrowser.Open(
+                Owner.ColorsSubViewModel.PaletteProvider,
+                Owner.ColorsSubViewModel.ImportPaletteCommand,
+                Owner.DocumentManagerSubViewModel.ActiveDocument?.Palette);
+        });
         Owner.OnStartupEvent += Owner_OnStartupEvent;
         Owner.OnStartupEvent += Owner_OnStartupEvent;
     }
     }
 
 

+ 2 - 0
src/PixiEditor/Views/Dialogs/BasicPopup.xaml.cs

@@ -6,6 +6,8 @@ namespace PixiEditor.Views.Dialogs;
 
 
 internal partial class BasicPopup : Window, IPopupWindow
 internal partial class BasicPopup : Window, IPopupWindow
 {
 {
+    public string UniqueId => "PixiEditor.EmptyPopup";
+
     public RelayCommand CancelCommand { get; set; }
     public RelayCommand CancelCommand { get; set; }
 
 
     public static readonly DependencyProperty BodyProperty = DependencyProperty.Register(
     public static readonly DependencyProperty BodyProperty = DependencyProperty.Register(

+ 2 - 1
src/PixiEditor/Views/Dialogs/PalettesBrowser.xaml

@@ -97,7 +97,8 @@
                           IsChecked="{Binding ShowOnlyFavourites, ElementName=palettesBrowser}" ui:Translator.Key="FAVORITES"/>
                           IsChecked="{Binding ShowOnlyFavourites, ElementName=palettesBrowser}" ui:Translator.Key="FAVORITES"/>
             </StackPanel>
             </StackPanel>
             <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0 0 10 0">
             <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0 0 10 0">
-                <Button ui:Translator.TooltipKey="ADD_FROM_CURRENT_PALETTE" Click="AddFromPalette_OnClick" Cursor="Hand" Margin="10 0" Style="{StaticResource ImageButtonStyle}" Width="24" Height="24">
+                <Button ui:Translator.TooltipKey="ADD_FROM_CURRENT_PALETTE" Command="{Binding ElementName=palettesBrowser, Path=AddFromPaletteCommand}" Cursor="Hand" Margin="10 0"
+                        Style="{StaticResource ToolButtonStyle}" Width="24" Height="24">
                     <Image Source="/Images/Plus-square.png"/>
                     <Image Source="/Images/Plus-square.png"/>
                 </Button>
                 </Button>
                 <Button Cursor="Hand" Click="OpenFolder_OnClick" Style="{StaticResource ImageButtonStyle}" Width="24" Height="24"
                 <Button Cursor="Hand" Click="OpenFolder_OnClick" Style="{StaticResource ImageButtonStyle}" Width="24" Height="24"

+ 23 - 3
src/PixiEditor/Views/Dialogs/PalettesBrowser.xaml.cs

@@ -8,10 +8,12 @@ using System.Windows.Input;
 using System.Windows.Navigation;
 using System.Windows.Navigation;
 using Microsoft.Win32;
 using Microsoft.Win32;
 using PixiEditor.DrawingApi.Core.ColorsImpl;
 using PixiEditor.DrawingApi.Core.ColorsImpl;
+using PixiEditor.Extensions;
 using PixiEditor.Extensions.Common.Localization;
 using PixiEditor.Extensions.Common.Localization;
 using PixiEditor.Extensions.Common.UserPreferences;
 using PixiEditor.Extensions.Common.UserPreferences;
 using PixiEditor.Extensions.Palettes;
 using PixiEditor.Extensions.Palettes;
 using PixiEditor.Extensions.Palettes.Parsers;
 using PixiEditor.Extensions.Palettes.Parsers;
+using PixiEditor.Extensions.Windowing;
 using PixiEditor.Helpers;
 using PixiEditor.Helpers;
 using PixiEditor.Models.AppExtensions.Services;
 using PixiEditor.Models.AppExtensions.Services;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.DataHolders;
@@ -27,8 +29,11 @@ using PaletteColor = PixiEditor.Extensions.Palettes.PaletteColor;
 
 
 namespace PixiEditor.Views.Dialogs;
 namespace PixiEditor.Views.Dialogs;
 
 
-internal partial class PalettesBrowser : Window
+internal partial class PalettesBrowser : Window, IPopupWindow
 {
 {
+    public static string UniqueId => "PixiEditor.BrowserPalette";
+    string IPopupWindow.UniqueId => UniqueId;
+
     private const int ItemsPerLoad = 25;
     private const int ItemsPerLoad = 25;
 
 
     private readonly LocalizedString[] stopItTexts = new[]
     private readonly LocalizedString[] stopItTexts = new[]
@@ -66,6 +71,15 @@ internal partial class PalettesBrowser : Window
         set => SetValue(DeletePaletteCommandProperty, value);
         set => SetValue(DeletePaletteCommandProperty, value);
     }
     }
 
 
+    public static readonly DependencyProperty AddFromPaletteCommandProperty = DependencyProperty.Register(
+        nameof(AddFromPaletteCommand), typeof(ICommand), typeof(PalettesBrowser), new PropertyMetadata(default(ICommand)));
+
+    public ICommand AddFromPaletteCommand
+    {
+        get { return (ICommand)GetValue(AddFromPaletteCommandProperty); }
+        set { SetValue(AddFromPaletteCommandProperty, value); }
+    }
+
     public bool IsFetching
     public bool IsFetching
     {
     {
         get => (bool)GetValue(IsFetchingProperty);
         get => (bool)GetValue(IsFetchingProperty);
@@ -168,9 +182,10 @@ internal partial class PalettesBrowser : Window
         Instance = this;
         Instance = this;
         DeletePaletteCommand = new RelayCommand<Palette>(DeletePalette, CanDeletePalette);
         DeletePaletteCommand = new RelayCommand<Palette>(DeletePalette, CanDeletePalette);
         ToggleFavouriteCommand = new RelayCommand<Palette>(ToggleFavourite, CanToggleFavourite);
         ToggleFavouriteCommand = new RelayCommand<Palette>(ToggleFavourite, CanToggleFavourite);
+        AddFromPaletteCommand = new RelayCommand(AddFromCurrentPalette, CanAddFromPalette);
         Loaded += async (_, _) =>
         Loaded += async (_, _) =>
         {
         {
-            await LocalPalettesFetcher.RefreshCacheAll();
+            await UpdatePaletteList();
             LocalPalettesFetcher.CacheUpdated += LocalCacheRefreshed;
             LocalPalettesFetcher.CacheUpdated += LocalCacheRefreshed;
         };
         };
         Closed += (_, _) =>
         Closed += (_, _) =>
@@ -182,6 +197,11 @@ internal partial class PalettesBrowser : Window
         IPreferences.Current.AddCallback(PreferencesConstants.FavouritePalettes, OnFavouritePalettesChanged);
         IPreferences.Current.AddCallback(PreferencesConstants.FavouritePalettes, OnFavouritePalettesChanged);
     }
     }
 
 
+    private bool CanAddFromPalette(object param)
+    {
+        return CurrentEditingPalette != null;
+    }
+
     private bool CanDeletePalette(Palette palette)
     private bool CanDeletePalette(Palette palette)
     {
     {
         return palette != null && palette.Source.GetType() == typeof(LocalPalettesFetcher);
         return palette != null && palette.Source.GetType() == typeof(LocalPalettesFetcher);
@@ -520,7 +540,7 @@ internal partial class PalettesBrowser : Window
         }
         }
     }
     }
 
 
-    private async void AddFromPalette_OnClick(object sender, RoutedEventArgs e)
+    private async void AddFromCurrentPalette(object param)
     {
     {
         if (CurrentEditingPalette?.Count == 0)
         if (CurrentEditingPalette?.Count == 0)
             return;
             return;

+ 1 - 1
src/PixiEditor/Views/MainWindow.xaml

@@ -747,7 +747,7 @@
                                                     Colors="{Binding DocumentManagerSubViewModel.ActiveDocument.Palette}" 
                                                     Colors="{Binding DocumentManagerSubViewModel.ActiveDocument.Palette}" 
                                                     Visibility="{Binding RelativeSource={RelativeSource Mode=Self}, Path=ActualWidth, Converter={converters:PaletteViewerWidthToVisibilityConverter}}"/>
                                                     Visibility="{Binding RelativeSource={RelativeSource Mode=Self}, Path=ActualWidth, Converter={converters:PaletteViewerWidthToVisibilityConverter}}"/>
                                                 <palettes:PaletteViewer 
                                                 <palettes:PaletteViewer 
-                                                    IsEnabled="{Binding DocumentManagerSubViewModel.ActiveDocument, Converter={converters:NotNullToBoolConverter}}" 
+                                                    IsEnabled="{Binding DocumentManagerSubViewModel.ActiveDocument, Converter={converters:NotNullToBoolConverter}}"
                                                     Colors="{Binding DocumentManagerSubViewModel.ActiveDocument.Palette}"
                                                     Colors="{Binding DocumentManagerSubViewModel.ActiveDocument.Palette}"
                                                     Swatches="{Binding DocumentManagerSubViewModel.ActiveDocument.Swatches}"
                                                     Swatches="{Binding DocumentManagerSubViewModel.ActiveDocument.Swatches}"
                                                     SelectColorCommand="{cmds:Command PixiEditor.Colors.SelectColor, UseProvided=True}"
                                                     SelectColorCommand="{cmds:Command PixiEditor.Colors.SelectColor, UseProvided=True}"

+ 1 - 1
src/PixiEditor/Views/UserControls/Palettes/PaletteViewer.xaml

@@ -30,7 +30,7 @@
                                             HintColor="{Binding ElementName=paletteControl, Path=HintColor}"
                                             HintColor="{Binding ElementName=paletteControl, Path=HintColor}"
                                             Colors="{Binding ElementName=paletteControl, Path=Colors}"/>
                                             Colors="{Binding ElementName=paletteControl, Path=Colors}"/>
                     <StackPanel Margin="0, 0, 5, 0" HorizontalAlignment="Right" Width="110" VerticalAlignment="Center" Orientation="Horizontal">
                     <StackPanel Margin="0, 0, 5, 0" HorizontalAlignment="Right" Width="110" VerticalAlignment="Center" Orientation="Horizontal">
-                        <Button Margin="0, 0, 5, 0" Style="{StaticResource ToolButtonStyle}" Click="BrowsePalettes_Click"
+                        <Button IsEnabled="True" Margin="0, 0, 5, 0" Style="{StaticResource ToolButtonStyle}" Click="BrowsePalettes_Click"
                 Cursor="Hand" Height="24" Width="24" ui:Translator.TooltipKey="BROWSE_PALETTES">
                 Cursor="Hand" Height="24" Width="24" ui:Translator.TooltipKey="BROWSE_PALETTES">
                             <Button.Background>
                             <Button.Background>
                                 <ImageBrush ImageSource="/Images/Database.png"/>
                                 <ImageBrush ImageSource="/Images/Database.png"/>