Skip to content

Commit

Permalink
Settings window + ScriptConfigs add/remove
Browse files Browse the repository at this point in the history
  • Loading branch information
gosukretess authored and cezarypiatek committed Jun 16, 2022
1 parent 5b23d7b commit 23e4002
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 17 deletions.
16 changes: 9 additions & 7 deletions src/ScriptRunner/ScriptRunner.GUI/ScriptRunner.GUI.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0</TargetFramework>
Expand All @@ -8,8 +8,7 @@
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
</PropertyGroup>
<ItemGroup>
<Folder Include="Models\" />
<AvaloniaResource Include="Assets\**" />
<AvaloniaResource Include="Assets\**" />
<Folder Include="ScriptReader\" />
<None Remove=".gitignore" />
<None Remove="Themes\DarkTheme.xaml" />
Expand All @@ -27,11 +26,14 @@
<TrimmableAssembly Include="Avalonia.Themes.Default" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Avalonia" Version="0.10.13" />
<PackageReference Include="Avalonia.Desktop" Version="0.10.13" />
<PackageReference Include="Avalonia" Version="0.10.14" />
<PackageReference Include="Avalonia.Desktop" Version="0.10.14" />
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="0.10.13" />
<PackageReference Include="Avalonia.ReactiveUI" Version="0.10.13" />
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="0.10.14" />
<PackageReference Include="Avalonia.ReactiveUI" Version="0.10.14" />
<PackageReference Include="Avalonia.Xaml.Behaviors" Version="0.10.14" />
<PackageReference Include="Avalonia.Xaml.Interactions" Version="0.10.14" />
<PackageReference Include="Avalonia.Xaml.Interactivity" Version="0.10.14" />
<PackageReference Include="CliWrap" Version="3.4.4" />
<PackageReference Include="Projektanker.Icons.Avalonia" Version="4.4.0" />
<PackageReference Include="Projektanker.Icons.Avalonia.FontAwesome" Version="4.4.0" />
Expand Down
17 changes: 16 additions & 1 deletion src/ScriptRunner/ScriptRunner.GUI/Settings/AppSettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -63,6 +64,20 @@ public static void UpdateLayoutSettings(Action<LayoutSettings> 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<string> configScripts)
{
var allSettings = Load();
allSettings.ConfigScripts = configScripts.ToList();
Save(allSettings);
}

private static string GetSettingsPath()
{
var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "ScriptRunner");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ public class ScriptRunnerAppSettings
{
public LayoutSettings? Layout { get; set; }
public Dictionary<string, CommandInstallationStatus> InstalledActions { get; set; }

public List<string> ConfigScripts { get; set; }

}

public class CommandInstallationStatus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -151,6 +153,12 @@ public void InstallScript()
}
}

public void OpenSettingsWindow()
{
var window = new SettingsWindow();
window.Show();
}

public bool SelectedActionInstalled
{
get => _selectedActionInstalled;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Contains records of configuration scripts loaded from settings file
/// </summary>
private ObservableCollection<ConfigScriptFileRow> _configScriptFiles;

public ObservableCollection<ConfigScriptFileRow> ConfigScriptFiles
{
get => _configScriptFiles;
private set => this.RaiseAndSetIfChanged(ref _configScriptFiles, value);
}

public SettingsWindowViewModel()
{
var configScripts = AppSettingsService.Load().ConfigScripts;
ConfigScriptFiles =
new ObservableCollection<ConfigScriptFileRow>(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();
}
}
20 changes: 18 additions & 2 deletions src/ScriptRunner/ScriptRunner.GUI/Views/FilePicker.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using System.IO;
using System.Linq;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Avalonia.VisualTree;

namespace ScriptRunner.GUI.Views
{
Expand All @@ -21,7 +23,8 @@ private void InitializeComponent()
}

public static readonly DirectProperty<FilePicker, string> FilePathProperty = AvaloniaProperty.RegisterDirect<FilePicker, string>(nameof(FilePath), picker => picker.FindControl<TextBox>("FilePathTextBox").Text, (picker, s) => picker.FindControl<TextBox>("FilePathTextBox").Text = s);


public event EventHandler? OnFilePicked;

public string FilePath
{
Expand All @@ -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)
{
Expand All @@ -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;
}
}
}
11 changes: 5 additions & 6 deletions src/ScriptRunner/ScriptRunner.GUI/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<RowDefinition Height="300" ></RowDefinition>
</Grid.RowDefinitions>

