diff --git a/ShareX.HistoryLib/Forms/HistoryForm.Designer.cs b/ShareX.HistoryLib/Forms/HistoryForm.Designer.cs index 19ba5e6ea..b277cb0f2 100644 --- a/ShareX.HistoryLib/Forms/HistoryForm.Designer.cs +++ b/ShareX.HistoryLib/Forms/HistoryForm.Designer.cs @@ -220,8 +220,11 @@ private void InitializeComponent() this.lvHistory.Name = "lvHistory"; this.lvHistory.UseCompatibleStateImageBehavior = false; this.lvHistory.View = System.Windows.Forms.View.Details; + this.lvHistory.VirtualMode = true; + this.lvHistory.CacheVirtualItems += new System.Windows.Forms.CacheVirtualItemsEventHandler(this.lvHistory_CacheVirtualItems); this.lvHistory.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.lvHistory_ItemDrag); this.lvHistory.ItemSelectionChanged += new System.Windows.Forms.ListViewItemSelectionChangedEventHandler(this.lvHistory_ItemSelectionChanged); + this.lvHistory.RetrieveVirtualItem += new System.Windows.Forms.RetrieveVirtualItemEventHandler(this.lvHistory_RetrieveVirtualItem); this.lvHistory.KeyDown += new System.Windows.Forms.KeyEventHandler(this.lvHistory_KeyDown); this.lvHistory.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.lvHistory_MouseDoubleClick); // diff --git a/ShareX.HistoryLib/Forms/HistoryForm.cs b/ShareX.HistoryLib/Forms/HistoryForm.cs index f74812074..38d008f9b 100644 --- a/ShareX.HistoryLib/Forms/HistoryForm.cs +++ b/ShareX.HistoryLib/Forms/HistoryForm.cs @@ -41,9 +41,12 @@ public partial class HistoryForm : Form private HistoryItemManager him; private HistoryItem[] allHistoryItems; + private HistoryItem[] filteredHistoryItems; private string defaultTitle; private Dictionary typeNamesLocaleLookup; private string[] allTypeNames; + private ListViewItem[] listViewCache; + private int listViewFirstItem; public HistoryForm(string historyPath, HistorySettings settings, Action uploadFile = null, Action editImage = null) { @@ -137,7 +140,7 @@ private void RefreshHistoryItems(bool mockData = false) private HistoryItem[] him_GetHistoryItems() { - return lvHistory.SelectedItems.Cast().Select(x => x.Tag as HistoryItem).ToArray(); + return lvHistory.SelectedIndices.Cast().Select(i => filteredHistoryItems[i]).ToArray(); } private HistoryItem[] GetHistoryItems(bool mockData = false) @@ -163,8 +166,13 @@ private void ApplyFilter(HistoryFilter filter) if (allHistoryItems != null && allHistoryItems.Length > 0) { IEnumerable historyItems = filter.ApplyFilter(allHistoryItems); + filteredHistoryItems = historyItems.ToArray(); - AddHistoryItems(historyItems.ToArray()); + UpdateTitle(filteredHistoryItems); + + listViewCache = null; + lvHistory.VirtualListSize = 0; + lvHistory.VirtualListSize = filteredHistoryItems.Length; } } @@ -213,54 +221,35 @@ private void ApplyFilterAdvanced() ApplyFilter(filter); } - private void AddHistoryItems(HistoryItem[] historyItems) + private ListViewItem CreateListViewItem(int index) { - Cursor = Cursors.WaitCursor; + HistoryItem hi = filteredHistoryItems[index]; - UpdateTitle(historyItems); + ListViewItem lvi = new ListViewItem(); - lvHistory.Items.Clear(); - - ListViewItem[] listViewItems = new ListViewItem[historyItems.Length]; - - for (int i = 0; i < historyItems.Length; i++) + if (hi.Type.Equals("Image", StringComparison.InvariantCultureIgnoreCase)) { - HistoryItem hi = historyItems[i]; - ListViewItem lvi = listViewItems[i] = new ListViewItem(); - - if (hi.Type.Equals("Image", StringComparison.InvariantCultureIgnoreCase)) - { - lvi.ImageIndex = 0; - } - else if (hi.Type.Equals("Text", StringComparison.InvariantCultureIgnoreCase)) - { - lvi.ImageIndex = 1; - } - else if (hi.Type.Equals("File", StringComparison.InvariantCultureIgnoreCase)) - { - lvi.ImageIndex = 2; - } - else - { - lvi.ImageIndex = 3; - } - - lvi.SubItems.Add(hi.DateTime.ToString()).Tag = hi.DateTime; - lvi.SubItems.Add(hi.FileName); - lvi.SubItems.Add(hi.URL); - lvi.Tag = hi; + lvi.ImageIndex = 0; + } + else if (hi.Type.Equals("Text", StringComparison.InvariantCultureIgnoreCase)) + { + lvi.ImageIndex = 1; + } + else if (hi.Type.Equals("File", StringComparison.InvariantCultureIgnoreCase)) + { + lvi.ImageIndex = 2; + } + else + { + lvi.ImageIndex = 3; } - lvHistory.Items.AddRange(listViewItems); - lvHistory.FillLastColumn(); - lvHistory.Focus(); + lvi.SubItems.Add(hi.DateTime.ToString()).Tag = hi.DateTime; + lvi.SubItems.Add(hi.FileName); + lvi.SubItems.Add(hi.URL); + lvi.Tag = hi; - if (lvHistory.Items.Count > 0) - { - lvHistory.Items[0].Selected = true; - } - - Cursor = Cursors.Default; + return lvi; } private void UpdateTitle(HistoryItem[] historyItems = null) @@ -396,9 +385,7 @@ private string OutputStats(HistoryItem[] historyItems) private void HistoryForm_Shown(object sender, EventArgs e) { Refresh(); - RefreshHistoryItems(); - this.ForceActivate(); } @@ -517,6 +504,35 @@ private void btnAdvancedSearchReset_Click(object sender, EventArgs e) ApplyFilterAdvanced(); } + private void lvHistory_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e) + { + if (listViewCache != null && e.ItemIndex >= listViewFirstItem && e.ItemIndex < listViewFirstItem + listViewCache.Length) + { + e.Item = listViewCache[e.ItemIndex - listViewFirstItem]; + } + else + { + e.Item = CreateListViewItem(e.ItemIndex); + } + } + + private void lvHistory_CacheVirtualItems(object sender, CacheVirtualItemsEventArgs e) + { + if (listViewCache != null && e.StartIndex >= listViewFirstItem && e.EndIndex <= listViewFirstItem + listViewCache.Length) + { + return; + } + + listViewFirstItem = e.StartIndex; + int length = e.EndIndex - e.StartIndex + 1; + listViewCache = new ListViewItem[length]; + + for (int i = 0; i < length; i++) + { + listViewCache[i] = CreateListViewItem(e.StartIndex + i); + } + } + private void lvHistory_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) { UpdateControls(); @@ -539,9 +555,10 @@ private void lvHistory_ItemDrag(object sender, ItemDragEventArgs e) { List selection = new List(); - foreach (ListViewItem item in lvHistory.SelectedItems) + foreach (int index in lvHistory.SelectedIndices) { - HistoryItem hi = (HistoryItem)item.Tag; + HistoryItem hi = filteredHistoryItems[index]; + if (File.Exists(hi.FilePath)) { selection.Add(hi.FilePath); diff --git a/ShareX.HistoryLib/Forms/HistoryForm.resx b/ShareX.HistoryLib/Forms/HistoryForm.resx index ae5607b46..0d48ec4e7 100644 --- a/ShareX.HistoryLib/Forms/HistoryForm.resx +++ b/ShareX.HistoryLib/Forms/HistoryForm.resx @@ -586,7 +586,7 @@ lvHistory - ShareX.HelpersLib.MyListView, ShareX.HelpersLib, Version=13.6.2.0, Culture=neutral, PublicKeyToken=null + ShareX.HelpersLib.MyListView, ShareX.HelpersLib, Version=13.7.0.0, Culture=neutral, PublicKeyToken=null tscHistory.ContentPanel @@ -718,7 +718,7 @@ 3, 0 - 508, 25 + 477, 25 0 @@ -799,7 +799,7 @@ 0, 0 - 627, 445 + 627, 442 3 @@ -808,7 +808,7 @@ pbThumbnail - ShareX.HelpersLib.MyPictureBox, ShareX.HelpersLib, Version=13.6.2.0, Culture=neutral, PublicKeyToken=null + ShareX.HelpersLib.MyPictureBox, ShareX.HelpersLib, Version=13.7.0.0, Culture=neutral, PublicKeyToken=null scHistoryItemInfo.Panel1 @@ -838,7 +838,7 @@ 0, 0 - 627, 209 + 627, 212 0 @@ -871,7 +871,7 @@ 627, 661 - 445 + 442 7 @@ -883,7 +883,7 @@ scHistoryItemInfo - ShareX.HelpersLib.SplitContainerCustomSplitter, ShareX.HelpersLib, Version=13.6.2.0, Culture=neutral, PublicKeyToken=null + ShareX.HelpersLib.SplitContainerCustomSplitter, ShareX.HelpersLib, Version=13.7.0.0, Culture=neutral, PublicKeyToken=null scMain.Panel2 @@ -922,7 +922,7 @@ scMain - ShareX.HelpersLib.SplitContainerCustomSplitter, ShareX.HelpersLib, Version=13.6.2.0, Culture=neutral, PublicKeyToken=null + ShareX.HelpersLib.SplitContainerCustomSplitter, ShareX.HelpersLib, Version=13.7.0.0, Culture=neutral, PublicKeyToken=null $this