Skip to content

Commit b6f23da

Browse files
committed
Feature: Custom options dialog can now load from predefined presets.
1 parent b88dba9 commit b6f23da

File tree

11 files changed

+224
-14
lines changed

11 files changed

+224
-14
lines changed

ShaderShrinker/InnoSetupProject/InstallScript.iss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{
2121

2222
[Files]
2323
Source: "bin\Release\net5.0-windows\publish\win-x64\*.*"; DestDir: "{app}"; Excludes: "*.pdb"; Flags: ignoreversion
24+
Source: "bin\Release\net5.0-windows\publish\win-x64\Presets\*.*"; DestDir: "{app}\Presets"; Flags: ignoreversion
2425

2526
[Icons]
2627
Name: "{group}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"

ShaderShrinker/Shrinker.Parser/Optimizations/InlineConstantVariablesExtension.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
// </summary>
1010
// -----------------------------------------------------------------------
1111

12-
using System.Collections.Generic;
1312
using System.Linq;
1413
using Shrinker.Lexer;
1514
using Shrinker.Parser.SyntaxNodes;

ShaderShrinker/Shrinker.WpfApp/AppViewModel.cs

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public class AppViewModel : ViewModelBase
4848
private bool m_showProgress;
4949
private string m_originalCode;
5050
private int m_originalSize;
51+
private FileInfo m_selectedPreset;
52+
private CustomOptions m_customOptions = CustomOptions.All();
53+
private List<FileInfo> m_presets;
54+
private readonly FileInfo m_customPreset = new FileInfo("Custom");
5155

5256
public event EventHandler<(string originalCode, string newCode)> GlslLoaded;
5357

@@ -60,7 +64,7 @@ public class AppViewModel : ViewModelBase
6064
public ICommand OnCustomOptionsAcceptedCommand => m_customOptionsAcceptedCommand ??= new RelayCommand(AcceptCustomOptions);
6165
public ICommand OnHintsAcceptedCommand => m_hintsAcceptedCommand ??= new RelayCommand(AcceptHints);
6266

63-
public CustomOptions CustomOptions { get; }
67+
public CustomOptions CustomOptions => m_customOptions;
6468

6569
public string ShadertoyShaderId { get; set; }
6670

@@ -145,13 +149,56 @@ public string AppTitle
145149
return $"{assemblyInfo.ProductName} v{assemblyInfo.Version}";
146150
}
147151
}
152+
148153
public ObservableCollection<CodeHint> Hints { get; } = new ObservableCollection<CodeHint>();
149154

155+
public IEnumerable<FileInfo> Presets
156+
{
157+
get
158+
{
159+
if (m_presets == null)
160+
{
161+
var presetsDir = new DirectoryInfo(Path.Combine(AppContext.BaseDirectory, "Presets"));
162+
m_presets = presetsDir.Exists ? presetsDir.EnumerateFiles().ToList() : new List<FileInfo>();
163+
164+
// Add 'special' case.
165+
m_presets.Add(m_customPreset);
166+
}
167+
168+
return m_presets;
169+
}
170+
}
171+
172+
public FileInfo SelectedPreset
173+
{
174+
get => m_selectedPreset ??= Presets.FirstOrDefault(o => o.Name == Settings.Default.MostRecentPreset) ?? Presets.FirstOrDefault();
175+
set
176+
{
177+
if (m_selectedPreset == value)
178+
return;
179+
180+
m_selectedPreset = value;
181+
Settings.Default.MostRecentPreset = value.Name;
182+
Settings.Default.Save();
183+
184+
LoadOptionsFromPreset();
185+
}
186+
}
187+
150188
public AppViewModel()
151189
{
152-
if (!string.IsNullOrWhiteSpace(Settings.Default.CustomOptions))
153-
CustomOptions = JsonConvert.DeserializeObject<CustomOptions>(Settings.Default.CustomOptions);
154-
CustomOptions ??= CustomOptions.All();
190+
LoadOptionsFromPreset();
191+
}
192+
193+
private void LoadOptionsFromPreset()
194+
{
195+
var options = SelectedPreset == m_customPreset ? Settings.Default.CustomOptions : File.ReadAllText(SelectedPreset.FullName);
196+
if (string.IsNullOrEmpty(options))
197+
m_customOptions = CustomOptions.All();
198+
else
199+
JsonConvert.PopulateObject(options, m_customOptions);
200+
201+
OnPropertyChanged(nameof(CustomOptions));
155202
}
156203

157204
private void LoadGlslFromClipboard(object obj)
@@ -187,7 +234,7 @@ private void LoadGlslFromFile(object obj)
187234