<DockPanel Grid.Column="0" Classes="actionsMenu" >
<DockPanel Grid.Column="0" Classes="actionsMenu" LastChildFill="True">
<!-- List of available actions, selectable -->
<StackPanel DockPanel.Dock="Top" >
<ListBox Items="{Binding Actions}" SelectedItem="{Binding SelectedAction, Mode=TwoWay}">
Expand All @@ -39,11 +39,10 @@
</ListBox>
</StackPanel>

<!-- Settings button, for future use :D -->
<!--<StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal">
<Button i:Attached.Icon="fas fa-cog">
</Button>
</StackPanel>-->
<StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" VerticalAlignment="Bottom">
<Button i:Attached.Icon="fas fa-cog" Height="35" Width="50" FontSize="18"
Command="{Binding OpenSettingsWindow}" />
</StackPanel>
</DockPanel>

<GridSplitter Grid.Column="1" Grid.Row="0" ResizeDirection="Columns"></GridSplitter>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Avalonia.Controls;
using Avalonia.Layout;
using ScriptRunner.GUI.Settings;
using static System.Double;

namespace ScriptRunner.GUI.Views;
Expand Down
50 changes: 50 additions & 0 deletions src/ScriptRunner/ScriptRunner.GUI/Views/SettingsWindow.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:ScriptRunner.GUI.ViewModels"
xmlns:views="clr-namespace:ScriptRunner.GUI.Views"
xmlns:avalonia="clr-namespace:Projektanker.Icons.Avalonia;assembly=Projektanker.Icons.Avalonia"
mc:Ignorable="d" d:DesignWidth="600" d:DesignHeight="450"
Width="600" Height="450"
x:Class="ScriptRunner.GUI.Views.SettingsWindow"
Title="Settings"
Icon="/Assets/avalonia-logo.ico"
FontFamily="Segoe UI Variable">
<Design.DataContext>
<vm:SettingsWindowViewModel />
</Design.DataContext>

<StackPanel Orientation="Vertical" Margin="5">
<TextBlock Classes="h2" Text="Config sources:" Margin="0, 5"/>
<ItemsControl Items="{Binding ConfigScriptFiles}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<views:FilePicker Grid.Column="0" Name="FilePicker" FilePath="{Binding Path}">
<Interaction.Behaviors>
<EventTriggerBehavior EventName="OnFilePicked" SourceObject="{Binding #FilePicker}">
<InvokeCommandAction Command="{Binding OnFilePicked}" PassEventArgsToCommand="True"/>
</EventTriggerBehavior>
</Interaction.Behaviors>
</views:FilePicker>
<Button Grid.Column="1" avalonia:Attached.Icon="fa fa-trash" FontSize="18" Margin="10,0"
Command="{Binding DataContext.RemoveConfigScript, RelativeSource={RelativeSource AncestorType={x:Type views:SettingsWindow}}}"
CommandParameter="{Binding}"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Button Command="{Binding AddNewConfigScriptRow}">
<StackPanel Orientation="Horizontal">
<avalonia:Icon Value="fa fa-plus" Margin="4,0"/>
<TextBlock>Add new config script</TextBlock>
</StackPanel>
</Button>
</StackPanel>

</Window>
24 changes: 24 additions & 0 deletions src/ScriptRunner/ScriptRunner.GUI/Views/SettingsWindow.axaml.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit 23e4002

Please sign in to comment.