1 // OpenTween - Client of Twitter
2 // Copyright (c) 2015 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
5 // This file is part of OpenTween.
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General public License
17 // You should have received a copy of the GNU General public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
23 using System.Collections.Generic;
26 using System.Threading.Tasks;
27 using System.Windows.Forms;
31 public enum FocusedControl
40 /// ショートカットキーの条件と動作を定義するクラス
42 public class ShortcutCommand
44 private Func<bool> onlyWhen;
45 private Func<Task> command;
48 /// ショートカットキーが動作する条件となるキー入力
50 public Keys[] Shortcuts { get; private set; }
53 /// ショートカットキーが動作する条件となるフォーカス状態
55 public FocusedControl FocusedOn { get; private set; }
58 /// ショートカットキーが動作する否定条件となるフォーカス状態
60 public FocusedControl NotFocusedOn { get; private set; }
63 /// コマンドを実行した後、コントロール既定の動作を無効化するか否か (デフォルトは true)
65 public bool PreventDefault { get; private set; }
67 private ShortcutCommand()
69 this.Shortcuts = new Keys[0];
70 this.command = () => Task.FromResult(0);
71 this.onlyWhen = () => true;
72 this.FocusedOn = FocusedControl.None;
73 this.NotFocusedOn = FocusedControl.None;
74 this.PreventDefault = true;
78 /// コマンドを実行する条件を満たしているか判定します
80 public bool IsMatch(Keys pressedKey, FocusedControl focusedOn)
82 if (!this.Shortcuts.Contains(pressedKey))
85 if (this.FocusedOn != FocusedControl.None && this.FocusedOn != focusedOn)
88 if (this.NotFocusedOn != FocusedControl.None && this.NotFocusedOn == focusedOn)
100 public async Task RunCommand()
102 await this.command();
106 /// 新規に ShortcutCommand インスタンスを作成するビルダーを返します
108 public static ShortcutCommand.Builder Create(params Keys[] shortcuts)
110 return new Builder().Keys(shortcuts);
115 private readonly ShortcutCommand instance;
119 this.instance = new ShortcutCommand();
123 /// 指定されたキーが入力された時にショートカットを発動します
125 public Builder Keys(params Keys[] shortcuts)
127 this.instance.Shortcuts = shortcuts;
132 /// 指定されたコントロールにフォーカスが当たっている時のみショートカットを有効にします
134 public Builder FocusedOn(FocusedControl focusedOn)
136 this.instance.FocusedOn = focusedOn;
141 /// 指定されたコントロールにフォーカスが当たっている時はショートカットを有効にしません
143 public Builder NotFocusedOn(FocusedControl notFocusedOn)
145 this.instance.NotFocusedOn = notFocusedOn;
150 /// 指定された条件が true になる間のみショートカットを有効にします
152 public Builder OnlyWhen(Func<bool> condition)
154 this.instance.onlyWhen = condition;
159 /// ショートカットが入力された時に行う動作の内容
161 public ShortcutCommand Do(Action action, bool preventDefault = true)
163 return this.Do(SynchronousTask(action), preventDefault);
167 /// ショートカットが入力された時に行う動作の内容
169 public ShortcutCommand Do(Func<Task> action, bool preventDefault = true)
171 this.instance.command = action;
172 this.instance.PreventDefault = preventDefault;
174 return this.instance;
177 /// <summary>何もしないタスク</summary>
178 private static Task noOpTask = Task.FromResult(0);
181 /// Action を Func<Task> に変換します
183 private static Func<Task> SynchronousTask(Action action)
185 return () => { action(); return noOpTask; };