OSDN Git Service

graphqlエンドポイントを使用した関連発言表示に対応
[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.ImageInternal;
48             set
49             {
50                 this.ImageInternal = value;
51
52                 // Image を直接更新した場合も SetImageFromTask による更新をキャンセルした扱いにする
53                 Interlocked.Increment(ref this.currentImageTaskId);
54             }
55         }
56
57         private MemoryImage? ImageInternal
58         {
59             get => this.memoryImage;
60             set
61             {
62                 this.memoryImage = value;
63                 base.Image = value?.Image;
64
65                 this.RestoreSizeMode();
66             }
67         }
68
69         private MemoryImage? memoryImage;
70
71         public bool PlayableMark
72         {
73             get => this.playableMark;
74             set
75             {
76                 if (value == this.playableMark)
77                     return;
78
79                 this.playableMark = value;
80                 this.Invalidate();
81             }
82         }
83
84         private bool playableMark = false;
85
86         [Localizable(true)]
87         [DefaultValue(PictureBoxSizeMode.Normal)]
88         public new PictureBoxSizeMode SizeMode
89         {
90             get => this.currentSizeMode;
91             set
92             {
93                 this.currentSizeMode = value;
94
95                 if (base.Image != base.InitialImage && base.Image != base.ErrorImage)
96                 {
97                     base.SizeMode = value;
98                 }
99             }
100         }
101
102         /// <summary>
103         /// InitialImage や ErrorImage の表示に SizeMode を一時的に変更するため、
104         /// 現在の SizeMode を記憶しておくためのフィールド
105         /// </summary>
106         private PictureBoxSizeMode currentSizeMode;
107
108         public void ShowInitialImage()
109         {
110             base.Image = base.InitialImage;
111
112             // InitialImage は SizeMode の値に依らず中央等倍に表示する必要がある
113             base.SizeMode = PictureBoxSizeMode.CenterImage;
114         }
115
116         public void ShowErrorImage()
117         {
118             base.Image = base.ErrorImage;
119
120             // ErrorImage は SizeMode の値に依らず中央等倍に表示する必要がある
121             base.SizeMode = PictureBoxSizeMode.CenterImage;
122         }
123
124         private void RestoreSizeMode()
125             => base.SizeMode = this.currentSizeMode;
126
127         /// <summary>
128         /// SetImageFromTask メソッドを連続で呼び出した際に設定される画像が前後するのを防ぐため、
129         /// 現在進行中の Task を表す Id を記憶しておくためのフィールド
130         /// </summary>
131         private int currentImageTaskId = 0;
132
133         public async Task SetImageFromTask(Func<Task<MemoryImage>> imageTask, bool useStatusImage = true)
134         {
135             var id = Interlocked.Increment(ref this.currentImageTaskId);
136
137             try
138             {
139                 if (useStatusImage)
140                     this.ShowInitialImage();
141
142                 var image = await Task.Run(imageTask);
143
144                 if (id == this.currentImageTaskId)
145                     this.ImageInternal = image;
146             }
147             catch (Exception)
148             {
149                 if (id == this.currentImageTaskId && useStatusImage)
150                     this.ShowErrorImage();
151                 try
152                 {
153                     throw;
154                 }
155                 catch (HttpRequestException)
156                 {
157                 }
158                 catch (InvalidImageException)
159                 {
160                 }
161                 catch (OperationCanceledException)
162                 {
163                 }
164                 catch (ObjectDisposedException)
165                 {
166                 }
167                 catch (WebException)
168                 {
169                 }
170                 catch (IOException)
171                 {
172                 }
173             }
174         }
175
176         protected override void OnPaint(PaintEventArgs pe)
177         {
178             try
179             {
180                 base.OnPaint(pe);
181
182                 // 動画なら再生ボタンを上から描画
183                 this.DrawPlayableMark(pe);
184             }
185             catch (ExternalException)
186             {
187                 // アニメーション GIF 再生中に発生するエラーの対策
188                 // 参照: https://sourceforge.jp/ticket/browse.php?group_id=6526&tid=32894
189                 this.ShowErrorImage();
190             }
191         }
192
193         private void DrawPlayableMark(PaintEventArgs pe)
194         {
195             if (!this.PlayableMark)
196                 return;
197
198             if (this.Image == null)
199                 return;
200
201             if (base.Image == base.InitialImage || base.Image == base.ErrorImage)
202                 return;
203
204             var overlayImage = Properties.Resources.PlayableOverlayImage;
205
206             var overlaySize = Math.Min(this.Width, this.Height) / 4;
207             var destRect = new Rectangle(
208                 (this.Width - overlaySize) / 2,
209                 (this.Height - overlaySize) / 2,
210                 overlaySize,
211                 overlaySize);
212
213             pe.Graphics.DrawImage(overlayImage, destRect, 0, 0, overlayImage.Width, overlayImage.Height, GraphicsUnit.Pixel);
214         }
215
216         [Browsable(false)]
217         [EditorBrowsable(EditorBrowsableState.Never)]
218         [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
219         public new string? ImageLocation
220         {
221             get => null;
222             set { }
223         }
224
225         [EditorBrowsable(EditorBrowsableState.Never)]
226         public new void Load(string url)
227             => throw new NotSupportedException();
228     }
229 }