Sfoglia il codice sorgente

Gallery updates and fixes

Ruben 1 anno fa
parent
commit
95e659f7c7

+ 24 - 5
src/PicView.Avalonia/Gallery/GalleryFunctions.cs

@@ -374,12 +374,31 @@ public static class GalleryFunctions
          return;
          void ClearItems()
          {
-             var mainView = UIHelper.GetMainView;
+             try
+             {
+                 var mainView = UIHelper.GetMainView;
 
-             var galleryListBox = mainView.GalleryView.GalleryListBox;
-             if (galleryListBox == null) 
-                 return;
-             galleryListBox.Items.Clear();
+                 var galleryListBox = mainView.GalleryView.GalleryListBox;
+                 if (galleryListBox == null) 
+                     return;
+                 for (var i = 0; i < galleryListBox.ItemCount; i++)
+                 {
+                     if (galleryListBox.Items[i] is not GalleryItem galleryItem) 
+                         continue;
+                     if (galleryItem.GalleryImage.Source is IDisposable galleryImage)
+                     {
+                         galleryImage.Dispose();
+                     }
+                     galleryListBox.Items.Remove(galleryItem);
+                 }
+                 galleryListBox.Items.Clear();
+             }
+             catch (Exception e)
+             {
+#if DEBUG
+                     Console.WriteLine(e);
+#endif
+             }
          }
      }
 }

+ 4 - 11
src/PicView.Avalonia/Gallery/GalleryLoad.cs

