Skip to content
This repository was archived by the owner on Sep 11, 2023. It is now read-only.

Commit f261ae4

Browse files
committed
added default on close actions in options (fix #110)
1 parent a158659 commit f261ae4

File tree

7 files changed

+98
-63
lines changed

7 files changed

+98
-63
lines changed

Interop/OptionsControl.cs

Lines changed: 58 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace SPCode
1111
[Serializable]
1212
public class OptionsControl
1313
{
14-
public static int SVersion = 13;
14+
public static int SVersion = 14;
1515
public bool Editor_AgressiveIndentation = true;
1616
public bool Editor_AutoCloseBrackets = true;
1717
public bool Editor_AutoCloseStringChars = true;
@@ -84,6 +84,9 @@ public class OptionsControl
8484
// Version 13
8585
public SearchOptions SearchOptions;
8686

87+
// Version 14
88+
public ActionOnClose ActionOnClose;
89+
8790
public int Version = 11;
8891

8992
public void FillNullToDefaults()
@@ -195,7 +198,7 @@ public void NormalizeSHColors()
195198
SH_CommentsMarker = new SerializableColor(0xFF, 0xFF, 0x20, 0x20);
196199
SH_Strings = new SerializableColor(0xFF, 0xF4, 0x6B, 0x6C);
197200
SH_PreProcessor = new SerializableColor(0xFF, 0x7E, 0x7E, 0x7E);
198-
SH_Types = new SerializableColor(0xFF, 0x28, 0x90, 0xB0); //56 9C D5
201+
SH_Types = new SerializableColor(0xFF, 0x28, 0x90, 0xB0);
199202
SH_TypesValues = new SerializableColor(0xFF, 0x56, 0x9C, 0xD5);
200203
SH_Keywords = new SerializableColor(0xFF, 0x56, 0x9C, 0xD5);
201204
SH_ContextKeywords = new SerializableColor(0xFF, 0x56, 0x9C, 0xD5);
@@ -208,49 +211,7 @@ public void NormalizeSHColors()
208211
SH_Functions = new SerializableColor(0xFF, 0x56, 0x9C, 0xD5);
209212
SH_Methods = new SerializableColor(0xFF, 0x3B, 0xC6, 0x7E);
210213
}
211-
}
212-
213-
[Serializable]
214-
public class SerializableColor
215-
{
216-
public byte A;
217-
public byte B;
218-
public byte G;
219-
public byte R;
220-
221-
public SerializableColor(byte _A, byte _R, byte _G, byte _B)
222-
{
223-
A = _A;
224-
R = _R;
225-
G = _G;
226-
B = _B;
227-
}
228214

229-
public static implicit operator SerializableColor(Color c)
230-
{
231-
return new SerializableColor(c.A, c.R, c.G, c.B);
232-
}
233-
234-
public static implicit operator Color(SerializableColor c)
235-
{
236-
return Color.FromArgb(c.A, c.R, c.G, c.B);
237-
}
238-
}
239-
240-
[Serializable]
241-
public struct SearchOptions
242-
{
243-
public string FindText;
244-
public string ReplaceText;
245-
public int SearchType;
246-
public int Document;
247-
public bool CaseSensitive;
248-
public bool MultilineRegex;
249-
public int ReplaceType;
250-
}
251-
252-
public static class OptionsControlIOObject
253-
{
254215
public static void Save()
255216
{
256217
try
@@ -271,32 +232,79 @@ public static OptionsControl Load(out bool ProgramIsNew)
271232
{
272233
if (File.Exists(Paths.GetOptionsFilePath()))
273234
{
274-
object deserializedOptionsObj;
235+
OptionsControl optionsObject;
275236
var formatter = new BinaryFormatter();
276237
using (var fileStream = new FileStream(Paths.GetOptionsFilePath(), FileMode.Open, FileAccess.Read,
277238
FileShare.ReadWrite))
278239
{
279-
deserializedOptionsObj = formatter.Deserialize(fileStream);
240+
optionsObject = (OptionsControl)formatter.Deserialize(fileStream);
280241
}
281242

282-
var oc = (OptionsControl)deserializedOptionsObj;
283-
oc.FillNullToDefaults();
243+
optionsObject.FillNullToDefaults();
284244
ProgramIsNew = false;
285-
return oc;
245+
return optionsObject;
286246
}
287247
}
288248
catch (Exception)
289249
{
250+
290251
}
291252

292253
var oco = new OptionsControl();
293254
oco.ReCreateCryptoKey();
294255
#if DEBUG
295-
ProgramIsNew = false;
256+
ProgramIsNew = false;
296257
#else
297258
ProgramIsNew = true;
298259
#endif
299260
return oco;
300261
}
301262
}
263+
264+
[Serializable]
265+
public class SerializableColor
266+
{
267+
public byte A;
268+
public byte B;
269+
public byte G;
270+
public byte R;
271+
272+
public SerializableColor(byte _A, byte _R, byte _G, byte _B)
273+
{
274+
A = _A;
275+
R = _R;
276+
G = _G;
277+
B = _B;
278+
}
279+
280+
public static implicit operator SerializableColor(Color c)
281+
{
282+
return new SerializableColor(c.A, c.R, c.G, c.B);
283+
}
284+
285+
public static implicit operator Color(SerializableColor c)
286+
{
287+
return Color.FromArgb(c.A, c.R, c.G, c.B);
288+
}
289+
}
290+
291+
[Serializable]
292+
public struct SearchOptions
293+
{
294+
public string FindText;
295+
public string ReplaceText;
296+
public int SearchType;
297+
public int Document;
298+
public bool CaseSensitive;
299+
public bool MultilineRegex;
300+
public int ReplaceType;
301+
}
302+
303+
[Serializable]
304+
public enum ActionOnClose
305+
{
306+
Prompt,
307+
Save,
308+
DontSave
309+
}
302310
}

Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public static void Main(string[] args)
6565
#endif
6666
_IsLocalInstallation = Paths.IsLocalInstallation();
6767
UpdateStatus = new UpdateInfo();
68-
OptionsObject = OptionsControlIOObject.Load(out var ProgramIsNew);
68+
OptionsObject = OptionsControl.Load(out var ProgramIsNew);
6969

