|
@@ -80,19 +80,33 @@ namespace InABox.WPF
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private static MenuItem DoAddMenuItem(ItemsControl menu, string caption, Bitmap? image, bool enabled)
|
|
|
+ #region Menu Utils
|
|
|
+
|
|
|
+ private static void ItemsControlInsert(ItemsControl menu, FrameworkElement item, int index)
|
|
|
+ {
|
|
|
+ if (index != -1)
|
|
|
+ {
|
|
|
+ menu.Items.Insert(index, item);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ menu.Items.Add(item);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static MenuItem DoAddMenuItem(ItemsControl menu, string caption, Bitmap? image, bool enabled, int index = -1)
|
|
|
{
|
|
|
var item = new MenuItem { Header = caption, IsEnabled = enabled };
|
|
|
if (image != null)
|
|
|
item.Icon = new Image() { Source = enabled ? image.AsBitmapImage(24, 24) : image.AsGrayScale().AsBitmapImage(24, 24) };
|
|
|
|
|
|
- menu.Items.Add(item);
|
|
|
+ ItemsControlInsert(menu, item, index);
|
|
|
return item;
|
|
|
}
|
|
|
|
|
|
- private static MenuItem DoAddMenuItem(ItemsControl menu, string caption, Bitmap? image, Action? click, bool enabled)
|
|
|
+ private static MenuItem DoAddMenuItem(ItemsControl menu, string caption, Bitmap? image, Action? click, bool enabled, int index = -1)
|
|
|
{
|
|
|
- var item = DoAddMenuItem(menu, caption, image, enabled);
|
|
|
+ var item = DoAddMenuItem(menu, caption, image, enabled, index);
|
|
|
|
|
|
if (click != null)
|
|
|
{
|
|
@@ -104,9 +118,9 @@ namespace InABox.WPF
|
|
|
|
|
|
return item;
|
|
|
}
|
|
|
- private static MenuItem DoAddMenuItem<T>(ItemsControl menu, string caption, Bitmap? image, T tag, Action<T> click, bool enabled)
|
|
|
+ private static MenuItem DoAddMenuItem<T>(ItemsControl menu, string caption, Bitmap? image, T tag, Action<T> click, bool enabled, int index = -1)
|
|
|
{
|
|
|
- var item = DoAddMenuItem(menu, caption, image, enabled);
|
|
|
+ var item = DoAddMenuItem(menu, caption, image, enabled, index);
|
|
|
|
|
|
item.Tag = tag;
|
|
|
item.Click += (o, e) =>
|
|
@@ -119,7 +133,7 @@ namespace InABox.WPF
|
|
|
|
|
|
public delegate void CheckToggleAction<T>(T tag, bool isChecked);
|
|
|
|
|
|
- private static MenuItem DoAddCheckItem<T>(ItemsControl menu, string caption, T tag, CheckToggleAction<T> click, bool isChecked, bool enabled)
|
|
|
+ private static MenuItem DoAddCheckItem<T>(ItemsControl menu, string caption, T tag, CheckToggleAction<T> click, bool isChecked, bool enabled, int index = -1)
|
|
|
{
|
|
|
var item = new MenuItem { Header = caption, IsEnabled = enabled, IsCheckable = true, IsChecked = isChecked };
|
|
|
|
|
@@ -129,41 +143,46 @@ namespace InABox.WPF
|
|
|
click((T)(o as MenuItem)!.Tag, item.IsChecked);
|
|
|
};
|
|
|
|
|
|
- menu.Items.Add(item);
|
|
|
+ ItemsControlInsert(menu, item, index);
|
|
|
return item;
|
|
|
}
|
|
|
|
|
|
- public static Separator AddSeparator(this ContextMenu menu)
|
|
|
+ public static Separator AddSeparator(this ContextMenu menu, int index = -1)
|
|
|
{
|
|
|
var separator = new Separator();
|
|
|
|
|
|
- menu.Items.Add(separator);
|
|
|
+ ItemsControlInsert(menu, separator, index);
|
|
|
|
|
|
return separator;
|
|
|
}
|
|
|
- public static Separator? AddSeparatorIfNeeded(this ContextMenu menu)
|
|
|
+ public static Separator? AddSeparatorIfNeeded(this ContextMenu menu, int index = -1)
|
|
|
{
|
|
|
if (menu.Items.Count == 0) return null;
|
|
|
|
|
|
- var last = menu.Items[menu.Items.Count - 1];
|
|
|
+ var lastIndex = index != -1 ? index - 1 : menu.Items.Count - 1;
|
|
|
+ if (lastIndex < 0 || lastIndex >= menu.Items.Count) return null;
|
|
|
+
|
|
|
+ var last = menu.Items[lastIndex];
|
|
|
if (last is Separator) return null;
|
|
|
|
|
|
var separator = new Separator();
|
|
|
|
|
|
- menu.Items.Add(separator);
|
|
|
+ ItemsControlInsert(menu, separator, index);
|
|
|
|
|
|
return separator;
|
|
|
}
|
|
|
|
|
|
- public static MenuItem AddItem(this ContextMenu menu, string caption, Bitmap? image, Action? click, bool enabled = true)
|
|
|
- => DoAddMenuItem(menu, caption, image, click, enabled);
|
|
|
- public static MenuItem AddItem(this MenuItem menu, string caption, Bitmap? image, Action? click, bool enabled = true)
|
|
|
- => DoAddMenuItem(menu, caption, image, click, enabled);
|
|
|
- public static MenuItem AddItem<T>(this ContextMenu menu, string caption, Bitmap? image, T tag, Action<T> click, bool enabled = true)
|
|
|
- => DoAddMenuItem(menu, caption, image, tag, click, enabled);
|
|
|
- public static MenuItem AddItem<T>(this MenuItem menu, string caption, Bitmap? image, T tag, Action<T> click, bool enabled = true)
|
|
|
- => DoAddMenuItem(menu, caption, image, tag, click, enabled);
|
|
|
- public static MenuItem AddCheckItem<T>(this ContextMenu menu, string caption, T tag, CheckToggleAction<T> click, bool isChecked = false, bool enabled = true)
|
|
|
- => DoAddCheckItem<T>(menu, caption, tag, click, isChecked, enabled);
|
|
|
+ public static MenuItem AddItem(this ContextMenu menu, string caption, Bitmap? image, Action? click, bool enabled = true, int index = -1)
|
|
|
+ => DoAddMenuItem(menu, caption, image, click, enabled, index);
|
|
|
+ public static MenuItem AddItem(this MenuItem menu, string caption, Bitmap? image, Action? click, bool enabled = true, int index = -1)
|
|
|
+ => DoAddMenuItem(menu, caption, image, click, enabled, index);
|
|
|
+ public static MenuItem AddItem<T>(this ContextMenu menu, string caption, Bitmap? image, T tag, Action<T> click, bool enabled = true, int index = -1)
|
|
|
+ => DoAddMenuItem(menu, caption, image, tag, click, enabled, index);
|
|
|
+ public static MenuItem AddItem<T>(this MenuItem menu, string caption, Bitmap? image, T tag, Action<T> click, bool enabled = true, int index = -1)
|
|
|
+ => DoAddMenuItem(menu, caption, image, tag, click, enabled, index);
|
|
|
+ public static MenuItem AddCheckItem<T>(this ContextMenu menu, string caption, T tag, CheckToggleAction<T> click, bool isChecked = false, bool enabled = true, int index = -1)
|
|
|
+ => DoAddCheckItem<T>(menu, caption, tag, click, isChecked, enabled, index);
|
|
|
+
|
|
|
+ #endregion
|
|
|
}
|
|
|
}
|