JT
1 year ago
committed by
GitHub
32 changed files with 792 additions and 15 deletions
@ -0,0 +1,7 @@
|
||||
namespace StabilityMatrix.Avalonia.Models; |
||||
|
||||
public class PackageOutputCategory |
||||
{ |
||||
public required string Name { get; set; } |
||||
public required string Path { get; set; } |
||||
} |
@ -0,0 +1,265 @@
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Collections.ObjectModel; |
||||
using System.IO; |
||||
using System.Linq; |
||||
using System.Reactive.Linq; |
||||
using System.Threading.Tasks; |
||||
using AsyncAwaitBestPractices; |
||||
using AsyncImageLoader; |
||||
using Avalonia.Controls; |
||||
using Avalonia.Threading; |
||||
using CommunityToolkit.Mvvm.ComponentModel; |
||||
using DynamicData; |
||||
using DynamicData.Binding; |
||||
using FluentAvalonia.UI.Controls; |
||||
using StabilityMatrix.Avalonia.Extensions; |
||||
using StabilityMatrix.Avalonia.Models; |
||||
using StabilityMatrix.Avalonia.Services; |
||||
using StabilityMatrix.Avalonia.ViewModels.Base; |
||||
using StabilityMatrix.Avalonia.ViewModels.Dialogs; |
||||
using StabilityMatrix.Avalonia.Views; |
||||
using StabilityMatrix.Core.Attributes; |
||||
using StabilityMatrix.Core.Helper; |
||||
using StabilityMatrix.Core.Helper.Factory; |
||||
using StabilityMatrix.Core.Models; |
||||
using StabilityMatrix.Core.Models.Database; |
||||
using StabilityMatrix.Core.Models.FileInterfaces; |
||||
using StabilityMatrix.Core.Processes; |
||||
using StabilityMatrix.Core.Services; |
||||
using Symbol = FluentIcons.Common.Symbol; |
||||
using SymbolIconSource = FluentIcons.FluentAvalonia.SymbolIconSource; |
||||
|
||||
namespace StabilityMatrix.Avalonia.ViewModels; |
||||
|
||||
[View(typeof(OutputsPage))] |
||||
public partial class OutputsPageViewModel : PageViewModelBase |
||||
{ |
||||
private readonly ISettingsManager settingsManager; |
||||
private readonly INotificationService notificationService; |
||||
private readonly INavigationService navigationService; |
||||
public override string Title => "Outputs"; |
||||
|
||||
public override IconSource IconSource => |
||||
new SymbolIconSource { Symbol = Symbol.Grid, IsFilled = true }; |
||||
|
||||
public SourceCache<LocalImageFile, string> OutputsCache { get; } = new(p => p.AbsolutePath); |
||||
|
||||
public IObservableCollection<LocalImageFile> Outputs { get; } = |
||||
new ObservableCollectionExtended<LocalImageFile>(); |
||||
|
||||
public IEnumerable<SharedOutputType> OutputTypes { get; } = Enum.GetValues<SharedOutputType>(); |
||||
|
||||
[ObservableProperty] |
||||
private ObservableCollection<PackageOutputCategory> categories; |
||||
|
||||
[ObservableProperty] |
||||
[NotifyPropertyChangedFor(nameof(CanShowOutputTypes))] |
||||
private PackageOutputCategory selectedCategory; |
||||
|
||||
[ObservableProperty] |
||||
private SharedOutputType selectedOutputType; |
||||
|
||||
public bool CanShowOutputTypes => SelectedCategory.Name.Equals("Shared Output Folder"); |
||||
|
||||
public OutputsPageViewModel( |
||||
ISettingsManager settingsManager, |
||||
IPackageFactory packageFactory, |
||||
INotificationService notificationService, |
||||
INavigationService navigationService |
||||
) |
||||
{ |
||||
this.settingsManager = settingsManager; |
||||
this.notificationService = notificationService; |
||||
this.navigationService = navigationService; |
||||
|
||||
OutputsCache |
||||
.Connect() |
||||
.DeferUntilLoaded() |
||||
.SortBy(x => x.CreatedAt, SortDirection.Descending) |
||||
.Bind(Outputs) |
||||
.Subscribe(); |
||||
|
||||
if (!settingsManager.IsLibraryDirSet || Design.IsDesignMode) |
||||
return; |
||||
|
||||
var packageCategories = settingsManager.Settings.InstalledPackages |
||||
.Where(x => !x.UseSharedOutputFolder) |
||||
.Select(p => |
||||
{ |
||||
var basePackage = packageFactory[p.PackageName!]; |
||||
if (basePackage is null) |
||||
return null; |
||||
|
||||
return new PackageOutputCategory |
||||
{ |
||||
Path = Path.Combine(p.FullPath, basePackage.OutputFolderName), |
||||
Name = p.DisplayName |
||||
}; |
||||
}) |
||||
.ToList(); |
||||
|
||||
packageCategories.Insert( |
||||
0, |
||||
new PackageOutputCategory |
||||
{ |
||||
Path = settingsManager.ImagesDirectory, |
||||
Name = "Shared Output Folder" |
||||
} |
||||
); |
||||
|
||||
Categories = new ObservableCollection<PackageOutputCategory>(packageCategories); |
||||
SelectedCategory = Categories.First(); |
||||
SelectedOutputType = SharedOutputType.All; |
||||
} |
||||
|
||||
public override void OnLoaded() |
||||
{ |
||||
if (Design.IsDesignMode) |
||||
return; |
||||
|
||||
var path = |
||||
CanShowOutputTypes && SelectedOutputType != SharedOutputType.All |
||||
? Path.Combine(SelectedCategory.Path, SelectedOutputType.ToString()) |
||||
: SelectedCategory.Path; |
||||
GetOutputs(path); |
||||
} |
||||
|
||||
partial void OnSelectedCategoryChanged( |
||||
PackageOutputCategory? oldValue, |
||||
PackageOutputCategory? newValue |
||||
) |
||||
{ |
||||
if (oldValue == newValue || newValue == null) |
||||
return; |
||||
|
||||
var path = |
||||
CanShowOutputTypes && SelectedOutputType != SharedOutputType.All |
||||
? Path.Combine(newValue.Path, SelectedOutputType.ToString()) |
||||
: SelectedCategory.Path; |
||||
GetOutputs(path); |
||||
} |
||||
|
||||
partial void OnSelectedOutputTypeChanged(SharedOutputType oldValue, SharedOutputType newValue) |
||||
{ |
||||
if (oldValue == newValue) |
||||
return; |
||||
|
||||
var path = |
||||
newValue == SharedOutputType.All |
||||
? SelectedCategory.Path |
||||
: Path.Combine(SelectedCategory.Path, newValue.ToString()); |
||||
GetOutputs(path); |
||||
} |
||||
|
||||
public async Task OnImageClick(LocalImageFile item) |
||||
{ |
||||
var currentIndex = Outputs.IndexOf(item); |
||||
|
||||
var image = new ImageSource(new FilePath(item.AbsolutePath)); |
||||
|
||||
// Preload |
||||
await image.GetBitmapAsync(); |
||||
|
||||
var vm = new ImageViewerViewModel { ImageSource = image, LocalImageFile = item }; |
||||
|
||||
using var onNext = Observable |
||||
.FromEventPattern<DirectionalNavigationEventArgs>( |
||||
vm, |
||||
nameof(ImageViewerViewModel.NavigationRequested) |
||||
) |
||||
.Subscribe(ctx => |
||||
{ |
||||
Dispatcher.UIThread |
||||
.InvokeAsync(async () => |
||||
{ |
||||
var sender = (ImageViewerViewModel)ctx.Sender!; |
||||
var newIndex = currentIndex + (ctx.EventArgs.IsNext ? 1 : -1); |
||||
|
||||
if (newIndex >= 0 && newIndex < Outputs.Count) |
||||
{ |
||||
var newImage = Outputs[newIndex]; |
||||
var newImageSource = new ImageSource( |
||||
new FilePath(newImage.AbsolutePath) |
||||
); |
||||
|
||||
// Preload |
||||
await newImageSource.GetBitmapAsync(); |
||||
|
||||
sender.ImageSource = newImageSource; |
||||
sender.LocalImageFile = newImage; |
||||
|
||||
currentIndex = newIndex; |
||||
} |
||||
}) |
||||
.SafeFireAndForget(); |
||||
}); |
||||
|
||||
await vm.GetDialog().ShowAsync(); |
||||
} |
||||
|
||||
public async Task CopyImage(string imagePath) |
||||
{ |
||||
var clipboard = App.Clipboard; |
||||
|
||||
await clipboard.SetFileDataObjectAsync(imagePath); |
||||
} |
||||
|
||||
public async Task OpenImage(string imagePath) => await ProcessRunner.OpenFileBrowser(imagePath); |
||||
|
||||
public async Task DeleteImage(LocalImageFile? item) |
||||
{ |
||||
if (item?.GetFullPath(settingsManager.ImagesDirectory) is not { } imagePath) |
||||
{ |
||||
return; |
||||
} |
||||
|
||||
// Delete the file |
||||
var imageFile = new FilePath(imagePath); |
||||
var result = await notificationService.TryAsync(imageFile.DeleteAsync()); |
||||
|
||||
if (!result.IsSuccessful) |
||||
{ |
||||
return; |
||||
} |
||||
|
||||
Outputs.Remove(item); |
||||
|
||||
// Invalidate cache |
||||
if (ImageLoader.AsyncImageLoader is FallbackRamCachedWebImageLoader loader) |
||||
{ |
||||
loader.RemoveAllNamesFromCache(imageFile.Name); |
||||
} |
||||
} |
||||
|
||||
public void SendToTextToImage(LocalImageFile image) |
||||
{ |
||||
navigationService.NavigateTo<InferenceViewModel>(); |
||||
EventManager.Instance.OnInferenceTextToImageRequested(image); |
||||
} |
||||
|
||||
public void SendToUpscale(LocalImageFile image) |
||||
{ |
||||
navigationService.NavigateTo<InferenceViewModel>(); |
||||
EventManager.Instance.OnInferenceUpscaleRequested(image); |
||||
} |
||||
|
||||
private void GetOutputs(string directory) |
||||
{ |
||||
if (!settingsManager.IsLibraryDirSet) |
||||
return; |
||||
|
||||
if (!Directory.Exists(directory) && SelectedOutputType != SharedOutputType.All) |
||||
{ |
||||
Directory.CreateDirectory(directory); |
||||
return; |
||||
} |
||||
|
||||
var list = Directory |
||||
.EnumerateFiles(directory, "*.png", SearchOption.AllDirectories) |
||||
.Select(file => LocalImageFile.FromPath(file)) |
||||
.OrderByDescending(f => f.CreatedAt); |
||||
|
||||
OutputsCache.EditDiff(list, (x, y) => x.AbsolutePath == y.AbsolutePath); |
||||
} |
||||
} |
@ -0,0 +1,145 @@
|
||||
<controls:UserControlBase |
||||
x:Class="StabilityMatrix.Avalonia.Views.OutputsPage" |
||||
xmlns="https://github.com/avaloniaui" |
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
||||
xmlns:avaloniaEdit="clr-namespace:AvaloniaEdit;assembly=AvaloniaEdit" |
||||
xmlns:controls="clr-namespace:StabilityMatrix.Avalonia.Controls" |
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" |
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" |
||||
xmlns:mocks="clr-namespace:StabilityMatrix.Avalonia.DesignData" |
||||
xmlns:ui="using:FluentAvalonia.UI.Controls" |
||||
xmlns:vm="clr-namespace:StabilityMatrix.Avalonia.ViewModels" |
||||
xmlns:lang="clr-namespace:StabilityMatrix.Avalonia.Languages" |
||||
xmlns:models1="clr-namespace:StabilityMatrix.Avalonia.Models" |
||||
xmlns:database="clr-namespace:StabilityMatrix.Core.Models.Database;assembly=StabilityMatrix.Core" |
||||
xmlns:fluentAvalonia="clr-namespace:FluentIcons.FluentAvalonia;assembly=FluentIcons.FluentAvalonia" |
||||
xmlns:models="clr-namespace:StabilityMatrix.Core.Models;assembly=StabilityMatrix.Core" |
||||
d:DataContext="{x:Static mocks:DesignData.OutputsPageViewModel}" |
||||
d:DesignHeight="450" |
||||
d:DesignWidth="700" |
||||
x:CompileBindings="True" |
||||
x:DataType="vm:OutputsPageViewModel" |
||||
mc:Ignorable="d"> |
||||
<Grid RowDefinitions="Auto, *" Margin="16"> |
||||
<StackPanel Grid.Row="0" Orientation="Horizontal" Margin="0,0,0,16" |
||||
HorizontalAlignment="Left"> |
||||
<Grid RowDefinitions="Auto, *"> |
||||
<TextBlock Text="Output Folder" Margin="4"/> |
||||
<ComboBox Grid.Row="1" ItemsSource="{Binding Categories}" |
||||
SelectedItem="{Binding SelectedCategory}" |
||||
MinWidth="150"> |
||||
<ComboBox.Styles> |
||||
<Style |
||||
Selector="ComboBox /template/ ContentControl#ContentPresenter > StackPanel > TextBlock:nth-child(2)"> |
||||
<Setter Property="IsVisible" Value="False" /> |
||||
</Style> |
||||
</ComboBox.Styles> |
||||
<ComboBox.ItemTemplate> |
||||
<DataTemplate DataType="{x:Type models1:PackageOutputCategory}"> |
||||
<StackPanel> |
||||
<TextBlock |
||||
Margin="0,4,0,4" |
||||
Text="{Binding Name, Mode=OneWay}" /> |
||||
<TextBlock Text="{Binding Path, Mode=OneWay}" /> |
||||
</StackPanel> |
||||
</DataTemplate> |
||||
</ComboBox.ItemTemplate> |
||||
</ComboBox> |
||||
</Grid> |
||||
<Grid RowDefinitions="Auto, *" Margin="8,0" |
||||
IsVisible="{Binding CanShowOutputTypes}"> |
||||
<TextBlock Text="Output Type" Margin="4"/> |
||||
<ComboBox Grid.Row="1" ItemsSource="{Binding OutputTypes}" |
||||
SelectedItem="{Binding SelectedOutputType}" |
||||
MinWidth="150" |
||||
VerticalAlignment="Stretch" |
||||
VerticalContentAlignment="Center"/> |
||||
</Grid> |
||||
</StackPanel> |
||||
|
||||
<ScrollViewer Grid.Row="1"> |
||||
<ItemsRepeater |
||||
ItemsSource="{Binding Outputs}" |
||||
VerticalAlignment="Top"> |
||||
<ItemsRepeater.Layout> |
||||
<UniformGridLayout MinColumnSpacing="16" MinRowSpacing="16" /> |
||||
</ItemsRepeater.Layout> |
||||
<ItemsRepeater.ItemTemplate> |
||||
<DataTemplate DataType="{x:Type database:LocalImageFile}"> |
||||
<Button |
||||
Margin="0" |
||||
Padding="4" |
||||
Command="{Binding $parent[ItemsRepeater].((vm:OutputsPageViewModel)DataContext).OnImageClick}" |
||||
CommandParameter="{Binding }"> |
||||
<controls:BetterAdvancedImage |
||||
Width="300" |
||||
Height="300" |
||||
Stretch="UniformToFill" |
||||
Source="{Binding AbsolutePath}" /> |
||||
|
||||
<Button.ContextFlyout> |
||||
<ui:FAMenuFlyout> |
||||
<ui:MenuFlyoutItem |
||||
HotKey="{x:Null}" |
||||
Text="{x:Static lang:Resources.Action_Delete}" |
||||
IconSource="Delete" |
||||
Command="{Binding $parent[ItemsRepeater].((vm:OutputsPageViewModel)DataContext).DeleteImage}" |
||||
CommandParameter="{Binding }" /> |
||||
<ui:MenuFlyoutItem |
||||
HotKey="{x:Null}" |
||||
Text="{x:Static lang:Resources.Action_Copy}" |
||||
IconSource="Copy" |
||||
Command="{Binding $parent[ItemsRepeater].((vm:OutputsPageViewModel)DataContext).CopyImage}" |
||||
CommandParameter="{Binding AbsolutePath}" /> |
||||
|
||||
<ui:MenuFlyoutItem |
||||
HotKey="{x:Null}" |
||||
Text="{x:Static lang:Resources.Action_OpenInExplorer}" |
||||
IconSource="Folder" |
||||
Command="{Binding $parent[ItemsRepeater].((vm:OutputsPageViewModel)DataContext).OpenImage}" |
||||
CommandParameter="{Binding AbsolutePath}" /> |
||||
|
||||
<ui:MenuFlyoutSeparator |
||||
IsVisible="{Binding GenerationParameters, Converter={x:Static ObjectConverters.IsNotNull}}" /> |
||||
|
||||
<ui:MenuFlyoutSubItem Text="Send to Inference" IconSource="Share" |
||||
IsVisible="{Binding GenerationParameters, Converter={x:Static ObjectConverters.IsNotNull}}"> |
||||
<ui:MenuFlyoutItem |
||||
HotKey="{x:Null}" |
||||
Text="Text to Image" |
||||
IconSource="FullScreenMaximize" |
||||
Command="{Binding $parent[ItemsRepeater].((vm:OutputsPageViewModel)DataContext).SendToTextToImage}" |
||||
CommandParameter="{Binding }" /> |
||||
<ui:MenuFlyoutItem |
||||
HotKey="{x:Null}" |
||||
Text="Image to Image" |
||||
IsEnabled="False" |
||||
IconSource="ImageCopy" |
||||
CommandParameter="{Binding }" /> |
||||
<ui:MenuFlyoutItem |
||||
Text="Inpainting" |
||||
IconSource="ImageEdit" |
||||
IsEnabled="False" |
||||
HotKey="{x:Null}" |
||||
CommandParameter="{Binding }" /> |
||||
<ui:MenuFlyoutItem |
||||
Text="Upscale" |
||||
HotKey="{x:Null}" |
||||
Command="{Binding $parent[ItemsRepeater].((vm:OutputsPageViewModel)DataContext).SendToUpscale}" |
||||
CommandParameter="{Binding }"> |
||||
<ui:MenuFlyoutItem.IconSource> |
||||
<fluentAvalonia:SymbolIconSource |
||||
FontSize="10" |
||||
Symbol="ResizeImage" /> |
||||
</ui:MenuFlyoutItem.IconSource> |
||||
</ui:MenuFlyoutItem> |
||||
</ui:MenuFlyoutSubItem> |
||||
</ui:FAMenuFlyout> |
||||
</Button.ContextFlyout> |
||||
</Button> |
||||
</DataTemplate> |
||||
</ItemsRepeater.ItemTemplate> |
||||
</ItemsRepeater> |
||||
</ScrollViewer> |
||||
</Grid> |
||||
</controls:UserControlBase> |
@ -0,0 +1,14 @@
|
||||
using Avalonia; |
||||
using Avalonia.Controls; |
||||
using Avalonia.Markup.Xaml; |
||||
using StabilityMatrix.Avalonia.Controls; |
||||
|
||||
namespace StabilityMatrix.Avalonia.Views; |
||||
|
||||
public partial class OutputsPage : UserControlBase |
||||
{ |
||||
public OutputsPage() |
||||
{ |
||||
InitializeComponent(); |
||||
} |
||||
} |
Loading…
Reference in new issue