OSDN Git Service

不要な ListView.SelectedIndices への参照を削除
[opentween/open-tween.git] / OpenTween / ControlTransaction.cs
index 99f37a4..2031159 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;
@@ -31,41 +33,57 @@ namespace OpenTween
     /// <summary>
     /// WinformsコントロールのBegin/EndUpdate等のメソッドをusingブロックによって呼び出すためのクラス
     /// </summary>
-    public class ControlTransaction
+    public static class ControlTransaction
     {
         public static IDisposable Update(ListView control)
-        {
-            return new Transaction<ListView>(control, x => x.BeginUpdate(), x => x.EndUpdate());
-        }
+            => new Transaction<ListView>(control, x => x.BeginUpdate(), x => x.EndUpdate());
 
         public static IDisposable Update(ListBox control)
-        {
-            return new Transaction<ListBox>(control, x => x.BeginUpdate(), x => x.EndUpdate());
-        }
+            => new Transaction<ListBox>(control, x => x.BeginUpdate(), x => x.EndUpdate());
 
         public static IDisposable Update(ComboBox control)
-        {
-            return new Transaction<ComboBox>(control, x => x.BeginUpdate(), x => x.EndUpdate());
-        }
+            => new Transaction<ComboBox>(control, x => x.BeginUpdate(), x => x.EndUpdate());
 
         public static IDisposable Update(TreeView control)
+            => new Transaction<TreeView>(control, x => x.BeginUpdate(), x => x.EndUpdate());
+
+        public static IDisposable Update(Control control)
         {
-            return new Transaction<TreeView>(control, x => x.BeginUpdate(), x => x.EndUpdate());
+            // Begin/EndUpdate メソッドを持たないコントロールに対しては、
+            // WM_SETREDRAW メッセージを直接コントロールに送信する。
+            return new Transaction<Control>(control,
+                x => NativeMethods.SetRedrawState(x, false),
+                x => { NativeMethods.SetRedrawState(x, true); x.Invalidate(true); });
         }
 
         public static IDisposable Layout(Control control)
+            => Layout(control, performLayout: true);
+
+        public static IDisposable Layout(Control control, bool performLayout)
+            => new Transaction<Control>(control, x => x.SuspendLayout(), x => x.ResumeLayout(performLayout));
+
+        public static IDisposable Init(ISupportInitialize control)
+            => new Transaction<ISupportInitialize>(control, x => x.BeginInit(), x => x.EndInit());
+
+        public static IDisposable Cursor(Control control, Cursor newCursor)
         {
-            return Layout(control, performLayout: true);
+            var oldCursor = control.Cursor;
+
+            return new Transaction<Control>(control, x => x.Cursor = newCursor, x => x.Cursor = oldCursor);
         }
 
-        public static IDisposable Layout(Control control, bool performLayout)
+        public static IDisposable Enabled(Control control)
         {
-            return new Transaction<Control>(control, x => x.SuspendLayout(), x => x.ResumeLayout(performLayout));
+            var oldState = control.Enabled;
+
+            return new Transaction<Control>(control, x => x.Enabled = true, x => x.Enabled = oldState);
         }
 
-        public static IDisposable Init(ISupportInitialize control)
+        public static IDisposable Disabled(Control control)
         {
-            return new Transaction<ISupportInitialize>(control, x => x.BeginInit(), x => x.EndInit());
+            var oldState = control.Enabled;
+
+            return new Transaction<Control>(control, x => x.Enabled = false, x => x.Enabled = oldState);
         }
 
         private class Transaction<T> : IDisposable
@@ -86,9 +104,7 @@ namespace OpenTween
             }
 
             public void Dispose()
-            {
-                this.endTransaction(this.control);
-            }
+                => this.endTransaction(this.control);
         }
     }
 }