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;
38 using System.Runtime.InteropServices;
42 public partial class TweetThumbnail : UserControl
44 protected internal List<OTPictureBox> pictureBox = new List<OTPictureBox>();
45 protected MouseWheelMessageFilter filter = new MouseWheelMessageFilter();
47 public event EventHandler<EventArgs> ThumbnailLoading;
48 public event EventHandler<ThumbnailDoubleClickEventArgs> ThumbnailDoubleClick;
49 public event EventHandler<ThumbnailImageSearchEventArgs> ThumbnailImageSearchClick;
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;
71 this.scrollBar.Visible = false;
73 if (post.ExpandedUrls.Length == 0 && post.Media.Count == 0 && post.PostGeo == null)
75 this.SetThumbnailCount(0);
79 var thumbnails = (await this.GetThumbailInfoAsync(post, cancelToken))
82 cancelToken.ThrowIfCancellationRequested();
84 this.SetThumbnailCount(thumbnails.Length);
85 if (thumbnails.Length == 0)
88 for (int i = 0; i < thumbnails.Length; i++)
90 var thumb = thumbnails[i];
91 var picbox = this.pictureBox[i];
94 picbox.ContextMenuStrip = this.contextMenuStrip;
96 var loadTask = picbox.SetImageFromTask(() => thumb.LoadThumbnailImageAsync(cancelToken));
97 loadTasks.Add(loadTask);
99 var tooltipText = thumb.TooltipText;
100 if (!string.IsNullOrEmpty(tooltipText))
102 this.toolTip.SetToolTip(picbox, tooltipText);
103 picbox.AccessibleDescription = tooltipText;
106 cancelToken.ThrowIfCancellationRequested();
109 if (thumbnails.Length > 1)
111 this.scrollBar.Enabled = true;
112 this.scrollBar.Visible = true;
115 this.ThumbnailLoading?.Invoke(this, EventArgs.Empty);
117 await Task.WhenAll(loadTasks).ConfigureAwait(false);
120 private string GetImageSearchUriGoogle(string image_uri)
122 return @"https://www.google.com/searchbyimage?image_url=" + Uri.EscapeDataString(image_uri);
125 private string GetImageSearchUriSauceNao(string imageUri)
127 return @"https://saucenao.com/search.php?url=" + Uri.EscapeDataString(imageUri);
130 protected virtual Task<IEnumerable<ThumbnailInfo>> GetThumbailInfoAsync(PostClass post, CancellationToken token)
132 return ThumbnailGenerator.GetThumbnailsAsync(post, token);
138 /// <param name="count">表示するサムネイルの数</param>
139 protected void SetThumbnailCount(int count)
141 if (count == 0 && this.pictureBox.Count == 0)
144 using (ControlTransaction.Layout(this.panelPictureBox, false))
146 this.panelPictureBox.Controls.Clear();
147 foreach (var picbox in this.pictureBox)
149 var memoryImage = picbox.Image;
151 filter.Unregister(picbox);
153 picbox.MouseWheel -= this.pictureBox_MouseWheel;
154 picbox.DoubleClick -= this.pictureBox_DoubleClick;
157 memoryImage?.Dispose();
159 // メモリリーク対策 (http://stackoverflow.com/questions/2792427#2793714)
160 picbox.ContextMenuStrip = null;
162 this.pictureBox.Clear();
164 this.scrollBar.Maximum = (count > 0) ? count - 1 : 0;
165 this.scrollBar.Value = 0;
167 for (int i = 0; i < count; i++)
169 var picbox = CreatePictureBox("pictureBox" + i);
170 picbox.Visible = (i == 0);
171 picbox.MouseWheel += this.pictureBox_MouseWheel;
172 picbox.DoubleClick += this.pictureBox_DoubleClick;
174 filter.Register(picbox);
176 this.panelPictureBox.Controls.Add(picbox);
177 this.pictureBox.Add(picbox);
182 [SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope")]
183 protected virtual OTPictureBox CreatePictureBox(string name)
185 return new OTPictureBox
188 SizeMode = PictureBoxSizeMode.Zoom,
190 Dock = DockStyle.Fill,
191 AccessibleRole = AccessibleRole.Graphic,
195 public void OpenImage(ThumbnailInfo thumb)
196 => this.ThumbnailDoubleClick?.Invoke(this, new ThumbnailDoubleClickEventArgs(thumb));
198 public void ScrollUp()
200 var newval = this.scrollBar.Value - this.scrollBar.SmallChange;
202 if (newval < this.scrollBar.Minimum)
203 newval = this.scrollBar.Minimum;
205 this.scrollBar.Value = newval;
208 public void ScrollDown()
210 var newval = this.scrollBar.Value + this.scrollBar.SmallChange;
212 if (newval > this.scrollBar.Maximum)
213 newval = this.scrollBar.Maximum;
215 this.scrollBar.Value = newval;
218 protected override void ScaleControl(SizeF factor, BoundsSpecified specified)
220 base.ScaleControl(factor, specified);
221 OTBaseForm.ScaleChildControl(this.scrollBar, factor);
224 private void scrollBar_ValueChanged(object sender, EventArgs e)
226 using (ControlTransaction.Layout(this, false))
228 var value = this.scrollBar.Value;
229 for (var i = 0; i < this.pictureBox.Count; i++)
231 this.pictureBox[i].Visible = (i == value);
236 private void pictureBox_MouseWheel(object sender, MouseEventArgs e)
244 private void pictureBox_DoubleClick(object sender, EventArgs e)
246 var thumb = ((PictureBox)sender).Tag as ThumbnailInfo;
248 if (thumb == null) return;
250 this.OpenImage(thumb);
253 private void contextMenuStrip_Opening(object sender, CancelEventArgs e)
255 var picbox = (OTPictureBox)this.contextMenuStrip.SourceControl;
256 var thumb = (ThumbnailInfo)picbox.Tag;
258 var searchTargetUri = thumb.FullSizeImageUrl ?? thumb.ThumbnailImageUrl ?? null;
259 if (searchTargetUri != null)
261 this.searchImageGoogleMenuItem.Enabled = true;
262 this.searchImageGoogleMenuItem.Tag = searchTargetUri;
263 this.searchImageSauceNaoMenuItem.Enabled = true;
264 this.searchImageSauceNaoMenuItem.Tag = searchTargetUri;
268 this.searchImageGoogleMenuItem.Enabled = false;
269 this.searchImageSauceNaoMenuItem.Enabled = false;
273 private void searchSimilarImageMenuItem_Click(object sender, EventArgs e)
275 var searchTargetUri = (string)this.searchImageGoogleMenuItem.Tag;
276 var searchUri = this.GetImageSearchUriGoogle(searchTargetUri);
278 this.ThumbnailImageSearchClick?.Invoke(this, new ThumbnailImageSearchEventArgs(searchUri));
281 private void searchImageSauceNaoMenuItem_Click(object sender, EventArgs e)
283 var searchTargetUri = (string)this.searchImageSauceNaoMenuItem.Tag;
284 var searchUri = this.GetImageSearchUriSauceNao(searchTargetUri);
286 this.ThumbnailImageSearchClick?.Invoke(this, new ThumbnailImageSearchEventArgs(searchUri));
289 private void openMenuItem_Click(object sender, EventArgs e)
290 => this.OpenImage(this.Thumbnail);
292 private void copyUrlMenuItem_Click(object sender, EventArgs e)
296 Clipboard.SetText(this.Thumbnail.FullSizeImageUrl ?? this.Thumbnail.MediaPageUrl);
298 catch (ExternalException ex)
300 MessageBox.Show(ex.Message);
305 public class ThumbnailDoubleClickEventArgs : EventArgs
307 public ThumbnailInfo Thumbnail { get; }
309 public ThumbnailDoubleClickEventArgs(ThumbnailInfo thumbnail)
311 this.Thumbnail = thumbnail;
315 public class ThumbnailImageSearchEventArgs : EventArgs
317 public string ImageUrl { get; }
319 public ThumbnailImageSearchEventArgs(string url)