diff --git a/Ink Canvas/MainWindow.xaml b/Ink Canvas/MainWindow.xaml index 93032d3e..d910da66 100644 --- a/Ink Canvas/MainWindow.xaml +++ b/Ink Canvas/MainWindow.xaml @@ -3083,6 +3083,30 @@ Toggled="ToggleSwitchAutoSaveStrokesAtScreenshot_Toggled" /> + + + + + + + + + + + + + + + + + + diff --git a/Ink Canvas/MainWindow.xaml.cs b/Ink Canvas/MainWindow.xaml.cs index d0f2ceaf..5e9b8d14 100644 --- a/Ink Canvas/MainWindow.xaml.cs +++ b/Ink Canvas/MainWindow.xaml.cs @@ -1853,6 +1853,7 @@ namespace Ink_Canvas // 添加定时器来维护置顶状态 private DispatcherTimer topmostMaintenanceTimer; + private DispatcherTimer autoSaveStrokesTimer; private bool isTopmostMaintenanceEnabled; private void ApplyNoFocusMode() diff --git a/Ink Canvas/MainWindow_cs/MW_Settings.cs b/Ink Canvas/MainWindow_cs/MW_Settings.cs index 4624ac44..9f7b9907 100644 --- a/Ink Canvas/MainWindow_cs/MW_Settings.cs +++ b/Ink Canvas/MainWindow_cs/MW_Settings.cs @@ -1877,6 +1877,29 @@ namespace Ink_Canvas SaveSettingsToFile(); } + private void ToggleSwitchEnableAutoSaveStrokes_Toggled(object sender, RoutedEventArgs e) + { + if (!isLoaded) return; + Settings.Automation.IsEnableAutoSaveStrokes = ToggleSwitchEnableAutoSaveStrokes.IsOn; + SaveSettingsToFile(); + // 更新定时器状态 + UpdateAutoSaveStrokesTimer(); + } + + private void ComboBoxAutoSaveStrokesInterval_SelectionChanged(object sender, SelectionChangedEventArgs e) + { + if (!isLoaded || ComboBoxAutoSaveStrokesInterval.SelectedItem == null) return; + + var selectedItem = ComboBoxAutoSaveStrokesInterval.SelectedItem as System.Windows.Controls.ComboBoxItem; + if (selectedItem?.Tag != null && int.TryParse(selectedItem.Tag.ToString(), out int intervalMinutes)) + { + Settings.Automation.AutoSaveStrokesIntervalMinutes = intervalMinutes; + SaveSettingsToFile(); + // 更新定时器间隔 + UpdateAutoSaveStrokesTimer(); + } + } + #endregion #region Gesture diff --git a/Ink Canvas/MainWindow_cs/MW_SettingsToLoad.cs b/Ink Canvas/MainWindow_cs/MW_SettingsToLoad.cs index 85e8ec86..0e51b58f 100644 --- a/Ink Canvas/MainWindow_cs/MW_SettingsToLoad.cs +++ b/Ink Canvas/MainWindow_cs/MW_SettingsToLoad.cs @@ -1053,6 +1053,23 @@ namespace Ink_Canvas ToggleSwitchSaveFullPageStrokes.IsOn = Settings.Automation.IsSaveFullPageStrokes; + // 加载定时保存墨迹设置 + ToggleSwitchEnableAutoSaveStrokes.IsOn = Settings.Automation.IsEnableAutoSaveStrokes; + // 初始化保存间隔下拉框 + if (ComboBoxAutoSaveStrokesInterval != null) + { + int intervalMinutes = Settings.Automation.AutoSaveStrokesIntervalMinutes; + if (intervalMinutes < 1) intervalMinutes = 5; // 默认5分钟 + foreach (System.Windows.Controls.ComboBoxItem item in ComboBoxAutoSaveStrokesInterval.Items) + { + if (item.Tag != null && int.TryParse(item.Tag.ToString(), out int tagValue) && tagValue == intervalMinutes) + { + ComboBoxAutoSaveStrokesInterval.SelectedItem = item; + break; + } + } + } + SideControlMinimumAutomationSlider.Value = Settings.Automation.MinimumAutomationStrokeNumber; AutoSavedStrokesLocation.Text = Settings.Automation.AutoSavedStrokesLocation; diff --git a/Ink Canvas/MainWindow_cs/MW_Timer.cs b/Ink Canvas/MainWindow_cs/MW_Timer.cs index afb21c29..3de74f33 100644 --- a/Ink Canvas/MainWindow_cs/MW_Timer.cs +++ b/Ink Canvas/MainWindow_cs/MW_Timer.cs @@ -12,6 +12,7 @@ using System.Threading.Tasks; using System.Timers; using System.Windows; using System.Windows.Controls; +using System.Windows.Threading; namespace Ink_Canvas { @@ -148,6 +149,55 @@ namespace Ink_Canvas LogHelper.WriteLogToFile($"程序启动时NTP同步失败: {ex.Message}", LogHelper.LogType.Error); } }); + + // 初始化定时保存墨迹定时器 + InitAutoSaveStrokesTimer(); + } + + // 初始化定时保存墨迹定时器 + private void InitAutoSaveStrokesTimer() + { + if (autoSaveStrokesTimer == null) + { + autoSaveStrokesTimer = new DispatcherTimer(); + autoSaveStrokesTimer.Tick += AutoSaveStrokesTimer_Tick; + } + + // 根据设置更新定时器间隔和启动状态 + UpdateAutoSaveStrokesTimer(); + } + + // 更新定时保存墨迹定时器状态 + private void UpdateAutoSaveStrokesTimer() + { + if (autoSaveStrokesTimer == null) return; + + autoSaveStrokesTimer.Stop(); + + if (Settings.Automation.IsEnableAutoSaveStrokes) + { + int intervalMinutes = Settings.Automation.AutoSaveStrokesIntervalMinutes; + if (intervalMinutes < 1) intervalMinutes = 1; // 最小间隔1分钟 + autoSaveStrokesTimer.Interval = TimeSpan.FromMinutes(intervalMinutes); + autoSaveStrokesTimer.Start(); + } + } + + // 定时保存墨迹定时器事件处理 + private void AutoSaveStrokesTimer_Tick(object sender, EventArgs e) + { + try + { + // 只有在画布可见且有墨迹时才保存 + if (inkCanvas.Visibility == Visibility.Visible && inkCanvas.Strokes.Count > 0) + { + // 静默保存 + SaveInkCanvasStrokes(false, false); + } + } + catch (Exception) + { + } } // NTP同步定时器事件处理 diff --git a/Ink Canvas/Resources/Settings.cs b/Ink Canvas/Resources/Settings.cs index 395ea46d..ed819bd1 100644 --- a/Ink Canvas/Resources/Settings.cs +++ b/Ink Canvas/Resources/Settings.cs @@ -462,6 +462,12 @@ namespace Ink_Canvas [JsonProperty("isAutoEnterAnnotationAfterKillHite")] public bool IsAutoEnterAnnotationAfterKillHite { get; set; } + [JsonProperty("isEnableAutoSaveStrokes")] + public bool IsEnableAutoSaveStrokes { get; set; } = true; + + [JsonProperty("autoSaveStrokesIntervalMinutes")] + public int AutoSaveStrokesIntervalMinutes { get; set; } = 5; + [JsonProperty("floatingWindowInterceptor")] public FloatingWindowInterceptorSettings FloatingWindowInterceptor { get; set; } = new FloatingWindowInterceptorSettings(); }