Migrate to new EnumDropDownWidget

This commit is contained in:
Ben Olden-Cooligan 2024-08-10 11:13:08 -07:00
parent 260f281c09
commit 22543d79c2
10 changed files with 75 additions and 113 deletions

View File

@ -144,41 +144,6 @@ public static class C
return dropDown;
}
public static DropDown EnumDropDown<T>(params T[] values) where T : Enum
{
var dropDown = new DropDown();
EtoPlatform.Current.ConfigureDropDown(dropDown);
foreach (var item in values)
{
dropDown.Items.Add(new ListItem
{
Key = item.ToString(),
Text = item.Description()
});
}
return dropDown;
}
public static DropDown EnumDropDown<T>() where T : Enum
{
return EnumDropDown<T>(x => x.Description());
}
public static DropDown EnumDropDown<T>(Func<T, string> format) where T : Enum
{
var dropDown = new DropDown();
EtoPlatform.Current.ConfigureDropDown(dropDown);
foreach (var item in Enum.GetValues(typeof(T)))
{
dropDown.Items.Add(new ListItem
{
Key = item.ToString(),
Text = format((T) item)
});
}
return dropDown;
}
public static CheckBox CheckBox(string text) => new() { Text = text };
public static Button CancelButton(Dialog dialog, string? text = null) =>

View File

@ -27,15 +27,9 @@ public class AdvancedProfileForm : EtoDialogBase
"FlipBackSidesOfDuplexPages")
};
private readonly DropDown _wiaVersion = C.EnumDropDown<WiaApiVersion>(value => value switch
{
WiaApiVersion.Default => SettingsResources.WiaVersion_Default,
WiaApiVersion.Wia10 => SettingsResources.WiaVersion_Wia10,
WiaApiVersion.Wia20 => SettingsResources.WiaVersion_Wia20,
_ => value.ToString()
});
private readonly EnumDropDownWidget<WiaApiVersion> _wiaVersion = new();
private readonly DropDown _twainImpl = C.EnumDropDown<TwainImpl>();
private readonly EnumDropDownWidget<TwainImpl> _twainImpl = new();
private readonly CheckBox _twainProgress = new() { Text = UiStrings.ShowNativeTwainProgress };
private readonly Button _restoreDefaults = new() { Text = UiStrings.RestoreDefaults };
@ -44,9 +38,16 @@ public class AdvancedProfileForm : EtoDialogBase
_restoreDefaults.Click += RestoreDefaults_Click;
_maximumQuality.CheckedChanged += MaximumQuality_CheckedChanged;
_excludeBlank.CheckedChanged += ExcludeBlank_CheckedChanged;
_wiaVersion.Format = value => value switch
{
WiaApiVersion.Default => SettingsResources.WiaVersion_Default,
WiaApiVersion.Wia10 => SettingsResources.WiaVersion_Wia10,
WiaApiVersion.Wia20 => SettingsResources.WiaVersion_Wia20,
_ => value.ToString()
};
if (!Environment.Is64BitProcess)
{
_twainImpl.Items.RemoveAt((int) TwainImpl.X64);
_twainImpl.Items = EnumDropDownWidget<TwainImpl>.DefaultItems.Where(x => x != TwainImpl.X64);
}
}
@ -117,11 +118,11 @@ public class AdvancedProfileForm : EtoDialogBase
_brightContAfterScan.Checked = scanProfile.BrightnessContrastAfterScan;
_deskew.Checked = scanProfile.AutoDeskew;
_offsetWidth.Checked = scanProfile.WiaOffsetWidth;
_wiaVersion.SelectedIndex = (int) scanProfile.WiaVersion;
_wiaVersion.SelectedItem = scanProfile.WiaVersion;
_stretchToPageSize.Checked = scanProfile.ForcePageSize;
_cropToPageSize.Checked = scanProfile.ForcePageSizeCrop;
_flipDuplexed.Checked = scanProfile.FlipDuplexedPages;
_twainImpl.SelectedIndex = (int) scanProfile.TwainImpl;
_twainImpl.SelectedItem = scanProfile.TwainImpl;
_twainProgress.Checked = scanProfile.TwainProgress;
_excludeBlank.Checked = scanProfile.ExcludeBlankPages;
_whiteThreshold.IntValue = scanProfile.BlankPageWhiteThreshold;
@ -144,17 +145,11 @@ public class AdvancedProfileForm : EtoDialogBase
ScanProfile.BrightnessContrastAfterScan = _brightContAfterScan.IsChecked();
ScanProfile.AutoDeskew = _deskew.IsChecked();
ScanProfile.WiaOffsetWidth = _offsetWidth.IsChecked();
if (_wiaVersion.SelectedIndex != -1)
{
ScanProfile.WiaVersion = (WiaApiVersion) _wiaVersion.SelectedIndex;
}
ScanProfile.WiaVersion = _wiaVersion.SelectedItem;
ScanProfile.ForcePageSize = _stretchToPageSize.IsChecked();
ScanProfile.ForcePageSizeCrop = _cropToPageSize.IsChecked();
ScanProfile.FlipDuplexedPages = _flipDuplexed.IsChecked();
if (_twainImpl.SelectedIndex != -1)
{
ScanProfile.TwainImpl = (TwainImpl) _twainImpl.SelectedIndex;
}
ScanProfile.TwainImpl = _twainImpl.SelectedItem;
ScanProfile.TwainProgress = _twainProgress.IsChecked();
ScanProfile.ExcludeBlankPages = _excludeBlank.IsChecked();
ScanProfile.BlankPageWhiteThreshold = _whiteThreshold.IntValue;

