From 537d0ffad3a8b11cc946323ad05b8e94d5e5cd4a Mon Sep 17 00:00:00 2001 From: Ionite Date: Thu, 17 Aug 2023 17:26:40 -0400 Subject: [PATCH] Settings config for tag csv source --- .../Controls/CodeCompletion/CompletionList.cs | 6 ++ .../CodeCompletion/CompletionListThemes.axaml | 22 +++--- .../ViewModels/SettingsViewModel.cs | 79 ++++++++++++++++++- .../Views/SettingsPage.axaml | 51 ++++++++++-- .../Models/Settings/InferenceSettings.cs | 6 -- .../Models/Settings/Settings.cs | 8 +- 6 files changed, 145 insertions(+), 27 deletions(-) delete mode 100644 StabilityMatrix.Core/Models/Settings/InferenceSettings.cs diff --git a/StabilityMatrix.Avalonia/Controls/CodeCompletion/CompletionList.cs b/StabilityMatrix.Avalonia/Controls/CodeCompletion/CompletionList.cs index c14302dc..1f798719 100644 --- a/StabilityMatrix.Avalonia/Controls/CodeCompletion/CompletionList.cs +++ b/StabilityMatrix.Avalonia/Controls/CodeCompletion/CompletionList.cs @@ -263,9 +263,14 @@ public class CompletionList : TemplatedControl public void SelectItem(string text) { if (text == _currentText) + { return; + } + if (_listBox == null) + { ApplyTemplate(); + } if (IsFiltering) { @@ -275,6 +280,7 @@ public class CompletionList : TemplatedControl { SelectItemWithStart(text); } + _currentText = text; } diff --git a/StabilityMatrix.Avalonia/Controls/CodeCompletion/CompletionListThemes.axaml b/StabilityMatrix.Avalonia/Controls/CodeCompletion/CompletionListThemes.axaml index bf12be87..d91c9aa3 100644 --- a/StabilityMatrix.Avalonia/Controls/CodeCompletion/CompletionListThemes.axaml +++ b/StabilityMatrix.Avalonia/Controls/CodeCompletion/CompletionListThemes.axaml @@ -16,6 +16,10 @@ + + - - - + availableTagCompletionCsvs = Array.Empty(); + + [ObservableProperty] + private string? selectedTagCompletionCsv; + // Integrations section [ObservableProperty] private bool isDiscordRichPresenceEnabled; @@ -136,12 +145,26 @@ public partial class SettingsViewModel : PageViewModelBase vm => vm.IsDiscordRichPresenceEnabled, settings => settings.IsDiscordRichPresenceEnabled); - DebugThrowAsyncExceptionCommand.WithNotificationErrorHandler(notificationService, LogLevel.Warn); settingsManager.RelayPropertyFor(this, vm => vm.SelectedAnimationScale, settings => settings.AnimationScale); + + settingsManager.RelayPropertyFor(this, + vm => vm.SelectedTagCompletionCsv, + settings => settings.TagCompletionCsv); + + DebugThrowAsyncExceptionCommand.WithNotificationErrorHandler(notificationService, LogLevel.Warn); + ImportTagCsvCommand.WithNotificationErrorHandler(notificationService, LogLevel.Warn); } - + + /// + public override void OnLoaded() + { + base.OnLoaded(); + + UpdateAvailableTagCompletionCsvs(); + } + partial void OnSelectedThemeChanged(string? value) { // In case design / tests @@ -237,6 +260,58 @@ public partial class SettingsViewModel : PageViewModelBase #endregion + #region Inference UI + + private void UpdateAvailableTagCompletionCsvs() + { + var tagsDir = settingsManager.TagsDirectory; + if (!tagsDir.Exists) return; + + var csvFiles = tagsDir.Info.EnumerateFiles("*.csv"); + AvailableTagCompletionCsvs = csvFiles.Select(f => f.Name).ToImmutableArray(); + + // Set selected to current if exists + var settingsCsv = settingsManager.Settings.TagCompletionCsv; + if (settingsCsv is not null && AvailableTagCompletionCsvs.Contains(settingsCsv)) + { + SelectedTagCompletionCsv = settingsCsv; + } + } + + [RelayCommand(FlowExceptionsToTaskScheduler = true)] + private async Task ImportTagCsv() + { + var storage = App.StorageProvider; + var files = await storage.OpenFilePickerAsync(new FilePickerOpenOptions + { + FileTypeFilter = new List + { + new("CSV") + { + Patterns = new[] {"*.csv"}, + } + } + }); + + if (files.Count == 0) return; + + var sourceFile = new FilePath(files[0].TryGetLocalPath()!); + + var tagsDir = settingsManager.TagsDirectory; + tagsDir.Create(); + + // Copy to tags directory + await sourceFile.CopyToAsync(tagsDir.JoinFile(sourceFile.Name)); + + // Update index + UpdateAvailableTagCompletionCsvs(); + + notificationService.Show($"Imported {sourceFile.Name}", + $"The {sourceFile.Name} file has been imported.", NotificationType.Success); + } + + #endregion + #region System /// diff --git a/StabilityMatrix.Avalonia/Views/SettingsPage.axaml b/StabilityMatrix.Avalonia/Views/SettingsPage.axaml index 5527bf20..9cf268df 100644 --- a/StabilityMatrix.Avalonia/Views/SettingsPage.axaml +++ b/StabilityMatrix.Avalonia/Views/SettingsPage.axaml @@ -6,6 +6,7 @@ xmlns:ui="using:FluentAvalonia.UI.Controls" xmlns:mocks="clr-namespace:StabilityMatrix.Avalonia.DesignData" xmlns:controls="clr-namespace:StabilityMatrix.Avalonia.Controls" + xmlns:icons="clr-namespace:Projektanker.Icons.Avalonia;assembly=Projektanker.Icons.Avalonia" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="700" x:DataType="vm:SettingsViewModel" x:CompileBindings="True" @@ -14,7 +15,7 @@ - @@ -104,8 +105,42 @@ - + + + + + + + + + + + +