1 // OpenTween - Client of Twitter
2 // Copyright (c) 2012 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
5 // This file is part of OpenTween.
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)
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
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.
23 using System.Collections.Generic;
24 using System.ComponentModel;
29 using System.Net.Http;
31 using System.Windows.Forms;
32 using System.Text.RegularExpressions;
33 using System.Threading.Tasks;
34 using OpenTween.Thumbnail;
35 using System.Threading;
39 public partial class TweetThumbnail : UserControl
41 protected internal List<OTPictureBox> pictureBox = new List<OTPictureBox>();
43 public event EventHandler ThumbnailLoading;
44 public event EventHandler<ThumbnailDoubleClickEventArgs> ThumbnailDoubleClick;
45 public event EventHandler<ThumbnailImageSearchEventArgs> ThumbnailImageSearchClick;
47 private readonly HttpClient http = MyCommon.CreateHttpClient();
49 private object uiLockObj = new object();
51 public ThumbnailInfo Thumbnail
53 get { return this.pictureBox[this.scrollBar.Value].Tag as ThumbnailInfo; }
56 public TweetThumbnail()
58 InitializeComponent();
61 public Task ShowThumbnailAsync(PostClass post)
63 return this.ShowThumbnailAsync(post, CancellationToken.None);
66 public async Task ShowThumbnailAsync(PostClass post, CancellationToken cancelToken)
68 var loadTasks = new List<Task>();
70 this.scrollBar.Enabled = false;
72 var thumbnails = (await this.GetThumbailInfoAsync(post, cancelToken))
75 cancelToken.ThrowIfCancellationRequested();
79 this.SetThumbnailCount(thumbnails.Length);
80 if (thumbnails.Length == 0) return;
82 for (int i = 0; i < thumbnails.Length; i++)
84 var thumb = thumbnails[i];
85 var picbox = this.pictureBox[i];
88 picbox.ContextMenuStrip = this.contextMenuStrip;
90 var loadTask = picbox.SetImageFromTask(() => thumb.LoadThumbnailImageAsync(this.http, cancelToken));
91 loadTasks.Add(loadTask);
93 var tooltipText = thumb.TooltipText;
94 if (!string.IsNullOrEmpty(tooltipText))
96 this.toolTip.SetToolTip(picbox, tooltipText);
99 cancelToken.ThrowIfCancellationRequested();
102 if (thumbnails.Length > 1)
103 this.scrollBar.Enabled = true;
106 if (this.ThumbnailLoading != null)
107 this.ThumbnailLoading(this, EventArgs.Empty);
109 await Task.WhenAll(loadTasks).ConfigureAwait(false);
112 private string GetImageSearchUri(string image_uri)
114 return @"https://www.google.com/searchbyimage?image_url=" + Uri.EscapeDataString(image_uri);
117 protected virtual Task<IEnumerable<ThumbnailInfo>> GetThumbailInfoAsync(PostClass post, CancellationToken token)
119 return ThumbnailGenerator.GetThumbnailsAsync(post, token);
125 /// <param name="count">表示するサムネイルの数</param>
126 protected void SetThumbnailCount(int count)
128 using (ControlTransaction.Layout(this, false))
130 this.panelPictureBox.Controls.Clear();
131 foreach (var picbox in this.pictureBox)
133 var memoryImage = picbox.Image;
136 if (memoryImage != null)
137 memoryImage.Dispose();
139 // メモリリーク対策 (http://stackoverflow.com/questions/2792427#2793714)
140 picbox.ContextMenuStrip = null;
142 this.pictureBox.Clear();
144 this.scrollBar.Maximum = (count > 0) ? count - 1 : 0;
145 this.scrollBar.Value = 0;
147 for (int i = 0; i < count; i++)
149 var picbox = CreatePictureBox("pictureBox" + i);
150 picbox.Visible = (i == 0);
151 picbox.DoubleClick += this.pictureBox_DoubleClick;
153 this.panelPictureBox.Controls.Add(picbox);
154 this.pictureBox.Add(picbox);
159 protected virtual OTPictureBox CreatePictureBox(string name)
161 return new OTPictureBox()
164 SizeMode = PictureBoxSizeMode.Zoom,
166 Dock = DockStyle.Fill,
170 public void ScrollUp()
172 var newval = this.scrollBar.Value + this.scrollBar.SmallChange;
174 if (newval > this.scrollBar.Maximum)
175 newval = this.scrollBar.Maximum;
177 this.scrollBar.Value = newval;
180 public void ScrollDown()
182 var newval = this.scrollBar.Value - this.scrollBar.SmallChange;
184 if (newval < this.scrollBar.Minimum)
185 newval = this.scrollBar.Minimum;
187 this.scrollBar.Value = newval;
190 private void scrollBar_ValueChanged(object sender, EventArgs e)
192 using (ControlTransaction.Layout(this, false))
194 var value = this.scrollBar.Value;
195 for (var i = 0; i < this.pictureBox.Count; i++)
197 this.pictureBox[i].Visible = (i == value);
202 private void pictureBox_DoubleClick(object sender, EventArgs e)
204 var thumb = ((PictureBox)sender).Tag as ThumbnailInfo;
206 if (thumb == null) return;
208 if (this.ThumbnailDoubleClick != null)
210 this.ThumbnailDoubleClick(this, new ThumbnailDoubleClickEventArgs(thumb));
214 private void contextMenuStrip_Opening(object sender, CancelEventArgs e)
216 var picbox = (OTPictureBox)this.contextMenuStrip.SourceControl;
217 var thumb = (ThumbnailInfo)picbox.Tag;
219 var searchTargetUri = thumb.FullSizeImageUrl ?? thumb.ThumbnailUrl ?? null;
220 if (searchTargetUri != null)
222 this.searchSimilarImageMenuItem.Enabled = true;
223 this.searchSimilarImageMenuItem.Tag = searchTargetUri;
227 this.searchSimilarImageMenuItem.Enabled = false;
231 private void searchSimilarImageMenuItem_Click(object sender, EventArgs e)
233 var searchTargetUri = (string)this.searchSimilarImageMenuItem.Tag;
234 var searchUri = this.GetImageSearchUri(searchTargetUri);
236 if (this.ThumbnailImageSearchClick != null)
237 this.ThumbnailImageSearchClick(this, new ThumbnailImageSearchEventArgs(searchUri));
241 public class ThumbnailDoubleClickEventArgs : EventArgs
243 public ThumbnailInfo Thumbnail { get; private set; }
245 public ThumbnailDoubleClickEventArgs(ThumbnailInfo thumbnail)
247 this.Thumbnail = thumbnail;
251 public class ThumbnailImageSearchEventArgs : EventArgs
253 public string ImageUrl { get; private set; }
255 public ThumbnailImageSearchEventArgs(string url)