188235
private void LoadGlslFromShadertoy(object obj)
189236
{
190-
var id = ShadertoyShaderId.Trim();
237+
var id = ShadertoyShaderId?.Trim();
191238
if (string.IsNullOrWhiteSpace(id) || id.Length != 6)
192239
return;
193240

@@ -273,14 +320,13 @@ private async void ShrinkAsync(object levelParam = null)
273320
finally
274321
{
275322
ShowProgress = false;
323+
SaveCustomOptions();
276324
}
277325

278326
m_optimizedRoot = null;
279327
OptimizedCode = null;
280328
OptimizedSize = 0;
281329
GlslLoaded?.Invoke(this, (OriginalCode, string.Empty));
282-
283-
SaveCustomOptions();
284330
}
285331

286332
private (int optimizedSize, string optimizedCode, IEnumerable<CodeHint> hints) Shrink(string glsl, string level)
@@ -305,6 +351,9 @@ private async void ShrinkAsync(object levelParam = null)
305351

306352
public void SaveCustomOptions()
307353
{
354+
if (Settings.Default.MostRecentPreset != "Custom")
355+
return;
356+
308357
Settings.Default.CustomOptions = JsonConvert.SerializeObject(CustomOptions);
309358
Settings.Default.Save();
310359
}

ShaderShrinker/Shrinker.WpfApp/OptionsDialog.xaml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
</Grid.Resources>
2222

2323
<Grid.RowDefinitions>
24+
<RowDefinition Height="*"/>
2425
<RowDefinition Height="*"/>
2526
<RowDefinition Height="Auto"/>
2627
</Grid.RowDefinitions>
@@ -31,7 +32,12 @@
3132
<ColumnDefinition Width="0.5*"/>
3233
</Grid.ColumnDefinitions>
3334

34-
<StackPanel Grid.Row="0" Grid.Column="0">
35+
<StackPanel Grid.Column="0" Grid.ColumnSpan="3" HorizontalAlignment="Right" Orientation="Horizontal" Margin="0,0,0,16">
36+
<Label Content="Presets:" VerticalAlignment="Center"/>
37+
<ComboBox ItemsSource="{Binding Presets}" DisplayMemberPath="Name" Width="150" SelectedItem="{Binding SelectedPreset}"/>
38+
</StackPanel>
39+
40+
<StackPanel Grid.Row="1" Grid.Column="0">
3541
<CheckBox x:Name="removeCommentsCheck" Content="Remove comments" IsChecked="{Binding CustomOptions.RemoveComments}">
3642
<CheckBox.ToolTip>
3743
<MdXaml:MarkdownScrollViewer xml:space="preserve">
@@ -332,11 +338,11 @@
332338
</CheckBox>
333339
</StackPanel>
334340

335-
<StackPanel Grid.Row="0" Grid.Column="1" Orientation="Horizontal" Margin="16,0">
341+
<StackPanel Grid.Row="1" Grid.Column="1" Orientation="Horizontal" Margin="16,0">
336342
<Separator Width="3" Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" />
337343
</StackPanel>
338344

339-
<StackPanel Grid.Row="0" Grid.Column="2">
345+
<StackPanel Grid.Row="1" Grid.Column="2">
340346
<CheckBox Content="Simplify float number format" IsChecked="{Binding CustomOptions.SimplifyNumberFormat}">
341347
<CheckBox.ToolTip>
342348
<MdXaml:MarkdownScrollViewer xml:space="preserve">
@@ -627,10 +633,10 @@
627633
</CheckBox>
628634
</StackPanel>
629635

630-
<StackPanel Grid.Row="1" Grid.Column="0" Orientation="Horizontal" Margin="0,16,0,0" VerticalAlignment="Center">
636+
<StackPanel Grid.Row="2" Grid.Column="0" Orientation="Horizontal" Margin="0,16,0,0" VerticalAlignment="Center">
631637
<materialDesign:PackIcon Kind="InfoCircle" Width="16" Height="16" Foreground="{DynamicResource PrimaryHueLightBrush}"/>
632638
<TextBlock Text="See tooltips for more information" Margin="4,0" VerticalAlignment="Center"/>
633639
</StackPanel>
634640

