OSDN Git Service

OpenTween v2.4.2 リリース
[opentween/open-tween.git] / OpenTween / ShortcutCommand.cs
index 8806011..86305e3 100644 (file)
@@ -19,6 +19,8 @@
 // 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;
@@ -41,53 +43,37 @@ namespace OpenTween
     /// </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>
@@ -114,33 +100,27 @@ namespace OpenTween
         /// コマンドを実行します
         /// </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;
             }
 
@@ -149,7 +129,7 @@ namespace OpenTween
             /// </summary>
             public Builder FocusedOn(FocusedControl focusedOn)
             {
-                this.instance.focusedOn = focusedOn;
+                this.instance.FocusedOn = focusedOn;
                 return this;
             }
 
@@ -158,7 +138,7 @@ namespace OpenTween
             /// </summary>
             public Builder NotFocusedOn(FocusedControl notFocusedOn)
             {
-                this.instance.notFocusedOn = notFocusedOn;
+                this.instance.NotFocusedOn = notFocusedOn;
                 return this;
             }
 
@@ -175,9 +155,7 @@ namespace OpenTween
             /// ショートカットが入力された時に行う動作の内容
             /// </summary>
             public ShortcutCommand Do(Action action, bool preventDefault = true)
-            {
-                return this.Do(SynchronousTask(action), preventDefault);
-            }
+                => this.Do(SynchronousTask(action), preventDefault);
 
             /// <summary>
             /// ショートカットが入力された時に行う動作の内容
@@ -185,21 +163,16 @@ namespace OpenTween
             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&lt;Task&gt; に変換します
             /// </summary>
             private static Func<Task> SynchronousTask(Action action)
-            {
-                return () => { action(); return noOpTask; };
-            }
+                => () => { action(); return Task.CompletedTask; };
         }
     }
 }