// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
// Boston, MA 02110-1301, USA.
+#nullable enable
+
using System;
using System.Collections.Generic;
using System.Linq;
/// </summary>
public class ShortcutCommand
{
- private Keys[] shortcuts;
- private FocusedControl focusedOn;
- private FocusedControl notFocusedOn;
private Func<bool> onlyWhen;
private Func<Task> command;
- private bool preventDefault;
/// <summary>
/// ショートカットキーが動作する条件となるキー入力
/// </summary>
- public Keys[] Shortcuts
- {
- get { return this.shortcuts; }
- }
+ public Keys[] Shortcuts { get; private set; }
/// <summary>
/// ショートカットキーが動作する条件となるフォーカス状態
/// </summary>
- public FocusedControl FocusedOn
- {
- get { return this.focusedOn; }
- }
+ public FocusedControl FocusedOn { get; private set; }
/// <summary>
/// ショートカットキーが動作する否定条件となるフォーカス状態
/// </summary>
- public FocusedControl NotFocusedOn
- {
- get { return this.notFocusedOn; }
- }
+ public FocusedControl NotFocusedOn { get; private set; }
/// <summary>
/// コマンドを実行した後、コントロール既定の動作を無効化するか否か (デフォルトは true)
/// </summary>
- public bool PreventDefault
- {
- get { return this.preventDefault; }
- }
+ public bool PreventDefault { get; private set; }
private ShortcutCommand()
{
- this.shortcuts = new Keys[0];
- this.command = () => Task.FromResult(0);
+ this.Shortcuts = Array.Empty<Keys>();
+ this.command = () => Task.CompletedTask;
this.onlyWhen = () => true;
- this.focusedOn = FocusedControl.None;
- this.notFocusedOn = FocusedControl.None;
- this.preventDefault = true;
+ this.FocusedOn = FocusedControl.None;
+ this.NotFocusedOn = FocusedControl.None;
+ this.PreventDefault = true;
}
/// <summary>
/// コマンドを実行します
/// </summary>
public async Task RunCommand()
- {
- await this.command();
- }
+ => await this.command();
/// <summary>
/// 新規に ShortcutCommand インスタンスを作成するビルダーを返します
/// </summary>
public static ShortcutCommand.Builder Create(params Keys[] shortcuts)
- {
- return new Builder().Keys(shortcuts);
- }
+ => new Builder().Keys(shortcuts);
public class Builder
{
private readonly ShortcutCommand instance;
internal Builder()
- {
- this.instance = new ShortcutCommand();
- }
+ => this.instance = new ShortcutCommand();
/// <summary>
/// 指定されたキーが入力された時にショートカットを発動します
/// </summary>
public Builder Keys(params Keys[] shortcuts)
{
- this.instance.shortcuts = shortcuts;
+ this.instance.Shortcuts = shortcuts;
return this;
}
/// </summary>
public Builder FocusedOn(FocusedControl focusedOn)
{
- this.instance.focusedOn = focusedOn;
+ this.instance.FocusedOn = focusedOn;
return this;
}
/// </summary>
public Builder NotFocusedOn(FocusedControl notFocusedOn)
{
- this.instance.notFocusedOn = notFocusedOn;
+ this.instance.NotFocusedOn = notFocusedOn;
return this;
}
/// ショートカットが入力された時に行う動作の内容
/// </summary>
public ShortcutCommand Do(Action action, bool preventDefault = true)
- {
- return this.Do(SynchronousTask(action), preventDefault);
- }
+ => this.Do(SynchronousTask(action), preventDefault);
/// <summary>
/// ショートカットが入力された時に行う動作の内容
public ShortcutCommand Do(Func<Task> action, bool preventDefault = true)
{
this.instance.command = action;
- this.instance.preventDefault = preventDefault;
+ this.instance.PreventDefault = preventDefault;
return this.instance;
}
- /// <summary>何もしないタスク</summary>
- private static Task noOpTask = Task.FromResult(0);
-
/// <summary>
/// Action を Func<Task> に変換します
/// </summary>
private static Func<Task> SynchronousTask(Action action)
- {
- return () => { action(); return noOpTask; };
- }
+ => () => { action(); return Task.CompletedTask; };
}
}
}