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

Commit c6ed78f

Browse files
committed
implemented Recent Files
1 parent f6738be commit c6ed78f

File tree

5 files changed

+117
-15
lines changed

5 files changed

+117
-15
lines changed

Interop/OptionsControl.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.IO;
34
using System.Runtime.Serialization.Formatters.Binary;
45
using System.Security.Cryptography;
6+
using System.Windows.Controls;
57
using System.Windows.Media;
68
using SPCode.Utils;
9+
using SPCode.Utils.Models;
710

811
namespace SPCode.Interop
912
{
@@ -75,6 +78,9 @@ public class OptionsControl
7578

7679
public bool UI_Animations = true;
7780
public bool UI_ShowToolBar;
81+
82+
public LinkedList<string> RecentFiles = new();
83+
7884
public int Version = 11;
7985

8086
public void FillNullToDefaults()
@@ -224,7 +230,6 @@ public static void Save()
224230
using var fileStream = new FileStream(Paths.GetOptionsFilePath(), FileMode.Create, FileAccess.ReadWrite,
225231
FileShare.None);
226232
formatter.Serialize(fileStream, Program.OptionsObject);
227-
var test = Program.OptionsObject;
228233
}
229234
catch (Exception)
230235
{

UI/MainWindow/MainWindow.xaml

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Red.xaml" />
2323
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/FlatButton.xaml" />
2424
<ResourceDictionary>
25-
25+
2626
<!-- Button style with pressed effects -->
27-
27+
2828
<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
2929
<Setter Property="Background" Value="{DynamicResource WhiteBrush}"/>
3030
<Setter Property="Template">
@@ -45,23 +45,23 @@
4545
</Trigger>
4646
</Style.Triggers>
4747
</Style>
48-
48+
4949
<!--Button Images-->
5050

5151
<Image x:Key="ImgReload" Source="/SPCode;component/Resources/Icons/icon-reload.png" Width="16"/>
5252
<Image x:Key="ImgCollapse" Source="/SPCode;component/Resources/Icons/icon-collapse.png" Width="16"/>
5353
<Image x:Key="ImgExpand" Source="/SPCode;component/Resources/Icons/icon-expand.png" Width="16"/>
5454

5555
<!--BlendOverEffect used on save and such-->
56-
56+
5757
<Storyboard x:Key="BlendOverEffect" Duration="00:00:00.5" Storyboard.TargetName="BlendEffectPlane" Storyboard.TargetProperty="Opacity">
5858
<DoubleAnimationUsingKeyFrames>
5959
<SplineDoubleKeyFrame Value="0.0" KeyTime="00:00:00.00" />
6060
<SplineDoubleKeyFrame Value="0.5" KeyTime="00:00:00.25" />
6161
<SplineDoubleKeyFrame Value="0.0" KeyTime="00:00:00.50" />
6262
</DoubleAnimationUsingKeyFrames>
6363
</Storyboard>
64-
64+
6565
<Storyboard x:Key="FadeFindReplaceGridIn" Duration="00:00:00.2" Storyboard.TargetName="FindReplaceGrid">
6666
<DoubleAnimation To="1" Duration="00:00:00.2" Storyboard.TargetProperty="Opacity" />
6767
</Storyboard>
@@ -92,7 +92,7 @@
9292
</ResourceDictionary.MergedDictionaries>
9393
</ResourceDictionary>
9494
</controls:MetroWindow.Resources>
95-
95+
9696
<controls:MetroWindow.LeftWindowCommands>
9797
<controls:WindowCommands>
9898
<Grid x:Name="MainWindowCommands">
@@ -101,6 +101,13 @@
101101
<MenuItem x:Name="MenuI_New" Header="New" Click="Menu_New"/>
102102
<MenuItem x:Name="MenuI_NewTemplate" Header="New From Template" Click="Menu_NewFromTemplate"/>
103103
<MenuItem x:Name="MenuI_Open" Header="Open" Click="Menu_Open"/>
104+
<MenuItem x:Name="MenuI_Recent" Header="Recent files">
105+
<MenuItem.ContextMenu>
106+
<ContextMenu>
107+
<MenuItem x:Name="MenuI_ClearRecent" Header="Clear recent" Click="Menu_ClearRecent"/>
108+
</ContextMenu>
109+
</MenuItem.ContextMenu>
110+
</MenuItem>
104111
<Separator />
105112
<MenuItem x:Name="MenuI_Save" Header="Save" Click="Menu_Save"/>
106113
<MenuItem x:Name="MenuI_SaveAll" Header="Save All" Click="Menu_SaveAll"/>

UI/MainWindow/MainWindow.xaml.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
using System.ComponentModel;
55
using System.Diagnostics;
66
using System.IO;
7+
using System.Linq;
78
using System.Text.RegularExpressions;
89
using System.Windows;
910
using System.Windows.Controls;
11+
using System.Windows.Documents;
12+
using System.Windows.Media;
1013
using System.Windows.Media.Animation;
1114
using System.Windows.Threading;
1215
using DiscordRPC;
@@ -122,6 +125,7 @@ public MainWindow(SplashScreen sc)
122125

123126
LoadInputGestureTexts();
124127
LoadCommandsDictionary();
128+
LoadRecentsList();
125129

126130
UpdateOBFileButton();
127131

@@ -373,6 +377,7 @@ private void AddEditorElement(string filePath, string name, bool SelectMe, out E
373377
layoutDocument.Content = editor;
374378
EditorsReferences.Add(editor);
375379
DockingPane.Children.Add(layoutDocument);
380+
AddNewRecentFile(filePath);
376381
if (SelectMe)
377382
{
378383
layoutDocument.IsSelected = true;
@@ -437,5 +442,42 @@ public void UpdateWindowTitle()
437442
Title = outString;
438443
}
439444
#endregion
445+
446+
private void AddNewRecentFile(string filePath)
447+
{
448+
if (Program.OptionsObject.RecentFiles.Any(x => x.Equals(filePath)))
449+
{
450+
return;
451+
}
452+
MenuI_Recent.IsEnabled = true;
453+
Program.OptionsObject.RecentFiles.AddFirst(filePath);
454+
var fInfo = new FileInfo(filePath);
455+
var lbl = new TextBlock();
456+
457+
lbl.Inlines.Add($"{fInfo.Name} ");
458+
lbl.Inlines.Add(new Run(fInfo.FullName)
459+
{
460+
FontSize = FontSize - 2,
461+
Foreground = new SolidColorBrush(Colors.DarkGray),
462+
FontStyle = FontStyles.Italic
463+
});
464+
465+
var mi = new MenuItem()
466+
{
467+
Header = lbl
468+
};
469+
470+
mi.Click += (sender, e) =>
471+
{
472+
TryLoadSourceFile(fInfo.FullName, out _, true, false, true);
473+
};
474+
475+
MenuI_Recent.Items.Insert(0, mi);
476+
if (MenuI_Recent.Items.Count > 10)
477+
{
478+
MenuI_Recent.Items.RemoveAt(10);
479+
Program.OptionsObject.RecentFiles.RemoveLast();
480+
}
481+
}
440482
}
441483
}

