JD2022-TU1/main/tools/framework/JD.WPF/Models/Menus/MenuItemData.cs

131 lines
4 KiB
C#

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using JD.Interfaces;
using JD.Interfaces.Models;
namespace JD.WPF.Models.Menus
{
public class MenuItemData : DataModelBase, IMenuItemData
{
private readonly MenuItemData _parent;
private readonly DataTemplate _iconTemplate;
private bool _isCheckable;
private bool _isChecked;
public string Name { get; }
public object Data { get; set; }
public IEnumerable<IMenuItemData> Children
{
get { return ChildrenCollection; }
}
public MenuItemDataCollection ChildrenCollection { get; }
public IMenuItemData Parent
{
get { return _parent; }
}
public ImageSource Icon { get; }
public Uri IconUri { get; }
public string IconTemplateName { get; }
public DataTemplate IconTemplate
{
get { return _iconTemplate; }
}
public ICommand Command { get; }
public string InputGestureText { get; }
public IKeyWithModifier InputGesture { get; }
public bool HasIcon
{
get { return Icon != null; }
}
public bool HasIconTemplate
{
get { return IconTemplateName != null; }
}
public bool IsSeparator
{
get { return string.IsNullOrEmpty(Name); }
}
public bool IsCheckable
{
get { return _isCheckable; }
set
{
if (_isCheckable != value)
{
_isCheckable = value;
RaisePropertyChanged("IsCheckable");
}
}
}
public bool IsChecked
{
get { return _isChecked; }
set
{
if (_isChecked != value)
{
_isChecked = value;
RaisePropertyChanged("IsChecked");
}
}
}
public MenuItemData(string name, MenuItemData parent, ICommand command = null, Uri iconUri = null, string iconTemplateName = null, bool addToParent = true, object data = null, string inputGestureText = null, IKeyWithModifier inputGesture = null)
{
Name = name;
Data = data;
_parent = parent;
ChildrenCollection = new MenuItemDataCollection();
Icon = null;
IconTemplateName = null;
if (iconUri != null)
{
IconUri = iconUri;
Icon = new BitmapImage(iconUri);
}
else if (iconTemplateName != null)
{
IconTemplateName = iconTemplateName;
ResourceManager.Instance.LoadResourceFromDictionary("JD.WPF", "Resources.xaml", IconTemplateName, out _iconTemplate);
}
Command = command;
InputGesture = null;
InputGestureText = null;
if (inputGesture != null)
{
// TODO : Find a way to link command shortcuts and menu input gesture text.
InputGesture = inputGesture;
InputGestureText = "";
if ((InputGesture.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
InputGestureText += "Ctrl+";
if ((InputGesture.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt)
InputGestureText += "Alt+";
if ((InputGesture.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
InputGestureText += "Shift+";
InputGestureText += InputGesture.Key.ToString();
}
else if (inputGestureText != null)
{
InputGestureText = inputGestureText;
}
if (addToParent && _parent != null)
_parent.ChildrenCollection.Add(this);
}
}
}