OSDN Git Service

TweetThumbnailのコンテキストメニューを毎回生成せず、事前に生成したものを再利用する
[opentween/open-tween.git] / OpenTween / TweetThumbnail.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2012 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 using System;
23 using System.Collections.Generic;
24 using System.ComponentModel;
25 using System.Drawing;
26 using System.Data;
27 using System.Linq;
28 using System.Net;
29 using System.Net.Http;
30 using System.Text;
31 using System.Windows.Forms;
32 using System.Text.RegularExpressions;
33 using System.Threading.Tasks;
34 using OpenTween.Thumbnail;
35 using System.Threading;
36
37 namespace OpenTween
38 {
39     public partial class TweetThumbnail : UserControl
40     {
41         protected internal List<OTPictureBox> pictureBox = new List<OTPictureBox>();
42
43         public event EventHandler ThumbnailLoading;
44         public event EventHandler<ThumbnailDoubleClickEventArgs> ThumbnailDoubleClick;
45         public event EventHandler<ThumbnailImageSearchEventArgs> ThumbnailImageSearchClick;
46
47         private object uiLockObj = new object();
48
49         public ThumbnailInfo Thumbnail
50         {
51             get { return this.pictureBox[this.scrollBar.Value].Tag as ThumbnailInfo; }
52         }
53
54         public TweetThumbnail()
55         {
56             InitializeComponent();
57         }
58
59         public Task ShowThumbnailAsync(PostClass post)
60         {
61             return this.ShowThumbnailAsync(post, CancellationToken.None);
62         }
63
64         public async Task ShowThumbnailAsync(PostClass post, CancellationToken cancelToken)
65         {
66             var loadTasks = new List<Task>();
67
68             this.scrollBar.Enabled = false;
69
70             var thumbnails = (await this.GetThumbailInfoAsync(post, cancelToken))
71                 .ToArray();
72
73             cancelToken.ThrowIfCancellationRequested();
74
75             lock (this.uiLockObj)
76             {
77                 this.SetThumbnailCount(thumbnails.Length);
78                 if (thumbnails.Length == 0) return;
79
80                 for (int i = 0; i < thumbnails.Length; i++)
81                 {
82                     var thumb = thumbnails[i];
83                     var picbox = this.pictureBox[i];
84
85                     picbox.Tag = thumb;
86                     picbox.ContextMenuStrip = this.contextMenuStrip;
87
88                     var loadTask = picbox.SetImageFromTask(() => thumb.LoadThumbnailImageAsync(cancelToken));
89                     loadTasks.Add(loadTask);
90
91                     var tooltipText = thumb.TooltipText;
92                     if (!string.IsNullOrEmpty(tooltipText))
93                     {
94                         this.toolTip.SetToolTip(picbox, tooltipText);
95                     }
96
97                     cancelToken.ThrowIfCancellationRequested();
98                 }
99
100                 if (thumbnails.Length > 1)
101                     this.scrollBar.Enabled = true;
102             }
103
104             if (this.ThumbnailLoading != null)
105                 this.ThumbnailLoading(this, EventArgs.Empty);
106
107             await Task.WhenAll(loadTasks).ConfigureAwait(false);
108         }
109
110         private string GetImageSearchUri(string image_uri)
111         {
112             return @"https://www.google.com/searchbyimage?image_url=" + Uri.EscapeDataString(image_uri);
113         }
114
115         protected virtual Task<IEnumerable<ThumbnailInfo>> GetThumbailInfoAsync(PostClass post, CancellationToken token)
116         {
117             return ThumbnailGenerator.GetThumbnailsAsync(post, token);
118         }
119
120         /// <summary>
121         /// 表示するサムネイルの数を設定する
122         /// </summary>
123         /// <param name="count">表示するサムネイルの数</param>
124         protected void SetThumbnailCount(int count)
125         {
126             using (ControlTransaction.Layout(this, false))
127             {
128                 this.panelPictureBox.Controls.Clear();
129                 foreach (var picbox in this.pictureBox)
130                 {
131                     var memoryImage = picbox.Image;
132                     picbox.Dispose();
133
134                     if (memoryImage != null)
135                         memoryImage.Dispose();
136                 }
137                 this.pictureBox.Clear();
138
139                 this.scrollBar.Maximum = (count > 0) ? count - 1 : 0;
140                 this.scrollBar.Value = 0;
141
142                 for (int i = 0; i < count; i++)
143                 {
144                     var picbox = CreatePictureBox("pictureBox" + i);
145                     picbox.Visible = (i == 0);
146                     picbox.DoubleClick += this.pictureBox_DoubleClick;
147
148                     this.panelPictureBox.Controls.Add(picbox);
149                     this.pictureBox.Add(picbox);
150                 }
151             }
152         }
153
154         protected virtual OTPictureBox CreatePictureBox(string name)
155         {
156             return new OTPictureBox()
157             {
158                 Name = name,
159                 SizeMode = PictureBoxSizeMode.Zoom,
160                 WaitOnLoad = false,
161                 Dock = DockStyle.Fill,
162             };
163         }
164
165         public void ScrollUp()
166         {
167             var newval = this.scrollBar.Value + this.scrollBar.SmallChange;
168
169             if (newval > this.scrollBar.Maximum)
170                 newval = this.scrollBar.Maximum;
171
172             this.scrollBar.Value = newval;
173         }
174
175         public void ScrollDown()
176         {
177             var newval = this.scrollBar.Value - this.scrollBar.SmallChange;
178
179             if (newval < this.scrollBar.Minimum)
180                 newval = this.scrollBar.Minimum;
181
182             this.scrollBar.Value = newval;
183         }
184
185         private void scrollBar_ValueChanged(object sender, EventArgs e)
186         {
187             using (ControlTransaction.Layout(this, false))
188             {
189                 var value = this.scrollBar.Value;
190                 for (var i = 0; i < this.pictureBox.Count; i++)
191                 {
192                     this.pictureBox[i].Visible = (i == value);
193                 }
194             }
195         }
196
197         private void pictureBox_DoubleClick(object sender, EventArgs e)
198         {
199             var thumb = ((PictureBox)sender).Tag as ThumbnailInfo;
200
201             if (thumb == null) return;
202
203             if (this.ThumbnailDoubleClick != null)
204             {
205                 this.ThumbnailDoubleClick(this, new ThumbnailDoubleClickEventArgs(thumb));
206             }
207         }
208
209         private void contextMenuStrip_Opening(object sender, CancelEventArgs e)
210         {
211             var picbox = (OTPictureBox)this.contextMenuStrip.SourceControl;
212             var thumb = (ThumbnailInfo)picbox.Tag;
213
214             var searchTargetUri = thumb.FullSizeImageUrl ?? thumb.ThumbnailUrl ?? null;
215             if (searchTargetUri != null)
216             {
217                 this.searchSimilarImageMenuItem.Enabled = true;
218                 this.searchSimilarImageMenuItem.Tag = searchTargetUri;
219             }
220             else
221             {
222                 this.searchSimilarImageMenuItem.Enabled = false;
223             }
224         }
225
226         private void searchSimilarImageMenuItem_Click(object sender, EventArgs e)
227         {
228             var searchTargetUri = (string)this.searchSimilarImageMenuItem.Tag;
229             var searchUri = this.GetImageSearchUri(searchTargetUri);
230
231             if (this.ThumbnailImageSearchClick != null)
232                 this.ThumbnailImageSearchClick(this, new ThumbnailImageSearchEventArgs(searchUri));
233         }
234     }
235
236     public class ThumbnailDoubleClickEventArgs : EventArgs
237     {
238         public ThumbnailInfo Thumbnail { get; private set; }
239
240         public ThumbnailDoubleClickEventArgs(ThumbnailInfo thumbnail)
241         {
242             this.Thumbnail = thumbnail;
243         }
244     }
245
246     public class ThumbnailImageSearchEventArgs : EventArgs
247     {
248         public string ImageUrl { get; private set; }
249
250         public ThumbnailImageSearchEventArgs(string url)
251         {
252             this.ImageUrl = url;
253         }
254     }
255 }