From 7a0f0fdf4b560789d273039059d0c3b3d3147bcd Mon Sep 17 00:00:00 2001 From: Ben Olden-Cooligan Date: Tue, 21 Aug 2018 14:05:16 -0400 Subject: [PATCH] Cache options and poll when the progress form is visible --- NAPS2.Core/Scan/Sane/SaneScanDriver.cs | 12 +++-- NAPS2.Core/Util/CollectionExtensions.cs | 72 +++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 5 deletions(-) diff --git a/NAPS2.Core/Scan/Sane/SaneScanDriver.cs b/NAPS2.Core/Scan/Sane/SaneScanDriver.cs index 1292e305d..55e9d3019 100644 --- a/NAPS2.Core/Scan/Sane/SaneScanDriver.cs +++ b/NAPS2.Core/Scan/Sane/SaneScanDriver.cs @@ -17,6 +17,8 @@ namespace NAPS2.Scan.Sane { public const string DRIVER_NAME = "sane"; + private static Dictionary SaneOptionCache = new Dictionary(); + private readonly SaneWrapper saneWrapper; private readonly IFormFactory formFactory; private readonly IBlankDetector blankDetector; @@ -59,7 +61,7 @@ namespace NAPS2.Scan.Sane protected override IEnumerable ScanInternal() { // TODO: Support ADF - var options = GetOptions(); + var options = new Lazy(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 match) @@ -202,17 +204,17 @@ namespace NAPS2.Scan.Sane return options; } - private ScannedImage Transfer(KeyValueScanOptions options) + private ScannedImage Transfer(Lazy 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(); - form.Transfer = () => saneWrapper.ScanOne(ScanDevice.ID, options, form.OnProgress); + form.Transfer = () => saneWrapper.ScanOne(ScanDevice.ID, options.Value, form.OnProgress); form.PageNumber = 1; form.ShowDialog(); diff --git a/NAPS2.Core/Util/CollectionExtensions.cs b/NAPS2.Core/Util/CollectionExtensions.cs index bbd89ac6b..f26efe883 100644 --- a/NAPS2.Core/Util/CollectionExtensions.cs +++ b/NAPS2.Core/Util/CollectionExtensions.cs @@ -98,5 +98,77 @@ namespace NAPS2.Util } return default; } + + /// + /// Gets the element for the given key, or the provided value if none is present. + /// + /// + /// + /// + /// + /// + /// + public static TValue Get(this Dictionary dict, TKey key, TValue defaultValue) + { + if (dict.ContainsKey(key)) + { + return dict[key]; + } + return defaultValue; + } + + /// + /// Gets the element for the given key, or the provided value if none is present. + /// + /// + /// + /// + /// + /// + /// + public static TValue Get(this Dictionary dict, TKey key, Func defaultValue) + { + if (dict.ContainsKey(key)) + { + return dict[key]; + } + return defaultValue(); + } + + /// + /// Gets the element for the given key, or sets and returns the provided value if none is present. + /// + /// + /// + /// + /// + /// + /// + public static TValue GetOrSet(this Dictionary dict, TKey key, TValue defaultValue) + { + if (!dict.ContainsKey(key)) + { + dict[key] = defaultValue; + } + return dict[key]; + } + + /// + /// Gets the element for the given key, or sets and returns the provided value if none is present. + /// + /// + /// + /// + /// + /// + /// + public static TValue GetOrSet(this Dictionary dict, TKey key, Func defaultValue) + { + if (!dict.ContainsKey(key)) + { + dict[key] = defaultValue(); + } + return dict[key]; + } } }