Hide update check on Linux and Mac

This commit is contained in:
Ben Olden-Cooligan 2023-04-08 21:02:33 -07:00
parent 09dbcfe2a5
commit 5830df4d72
2 changed files with 114 additions and 83 deletions

View File

@ -1,6 +1,7 @@
using Eto.Drawing;
using Eto.Forms;
using NAPS2.EtoForms.Layout;
using NAPS2.EtoForms.Widgets;
using NAPS2.Update;
namespace NAPS2.EtoForms.Ui;
@ -12,14 +13,7 @@ public class AboutForm : EtoDialogBase
private const string DONATE_URL = "https://www.naps2.com/donate";
private readonly Control _donateButton;
#if !MSI
private readonly UpdateChecker _updateChecker;
private readonly CheckBox _checkForUpdates;
private readonly Panel _updatePanel;
private bool _hasCheckedForUpdates;
private UpdateInfo? _update;
#endif
public AboutForm(Naps2Config config, UpdateChecker updateChecker)
: base(config)
@ -29,21 +23,11 @@ public class AboutForm : EtoDialogBase
UiStrings.Donate,
() => ProcessHelper.OpenUrl(DONATE_URL));
#if !MSI
_updateChecker = updateChecker;
_checkForUpdates = new CheckBox { Text = UiStrings.CheckForUpdates };
_checkForUpdates.CheckedChanged += CheckForUpdatesChanged;
_updatePanel = new Panel();
UpdateControls();
#endif
}
protected override void BuildLayout()
{
#if !MSI
_checkForUpdates.Checked = Config.Get(c => c.CheckForUpdates);
#endif
Title = UiStrings.AboutFormTitle;
Icon = new Icon(1f, Icons.information_small.ToEtoImage());
@ -67,11 +51,7 @@ public class AboutForm : EtoDialogBase
_donateButton
).Padding(left: 10)
),
#if !MSI
C.TextSpace(),
_checkForUpdates.Padding(left: 4),
_updatePanel,
#endif
GetUpdateWidget(),
C.TextSpace(),
C.NoWrap(string.Format(UiStrings.CopyrightFormat, AssemblyHelper.COPYRIGHT_YEARS)),
C.TextSpace(),
@ -89,67 +69,15 @@ public class AboutForm : EtoDialogBase
);
}
#if !MSI
private void DoUpdateCheck()
private LayoutElement GetUpdateWidget()
{
if (_checkForUpdates.IsChecked())
{
_updateChecker.CheckForUpdates().ContinueWith(task =>
{
if (task.IsFaulted)
{
Log.ErrorException("Error checking for updates", task.Exception!);
}
else
{
var transact = Config.User.BeginTransaction();
transact.Set(c => c.HasCheckedForUpdates, true);
transact.Set(c => c.LastUpdateCheckDate, DateTime.Now);
transact.Commit();
}
_update = task.Result;
_hasCheckedForUpdates = true;
Invoker.Current.Invoke(UpdateControls);
});
}
}
private void UpdateControls()
{
_updatePanel.Content = GetUpdatePanelContent();
}
private Control GetUpdatePanelContent()
{
if (!_checkForUpdates.IsChecked())
{
return C.NoWrap(MiscResources.UpdateCheckDisabled);
}
if (!_hasCheckedForUpdates)
{
return C.NoWrap(MiscResources.CheckingForUpdates);
}
if (_update == null)
{
return C.NoWrap(MiscResources.NoUpdates);
}
return C.Link(string.Format(MiscResources.Install, _update.Name),
InstallLinkClicked);
}
private void InstallLinkClicked()
{
if (_update != null)
{
_updateChecker.StartUpdate(_update);
}
}
private void CheckForUpdatesChanged(object? sender, EventArgs e)
{
Config.User.Set(c => c.CheckForUpdates, _checkForUpdates.IsChecked());
UpdateControls();
DoUpdateCheck();
}
#if MSI
return C.None();
#else
#if NET6_0_OR_GREATER
if (!OperatingSystem.IsWindows()) return C.None();
#endif
return new UpdateCheckWidget(_updateChecker, Config);
#endif
}
}

View File

@ -0,0 +1,103 @@
using Eto.Forms;
using NAPS2.EtoForms.Layout;
using NAPS2.Update;
namespace NAPS2.EtoForms.Widgets;
public class UpdateCheckWidget
{
private readonly UpdateChecker _updateChecker;
private readonly Naps2Config _config;
private readonly CheckBox _checkForUpdates;
private readonly Panel _updatePanel;
private bool _hasCheckedForUpdates;
private UpdateInfo? _update;
public UpdateCheckWidget(UpdateChecker updateChecker, Naps2Config config)
{
_updateChecker = updateChecker;
_config = config;
_checkForUpdates = new CheckBox { Text = UiStrings.CheckForUpdates };
_checkForUpdates.Checked = config.Get(c => c.CheckForUpdates);
_checkForUpdates.CheckedChanged += CheckForUpdatesChanged;
_updatePanel = new Panel();
UpdateControls();
DoUpdateCheck();
}
public LayoutColumn AsControl()
{
return L.Column(
C.TextSpace(),
_checkForUpdates.Padding(left: 4),
_updatePanel
);
}
public static implicit operator LayoutElement(UpdateCheckWidget control)
{
return control.AsControl();
}
private void DoUpdateCheck()
{
if (_checkForUpdates.IsChecked())
{
_updateChecker.CheckForUpdates().ContinueWith(task =>
{
if (task.IsFaulted)
{
Log.ErrorException("Error checking for updates", task.Exception!);
}
else
{
var transact = _config.User.BeginTransaction();
transact.Set(c => c.HasCheckedForUpdates, true);
transact.Set(c => c.LastUpdateCheckDate, DateTime.Now);
transact.Commit();
}
_update = task.Result;
_hasCheckedForUpdates = true;
Invoker.Current.Invoke(UpdateControls);
});
}
}
private void UpdateControls()
{
_updatePanel.Content = GetUpdatePanelContent();
}
private Control GetUpdatePanelContent()
{
if (!_checkForUpdates.IsChecked())
{
return C.NoWrap(MiscResources.UpdateCheckDisabled);
}
if (!_hasCheckedForUpdates)
{
return C.NoWrap(MiscResources.CheckingForUpdates);
}
if (_update == null)
{
return C.NoWrap(MiscResources.NoUpdates);
}
return C.Link(string.Format(MiscResources.Install, _update.Name),
InstallLinkClicked);
}
private void InstallLinkClicked()
{
if (_update != null)
{
_updateChecker.StartUpdate(_update);
}
}
private void CheckForUpdatesChanged(object? sender, EventArgs e)
{
_config.User.Set(c => c.CheckForUpdates, _checkForUpdates.IsChecked());
UpdateControls();
DoUpdateCheck();
}
}