635-
<Button Grid.Row="1" Grid.Column="2" Content="APPLY" Command="{Binding OnCustomOptionsAcceptedCommand}" HorizontalAlignment="Right" Margin="0,16,0,0" Foreground="White"/>
641+
<Button Grid.Row="2" Grid.Column="2" Content="APPLY" Command="{Binding OnCustomOptionsAcceptedCommand}" HorizontalAlignment="Right" Margin="0,16,0,0" Foreground="White"/>
636642
</Grid>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"RemoveDisabledCode": true,
3+
"RemoveComments": true,
4+
"KeepHeaderComments": false,
5+
"RemoveUnusedFunctions": true,
6+
"SimplifyFunctionDeclarations": true,
7+
"SimplifyFunctionParams": true,
8+
"GroupVariableDeclarations": true,
9+
"JoinVariableDeclarationsWithAssignments": true,
10+
"RemoveUnusedVariables": true,
11+
"DetectConstants": true,
12+
"InlineConstantVariables": true,
13+
"InlineDefines": true,
14+
"SimplifyNumberFormat": true,
15+
"SimplifyVectorConstructors": true,
16+
"SimplifyVectorReferences": true,
17+
"RemoveUnreachableCode": true,
18+
"CombineConsecutiveAssignments": true,
19+
"CombineAssignmentWithSingleUse": true,
20+
"IntroduceMathOperators": true,
21+
"SimplifyArithmetic": true,
22+
"PerformArithmetic": true,
23+
"SimplifyBranching": true,
24+
"ReplaceFunctionCallsWithResult": true,
25+
"MoveConstantParametersIntoCalledFunctions": true
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"RemoveDisabledCode": false,
3+
"RemoveComments": false,
4+
"KeepHeaderComments": false,
5+
"RemoveUnusedFunctions": false,
6+
"SimplifyFunctionDeclarations": false,
7+
"SimplifyFunctionParams": false,
8+
"GroupVariableDeclarations": false,
9+
"JoinVariableDeclarationsWithAssignments": false,
10+
"RemoveUnusedVariables": false,
11+
"DetectConstants": false,
12+
"InlineConstantVariables": false,
13+
"InlineDefines": false,
14+
"SimplifyNumberFormat": false,
15+
"SimplifyVectorConstructors": false,
16+
"SimplifyVectorReferences": false,
17+
"RemoveUnreachableCode": false,
18+
"CombineConsecutiveAssignments": false,
19+
"CombineAssignmentWithSingleUse": false,
20+
"IntroduceMathOperators": false,
21+
"SimplifyArithmetic": false,
22+
"PerformArithmetic": false,
23+
"SimplifyBranching": false,
24+
"ReplaceFunctionCallsWithResult": false,
25+
"MoveConstantParametersIntoCalledFunctions": false
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"RemoveDisabledCode": true,
3+
"RemoveComments": false,
4+
"KeepHeaderComments": false,
5+
"RemoveUnusedFunctions": true,
6+
"SimplifyFunctionDeclarations": false,
7+
"SimplifyFunctionParams": false,
8+
"GroupVariableDeclarations": false,
9+
"JoinVariableDeclarationsWithAssignments": false,
10+
"RemoveUnusedVariables": true,
11+
"DetectConstants": false,
12+
"InlineConstantVariables": false,
13+
"InlineDefines": false,
14+
"SimplifyNumberFormat": false,
15+
"SimplifyVectorConstructors": false,
16+
"SimplifyVectorReferences": false,
17+
"RemoveUnreachableCode": true,
18+
"CombineConsecutiveAssignments": false,
19+
"CombineAssignmentWithSingleUse": false,
20+
"IntroduceMathOperators": false,
21+
"SimplifyArithmetic": false,
22+
"PerformArithmetic": false,
23+
"SimplifyBranching": false,
24+
"ReplaceFunctionCallsWithResult": false,
25+
"MoveConstantParametersIntoCalledFunctions": false
26+
}

ShaderShrinker/Shrinker.WpfApp/Properties/Settings.Designer.cs

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ShaderShrinker/Shrinker.WpfApp/Properties/Settings.settings

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@
55
<Setting Name="CustomOptions" Type="System.String" Scope="User">
66
<Value Profile="(Default)" />
77
</Setting>
8+
<Setting Name="MostRecentPreset" Type="System.String" Scope="User">
9+
<Value Profile="(Default)">Maximum</Value>
10+
</Setting>
811
</Settings>
912
</SettingsFile>

ShaderShrinker/Shrinker.WpfApp/Shrinker.WpfApp.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@
5252
</Compile>
5353
</ItemGroup>
5454
<ItemGroup>
55+
<None Update="Presets\Maximum">
56+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
57+
</None>
58+
<None Update="Presets\Minimum %28Reformat%29">
59+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
60+
</None>
61+
<None Update="Presets\Remove Dead Code">
62+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
63+
</None>
5564
<None Update="Properties\Settings.settings">
5665
<Generator>SettingsSingleFileGenerator</Generator>
5766
<LastGenOutput>Settings.Designer.cs</LastGenOutput>

0 commit comments

Comments
 (0)