-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6014db8
commit cb268a9
Showing
4 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Windows.Forms; | ||
|
||
namespace Ched.UI.Shortcuts | ||
{ | ||
public interface IShortcutCommandSource | ||
{ | ||
/// <summary> | ||
/// 指定のコマンドを実行します。 | ||
/// </summary> | ||
/// <param name="command">実行するコマンド</param> | ||
/// <returns>コマンドが実行された場合はTrue</returns> | ||
bool ExecuteCommand(string command); | ||
|
||
bool ResolveCommandName(string command, out string name); | ||
} | ||
|
||
public class NullShortcutCommandSource : IShortcutCommandSource | ||
{ | ||
// Do nothing | ||
public bool ExecuteCommand(string command) => true; | ||
|
||
public bool ResolveCommandName(string command, out string name) | ||
{ | ||
name = null; | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Windows.Forms; | ||
|
||
namespace Ched.UI.Shortcuts | ||
{ | ||
public interface IShortcutKeySource | ||
{ | ||
/// <summary> | ||
/// 指定のショートカットキーに対応するコマンドを取得します。 | ||
/// </summary> | ||
/// <param name="key">コマンドを取得するショートカットキー</param> | ||
/// <param name="command">ショートカットキーに対応するコマンド</param> | ||
/// <returns>ショートカットキーに対応するコマンドが存在すればTrue</returns> | ||
bool ResolveCommand(Keys key, out string command); | ||
|
||
/// <summary> | ||
/// 指定のコマンドに対応するショートカットキーを取得します。 | ||
/// </summary> | ||
/// <param name="command">ショートカットキーを取得するコマンド</param> | ||
/// <param name="key">コマンドに対応するショートカットキー</param> | ||
/// <returns>コマンドに対応するショートカットキーが存在すればTrue</returns> | ||
bool ResolveShortcutKey(string command, out Keys key); | ||
} | ||
|
||
public class NullShortcutKeySource : IShortcutKeySource | ||
{ | ||
public bool ResolveCommand(Keys key, out string command) | ||
{ | ||
command = null; | ||
return false; | ||
} | ||
|
||
public bool ResolveShortcutKey(string command, out Keys key) | ||
{ | ||
key = Keys.None; | ||
return false; | ||
} | ||
} | ||
|
||
public abstract class ShortcutKeySource : IShortcutKeySource | ||
{ | ||
private Dictionary<Keys, string> KeyMap { get; } = new Dictionary<Keys, string>(); | ||
private Dictionary<string, HashSet<Keys>> CommandMap { get; } = new Dictionary<string, HashSet<Keys>>(); | ||
|
||
public IEnumerable<Keys> ShortcutKeys => KeyMap.Keys; | ||
|
||
public ShortcutKeySource() | ||
{ | ||
} | ||
|
||
protected ShortcutKeySource(ShortcutKeySource other) | ||
{ | ||
foreach (var item in other.KeyMap) | ||
{ | ||
RegisterShortcut(item.Value, item.Key); | ||
} | ||
} | ||
|
||
protected void RegisterShortcut(string command, Keys key) | ||
{ | ||
// キーの重複を確認。コマンドは重複してもよい(異なるキーから同じコマンドを呼び出しても問題ない) | ||
if (KeyMap.ContainsKey(key)) throw new InvalidOperationException("The shortcut key has already been registered."); | ||
KeyMap.Add(key, command); | ||
if (!CommandMap.ContainsKey(command)) CommandMap.Add(command, new HashSet<Keys>()); | ||
CommandMap[command].Add(key); | ||
} | ||
|
||
public bool ResolveCommand(Keys key, out string command) => KeyMap.TryGetValue(key, out command); | ||
|
||
public bool ResolveShortcutKey(string command, out Keys key) | ||
{ | ||
key = Keys.None; | ||
if (!CommandMap.TryGetValue(command, out HashSet<Keys> keys)) return false; | ||
if (keys.Count > 0) | ||
{ | ||
key = keys.First(); | ||
return true; | ||
} | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Windows.Forms; | ||
|
||
namespace Ched.UI.Shortcuts | ||
{ | ||
public class ShortcutManager | ||
{ | ||
public IShortcutCommandSource CommandSource { get; set; } = new NullShortcutCommandSource(); | ||
public IShortcutKeySource DefaultKeySource { get; set; } = new NullShortcutKeySource(); | ||
public IShortcutKeySource UserKeySource { get; set; } = new NullShortcutKeySource(); | ||
|
||
public bool ExecuteCommand(Keys key) | ||
{ | ||
bool Resolve(IShortcutKeySource source) | ||
{ | ||
if (source.ResolveCommand(key, out string command)) | ||
{ | ||
return CommandSource.ExecuteCommand(command); | ||
} | ||
return false; | ||
} | ||
|
||
// User, Defaultの順にトラバースしてひっかける | ||
return Resolve(UserKeySource) || Resolve(DefaultKeySource); | ||
} | ||
|
||
public bool ResolveShortcutKey(string command, out Keys key) | ||
{ | ||
return UserKeySource.ResolveShortcutKey(command, out key) || DefaultKeySource.ResolveShortcutKey(command, out key); | ||
} | ||
} | ||
} |