From 5494868af219555606bb1015f35e07cb7061c123 Mon Sep 17 00:00:00 2001 From: JT Date: Mon, 18 Dec 2023 14:07:41 -0800 Subject: [PATCH] Show tooltip explaining why you cant import cuz your hard drive is full --- .../Dialogs/SelectModelVersionViewModel.cs | 51 ++++++++++++++----- .../Dialogs/SelectModelVersionDialog.axaml | 3 ++ StabilityMatrix.Core/Helper/SystemInfo.cs | 4 +- .../Models/Packages/BaseGitPackage.cs | 2 +- .../Services/SettingsManager.cs | 2 +- 5 files changed, 44 insertions(+), 18 deletions(-) diff --git a/StabilityMatrix.Avalonia/ViewModels/Dialogs/SelectModelVersionViewModel.cs b/StabilityMatrix.Avalonia/ViewModels/Dialogs/SelectModelVersionViewModel.cs index c3f10931..61221efa 100644 --- a/StabilityMatrix.Avalonia/ViewModels/Dialogs/SelectModelVersionViewModel.cs +++ b/StabilityMatrix.Avalonia/ViewModels/Dialogs/SelectModelVersionViewModel.cs @@ -1,5 +1,7 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Collections.ObjectModel; +using System.IO; using System.Linq; using Avalonia.Media.Imaging; using Avalonia.Threading; @@ -8,6 +10,8 @@ using FluentAvalonia.UI.Controls; using StabilityMatrix.Avalonia.Models; using StabilityMatrix.Avalonia.ViewModels.Base; using StabilityMatrix.Core.Attributes; +using StabilityMatrix.Core.Helper; +using StabilityMatrix.Core.Models.FileInterfaces; using StabilityMatrix.Core.Services; namespace StabilityMatrix.Avalonia.ViewModels.Dialogs; @@ -49,12 +53,12 @@ public partial class SelectModelVersionViewModel : ContentDialogViewModelBase [NotifyPropertyChangedFor(nameof(DisplayedPageNumber))] private int selectedImageIndex; + [ObservableProperty] + private string importTooltip = string.Empty; + public int DisplayedPageNumber => SelectedImageIndex + 1; - public SelectModelVersionViewModel( - ISettingsManager settingsManager, - IDownloadService downloadService - ) + public SelectModelVersionViewModel(ISettingsManager settingsManager, IDownloadService downloadService) { this.settingsManager = settingsManager; this.downloadService = downloadService; @@ -70,7 +74,9 @@ public partial class SelectModelVersionViewModel : ContentDialogViewModelBase { var nsfwEnabled = settingsManager.Settings.ModelBrowserNsfwEnabled; var allImages = value - ?.ModelVersion?.Images?.Where(img => nsfwEnabled || img.Nsfw == "None") + ?.ModelVersion + ?.Images + ?.Where(img => nsfwEnabled || img.Nsfw == "None") ?.Select(x => new ImageSource(x.Url)) .ToList(); @@ -84,18 +90,35 @@ public partial class SelectModelVersionViewModel : ContentDialogViewModelBase CanGoToNextImage = allImages.Count > 1; } - Dispatcher.UIThread.Post(() => - { - CanGoToPreviousImage = false; - SelectedFile = SelectedVersionViewModel?.CivitFileViewModels.FirstOrDefault(); - ImageUrls = new ObservableCollection(allImages); - SelectedImageIndex = 0; - }); + Dispatcher + .UIThread + .Post(() => + { + CanGoToPreviousImage = false; + SelectedFile = SelectedVersionViewModel?.CivitFileViewModels.FirstOrDefault(); + ImageUrls = new ObservableCollection(allImages); + SelectedImageIndex = 0; + }); } partial void OnSelectedFileChanged(CivitFileViewModel? value) { - IsImportEnabled = value?.CivitFile != null; + var canImport = true; + if (settingsManager.IsLibraryDirSet) + { + var fileSizeBytes = value?.CivitFile.SizeKb * 1024; + var freeSizeBytes = SystemInfo.GetDiskFreeSpaceBytes(settingsManager.ModelsDirectory); + canImport = fileSizeBytes < freeSizeBytes; + ImportTooltip = canImport + ? $"Free space after download: {Size.FormatBytes(Convert.ToUInt64(freeSizeBytes - fileSizeBytes))}" + : $"Not enough space on disk. Need {Size.FormatBytes(Convert.ToUInt64(fileSizeBytes))} but only have {Size.FormatBytes(Convert.ToUInt64(freeSizeBytes))}"; + } + else + { + ImportTooltip = "Please set the library directory in settings"; + } + + IsImportEnabled = value?.CivitFile != null && canImport; } public void Cancel() diff --git a/StabilityMatrix.Avalonia/Views/Dialogs/SelectModelVersionDialog.axaml b/StabilityMatrix.Avalonia/Views/Dialogs/SelectModelVersionDialog.axaml index 4fb5151d..632c8fd4 100644 --- a/StabilityMatrix.Avalonia/Views/Dialogs/SelectModelVersionDialog.axaml +++ b/StabilityMatrix.Avalonia/Views/Dialogs/SelectModelVersionDialog.axaml @@ -11,6 +11,7 @@ xmlns:models="clr-namespace:StabilityMatrix.Avalonia.Models" xmlns:avalonia="clr-namespace:Projektanker.Icons.Avalonia;assembly=Projektanker.Icons.Avalonia" xmlns:lang="clr-namespace:StabilityMatrix.Avalonia.Languages" + xmlns:markupExtensions="clr-namespace:StabilityMatrix.Avalonia.MarkupExtensions" d:DataContext="{x:Static designData:DesignData.SelectModelVersionViewModel}" x:Class="StabilityMatrix.Avalonia.Views.Dialogs.SelectModelVersionDialog"> @@ -190,6 +191,8 @@ Content="{x:Static lang:Resources.Action_Import}" Command="{Binding Import}" IsEnabled="{Binding IsImportEnabled}" + ToolTip.Tip="{Binding ImportTooltip}" + markupExtensions:ShowDisabledTooltipExtension.ShowOnDisabled="True" Classes="accent" /> diff --git a/StabilityMatrix.Core/Helper/SystemInfo.cs b/StabilityMatrix.Core/Helper/SystemInfo.cs index 3e8a7d1a..935a087b 100644 --- a/StabilityMatrix.Core/Helper/SystemInfo.cs +++ b/StabilityMatrix.Core/Helper/SystemInfo.cs @@ -5,8 +5,8 @@ namespace StabilityMatrix.Core.Helper; public static class SystemInfo { - public const long Gigabyte = 1024 * 1024 * 1024; - public const long Megabyte = 1024 * 1024; + public const long Gibibyte = 1024 * 1024 * 1024; + public const long Mebibyte = 1024 * 1024; [DllImport("UXTheme.dll", SetLastError = true, EntryPoint = "#138")] public static extern bool ShouldUseDarkMode(); diff --git a/StabilityMatrix.Core/Models/Packages/BaseGitPackage.cs b/StabilityMatrix.Core/Models/Packages/BaseGitPackage.cs index 89293dea..bd09057c 100644 --- a/StabilityMatrix.Core/Models/Packages/BaseGitPackage.cs +++ b/StabilityMatrix.Core/Models/Packages/BaseGitPackage.cs @@ -172,7 +172,7 @@ public abstract class BaseGitPackage : BasePackage IProgress? progress = null ) { - const long fiveGigs = 5 * SystemInfo.Gigabyte; + const long fiveGigs = 5 * SystemInfo.Gibibyte; if (SystemInfo.GetDiskFreeSpaceBytes(installLocation) is < fiveGigs) { throw new ApplicationException( diff --git a/StabilityMatrix.Core/Services/SettingsManager.cs b/StabilityMatrix.Core/Services/SettingsManager.cs index 1ff8ba51..d36314cc 100644 --- a/StabilityMatrix.Core/Services/SettingsManager.cs +++ b/StabilityMatrix.Core/Services/SettingsManager.cs @@ -650,7 +650,7 @@ public class SettingsManager : ISettingsManager if (!isLoaded) return; - if (SystemInfo.GetDiskFreeSpaceBytes(SettingsPath) is < 1 * SystemInfo.Megabyte) + if (SystemInfo.GetDiskFreeSpaceBytes(SettingsPath) is < 1 * SystemInfo.Mebibyte) { Logger.Warn("Not enough disk space to save settings"); return;