@@ -140,10 +140,7 @@ public static class GalleryLoad
         }
         catch (OperationCanceledException)
         {
-            await Dispatcher.UIThread.InvokeAsync(() =>
-            {
-                galleryListBox?.Items.Clear();
-            });
+            await Dispatcher.UIThread.InvokeAsync(GalleryFunctions.Clear);
         }
         catch (Exception e)
         {
@@ -206,7 +203,7 @@ public static class GalleryLoad
                     if (vm.ImageIterator is null)
                     {
                         ct.ThrowIfCancellationRequested();
-                        galleryListBox?.Items.Clear();
+                        GalleryFunctions.Clear();
                         if (GalleryFunctions.IsBottomGalleryOpen)
                         {
                             mainView.GalleryView.GalleryMode = GalleryMode.BottomToClosed;
@@ -233,9 +230,7 @@ public static class GalleryLoad
             {
                 try
                 {
-                    var mainView = UIHelper.GetMainView;
-                    var galleryListBox = mainView.GalleryView.GalleryListBox;
-                    galleryListBox?.Items.Clear();
+                    GalleryFunctions.Clear();
                 }
                 catch (Exception e)
                 {
@@ -260,9 +255,7 @@ public static class GalleryLoad
         {
             try
             {
-                var mainView = UIHelper.GetMainView;
-                var galleryListBox = mainView.GalleryView.GalleryListBox;
-                galleryListBox?.Items.Clear();
+                GalleryFunctions.Clear();
             }
             catch (Exception e)
             {

+ 2 - 16
src/PicView.Avalonia/ViewModels/MainViewModel.cs

@@ -224,22 +224,8 @@ public class MainViewModel : ViewModelBase
     private double _getGalleryItemHeight;
     public double GetGalleryItemHeight
     {
-        get
-        {
-            return GalleryFunctions.IsFullGalleryOpen ? GetFullGalleryItemHeight : GetBottomGalleryItemHeight;
-        }
-        set
-        {
-            this.RaiseAndSetIfChanged(ref _getGalleryItemHeight, value);
-            if (GalleryFunctions.IsBottomGalleryOpen && !GalleryFunctions.IsFullGalleryOpen)
-            {
-                GetBottomGalleryItemHeight = value;
-            }
-            else
-            {
-                GetFullGalleryItemHeight = value;
-            }
-        }
+        get => _getGalleryItemHeight;
+        set => this.RaiseAndSetIfChanged(ref _getGalleryItemHeight, value);
     }
 
     private double _getFullGalleryItemHeight = SettingsHelper.Settings.Gallery.ExpandedGalleryItemSize;

+ 5 - 9
src/PicView.Avalonia/Views/GalleryView.axaml

@@ -23,11 +23,9 @@
             <ContextMenu Opened="MenuBase_OnOpened">
                 <MenuItem Header="{CompiledBinding Stretch, Mode=OneWay}">
                     <MenuItem.Icon>
-                        <Path
-                            Data="{StaticResource ImageGeometry}"
-                            Fill="{DynamicResource MainTextColor}"
+                        <Image
                             Height="12"
-                            Stretch="Fill"
+                            Source="{StaticResource Image}"
                             Width="12" />
                     </MenuItem.Icon>
                     <MenuItem
@@ -79,11 +77,9 @@
                     PointerPressed="Flyout_OnPointerPressed"
                     StaysOpenOnClick="True">
                     <MenuItem.Icon>
-                        <Path
-                            Data="{StaticResource ImageGeometry}"
-                            Fill="{DynamicResource MainTextColor}"
+                        <Image
                             Height="12"
-                            Stretch="Fill"
+                            Source="{StaticResource Image}"
                             Width="12" />
                     </MenuItem.Icon>
                     <FlyoutBase.AttachedFlyout>
@@ -100,7 +96,7 @@
                     </FlyoutBase.AttachedFlyout>
                 </MenuItem>
                 <Separator />
-                <MenuItem Command="{CompiledBinding CloseGalleryCommand}" Header="{CompiledBinding Close, Mode=OneWay}">
+                <MenuItem Command="{CompiledBinding CloseGalleryCommand}" Header="{CompiledBinding HideBottomGallery, Mode=OneWay}">
                     <MenuItem.Icon>
                         <Path
                             Data="{StaticResource CloseGeometry}"

+ 2 - 1
src/PicView.Avalonia/Views/UC/BottomGalleryItemSizeSlider.axaml

@@ -19,7 +19,8 @@
         Minimum="{CompiledBinding MinBottomGalleryItemHeight}"
         TickFrequency="1"
         TickPlacement="BottomRight"
-        Value="{CompiledBinding GetBottomGalleryItemHeight}"
+        Value="{CompiledBinding GetBottomGalleryItemHeight,
+                                Mode=OneWay}"
         ValueChanged="BottomGallery_OnValueChanged"
         Width="300" />
 </UserControl>

+ 7 - 11
src/PicView.Avalonia/Views/UC/BottomGalleryItemSizeSlider.axaml.cs

@@ -1,6 +1,4 @@
-using Avalonia;
-using Avalonia.Controls;
-using Avalonia.Controls.ApplicationLifetimes;
+using Avalonia.Controls;
 using Avalonia.Controls.Primitives;
 using PicView.Avalonia.Gallery;
 using PicView.Avalonia.UI;
@@ -17,8 +15,7 @@ public partial class BottomGalleryItemSizeSlider : UserControl
     }
     private void BottomGallery_OnValueChanged(object? sender, RangeBaseValueChangedEventArgs e)
     {
-        if (DataContext is not MainViewModel vm ||
-            Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
+        if (DataContext is not MainViewModel vm )
         {
             return;
         }
@@ -27,18 +24,17 @@ public partial class BottomGalleryItemSizeSlider : UserControl
         {
             return;
         }
-        SettingsHelper.Settings.Gallery.BottomGalleryItemSize = e.NewValue;
-        if (GalleryFunctions.IsBottomGalleryOpen && !GalleryFunctions.IsFullGalleryOpen)
+        vm.GetBottomGalleryItemHeight = e.NewValue;
+        
+        if (SettingsHelper.Settings.Gallery.IsBottomGalleryShown && !GalleryFunctions.IsFullGalleryOpen)
         {
             vm.GetGalleryItemHeight = e.NewValue;
-            var mainView = desktop.MainWindow.GetControl<MainView>("MainView");
-            var gallery = mainView.GalleryView;
-            gallery.Height = vm.GalleryHeight;
+            UIHelper.GetGalleryView.Height = vm.GalleryHeight;
             WindowHelper.SetSize(vm);
         }
         
         // Binding to height depends on timing of the update. Maybe find a cleaner mvvm solution one day
         // Maybe save this on close or some other way
-        _ = SettingsHelper.SaveSettingsAsync();
+        SettingsHelper.Settings.Gallery.BottomGalleryItemSize = e.NewValue;
     }
 }

+ 1 - 1
src/PicView.Avalonia/Views/UC/FullGalleryItemSizeSlider.axaml

@@ -20,7 +20,7 @@
         TickFrequency="1"
         TickPlacement="BottomRight"
         Value="{CompiledBinding GetFullGalleryItemHeight,
-                                Mode=TwoWay}"
+                                Mode=OneWay}"
         ValueChanged="FullGallery_OnValueChanged"
         Width="300" />
 </UserControl>

+ 2 - 2
src/PicView.Avalonia/Views/UC/FullGalleryItemSizeSlider.axaml.cs

@@ -25,14 +25,14 @@ public partial class FullGalleryItemSizeSlider : UserControl
         {
             return;
         }
-        
+        vm.GetFullGalleryItemHeight = e.NewValue;
         if (GalleryFunctions.IsFullGalleryOpen)
         {
             vm.GetGalleryItemHeight = vm.GetFullGalleryItemHeight;
             WindowHelper.SetSize(vm);
         }
         // Binding to height depends on timing of the update. Maybe find a cleaner mvvm solution one day
-
+        
         // Maybe save this on close or some other way
         SettingsHelper.Settings.Gallery.ExpandedGalleryItemSize = e.NewValue;
     }

+ 87 - 0
src/PicView.Avalonia/Views/UC/GalleryItem.axaml

@@ -7,6 +7,7 @@
     xmlns="https://github.com/avaloniaui"
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+    xmlns:uc="clr-namespace:PicView.Avalonia.Views.UC"
     xmlns:viewModels="clr-namespace:PicView.Avalonia.ViewModels"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
     <Border
@@ -212,6 +213,92 @@
                             Width="12" />
                     </MenuItem.Icon>
                 </MenuItem>
+                <Separator />
+                <MenuItem Header="{CompiledBinding Stretch, Mode=OneWay}">
+                    <MenuItem.Icon>
+                        <Image
+                            Height="12"
+                            Source="{StaticResource Image}"
+                            Width="12" />
+                    </MenuItem.Icon>
+                    <MenuItem
+                        Command="{CompiledBinding GalleryItemStretchCommand}"
+                        CommandParameter="Uniform"
+                        Header="{CompiledBinding Uniform,
+                                                 Mode=OneWay}"
+                        IsChecked="{CompiledBinding IsUniformMenuChecked}"
+                        ToggleType="Radio" />
+                    <MenuItem
+                        Command="{CompiledBinding GalleryItemStretchCommand}"
+                        CommandParameter="UniformToFill"
+                        Header="{CompiledBinding UniformToFill,
+                                                 Mode=OneWay}"
+                        IsChecked="{CompiledBinding IsUniformToFillMenuChecked}"
+                        ToggleType="Radio" />
+                    <MenuItem
+                        Command="{CompiledBinding GalleryItemStretchCommand}"
+                        CommandParameter="Fill"
+                        Header="{CompiledBinding Fill,
+                                                 Mode=OneWay}"
+                        IsChecked="{CompiledBinding IsFillMenuChecked}"
+                        ToggleType="Radio" />
+                    <MenuItem
+                        Command="{CompiledBinding GalleryItemStretchCommand}"
+                        CommandParameter="None"
+                        Header="{CompiledBinding None,
+                                                 Mode=OneWay}"
+                        IsChecked="{CompiledBinding IsNoneMenuChecked}"
+                        ToggleType="Radio" />
+                    <MenuItem
+                        Command="{CompiledBinding GalleryItemStretchCommand}"
+                        CommandParameter="Square"
+                        Header="{CompiledBinding Square,
+                                                 Mode=OneWay}"
+                        IsChecked="{CompiledBinding IsSquareMenuChecked}"
+                        ToggleType="Radio" />
+                    <MenuItem
+                        Command="{CompiledBinding GalleryItemStretchCommand}"
+                        CommandParameter="FillSquare"
+                        Header="{CompiledBinding FillSquare,
+                                                 Mode=OneWay}"
+                        IsChecked="{CompiledBinding IsFillSquareMenuChecked}"
+                        ToggleType="Radio" />
+                </MenuItem>
+                <MenuItem
+                    Header="{CompiledBinding ExpandedGalleryItemSize,
+                                             Mode=OneWay}"
+                    PointerPressed="Flyout_OnPointerPressed"
+                    StaysOpenOnClick="True">
+                    <MenuItem.Icon>
+                        <Image
+                            Height="12"
+                            Source="{StaticResource Image}"
+                            Width="12" />
+                    </MenuItem.Icon>
+                    <FlyoutBase.AttachedFlyout>
+                        <Flyout>
+                            <Border
+                                Background="{DynamicResource MainButtonBackgroundColor}"
+                                BorderBrush="{DynamicResource MainBorderColor}"
+                                BorderThickness="2"
+                                CornerRadius="4"
+                                Padding="10,3">
+                                <uc:GalleryItemSizeSlider x:Name="GalleryItemSizeSlider" />
+                            </Border>
+                        </Flyout>
+                    </FlyoutBase.AttachedFlyout>
+                </MenuItem>
+                <Separator />
+                <MenuItem Command="{CompiledBinding CloseGalleryCommand}" Header="{CompiledBinding HideBottomGallery, Mode=OneWay}">
+                    <MenuItem.Icon>
+                        <Path
+                            Data="{StaticResource CloseGeometry}"
+                            Fill="{DynamicResource MainTextColor}"
+                            Height="12"
+                            Stretch="Fill"
+                            Width="12" />
+                    </MenuItem.Icon>
+                </MenuItem>
             </ContextMenu>
         </Border.ContextMenu>
     </Border>

