ShareX/ShareX.HistoryLib/Forms/ImageHistoryForm.cs

220 lines
6.7 KiB
C#
Raw Normal View History

2013-11-03 23:53:49 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2018-01-02 03:59:14 +13:00
Copyright (c) 2007-2018 ShareX Team
2013-11-03 23:53:49 +13:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using Manina.Windows.Forms;
2014-12-11 12:19:28 +13:00
using ShareX.HelpersLib;
2013-11-03 23:53:49 +13:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using View = Manina.Windows.Forms.View;
2014-12-11 09:25:20 +13:00
namespace ShareX.HistoryLib
2013-11-03 23:53:49 +13:00
{
public partial class ImageHistoryForm : Form
2013-11-03 23:53:49 +13:00
{
public string HistoryPath { get; private set; }
2018-08-01 23:44:48 +12:00
public ImageHistorySettings Settings { get; private set; }
2018-07-24 04:01:20 +12:00
public string SearchText { get; set; }
2013-11-03 23:53:49 +13:00
private HistoryManager history;
private HistoryItemManager him;
private string defaultTitle;
2013-11-03 23:53:49 +13:00
2018-08-01 23:44:48 +12:00
public ImageHistoryForm(string historyPath, ImageHistorySettings settings, Action<string> uploadFile = null, Action<string> editImage = null)
2013-11-03 23:53:49 +13:00
{
InitializeComponent();
Icon = ShareXResources.Icon;
2013-11-03 23:53:49 +13:00
HistoryPath = historyPath;
2018-08-01 23:44:48 +12:00
Settings = settings;
2018-07-24 04:01:20 +12:00
tsMain.Renderer = new CustomToolStripProfessionalRenderer();
2018-08-01 23:44:48 +12:00
ilvImages.View = (View)Settings.ViewMode;
ilvImages.ThumbnailSize = Settings.ThumbnailSize;
2013-11-03 23:53:49 +13:00
him = new HistoryItemManager(uploadFile, editImage);
2013-11-03 23:53:49 +13:00
him.GetHistoryItems += him_GetHistoryItems;
2018-08-01 23:44:48 +12:00
defaultTitle = Text;
2018-08-02 01:12:18 +12:00
if (Settings.RememberSearchText)
{
tstbSearch.Text = Settings.SearchText;
}
2018-08-01 23:44:48 +12:00
Settings.WindowState.AutoHandleFormState(this);
2013-11-03 23:53:49 +13:00
}
private void UpdateTitle(int total, int filtered)
{
Text = $"{defaultTitle} (Total: {total.ToString("N0")} - Filtered: {filtered.ToString("N0")})";
}
2013-11-03 23:53:49 +13:00
private void RefreshHistoryItems()
2018-08-02 01:12:18 +12:00
{
UpdateSearchText();
ilvImages.Items.Clear();
ImageListViewItem[] ilvItems = GetHistoryItems().Select(hi => new ImageListViewItem(hi.Filepath) { Tag = hi }).ToArray();
ilvImages.Items.AddRange(ilvItems);
}
private void UpdateSearchText()
2013-11-03 23:53:49 +13:00
{
2018-07-24 05:04:18 +12:00
SearchText = tstbSearch.Text;
2018-08-02 01:12:18 +12:00
if (Settings.RememberSearchText)
2013-11-03 23:53:49 +13:00
{
2018-08-02 01:12:18 +12:00
Settings.SearchText = SearchText;
}
else
{
Settings.SearchText = "";
2013-11-03 23:53:49 +13:00
}
}
private IEnumerable<HistoryItem> GetHistoryItems()
2013-11-03 23:53:49 +13:00
{
2018-08-02 01:12:18 +12:00
if (history == null)
{
history = new HistoryManager(HistoryPath);
}
List<HistoryItem> historyItems = history.GetHistoryItems();
List<HistoryItem> filteredHistoryItems = new List<HistoryItem>();
for (int i = historyItems.Count - 1; i >= 0; i--)
2013-11-03 23:53:49 +13:00
{
HistoryItem hi = historyItems[i];
if (!string.IsNullOrEmpty(hi.Filepath) && Helpers.IsImageFile(hi.Filepath) &&
(string.IsNullOrEmpty(SearchText) || hi.Filename.Contains(SearchText, StringComparison.InvariantCultureIgnoreCase)))
{
filteredHistoryItems.Add(hi);
2018-08-02 01:12:18 +12:00
if (Settings.MaxItemCount > 0 && filteredHistoryItems.Count >= Settings.MaxItemCount)
{
break;
}
}
2013-11-03 23:53:49 +13:00
}
2018-08-02 01:12:18 +12:00
UpdateTitle(historyItems.Count, filteredHistoryItems.Count);
return filteredHistoryItems;
2013-11-03 23:53:49 +13:00
}
private HistoryItem[] him_GetHistoryItems()
{
return ilvImages.SelectedItems.Select(x => x.Tag as HistoryItem).ToArray();
}
#region Form events
private void ImageHistoryForm_Shown(object sender, EventArgs e)
{
2018-07-24 05:04:18 +12:00
tstbSearch.Focus();
2013-11-03 23:53:49 +13:00
Application.DoEvents();
this.ForceActivate();
2013-11-03 23:53:49 +13:00
RefreshHistoryItems();
}
private void ImageHistoryForm_KeyDown(object sender, KeyEventArgs e)
{
2018-07-24 05:04:18 +12:00
if (e.KeyCode == Keys.F5)
2013-11-03 23:53:49 +13:00
{
2018-07-24 05:04:18 +12:00
RefreshHistoryItems();
e.Handled = true;
2013-11-03 23:53:49 +13:00
}
}
private void ilvImages_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
him.cmsHistory.Show(ilvImages, e.X + 1, e.Y + 1);
}
}
private void ilvImages_SelectionChanged(object sender, EventArgs e)
{
him.RefreshInfo();
}
private void ilvImages_ItemDoubleClick(object sender, ItemClickEventArgs e)
{
him.ShowImagePreview();
}
2018-07-24 05:04:18 +12:00
private void tstbSearch_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
RefreshHistoryItems();
e.Handled = true;
e.SuppressKeyPress = true;
}
}
2018-07-24 04:01:20 +12:00
private void tsbSearch_Click(object sender, EventArgs e)
{
RefreshHistoryItems();
}
2018-08-01 23:44:48 +12:00
private void tsbSettings_Click(object sender, EventArgs e)
2013-11-03 23:53:49 +13:00
{
2018-08-01 23:44:48 +12:00
using (ImageHistorySettingsForm form = new ImageHistorySettingsForm(Settings))
{
form.ShowDialog();
}
2013-11-03 23:53:49 +13:00
2018-08-01 23:44:48 +12:00
ilvImages.View = (View)Settings.ViewMode;
ilvImages.ThumbnailSize = Settings.ThumbnailSize;
RefreshHistoryItems();
2013-11-03 23:53:49 +13:00
}
private void ilvImages_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyData)
{
default:
return;
case Keys.Enter:
him.OpenURL();
break;
case Keys.Control | Keys.Enter:
him.OpenFile();
break;
case Keys.Control | Keys.C:
him.CopyURL();
break;
}
e.Handled = true;
}
#endregion Form events
}
}