OSDN Git Service

usingの順序を揃える (SA1208, SA1210)
[opentween/open-tween.git] / OpenTween / OTPictureBox.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2013 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
4 //
5 // This file is part of OpenTween.
6 //
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)
10 // any later version.
11 //
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
15 // for more details.
16 //
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.
21
22 #nullable enable
23
24 using System;
25 using System.Collections.Generic;
26 using System.ComponentModel;
27 using System.Drawing;
28 using System.IO;
29 using System.Linq;
30 using System.Net;
31 using System.Net.Http;
32 using System.Runtime.InteropServices;
33 using System.Text;
34 using System.Threading;
35 using System.Threading.Tasks;
36 using System.Windows.Forms;
37 using OpenTween.Thumbnail;
38
39 namespace OpenTween
40 {
41     public class OTPictureBox : PictureBox
42     {
43         [Browsable(false)]
44         [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
45         public new MemoryImage? Image
46         {
47             get => this.memoryImage;
48             set
49             {
50                 this.memoryImage = value;
51                 base.Image = value?.Image;
52
53                 this.RestoreSizeMode();
54             }
55         }
56         private MemoryImage? memoryImage;
57
58         [Localizable(true)]
59         [DefaultValue(PictureBoxSizeMode.Normal)]
60         public new PictureBoxSizeMode SizeMode
61         {
62             get => this.currentSizeMode;
63             set
64             {
65                 this.currentSizeMode = value;
66
67                 if (base.Image != base.InitialImage && base.Image != base.ErrorImage)
68                 {
69                     base.SizeMode = value;
70                 }
71             }
72         }
73
74         /// <summary>
75         /// InitialImage や ErrorImage の表示に SizeMode を一時的に変更するため、
76         /// 現在の SizeMode を記憶しておくためのフィールド
77         /// </summary>
78         private PictureBoxSizeMode currentSizeMode;
79
80         public void ShowInitialImage()
81         {
82             base.Image = base.InitialImage;
83
84             // InitialImage は SizeMode の値に依らず中央等倍に表示する必要がある
85             base.SizeMode = PictureBoxSizeMode.CenterImage;
86         }
87
88         public void ShowErrorImage()
89         {
90             base.Image = base.ErrorImage;
91
92             // ErrorImage は SizeMode の値に依らず中央等倍に表示する必要がある
93             base.SizeMode = PictureBoxSizeMode.CenterImage;
94         }
95
96         private void RestoreSizeMode()
97             => base.SizeMode = this.currentSizeMode;
98
99         /// <summary>
100         /// SetImageFromTask メソッドを連続で呼び出した際に設定される画像が前後するのを防ぐため、
101         /// 現在進行中の Task を表す Id を記憶しておくためのフィールド
102         /// </summary>
103         private int currentImageTaskId = 0;
104
105         public async Task SetImageFromTask(Func<Task<MemoryImage>> imageTask)
106         {
107             var id = Interlocked.Increment(ref this.currentImageTaskId);
108
109             try
110             {
111                 this.ShowInitialImage();
112
113                 var image = await imageTask();
114
115                 if (id == this.currentImageTaskId)
116                     this.Image = image;
117             }
118             catch (Exception)
119             {
120                 if (id == this.currentImageTaskId)
121                     this.ShowErrorImage();
122                 try
123                 {
124                     throw;
125                 }
126                 catch (HttpRequestException) { }
127                 catch (InvalidImageException) { }
128                 catch (OperationCanceledException) { }
129                 catch (ObjectDisposedException) { }
130                 catch (WebException) { }
131                 catch (IOException) { }
132             }
133         }
134
135         protected override void OnPaint(PaintEventArgs pe)
136         {
137             try
138             {
139                 base.OnPaint(pe);
140
141                 // 動画なら再生ボタンを上から描画
142                 this.DrawPlayableMark(pe);
143             }
144             catch (ExternalException)
145             {
146                 // アニメーション GIF 再生中に発生するエラーの対策
147                 // 参照: https://sourceforge.jp/ticket/browse.php?group_id=6526&tid=32894
148                 this.ShowErrorImage();
149             }
150         }
151
152         private void DrawPlayableMark(PaintEventArgs pe)
153         {
154             if (!(this.Tag is ThumbnailInfo thumb && thumb.IsPlayable)) return;
155             if (base.Image == base.InitialImage || base.Image == base.ErrorImage) return;
156
157             var overlayImage = Properties.Resources.PlayableOverlayImage;
158
159             var overlaySize = Math.Min(this.Width, this.Height) / 4;
160             var destRect = new Rectangle(
161                 (this.Width - overlaySize) / 2,
162                 (this.Height - overlaySize) / 2,
163                 overlaySize,
164                 overlaySize);
165
166             pe.Graphics.DrawImage(overlayImage, destRect, 0, 0, overlayImage.Width, overlayImage.Height, GraphicsUnit.Pixel);
167         }
168
169         [Browsable(false)]
170         [EditorBrowsable(EditorBrowsableState.Never)]
171         [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
172         public new string? ImageLocation
173         {
174             get => null;
175             set { }
176         }
177
178         [EditorBrowsable(EditorBrowsableState.Never)]
179         public new void Load(string url)
180             => throw new NotSupportedException();
181     }
182 }