From 7ca1cf7ef5bd7a605a2e6fd8f7e19ef73a94fe3f Mon Sep 17 00:00:00 2001 From: Ben Olden-Cooligan Date: Sat, 10 Dec 2022 11:36:54 -0800 Subject: [PATCH] Use a BuildLayout method pattern This helps organize logic a bit and better handles cases where forms have parameters. --- NAPS2.Lib/EtoForms/EtoDialogBase.cs | 8 +++ NAPS2.Lib/EtoForms/EtoFormBase.cs | 8 +++ NAPS2.Lib/EtoForms/Ui/AboutForm.cs | 14 ++-- NAPS2.Lib/EtoForms/Ui/AuthorizeForm.cs | 5 ++ NAPS2.Lib/EtoForms/Ui/DesktopForm.cs | 29 ++++---- NAPS2.Lib/EtoForms/Ui/DownloadProgressForm.cs | 12 ++-- NAPS2.Lib/EtoForms/Ui/EditProfileForm.cs | 19 ++++-- NAPS2.Lib/EtoForms/Ui/EmailProviderForm.cs | 9 ++- NAPS2.Lib/EtoForms/Ui/EmailSettingsForm.cs | 9 ++- NAPS2.Lib/EtoForms/Ui/ErrorForm.cs | 12 ++-- NAPS2.Lib/EtoForms/Ui/ImageFormBase.cs | 46 +++++++------ NAPS2.Lib/EtoForms/Ui/ImageSettingsForm.cs | 9 ++- NAPS2.Lib/EtoForms/Ui/OcrDownloadForm.cs | 12 ++-- NAPS2.Lib/EtoForms/Ui/OcrSetupForm.cs | 14 ++-- NAPS2.Lib/EtoForms/Ui/PdfSettingsForm.cs | 9 ++- NAPS2.Lib/EtoForms/Ui/PlaceholdersForm.cs | 23 ++++--- NAPS2.Lib/EtoForms/Ui/PreviewForm.cs | 20 +++--- NAPS2.Lib/EtoForms/Ui/ProfilesForm.cs | 11 ++-- NAPS2.Lib/EtoForms/Ui/ProgressForm.cs | 7 +- NAPS2.Lib/EtoForms/Ui/RecoverForm.cs | 17 +++-- NAPS2.Lib/EtoForms/Ui/SelectDeviceForm.cs | 66 ++++++------------- 21 files changed, 204 insertions(+), 155 deletions(-) diff --git a/NAPS2.Lib/EtoForms/EtoDialogBase.cs b/NAPS2.Lib/EtoForms/EtoDialogBase.cs index 7805bbebb..cd39d4135 100644 --- a/NAPS2.Lib/EtoForms/EtoDialogBase.cs +++ b/NAPS2.Lib/EtoForms/EtoDialogBase.cs @@ -19,6 +19,14 @@ public abstract class EtoDialogBase : Dialog, IFormBase LayoutController.Invalidated += (_, _) => FormStateController.UpdateLayoutSize(LayoutController); } + protected abstract void BuildLayout(); + + protected override void OnPreLoad(EventArgs e) + { + BuildLayout(); + base.OnPreLoad(e); + } + // TODO: PR for Eto to integrate this public new Icon Icon { diff --git a/NAPS2.Lib/EtoForms/EtoFormBase.cs b/NAPS2.Lib/EtoForms/EtoFormBase.cs index 915ab912c..c9d8423b7 100644 --- a/NAPS2.Lib/EtoForms/EtoFormBase.cs +++ b/NAPS2.Lib/EtoForms/EtoFormBase.cs @@ -17,6 +17,14 @@ public abstract class EtoFormBase : Form, IFormBase LayoutController.Invalidated += (_, _) => FormStateController.UpdateLayoutSize(LayoutController); } + protected abstract void BuildLayout(); + + protected override void OnPreLoad(EventArgs e) + { + BuildLayout(); + base.OnPreLoad(e); + } + public FormStateController FormStateController { get; } public LayoutController LayoutController { get; } = new(); diff --git a/NAPS2.Lib/EtoForms/Ui/AboutForm.cs b/NAPS2.Lib/EtoForms/Ui/AboutForm.cs index 1d449d2f7..6065e9b87 100644 --- a/NAPS2.Lib/EtoForms/Ui/AboutForm.cs +++ b/NAPS2.Lib/EtoForms/Ui/AboutForm.cs @@ -25,11 +25,6 @@ public class AboutForm : EtoDialogBase { _updateChecker = updateChecker; - Title = UiStrings.AboutFormTitle; - Icon = new Icon(1f, Icons.information_small.ToEtoImage()); - FormStateController.Resizable = false; - FormStateController.RestoreFormState = false; - _donateButton = EtoPlatform.Current.AccessibleImageButton( Icons.btn_donate_LG.ToEtoImage(), UiStrings.Donate, @@ -39,11 +34,16 @@ public class AboutForm : EtoDialogBase _updatePanel = new Panel(); UpdateControls(); - BuildLayout(); } - private void BuildLayout() + protected override void BuildLayout() { + Title = UiStrings.AboutFormTitle; + Icon = new Icon(1f, Icons.information_small.ToEtoImage()); + + FormStateController.Resizable = false; + FormStateController.RestoreFormState = false; + LayoutController.DefaultSpacing = 2; LayoutController.Content = L.Row( L.Column(new ImageView { Image = Icons.scanner_128.ToEtoImage() }).Padding(right: 4), diff --git a/NAPS2.Lib/EtoForms/Ui/AuthorizeForm.cs b/NAPS2.Lib/EtoForms/Ui/AuthorizeForm.cs index 364e2f019..359b8dcf4 100644 --- a/NAPS2.Lib/EtoForms/Ui/AuthorizeForm.cs +++ b/NAPS2.Lib/EtoForms/Ui/AuthorizeForm.cs @@ -13,12 +13,17 @@ public class AuthorizeForm : EtoDialogBase public AuthorizeForm(Naps2Config config, ErrorOutput errorOutput) : base(config) { _errorOutput = errorOutput; + } + + protected override void BuildLayout() + { Title = UiStrings.AuthorizeFormTitle; Icon = new Icon(1f, Icons.key_small.ToEtoImage()); FormStateController.FixedHeightLayout = true; FormStateController.RestoreFormState = false; FormStateController.Resizable = false; + LayoutController.Content = L.Row( C.Label(UiStrings.WaitingForAuthorization).Padding(right: 30), C.CancelButton(this) diff --git a/NAPS2.Lib/EtoForms/Ui/DesktopForm.cs b/NAPS2.Lib/EtoForms/Ui/DesktopForm.cs index 5f60c1017..61e4a7440 100644 --- a/NAPS2.Lib/EtoForms/Ui/DesktopForm.cs +++ b/NAPS2.Lib/EtoForms/Ui/DesktopForm.cs @@ -66,7 +66,6 @@ public abstract class DesktopForm : EtoFormBase // PostInitializeComponent(); // - Icon = Icons.favicon.ToEtoIcon(); CreateToolbarsAndMenus(); UpdateScanButton(); InitLanguageDropdown(); @@ -89,17 +88,6 @@ public abstract class DesktopForm : EtoFormBase _listView.Control.KeyDown += OnKeyDown; _listView.Control.MouseWheel += ListViewMouseWheel; - LayoutController.RootPadding = 0; - FormStateController.AutoLayoutSize = false; - FormStateController.DefaultClientSize = new Size(1210, 600); - LayoutController.Content = L.Overlay( - GetMainContent(), - L.Column( - C.Filler(), - L.Row(GetZoomButtons(), C.Filler()) - ).Padding(10) - ); - // // Shown += FDesktop_Shown; // Closing += FDesktop_Closing; @@ -113,6 +101,23 @@ public abstract class DesktopForm : EtoFormBase _profileManager.ProfilesUpdated += ProfileManager_ProfilesUpdated; } + protected override void BuildLayout() + { + Icon = Icons.favicon.ToEtoIcon(); + + FormStateController.AutoLayoutSize = false; + FormStateController.DefaultClientSize = new Size(1210, 600); + + LayoutController.RootPadding = 0; + LayoutController.Content = L.Overlay( + GetMainContent(), + L.Column( + C.Filler(), + L.Row(GetZoomButtons(), C.Filler()) + ).Padding(10) + ); + } + private void OpeningContextMenu(object? sender, EventArgs e) { _contextMenu.Items.Clear(); diff --git a/NAPS2.Lib/EtoForms/Ui/DownloadProgressForm.cs b/NAPS2.Lib/EtoForms/Ui/DownloadProgressForm.cs index f91b75562..e9ed3f868 100644 --- a/NAPS2.Lib/EtoForms/Ui/DownloadProgressForm.cs +++ b/NAPS2.Lib/EtoForms/Ui/DownloadProgressForm.cs @@ -28,15 +28,19 @@ public class DownloadProgressForm : EtoDialogBase public DownloadProgressForm(Naps2Config config) : base(config) { - FormStateController.RestoreFormState = false; - Title = UiStrings.DownloadProgressFormTitle; - Icon = new Icon(1f, Icons.text_small.ToEtoImage()); - // TODO: Is this needed for net462? ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; _client.DownloadFileCompleted += client_DownloadFileCompleted; _client.DownloadProgressChanged += client_DownloadProgressChanged; + } + + protected override void BuildLayout() + { + Title = UiStrings.DownloadProgressFormTitle; + Icon = new Icon(1f, Icons.text_small.ToEtoImage()); + + FormStateController.RestoreFormState = false; LayoutController.Content = L.Column( _totalStatus, diff --git a/NAPS2.Lib/EtoForms/Ui/EditProfileForm.cs b/NAPS2.Lib/EtoForms/Ui/EditProfileForm.cs index 3b357136b..19a91959a 100644 --- a/NAPS2.Lib/EtoForms/Ui/EditProfileForm.cs +++ b/NAPS2.Lib/EtoForms/Ui/EditProfileForm.cs @@ -48,8 +48,6 @@ public class EditProfileForm : EtoDialogBase _scanPerformer = scanPerformer; _errorOutput = errorOutput; _profileNameTracker = profileNameTracker; - Title = UiStrings.EditProfileFormTitle; - Icon = new Icon(1f, Icons.blueprints_small.ToEtoImage()); _wiaDriver = new RadioButton { Text = UiStrings.WiaDriver }; _twainDriver = new RadioButton(_wiaDriver) { Text = UiStrings.TwainDriver }; @@ -67,6 +65,15 @@ public class EditProfileForm : EtoDialogBase _ok.Click += Ok_Click; _cancel.Click += Cancel_Click; + _chooseDevice.Click += ChooseDevice; + _enableAutoSave.CheckedChanged += EnableAutoSave_CheckedChanged; + _autoSaveSettings.Click += AutoSaveSettings_LinkClicked; + _advanced.Click += Advanced_Click; + _deviceName.KeyDown += DeviceName_KeyDown; + } + + protected override void BuildLayout() + { // TODO: Don't show if only one driver is available var driverElements = new List(); if (PlatformCompat.System.IsWiaDriverSupported) @@ -86,14 +93,12 @@ public class EditProfileForm : EtoDialogBase driverElements.Add(_saneDriver.XScale()); } - _chooseDevice.Click += ChooseDevice; - _enableAutoSave.CheckedChanged += EnableAutoSave_CheckedChanged; - _autoSaveSettings.Click += AutoSaveSettings_LinkClicked; - _advanced.Click += Advanced_Click; - _deviceName.KeyDown += DeviceName_KeyDown; + Title = UiStrings.EditProfileFormTitle; + Icon = new Icon(1f, Icons.blueprints_small.ToEtoImage()); FormStateController.DefaultExtraLayoutSize = new Size(60, 0); FormStateController.FixedHeightLayout = true; + LayoutController.Content = L.Column( L.Row( L.Column( diff --git a/NAPS2.Lib/EtoForms/Ui/EmailProviderForm.cs b/NAPS2.Lib/EtoForms/Ui/EmailProviderForm.cs index 6f11c2349..4b7c112c9 100644 --- a/NAPS2.Lib/EtoForms/Ui/EmailProviderForm.cs +++ b/NAPS2.Lib/EtoForms/Ui/EmailProviderForm.cs @@ -24,8 +24,6 @@ public class EmailProviderForm : EtoDialogBase _systemEmailClients = systemEmailClients; _gmailOauthProvider = gmailOauthProvider; _outlookWebOauthProvider = outlookWebOauthProvider; - Title = UiStrings.EmailProviderFormTitle; - Icon = new Icon(1f, Icons.email_small.ToEtoImage()); _providerWidgets = new List(); #if NET6_0_OR_GREATER @@ -93,8 +91,15 @@ public class EmailProviderForm : EtoDialogBase _providerWidgets.Remove(defaultWidget); _providerWidgets.Insert(0, defaultWidget); } + } + + protected override void BuildLayout() + { + Title = UiStrings.EmailProviderFormTitle; + Icon = new Icon(1f, Icons.email_small.ToEtoImage()); FormStateController.FixedHeightLayout = true; + LayoutController.DefaultSpacing = 0; LayoutController.Content = L.Column( _providerWidgets.Select(x => C.Button(new ActionCommand(x.ClickAction) diff --git a/NAPS2.Lib/EtoForms/Ui/EmailSettingsForm.cs b/NAPS2.Lib/EtoForms/Ui/EmailSettingsForm.cs index 16f374bef..2ddc213a9 100644 --- a/NAPS2.Lib/EtoForms/Ui/EmailSettingsForm.cs +++ b/NAPS2.Lib/EtoForms/Ui/EmailSettingsForm.cs @@ -20,17 +20,22 @@ public class EmailSettingsForm : EtoDialogBase public EmailSettingsForm(Naps2Config config, SystemEmailClients systemEmailClients) : base(config) { _systemEmailClients = systemEmailClients; - Title = UiStrings.EmailSettingsFormTitle; - Icon = new Icon(1f, Icons.email_small.ToEtoImage()); UpdateValues(Config); UpdateProvider(Config); _restoreDefaults.Click += RestoreDefaults_Click; _placeholders.Click += Placeholders_Click; + } + + protected override void BuildLayout() + { + Title = UiStrings.EmailSettingsFormTitle; + Icon = new Icon(1f, Icons.email_small.ToEtoImage()); FormStateController.DefaultExtraLayoutSize = new Size(60, 0); FormStateController.FixedHeightLayout = true; + LayoutController.Content = L.Column( L.GroupBox( UiStrings.Provider, diff --git a/NAPS2.Lib/EtoForms/Ui/ErrorForm.cs b/NAPS2.Lib/EtoForms/Ui/ErrorForm.cs index 0caea0150..83cef5529 100644 --- a/NAPS2.Lib/EtoForms/Ui/ErrorForm.cs +++ b/NAPS2.Lib/EtoForms/Ui/ErrorForm.cs @@ -5,22 +5,26 @@ namespace NAPS2.EtoForms.Ui; public class ErrorForm : EtoDialogBase { + private readonly ImageView _image = new() { Image = Icons.exclamation.ToEtoImage() }; private readonly Label _message = new(); private readonly TextArea _details = new() { ReadOnly = true }; private readonly LayoutVisibility _detailsVisibility = new(false); public ErrorForm(Naps2Config config) : base(config) + { + } + + protected override void BuildLayout() { Title = UiStrings.ErrorFormTitle; + FormStateController.RestoreFormState = false; - - var image = new ImageView { Image = Icons.exclamation.ToEtoImage() }; - FormStateController.FixedHeightLayout = true; + LayoutController.Content = L.Column( L.Row( - image.AlignCenter().Padding(right: 5), + _image.AlignCenter().Padding(right: 5), _message.Wrap(350).NaturalWidth(350).AlignCenter().XScale() ), L.Row( diff --git a/NAPS2.Lib/EtoForms/Ui/ImageFormBase.cs b/NAPS2.Lib/EtoForms/Ui/ImageFormBase.cs index 5f7151c96..8cdbf289b 100644 --- a/NAPS2.Lib/EtoForms/Ui/ImageFormBase.cs +++ b/NAPS2.Lib/EtoForms/Ui/ImageFormBase.cs @@ -22,8 +22,29 @@ public abstract class ImageFormBase : EtoDialogBase _thumbnailController = thumbnailController; _revert.Click += Revert; _renderThrottle = new RefreshThrottle(RenderImage); - FormStateController.DefaultExtraLayoutSize = new Size(400, 400); Overlay.Paint += PaintOverlay; + FormStateController.DefaultExtraLayoutSize = new Size(400, 400); + } + + protected override void BuildLayout() + { + foreach (var slider in Sliders) + { + slider.ValueChanged += UpdatePreviewBox; + } + + LayoutController.Content = L.Column( + Overlay.YScale(), + CreateControls(), + SelectedImages is { Count: > 1 } ? _applyToSelected : C.None(), + L.Row( + _revert, + C.Filler(), + L.OkCancel( + C.OkButton(this, beforeClose: Apply), + C.CancelButton(this)) + ) + ); } protected int ImageHeight { get; set; } @@ -133,29 +154,6 @@ public abstract class ImageFormBase : EtoDialogBase { } - protected override void OnPreLoad(EventArgs e) - { - foreach (var slider in Sliders) - { - slider.ValueChanged += UpdatePreviewBox; - } - - LayoutController.Content = L.Column( - Overlay.YScale(), - CreateControls(), - SelectedImages is { Count: > 1 } ? _applyToSelected : C.None(), - L.Row( - _revert, - C.Filler(), - L.OkCancel( - C.OkButton(this, beforeClose: Apply), - C.CancelButton(this)) - ) - ); - - base.OnPreLoad(e); - } - protected override void OnLoad(EventArgs e) { base.OnLoad(e); diff --git a/NAPS2.Lib/EtoForms/Ui/ImageSettingsForm.cs b/NAPS2.Lib/EtoForms/Ui/ImageSettingsForm.cs index 0bb7e62ac..2505d0d44 100644 --- a/NAPS2.Lib/EtoForms/Ui/ImageSettingsForm.cs +++ b/NAPS2.Lib/EtoForms/Ui/ImageSettingsForm.cs @@ -22,8 +22,6 @@ public class ImageSettingsForm : EtoDialogBase public ImageSettingsForm(Naps2Config config, DialogHelper dialogHelper) : base(config) { _dialogHelper = dialogHelper; - Title = UiStrings.ImageSettingsFormTitle; - Icon = new Icon(1f, Icons.picture_small.ToEtoImage()); UpdateValues(Config); UpdateEnabled(); @@ -32,9 +30,16 @@ public class ImageSettingsForm : EtoDialogBase _defaultFilePath.TextChanged += DefaultFilePath_TextChanged; _placeholders.Click += Placeholders_Click; _chooseFolder.Click += ChooseFolder_Click; + } + + protected override void BuildLayout() + { + Title = UiStrings.ImageSettingsFormTitle; + Icon = new Icon(1f, Icons.picture_small.ToEtoImage()); FormStateController.DefaultExtraLayoutSize = new Size(60, 0); FormStateController.FixedHeightLayout = true; + LayoutController.Content = L.Column( C.Label(UiStrings.DefaultFilePathLabel), L.Row( diff --git a/NAPS2.Lib/EtoForms/Ui/OcrDownloadForm.cs b/NAPS2.Lib/EtoForms/Ui/OcrDownloadForm.cs index 6989daafb..d5592d7ff 100644 --- a/NAPS2.Lib/EtoForms/Ui/OcrDownloadForm.cs +++ b/NAPS2.Lib/EtoForms/Ui/OcrDownloadForm.cs @@ -18,10 +18,6 @@ public class OcrDownloadForm : EtoDialogBase { _tesseractLanguageManager = tesseractLanguageManager; - Title = UiStrings.OcrDownloadFormTitle; - Icon = new Icon(1f, Icons.text_small.ToEtoImage()); - FormStateController.RestoreFormState = false; - var initialSelection = new HashSet(); // TODO: We used to select old installed languages here, maybe we could do it again if we get new lang data if (!_tesseractLanguageManager.InstalledLanguages.Any()) @@ -40,8 +36,16 @@ public class OcrDownloadForm : EtoDialogBase _languageList.SelectionChanged += (_, _) => UpdateView(); UpdateView(); + } + protected override void BuildLayout() + { + Title = UiStrings.OcrDownloadFormTitle; + Icon = new Icon(1f, Icons.text_small.ToEtoImage()); + + FormStateController.RestoreFormState = false; FormStateController.DefaultExtraLayoutSize = new Size(300, 300); + LayoutController.Content = L.Column( C.Label(UiStrings.OcrDownloadSummaryText), C.Spacer(), diff --git a/NAPS2.Lib/EtoForms/Ui/OcrSetupForm.cs b/NAPS2.Lib/EtoForms/Ui/OcrSetupForm.cs index 928afbed4..730a9b125 100644 --- a/NAPS2.Lib/EtoForms/Ui/OcrSetupForm.cs +++ b/NAPS2.Lib/EtoForms/Ui/OcrSetupForm.cs @@ -19,10 +19,6 @@ public class OcrSetupForm : EtoDialogBase { _tesseractLanguageManager = tesseractLanguageManager; - Title = UiStrings.OcrSetupFormTitle; - Icon = new Icon(1f, Icons.text_small.ToEtoImage()); - FormStateController.Resizable = false; - _enableOcr.CheckedChanged += EnableOcr_CheckedChanged; _moreLanguages.Click += MoreLanguages_Click; @@ -36,6 +32,14 @@ public class OcrSetupForm : EtoDialogBase _ocrAfterScanning.Checked = Config.Get(c => c.OcrAfterScanning); UpdateView(); + } + + protected override void BuildLayout() + { + Title = UiStrings.OcrSetupFormTitle; + Icon = new Icon(1f, Icons.text_small.ToEtoImage()); + + FormStateController.Resizable = false; LayoutController.Content = L.Column( _enableOcr, @@ -54,7 +58,7 @@ public class OcrSetupForm : EtoDialogBase C.Filler(), L.OkCancel( C.OkButton(this, Save), - // TODO: Should we allow Esc to close the window if there are unsaved changes? + // TODO: Should we allow Esc to close the window if there are unsaved changes? C.CancelButton(this)) ) ); diff --git a/NAPS2.Lib/EtoForms/Ui/PdfSettingsForm.cs b/NAPS2.Lib/EtoForms/Ui/PdfSettingsForm.cs index 2b64717db..8ab95a235 100644 --- a/NAPS2.Lib/EtoForms/Ui/PdfSettingsForm.cs +++ b/NAPS2.Lib/EtoForms/Ui/PdfSettingsForm.cs @@ -49,8 +49,6 @@ public class PdfSettingsForm : EtoDialogBase public PdfSettingsForm(Naps2Config config, DialogHelper dialogHelper) : base(config) { _dialogHelper = dialogHelper; - Title = UiStrings.PdfSettingsFormTitle; - Icon = new Icon(1f, Icons.file_extension_pdf_small.ToEtoImage()); UpdateValues(Config); UpdateEnabled(); @@ -60,9 +58,16 @@ public class PdfSettingsForm : EtoDialogBase _encryptPdf.CheckedChanged += EncryptPdf_CheckedChanged; _placeholders.Click += Placeholders_Click; _chooseFolder.Click += ChooseFolder_Click; + } + + protected override void BuildLayout() + { + Title = UiStrings.PdfSettingsFormTitle; + Icon = new Icon(1f, Icons.file_extension_pdf_small.ToEtoImage()); FormStateController.DefaultExtraLayoutSize = new Size(60, 0); FormStateController.FixedHeightLayout = true; + LayoutController.Content = L.Column( C.Label(UiStrings.DefaultFilePathLabel), // TODO: Maybe make a widget for this kind of file picker diff --git a/NAPS2.Lib/EtoForms/Ui/PlaceholdersForm.cs b/NAPS2.Lib/EtoForms/Ui/PlaceholdersForm.cs index 6b5d05289..b449d35b1 100644 --- a/NAPS2.Lib/EtoForms/Ui/PlaceholdersForm.cs +++ b/NAPS2.Lib/EtoForms/Ui/PlaceholdersForm.cs @@ -7,15 +7,7 @@ namespace NAPS2.EtoForms.Ui; public class PlaceholdersForm : EtoDialogBase { - private readonly TextBox _fileName = new(); - private readonly Label _preview = new() { Text = " " }; - - public PlaceholdersForm(Naps2Config config) : base(config) - { - Title = UiStrings.PlaceholdersFormTitle; - - var placeholders = new (string val, string text)[] - { + private static readonly (string val, string text)[] PlaceholderButtons = { (Placeholders.YEAR_4_DIGITS, UiStrings.Year4Digit), (Placeholders.YEAR_2_DIGITS, UiStrings.Year2Digit), (Placeholders.MONTH_2_DIGITS, UiStrings.Month2Digit), @@ -29,12 +21,23 @@ public class PlaceholdersForm : EtoDialogBase (Placeholders.NUMBER_1_DIGIT, UiStrings.AutoIncrementing1Digit) }; + private readonly TextBox _fileName = new(); + private readonly Label _preview = new() { Text = " " }; + + public PlaceholdersForm(Naps2Config config) : base(config) + { // TODO: Ellipsis aren't working, presumably because Eto uses custom label rendering on WinForms EtoPlatform.Current.ConfigureEllipsis(_preview); _fileName.TextChanged += FileName_TextChanged; + } + + protected override void BuildLayout() + { + Title = UiStrings.PlaceholdersFormTitle; FormStateController.DefaultExtraLayoutSize = new Size(60, 0); FormStateController.FixedHeightLayout = true; + LayoutController.Content = L.Column( C.Label(UiStrings.FileNameLabel), _fileName, @@ -49,7 +52,7 @@ public class PlaceholdersForm : EtoDialogBase L.GroupBox( UiStrings.Placeholders, L.Column( - placeholders.Select(x => L.Row( + PlaceholderButtons.Select(x => L.Row( C.Button(x.val, () => Add(x.val)), C.Label(x.text) )).Expand(), diff --git a/NAPS2.Lib/EtoForms/Ui/PreviewForm.cs b/NAPS2.Lib/EtoForms/Ui/PreviewForm.cs index ac78e02c9..c4ff03ee3 100644 --- a/NAPS2.Lib/EtoForms/Ui/PreviewForm.cs +++ b/NAPS2.Lib/EtoForms/Ui/PreviewForm.cs @@ -16,14 +16,6 @@ public class PreviewForm : EtoDialogBase _desktopCommands = desktopCommands; ImageList = imageList; - Title = UiStrings.PreviewFormTitle; - Icon = new Icon(1f, Icons.picture.ToEtoImage()); - - FormStateController.AutoLayoutSize = false; - FormStateController.DefaultClientSize = new Size(800, 600); - LayoutController.RootPadding = 0; - LayoutController.Content = _imageView; - GoToPrevCommand = new ActionCommand(() => GoTo(ImageIndex - 1)) { Text = UiStrings.Previous, @@ -36,6 +28,18 @@ public class PreviewForm : EtoDialogBase }; } + protected override void BuildLayout() + { + Title = UiStrings.PreviewFormTitle; + Icon = new Icon(1f, Icons.picture.ToEtoImage()); + + FormStateController.AutoLayoutSize = false; + FormStateController.DefaultClientSize = new Size(800, 600); + + LayoutController.RootPadding = 0; + LayoutController.Content = _imageView; + } + protected DesktopCommands Commands { get; set; } = null!; protected ActionCommand GoToPrevCommand { get; } protected ActionCommand GoToNextCommand { get; } diff --git a/NAPS2.Lib/EtoForms/Ui/ProfilesForm.cs b/NAPS2.Lib/EtoForms/Ui/ProfilesForm.cs index 3f9c955d4..26bcd8be6 100644 --- a/NAPS2.Lib/EtoForms/Ui/ProfilesForm.cs +++ b/NAPS2.Lib/EtoForms/Ui/ProfilesForm.cs @@ -36,9 +36,6 @@ public class ProfilesForm : EtoDialogBase _profileTransfer = profileTransfer; _thumbnailController = thumbnailController; - Title = UiStrings.ProfilesFormTitle; - Icon = new Icon(1f, Icons.blueprints_small.ToEtoImage()); - // TODO: Do this only in WinForms (?) // switch (Handler) // { @@ -119,13 +116,15 @@ public class ProfilesForm : EtoDialogBase ContextMenu.AddItems( new ButtonMenuItem(_deleteCommand)); ContextMenu.Opening += ContextMenuOpening; - - BuildLayout(); } - private void BuildLayout() + protected override void BuildLayout() { + Title = UiStrings.ProfilesFormTitle; + Icon = new Icon(1f, Icons.blueprints_small.ToEtoImage()); + FormStateController.DefaultExtraLayoutSize = new Size(200, 0); + LayoutController.Content = L.Column( L.Row( _listView.Control.XScale(), diff --git a/NAPS2.Lib/EtoForms/Ui/ProgressForm.cs b/NAPS2.Lib/EtoForms/Ui/ProgressForm.cs index 2ef4426bd..e0baa979d 100644 --- a/NAPS2.Lib/EtoForms/Ui/ProgressForm.cs +++ b/NAPS2.Lib/EtoForms/Ui/ProgressForm.cs @@ -19,12 +19,15 @@ public class ProgressForm : EtoDialogBase public ProgressForm(Naps2Config config) : base(config) { - FormStateController.RestoreFormState = false; - _cancel.Click += Cancel_Click; _runInBg.Click += RunInBg_Click; Size = new Size(); + } + + protected override void BuildLayout() + { + FormStateController.RestoreFormState = false; LayoutController.Content = L.Column( _status, diff --git a/NAPS2.Lib/EtoForms/Ui/RecoverForm.cs b/NAPS2.Lib/EtoForms/Ui/RecoverForm.cs index 1f6bc8af4..a1ac9e7dd 100644 --- a/NAPS2.Lib/EtoForms/Ui/RecoverForm.cs +++ b/NAPS2.Lib/EtoForms/Ui/RecoverForm.cs @@ -11,24 +11,24 @@ public class RecoverForm : EtoDialogBase public RecoverForm(Naps2Config config) : base(config) { - FormStateController.SaveFormState = false; - FormStateController.RestoreFormState = false; - - Title = UiStrings.RecoverFormTitle; - // FormStateController.Resizable = false; } - protected override void OnPreLoad(EventArgs e) + protected override void BuildLayout() { + Title = UiStrings.RecoverFormTitle; + + FormStateController.SaveFormState = false; + FormStateController.RestoreFormState = false; + // FormStateController.Resizable = false; + var recoverButton = C.DialogButton(this, UiStrings.Recover, beforeClose: () => SelectedAction = RecoverAction.Recover); var deleteButton = C.DialogButton(this, UiStrings.Delete, beforeClose: () => SelectedAction = RecoverAction.Delete); var notNowButton = C.CancelButton(this, UiStrings.NotNow); - MinimumSize = new Size(350, 0); LayoutController.Content = L.Column( - _prompt.Wrap(400), + _prompt.Wrap(400).MinWidth(300), C.Filler(), L.Row( recoverButton.XScale().Height(32), @@ -36,7 +36,6 @@ public class RecoverForm : EtoDialogBase notNowButton.XScale().Height(32) ) ); - base.OnPreLoad(e); } public RecoverAction SelectedAction { get; private set; } diff --git a/NAPS2.Lib/EtoForms/Ui/SelectDeviceForm.cs b/NAPS2.Lib/EtoForms/Ui/SelectDeviceForm.cs index 28e4c427c..467830c1d 100644 --- a/NAPS2.Lib/EtoForms/Ui/SelectDeviceForm.cs +++ b/NAPS2.Lib/EtoForms/Ui/SelectDeviceForm.cs @@ -7,45 +7,13 @@ namespace NAPS2.EtoForms.Ui; public class SelectDeviceForm : EtoDialogBase { - private readonly ListBox _devices; + private readonly ListBox _devices = new(); public SelectDeviceForm(Naps2Config config) : base(config) { - FormStateController.SaveFormState = false; - FormStateController.RestoreFormState = false; - - var selectButton = new Button - { - Text = UiStrings.Select - }; - DefaultButton = selectButton; - var cancelButton = new Button - { - Text = UiStrings.Cancel - }; - AbortButton = cancelButton; - - _devices = new ListBox(); - - selectButton.Click += Select_Click; - cancelButton.Click += Cancel_Click; - - Title = UiStrings.SelectSource; - FormStateController.DefaultExtraLayoutSize = new Size(50, 0); - LayoutController.Content = L.Row( - _devices.NaturalSize(150, 100).XScale(), - L.Column( - selectButton, - cancelButton - ) - ); } - public List DeviceList { get; set; } = null!; - - public ScanDevice? SelectedDevice { get; private set; } - - protected override void OnPreLoad(EventArgs e) + protected override void BuildLayout() { foreach (var device in DeviceList) { @@ -59,13 +27,27 @@ public class SelectDeviceForm : EtoDialogBase { _devices.SelectedIndex = 0; } - // TODO: Re-order things so we don't need to do this twice - // Just need to be careful not to break things cross-platform - FormStateController.UpdateLayoutSize(LayoutController); - base.OnPreLoad(e); + + Title = UiStrings.SelectSource; + + FormStateController.SaveFormState = false; + FormStateController.RestoreFormState = false; + FormStateController.DefaultExtraLayoutSize = new Size(50, 0); + + LayoutController.Content = L.Row( + _devices.NaturalSize(150, 100).XScale(), + L.Column( + C.OkButton(this, SelectDevice, UiStrings.Select), + C.CancelButton(this) + ) + ); } - private void Select_Click(object? sender, EventArgs e) + public List DeviceList { get; set; } = null!; + + public ScanDevice? SelectedDevice { get; private set; } + + private void SelectDevice() { if (_devices.SelectedValue == null) { @@ -73,11 +55,5 @@ public class SelectDeviceForm : EtoDialogBase return; } SelectedDevice = DeviceList.FirstOrDefault(x => x.ID == _devices.SelectedKey); - Close(); - } - - private void Cancel_Click(object? sender, EventArgs e) - { - Close(); } } \ No newline at end of file