Skip to content

Commit

Permalink
Add events for details and shell info cache threads
Browse files Browse the repository at this point in the history
  • Loading branch information
oozcitak committed Mar 27, 2020
1 parent 1e582fa commit ae3d0e1
Show file tree
Hide file tree
Showing 6 changed files with 261 additions and 29 deletions.
4 changes: 4 additions & 0 deletions ImageListView/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ public enum CacheThread
/// The cache thread responsible for generating item details.
/// </summary>
Details,
/// <summary>
/// The cache thread responsible for generating shell information.
/// </summary>
ShellInfo,
}
/// <summary>
/// Represents the visual state of an image list column.
Expand Down
90 changes: 88 additions & 2 deletions ImageListView/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ namespace Manina.Windows.Forms
/// Represents the method that will handle the ItemDoubleClick event.
/// </summary>
/// <param name="sender">The ImageListView object that is the source of the event.</param>
/// <param name="e">A ItemClickEventArgs that contains event data.</param>
/// <param name="e">An ItemClickEventArgs that contains event data.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
public delegate void ItemDoubleClickEventHandler(object sender, ItemClickEventArgs e);
/// <summary>
/// Represents the method that will handle the ThumbnailCaching event.
/// </summary>
/// <param name="sender">The ImageListView object that is the source of the event.</param>
/// <param name="e">A ItemEventArgs that contains event data.</param>
/// <param name="e">A ThumbnailCachingEventArgs that contains event data.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
public delegate void ThumbnailCachingEventHandler(object sender, ThumbnailCachingEventArgs e);
/// <summary>
Expand All @@ -117,6 +117,34 @@ namespace Manina.Windows.Forms
[EditorBrowsable(EditorBrowsableState.Never)]
public delegate void ThumbnailCachedEventHandler(object sender, ThumbnailCachedEventArgs e);
/// <summary>
/// Represents the method that will handle the DetailsCaching event.
/// </summary>
/// <param name="sender">The ImageListView object that is the source of the event.</param>
/// <param name="e">An ItemEventArgs that contains event data.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
public delegate void DetailsCachingEventHandler(object sender, ItemEventArgs e);
/// <summary>
/// Represents the method that will handle the DetailsCached event.
/// </summary>
/// <param name="sender">The ImageListView object that is the source of the event.</param>
/// <param name="e">An ItemEventArgs that contains event data.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
public delegate void DetailsCachedEventHandler(object sender, ItemEventArgs e);
/// <summary>
/// Represents the method that will handle the ShellInfoCachingEventHandler event.
/// </summary>
/// <param name="sender">The ImageListView object that is the source of the event.</param>
/// <param name="e">A ShellInfoCachingEventArgs that contains event data.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
public delegate void ShellInfoCachingEventHandler(object sender, ShellInfoCachingEventArgs e);
/// <summary>
/// Represents the method that will handle the ShellInfoCachedEventHandler event.
/// </summary>
/// <param name="sender">The ImageListView object that is the source of the event.</param>
/// <param name="e">A ShellInfoCachedEventArgs that contains event data.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
public delegate void ShellInfoCachedEventHandler(object sender, ShellInfoCachedEventArgs e);
/// <summary>
/// Refreshes the owner control.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
Expand Down Expand Up @@ -570,6 +598,64 @@ public ThumbnailCachedEventArgs(ImageListViewItem item, Image thumbnail, Size si
}
}
/// <summary>
/// Represents the event arguments for the shell info caching event.
/// </summary>
[Serializable, ComVisible(true)]
public class ShellInfoCachingEventArgs : EventArgs
{
/// <summary>
/// Gets the file extension for which the shell info is requested.
/// </summary>
public string Extension { get; private set; }

/// <summary>
/// Initializes a new instance of the ShellInfoCachingEventArgs class.
/// </summary>
/// <param name="extension">The file extension for which the shell info is requested.</param>
public ShellInfoCachingEventArgs(string extension)
{
Extension = extension;
}
}
/// <summary>
/// Represents the event arguments for the shell info cached event.
/// </summary>
[Serializable, ComVisible(true)]
public class ShellInfoCachedEventArgs : EventArgs
{
/// <summary>
/// Gets the file extension for which the shell info is requested.
/// </summary>
public string Extension { get; private set; }
/// <summary>
/// Gets the small shell icon.
/// </summary>
public Image SmallIcon { get; private set; }
/// <summary>
/// Gets the large shell icon.
/// </summary>
public Image LargeIcon { get; private set; }
/// <summary>
/// Gets the shell file type.
/// </summary>
public string FileType { get; private set; }

/// <summary>
/// Initializes a new instance of the ShellInfoCachedEventArgs class.
/// </summary>
/// <param name="extension">The file extension for which the shell info is requested.</param>
/// <param name="smallIcon">The small shell icon.</param>
/// <param name="largeIcon">The large shell icon.</param>
/// <param name="filetype">The shell file type.</param>
public ShellInfoCachedEventArgs(string extension, Image smallIcon, Image largeIcon, string filetype)
{
Extension = extension;
SmallIcon = smallIcon;
LargeIcon = largeIcon;
FileType = filetype;
}
}
/// <summary>
/// Represents the event arguments for the pane resized event.
/// </summary>
[Serializable, ComVisible(true)]
Expand Down
80 changes: 80 additions & 0 deletions ImageListView/ImageListView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2263,6 +2263,56 @@ protected virtual void OnPaneResizing(PaneResizingEventArgs e)
PaneResizing?.Invoke(this, e);
}
/// <summary>
/// Raises the DetailsCaching event.
/// </summary>
/// <param name="e">An ItemEventArgs that contains event data.</param>
protected virtual void OnDetailsCaching(ItemEventArgs e)
{
DetailsCaching?.Invoke(this, e);
}
/// <summary>
/// Raises the DetailsCached event.
/// </summary>
/// <param name="e">An ItemEventArgs that contains event data.</param>
protected virtual void OnDetailsCached(ItemEventArgs e)
{
DetailsCached?.Invoke(this, e);
}
/// <summary>
/// Raises the DetailsCaching event.
/// This method is invoked from the thumbnail thread.
/// </summary>
internal virtual void OnDetailsCachingInternal(Guid guid)
{
if (mItems.TryGetValue(guid, out ImageListViewItem item))
OnDetailsCaching(new ItemEventArgs(item));
}
/// <summary>
/// Raises the DetailsCached event.
/// This method is invoked from the thumbnail thread.
/// </summary>
internal virtual void OnDetailsCachedInternal(Guid guid)
{
if (mItems.TryGetValue(guid, out ImageListViewItem item))
OnDetailsCached(new ItemEventArgs(item));
}
/// <summary>
/// Raises the ShellInfoCaching event.
/// </summary>
/// <param name="e">A ShellInfoCachingEventArgs that contains event data.</param>
protected internal virtual void OnShellInfoCaching(ShellInfoCachingEventArgs e)
{
ShellInfoCaching?.Invoke(this, e);
}
/// <summary>
/// Raises the ShellInfoCached event.
/// </summary>
/// <param name="e">A ShellInfoCachedEventArgs that contains event data.</param>
protected internal virtual void OnShellInfoCached(ShellInfoCachedEventArgs e)
{
ShellInfoCached?.Invoke(this, e);
}
/// <summary>
/// Raises the CacheError event.
/// This method is invoked from the thumbnail thread.
/// </summary>
Expand All @@ -2276,6 +2326,16 @@ internal void OnCacheErrorInternal(Guid guid, Exception error, CacheThread cache
OnCacheError(new CacheErrorEventArgs(item, error, cacheThread));
}
/// <summary>
/// Raises the CacheError event.
/// This method is invoked from the thumbnail thread.
/// </summary>
/// <param name="error">The error that occurred during an asynchronous operation.</param>
/// <param name="cacheThread">The thread raising the error.</param>
internal void OnCacheErrorInternal(Exception error, CacheThread cacheThread)
{
OnCacheError(new CacheErrorEventArgs(null, error, cacheThread));
}
/// <summary>
/// Raises the ThumbnailCached event.
/// This method is invoked from the thumbnail thread.
/// </summary>
Expand Down Expand Up @@ -2415,6 +2475,26 @@ protected virtual void OnItemCollectionChanged(ItemCollectionChangedEventArgs e)
/// </summary>
[Category("Action"), Browsable(true), Description("Occurs while the pane is being resized.")]
public event PaneResizingEventHandler PaneResizing;
/// <summary>
/// Occurs before an item details is cached.
/// </summary>
[Category("Action"), Browsable(true), Description("Occurs before an item details is cached.")]
public event DetailsCachingEventHandler DetailsCaching;
/// <summary>
/// Occurs after an item details is cached.
/// </summary>
[Category("Action"), Browsable(true), Description("Occurs after an item details is cached.")]
public event DetailsCachedEventHandler DetailsCached;
/// <summary>
/// Occurs before shell info for a file extension is cached.
/// </summary>
[Category("Action"), Browsable(true), Description("Occurs before shell info for a file extension is cached.")]
public event ShellInfoCachingEventHandler ShellInfoCaching;
/// <summary>
/// Occurs after shell info for a file extension is cached.
/// </summary>
[Category("Action"), Browsable(true), Description("Occurs after shell info for a file extension is cached.")]
public event ShellInfoCachedEventHandler ShellInfoCached;
#endregion
}
}
8 changes: 8 additions & 0 deletions ImageListView/ImageListViewCacheMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ private void bw_RunWorkerCompleted(object sender, QueuedWorkerCompletedEventArgs
if (mImageListView != null && mImageListView.IsItemVisible(request.Guid))
mImageListView.Refresh(false, true);

// Raise the DetailsCached event
if (details != null && mImageListView != null)
mImageListView.OnDetailsCachedInternal(request.Guid);

// Raise the CacheError event
if (e.Error != null && mImageListView != null)
mImageListView.OnCacheErrorInternal(request.Guid, e.Error, CacheThread.Details);
Expand Down Expand Up @@ -318,6 +322,10 @@ private void RunWorker(CacheRequest item)
else
processing.Add(item.Guid, false);

// Raise the DetailsCaching event
if (mImageListView != null)
mImageListView.OnDetailsCachingInternal(item.Guid);

// Add the item to the queue for processing
bw.RunWorkerAsync(item);
}
Expand Down
43 changes: 24 additions & 19 deletions ImageListView/ImageListViewCacheShellInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal class ImageListViewCacheShellInfo : IDisposable
#region Member Variables
private QueuedBackgroundWorker bw;
private SynchronizationContext context;
private SendOrPostCallback checkProcessingCallback;
private readonly SendOrPostCallback checkProcessingCallback;