View File

@ -29,7 +29,7 @@ public class BatchScanForm : EtoDialogBase
private readonly Label _status = new() { Text = UiStrings.PressStartWhenReady };
private readonly Button _start = new() { Text = UiStrings.Start };
private readonly Button _cancel = new() { Text = UiStrings.Cancel };
private readonly DropDown _profile = C.DropDown();
private readonly DropDownWidget<ScanProfile> _profile = new();
private readonly RadioButton _singleScan;
private readonly RadioButton _multipleScansPrompt;
private readonly RadioButton _multipleScansDelay;
@ -69,6 +69,7 @@ public class BatchScanForm : EtoDialogBase
_filePerPage = new RadioButton(_filePerScan) { Text = UiStrings.OneFilePerPage };
_separateByPatchT = new RadioButton(_filePerScan) { Text = UiStrings.SeparateByPatchT };
_filePath = new(this, dialogHelper);
_profile.Format = x => x.DisplayName;
_start.Click += Start;
_cancel.Click += Cancel;
@ -122,7 +123,7 @@ public class BatchScanForm : EtoDialogBase
L.Column(
C.Label(UiStrings.ProfileLabel),
L.Row(
_profile.Scale(),
_profile.AsControl().Scale(),
C.Button(EditProfileCommand, ButtonImagePosition.Overlay),
C.Button(NewProfileCommand, ButtonImagePosition.Overlay)
),
@ -192,11 +193,11 @@ public class BatchScanForm : EtoDialogBase
{
bool ok = true;
_userTransact.Set(c => c.BatchSettings.ProfileDisplayName, _profile.SelectedKey);
if (_profile.SelectedIndex == -1)
_userTransact.Set(c => c.BatchSettings.ProfileDisplayName, _profile.SelectedItem?.DisplayName);
if (_profile.SelectedItem == null)
{
ok = false;
_profile.Focus();
_profile.AsControl().Focus();
}
_userTransact.Set(c => c.BatchSettings.ScanType, _multipleScansPrompt.Checked
@ -245,32 +246,17 @@ public class BatchScanForm : EtoDialogBase
private void UpdateProfiles()
{
_profile.Items.Clear();
_profile.Items.AddRange(_profileManager.Profiles.Select(profile => new ListItem
{
Text = profile.DisplayName,
Key = profile.DisplayName,
Tag = profile
}));
if (!string.IsNullOrEmpty(_transactionConfig.Get(c => c.BatchSettings.ProfileDisplayName)) &&
_profileManager.Profiles.Any(x =>
x.DisplayName == _transactionConfig.Get(c => c.BatchSettings.ProfileDisplayName)))
{
_profile.SelectedKey = _transactionConfig.Get(c => c.BatchSettings.ProfileDisplayName);
}
else if (_profileManager.DefaultProfile != null)
{
_profile.SelectedKey = _profileManager.DefaultProfile.DisplayName;
}
else
{
_profile.SelectedKey = null;
}
_profile.Items = _profileManager.Profiles;
var selectedName = _transactionConfig.Get(c => c.BatchSettings.ProfileDisplayName);
var selectedProfile = selectedName != null
? _profileManager.Profiles.FirstOrDefault(x => x.DisplayName == selectedName)
: null;
_profile.SelectedItem = selectedProfile ?? _profileManager.DefaultProfile;
}
private void EditProfile()
{
var originalProfile = (ScanProfile) ((ListItem) _profile.SelectedValue).Tag;
var originalProfile = _profile.SelectedItem;
if (originalProfile != null)
{
var fedit = FormFactory.Create<EditProfileForm>();
@ -336,7 +322,7 @@ public class BatchScanForm : EtoDialogBase
{
var controls = new Control[]
{
_profile, _singleScan, _multipleScansPrompt, _multipleScansDelay, _numberOfScans,
_profile.AsControl(), _singleScan, _multipleScansPrompt, _multipleScansDelay, _numberOfScans,
_timeBetweenScans, _load, _saveToSingleFile, _saveToMultipleFiles, _filePerScan, _filePerPage,
_separateByPatchT, _moreInfo
};

View File

@ -12,7 +12,7 @@ public class ImageSettingsForm : EtoDialogBase
private readonly CheckBox _skipSavePrompt = new() { Text = UiStrings.SkipSavePrompt };
private readonly SliderWithTextBox _jpegQuality = new(new SliderWithTextBox.IntConstraints(0, 100, 25));
private readonly CheckBox _singlePageTiff = new() { Text = UiStrings.SinglePageFiles };
private readonly DropDown _compression = C.EnumDropDown<TiffCompression>();
private readonly EnumDropDownWidget<TiffCompression> _compression = new();
private readonly CheckBox _rememberSettings = new() { Text = UiStrings.RememberTheseSettings };
private readonly Button _restoreDefaults = new() { Text = UiStrings.RestoreDefaults };
@ -72,7 +72,7 @@ public class ImageSettingsForm : EtoDialogBase
_skipSavePrompt.Checked = config.Get(c => c.ImageSettings.SkipSavePrompt);
_jpegQuality.IntValue = config.Get(c => c.ImageSettings.JpegQuality);
_singlePageTiff.Checked = config.Get(c => c.ImageSettings.SinglePageTiff);
_compression.SelectedIndex = (int) config.Get(c => c.ImageSettings.TiffCompression);
_compression.SelectedItem = config.Get(c => c.ImageSettings.TiffCompression);
_rememberSettings.Checked = config.Get(c => c.RememberImageSettings);
}
@ -88,7 +88,7 @@ public class ImageSettingsForm : EtoDialogBase
DefaultFileName = _defaultFilePath.Text,
SkipSavePrompt = _skipSavePrompt.IsChecked(),
JpegQuality = _jpegQuality.IntValue,
TiffCompression = (TiffCompression) _compression.SelectedIndex,
TiffCompression = _compression.SelectedItem,
SinglePageTiff = _singlePageTiff.IsChecked()
};

View File

@ -2,6 +2,7 @@ using System.Globalization;
using Eto.Drawing;
using Eto.Forms;
using NAPS2.EtoForms.Layout;
using NAPS2.EtoForms.Widgets;
using NAPS2.Lang;
using NAPS2.Ocr;
@ -13,7 +14,8 @@ public class OcrSetupForm : EtoDialogBase
private readonly CheckBox _enableOcr = C.CheckBox(UiStrings.MakePdfsSearchable);
private readonly DropDown _ocrLang = C.DropDown();
private readonly DropDown _ocrMode = C.EnumDropDown(LocalizedOcrMode.Fast, LocalizedOcrMode.Best);
private readonly EnumDropDownWidget<LocalizedOcrMode> _ocrMode = new()
{ Items = [LocalizedOcrMode.Fast, LocalizedOcrMode.Best] };
private readonly CheckBox _ocrPreProcessing = C.CheckBox(UiStrings.OcrPreProcessing);
private readonly CheckBox _ocrAfterScanning = C.CheckBox(
TranslationMigrator.PickTranslated(
@ -45,8 +47,7 @@ public class OcrSetupForm : EtoDialogBase
_enableOcr.Checked = Config.Get(c => c.EnableOcr);
_ocrLang.SelectedIndexChanged += OcrLang_SelectedIndexChanged;
_ocrMode.SelectedIndex = (int) configOcrMode;
if (_ocrMode.SelectedIndex == -1) _ocrMode.SelectedIndex = 0;
_ocrMode.SelectedItem = configOcrMode;
_ocrPreProcessing.Checked = Config.Get(c => c.OcrPreProcessing);
_ocrAfterScanning.Checked = Config.Get(c => c.OcrAfterScanning);
@ -69,7 +70,7 @@ public class OcrSetupForm : EtoDialogBase
).Aligned(),
L.Row(
C.Label(UiStrings.OcrModeLabel).AlignCenter().Padding(right: 40),
_ocrMode.Scale()
_ocrMode.AsControl().Scale()
).Aligned(),
_ocrPreProcessing,
_ocrAfterScanning,
@ -179,7 +180,7 @@ public class OcrSetupForm : EtoDialogBase
{
Config.User.Set(c => c.LastOcrMultiLangCode, _multiLangCode);
}
transact.Set(c => c.OcrMode, (LocalizedOcrMode) _ocrMode.SelectedIndex);
transact.Set(c => c.OcrMode, _ocrMode.SelectedItem);
transact.Set(c => c.OcrPreProcessing, _ocrPreProcessing.IsChecked());
transact.Set(c => c.OcrAfterScanning, _ocrAfterScanning.IsChecked());
transact.Commit();

View File

@ -2,6 +2,7 @@ using System.Collections.Immutable;
using System.Globalization;
using Eto.Forms;
using NAPS2.EtoForms.Layout;
using NAPS2.EtoForms.Widgets;
using NAPS2.Scan;
namespace NAPS2.EtoForms.Ui;
@ -11,7 +12,7 @@ public class PageSizeForm : EtoDialogBase
private readonly ComboBox _name = new();
private readonly NumericMaskedTextBox<decimal> _width = new();
private readonly NumericMaskedTextBox<decimal> _height = new();
private readonly DropDown _unit = C.EnumDropDown<LocalizedPageSizeUnit>();
private readonly EnumDropDownWidget<LocalizedPageSizeUnit> _unit = new();
private PageDimensions? _initialDimens;
@ -76,7 +77,7 @@ public class PageSizeForm : EtoDialogBase
_width.Scale().AlignCenter(),
C.Label("X").AlignCenter(),
_height.Scale().AlignCenter(),
_unit.Scale().AlignCenter()
_unit.AsControl().Scale().AlignCenter()
),
L.Row(
C.Filler(),
@ -100,7 +101,7 @@ public class PageSizeForm : EtoDialogBase
{
_width.Text = dimens.Width.ToString(CultureInfo.CurrentCulture);
_height.Text = dimens.Height.ToString(CultureInfo.CurrentCulture);
_unit.SelectedIndex = (int) dimens.Unit;
_unit.SelectedItem = dimens.Unit;
}
private void Name_SelectionChange(object? sender, EventArgs e)
@ -137,7 +138,7 @@ public class PageSizeForm : EtoDialogBase
{
Width = width,
Height = height,
Unit = (LocalizedPageSizeUnit) _unit.SelectedIndex
Unit = _unit.SelectedItem
};
if (!string.IsNullOrWhiteSpace(_name.Text))
{

View File

@ -34,19 +34,20 @@ public class PdfSettingsForm : EtoDialogBase
new CheckBox { Text = UiStrings.AllowFormFilling }
];
private readonly DropDown _compat = C.EnumDropDown<PdfCompat>(compat => compat switch
{
PdfCompat.Default => UiStrings.Default,
PdfCompat.PdfA1B => "PDF/A-1b",
PdfCompat.PdfA2B => "PDF/A-2b",
PdfCompat.PdfA3B => "PDF/A-3b",
PdfCompat.PdfA3U => "PDF/A-3u",
_ => throw new ArgumentException()
});
private readonly EnumDropDownWidget<PdfCompat> _compat = new();
public PdfSettingsForm(Naps2Config config, DialogHelper dialogHelper) : base(config)
{
_defaultFilePath = new(this, dialogHelper) { PdfOnly = true };
_compat.Format = compat => compat switch
{
PdfCompat.Default => UiStrings.Default,
PdfCompat.PdfA1B => "PDF/A-1b",
PdfCompat.PdfA2B => "PDF/A-2b",
PdfCompat.PdfA3B => "PDF/A-3b",
PdfCompat.PdfA3U => "PDF/A-3u",
_ => throw new ArgumentException()
};
UpdateValues(Config);
UpdateEnabled();
@ -130,7 +131,7 @@ public class PdfSettingsForm : EtoDialogBase
config.Get(c => c.PdfSettings.Encryption.AllowContentCopyingForAccessibility);
_permissions[6].Checked = config.Get(c => c.PdfSettings.Encryption.AllowAnnotations);
_permissions[7].Checked = config.Get(c => c.PdfSettings.Encryption.AllowFormFilling);
_compat.SelectedIndex = (int) config.Get(c => c.PdfSettings.Compat);
_compat.SelectedItem = config.Get(c => c.PdfSettings.Compat);
_rememberSettings.Checked = config.Get(c => c.RememberPdfSettings);
}
@ -169,7 +170,7 @@ public class PdfSettingsForm : EtoDialogBase
AllowAnnotations = _permissions[6].IsChecked(),
AllowFormFilling = _permissions[7].IsChecked()
},
Compat = (PdfCompat) _compat.SelectedIndex
Compat = _compat.SelectedItem
};
var runTransact = Config.Run.BeginTransaction();

View File

@ -3,6 +3,7 @@ using Eto.Drawing;
using Eto.Forms;
using NAPS2.EtoForms.Desktop;
using NAPS2.EtoForms.Layout;
using NAPS2.EtoForms.Widgets;
namespace NAPS2.EtoForms.Ui;
@ -12,8 +13,8 @@ internal class SettingsForm : EtoDialogBase
private readonly CheckBox _scanChangesDefaultProfile = C.CheckBox(UiStrings.ScanChangesDefaultProfile);
private readonly CheckBox _showProfilesToolbar = C.CheckBox(UiStrings.ShowProfilesToolbar);
private readonly CheckBox _showPageNumbers = C.CheckBox(UiStrings.ShowPageNumbers);
private readonly DropDown _scanButtonDefaultAction = C.EnumDropDown<ScanButtonDefaultAction>();
private readonly DropDown _saveButtonDefaultAction = C.EnumDropDown<SaveButtonDefaultAction>();
private readonly EnumDropDownWidget<ScanButtonDefaultAction> _scanButtonDefaultAction = new();
private readonly EnumDropDownWidget<SaveButtonDefaultAction> _saveButtonDefaultAction = new();
private readonly CheckBox _clearAfterSaving = C.CheckBox(UiStrings.ClearAfterSaving);
private readonly CheckBox _keepSession = C.CheckBox(UiStrings.KeepSession);
private readonly CheckBox _singleInstance = C.CheckBox(UiStrings.SingleInstanceDesc);
@ -113,9 +114,9 @@ internal class SettingsForm : EtoDialogBase
UpdateCheckbox(_scanChangesDefaultProfile, c => c.ScanChangesDefaultProfile);
UpdateCheckbox(_showProfilesToolbar, c => c.ShowProfilesToolbar);
UpdateCheckbox(_showPageNumbers, c => c.ShowPageNumbers);
_scanButtonDefaultAction.SelectedIndex = (int) config.Get(c => c.ScanButtonDefaultAction);
_scanButtonDefaultAction.SelectedItem = config.Get(c => c.ScanButtonDefaultAction);
_scanButtonDefaultAction.Enabled = !config.AppLocked.Has(c => c.ScanButtonDefaultAction);
_saveButtonDefaultAction.SelectedIndex = (int) config.Get(c => c.SaveButtonDefaultAction);
_saveButtonDefaultAction.SelectedItem = config.Get(c => c.SaveButtonDefaultAction);
_saveButtonDefaultAction.Enabled = !config.AppLocked.Has(c => c.SaveButtonDefaultAction);
UpdateCheckbox(_clearAfterSaving, c => c.DeleteAfterSaving);
UpdateCheckbox(_keepSession, c => c.KeepSession);
@ -136,8 +137,8 @@ internal class SettingsForm : EtoDialogBase
SetIfChanged(c => c.ScanChangesDefaultProfile, _scanChangesDefaultProfile.IsChecked());
SetIfChanged(c => c.ShowProfilesToolbar, _showProfilesToolbar.IsChecked());
SetIfChanged(c => c.ShowPageNumbers, _showPageNumbers.IsChecked());
SetIfChanged(c => c.ScanButtonDefaultAction, (ScanButtonDefaultAction) _scanButtonDefaultAction.SelectedIndex);
SetIfChanged(c => c.SaveButtonDefaultAction, (SaveButtonDefaultAction) _saveButtonDefaultAction.SelectedIndex);
SetIfChanged(c => c.ScanButtonDefaultAction, _scanButtonDefaultAction.SelectedItem);
SetIfChanged(c => c.SaveButtonDefaultAction, _saveButtonDefaultAction.SelectedItem);
SetIfChanged(c => c.DeleteAfterSaving, _clearAfterSaving.IsChecked());
SetIfChanged(c => c.KeepSession, _keepSession.IsChecked());
SetIfChanged(c => c.SingleInstance, _singleInstance.IsChecked());

View File

@ -16,12 +16,17 @@ public class DropDownWidget<T> where T : notnull
{
EtoPlatform.Current.ConfigureDropDown(_dropDown);
_dropDown.SelectedIndexChanged += DropDown_SelectedIndexChanged;
_dropDown.PreLoad += PreLoad;
if (typeof(IComparable<T>).IsAssignableFrom(typeof(T)))
{
GetClosestItem = GetClosestItemByComparing;
}
}
protected virtual void PreLoad(object sender, EventArgs e)
{
}
public Func<T, string> Format { get; set; } = x => x?.ToString() ?? "";
public Func<T[], T, T>? GetClosestItem { get; set; }
@ -129,5 +134,5 @@ public class DropDownWidget<T> where T : notnull
public static implicit operator LayoutElement(DropDownWidget<T> control) => control.AsControl();
public LayoutElement AsControl() => _dropDown;
public DropDown AsControl() => _dropDown;
}

View File

@ -9,6 +9,13 @@ public class EnumDropDownWidget<T> : DropDownWidget<T> where T : struct, Enum
public EnumDropDownWidget()
{
Format = x => x.Description();
Items = DefaultItems;
}
protected override void PreLoad(object sender, EventArgs e)
{
if (!Items.Any())
{
Items = DefaultItems;
}
}
}