+ 13 - 0
src/PicView.Avalonia/Views/UC/GalleryItem.axaml.cs

@@ -1,5 +1,7 @@
 using Avalonia;
 using Avalonia.Controls;
+using Avalonia.Controls.Primitives;
+using Avalonia.Input;
 using Avalonia.Media;
 using Exception = System.Exception;
 
@@ -36,5 +38,16 @@ namespace PicView.Avalonia.Views.UC
                 ImageBorder.BorderBrush = Brushes.Transparent;
             };
         }
+        
+        private void Flyout_OnPointerPressed(object? sender, PointerPressedEventArgs e)
+        {
+            if (sender is not Control ctl)
+            {
+                return;
+            }
+
+            FlyoutBase.ShowAttachedFlyout(ctl);
+            GalleryItemSizeSlider.SetMaxAndMin();
+        }
     }
 }

+ 1 - 1
src/PicView.Avalonia/Views/UC/GalleryItemSizeSlider.axaml

@@ -18,7 +18,7 @@
         TickFrequency="1"
         TickPlacement="BottomRight"
         Value="{CompiledBinding GetGalleryItemHeight,
-                                Mode=TwoWay}"
+                                Mode=OneWay}"
         ValueChanged="RangeBase_OnValueChanged"
         Width="270"
         x:Name="CustomSlider" />

