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 object uiLockObj = new object();
49 public ThumbnailInfo Thumbnail
51 get { return this.pictureBox[this.scrollBar.Value].Tag as ThumbnailInfo; }
54 public TweetThumbnail()
56 InitializeComponent();
59 public Task ShowThumbnailAsync(PostClass post)
61 return this.ShowThumbnailAsync(post, CancellationToken.None);
64 public async Task ShowThumbnailAsync(PostClass post, CancellationToken cancelToken)
66 var loadTasks = new List<Task>();
68 this.scrollBar.Enabled = false;
70 var thumbnails = (await this.GetThumbailInfoAsync(post, cancelToken))
73 cancelToken.ThrowIfCancellationRequested();
77 this.SetThumbnailCount(thumbnails.Length);
78 if (thumbnails.Length == 0) return;
80 for (int i = 0; i < thumbnails.Length; i++)
82 var thumb = thumbnails[i];
83 var picbox = this.pictureBox[i];
86 picbox.ContextMenuStrip = this.contextMenuStrip;
88 var loadTask = picbox.SetImageFromTask(() => thumb.LoadThumbnailImageAsync(cancelToken));
89 loadTasks.Add(loadTask);
91 var tooltipText = thumb.TooltipText;
92 if (!string.IsNullOrEmpty(tooltipText))
94 this.toolTip.SetToolTip(picbox, tooltipText);
97 cancelToken.ThrowIfCancellationRequested();
100 if (thumbnails.Length > 1)
101 this.scrollBar.Enabled = true;
104 if (this.ThumbnailLoading != null)
105 this.ThumbnailLoading(this, EventArgs.Empty);
107 await Task.WhenAll(loadTasks).ConfigureAwait(false);
110 private string GetImageSearchUri(string image_uri)
112 return @"https://www.google.com/searchbyimage?image_url=" + Uri.EscapeDataString(image_uri);
115 protected virtual Task<IEnumerable<ThumbnailInfo>> GetThumbailInfoAsync(PostClass post, CancellationToken token)
117 return ThumbnailGenerator.GetThumbnailsAsync(post, token);
123 /// <param name="count">表示するサムネイルの数</param>
124 protected void SetThumbnailCount(int count)
126 using (ControlTransaction.Layout(this, false))
128 this.panelPictureBox.Controls.Clear();
129 foreach (var picbox in this.pictureBox)
131 var memoryImage = picbox.Image;
134 if (memoryImage != null)
135 memoryImage.Dispose();
137 this.pictureBox.Clear();
139 this.scrollBar.Maximum = (count > 0) ? count - 1 : 0;
140 this.scrollBar.Value = 0;
142 for (int i = 0; i < count; i++)
144 var picbox = CreatePictureBox("pictureBox" + i);
145 picbox.Visible = (i == 0);
146 picbox.DoubleClick += this.pictureBox_DoubleClick;
148 this.panelPictureBox.Controls.Add(picbox);
149 this.pictureBox.Add(picbox);
154 protected virtual OTPictureBox CreatePictureBox(string name)
156 return new OTPictureBox()
159 SizeMode = PictureBoxSizeMode.Zoom,
161 Dock = DockStyle.Fill,
165 public void ScrollUp()
167 var newval = this.scrollBar.Value + this.scrollBar.SmallChange;
169 if (newval > this.scrollBar.Maximum)
170 newval = this.scrollBar.Maximum;
172 this.scrollBar.Value = newval;
175 public void ScrollDown()
177 var newval = this.scrollBar.Value - this.scrollBar.SmallChange;
179 if (newval < this.scrollBar.Minimum)
180 newval = this.scrollBar.Minimum;
182 this.scrollBar.Value = newval;
185 private void scrollBar_ValueChanged(object sender, EventArgs e)
187 using (ControlTransaction.Layout(this, false))
189 var value = this.scrollBar.Value;
190 for (var i = 0; i < this.pictureBox.Count; i++)
192 this.pictureBox[i].Visible = (i == value);
197 private void pictureBox_DoubleClick(object sender, EventArgs e)
199 var thumb = ((PictureBox)sender).Tag as ThumbnailInfo;
201 if (thumb == null) return;
203 if (this.ThumbnailDoubleClick != null)
205 this.ThumbnailDoubleClick(this, new ThumbnailDoubleClickEventArgs(thumb));
209 private void contextMenuStrip_Opening(object sender, CancelEventArgs e)
211 var picbox = (OTPictureBox)this.contextMenuStrip.SourceControl;
212 var thumb = (ThumbnailInfo)picbox.Tag;
214 var searchTargetUri = thumb.FullSizeImageUrl ?? thumb.ThumbnailUrl ?? null;
215 if (searchTargetUri != null)
217 this.searchSimilarImageMenuItem.Enabled = true;
218 this.searchSimilarImageMenuItem.Tag = searchTargetUri;
222 this.searchSimilarImageMenuItem.Enabled = false;
226 private void searchSimilarImageMenuItem_Click(object sender, EventArgs e)
228 var searchTargetUri = (string)this.searchSimilarImageMenuItem.Tag;
229 var searchUri = this.GetImageSearchUri(searchTargetUri);
231 if (this.ThumbnailImageSearchClick != null)
232 this.ThumbnailImageSearchClick(this, new ThumbnailImageSearchEventArgs(searchUri));
236 public class ThumbnailDoubleClickEventArgs : EventArgs
238 public ThumbnailInfo Thumbnail { get; private set; }
240 public ThumbnailDoubleClickEventArgs(ThumbnailInfo thumbnail)
242 this.Thumbnail = thumbnail;
246 public class ThumbnailImageSearchEventArgs : EventArgs
248 public string ImageUrl { get; private set; }
250 public ThumbnailImageSearchEventArgs(string url)