Skip to content

Commit

Permalink
Add "Always Ask" option to device selection form
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanfish committed Aug 2, 2024
1 parent 95bf090 commit ca95b34
Show file tree
Hide file tree
Showing 15 changed files with 152 additions and 75 deletions.
4 changes: 2 additions & 2 deletions NAPS2.Lib/Automation/ConsoleDevicePrompt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace NAPS2.Automation;

public class ConsoleDevicePrompt : IDevicePrompt
{
public Task<ScanDevice?> PromptForDevice(ScanOptions options)
public Task<DeviceChoice> PromptForDevice(ScanOptions options, bool allowAlwaysAsk)
{
// TODO: What's best to do here? Use a GUI prompt? A console prompt? Or just do nothing like this?
return Task.FromResult<ScanDevice?>(null);
return Task.FromResult(DeviceChoice.None);
}
}
4 changes: 3 additions & 1 deletion NAPS2.Lib/EtoForms/Desktop/DesktopScanController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public async Task ScanWithDevice(string deviceID)
// No profile for the device we're scanning with, so prompt to create one
var editSettingsForm = _formFactory.Create<EditProfileForm>();
editSettingsForm.ScanProfile = _config.DefaultProfileSettings();
editSettingsForm.NewProfile = true;
#if !MAC
#if NET6_0_OR_GREATER
if (OperatingSystem.IsWindows())
Expand All @@ -75,7 +76,7 @@ public async Task ScanWithDevice(string deviceID)
// Populate the device field automatically (because we can do that!)
using var deviceManager = new WiaDeviceManager();
using var device = deviceManager.FindDevice(deviceID);
editSettingsForm.CurrentDevice = new ScanDevice(Driver.Wia, deviceID, device.Name());
editSettingsForm.CurrentDevice = DeviceChoice.ForDevice(new ScanDevice(Driver.Wia, deviceID, device.Name()));
}
catch (WiaException)
{
Expand Down Expand Up @@ -125,6 +126,7 @@ public async Task ScanWithNewProfile()
{
var editSettingsForm = _formFactory.Create<EditProfileForm>();
editSettingsForm.ScanProfile = _config.DefaultProfileSettings();
editSettingsForm.NewProfile = true;
editSettingsForm.ShowModal();
if (!editSettingsForm.Result)
{
Expand Down
5 changes: 3 additions & 2 deletions NAPS2.Lib/EtoForms/EtoDevicePrompt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ public EtoDevicePrompt(IFormFactory formFactory, ScanningContext scanningContext
_scanningContext = scanningContext;
}

public Task<ScanDevice?> PromptForDevice(ScanOptions options)
public Task<DeviceChoice> PromptForDevice(ScanOptions options, bool allowAlwaysAsk)
{
// TODO: Extension method or something to turn InvokeGet into Task<T>?
return Task.FromResult(Invoker.Current.InvokeGet(() =>
{
var deviceForm = _formFactory.Create<SelectDeviceForm>();
deviceForm.ScanOptions = options;
deviceForm.AllowAlwaysAsk = allowAlwaysAsk;
deviceForm.ShowModal();
return deviceForm.SelectedDevice;
return deviceForm.Choice;
}));
}
}
1 change: 0 additions & 1 deletion NAPS2.Lib/EtoForms/EtoExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public static Image PadTo(this Image image, Size size)
using var graphics = new Graphics(newImage);
graphics.Clear(Colors.Transparent);
graphics.DrawImage(image, (size.Width - image.Width) / 2f, (size.Height - image.Height) / 2f);
image.Dispose();
return newImage;
}
return image;
Expand Down
1 change: 1 addition & 0 deletions NAPS2.Lib/EtoForms/Ui/BatchScanForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ private void NewProfile()
{
var fedit = FormFactory.Create<EditProfileForm>();
fedit.ScanProfile = Config.DefaultProfileSettings();
fedit.NewProfile = true;
fedit.ShowModal();
if (fedit.Result)
{
Expand Down
83 changes: 49 additions & 34 deletions NAPS2.Lib/EtoForms/Ui/EditProfileForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class EditProfileForm : EtoDialogBase
private readonly SliderWithTextBox _contrastSlider = new();

private ScanProfile _scanProfile = null!;
private ScanDevice? _currentDevice;
private DeviceChoice _currentDevice = DeviceChoice.None;
private bool _isDefault;
private bool _result;
private bool _suppressChangeEvent;
Expand All @@ -58,7 +58,6 @@ public EditProfileForm(Naps2Config config, IScanPerformer scanPerformer, ErrorOu
_enableAutoSave.CheckedChanged += EnableAutoSave_CheckedChanged;
_autoSaveSettings.Click += AutoSaveSettings_LinkClicked;
_advanced.Click += Advanced_Click;
_deviceName.KeyDown += DeviceName_KeyDown;
}

protected override void BuildLayout()
Expand Down Expand Up @@ -138,24 +137,34 @@ public ScanProfile ScanProfile
set => _scanProfile = value.Clone();
}

public ScanDevice? CurrentDevice
public bool NewProfile { get; set; }

public DeviceChoice CurrentDevice
{
get => _currentDevice;
set
{
_currentDevice = value;
_deviceName.Text = value?.Name ?? "";
_deviceDriver.Text = value?.Driver switch
if (value == DeviceChoice.None)
{
_deviceName.Text = "";
_deviceVis.IsVisible = false;
}
else
{
Driver.Wia => UiStrings.WiaDriver,
Driver.Twain => UiStrings.TwainDriver,
Driver.Sane => UiStrings.SaneDriver,
Driver.Escl => UiStrings.EsclDriver,
Driver.Apple => UiStrings.AppleDriver,
_ => ""
};
_deviceVis.IsVisible = _deviceName.Text.Length > 0;
_deviceIcon.Image = Icons.device.ToEtoImage();
_deviceName.Text = value.Device?.Name ?? UiStrings.AlwaysAsk;
_deviceDriver.Text = value.Driver switch
{
Driver.Wia => UiStrings.WiaDriver,
Driver.Twain => UiStrings.TwainDriver,
Driver.Sane => UiStrings.SaneDriver,
Driver.Escl => UiStrings.EsclDriver,
Driver.Apple => UiStrings.AppleDriver,
_ => ""
};
_deviceVis.IsVisible = true;
_deviceIcon.Image = value.AlwaysAsk ? Icons.ask.ToEtoImage() : Icons.device.ToEtoImage();
}
}
}

Expand All @@ -174,7 +183,18 @@ protected override void OnLoad(EventArgs e)
: Driver.Default);