+ 21 - 25
src/PicView.Avalonia/Views/UC/GalleryItemSizeSlider.axaml.cs

@@ -1,6 +1,4 @@
-using Avalonia;
-using Avalonia.Controls;
-using Avalonia.Controls.ApplicationLifetimes;
+using Avalonia.Controls;
 using Avalonia.Controls.Primitives;
 using PicView.Avalonia.Gallery;
 using PicView.Avalonia.UI;
@@ -37,45 +35,43 @@ public partial class GalleryItemSizeSlider : UserControl
 
     private void RangeBase_OnValueChanged(object? sender, RangeBaseValueChangedEventArgs e)
     {
-        if (DataContext is not MainViewModel vm ||
-            Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
+        if (DataContext is not MainViewModel vm)
         {
             return;
         }
 
         if (GalleryFunctions.IsFullGalleryOpen)
         {
-            
-        }
-        else if (GalleryFunctions.IsBottomGalleryOpen)
-        {
-            // Change the sizes of the bottom gallery items
             // ReSharper disable once CompareOfFloatsByEqualityOperator
-            if (vm.GetBottomGalleryItemHeight == e.NewValue)
+            if (vm.GetFullGalleryItemHeight == e.NewValue)
             {
                 return;
             }
-            SettingsHelper.Settings.Gallery.BottomGalleryItemSize = e.NewValue;
-            vm.GetGalleryItemHeight = e.NewValue;
-            var mainView = desktop.MainWindow.GetControl<MainView>("MainView");
-            var gallery = mainView.GalleryView;
-            gallery.Height = vm.GalleryHeight;
+            vm.GetFullGalleryItemHeight = e.NewValue;
+            vm.GetGalleryItemHeight = vm.GetFullGalleryItemHeight;
             WindowHelper.SetSize(vm);
+            // Binding to height depends on timing of the update. Maybe find a cleaner mvvm solution one day
+        
+            // Maybe save this on close or some other way
+            SettingsHelper.Settings.Gallery.ExpandedGalleryItemSize = e.NewValue;
+            
         }
-        else
+        else if (SettingsHelper.Settings.Gallery.IsBottomGalleryShown)
         {
             // ReSharper disable once CompareOfFloatsByEqualityOperator
-            if (vm.GetGalleryItemHeight == e.NewValue)
+            if (vm.GetBottomGalleryItemHeight == e.NewValue)
             {
                 return;
             }
-            vm.GetFullGalleryItemHeight = e.NewValue;
-            if (GalleryFunctions.IsFullGalleryOpen)
-            {
-                WindowHelper.SetSize(vm);
-                vm.GetGalleryItemHeight = e.NewValue;
-            }
-            SettingsHelper.Settings.Gallery.ExpandedGalleryItemSize = e.NewValue;
+            vm.GetBottomGalleryItemHeight = e.NewValue;
+        
+            vm.GetGalleryItemHeight = e.NewValue;
+            UIHelper.GetGalleryView.Height = vm.GalleryHeight;
+            WindowHelper.SetSize(vm);
+        
+            // Binding to height depends on timing of the update. Maybe find a cleaner mvvm solution one day
+            // Maybe save this on close or some other way
+            SettingsHelper.Settings.Gallery.BottomGalleryItemSize = e.NewValue;
         }
        
         _ = SettingsHelper.SaveSettingsAsync();