diff --git a/App.xaml.cs b/App.xaml.cs index 1f49957..619ecb8 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -122,7 +122,10 @@ public static void Quit(bool savesetting) foreach (var win in MainWindows) { if (win == null) continue; - win.SaveToXamlPkg(); + var exMsg = win.SaveToXamlPkg(); + if (exMsg != null && + MessageBox.Show(exMsg + "\r\n" + (string)App.Current.Resources["msgbox_not_saved_confirm"], "", MessageBoxButton.YesNo, MessageBoxImage.Exclamation) != MessageBoxResult.Yes) + return; if (savesetting) { win.CurrentSetting.Win_Pos = new Point(win.Left, win.Top); diff --git a/MainWindow.xaml b/MainWindow.xaml index f03b930..3670dda 100644 --- a/MainWindow.xaml +++ b/MainWindow.xaml @@ -26,7 +26,8 @@ + GotKeyboardFocus="RTB_Main_GotKeyboardFocus" LostKeyboardFocus="RTB_Main_LostKeyboardFocus" + ContextMenuOpening="RTB_Main_ContextMenuOpening" PreviewMouseDown="RTB_Main_PreviewMouseDown"> diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index bd0c8f8..50c2524 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -270,6 +270,11 @@ private void RTB_Main_LostKeyboardFocus(object sender, KeyboardFocusChangedEvent DockToSide(); } + private void RTB_Main_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) + { + App.FormatWindow.UpdateTargets(this); + } + private void RTB_Main_ContextMenuOpening(object sender, ContextMenuEventArgs e) { e.Handled = true; @@ -338,60 +343,43 @@ private void SaveNotes() } } - internal void SaveToXamlPkg() + private string TextRangeSave() { - lock (Lock_Save) + string statusText, exMsg = null; + try { - TextRange tr = null; - bool isUIthread = Dispatcher.CheckAccess(); - string result; - if (isUIthread) - tr = new TextRange(RTB_Main.Document.ContentStart, RTB_Main.Document.ContentEnd); - else - Dispatcher.Invoke(delegate { tr = new TextRange(RTB_Main.Document.ContentStart, RTB_Main.Document.ContentEnd); }); - - try - { - if (isUIthread) - { - using (var ms = new FileStream(CurrentSetting.Doc_Location, FileMode.Create)) - { - tr.Save(ms, DataFormats.XamlPackage, true); - } - File.WriteAllText(CurrentSetting.Bak_Location, tr.Text); - } - else - { - Dispatcher.Invoke(delegate - { - using (var ms = new FileStream(CurrentSetting.Doc_Location, FileMode.Create)) - { - tr.Save(ms, DataFormats.XamlPackage, true); - }; - File.WriteAllText(CurrentSetting.Bak_Location, tr.Text); - }); - } - result = (string)Application.Current.Resources["status_saved"]; - } - catch + var tr = new TextRange(RTB_Main.Document.ContentStart, RTB_Main.Document.ContentEnd); + using (var ms = new FileStream(CurrentSetting.Doc_Location, FileMode.Create)) { - result = (string)Application.Current.Resources["status_save_failed"]; + tr.Save(ms, DataFormats.XamlPackage, true); } + File.WriteAllText(CurrentSetting.Bak_Location, tr.Text); + statusText = (string)Application.Current.Resources["status_saved"]; + } + catch (Exception ex) + { + statusText = (string)Application.Current.Resources["status_save_failed"]; + exMsg = ex.ToString(); + } + + TB_Status.Text = statusText; + TB_Status.Visibility = Visibility.Visible; + return exMsg; + } + /// Non-null string if there are errors when saving. + internal string SaveToXamlPkg() + { + string exMsg = null; + lock (Lock_Save) + { + bool isUIthread = Dispatcher.CheckAccess(); if (isUIthread) - { - TB_Status.Text = result; - TB_Status.Visibility = Visibility.Visible; - } + exMsg = TextRangeSave(); else - { - Dispatcher.Invoke(delegate - { - TB_Status.Text = result; - TB_Status.Visibility = Visibility.Visible; - }); - } + exMsg = Dispatcher.Invoke(() => TextRangeSave()); } + return exMsg; } private void Win_Main_Loaded(object sender, RoutedEventArgs e) @@ -583,8 +571,8 @@ private void TrayIcon_TrayMouseDoubleClick(object sender, RoutedEventArgs e) win.UnDock(); } } - #endregion + #endregion } } diff --git a/Resources/StringResources.en.xaml b/Resources/StringResources.en.xaml index fd64b72..35ce4a6 100644 --- a/Resources/StringResources.en.xaml +++ b/Resources/StringResources.en.xaml @@ -59,4 +59,5 @@ Program will exit now. Text content path New Note Do you also want to delete the note content and backup files? + Current note content is not saved. Changes will be lost after exiting. Exit anyway? \ No newline at end of file diff --git a/Resources/StringResources.zh.xaml b/Resources/StringResources.zh.xaml index d6dd852..99df3d9 100644 --- a/Resources/StringResources.zh.xaml +++ b/Resources/StringResources.zh.xaml @@ -59,4 +59,5 @@ 文本保存路径 新笔记 是否需要一起删除笔记内容和备份文件? + 当前笔记内容并未保存,退出会丢失更改的内容。确定要退出吗? \ No newline at end of file diff --git a/bin/Release/DesktopNote.exe b/bin/Release/DesktopNote.exe index 620c401..ec72550 100644 Binary files a/bin/Release/DesktopNote.exe and b/bin/Release/DesktopNote.exe differ