ShareX/ShareX.HistoryLib/Forms/ImageHistoryForm.cs

279 lines
9 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
2024-01-03 12:57:14 +13:00
Copyright (c) 2007-2024 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;
using ShareX.HistoryLib.Properties;
2013-11-03 23:53:49 +13:00
using System;
using System.Collections.Generic;
using System.IO;
2013-11-03 23:53:49 +13:00
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
2013-11-03 23:53:49 +13:00
using System.Windows.Forms;
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; }
public bool SearchInTags { get; set; } = true;
2013-11-03 23:53:49 +13:00
private HistoryItemManager him;
private string defaultTitle;
private List<HistoryItem> allHistoryItems;
2013-11-03 23:53:49 +13:00
public ImageHistoryForm(string historyPath, ImageHistorySettings settings, Action<string> uploadFile = null, Action<string> editImage = null, Action<string> pinToScreen = null)
2013-11-03 23:53:49 +13:00
{
InitializeComponent();
2019-06-25 05:59:48 +12:00
tsMain.Renderer = new ToolStripRoundedEdgeRenderer();
2013-11-03 23:53:49 +13:00
HistoryPath = historyPath;
2018-08-01 23:44:48 +12:00
Settings = settings;
2021-10-05 17:12:13 +13:00
ilvImages.SetRenderer(new HistoryImageListViewRenderer());
2018-08-01 23:44:48 +12:00
ilvImages.ThumbnailSize = Settings.ThumbnailSize;
2013-11-03 23:53:49 +13:00
2020-05-22 17:44:02 +12:00
if (ShareXResources.UseCustomTheme)
2019-07-03 18:35:34 +12:00
{
ilvImages.BorderStyle = BorderStyle.None;
}
him = new HistoryItemManager(uploadFile, editImage, pinToScreen);
2013-11-03 23:53:49 +13:00
him.GetHistoryItems += him_GetHistoryItems;
2019-06-25 07:33:17 +12:00
ilvImages.ContextMenuStrip = him.cmsHistory;
2018-08-01 23:44:48 +12:00
defaultTitle = Text;
tstbSearch.TextBox.HandleCreated += (sender, e) => tstbSearch.TextBox.SetWatermark(Resources.HistoryForm_Search_Watermark, true);
2018-08-02 01:12:18 +12:00
if (Settings.RememberSearchText)
{
tstbSearch.Text = Settings.SearchText;
}
2023-06-04 10:07:30 +12:00
ShareXResources.ApplyTheme(this, true);
if (Settings.RememberWindowState)
{
Settings.WindowState.ApplyFormState(this);
}
2013-11-03 23:53:49 +13:00
}
private void UpdateTitle(int total, int filtered)
{
Text = $"{defaultTitle} ({Resources.Total}: {total:N0} - {Resources.Filtered}: {filtered:N0})";
}
private async Task RefreshHistoryItems(bool mockData = false)
2018-08-02 01:12:18 +12:00
{
allHistoryItems = await GetHistoryItems(mockData);
tstbSearch.AutoCompleteCustomSource.Clear();
if (allHistoryItems.Count > 0)
{
tstbSearch.AutoCompleteCustomSource.AddRange(allHistoryItems.Select(x => x.TagsProcessName).Where(x => !string.IsNullOrWhiteSpace(x)).Distinct().ToArray());
}
ApplyFilter();
2018-08-02 01:12:18 +12:00
}
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 async Task<List<HistoryItem>> GetHistoryItems(bool mockData = false)
2013-11-03 23:53:49 +13:00
{
HistoryManager history;
if (mockData)
{
history = new HistoryManagerMock(HistoryPath);
}
else
2018-08-02 01:12:18 +12:00
{
history = new HistoryManagerJSON(HistoryPath);
2018-08-02 01:12:18 +12:00
}
List<HistoryItem> historyItems = await history.GetHistoryItemsAsync();
historyItems.Reverse();
return historyItems;
}
private void ApplyFilter()
{
UpdateSearchText();
ilvImages.Items.Clear();
List<HistoryItem> filteredHistoryItems = new List<HistoryItem>();
Regex regex = null;
if (!string.IsNullOrEmpty(SearchText))
{
string pattern = Regex.Escape(SearchText).Replace("\\?", ".").Replace("\\*", ".*");
regex = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
}
for (int i = 0; i < allHistoryItems.Count; i++)
2013-11-03 23:53:49 +13:00
{
HistoryItem hi = allHistoryItems[i];
if (!string.IsNullOrEmpty(hi.FilePath) && FileHelpers.IsImageFile(hi.FilePath) &&
(regex == null || regex.IsMatch(hi.FileName) || (SearchInTags && hi.Tags != null && hi.Tags.Any(tag => regex.IsMatch(tag.Value)))) &&
2019-10-22 00:20:21 +13:00
(!Settings.FilterMissingFiles || File.Exists(hi.FilePath)))
{
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
}
UpdateTitle(allHistoryItems.Count, filteredHistoryItems.Count);
ImageListViewItem[] ilvItems = filteredHistoryItems.Select(hi => new ImageListViewItem(hi.FilePath) { Tag = hi }).ToArray();
ilvImages.Items.AddRange(ilvItems);
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 async void ImageHistoryForm_Shown(object sender, EventArgs e)
2013-11-03 23:53:49 +13:00
{
2018-07-24 05:04:18 +12:00
tstbSearch.Focus();
this.ForceActivate();
await RefreshHistoryItems();
2013-11-03 23:53:49 +13:00
}
private void ImageHistoryForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (Settings.RememberWindowState)
{
Settings.WindowState.UpdateFormState(this);
}
}
private async void ImageHistoryForm_KeyDown(object sender, KeyEventArgs e)
2013-11-03 23:53:49 +13:00
{
switch (e.KeyData)
2013-11-03 23:53:49 +13:00
{
case Keys.F5:
await RefreshHistoryItems();
2023-06-04 10:07:30 +12:00
e.SuppressKeyPress = true;
break;
case Keys.Control | Keys.F5 when HelpersOptions.DevMode:
await RefreshHistoryItems(true);
2023-06-04 10:07:30 +12:00
e.SuppressKeyPress = true;
break;
2013-11-03 23:53:49 +13:00
}
}
private void ilvImages_SelectionChanged(object sender, EventArgs e)
{
2019-10-27 05:36:24 +13:00
him.UpdateSelectedHistoryItem();
2013-11-03 23:53:49 +13:00
}
private void ilvImages_ItemDoubleClick(object sender, ItemClickEventArgs e)
{
int currentImageIndex = ilvImages.SelectedItems[0].Index;
int modifiedImageIndex = 0;
int halfRange = 100;
int startIndex = Math.Max(currentImageIndex - halfRange, 0);
int endIndex = Math.Min(startIndex + (halfRange * 2) + 1, ilvImages.Items.Count);
List<string> filteredImages = new List<string>();
for (int i = startIndex; i < endIndex; i++)
{
string imageFilePath = ilvImages.Items[i].FileName;
if (i == currentImageIndex)
{
modifiedImageIndex = filteredImages.Count;
}
filteredImages.Add(imageFilePath);
}
ImageViewer.ShowImage(filteredImages.ToArray(), modifiedImageIndex);
2013-11-03 23:53:49 +13:00
}
2018-07-24 05:04:18 +12:00
private void tstbSearch_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
ApplyFilter();
2018-07-24 05:04:18 +12:00
e.SuppressKeyPress = true;
}
}
2018-07-24 04:01:20 +12:00
private void tsbSearch_Click(object sender, EventArgs e)
{
ApplyFilter();
2018-07-24 04:01:20 +12:00
}
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.ThumbnailSize = Settings.ThumbnailSize;
ApplyFilter();
2013-11-03 23:53:49 +13:00
}
private void ilvImages_KeyDown(object sender, KeyEventArgs e)
{
2023-06-04 10:07:30 +12:00
e.SuppressKeyPress = him.HandleKeyInput(e);
2013-11-03 23:53:49 +13:00
}
#endregion Form events
}
}