207 lines
7.2 KiB
C#
207 lines
7.2 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Reflection;
|
|||
|
|
|
|||
|
|
using Autodesk.Revit.UI;
|
|||
|
|
|
|||
|
|
|
|||
|
|
using UIFramework;
|
|||
|
|
using UIFrameworkServices;
|
|||
|
|
|
|||
|
|
namespace Sai.Toolkit.Revit.Helpers
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 通过命令id设置快捷键
|
|||
|
|
/// </summary>
|
|||
|
|
public class KeyboardShortcutService
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 设置菜单快捷键
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="id"></param>
|
|||
|
|
/// <param name="shortcutsRep"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public bool SetRibbonItemShortcut(string id, string shortcutsRep)
|
|||
|
|
{
|
|||
|
|
Dictionary<string, UIFrameworkServices.ShortcutItem> dictionary = this.LoadShortcuts();
|
|||
|
|
if (this.Commands.TryGetValue(id, out UIFrameworkServices.ShortcutItem shortcutItem))
|
|||
|
|
{
|
|||
|
|
shortcutItem.ShortcutsRep = shortcutsRep;
|
|||
|
|
shortcutItem.Shortcuts.Clear();
|
|||
|
|
//多个快捷键中间以#分割
|
|||
|
|
foreach (string text in shortcutsRep.Split('#'))
|
|||
|
|
{
|
|||
|
|
shortcutItem.Shortcuts.Add(text);
|
|||
|
|
}
|
|||
|
|
dictionary.Add(id, shortcutItem);
|
|||
|
|
List<UIFrameworkServices.ShortcutItem> list = dictionary.Select((KeyValuePair<string, UIFrameworkServices.ShortcutItem> e) => e.Value).ToList<UIFrameworkServices.ShortcutItem>();
|
|||
|
|
this.SaveShortcuts(list);
|
|||
|
|
this.ArmCommands();
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 通过外部命令类设置快捷键
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="id"></param>
|
|||
|
|
/// <param name="shortcutsRep"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public bool SetRibbonItemShortcut<T>() where T :IExternalCommand
|
|||
|
|
{
|
|||
|
|
var attribute=typeof(T).GetCustomAttribute<ShortcutRepAttribute>();
|
|||
|
|
if (attribute==null)
|
|||
|
|
{
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
Dictionary<string, UIFrameworkServices.ShortcutItem> dictionary = this.LoadShortcuts();
|
|||
|
|
foreach (var item in Commands)
|
|||
|
|
{
|
|||
|
|
if (item.Key.EndsWith(nameof(T)))
|
|||
|
|
{
|
|||
|
|
ShortcutItem shortcutItem = item.Value;
|
|||
|
|
shortcutItem.ShortcutsRep = attribute.Shortcuts;
|
|||
|
|
shortcutItem.Shortcuts.Clear();
|
|||
|
|
//多个快捷键中间以#分割
|
|||
|
|
foreach (string text in attribute.Shortcuts.Split('#'))
|
|||
|
|
{
|
|||
|
|
shortcutItem.Shortcuts.Add(text);
|
|||
|
|
}
|
|||
|
|
dictionary.Add(item.Key, shortcutItem);
|
|||
|
|
List<UIFrameworkServices.ShortcutItem> list = dictionary.Select((KeyValuePair<string, UIFrameworkServices.ShortcutItem> e) => e.Value).ToList<UIFrameworkServices.ShortcutItem>();
|
|||
|
|
this.SaveShortcuts(list);
|
|||
|
|
this.ArmCommands();
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 移除菜单快捷键
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="id">命令id</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public bool RemoveRibbonItemShortcut(string id)
|
|||
|
|
{
|
|||
|
|
Dictionary<string, UIFrameworkServices.ShortcutItem> dictionary = this.LoadShortcuts();
|
|||
|
|
UIFrameworkServices.ShortcutItem shortcutItem;
|
|||
|
|
if (dictionary.TryGetValue(id, out shortcutItem))
|
|||
|
|
{
|
|||
|
|
dictionary.Remove(id);
|
|||
|
|
List<UIFrameworkServices.ShortcutItem> list = dictionary.Select((KeyValuePair<string, UIFrameworkServices.ShortcutItem> e) => e.Value).ToList<UIFrameworkServices.ShortcutItem>();
|
|||
|
|
this.SaveShortcuts(list);
|
|||
|
|
this.ArmCommands();
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 是否具有菜单快捷键
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="id"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public bool HasRibbonItemShortcut(string id)
|
|||
|
|
{
|
|||
|
|
return this.LoadShortcuts().TryGetValue(id, out var _);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取快捷键的映射(最多五个按键)
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="id">CommandId</param>
|
|||
|
|
/// <returns>快捷键</returns>
|
|||
|
|
public string GetRibbonItemShortcutsRep(string id)
|
|||
|
|
{
|
|||
|
|
if (this.LoadShortcuts().TryGetValue(id, out var shortcutItem))
|
|||
|
|
{
|
|||
|
|
return shortcutItem.ShortcutsRep;
|
|||
|
|
}
|
|||
|
|
return "";
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取全部命令快捷键CommandId-ObservableCollect<string>()
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public Dictionary<string, IEnumerable<string>> GetCommandShortcuts()
|
|||
|
|
{
|
|||
|
|
return (from e in this.Commands
|
|||
|
|
select e.Value into e
|
|||
|
|
group e by e.CommandId).ToDictionary((IGrouping<string, UIFrameworkServices.ShortcutItem> i) => i.Key, (IGrouping<string, UIFrameworkServices.ShortcutItem> j) => j.SelectMany((UIFrameworkServices.ShortcutItem i) => i.Shortcuts));
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取自定义快捷键被关联到的命令CommandId
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="shortcut">自定义的快捷键</param>
|
|||
|
|
/// <returns>CommandId</returns>
|
|||
|
|
public string GetCommandShortcut(string shortcut)
|
|||
|
|
{
|
|||
|
|
return this.Commands.FirstOrDefault((KeyValuePair<string, UIFrameworkServices.ShortcutItem> e) => e.Value.Shortcuts.Contains(shortcut)).Key;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 自定义的快捷键是否已经被关联到某个命令上
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="shortcut">自定义的快捷键</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public bool HasCommandShortcut(string shortcut)
|
|||
|
|
{
|
|||
|
|
return this.GetCommandShortcuts().Any((KeyValuePair<string, IEnumerable<string>> e) => e.Value.Contains(shortcut));
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 加载命令
|
|||
|
|
/// </summary>
|
|||
|
|
public void LoadCommands()
|
|||
|
|
{
|
|||
|
|
ShortcutsHelper.LoadCommands();
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 应用快捷键更改
|
|||
|
|
/// </summary>
|
|||
|
|
public void ApplyShortcutChanges()
|
|||
|
|
{
|
|||
|
|
UIFrameworkServices.KeyboardShortcutService.applyShortcutChanges(this.LoadShortcuts());
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 应用更改
|
|||
|
|
/// </summary>
|
|||
|
|
private void ArmCommands()
|
|||
|
|
{
|
|||
|
|
ShortcutsHelper.ArmCommands();
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取已经加载的ShortcutItems
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
private Dictionary<string, UIFrameworkServices.ShortcutItem> LoadShortcuts()
|
|||
|
|
{
|
|||
|
|
return ShortcutsHelper.LoadShortcuts();
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 保存ShortcutItems
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="shortcutList"></param>
|
|||
|
|
private void SaveShortcuts(ICollection<UIFrameworkServices.ShortcutItem> shortcutList)
|
|||
|
|
{
|
|||
|
|
ShortcutsHelper.SaveShortcuts(shortcutList);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 所有快捷命令条目字典
|
|||
|
|
/// </summary>
|
|||
|
|
private Dictionary<string, UIFrameworkServices.ShortcutItem> Commands
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
return ShortcutsHelper.Commands;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取所有ShortcutItems
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns>ShortcutItems</returns>
|
|||
|
|
public IEnumerable<UIFrameworkServices.ShortcutItem> GetShortcutItems()
|
|||
|
|
{
|
|||
|
|
return from e in this.LoadShortcuts()
|
|||
|
|
select new UIFrameworkServices.ShortcutItem(e.Value);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|