improve:改进自动更新

This commit is contained in:
CJK_mkp
2025-06-29 11:56:38 +08:00
parent 352aa886c8
commit 54d0aaca04
9 changed files with 403 additions and 131 deletions
+188 -14
View File
@@ -15,7 +15,10 @@ namespace Ink_Canvas.Helpers
{
internal class AutoUpdateHelper
{
public static async Task<string> CheckForUpdates(string proxy = null)
// 定义超时时间为10秒
private static readonly TimeSpan RequestTimeout = TimeSpan.FromSeconds(10);
public static async Task<string> CheckForUpdates(string proxy = null, UpdateChannel channel = UpdateChannel.Release)
{
try
{
@@ -23,8 +26,24 @@ namespace Ink_Canvas.Helpers
LogHelper.WriteLogToFile($"AutoUpdate | Local version: {localVersion}");
string remoteAddress = proxy;
string primaryUrl = "https://raw.githubusercontent.com/InkCanvasForClass/community/refs/heads/main/AutomaticUpdateVersionControl.txt";
string fallbackUrl = "https://raw.bgithub.xyz/InkCanvasForClass/community/refs/heads/main/AutomaticUpdateVersionControl.txt";
// 根据通道选择URL
string primaryUrl, fallbackUrl;
if (channel == UpdateChannel.Release)
{
// Release通道版本信息地址
primaryUrl = "https://github.com/InkCanvasForClass/community/raw/refs/heads/beta/AutomaticUpdateVersionControl.txt";
fallbackUrl = "https://bgithub.xyz/InkCanvasForClass/community/raw/refs/heads/main/AutomaticUpdateVersionControl.txt";
}
else
{
// Beta通道版本信息地址
primaryUrl = "https://github.com/InkCanvasForClass/community-beta/raw/refs/heads/main/AutomaticUpdateVersionControl.txt";
fallbackUrl = "https://bgithub.xyz/InkCanvasForClass/community-beta/raw/refs/heads/main/AutomaticUpdateVersionControl.txt";
}
LogHelper.WriteLogToFile($"AutoUpdate | Checking for updates on {channel} channel");
// 先尝试主地址
remoteAddress += primaryUrl;
@@ -72,19 +91,19 @@ namespace Ink_Canvas.Helpers
{
try
{
// Set a reasonable timeout
client.Timeout = TimeSpan.FromSeconds(10); // 减少超时时间以便更快切换到备用地址
// 设置超时时间为10秒
client.Timeout = RequestTimeout;
LogHelper.WriteLogToFile($"AutoUpdate | Sending HTTP request to: {fileUrl}");
// 使用带超时的Task.WhenAny来确保请求不会无限期等待
var downloadTask = client.GetAsync(fileUrl);
var timeoutTask = Task.Delay(client.Timeout);
var timeoutTask = Task.Delay(RequestTimeout);
var completedTask = await Task.WhenAny(downloadTask, timeoutTask);
if (completedTask == timeoutTask)
{
LogHelper.WriteLogToFile($"AutoUpdate | Request timed out after {client.Timeout.TotalSeconds} seconds", LogHelper.LogType.Error);
LogHelper.WriteLogToFile($"AutoUpdate | Request timed out after {RequestTimeout.TotalSeconds} seconds", LogHelper.LogType.Error);
return null;
}
@@ -150,7 +169,7 @@ namespace Ink_Canvas.Helpers
private static string updatesFolderPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "AutoUpdate");
private static string statusFilePath = null;
public static async Task<bool> DownloadSetupFileAndSaveStatus(string version, string proxy = "")
public static async Task<bool> DownloadSetupFileAndSaveStatus(string version, string proxy = "", UpdateChannel channel = UpdateChannel.Release)
{
try
{
@@ -169,10 +188,21 @@ namespace Ink_Canvas.Helpers
LogHelper.WriteLogToFile($"AutoUpdate | Created updates directory: {updatesFolderPath}");
}
// 下载地址
string primaryUrl = $"{proxy}https://github.com/InkCanvasForClass/community/releases/download/{version}/InkCanvasForClass.CE.{version}.zip";
// 备用下载地址
string fallbackUrl = $"{proxy}https://bgithub.xyz/InkCanvasForClass/community/releases/download/{version}/InkCanvasForClass.CE.{version}.zip";
// 根据通道选择下载地址
string primaryUrl, fallbackUrl;
if (channel == UpdateChannel.Release)
{
// Release通道下载地址
primaryUrl = $"{proxy}https://github.com/InkCanvasForClass/community/releases/download/{version}/InkCanvasForClass.CE.{version}.zip";
fallbackUrl = $"{proxy}https://bgithub.xyz/InkCanvasForClass/community/releases/download/{version}/InkCanvasForClass.CE.{version}.zip";
}
else
{
// Beta通道下载地址
primaryUrl = $"{proxy}https://github.com/InkCanvasForClass/community-beta/releases/download/{version}/InkCanvasForClass.CE.{version}.zip";
fallbackUrl = $"{proxy}https://bgithub.xyz/InkCanvasForClass/community-beta/releases/download/{version}/InkCanvasForClass.CE.{version}.zip";
}
LogHelper.WriteLogToFile($"AutoUpdate | Primary download URL: {primaryUrl}");
@@ -222,7 +252,7 @@ namespace Ink_Canvas.Helpers
try
{
// Configure client
client.Timeout = TimeSpan.FromMinutes(5); // Longer timeout for downloading larger files
client.Timeout = TimeSpan.FromMinutes(5); // 下载文件需要更长的超时时间
client.DefaultRequestHeaders.Add("User-Agent", "ICC-CE Auto Updater");
LogHelper.WriteLogToFile($"AutoUpdate | Downloading from: {fileUrl}");
@@ -239,7 +269,7 @@ namespace Ink_Canvas.Helpers
// 使用带超时的Task.WhenAny来确保请求不会无限期等待
var downloadTask = client.GetAsync(fileUrl, HttpCompletionOption.ResponseHeadersRead);
var initialTimeoutTask = Task.Delay(TimeSpan.FromSeconds(30)); // 30秒内必须有响应
var initialTimeoutTask = Task.Delay(RequestTimeout); // 使用全局定义的10秒超时
var completedTask = await Task.WhenAny(downloadTask, initialTimeoutTask);
if (completedTask == initialTimeoutTask)
@@ -769,6 +799,150 @@ namespace Ink_Canvas.Helpers
LogHelper.WriteLogToFile($"AutoUpdate | Error deleting updates folder: {ex.Message}", LogHelper.LogType.Error);
}
}
// 新增:版本修复方法,强制下载并安装指定通道的最新版本
public static async Task<bool> FixVersion(UpdateChannel channel = UpdateChannel.Release)
{
try
{
LogHelper.WriteLogToFile($"AutoUpdate | Starting version fix for {channel} channel");
// 获取当前通道的最新版本
string latestVersion = await CheckForUpdates(null, channel);
if (string.IsNullOrEmpty(latestVersion))
{
LogHelper.WriteLogToFile("AutoUpdate | No newer version found for fixing", LogHelper.LogType.Warning);
return false;
}
// 下载最新版本
bool downloadResult = await DownloadSetupFileAndSaveStatus(latestVersion, "", channel);
if (!downloadResult)
{
LogHelper.WriteLogToFile("AutoUpdate | Failed to download update for fixing", LogHelper.LogType.Error);
return false;
}
// 执行安装,非静默模式
InstallNewVersionApp(latestVersion, false);
// 设置为用户主动退出,避免被看门狗判定为崩溃
App.IsAppExitByUser = true;
// 关闭应用程序
Application.Current.Dispatcher.Invoke(() => {
Application.Current.Shutdown();
});
return true;
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"AutoUpdate | Error in FixVersion: {ex.Message}", LogHelper.LogType.Error);
return false;
}
}
// 获取更新日志
public static async Task<string> GetUpdateLog(UpdateChannel channel = UpdateChannel.Release)
{
try
{
string primaryUrl, fallbackUrl;
if (channel == UpdateChannel.Release)
{
// Release通道更新日志地址
primaryUrl = "https://github.com/InkCanvasForClass/community/raw/refs/heads/beta/UpdateLog.txt";
fallbackUrl = "https://bgithub.xyz/InkCanvasForClass/community/raw/refs/heads/main/UpdateLog.txt";
}
else
{
// Beta通道更新日志地址
primaryUrl = "https://github.com/InkCanvasForClass/community-beta/raw/refs/heads/main/UpdateLog.txt";
fallbackUrl = "https://bgithub.xyz/InkCanvasForClass/community-beta/raw/refs/heads/main/UpdateLog.txt";
}
LogHelper.WriteLogToFile($"AutoUpdate | Getting update log from {channel} channel");
// 先尝试主地址
string updateLog = await GetRemoteContent(primaryUrl);
// 如果主地址失败,尝试备用地址
if (string.IsNullOrEmpty(updateLog))
{
LogHelper.WriteLogToFile($"AutoUpdate | Primary URL failed for update log, trying fallback URL");
updateLog = await GetRemoteContent(fallbackUrl);
}
if (!string.IsNullOrEmpty(updateLog))
{
LogHelper.WriteLogToFile($"AutoUpdate | Successfully retrieved update log");
return updateLog;
}
else
{
LogHelper.WriteLogToFile("AutoUpdate | Failed to retrieve update log from both URLs.", LogHelper.LogType.Error);
return $"# 无法获取更新日志\n\n无法从服务器获取更新日志信息,请检查网络连接后重试。";
}
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"AutoUpdate | Error in GetUpdateLog: {ex.Message}", LogHelper.LogType.Error);
return $"# 获取更新日志时发生错误\n\n错误信息: {ex.Message}";
}
}
// 获取远程内容的通用方法
private static async Task<string> GetRemoteContent(string fileUrl)
{
using (HttpClient client = new HttpClient())
{
try
{
// 设置超时时间为10秒
client.Timeout = RequestTimeout;
LogHelper.WriteLogToFile($"AutoUpdate | Sending HTTP request to: {fileUrl}");
// 使用带超时的Task.WhenAny来确保请求不会无限期等待
var downloadTask = client.GetAsync(fileUrl);
var timeoutTask = Task.Delay(RequestTimeout);
var completedTask = await Task.WhenAny(downloadTask, timeoutTask);
if (completedTask == timeoutTask)
{
LogHelper.WriteLogToFile($"AutoUpdate | Request timed out after {RequestTimeout.TotalSeconds} seconds", LogHelper.LogType.Error);
return null;
}
// 请求完成,检查结果
HttpResponseMessage response = await downloadTask;
LogHelper.WriteLogToFile($"AutoUpdate | HTTP response status: {response.StatusCode}");
response.EnsureSuccessStatusCode();
string content = await response.Content.ReadAsStringAsync();
return content;
}
catch (HttpRequestException ex)
{
LogHelper.WriteLogToFile($"AutoUpdate | HTTP request error: {ex.Message}", LogHelper.LogType.Error);
}
catch (TaskCanceledException ex)
{
LogHelper.WriteLogToFile($"AutoUpdate | Request timed out: {ex.Message}", LogHelper.LogType.Error);
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"AutoUpdate | Error: {ex.Message}", LogHelper.LogType.Error);
}
return null;
}
}
}
internal class AutoUpdateWithSilenceTimeComboBox
+23 -3
View File
@@ -652,16 +652,36 @@
FontSize="26" />
</GroupBox.Header>
<ui:SimpleStackPanel Spacing="6">
<ui:ToggleSwitch OnContent="" OffContent="" Visibility="Collapsed"
<ui:ToggleSwitch OnContent="" OffContent=""
Name="ToggleSwitchIsAutoUpdate" Header="自动检查更新"
FontFamily="Microsoft YaHei UI"
Toggled="ToggleSwitchIsAutoUpdate_Toggled" />
<ui:ToggleSwitch OnContent="" OffContent="" Visibility="Collapsed"
<ui:ToggleSwitch OnContent="" OffContent=""
Name="ToggleSwitchIsAutoUpdateWithSilence" Header="静默更新"
FontFamily="Microsoft YaHei UI"
Toggled="ToggleSwitchIsAutoUpdateWithSilence_Toggled" />
<TextBlock Text="# 静默更新将在软件不使用时自动安装,无需手动操作" TextWrapping="Wrap" Foreground="#a1a1aa" />
<!-- 更新通道选择 -->
<ui:SimpleStackPanel Spacing="8" Margin="0,8,0,0">
<TextBlock Text="更新通道" FontSize="15" FontWeight="Bold" Foreground="#fafafa"/>
<ui:RadioButtons x:Name="UpdateChannelSelector" Margin="0,4,0,0">
<RadioButton Content="稳定版 (Release)" GroupName="UpdateChannel"
Tag="Release" Checked="UpdateChannelSelector_Checked"/>
<RadioButton Content="测试版 (Beta)" GroupName="UpdateChannel"
Tag="Beta" Checked="UpdateChannelSelector_Checked"/>
</ui:RadioButtons>
<TextBlock Text="# 稳定版提供可靠更新,测试版提供新功能抢先体验" TextWrapping="Wrap" Foreground="#a1a1aa" />
</ui:SimpleStackPanel>
<!-- 版本修复按钮 -->
<Button x:Name="FixVersionButton" Content="版本修复" Margin="0,8,0,0"
Width="120" HorizontalAlignment="Left" Click="FixVersionButton_Click"/>
<TextBlock Text="# 版本修复会根据当前选择的通道下载最新版本并执行安装,可用于修复损坏的安装"
TextWrapping="Wrap" Foreground="#a1a1aa" />
<Border BorderBrush="Black" BorderThickness="1" CornerRadius="5" Padding="12"
Visibility="{Binding ElementName=ToggleSwitchIsAutoUpdateWithSilence, Path=Visibility}">
Visibility="{Binding ElementName=ToggleSwitchIsAutoUpdateWithSilence, Path=IsOn, Converter={StaticResource BooleanToVisibilityConverter}}">
<ui:SimpleStackPanel Spacing="12">
<TextBlock
Text="# 关闭静默更新后,已完成安装包的下载后将会弹窗询问是否进行更新,开启静默更新后将会在安装包下载完成后每隔十分钟进行如下检测:①处于静默更新时间段内 ②未处于书写模式 ③未处于画板内。若以上检测通过即会关闭软件进行自动更新。"
+75 -53
View File
@@ -337,68 +337,79 @@ namespace Ink_Canvas {
}
private async void AutoUpdate() {
AvailableLatestVersion = await AutoUpdateHelper.CheckForUpdates();
// 使用当前选择的更新通道检查更新
AvailableLatestVersion = await AutoUpdateHelper.CheckForUpdates(null, Settings.Startup.UpdateChannel);
// 声明下载状态变量,用于整个方法
bool isDownloadSuccessful = false;
if (AvailableLatestVersion != null) {
// 打开更新提示窗口
// 检测到新版本
LogHelper.WriteLogToFile($"AutoUpdate | New version available: {AvailableLatestVersion}");
// 获取当前版本和发布日期
string currentVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
string releaseDate = DateTime.Now.ToString("yyyy年MM月dd日");
// 检查是否是用户选择跳过的版本
if (!string.IsNullOrEmpty(Settings.Startup.SkippedVersion) &&
Settings.Startup.SkippedVersion == AvailableLatestVersion) {
LogHelper.WriteLogToFile($"AutoUpdate | Version {AvailableLatestVersion} was marked to be skipped by the user");
return; // 跳过此版本,不执行更新操作
}
// 创建更新说明内容(可以从服务器获取或直接在此处设置)
string releaseNotes = $@"# InkCanvasForClass v{AvailableLatestVersion}更新
你好,此次更新包含了一系列新功能和改进:
1. 修复了一些已知的bug
2. 优化了程序性能
3. 改进了用户界面
4. 添加了新的功能
感谢您使用InkCanvasForClass CE";
// 创建并显示更新窗口
HasNewUpdateWindow updateWindow = new HasNewUpdateWindow(currentVersion, AvailableLatestVersion, releaseDate, releaseNotes);
bool? dialogResult = updateWindow.ShowDialog();
// 声明下载结果变量
bool isDownloadSuccessful;
// 如果窗口被关闭但没有点击按钮,视为"稍后更新"
if (dialogResult != true) {
LogHelper.WriteLogToFile("AutoUpdate | Update dialog closed without selection");
// 更新自动更新设置并保存
Settings.Startup.IsAutoUpdate = updateWindow.IsAutoUpdateEnabled;
Settings.Startup.IsAutoUpdateWithSilence = updateWindow.IsSilentUpdateEnabled;
// 如果检测到的版本与跳过的版本不同,则清除跳过版本记录
// 这确保用户只能跳过当前最新版本,而不是永久跳过所有更新
if (!string.IsNullOrEmpty(Settings.Startup.SkippedVersion) &&
Settings.Startup.SkippedVersion != AvailableLatestVersion) {
LogHelper.WriteLogToFile($"AutoUpdate | Detected new version {AvailableLatestVersion} different from skipped version {Settings.Startup.SkippedVersion}, clearing skip record");
Settings.Startup.SkippedVersion = "";
SaveSettingsToFile();
}
// 获取当前版本
string currentVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
// 如果启用了静默更新,则自动下载更新而不显示提示
if (Settings.Startup.IsAutoUpdateWithSilence) {
LogHelper.WriteLogToFile("AutoUpdate | Silent update enabled, downloading update automatically without notification");
// 如果启用了静默更新,则自动下载更新
if (Settings.Startup.IsAutoUpdateWithSilence) {
LogHelper.WriteLogToFile("AutoUpdate | Silent update enabled, downloading update automatically");
// 静默下载更新,传递当前选择的更新通道
isDownloadSuccessful = await AutoUpdateHelper.DownloadSetupFileAndSaveStatus(AvailableLatestVersion, "", Settings.Startup.UpdateChannel);
if (isDownloadSuccessful) {
LogHelper.WriteLogToFile("AutoUpdate | Update downloaded successfully, will install when conditions are met");
// 静默下载更新
isDownloadSuccessful = await AutoUpdateHelper.DownloadSetupFileAndSaveStatus(AvailableLatestVersion);
if (isDownloadSuccessful) {
LogHelper.WriteLogToFile("AutoUpdate | Update downloaded successfully, will install when application closes");
// 启动检查定时器
timerCheckAutoUpdateWithSilence.Start();
} else {
LogHelper.WriteLogToFile("AutoUpdate | Silent update download failed", LogHelper.LogType.Error);
}
// 启动检查定时器,定期检查是否可以安装
timerCheckAutoUpdateWithSilence.Start();
} else {
LogHelper.WriteLogToFile("AutoUpdate | Silent update download failed", LogHelper.LogType.Error);
}
return;
}
// 更新自动更新设置并保存
Settings.Startup.IsAutoUpdate = updateWindow.IsAutoUpdateEnabled;
Settings.Startup.IsAutoUpdateWithSilence = updateWindow.IsSilentUpdateEnabled;
SaveSettingsToFile();
// 如果没有启用静默更新,则显示常规更新窗口
string releaseDate = DateTime.Now.ToString("yyyy年MM月dd日");
// 从服务器获取更新日志
string releaseNotes = await AutoUpdateHelper.GetUpdateLog(Settings.Startup.UpdateChannel);
// 如果获取失败,使用默认文本
if (string.IsNullOrEmpty(releaseNotes))
{
releaseNotes = $@"# InkCanvasForClass v{AvailableLatestVersion}更新
无法获取更新日志,但新版本已准备就绪。";
}
// 创建并显示更新窗口
HasNewUpdateWindow updateWindow = new HasNewUpdateWindow(currentVersion, AvailableLatestVersion, releaseDate, releaseNotes);
bool? dialogResult = updateWindow.ShowDialog();
// 如果窗口被关闭但没有点击按钮,则不执行任何操作
if (dialogResult != true) {
LogHelper.WriteLogToFile("AutoUpdate | Update dialog closed without selection");
return;
}
// 不再从更新窗口获取自动更新设置
// 根据用户选择处理更新
switch (updateWindow.Result) {
@@ -409,8 +420,8 @@ namespace Ink_Canvas {
// 显示下载进度提示
MessageBox.Show("开始下载更新,请稍候...", "正在更新", MessageBoxButton.OK, MessageBoxImage.Information);
// 下载更新文件
isDownloadSuccessful = await AutoUpdateHelper.DownloadSetupFileAndSaveStatus(AvailableLatestVersion);
// 下载更新文件,传递当前选择的更新通道
isDownloadSuccessful = await AutoUpdateHelper.DownloadSetupFileAndSaveStatus(AvailableLatestVersion, "", Settings.Startup.UpdateChannel);
if (isDownloadSuccessful) {
// 下载成功,提示用户准备安装
@@ -440,7 +451,7 @@ namespace Ink_Canvas {
LogHelper.WriteLogToFile("AutoUpdate | User chose to update later");
// 不管设置如何,都进行下载
isDownloadSuccessful = await AutoUpdateHelper.DownloadSetupFileAndSaveStatus(AvailableLatestVersion);
isDownloadSuccessful = await AutoUpdateHelper.DownloadSetupFileAndSaveStatus(AvailableLatestVersion, "", Settings.Startup.UpdateChannel);
if (isDownloadSuccessful) {
LogHelper.WriteLogToFile("AutoUpdate | Update downloaded successfully, will install when application closes");
@@ -463,7 +474,18 @@ namespace Ink_Canvas {
case HasNewUpdateWindow.UpdateResult.SkipVersion:
// 跳过该版本:记录到设置中
LogHelper.WriteLogToFile($"AutoUpdate | User chose to skip version {AvailableLatestVersion}");
// 可以在设置中添加"已跳过的版本"列表
// 记录要跳过的版本号
Settings.Startup.SkippedVersion = AvailableLatestVersion;
// 保存设置到文件
SaveSettingsToFile();
// 通知用户
MessageBox.Show($"已设置跳过版本 {AvailableLatestVersion},在下次发布新版本之前不会再提示更新。",
"已跳过此版本",
MessageBoxButton.OK,
MessageBoxImage.Information);
break;
}
} else {
+76 -3
View File
@@ -21,16 +21,33 @@ namespace Ink_Canvas {
private void ToggleSwitchIsAutoUpdate_Toggled(object sender, RoutedEventArgs e) {
if (!isLoaded) return;
Settings.Startup.IsAutoUpdate = ToggleSwitchIsAutoUpdate.IsOn;
ToggleSwitchIsAutoUpdateWithSilence.Visibility =
// 自动更新关闭时隐藏静默更新选项
ToggleSwitchIsAutoUpdateWithSilence.Visibility =
ToggleSwitchIsAutoUpdate.IsOn ? Visibility.Visible : Visibility.Collapsed;
// 如果关闭了自动更新,同时也关闭静默更新
if (!ToggleSwitchIsAutoUpdate.IsOn) {
Settings.Startup.IsAutoUpdateWithSilence = false;
ToggleSwitchIsAutoUpdateWithSilence.IsOn = false;
}
// 无论如何,静默更新时间区域的显示都要跟随静默更新设置
AutoUpdateTimePeriodBlock.Visibility =
(Settings.Startup.IsAutoUpdateWithSilence && Settings.Startup.IsAutoUpdate) ?
Visibility.Visible : Visibility.Collapsed;
SaveSettingsToFile();
}
private void ToggleSwitchIsAutoUpdateWithSilence_Toggled(object sender, RoutedEventArgs e) {
if (!isLoaded) return;
Settings.Startup.IsAutoUpdateWithSilence = ToggleSwitchIsAutoUpdateWithSilence.IsOn;
// 静默更新的时间设置区域只在静默更新开启时显示
AutoUpdateTimePeriodBlock.Visibility =
Settings.Startup.IsAutoUpdateWithSilence ? Visibility.Visible : Visibility.Collapsed;
SaveSettingsToFile();
}
@@ -1515,8 +1532,8 @@ namespace Ink_Canvas {
Settings.Startup.IsEnableNibMode = false;
Settings.Startup.IsAutoUpdate = true;
Settings.Startup.IsAutoUpdateWithSilence = true;
Settings.Startup.AutoUpdateWithSilenceStartTime = "18:20";
Settings.Startup.AutoUpdateWithSilenceEndTime = "07:40";
Settings.Startup.AutoUpdateWithSilenceStartTime = "06:00";
Settings.Startup.AutoUpdateWithSilenceEndTime = "22:00";
Settings.Startup.IsFoldAtStartup = false;
}
@@ -1781,5 +1798,61 @@ namespace Ink_Canvas {
Process.Start("https://github.com/WXRIW/Ink-Canvas");
HideSubPanels();
}
private void UpdateChannelSelector_Checked(object sender, RoutedEventArgs e) {
if (!isLoaded) return;
var radioButton = sender as System.Windows.Controls.RadioButton;
if (radioButton != null) {
string channel = radioButton.Tag.ToString();
Settings.Startup.UpdateChannel = channel == "Beta" ? UpdateChannel.Beta : UpdateChannel.Release;
LogHelper.WriteLogToFile($"Settings | Update channel changed to {Settings.Startup.UpdateChannel}");
SaveSettingsToFile();
}
}
private async void FixVersionButton_Click(object sender, RoutedEventArgs e) {
// 显示确认对话框
var confirm = MessageBox.Show(
"此操作将下载当前选择通道的最新版本并安装,软件将自动关闭并更新。\n\n确定要执行版本修复吗?",
"版本修复确认",
MessageBoxButton.YesNo,
MessageBoxImage.Question);
if (confirm == MessageBoxResult.Yes) {
// 禁用按钮,避免重复点击
FixVersionButton.IsEnabled = false;
FixVersionButton.Content = "正在修复...";
try {
// 执行版本修复
bool result = await AutoUpdateHelper.FixVersion(Settings.Startup.UpdateChannel);
if (!result) {
MessageBox.Show(
"版本修复失败,可能是网络问题或当前已是最新版本。",
"修复失败",
MessageBoxButton.OK,
MessageBoxImage.Error);
// 恢复按钮状态
FixVersionButton.IsEnabled = true;
FixVersionButton.Content = "版本修复";
}
// 成功则会自动关闭应用程序并安装
}
catch (Exception ex) {
LogHelper.WriteLogToFile($"Error in FixVersionButton_Click: {ex.Message}", LogHelper.LogType.Error);
MessageBox.Show(
$"版本修复过程中发生错误: {ex.Message}",
"修复错误",
MessageBoxButton.OK,
MessageBoxImage.Error);
// 恢复按钮状态
FixVersionButton.IsEnabled = true;
FixVersionButton.Content = "版本修复";
}
}
}
}
}
+16 -4
View File
@@ -70,18 +70,20 @@ namespace Ink_Canvas {
BoundsWidth = Settings.Advanced.FingerModeBoundsWidth;
}
// Always show update setting in UI based on stored preference
// 设置自动更新相关选项
ToggleSwitchIsAutoUpdate.IsOn = Settings.Startup.IsAutoUpdate;
// Always check for updates at startup, regardless of the setting
// 只有在启用了自动更新功能时才检查更新
if (Settings.Startup.IsAutoUpdate) {
if (isStartup) {
LogHelper.WriteLogToFile("AutoUpdate | Running auto-update check at startup");
AutoUpdate();
}
// Call auto-update when setting is changed (not at startup)
else if (Settings.Startup.IsAutoUpdate) {
// 当设置被修改时也检查更新(非启动时)
else {
LogHelper.WriteLogToFile("AutoUpdate | Running auto-update check after settings change");
AutoUpdate();
}
}
// ToggleSwitchIsAutoUpdateWithSilence.Visibility = Settings.Startup.IsAutoUpdate ? Visibility.Visible : Visibility.Collapsed;
@@ -89,6 +91,16 @@ namespace Ink_Canvas {
ToggleSwitchIsAutoUpdateWithSilence.IsOn = true;
}
// 初始化更新通道选择
foreach (var radioButton in UpdateChannelSelector.Items) {
if (radioButton is System.Windows.Controls.RadioButton rb) {
if (rb.Tag.ToString() == Settings.Startup.UpdateChannel.ToString()) {
rb.IsChecked = true;
break;
}
}
}
AutoUpdateTimePeriodBlock.Visibility = Settings.Startup.IsAutoUpdateWithSilence
? Visibility.Visible
: Visibility.Collapsed;
+8 -5
View File
@@ -9,6 +9,7 @@ using MessageBox = System.Windows.MessageBox;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Controls;
namespace Ink_Canvas {
public class TimeViewModel : INotifyPropertyChanged {
@@ -355,22 +356,24 @@ namespace Ink_Canvas {
return;
}
// 检查应用程序状态,确保可以安全更新
// 检查应用程序状态,确保可以安全更新
// 空闲状态的判定为不处于批注模式和画板模式
bool canSafelyUpdate = false;
Dispatcher.Invoke(() => {
try {
// 检查是否处于桌面模式(Topmost为true)且没有墨迹内容
if (Topmost && inkCanvas.Strokes.Count == 0) {
// 判断是否处于批注模式(inkCanvas.EditingMode == InkCanvasEditingMode.Ink
// 判断是否处于画板模式(!Topmost)
if (inkCanvas.EditingMode != InkCanvasEditingMode.Ink && Topmost) {
// 检查是否有未保存的内容或正在进行的操作
if (!isHidingSubPanelsWhenInking) {
canSafelyUpdate = true;
LogHelper.WriteLogToFile("AutoUpdate | Application is in a safe state for update");
LogHelper.WriteLogToFile("AutoUpdate | Application is in a safe state for update - not in ink or board mode");
} else {
LogHelper.WriteLogToFile("AutoUpdate | Application is currently performing operations");
}
} else {
LogHelper.WriteLogToFile("AutoUpdate | Application has unsaved content or is not in desktop mode");
LogHelper.WriteLogToFile("AutoUpdate | Application is in ink or board mode, cannot update now");
}
}
catch (Exception ex) {
+14 -2
View File
@@ -95,6 +95,13 @@ namespace Ink_Canvas
public bool IsEnableTwoFingerRotationOnSelection { get; set; } = false;
}
// 更新通道枚举
public enum UpdateChannel
{
Release,
Beta
}
public class Startup
{
[JsonProperty("isAutoUpdate")]
@@ -102,9 +109,14 @@ namespace Ink_Canvas
[JsonProperty("isAutoUpdateWithSilence")]
public bool IsAutoUpdateWithSilence { get; set; } = false;
[JsonProperty("isAutoUpdateWithSilenceStartTime")]
public string AutoUpdateWithSilenceStartTime { get; set; } = "00:00";
public string AutoUpdateWithSilenceStartTime { get; set; } = "06:00";
[JsonProperty("isAutoUpdateWithSilenceEndTime")]
public string AutoUpdateWithSilenceEndTime { get; set; } = "00:00";
public string AutoUpdateWithSilenceEndTime { get; set; } = "22:00";
[JsonProperty("updateChannel")]
public UpdateChannel UpdateChannel { get; set; } = UpdateChannel.Release;
[JsonProperty("skippedVersion")]
public string SkippedVersion { get; set; } = "";
[JsonProperty("isEnableNibMode")]
public bool IsEnableNibMode { get; set; } = false;
+2 -23
View File
@@ -10,7 +10,7 @@
ui:WindowHelper.UseModernWindowStyle = "True"
ui:WindowHelper.SystemBackdropType="Mica"
ui:TitleBar.Height="36"
Title="InkCanvasForClass CE有新版本可用" Height="680" Width="850" ResizeMode="NoResize"
Title="InkCanvasForClass CE有新版本可用" Height="600" Width="850" ResizeMode="NoResize"
WindowStartupLocation="CenterScreen">
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
<Grid Background="#fafafa" Margin="0,0,0,30">
@@ -50,28 +50,7 @@
<TextBlock x:Name="updateDateInfo" Text="2024年8月4日发布更新" FontSize="15" TextAlignment="Center"/>
</ui:SimpleStackPanel>
<!-- 自动更新选项 -->
<Border Background="White" BorderBrush="#e2e8f0" BorderThickness="1" CornerRadius="4" Margin="24,16,24,0">
<ui:SimpleStackPanel Orientation="Vertical" HorizontalAlignment="Center" Margin="0,16,0,16" Spacing="10">
<TextBlock Text="更新设置" FontWeight="Bold" FontSize="16" HorizontalAlignment="Center" Margin="0,0,0,6"/>
<!-- 水平排列两个ToggleSwitch -->
<ui:SimpleStackPanel Orientation="Horizontal" HorizontalAlignment="Center" Spacing="20">
<ui:SimpleStackPanel Orientation="Vertical" Spacing="4">
<ui:ToggleSwitch x:Name="EnableAutoUpdateToggle" Header="启用自动更新" OnContent="开启" OffContent="关闭" IsOn="True" Width="170" Foreground="Black"/>
</ui:SimpleStackPanel>
<ui:SimpleStackPanel Orientation="Vertical" Spacing="4">
<ui:ToggleSwitch x:Name="EnableSilentUpdateToggle" Header="启用静默更新" OnContent="开启" OffContent="关闭" Width="170" Foreground="Black"/>
</ui:SimpleStackPanel>
</ui:SimpleStackPanel>
<TextBlock Text="静默更新将在软件不使用时自动安装,无需手动操作" FontSize="13" Foreground="#71717a" HorizontalAlignment="Center"
Margin="4,0,0,0" Width="360" TextWrapping="Wrap"/>
</ui:SimpleStackPanel>
</Border>
<!-- 更新按钮组 -->
<!-- 更新按钮组 -->
<Border Background="#f1f5f9" BorderBrush="#e2e8f0" BorderThickness="1" CornerRadius="4" Margin="24,16,24,20">
<ui:SimpleStackPanel Orientation="Vertical" HorizontalAlignment="Center" Spacing="14" Margin="0,16,0,16">
<TextBlock Text="请选择更新方式" FontWeight="Bold" FontSize="16" HorizontalAlignment="Center" Margin="0,0,0,6"/>
+1 -24
View File
@@ -72,10 +72,6 @@ namespace Ink_Canvas
public UpdateResult Result { get; private set; } = UpdateResult.UpdateLater;
// 更新设置
public bool IsAutoUpdateEnabled => EnableAutoUpdateToggle.IsOn;
public bool IsSilentUpdateEnabled => EnableSilentUpdateToggle.IsOn;
public HasNewUpdateWindow(string currentVersion, string newVersion, string releaseDate, string releaseNotes = null)
{
InitializeComponent();
@@ -96,9 +92,7 @@ namespace Ink_Canvas
markdownContent.Markdown = ReleaseNotes;
}
// 初始化自动更新设置
EnableAutoUpdateToggle.IsOn = MainWindow.Settings.Startup.IsAutoUpdate;
EnableSilentUpdateToggle.IsOn = MainWindow.Settings.Startup.IsAutoUpdateWithSilence;
// 自动更新和静默更新设置已移至设置界面,此处不再需要
// 确保按钮可见且可用
EnsureButtonsVisibility();
@@ -148,9 +142,6 @@ namespace Ink_Canvas
{
LogHelper.WriteLogToFile("AutoUpdate | Update Now button clicked");
// 保存自动更新设置
SaveUpdateSettings();
// 设置结果为立即更新
Result = UpdateResult.UpdateNow;
@@ -163,9 +154,6 @@ namespace Ink_Canvas
{
LogHelper.WriteLogToFile("AutoUpdate | Update Later button clicked");
// 保存自动更新设置
SaveUpdateSettings();
// 设置结果为稍后更新
Result = UpdateResult.UpdateLater;
@@ -178,9 +166,6 @@ namespace Ink_Canvas
{
LogHelper.WriteLogToFile("AutoUpdate | Skip Version button clicked");
// 保存自动更新设置
SaveUpdateSettings();
// 设置结果为跳过该版本
Result = UpdateResult.SkipVersion;
@@ -189,15 +174,7 @@ namespace Ink_Canvas
Close();
}
private void SaveUpdateSettings()
{
// 保存自动更新设置
MainWindow.Settings.Startup.IsAutoUpdate = EnableAutoUpdateToggle.IsOn;
MainWindow.Settings.Startup.IsAutoUpdateWithSilence = EnableSilentUpdateToggle.IsOn;
// 记录到日志
LogHelper.WriteLogToFile($"AutoUpdate | User settings changed: AutoUpdate={EnableAutoUpdateToggle.IsOn}, SilentUpdate={EnableSilentUpdateToggle.IsOn}");
}
// 根据屏幕分辨率调整窗口大小
private void AdjustWindowSizeForScreenResolution()