From 23e4002f526445a93c70cdfaec6cfcf3aeb16aeb Mon Sep 17 00:00:00 2001 From: Kapu Date: Tue, 14 Jun 2022 23:21:48 +0200 Subject: [PATCH] Settings window + ScriptConfigs add/remove --- .../ScriptRunner.GUI/ScriptRunner.GUI.csproj | 16 +++-- .../Settings/AppSettingsService.cs | 17 ++++- .../Settings/ScriptRunnerAppSettings.cs | 3 +- .../ViewModels/MainWindowViewModel.cs | 8 +++ .../ViewModels/SettingsWindowViewModel.cs | 67 +++++++++++++++++++ .../Views/FilePicker.axaml.cs | 20 +++++- .../ScriptRunner.GUI/Views/MainWindow.axaml | 11 ++- .../Views/MainWindow.axaml.cs | 1 + .../Views/SettingsWindow.axaml | 50 ++++++++++++++ .../Views/SettingsWindow.axaml.cs | 24 +++++++ 10 files changed, 200 insertions(+), 17 deletions(-) create mode 100644 src/ScriptRunner/ScriptRunner.GUI/ViewModels/SettingsWindowViewModel.cs create mode 100644 src/ScriptRunner/ScriptRunner.GUI/Views/SettingsWindow.axaml create mode 100644 src/ScriptRunner/ScriptRunner.GUI/Views/SettingsWindow.axaml.cs diff --git a/src/ScriptRunner/ScriptRunner.GUI/ScriptRunner.GUI.csproj b/src/ScriptRunner/ScriptRunner.GUI/ScriptRunner.GUI.csproj index b620840..258d77b 100644 --- a/src/ScriptRunner/ScriptRunner.GUI/ScriptRunner.GUI.csproj +++ b/src/ScriptRunner/ScriptRunner.GUI/ScriptRunner.GUI.csproj @@ -1,4 +1,4 @@ - + WinExe net6.0 @@ -8,8 +8,7 @@ true - - + @@ -27,11 +26,14 @@ - - + + - - + + + + + diff --git a/src/ScriptRunner/ScriptRunner.GUI/Settings/AppSettingsService.cs b/src/ScriptRunner/ScriptRunner.GUI/Settings/AppSettingsService.cs index 42a377c..9460513 100644 --- a/src/ScriptRunner/ScriptRunner.GUI/Settings/AppSettingsService.cs +++ b/src/ScriptRunner/ScriptRunner.GUI/Settings/AppSettingsService.cs @@ -2,10 +2,11 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; +using System.Linq; using System.Text; using System.Text.Json; -namespace ScriptRunner.GUI; +namespace ScriptRunner.GUI.Settings; public class AppSettingsService { @@ -63,6 +64,20 @@ public static void UpdateLayoutSettings(Action updateSettings) Debug.WriteLine($"Width: {allSettings.Layout.Width}, Height: {allSettings.Layout.Height}, L: {allSettings.Layout.ActionsPanelWidth}, M: {allSettings.Layout.RunningJobsPanelHeight}"); } + public static void RemoveScriptConfig(string scriptPath) + { + var allSettings = Load(); + allSettings.ConfigScripts.Remove(scriptPath); + Save(allSettings); + } + + public static void UpdateScriptConfigs(IEnumerable configScripts) + { + var allSettings = Load(); + allSettings.ConfigScripts = configScripts.ToList(); + Save(allSettings); + } + private static string GetSettingsPath() { var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "ScriptRunner"); diff --git a/src/ScriptRunner/ScriptRunner.GUI/Settings/ScriptRunnerAppSettings.cs b/src/ScriptRunner/ScriptRunner.GUI/Settings/ScriptRunnerAppSettings.cs index 3118dae..3e2ecb5 100644 --- a/src/ScriptRunner/ScriptRunner.GUI/Settings/ScriptRunnerAppSettings.cs +++ b/src/ScriptRunner/ScriptRunner.GUI/Settings/ScriptRunnerAppSettings.cs @@ -6,7 +6,8 @@ public class ScriptRunnerAppSettings { public LayoutSettings? Layout { get; set; } public Dictionary InstalledActions { get; set; } - + public List ConfigScripts { get; set; } + } public class CommandInstallationStatus diff --git a/src/ScriptRunner/ScriptRunner.GUI/ViewModels/MainWindowViewModel.cs b/src/ScriptRunner/ScriptRunner.GUI/ViewModels/MainWindowViewModel.cs index d5e9718..57207df 100644 --- a/src/ScriptRunner/ScriptRunner.GUI/ViewModels/MainWindowViewModel.cs +++ b/src/ScriptRunner/ScriptRunner.GUI/ViewModels/MainWindowViewModel.cs @@ -5,6 +5,8 @@ using ReactiveUI; using ScriptRunner.GUI.ScriptConfigs; using ScriptRunner.GUI.ScriptReader; +using ScriptRunner.GUI.Settings; +using ScriptRunner.GUI.Views; namespace ScriptRunner.GUI.ViewModels; @@ -151,6 +153,12 @@ public void InstallScript() } } + public void OpenSettingsWindow() + { + var window = new SettingsWindow(); + window.Show(); + } + public bool SelectedActionInstalled { get => _selectedActionInstalled; diff --git a/src/ScriptRunner/ScriptRunner.GUI/ViewModels/SettingsWindowViewModel.cs b/src/ScriptRunner/ScriptRunner.GUI/ViewModels/SettingsWindowViewModel.cs new file mode 100644 index 0000000..b20c547 --- /dev/null +++ b/src/ScriptRunner/ScriptRunner.GUI/ViewModels/SettingsWindowViewModel.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.ObjectModel; +using System.Linq; +using ReactiveUI; +using ScriptRunner.GUI.Settings; +using ScriptRunner.GUI.Views; + +namespace ScriptRunner.GUI.ViewModels; + +public class SettingsWindowViewModel : ViewModelBase +{ + /// + /// Contains records of configuration scripts loaded from settings file + /// + private ObservableCollection _configScriptFiles; + + public ObservableCollection ConfigScriptFiles + { + get => _configScriptFiles; + private set => this.RaiseAndSetIfChanged(ref _configScriptFiles, value); + } + + public SettingsWindowViewModel() + { + var configScripts = AppSettingsService.Load().ConfigScripts; + ConfigScriptFiles = + new ObservableCollection(configScripts.Select(path => new ConfigScriptFileRow(path, SaveConfigScripts))); + } + + public void AddNewConfigScriptRow() + { + ConfigScriptFiles.Add(new ConfigScriptFileRow(string.Empty, SaveConfigScripts)); + } + + public void RemoveConfigScript(ConfigScriptFileRow configScriptFileRow) + { + AppSettingsService.RemoveScriptConfig(configScriptFileRow.Path); + ConfigScriptFiles.Remove(configScriptFileRow); + } + + private void SaveConfigScripts() + { + AppSettingsService.UpdateScriptConfigs(ConfigScriptFiles.Select(q => q.Path)); + } +} + +public class ConfigScriptFileRow +{ + public string Path { get; set; } + + // TODO: Check if file path is correct and mark this flag + public bool Exists { get; set; } + + public Action UpdateConfigScriptEntry; + + public ConfigScriptFileRow(string path, Action updateConfigScriptEntry) + { + Path = path; + UpdateConfigScriptEntry = updateConfigScriptEntry; + } + + private void OnFilePicked(FilePickedArgs args) + { + Path = args.Path; + UpdateConfigScriptEntry.Invoke(); + } +} \ No newline at end of file diff --git a/src/ScriptRunner/ScriptRunner.GUI/Views/FilePicker.axaml.cs b/src/ScriptRunner/ScriptRunner.GUI/Views/FilePicker.axaml.cs index f1c2ed9..0610fbe 100644 --- a/src/ScriptRunner/ScriptRunner.GUI/Views/FilePicker.axaml.cs +++ b/src/ScriptRunner/ScriptRunner.GUI/Views/FilePicker.axaml.cs @@ -1,3 +1,4 @@ +using System; using System.IO; using System.Linq; using Avalonia; @@ -5,6 +6,7 @@ using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Interactivity; using Avalonia.Markup.Xaml; +using Avalonia.VisualTree; namespace ScriptRunner.GUI.Views { @@ -21,7 +23,8 @@ private void InitializeComponent() } public static readonly DirectProperty FilePathProperty = AvaloniaProperty.RegisterDirect(nameof(FilePath), picker => picker.FindControl("FilePathTextBox").Text, (picker, s) => picker.FindControl("FilePathTextBox").Text = s); - + + public event EventHandler? OnFilePicked; public string FilePath { @@ -33,6 +36,8 @@ private void ChangeFileClick(object? sender, RoutedEventArgs e) { if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) { + var sourceWindow = (sender as IControl)?.GetVisualRoot() as Window ?? desktop.MainWindow; + var dialog = new OpenFileDialog(); if (string.IsNullOrWhiteSpace(FilePath) == false && Path.GetDirectoryName(FilePath) is { } dir) { @@ -43,12 +48,23 @@ private void ChangeFileClick(object? sender, RoutedEventArgs e) dialog.AllowMultiple = false; - var result = dialog.ShowAsync(desktop.MainWindow).GetAwaiter().GetResult(); + var result = dialog.ShowAsync(sourceWindow).GetAwaiter().GetResult(); if (result?.FirstOrDefault() is { } file) { FilePath = file; + OnFilePicked?.Invoke(this, new FilePickedArgs(file)); } } } } + + public class FilePickedArgs : EventArgs + { + public string Path { get; } + + public FilePickedArgs(string path) + { + Path = path; + } + } } diff --git a/src/ScriptRunner/ScriptRunner.GUI/Views/MainWindow.axaml b/src/ScriptRunner/ScriptRunner.GUI/Views/MainWindow.axaml index 3b4625a..847c951 100644 --- a/src/ScriptRunner/ScriptRunner.GUI/Views/MainWindow.axaml +++ b/src/ScriptRunner/ScriptRunner.GUI/Views/MainWindow.axaml @@ -27,7 +27,7 @@ - + @@ -39,11 +39,10 @@ - - + + + + + diff --git a/src/ScriptRunner/ScriptRunner.GUI/Views/SettingsWindow.axaml.cs b/src/ScriptRunner/ScriptRunner.GUI/Views/SettingsWindow.axaml.cs new file mode 100644 index 0000000..6601ea0 --- /dev/null +++ b/src/ScriptRunner/ScriptRunner.GUI/Views/SettingsWindow.axaml.cs @@ -0,0 +1,24 @@ +using Avalonia; +using Avalonia.Controls; +using Avalonia.Markup.Xaml; +using ScriptRunner.GUI.ViewModels; + +namespace ScriptRunner.GUI.Views +{ + public partial class SettingsWindow : Window + { + public SettingsWindow() + { + InitializeComponent(); + DataContext = new SettingsWindowViewModel(); +#if DEBUG + this.AttachDevTools(); +#endif + } + + private void InitializeComponent() + { + AvaloniaXamlLoader.Load(this); + } + } +}