private ImageListView mImageListView;

Expand Down Expand Up @@ -210,8 +210,7 @@ private void CanContinueProcessing(object argument)
bool canProcess = true;

// Is it already cached?
CacheItem existing;
if (shellCache.TryGetValue(arg.Extension, out existing))
if (shellCache.TryGetValue(arg.Extension, out CacheItem existing))
{
if (existing.SmallIcon != null && existing.LargeIcon != null)
canProcess = false;
Expand All @@ -228,7 +227,7 @@ private void CanContinueProcessing(object argument)
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="Manina.Windows.Forms.QueuedWorkerCompletedEventArgs"/>
/// instance containing the event data.</param>
void bw_RunWorkerCompleted(object sender, QueuedWorkerCompletedEventArgs e)
private void bw_RunWorkerCompleted(object sender, QueuedWorkerCompletedEventArgs e)
{
CacheItem result = e.Result as CacheItem;

Expand All @@ -238,8 +237,7 @@ void bw_RunWorkerCompleted(object sender, QueuedWorkerCompletedEventArgs e)
// Add to cache
if (result != null)
{
CacheItem existing = null;
if (shellCache.TryGetValue(result.Extension, out existing))
if (shellCache.TryGetValue(result.Extension, out CacheItem existing))
{
existing.Dispose();
shellCache.Remove(result.Extension);
Expand All @@ -250,14 +248,23 @@ void bw_RunWorkerCompleted(object sender, QueuedWorkerCompletedEventArgs e)
// Refresh the control lazily
if (result != null && mImageListView != null)
mImageListView.Refresh(false, true);

// Raise the ShellInfoCached event
if (result != null && mImageListView != null)
mImageListView.OnShellInfoCached(new ShellInfoCachedEventArgs(result.Extension, result.SmallIcon, result.LargeIcon, result.FileType));

// Raise the CacheError event
if (e.Error != null && mImageListView != null)
mImageListView.OnCacheErrorInternal(e.Error, CacheThread.ShellInfo);
}

/// <summary>
/// Handles the DoWork event of the queued background worker.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="Manina.Windows.Forms.QueuedWorkerDoWorkEventArgs"/> instance
/// containing the event data.</param>
void bw_DoWork(object sender, QueuedWorkerDoWorkEventArgs e)
private void bw_DoWork(object sender, QueuedWorkerDoWorkEventArgs e)
{
string extension = e.Argument as string;

Expand Down Expand Up @@ -307,8 +314,7 @@ public CacheState GetCacheState(string extension)
if (string.IsNullOrEmpty(extension))
throw new ArgumentException("extension cannot be null", "extension");

CacheItem item = null;
if (shellCache.TryGetValue(extension, out item))
if (shellCache.TryGetValue(extension, out CacheItem item))
return item.State;

return CacheState.Unknown;
Expand Down Expand Up @@ -342,8 +348,7 @@ public void Remove(string extension)
if (string.IsNullOrEmpty(extension))
throw new ArgumentException("extension cannot be null", "extension");

CacheItem item = null;
if (shellCache.TryGetValue(extension, out item))
if (shellCache.TryGetValue(extension, out CacheItem item))
{
item.Dispose();
shellCache.Remove(extension);
Expand All @@ -359,8 +364,7 @@ public void Add(string extension)
throw new ArgumentException("extension cannot be null", "extension");

// Already cached?
CacheItem item = null;
if (shellCache.TryGetValue(extension, out item))
if (shellCache.TryGetValue(extension, out CacheItem item))
return;

// Add to cache queue
Expand All @@ -376,8 +380,7 @@ public Image GetSmallIcon(string extension)
if (string.IsNullOrEmpty(extension))
throw new ArgumentException("extension cannot be null", "extension");

CacheItem item = null;
if (shellCache.TryGetValue(extension, out item))
if (shellCache.TryGetValue(extension, out CacheItem item))
{
return item.SmallIcon;
}
Expand All @@ -393,8 +396,7 @@ public Image GetLargeIcon(string extension)
if (string.IsNullOrEmpty(extension))
throw new ArgumentException("extension cannot be null", "extension");

CacheItem item = null;
if (shellCache.TryGetValue(extension, out item))
if (shellCache.TryGetValue(extension, out CacheItem item))
{
return item.LargeIcon;
}
Expand All @@ -410,8 +412,7 @@ public string GetFileType(string extension)
if (string.IsNullOrEmpty(extension))
throw new ArgumentException("extension cannot be null", "extension");

CacheItem item = null;
if (shellCache.TryGetValue(extension, out item))
if (shellCache.TryGetValue(extension, out CacheItem item))
{
return item.FileType;
}
Expand All @@ -436,6 +437,10 @@ private void RunWorker(string extension)
else
processing.Add(extension, false);

// Raise the ShellInfoCaching event
if (mImageListView != null)
mImageListView.OnShellInfoCaching(new ShellInfoCachingEventArgs(extension));

// Add the item to the queue for processing
bw.RunWorkerAsync(extension);
}
Expand Down
Loading

0 comments on commit ae3d0e1

Please sign in to comment.