UI/MainWindow/MainWindowMenuHandler.cs

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
using System;
22
using System.Diagnostics;
3+
using System.IO;
34
using System.Reflection;
45
using System.Windows;
56
using System.Windows.Controls;
7+
using System.Windows.Documents;
8+
using System.Windows.Media;
69
using MahApps.Metro.Controls.Dialogs;
710
using SPCode.Interop;
811
using SPCode.Interop.Updater;
912
using SPCode.UI.Windows;
1013
using SPCode.Utils;
14+
using SPCode.Utils.Models;
1115

1216
namespace SPCode.UI
1317
{
@@ -23,11 +27,18 @@ private void FileMenu_Open(object sender, RoutedEventArgs e)
2327
}
2428

2529
var EditorIsSelected = GetCurrentEditorElement() != null;
26-
((MenuItem)((MenuItem)sender).Items[4]).IsEnabled = EditorIsSelected;
27-
((MenuItem)((MenuItem)sender).Items[6]).IsEnabled = EditorIsSelected;
28-
((MenuItem)((MenuItem)sender).Items[8]).IsEnabled = EditorIsSelected;
29-
((MenuItem)((MenuItem)sender).Items[5]).IsEnabled = EditorsAreOpen;
30-
((MenuItem)((MenuItem)sender).Items[9]).IsEnabled = EditorsAreOpen;
30+
MenuI_Save.IsEnabled = EditorIsSelected;
31+
MenuI_SaveAs.IsEnabled = EditorIsSelected;
32+
MenuI_Close.IsEnabled = EditorIsSelected;
33+
MenuI_SaveAll.IsEnabled = EditorsAreOpen;
34+
MenuI_CloseAll.IsEnabled = EditorsAreOpen;
35+
}
36+
37+
private void Menu_ClearRecent(object sender, RoutedEventArgs e)
38+
{
39+
Program.OptionsObject.RecentFiles.Clear();
40+
MenuI_Recent.Items.Clear();
41+
MenuI_Recent.IsEnabled = false;
3142
}
3243

3344
private void Menu_New(object sender, RoutedEventArgs e)
@@ -324,5 +335,42 @@ private void LoadInputGestureTexts()
324335
}
325336
}
326337
}
338+
339+
private void LoadRecentsList()
340+
{
341+
var recentsList = Program.OptionsObject.RecentFiles;
342+
343+
if (recentsList.Count == 0)
344+
{
345+
MenuI_Recent.IsEnabled = false;
346+
return;
347+
}
348+
349+
foreach (var file in recentsList)
350+
{
351+
var fInfo = new FileInfo(file);
352+
var lbl = new TextBlock();
353+
354+
lbl.Inlines.Add($"{fInfo.Name} ");
355+
lbl.Inlines.Add(new Run(fInfo.FullName)
356+
{
357+
FontSize = FontSize - 2,
358+
Foreground = new SolidColorBrush(Colors.DarkGray),
359+
FontStyle = FontStyles.Italic
360+
});
361+
362+
var mi = new MenuItem()
363+
{
364+
Header = lbl
365+
};
366+
367+
mi.Click += (sender, e) =>
368+
{
369+
TryLoadSourceFile(fInfo.FullName, out _, true, false, true);
370+
};
371+
372+
MenuI_Recent.Items.Add(mi);
373+
}
374+
}
327375
}
328376
}

UI/MainWindow/MainWindowObjectBrowser.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -559,12 +559,12 @@ private void ChangeObjectBrowserToDrives()
559559
}
560560

561561
/// <summary>
562-
/// Helper function to build an expanded item's contents. <br/>
563-
/// It outs a TreeViewItem list to be used when using the Reload function to keep directories expanded after refreshing.
562+
/// <para> Helper function to build an expanded item's contents. </para>
563+
/// <para> It outs a TreeViewItem list to be used when using the Reload function to keep directories expanded after refreshing. </para>
564564
/// </summary>
565565
/// <param name="dir">Directory to fetch contents from.</param>
566566
/// <param name="itemsToExpand">List of items that were expanded before calling this function to reload the Object Browser items.</param>
567-
/// <returns></returns>
567+
/// <returns>List of Items build from the specified directory.</returns>
568568
private List<TreeViewItem> BuildDirectoryItems(string dir, out List<TreeViewItem> itemsToExpand)
569569
{
570570
itemsToExpand = new();

0 commit comments

Comments
 (0)