Cache options and poll when the progress form is visible

This commit is contained in:
Ben Olden-Cooligan 2018-08-21 14:05:16 -04:00
parent 957c6f21bf
commit 7a0f0fdf4b
2 changed files with 79 additions and 5 deletions

View File

@ -17,6 +17,8 @@ namespace NAPS2.Scan.Sane
{
public const string DRIVER_NAME = "sane";
private static Dictionary<string, SaneOptionCollection> SaneOptionCache = new Dictionary<string, SaneOptionCollection>();
private readonly SaneWrapper saneWrapper;
private readonly IFormFactory formFactory;
private readonly IBlankDetector blankDetector;
@ -59,7 +61,7 @@ namespace NAPS2.Scan.Sane
protected override IEnumerable<ScannedImage> ScanInternal()
{
// TODO: Support ADF
var options = GetOptions();
var options = new Lazy<KeyValueScanOptions>(GetOptions);
var img = Transfer(options);
if (img != null)
{
@ -69,7 +71,7 @@ namespace NAPS2.Scan.Sane
private KeyValueScanOptions GetOptions()
{
var saneOptions = saneWrapper.GetOptions(ScanDevice.ID);
var saneOptions = SaneOptionCache.GetOrSet(ScanDevice.ID, () => saneWrapper.GetOptions(ScanDevice.ID));
var options = new KeyValueScanOptions(ScanProfile.KeyValueOptions ?? new KeyValueScanOptions());
bool ChooseStringOption(string name, Func<string, bool> match)
@ -202,17 +204,17 @@ namespace NAPS2.Scan.Sane
return options;
}
private ScannedImage Transfer(KeyValueScanOptions options)
private ScannedImage Transfer(Lazy<KeyValueScanOptions> options)
{
Stream stream;
if (ScanParams.NoUI)
{
stream = saneWrapper.ScanOne(ScanDevice.ID, options, null);
stream = saneWrapper.ScanOne(ScanDevice.ID, options.Value, null);
}
else
{
var form = formFactory.Create<FScanProgress>();
form.Transfer = () => saneWrapper.ScanOne(ScanDevice.ID, options, form.OnProgress);
form.Transfer = () => saneWrapper.ScanOne(ScanDevice.ID, options.Value, form.OnProgress);
form.PageNumber = 1;
form.ShowDialog();

View File

@ -98,5 +98,77 @@ namespace NAPS2.Util
}
return default;
}
/// <summary>
/// Gets the element for the given key, or the provided value if none is present.
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
/// <param name="dict"></param>
/// <param name="key"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static TValue Get<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue defaultValue)
{
if (dict.ContainsKey(key))
{
return dict[key];
}
return defaultValue;
}
/// <summary>
/// Gets the element for the given key, or the provided value if none is present.
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
/// <param name="dict"></param>
/// <param name="key"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static TValue Get<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, Func<TValue> defaultValue)
{
if (dict.ContainsKey(key))
{
return dict[key];
}
return defaultValue();
}
/// <summary>
/// Gets the element for the given key, or sets and returns the provided value if none is present.
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
/// <param name="dict"></param>
/// <param name="key"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static TValue GetOrSet<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue defaultValue)
{
if (!dict.ContainsKey(key))
{
dict[key] = defaultValue;
}
return dict[key];
}
/// <summary>
/// Gets the element for the given key, or sets and returns the provided value if none is present.
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
/// <param name="dict"></param>
/// <param name="key"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static TValue GetOrSet<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, Func<TValue> defaultValue)
{
if (!dict.ContainsKey(key))
{
dict[key] = defaultValue();
}
return dict[key];
}
}
}