Skip to content

Commit

Permalink
Add a "Keep images across sessions" setting
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanfish committed Jan 15, 2024
1 parent ea46c1c commit 237e1c9
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 18 deletions.
3 changes: 3 additions & 0 deletions NAPS2.Lib/Config/CommonConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ public class CommonConfig
[Common]
public bool DeleteAfterSaving { get; set; }

[Common]
public bool KeepSession { get; set; }

[Common]
public bool DisableSaveNotifications { get; set; }

Expand Down
2 changes: 2 additions & 0 deletions NAPS2.Lib/Config/ConfigSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ private ConfigStorage<CommonConfig> AppConfigV0ToCommonConfigDefault(AppConfigV0
storage.Set(x => x.ScanButtonDefaultAction, c.ScanButtonDefaultAction);
storage.Set(x => x.SaveButtonDefaultAction, c.SaveButtonDefaultAction);
storage.Set(x => x.DeleteAfterSaving, c.DeleteAfterSaving);
storage.Set(x => x.KeepSession, c.KeepSession);
storage.Set(x => x.SingleInstance, c.SingleInstance);
storage.Set(x => x.HiddenButtons, GetHiddenButtonFlags(c));
storage.Set(x => x.DisableAutoSave, c.DisableAutoSave);
Expand Down Expand Up @@ -163,6 +164,7 @@ void SetIfLocked<T>(Expression<Func<CommonConfig, T>> accessor, T value, string
SetIfLocked(x => x.ScanButtonDefaultAction, c.ScanButtonDefaultAction, nameof(c.ScanButtonDefaultAction));
SetIfLocked(x => x.SaveButtonDefaultAction, c.SaveButtonDefaultAction, nameof(c.SaveButtonDefaultAction));
SetIfLocked(x => x.DeleteAfterSaving, c.DeleteAfterSaving, nameof(c.DeleteAfterSaving));
SetIfLocked(x => x.KeepSession, c.KeepSession, nameof(c.KeepSession));
SetIfLocked(x => x.SingleInstance, c.SingleInstance, nameof(c.SingleInstance));

return storage;
Expand Down
2 changes: 2 additions & 0 deletions NAPS2.Lib/Config/ObsoleteTypes/AppConfigV0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class AppConfigV0

public bool DeleteAfterSaving { get; set; }

public bool KeepSession { get; set; }

public bool SingleInstance { get; set; }

public bool HideOcrButton { get; set; }
Expand Down
21 changes: 16 additions & 5 deletions NAPS2.Lib/EtoForms/Desktop/DesktopController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public void Cleanup()
if (_suspended) return;
Pipes.KillServer();
_sharedDeviceManager.StopSharing();
if (!SkipRecoveryCleanup)
if (!SkipRecoveryCleanup && !_config.Get(c => c.KeepSession))
{
try
{
Expand Down Expand Up @@ -204,7 +204,7 @@ public bool PrepareForClosing(bool userClosing)
}
else if (_imageList.Images.Any() && _imageList.HasUnsavedChanges)
{
if (userClosing && !SkipRecoveryCleanup)
if (userClosing && !SkipRecoveryCleanup && !_config.Get(c => c.KeepSession))
{
var result = MessageBox.Show(_desktopFormProvider.DesktopForm, MiscResources.ExitWithUnsavedChanges,
MiscResources.UnsavedChanges,
Expand Down Expand Up @@ -301,10 +301,21 @@ private void ShowRecoveryPrompt()
{
// Allow scanned images to be recovered in case of an unexpected close
var op = _operationFactory.Create<RecoveryOperation>();
if (op.Start(_desktopImagesController.ReceiveScannedImage(),
new RecoveryParams { ThumbnailSize = _thumbnailController.RenderSize }))
var recoveryParams = new RecoveryParams
{
_operationProgress.ShowProgress(op);
AutoSessionRestore = _config.Get(c => c.KeepSession),
ThumbnailSize = _thumbnailController.RenderSize
};
if (op.Start(_desktopImagesController.ReceiveScannedImage(), recoveryParams))
{
if (recoveryParams.AutoSessionRestore)
{
_operationProgress.ShowBackgroundProgress(op);
}
else
{
_operationProgress.ShowProgress(op);
}
}
}

Expand Down
22 changes: 13 additions & 9 deletions NAPS2.Lib/EtoForms/Ui/SettingsForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ internal class SettingsForm : EtoDialogBase
private readonly DropDown _scanButtonDefaultAction = C.EnumDropDown<ScanButtonDefaultAction>();
private readonly DropDown _saveButtonDefaultAction = C.EnumDropDown<SaveButtonDefaultAction>();
private readonly CheckBox _clearAfterSaving = C.CheckBox(UiStrings.ClearAfterSaving);
private readonly CheckBox _keepSession = C.CheckBox(UiStrings.KeepSession);
private readonly CheckBox _singleInstance = C.CheckBox(UiStrings.SingleInstanceDesc);
private readonly Command _pdfSettingsCommand;
private readonly Command _imageSettingsCommand;
Expand Down Expand Up @@ -72,18 +73,19 @@ protected override void BuildLayout()
C.Label(UiStrings.SaveButtonDefaultAction).AlignCenter().Padding(right: 20),
_saveButtonDefaultAction
).Aligned()
: C.None(),
_clearAfterSaving
: C.None()
)
),
PlatformCompat.System.SupportsSingleInstance
? L.GroupBox(
UiStrings.Application,
L.Column(
_singleInstance
)
L.GroupBox(
UiStrings.Application,
L.Column(
_clearAfterSaving,
_keepSession,
PlatformCompat.System.SupportsSingleInstance
? _singleInstance
: C.None()
)
: C.None(),
),
// TODO: Probably only show these after we start adding tabs
// L.Row(
// C.Button(_pdfSettingsCommand, ButtonImagePosition.Left),
Expand Down Expand Up @@ -116,6 +118,7 @@ void UpdateCheckbox(CheckBox checkBox, Expression<Func<CommonConfig, bool>> acce
_saveButtonDefaultAction.SelectedIndex = (int) config.Get(c => c.SaveButtonDefaultAction);
_saveButtonDefaultAction.Enabled = !config.AppLocked.Has(c => c.SaveButtonDefaultAction);
UpdateCheckbox(_clearAfterSaving, c => c.DeleteAfterSaving);
UpdateCheckbox(_keepSession, c => c.KeepSession);
UpdateCheckbox(_singleInstance, c => c.SingleInstance);
}

Expand All @@ -136,6 +139,7 @@ void SetIfChanged<T>(Expression<Func<CommonConfig, T>> accessor, T value)
SetIfChanged(c => c.ScanButtonDefaultAction, (ScanButtonDefaultAction) _scanButtonDefaultAction.SelectedIndex);
SetIfChanged(c => c.SaveButtonDefaultAction, (SaveButtonDefaultAction) _saveButtonDefaultAction.SelectedIndex);
SetIfChanged(c => c.DeleteAfterSaving, _clearAfterSaving.IsChecked());
SetIfChanged(c => c.KeepSession, _keepSession.IsChecked());
SetIfChanged(c => c.SingleInstance, _singleInstance.IsChecked());
transact.Commit();

Expand Down
9 changes: 9 additions & 0 deletions NAPS2.Lib/Lang/Resources/UiStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions NAPS2.Lib/Lang/Resources/UiStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,9 @@
<data name="ClearAfterSaving" xml:space="preserve">
<value>Clear images after saving</value>
</data>
<data name="KeepSession" xml:space="preserve">
<value>Keep images across sessions</value>
</data>
<data name="PageSizeFormTitle" xml:space="preserve">
<value>Custom Page Size</value>
</data>
Expand Down
11 changes: 9 additions & 2 deletions NAPS2.Lib/Recovery/RecoverableFolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,14 @@ public bool TryRecover(Action<ProcessedImage> imageCallback, RecoveryParams reco

try
{
File.Copy(imagePath, newPath);
if (recoveryParams.AutoSessionRestore)
{
File.Move(imagePath, newPath);
}
else
{
File.Copy(imagePath, newPath);
}
}
catch (Exception e)
{
Expand Down Expand Up @@ -158,7 +165,7 @@ private ProcessedImage CreateRecoveredImage(RecoveryParams recoveryParams, IImag
// TODO: Make this take a lazy rendered image or something
processedImage = ImportPostProcessor.AddPostProcessingData(processedImage,
null,
recoveryParams.ThumbnailSize,
recoveryParams.AutoSessionRestore ? null : recoveryParams.ThumbnailSize,
new BarcodeDetectionOptions(),
true);
return processedImage;
Expand Down
4 changes: 2 additions & 2 deletions NAPS2.Lib/Recovery/RecoveryOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public RecoveryOperation(IFormFactory formFactory, RecoveryManager recoveryManag
_formFactory = formFactory;
_recoveryManager = recoveryManager;

ProgressTitle = MiscResources.ImportProgress;
ProgressTitle = MiscResources.RecoveryProgress;
AllowCancel = true;
AllowBackground = true;
}
Expand All @@ -32,7 +32,7 @@ public bool Start(Action<ProcessedImage> imageCallback, RecoveryParams recoveryP
}
try
{
switch (PromptToRecover(recoverableFolder))
switch (recoveryParams.AutoSessionRestore ? RecoverAction.Recover : PromptToRecover(recoverableFolder))
{
case RecoverAction.Recover:
RunAsync(() =>
Expand Down
2 changes: 2 additions & 0 deletions NAPS2.Lib/Recovery/RecoveryParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@

public class RecoveryParams
{
public bool AutoSessionRestore { get; set; }

public int? ThumbnailSize { get; set; }
}
1 change: 1 addition & 0 deletions NAPS2.Setup/appsettings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<ScanButtonDefaultAction mode="default">ScanWithDefaultProfile</ScanButtonDefaultAction>
<SaveButtonDefaultAction mode="default">SaveAll</SaveButtonDefaultAction>
<DeleteAfterSaving mode="default">false</DeleteAfterSaving>
<KeepSession mode="default">false</KeepSession>
<SingleInstance mode="default">false</SingleInstance>
<HideOcrButton>false</HideOcrButton>
<HideImportButton>false</HideImportButton>
Expand Down

0 comments on commit 237e1c9

Please sign in to comment.