ShareX/ShareX.HistoryLib/Forms/ImageHistoryForm.cs

247 lines
8.2 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
2021-07-29 15:22:51 +12:00
Copyright (c) 2007-2021 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;
2021-07-29 16:05:20 +12:00
using System.Drawing;
using System.IO;
2013-11-03 23:53:49 +13:00
using System.Linq;
using System.Text.RegularExpressions;
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;
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();
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;
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;
ilvImages.Colors.BackColor = ShareXResources.Theme.DarkBackgroundColor;
ilvImages.Colors.BorderColor = ShareXResources.Theme.DarkBackgroundColor;
ilvImages.Colors.ForeColor = ShareXResources.Theme.TextColor;
2021-07-29 16:05:20 +12:00
ilvImages.Colors.ImageInnerBorderColor = Color.Transparent;
ilvImages.Colors.ImageOuterBorderColor = Color.Transparent;
ilvImages.Colors.SelectedForeColor = ShareXResources.Theme.TextColor;
ilvImages.Colors.UnFocusedForeColor = ShareXResources.Theme.TextColor;
2019-07-03 18:35:34 +12:00
}
him = new HistoryItemManager(uploadFile, editImage);
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;
}
2019-06-25 07:33:17 +12:00
ShareXResources.ApplyTheme(this);
if (Settings.RememberWindowState)
{
Settings.WindowState.ApplyFormState(this);
}
2013-11-03 23:53:49 +13:00
}
private void UpdateTitle(int total, int filtered)
{
Text = $"{defaultTitle} (Total: {total:N0} - Filtered: {filtered:N0})";
}
private void RefreshHistoryItems(bool mockData = false)
2018-08-02 01:12:18 +12:00
{
UpdateSearchText();
ilvImages.Items.Clear();
IEnumerable<HistoryItem> historyItems = GetHistoryItems(mockData);
ImageListViewItem[] ilvItems = historyItems.Select(hi => new ImageListViewItem(hi.FilePath) { Tag = hi }).ToArray();
2018-08-02 01:12:18 +12:00
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(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 = history.GetHistoryItems();
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 = historyItems.Count - 1; i >= 0; i--)
2013-11-03 23:53:49 +13:00
{
HistoryItem hi = historyItems[i];
2019-10-22 00:20:21 +13:00
if (!string.IsNullOrEmpty(hi.FilePath) && Helpers.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
}
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_FormClosing(object sender, FormClosingEventArgs e)
{
if (Settings.RememberWindowState)
{
Settings.WindowState.UpdateFormState(this);
}
}
2013-11-03 23:53:49 +13:00
private void ImageHistoryForm_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyData)
2013-11-03 23:53:49 +13:00
{
case Keys.F5:
RefreshHistoryItems();
e.Handled = true;
break;
case Keys.Control | Keys.F5 when HelpersOptions.DevMode:
RefreshHistoryItems(true);
e.Handled = 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)
{
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.ThumbnailSize = Settings.ThumbnailSize;
RefreshHistoryItems();
2013-11-03 23:53:49 +13:00
}
private void ilvImages_KeyDown(object sender, KeyEventArgs e)
{
e.Handled = e.SuppressKeyPress = him.HandleKeyInput(e);
2013-11-03 23:53:49 +13:00
}
#endregion Form events
}
}