forked from LykosAI/StabilityMatrix
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathImageGalleryCardViewModel.cs
More file actions
166 lines (138 loc) · 4.8 KB
/
ImageGalleryCardViewModel.cs
File metadata and controls
166 lines (138 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System;
using System.Collections.Specialized;
using System.IO;
using System.Threading.Tasks;
using Avalonia.Collections;
using Avalonia.Media;
using Avalonia.Media.Imaging;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Injectio.Attributes;
using NLog;
using StabilityMatrix.Avalonia.Controls;
using StabilityMatrix.Avalonia.Helpers;
using StabilityMatrix.Avalonia.Models;
using StabilityMatrix.Avalonia.Services;
using StabilityMatrix.Avalonia.ViewModels.Base;
using StabilityMatrix.Avalonia.ViewModels.Dialogs;
using StabilityMatrix.Avalonia.Views.Dialogs;
using StabilityMatrix.Core.Attributes;
using StabilityMatrix.Core.Helper;
using StabilityMatrix.Core.Services;
namespace StabilityMatrix.Avalonia.ViewModels.Inference;
[View(typeof(ImageGalleryCard))]
[ManagedService]
[RegisterTransient<ImageGalleryCardViewModel>]
public partial class ImageGalleryCardViewModel : ViewModelBase
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly IServiceManager<ViewModelBase> vmFactory;
[ObservableProperty]
private bool isPreviewOverlayEnabled;
[ObservableProperty]
private Bitmap? previewImage;
[ObservableProperty]
private AvaloniaList<ImageSource> imageSources = new();
[ObservableProperty]
private ImageSource? selectedImage;
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(CanNavigateBack), nameof(CanNavigateForward))]
private int selectedImageIndex;
[ObservableProperty]
private bool isPixelGridEnabled;
public bool HasMultipleImages => ImageSources.Count > 1;
public bool CanNavigateBack => SelectedImageIndex > 0;
public bool CanNavigateForward => SelectedImageIndex < ImageSources.Count - 1;
public ImageGalleryCardViewModel(
IServiceManager<ViewModelBase> vmFactory,
ISettingsManager settingsManager
)
{
this.vmFactory = vmFactory;
IsPixelGridEnabled = settingsManager.Settings.IsImageViewerPixelGridEnabled;
settingsManager.RegisterPropertyChangedHandler(
s => s.IsImageViewerPixelGridEnabled,
newValue =>
{
IsPixelGridEnabled = newValue;
}
);
ImageSources.CollectionChanged += OnImageSourcesItemsChanged;
}
public void SetPreviewImage(byte[] imageBytes)
{
Dispatcher.UIThread.Post(() =>
{
using var stream = new MemoryStream(imageBytes);
var bitmap = new Bitmap(stream);
var currentImage = PreviewImage;
PreviewImage = bitmap;
IsPreviewOverlayEnabled = true;
// currentImage?.Dispose();
});
}
private void OnImageSourcesItemsChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
if (sender is AvaloniaList<ImageSource> sources)
{
if (
e.Action
is NotifyCollectionChangedAction.Add
or NotifyCollectionChangedAction.Remove
or NotifyCollectionChangedAction.Reset
)
{
if (sources.Count == 0)
{
SelectedImageIndex = -1;
}
else if (SelectedImageIndex == -1)
{
SelectedImageIndex = 0;
}
// Clamp the selected index to the new range
else
{
SelectedImageIndex = Math.Clamp(SelectedImageIndex, 0, sources.Count - 1);
}
OnPropertyChanged(nameof(CanNavigateBack));
OnPropertyChanged(nameof(CanNavigateForward));
OnPropertyChanged(nameof(HasMultipleImages));
}
}
}
[RelayCommand]
// ReSharper disable once UnusedMember.Local
private async Task FlyoutCopy(IImage? image)
{
if (image is null)
{
Logger.Trace("FlyoutCopy: image is null");
return;
}
Logger.Trace($"FlyoutCopy is copying bitmap...");
await Task.Run(() =>
{
if (Compat.IsWindows)
{
WindowsClipboard.SetBitmap((Bitmap)image);
}
});
}
[RelayCommand]
// ReSharper disable once UnusedMember.Local
private async Task FlyoutPreview(IImage? image)
{
if (image is null)
{
Logger.Trace("FlyoutPreview: image is null");
return;
}
Logger.Trace($"FlyoutPreview opening...");
var viewerVm = vmFactory.Get<ImageViewerViewModel>();
viewerVm.ImageSource = new ImageSource((Bitmap)image);
var dialog = new BetterContentDialog { Content = new ImageViewerDialog { DataContext = viewerVm } };
await dialog.ShowAsync();
}
}