Added ability to put MRU "Clear All" menu item at top (#104), ability to give "Clear All" menu an icon (#103), and provided callback to provide file icons (#102).

pull/119/head
dahall 2020-01-20 22:48:24 -07:00
parent c21f6e2329
commit 64c7389e97
1 changed files with 158 additions and 128 deletions

View File

@ -19,9 +19,12 @@ namespace Vanara.Configuration
[DefaultEvent(nameof(RecentFileMenuItemClick))] [DefaultEvent(nameof(RecentFileMenuItemClick))]
public class MRUManager : Component public class MRUManager : Component
{ {
/// <summary>This value should contain the appropriate image for the clear list menu item in derived classes.</summary>
protected object clearListMenuItemImage;
private const string defClearListMenuItemText = "Clear List"; private const string defClearListMenuItemText = "Clear List";
private const int defMaxHistoryCount = 10; private const int defMaxHistoryCount = 10;
private MenuPlacement clearListMenuItemPlacement;
private string clearListMenuItemText = defClearListMenuItemText; private string clearListMenuItemText = defClearListMenuItemText;
private string[] exts; private string[] exts;
private IFileListStorage storage; private IFileListStorage storage;
@ -42,10 +45,24 @@ namespace Vanara.Configuration
[Category("Behavior"), Description("Occurs when the clear recent files menu item is clicked.")] [Category("Behavior"), Description("Occurs when the clear recent files menu item is clicked.")]
public event Action<StringCollection> ClearListMenuItemClick; public event Action<StringCollection> ClearListMenuItemClick;
/// <summary>Occurs when [get menu image for file].</summary>
[Category("Behavior"), Description("Occurs when a file menu item is about to be drawn and is requesting an image.")]
public event Func<string, object> GetMenuImageForFile;
/// <summary>Occurs when one of the automatically added recent file menu items is clicked.</summary> /// <summary>Occurs when one of the automatically added recent file menu items is clicked.</summary>
[Category("Behavior"), Description("Occurs when one of the automatically added recent file menu items is clicked.")] [Category("Behavior"), Description("Occurs when one of the automatically added recent file menu items is clicked.")]
public event Action<string> RecentFileMenuItemClick; public event Action<string> RecentFileMenuItemClick;
/// <summary>The placement of a menu item in a list.</summary>
public enum MenuPlacement
{
/// <summary>The bottom of the list, after all items.</summary>
Bottom,
/// <summary>The top of the list, before all items.</summary>
Top
}
/// <summary>Defines a class that implements storage for an MRU file list.</summary> /// <summary>Defines a class that implements storage for an MRU file list.</summary>
public interface IFileListStorage public interface IFileListStorage
{ {
@ -74,9 +91,25 @@ namespace Vanara.Configuration
/// <summary>Rebuilds the menus.</summary> /// <summary>Rebuilds the menus.</summary>
/// <param name="files">The file listing.</param> /// <param name="files">The file listing.</param>
/// <param name="fileMenuItemClick">The handler for when one of the automatically added recent file menu items is clicked..</param> /// <param name="fileMenuItemClick">The handler for when one of the automatically added recent file menu items is clicked..</param>
/// <param name="clearListMenuItemText">The clear list menu item text. A <c>null</c> value indicates that this menu items should not be shown.</param> /// <param name="clearListMenuItemText">
/// The clear list menu item text. A <c>null</c> value indicates that this menu item should not be shown.
/// </param>
/// <param name="clearListMenuItemClick">The handler for when the clear recent files menu item is clicked.</param> /// <param name="clearListMenuItemClick">The handler for when the clear recent files menu item is clicked.</param>
void RebuildMenus(IEnumerable<string> files, Action<string> fileMenuItemClick, string clearListMenuItemText = null, Action clearListMenuItemClick = null); /// <param name="clearListMenuItemImage">
/// The clear list menu item image. A <c>null</c> value indicates that this menu item's image should not be shown.
/// </param>
/// <param name="clearListMenuItemOnTop">if set to <see langword="true"/>, the clear list menu item precedes the files.</param>
/// <param name="menuImageCallback">The menu image callback delegate.</param>
void RebuildMenus(IEnumerable<string> files, Action<string> fileMenuItemClick, string clearListMenuItemText = null, Action clearListMenuItemClick = null, object clearListMenuItemImage = null, bool clearListMenuItemOnTop = false, Func<string, object> menuImageCallback = null);
}
/// <summary>Gets or sets the clear list menu item placement relative to the MRU items.</summary>
/// <value>The clear list menu item placement.</value>
[Category("Appearance"), DefaultValue(typeof(MenuPlacement), "Bottom"), Description("The clear list menu item text."), Localizable(true)]
public MenuPlacement ClearListMenuItemPlacement
{
get => clearListMenuItemPlacement;
set { clearListMenuItemPlacement = value; RefreshRecentFilesMenu(); }
} }
/// <summary>Gets or sets the clear list menu item text.</summary> /// <summary>Gets or sets the clear list menu item text.</summary>
@ -219,7 +252,7 @@ namespace Vanara.Configuration
protected void RefreshRecentFilesMenu() protected void RefreshRecentFilesMenu()
{ {
if (!DesignMode) if (!DesignMode)
MenuBuilderHandler?.RebuildMenus(Files, OnRecentFileMenuItemClick, ClearListMenuItemText, OnClearListMenuItemClick); MenuBuilderHandler?.RebuildMenus(Files, OnRecentFileMenuItemClick, ClearListMenuItemText, OnClearListMenuItemClick, clearListMenuItemImage, clearListMenuItemPlacement == MenuPlacement.Top, GetMenuImageForFile);
} }
[DllImport("shell32.dll", CharSet = CharSet.Ansi)] [DllImport("shell32.dll", CharSet = CharSet.Ansi)]
@ -258,7 +291,9 @@ namespace Vanara.Configuration
private ApplicationSettingsBase settings; private ApplicationSettingsBase settings;
/// <summary>Initializes a new instance of the <see cref="AppSettingsFileListStorage"/> class.</summary> /// <summary>Initializes a new instance of the <see cref="AppSettingsFileListStorage"/> class.</summary>
/// <param name="settings">The settings object to use. If <see langword="null"/>, the component will search all loaded assemblies for an instance.</param> /// <param name="settings">
/// The settings object to use. If <see langword="null"/>, the component will search all loaded assemblies for an instance.
/// </param>
public AppSettingsFileListStorage(ApplicationSettingsBase settings = null) public AppSettingsFileListStorage(ApplicationSettingsBase settings = null)
{ {
if (settings != null) Settings = settings; if (settings != null) Settings = settings;
@ -371,12 +406,10 @@ namespace Vanara.Configuration
{ {
try try
{ {
using (var rK = GetKey()) using var rK = GetKey();
{ var l = new List<string>(GetFiles(rK));
var l = new List<string>(GetFiles(rK)); l.Insert(0, fileNameWithFullPath);
l.Insert(0, fileNameWithFullPath); SetFiles(rK, l.Distinct());
SetFiles(rK, l.Distinct());
}
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -410,12 +443,10 @@ namespace Vanara.Configuration
{ {
try try
{ {
using (var rK = GetKey()) using var rK = GetKey();
{ var l = new List<string>(GetFiles(rK));
var l = new List<string>(GetFiles(rK)); l.RemoveAll(s => string.Equals(s, fileNameWithFullPath, StringComparison.InvariantCultureIgnoreCase));
l.RemoveAll(s => string.Equals(s, fileNameWithFullPath, StringComparison.InvariantCultureIgnoreCase)); SetFiles(rK, l.Distinct());
SetFiles(rK, l.Distinct());
}
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -470,17 +501,16 @@ namespace Vanara.Configuration
} }
} }
#if !WPF
namespace Vanara.Windows.Forms namespace Vanara.Windows.Forms
{ {
using Vanara.Configuration;
using System.Drawing; using System.Drawing;
using System.Windows.Forms; using System.Windows.Forms;
using Vanara.Configuration;
/// <summary> /// <summary>
/// A class that manages a Most Recently Used file listing and interacts with a MenuStrip to display a menu list of the files. By default, the application /// A class that manages a Most Recently Used file listing and interacts with a MenuStrip to display a menu list of the files. By
/// settings are used to store the history. Optionally a constructor can be used to provide an alternate class to handle that work. /// default, the application settings are used to store the history. Optionally a constructor can be used to provide an alternate class
/// to handle that work.
/// </summary> /// </summary>
[DefaultProperty("RecentFileMenuItem")] [DefaultProperty("RecentFileMenuItem")]
public class MenuStripMRUManager : MRUManager public class MenuStripMRUManager : MRUManager
@ -510,6 +540,21 @@ namespace Vanara.Windows.Forms
RefreshRecentFilesMenu(); RefreshRecentFilesMenu();
} }
/// <summary>Gets or sets the clear list menu item image.</summary>
/// <value>The clear list menu item image.</value>
[Category("Appearance"), DefaultValue(null), Description("The clear list menu item icon."), Localizable(true)]
public Image ClearListMenuItemImage
{
get => clearListMenuItemImage as Image;
set
{
if (!(value is Image))
throw new ArgumentException("Must be of type Image.");
clearListMenuItemImage = value;
RefreshRecentFilesMenu();
}
}
/// <summary>Gets or sets the recent file menu item.</summary> /// <summary>Gets or sets the recent file menu item.</summary>
/// <value>The recent file menu item.</value> /// <value>The recent file menu item.</value>
[DefaultValue(null), Category("Behavior"), Description("The recent file menu item.")] [DefaultValue(null), Category("Behavior"), Description("The recent file menu item.")]
@ -549,30 +594,45 @@ namespace Vanara.Windows.Forms
} }
/// <summary>Rebuilds the menus.</summary> /// <summary>Rebuilds the menus.</summary>
/// <param name="files">The files.</param> /// <param name="files">The file listing.</param>
/// <param name="fileMenuItemClick">The file menu item click.</param> /// <param name="fileMenuItemClick">The handler for when one of the automatically added recent file menu items is clicked..</param>
/// <param name="clearListMenuItemText">The clear list menu item text.</param> /// <param name="clearListMenuItemText">
/// <param name="clearListMenuItemClick">The clear list menu item click.</param> /// The clear list menu item text. A <c>null</c> value indicates that this menu item should not be shown.
public void RebuildMenus(IEnumerable<string> files, Action<string> fileMenuItemClick, string clearListMenuItemText = null, Action clearListMenuItemClick = null) /// </param>
/// <param name="clearListMenuItemClick">The handler for when the clear recent files menu item is clicked.</param>
/// <param name="clearListMenuItemImage">
/// The clear list menu item image. A <c>null</c> value indicates that this menu item's image should not be shown.
/// </param>
/// <param name="clearListMenuItemOnTop">if set to <see langword="true"/>, the clear list menu item precedes the files.</param>
/// <param name="menuImageCallback">The menu image callback delegate.</param>
public void RebuildMenus(IEnumerable<string> files, Action<string> fileMenuItemClick, string clearListMenuItemText = null, Action clearListMenuItemClick = null, object clearListMenuItemImage = null, bool clearListMenuItemOnTop = false, Func<string, object> menuImageCallback = null)
{ {
if (RecentFileMenuItem == null) return; if (RecentFileMenuItem == null) return;
RecentFileMenuItem.DropDownItems.Clear(); RecentFileMenuItem.DropDownItems.Clear();
fileMenuItemClickAction = fileMenuItemClick; if (files is null || !files.Any())
foreach (var f in files)
RecentFileMenuItem.DropDownItems.Add(new ToolStripMenuItem(CompactPath(f, RecentFileMenuItem.Font, RecentFileMenuItem.Width), null, OnFileMenuItemClick) { Tag = f });
if (RecentFileMenuItem.DropDownItems.Count == 0)
{ {
RecentFileMenuItem.Enabled = false; RecentFileMenuItem.Enabled = false;
return; return;
} }
if (!string.IsNullOrEmpty(clearListMenuItemText)) if (clearListMenuItemOnTop && !string.IsNullOrEmpty(clearListMenuItemText))
{
RecentFileMenuItem.DropDownItems.Add(clearListMenuItemText, clearListMenuItemImage as Image, (o, e) => clearListMenuItemClick?.Invoke());
RecentFileMenuItem.DropDownItems.Add("-");
}
fileMenuItemClickAction = fileMenuItemClick;
foreach (var f in files)
{
RecentFileMenuItem.DropDownItems.Add(new ToolStripMenuItem(CompactPath(f, RecentFileMenuItem.Font, RecentFileMenuItem.Width), menuImageCallback?.Invoke(f) as Image, OnFileMenuItemClick) { Tag = f });
}
if (!clearListMenuItemOnTop && !string.IsNullOrEmpty(clearListMenuItemText))
{ {
RecentFileMenuItem.DropDownItems.Add("-"); RecentFileMenuItem.DropDownItems.Add("-");
RecentFileMenuItem.DropDownItems.Add(clearListMenuItemText, null, (o, e) => clearListMenuItemClick?.Invoke()); RecentFileMenuItem.DropDownItems.Add(clearListMenuItemText, clearListMenuItemImage as Image, (o, e) => clearListMenuItemClick?.Invoke());
} }
RecentFileMenuItem.Enabled = true; RecentFileMenuItem.Enabled = true;
} }
@ -586,111 +646,81 @@ namespace Vanara.Windows.Forms
} }
} }
#else //namespace Vanara.Windows.Controls
namespace Vanara.Windows.Controls //{
{ // using Vanara.Configuration;
using Vanara.Configuration; // using System.Windows.Controls;
using System.Windows;
/// <summary> // ///
/// A class that manages a Most Recently Used file listing and interacts with a MenuStrip to display a menu list of the files. By default, the application // <summary>
/// settings are used to store the history. Optionally a constructor can be used to provide an alternate class to handle that work. // /// A class that manages a Most Recently Used file listing and interacts with a MenuStrip to display a menu list of the files. By
/// </summary> // default, the application /// settings are used to store the history. Optionally a constructor can be used to provide an alternate class
[DefaultProperty("RecentFileMenuItem")] // to handle that work. ///
public class MenuMRUManager : MRUManager // </summary>
{ // [DefaultProperty("RecentFileMenuItem")] public class MenuMRUManager : MRUManager { ///
/// <summary>Initializes a new instance of the <see cref="MenuMRUManager"/> class.</summary> // <summary>Initializes a new instance of the <see cref="MenuMRUManager"/> class.</summary>
public MenuMRUManager() : base(new AppSettingsFileListStorage(), new MenuBuilder()) // public MenuMRUManager() : base(new AppSettingsFileListStorage(), new MenuBuilder()) { }
{
}
/// <summary>Initializes a new instance of the <see cref="MenuMRUManager"/> class.</summary> // /// <summary>Initializes a new instance of the <see cref="MenuMRUManager"/> class.</summary> /// <param name="extensions">The extensions
/// <param name="extensions">The extensions of files to find in system MRU list.</param> // of files to find in system MRU list.</param> /// <param name="parentMenuItem">The parent "Recent Files" menu item.</param> /// <param
/// <param name="parentMenuItem">The parent "Recent Files" menu item.</param> // name="onRecentFileClick">Action to run when The on recent file click.</param> /// <param name="onClearRecentFilesClick">Optional. The on
/// <param name="onRecentFileClick">Action to run when The on recent file click.</param> // clear recent files click.</param> /// <param name="storageHandler">Optional. The storage handler.</param> public MenuMRUManager(string
/// <param name="onClearRecentFilesClick">Optional. The on clear recent files click.</param> // extensions, MenuItem parentMenuItem, Action<string> onRecentFileClick, Action<StringCollection> onClearRecentFilesClick = null,
/// <param name="storageHandler">Optional. The storage handler.</param> // IFileListStorage storageHandler = null) : base(storageHandler ?? new AppSettingsFileListStorage(), new MenuBuilder()) { FileExtensions =
public MenuMRUManager(string extensions, MenuItem parentMenuItem, Action<string> onRecentFileClick, Action<StringCollection> onClearRecentFilesClick = null, IFileListStorage storageHandler = null) // extensions; ((MenuBuilder)MenuBuilderHandler).RecentFileMenuItem = parentMenuItem; if (onRecentFileClick != null) RecentFileMenuItemClick
: base(storageHandler ?? new AppSettingsFileListStorage(), new MenuBuilder()) // += onRecentFileClick; if (onClearRecentFilesClick != null) ClearListMenuItemClick += onClearRecentFilesClick;
{
FileExtensions = extensions;
((MenuBuilder)MenuBuilderHandler).RecentFileMenuItem = parentMenuItem;
if (onRecentFileClick != null)
RecentFileMenuItemClick += onRecentFileClick;
if (onClearRecentFilesClick != null)
ClearListMenuItemClick += onClearRecentFilesClick;
RefreshRecentFilesMenu(); // RefreshRecentFilesMenu(); }
}
/// <summary>Gets or sets the recent file menu item.</summary> // ///
/// <value>The recent file menu item.</value> // <summary>Gets or sets the recent file menu item.</summary>
[DefaultValue(null), Category("Behavior"), Description("The recent file menu item.")] // ///
public MenuItem RecentFileMenuItem // <value>The recent file menu item.</value>
{ // [DefaultValue(null), Category("Behavior"), Description("The recent file menu item.")] public MenuItem RecentFileMenuItem { get =&gt;
get => ((MenuBuilder)MenuBuilderHandler).RecentFileMenuItem; // ((MenuBuilder)MenuBuilderHandler).RecentFileMenuItem; set { ((MenuBuilder)MenuBuilderHandler).RecentFileMenuItem = value;
set { ((MenuBuilder)MenuBuilderHandler).RecentFileMenuItem = value; RefreshRecentFilesMenu(); } // RefreshRecentFilesMenu(); } }
}
/// <summary>Builds a menu within a MenuStrip.</summary> // /// <summary>Builds a menu within a MenuStrip.</summary> private class MenuBuilder : IMenuBuilder { private Action<string> fileMenuItemClickAction;
private class MenuBuilder : IMenuBuilder
{
private Action<string> fileMenuItemClickAction;
/// <summary>Gets or sets the recent file menu item.</summary> // ///
/// <value>The recent file menu item.</value> // <summary>Gets or sets the recent file menu item.</summary>
public MenuItem RecentFileMenuItem { get; set; } // ///
// <value>The recent file menu item.</value>
// public MenuItem RecentFileMenuItem { get; set; }
/// <summary>Clears the recent files.</summary> // ///
public void ClearRecentFiles() // <summary>Clears the recent files.</summary>
{ // public void ClearRecentFiles() { if (RecentFileMenuItem == null) return; RecentFileMenuItem.Items.Clear(); RecentFileMenuItem.IsEnabled =
if (RecentFileMenuItem == null) return; // false; }
RecentFileMenuItem.Items.Clear();
RecentFileMenuItem.IsEnabled = false;
}
/// <summary>Rebuilds the menus.</summary> // /// <summary>Rebuilds the menus.</summary> /// <param name="files">The file listing.</param> /// <param name="fileMenuItemClick">The
/// <param name="files">The files.</param> // handler for when one of the automatically added recent file menu items is clicked..</param> /// <param name="clearListMenuItemText">The
/// <param name="fileMenuItemClick">The file menu item click.</param> // clear list menu item text. A <c>null</c> value indicates that this menu item should not be shown.</param> /// <param
/// <param name="clearListMenuItemText">The clear list menu item text.</param> // name="clearListMenuItemClick">The handler for when the clear recent files menu item is clicked.</param> /// <param
/// <param name="clearListMenuItemClick">The clear list menu item click.</param> // name="clearListMenuItemImage">The clear list menu item image. A <c>null</c> value indicates that this menu item's image should not be
public void RebuildMenus(IEnumerable<string> files, Action<string> fileMenuItemClick, string clearListMenuItemText, Action clearListMenuItemClick) // shown.</param> /// <param name="clearListMenuItemOnTop">if set to <see langword="true" />, the clear list menu item precedes the
{ // files.</param> /// <param name="menuImageCallback">The menu image callback delegate.</param> public void RebuildMenus(IEnumerable<string>
if (RecentFileMenuItem == null) return; // files, Action<string> fileMenuItemClick, string clearListMenuItemText = null, Action clearListMenuItemClick = null, object
// clearListMenuItemImage = null, bool clearListMenuItemOnTop = false, Func<string, object> menuImageCallback = null) { if
// (RecentFileMenuItem == null) return;
RecentFileMenuItem.Items.Clear(); // RecentFileMenuItem.Items.Clear();
fileMenuItemClickAction = fileMenuItemClick; // fileMenuItemClickAction = fileMenuItemClick; foreach (var f in files) AddMenuItem(f, OnFileMenuItemClick);
foreach (var f in files)
AddMenuItem(f, OnFileMenuItemClick);
if (RecentFileMenuItem.Items.Count == 0) // if (RecentFileMenuItem.Items.Count == 0) { RecentFileMenuItem.IsEnabled = false; return; }
{
RecentFileMenuItem.IsEnabled = false;
return;
}
if (!string.IsNullOrEmpty(clearListMenuItemText)) // if (!string.IsNullOrEmpty(clearListMenuItemText)) { RecentFileMenuItem.Items.Add(new Separator()); AddMenuItem(clearListMenuItemText, (o,
{ // e) => clearListMenuItemClick()); } RecentFileMenuItem.IsEnabled = true; }
RecentFileMenuItem.Items.Add(new Separator());
AddMenuItem(clearListMenuItemText, (o, e) => clearListMenuItemClick());
}
RecentFileMenuItem.IsEnabled = true;
}
private void AddMenuItem(string f, System.Windows.RoutedEventHandler handler) // private void AddMenuItem(string f, System.Windows.RoutedEventHandler handler) { var m = new MenuItem() { Header = f, Tag = f }; m.Click
{ // += handler; RecentFileMenuItem.Items.Add(m); }
var m = new MenuItem() { Header = f, Tag = f };
m.Click += handler;
RecentFileMenuItem.Items.Add(m);
}
private void OnFileMenuItemClick(object sender, RoutedEventArgs e) // private void OnFileMenuItemClick(object sender, RoutedEventArgs e)
{ // {
if (sender is MenuItem item) // if (sender is MenuItem item)
fileMenuItemClickAction(item.Tag.ToString()); // fileMenuItemClickAction(item.Tag.ToString());
} // }
} // }
} // }
} //}
#endif