7070
if (!File.Exists(Constants.HotkeysFile))
7171
{
@@ -176,7 +176,7 @@ public static void Main(string[] args)
176176
#endif
177177
app.Startup += App_Startup;
178178
app.Run(MainWindow);
179-
OptionsControlIOObject.Save();
179+
OptionsControl.Save();
180180
#if !DEBUG
181181
}
182182
catch (Exception e)

UI/Components/EditorElement/EditorElement.xaml.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -736,9 +736,11 @@ public void UpdateFontSize(double size, bool updateLineHeight = true)
736736

737737
public async void Close(bool ForcedToSave = false, bool CheckSavings = true)
738738
{
739-
if (CheckSavings && _NeedsSave && !Closed)
739+
var action = Program.OptionsObject.ActionOnClose;
740+
741+
if (CheckSavings && _NeedsSave && !Closed && action != ActionOnClose.DontSave)
740742
{
741-
if (ForcedToSave)
743+
if (ForcedToSave || action == ActionOnClose.Save)
742744
{
743745
Save();
744746
}

UI/MainWindow/MainWindow.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ private async void MetroWindow_Closing(object sender, CancelEventArgs e)
242242
// Close directly if no files need to be saved
243243
var editors = GetAllEditorElements()?.ToList();
244244

245-
if (editors == null || (editors != null && !editors.Any(x => x.NeedsSave)))
245+
if (editors == null || (editors != null && !editors.Any(x => x.NeedsSave)) || Program.OptionsObject.ActionOnClose != ActionOnClose.Prompt)
246246
{
247247
ClosingBuffer = true;
248248
CloseProgram(true);

UI/Windows/OptionsWindow.xaml

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
<controls:MetroWindow x:Class="SPCode.UI.Windows.OptionsWindow"
2-
xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
3-
xmlns:components="clr-namespace:SPCode.UI.Components"
4-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
5-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
6-
Width="680" Height="400" ResizeMode="NoResize" WindowStartupLocation="CenterOwner" GlowBrush="{DynamicResource AccentColorBrush}"
7-
Background="{DynamicResource WhiteBrush}"
8-
Title="Options" ShowTitleBar="False" KeyDown="MetroWindow_KeyDown">
1+
<controls:MetroWindow
2+
x:Class="SPCode.UI.Windows.OptionsWindow"
3+
xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
4+
xmlns:components="clr-namespace:SPCode.UI.Components"
5+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
6+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
7+
Width="680" Height="400" ResizeMode="NoResize" WindowStartupLocation="CenterOwner" GlowBrush="{DynamicResource AccentColorBrush}"
8+
Background="{DynamicResource WhiteBrush}"
9+
Title="Options" ShowTitleBar="False" KeyDown="MetroWindow_KeyDown">
10+
911
<controls:MetroWindow.Resources>
1012
<ResourceDictionary>
1113
<ResourceDictionary.MergedDictionaries>
@@ -44,6 +46,8 @@
4446
<ComboBox Name="AccentColor" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="425,5,0,0" Width="200" SelectionChanged="AccentColor_Changed" />
4547
<Label Name="LanguageLabel" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="333,37,0,0" Content="Language:" />
4648
<ComboBox Name="LanguageBox" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="425,37,0,0" Width="200" SelectionChanged="LanguageBox_Changed" />
49+
<Label Name="ActionOnCloseLabel" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="333,72,0,0" Content="On close:" />
50+
<ComboBox Name="ActionOnCloseBox" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="425,72,0,0" Width="200" SelectionChanged="ActionOnCloseBox_Changed" />
4751
</Grid>
4852
</ScrollViewer>
4953
</controls:MetroTabItem>

UI/Windows/OptionsWindow.xaml.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Diagnostics;
34
using System.Linq;
45
using System.Windows;
@@ -26,6 +27,12 @@ public partial class OptionsWindow
2627
"Pink", "Magenta", "Crimson", "Amber",
2728
"Yellow", "Brown", "Olive", "Steel", "Mauve", "Taupe", "Sienna"
2829
};
30+
private readonly Dictionary<ActionOnClose, string> ActionOnCloseMessages = new()
31+
{
32+
{ ActionOnClose.Prompt, Program.Translations.Get("ActionClosePrompt") },
33+
{ ActionOnClose.Save, Program.Translations.Get("Save") },
34+
{ ActionOnClose.DontSave, Program.Translations.Get("DontSave") }
35+
};
2936
private bool RestartTextIsShown;
3037
private readonly bool AllowChanging;
3138
#endregion
@@ -421,6 +428,11 @@ private void LanguageBox_Changed(object sender, RoutedEventArgs e)
421428
ToggleRestartText(true);
422429
}
423430

431+
private void ActionOnCloseBox_Changed(object sender, SelectionChangedEventArgs e)
432+
{
433+
Program.OptionsObject.ActionOnClose = (ActionOnClose)ActionOnCloseBox.SelectedIndex;
434+
}
435+
424436
private void HardwareSalts_Changed(object sender, RoutedEventArgs e)
425437
{
426438
if (!AllowChanging)
@@ -630,6 +642,13 @@ private void LoadSettings()
630642
}
631643
}
632644

645+
foreach (var action in ActionOnCloseMessages.Values)
646+
{
647+
ActionOnCloseBox.Items.Add(action);
648+
}
649+
650+
ActionOnCloseBox.SelectedIndex = (int)Program.OptionsObject.ActionOnClose;
651+
633652
if (Program.OptionsObject.Editor_AutoSave)
634653
{
635654
var seconds = Program.OptionsObject.Editor_AutoSaveInterval;

Utils/DefaultTranslations.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public class DefaultTranslations
6060
{ "NewTemplate", "New from Template" },
6161
{ "Open", "Open" },
6262
{ "Save", "Save" },
63+
{ "DontSave", "Don't save" },
6364
{ "SaveAll", "Save all" },
6465
{ "SaveAs", "Save as" },
6566
{ "Close", "Close" },
@@ -286,6 +287,7 @@ public class DefaultTranslations
286287
{ "IllegalCharacters", "Illegal characters" },
287288
{ "ReopenLastClosedTab", "Reopen the last closed tab" },
288289
{ "DefaultTaken", "Default taken!" },
290+
{ "ActionClosePrompt", "Prompt whether to save" },
289291
};
290292
}
291293
}

0 commit comments

Comments
 (0)