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

Commit 45be5df

Browse files
committed
improved recent files item design
1 parent c6ed78f commit 45be5df

File tree

1 file changed

+62
-20
lines changed

1 file changed

+62
-20
lines changed

UI/MainWindow/MainWindowMenuHandler.cs

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
using System;
22
using System.Diagnostics;
33
using System.IO;
4+
using System.Linq;
45
using System.Reflection;
56
using System.Windows;
67
using System.Windows.Controls;
78
using System.Windows.Documents;
89
using System.Windows.Media;
10+
using System.Windows.Media.Imaging;
911
using MahApps.Metro.Controls.Dialogs;
1012
using SPCode.Interop;
1113
using SPCode.Interop.Updater;
@@ -17,6 +19,7 @@ namespace SPCode.UI
1719
{
1820
public partial class MainWindow
1921
{
22+
#region Events
2023
private void FileMenu_Open(object sender, RoutedEventArgs e)
2124
{
2225
var editors = GetAllEditorElements();
@@ -154,7 +157,6 @@ private void Menu_ToggleCommentLine(object sender, RoutedEventArgs e)
154157
Command_ToggleCommentLine();
155158
}
156159

157-
158160
private void Menu_SelectAll(object sender, RoutedEventArgs e)
159161
{
160162
Command_SelectAll();
@@ -297,7 +299,9 @@ private void MenuButton_Action(object sender, RoutedEventArgs e)
297299
case 2: Server_Start(); break;
298300
}
299301
}
302+
#endregion
300303

304+
#region Methods
301305
/// <summary>
302306
/// Loads the input gesture texts to the menu items.
303307
/// </summary>
@@ -336,6 +340,15 @@ private void LoadInputGestureTexts()
336340
}
337341
}
338342

343+
private void AddNewRecentFile(string filePath)
344+
{
345+
if (!Program.OptionsObject.RecentFiles.Any(x => x.Equals(filePath)))
346+
{
347+
MenuI_Recent.Items.Insert(0, BuildRecentFileItem(filePath));
348+
Program.OptionsObject.RecentFiles.AddFirst(filePath);
349+
}
350+
}
351+
339352
private void LoadRecentsList()
340353
{
341354
var recentsList = Program.OptionsObject.RecentFiles;
@@ -348,29 +361,58 @@ private void LoadRecentsList()
348361

349362
foreach (var file in recentsList)
350363
{
351-
var fInfo = new FileInfo(file);
352-
var lbl = new TextBlock();
364+
MenuI_Recent.Items.Add(BuildRecentFileItem(file));
365+
}
366+
}
353367

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-
});
368+
private MenuItem BuildRecentFileItem(string file)
369+
{
370+
// Create FileInfo to handle file
371+
var fInfo = new FileInfo(file);
361372

362-
var mi = new MenuItem()
363-
{
364-
Header = lbl
365-
};
373+
// Create the image for the file
374+
var img = new Image()
375+
{
376+
Source = new BitmapImage(new Uri($"/SPCode;component/Resources/Icons/{FileIcons[fInfo.Extension]}", UriKind.Relative)),
377+
Width = 15,
378+
Height = 15
379+
};
366380

367-
mi.Click += (sender, e) =>
368-
{
369-
TryLoadSourceFile(fInfo.FullName, out _, true, false, true);
370-
};
381+
// Create the text that the MenuItem will display
382+
var text = new TextBlock()
383+
{
384+
Margin = new Thickness(5.0, 0.0, 0.0, 0.0),
385+
};
386+
text.Inlines.Add($"{fInfo.Name} ");
387+
text.Inlines.Add(new Run(fInfo.FullName)
388+
{
389+
FontSize = FontSize - 2,
390+
Foreground = new SolidColorBrush(Colors.DarkGray),
391+
FontStyle = FontStyles.Italic
392+
});
371393

372-
MenuI_Recent.Items.Add(mi);
373-
}
394+
// Create the StackPanel where we will place image and text
395+
var stack = new StackPanel()
396+
{
397+
Orientation = Orientation.Horizontal,
398+
};
399+
stack.Children.Add(img);
400+
stack.Children.Add(text);
401+
402+
// Create the MenuItem and set the header to the created StackPanel
403+
var mi = new MenuItem()
404+
{
405+
Header = stack
406+
};
407+
408+
// Set the click callback to open the file
409+
mi.Click += (sender, e) =>
410+
{
411+
TryLoadSourceFile(fInfo.FullName, out _, true, false, true);
412+
};
413+
414+
return mi;
374415
}
416+
#endregion
375417
}
376418
}

0 commit comments

Comments
 (0)