OSDN Git Service

OTPictureBox.SetImageFromTaskメソッドで無視する対象の例外にIOExceptionを追加 (thx @sobachanko!)
[opentween/open-tween.git] / OpenTween / OTPictureBox.cs
index bbbc5be..2e9c290 100644 (file)
@@ -32,6 +32,7 @@ using System.Threading;
 using System.Net;
 using System.Net.Http;
 using System.IO;
+using OpenTween.Thumbnail;
 
 namespace OpenTween
 {
@@ -45,7 +46,7 @@ namespace OpenTween
             set
             {
                 this.memoryImage = value;
-                base.Image = value != null ? value.Image : null;
+                base.Image = value?.Image;
 
                 this.RestoreSizeMode();
             }
@@ -95,24 +96,38 @@ namespace OpenTween
             base.SizeMode = this.currentSizeMode;
         }
 
+        /// <summary>
+        /// SetImageFromTask メソッドを連続で呼び出した際に設定される画像が前後するのを防ぐため、
+        /// 現在進行中の Task を表す Id を記憶しておくためのフィールド
+        /// </summary>
+        private int currentImageTaskId = 0;
+
         public async Task SetImageFromTask(Func<Task<MemoryImage>> imageTask)
         {
+            var id = Interlocked.Increment(ref this.currentImageTaskId);
+
             try
             {
                 this.ShowInitialImage();
-                this.Image = await imageTask();
+
+                var image = await imageTask();
+
+                if (id == this.currentImageTaskId)
+                    this.Image = image;
             }
             catch (Exception)
             {
-                this.ShowErrorImage();
+                if (id == this.currentImageTaskId)
+                    this.ShowErrorImage();
                 try
                 {
                     throw;
                 }
                 catch (HttpRequestException) { }
                 catch (InvalidImageException) { }
-                catch (TaskCanceledException) { }
+                catch (OperationCanceledException) { }
                 catch (WebException) { }
+                catch (IOException) { }
             }
         }
 
@@ -121,6 +136,9 @@ namespace OpenTween
             try
             {
                 base.OnPaint(pe);
+
+                // 動画なら再生ボタンを上から描画
+                DrawPlayableMark(pe);
             }
             catch (ExternalException)
             {
@@ -130,13 +148,31 @@ namespace OpenTween
             }
         }
 
+        private void DrawPlayableMark(PaintEventArgs pe)
+        {
+            var thumb = this.Tag as ThumbnailInfo;
+            if (thumb == null || !thumb.IsPlayable) return;
+            if (base.Image == base.InitialImage || base.Image == base.ErrorImage) return;
+
+            var overlayImage = Properties.Resources.PlayableOverlayImage;
+
+            var overlaySize = Math.Min(this.Width, this.Height) / 4;
+            var destRect = new Rectangle(
+                (this.Width - overlaySize) / 2,
+                (this.Height - overlaySize) / 2,
+                overlaySize,
+                overlaySize);
+
+            pe.Graphics.DrawImage(overlayImage, destRect, 0, 0, overlayImage.Width, overlayImage.Height, GraphicsUnit.Pixel);
+        }
+
         [Browsable(false)]
         [EditorBrowsable(EditorBrowsableState.Never)]
         [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
         public new string ImageLocation
         {
-            get { throw new NotSupportedException(); }
-            set { throw new NotSupportedException(); }
+            get { return null; }
+            set { }
         }
 
         [EditorBrowsable(EditorBrowsableState.Never)]