Skip to content
This repository has been archived by the owner on Apr 24, 2022. It is now read-only.

Settings #5

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions avifencodergui.lib/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public static Config Load()
return null;
}

public static void Save(Config config)
public static void Save(Config config, string name)
{
if (!File.Exists(Constants.AppFolder)) Directory.CreateDirectory(Constants.AppFolder);

Expand All @@ -117,9 +117,11 @@ public static void Save(Config config)
WriteIndented = true
};
var jsonString = JsonSerializer.Serialize(config, jsonConfig);
File.WriteAllText(Constants.ConfigPath, jsonString);
var file = Path.Combine(Constants.AppFolder, $"{name}.config.json");
File.WriteAllText(file, jsonString);
}


public string CreateProgArgs(string input, string output)
{
return null;
Expand Down
9 changes: 8 additions & 1 deletion avifencodergui.wpf/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;
using avifencodergui.lib;
using avifencodergui.wpf.Messenger;
using Microsoft.Toolkit.Mvvm.ComponentModel;
Expand Down Expand Up @@ -37,6 +40,9 @@ public MainViewModel()
new RelayCommand(() => this.Messenger.Send(new WindowMessage(WindowEnum.SettingsWindows)));
this.OpenEncoderInstallWikiCommand = new RelayCommand(() => this.OpenUrl("https://github.com/dhcgn/avif_encoder_gui/wiki/Install-AVIF-Encoder-and-AVIF-Decoder"));

this.Configs = new List<string>() {"built in"};
this.SelectedConfig = Configs.First();

if (InDesignMode())
{
this.Jobs.Add(Job.GetDesignDate(Job.JobStateEnum.Pending));
Expand Down Expand Up @@ -64,7 +70,8 @@ public string AvifDecVersion

public RelayCommand ShowSettingsCommand { get; set; }
public RelayCommand OpenEncoderInstallWikiCommand { get; set; }

public List<string> Configs { get; private set; }
public string SelectedConfig { get; set; }
public IAsyncRelayCommand OnLoadCommand { get; }

public bool CanEncode
Expand Down
77 changes: 68 additions & 9 deletions avifencodergui.wpf/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,88 @@
using System.Text.Json;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Windows;
using avifencodergui.lib;
using Microsoft.Toolkit.Mvvm.ComponentModel;
using Microsoft.Toolkit.Mvvm.Input;

namespace avifencodergui.wpf.ViewModels
{
public class SettingsViewModel : ObservableRecipient
{
private JsonSerializerOptions jsonConfig = new()
{
WriteIndented = true
};
private ConfigViewModel selectedConfig;

public SettingsViewModel()
{
this.SaveCommand = new RelayCommand(()=>{}, ()=>false);
this.CancelCommand = new RelayCommand(()=>{}, ()=>false);
this.SaveCommand = new RelayCommand(() => { }, () => false);
this.CancelCommand = new RelayCommand(() => { }, () => false);

var config = avifencodergui.lib.Config.CreateEmpty();
var jsonConfig = new JsonSerializerOptions
if (InDesignMode())
{
WriteIndented = true
};
Configs = new List<ConfigViewModel>()
{
new ConfigViewModel() { Name = "sample"}
};
SelectedConfig = Configs.FirstOrDefault();
return;
}

var config = avifencodergui.lib.Config.CreateEmpty();

var jsonString = JsonSerializer.Serialize(config, jsonConfig);

this.Config = jsonString;
var configFiles = Directory.GetFiles(Constants.AppFolder, "*.config.json");
if (configFiles.Length == 0)
{
var emptyConfig = avifencodergui.lib.Config.CreateEmpty();
Config.Save(emptyConfig, "empty");

var sample1Config = avifencodergui.lib.Config.CreateSample1();
Config.Save(sample1Config, "sample1");
}

configFiles = Directory.GetFiles(Constants.AppFolder, "*.config.json");

Configs = new List<ConfigViewModel>();
foreach (var file in configFiles)
{
var c = new ConfigViewModel
{
Name = new FileInfo(file).Name.Replace(".config.json", ""),
Path = file
};
Configs.Add(c);
}


SelectedConfig = Configs.FirstOrDefault();
this.ConfigJsonText = jsonString;
}

public RelayCommand OnLoadCommand { get; set; }
public RelayCommand SaveCommand { get; set; }
public RelayCommand CancelCommand { get; set; }
public string Config { get; set; }
public string ConfigJsonText { get; set; }

public ConfigViewModel SelectedConfig { get => selectedConfig; set => base.SetProperty(ref selectedConfig, value); }
public List<ConfigViewModel> Configs { get; set; }

public static bool InDesignMode()
{
return !(Application.Current is App);
}
}



public class ConfigViewModel
{
public string Name { get; set; }
public string Path { get; set; }
}
}
21 changes: 18 additions & 3 deletions avifencodergui.wpf/Windows/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,24 @@
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:converter="clr-namespace:avifencodergui.wpf.Converter"
mc:Ignorable="d"
Title="AVIF Encoder" Height="369" Width="600">
Title="AVIF Encoder"
Height="369" Width="600"
MinHeight="369" MinWidth="600"
>

<Window.Resources>
<converter:VisiblityStringAnyConverter x:Key="VisibilityStringAnyConverter" />
<converter:JobStatusBrushConverter x:Key="JobStatusBrushConverter" />
<converter:VisiblityInverterConverter x:Key="VisibilityInverterConverter" />
<Style x:Key="FooterStyle" TargetType="FrameworkElement">
<Setter Property="FrameworkElement.Margin" Value="2" />
<Setter Property="Control.Padding" Value="5,2" />
<Setter Property="Control.FontSize" Value="10" />
<Setter Property="FrameworkElement.HorizontalAlignment" Value="Center" />
<Setter Property="FrameworkElement.VerticalAlignment" Value="Center" />
<Setter Property="Control.HorizontalContentAlignment" Value="Center" />
<Setter Property="Control.VerticalContentAlignment" Value="Center" />
</Style>
</Window.Resources>

<i:Interaction.Triggers>
Expand Down Expand Up @@ -96,9 +108,12 @@
</Border>
<Border Width="5" />

<Button Content="Download AVIF" Margin="2" FontSize="10" Padding="5,0"
<Button Content="Download AVIF" Style="{StaticResource FooterStyle}"
Command="{Binding OpenEncoderInstallWikiCommand}" />
<Button Content="Settings" Margin="2" FontSize="10" Padding="5,0" Command="{Binding ShowSettingsCommand}" />
<Button Content="Settings" Style="{StaticResource FooterStyle}" Command="{Binding ShowSettingsCommand}" />
<TextBlock Text="Selected:" Style="{StaticResource FooterStyle}" />
<ComboBox ItemsSource="{Binding Configs}" Style="{StaticResource FooterStyle}"
SelectedItem="{Binding SelectedConfig}" />
</StackPanel>
</Grid>
</Window>
10 changes: 7 additions & 3 deletions avifencodergui.wpf/Windows/SettingsWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@
</Grid.RowDefinitions>

<TextBox Padding="5" FontFamily="Consolas" FontSize="10" AcceptsReturn="True" AcceptsTab="True"
ScrollViewer.VerticalScrollBarVisibility="Visible" Text="{Binding Config}" />
ScrollViewer.VerticalScrollBarVisibility="Visible" Text="{Binding ConfigJsonText}" />
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right">
<Button Content="Save" Margin="10" Padding="5" Command="{Binding SaveCommand}" />
<Button Content="Cancel" Margin="10" Padding="5" Command="{Binding CancelCommand}" />
<TextBlock Text="Selected Settings"></TextBlock>
<ComboBox ItemsSource="{Binding Configs}" SelectedValue="{Binding SelectedConfig}" DisplayMemberPath="Name"></ComboBox>
<Button Content="Refesh" Margin="10" Padding="10,5" Command="{Binding SaveCommand}" />
<Button Content="Open folder" Margin="10" Padding="10,5" Command="{Binding SaveCommand}" />
<Button Content="Save" Margin="10" Padding="10,5" Command="{Binding SaveCommand}" />
<Button Content="Cancel" Margin="10" Padding="10,5" Command="{Binding CancelCommand}" />
</StackPanel>
</Grid>
</Window>