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;
25 using System.Diagnostics.CodeAnalysis;
30 using System.Net.Http;
32 using System.Windows.Forms;
33 using System.Text.RegularExpressions;
34 using System.Threading.Tasks;
35 using OpenTween.Thumbnail;
36 using System.Threading;
37 using OpenTween.Models;
41 public partial class TweetThumbnail : UserControl
43 protected internal List<OTPictureBox> pictureBox = new List<OTPictureBox>();
44 protected MouseWheelMessageFilter filter = new MouseWheelMessageFilter();
46 public event EventHandler ThumbnailLoading;
47 public event EventHandler<ThumbnailDoubleClickEventArgs> ThumbnailDoubleClick;
48 public event EventHandler<ThumbnailImageSearchEventArgs> ThumbnailImageSearchClick;
50 public ThumbnailInfo Thumbnail
52 get { return this.pictureBox[this.scrollBar.Value].Tag as ThumbnailInfo; }
55 public TweetThumbnail()
57 InitializeComponent();
60 public Task ShowThumbnailAsync(PostClass post)
62 return this.ShowThumbnailAsync(post, CancellationToken.None);
65 public async Task ShowThumbnailAsync(PostClass post, CancellationToken cancelToken)
67 var loadTasks = new List<Task>();
69 this.scrollBar.Enabled = false;
70 this.scrollBar.Visible = false;
72 if (post.ExpandedUrls.Length == 0 && post.Media.Count == 0 && post.PostGeo == null)
74 this.SetThumbnailCount(0);
78 var thumbnails = (await this.GetThumbailInfoAsync(post, cancelToken))
81 cancelToken.ThrowIfCancellationRequested();
83 this.SetThumbnailCount(thumbnails.Length);
84 if (thumbnails.Length == 0)
87 for (int i = 0; i < thumbnails.Length; i++)
89 var thumb = thumbnails[i];
90 var picbox = this.pictureBox[i];
93 picbox.ContextMenuStrip = this.contextMenuStrip;
95 var loadTask = picbox.SetImageFromTask(() => thumb.LoadThumbnailImageAsync(cancelToken));
96 loadTasks.Add(loadTask);
98 var tooltipText = thumb.TooltipText;
99 if (!string.IsNullOrEmpty(tooltipText))
101 this.toolTip.SetToolTip(picbox, tooltipText);
102 picbox.AccessibleDescription = tooltipText;
105 cancelToken.ThrowIfCancellationRequested();
108 if (thumbnails.Length > 1)
110 this.scrollBar.Enabled = true;
111 this.scrollBar.Visible = true;
114 this.ThumbnailLoading?.Invoke(this, EventArgs.Empty);
116 await Task.WhenAll(loadTasks).ConfigureAwait(false);
119 private string GetImageSearchUriGoogle(string image_uri)
121 return @"https://www.google.com/searchbyimage?image_url=" + Uri.EscapeDataString(image_uri);
124 private string GetImageSearchUriSauceNao(string imageUri)
126 return @"https://saucenao.com/search.php?url=" + Uri.EscapeDataString(imageUri);
129 protected virtual Task<IEnumerable<ThumbnailInfo>> GetThumbailInfoAsync(PostClass post, CancellationToken token)
131 return ThumbnailGenerator.GetThumbnailsAsync(post, token);
137 /// <param name="count">表示するサムネイルの数</param>
138 protected void SetThumbnailCount(int count)
140 if (count == 0 && this.pictureBox.Count == 0)
143 using (ControlTransaction.Layout(this.panelPictureBox, false))
145 this.panelPictureBox.Controls.Clear();
146 foreach (var picbox in this.pictureBox)
148 var memoryImage = picbox.Image;
150 filter.Unregister(picbox);
152 picbox.MouseWheel -= this.pictureBox_MouseWheel;
153 picbox.DoubleClick -= this.pictureBox_DoubleClick;
156 memoryImage?.Dispose();
158 // メモリリーク対策 (http://stackoverflow.com/questions/2792427#2793714)
159 picbox.ContextMenuStrip = null;
161 this.pictureBox.Clear();
163 this.scrollBar.Maximum = (count > 0) ? count - 1 : 0;
164 this.scrollBar.Value = 0;
166 for (int i = 0; i < count; i++)
168 var picbox = CreatePictureBox("pictureBox" + i);
169 picbox.Visible = (i == 0);
170 picbox.MouseWheel += this.pictureBox_MouseWheel;
171 picbox.DoubleClick += this.pictureBox_DoubleClick;
173 filter.Register(picbox);
175 this.panelPictureBox.Controls.Add(picbox);
176 this.pictureBox.Add(picbox);
181 [SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope")]
182 protected virtual OTPictureBox CreatePictureBox(string name)
184 return new OTPictureBox
187 SizeMode = PictureBoxSizeMode.Zoom,
189 Dock = DockStyle.Fill,
190 AccessibleRole = AccessibleRole.Graphic,
194 public void ScrollUp()
196 var newval = this.scrollBar.Value - this.scrollBar.SmallChange;
198 if (newval < this.scrollBar.Minimum)
199 newval = this.scrollBar.Minimum;
201 this.scrollBar.Value = newval;
204 public void ScrollDown()
206 var newval = this.scrollBar.Value + this.scrollBar.SmallChange;
208 if (newval > this.scrollBar.Maximum)
209 newval = this.scrollBar.Maximum;
211 this.scrollBar.Value = newval;
214 protected override void ScaleControl(SizeF factor, BoundsSpecified specified)
216 base.ScaleControl(factor, specified);
217 OTBaseForm.ScaleChildControl(this.scrollBar, factor);
220 private void scrollBar_ValueChanged(object sender, EventArgs e)
222 using (ControlTransaction.Layout(this, false))
224 var value = this.scrollBar.Value;
225 for (var i = 0; i < this.pictureBox.Count; i++)
227 this.pictureBox[i].Visible = (i == value);
232 private void pictureBox_MouseWheel(object sender, MouseEventArgs e)
240 private void pictureBox_DoubleClick(object sender, EventArgs e)
242 var thumb = ((PictureBox)sender).Tag as ThumbnailInfo;
244 if (thumb == null) return;
246 this.ThumbnailDoubleClick?.Invoke(this, new ThumbnailDoubleClickEventArgs(thumb));
249 private void contextMenuStrip_Opening(object sender, CancelEventArgs e)
251 var picbox = (OTPictureBox)this.contextMenuStrip.SourceControl;
252 var thumb = (ThumbnailInfo)picbox.Tag;
254 var searchTargetUri = thumb.FullSizeImageUrl ?? thumb.ThumbnailImageUrl ?? null;
255 if (searchTargetUri != null)
257 this.searchImageGoogleMenuItem.Enabled = true;
258 this.searchImageGoogleMenuItem.Tag = searchTargetUri;
259 this.searchImageSauceNaoMenuItem.Enabled = true;
260 this.searchImageSauceNaoMenuItem.Tag = searchTargetUri;
264 this.searchImageGoogleMenuItem.Enabled = false;
265 this.searchImageSauceNaoMenuItem.Enabled = false;
269 private void searchSimilarImageMenuItem_Click(object sender, EventArgs e)
271 var searchTargetUri = (string)this.searchImageGoogleMenuItem.Tag;
272 var searchUri = this.GetImageSearchUriGoogle(searchTargetUri);
274 this.ThumbnailImageSearchClick?.Invoke(this, new ThumbnailImageSearchEventArgs(searchUri));
277 private void searchImageSauceNaoMenuItem_Click(object sender, EventArgs e)
279 var searchTargetUri = (string)this.searchImageSauceNaoMenuItem.Tag;
280 var searchUri = this.GetImageSearchUriSauceNao(searchTargetUri);
282 this.ThumbnailImageSearchClick?.Invoke(this, new ThumbnailImageSearchEventArgs(searchUri));
286 public class ThumbnailDoubleClickEventArgs : EventArgs
288 public ThumbnailInfo Thumbnail { get; }
290 public ThumbnailDoubleClickEventArgs(ThumbnailInfo thumbnail)
292 this.Thumbnail = thumbnail;
296 public class ThumbnailImageSearchEventArgs : EventArgs
298 public string ImageUrl { get; }
300 public ThumbnailImageSearchEventArgs(string url)