_displayName.Text = ScanProfile.DisplayName;
CurrentDevice ??= ScanProfile.Device?.ToScanDevice(DeviceDriver);
if (CurrentDevice == DeviceChoice.None)
{
var device = ScanProfile.Device?.ToScanDevice(DeviceDriver);
if (device != null)
{
CurrentDevice = DeviceChoice.ForDevice(device);
}
else if (!NewProfile)
{
CurrentDevice = DeviceChoice.ForAlwaysAsk(DeviceDriver);
}
}
_isDefault = ScanProfile.IsDefault;

_paperSource.SelectedIndex = (int) ScanProfile.PaperSource;
Expand All @@ -201,16 +221,16 @@ protected override void OnLoad(EventArgs e)
private async void ChooseDevice(object? sender, EventArgs args)
{
ScanProfile.DriverName = DeviceDriver.ToString().ToLowerInvariant();
var device = await _scanPerformer.PromptForDevice(ScanProfile, NativeHandle);
if (device != null)
var choice = await _scanPerformer.PromptForDevice(ScanProfile, true, NativeHandle);
if (choice.Device != null || choice.AlwaysAsk)
{
if (string.IsNullOrEmpty(_displayName.Text) ||
CurrentDevice != null && CurrentDevice.Name == _displayName.Text)
if ((string.IsNullOrEmpty(_displayName.Text) ||
CurrentDevice.Device?.Name == _displayName.Text) && !choice.AlwaysAsk)
{
_displayName.Text = device.Name;
_displayName.Text = choice.Device!.Name;
}
CurrentDevice = device;
DeviceDriver = device.Driver;
CurrentDevice = choice;
DeviceDriver = choice.Driver;
UpdateEnabledControls();
}
}
Expand Down Expand Up @@ -293,20 +313,23 @@ private void SelectCustomPageSize(string? name, PageDimensions dimens)

private bool SaveSettings()
{
// Note: If CurrentDevice is null, that's fine. A prompt will be shown when scanning.

if (_displayName.Text == "")
{
_errorOutput.DisplayError(MiscResources.NameMissing);
return false;
}
if (CurrentDevice == DeviceChoice.None)
{
_errorOutput.DisplayError(MiscResources.NoDeviceSelected);
return false;
}
_result = true;

if (ScanProfile.IsLocked)
{
if (!ScanProfile.IsDeviceLocked)
{
ScanProfile.Device = ScanProfileDevice.FromScanDevice(CurrentDevice);
ScanProfile.Device = ScanProfileDevice.FromScanDevice(CurrentDevice.Device);
}
return true;
}
Expand All @@ -319,7 +342,7 @@ private bool SaveSettings()
{
Version = ScanProfile.CURRENT_VERSION,

Device = ScanProfileDevice.FromScanDevice(CurrentDevice),
Device = ScanProfileDevice.FromScanDevice(CurrentDevice.Device),
IsDefault = _isDefault,
DriverName = DeviceDriver.ToString().ToLowerInvariant(),
DisplayName = _displayName.Text,
Expand Down Expand Up @@ -479,14 +502,6 @@ private void EnableAutoSave_CheckedChanged(object? sender, EventArgs e)
_autoSaveSettings.Enabled = _enableAutoSave.IsChecked();
}

private void DeviceName_KeyDown(object? sender, KeyEventArgs e)
{
if (e.Key == Keys.Delete)
{
CurrentDevice = null;
}
}

private class PageSizeListItem : IListItem
{
public string Text { get; set; } = null!;
Expand Down
2 changes: 2 additions & 0 deletions NAPS2.Lib/EtoForms/Ui/ProfilesForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ private async void DoScan()
{
Version = ScanProfile.CURRENT_VERSION
};
editSettingsForm.NewProfile = true;
editSettingsForm.ShowModal();
if (!editSettingsForm.Result)
{
Expand Down Expand Up @@ -291,6 +292,7 @@ private void DoAdd()
{
var fedit = FormFactory.Create<EditProfileForm>();
fedit.ScanProfile = Config.DefaultProfileSettings();
fedit.NewProfile = true;
fedit.ShowModal();
if (fedit.Result)
{
Expand Down
Loading

0 comments on commit ca95b34

Please sign in to comment.