Skip to content

Commit

Permalink
update target window on keyboard focus;
Browse files Browse the repository at this point in the history
add warning when closing without successful save;
  • Loading branch information
Carl Chang committed May 13, 2019
1 parent 130c0ab commit 3d7d7d9
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 49 deletions.
5 changes: 4 additions & 1 deletion App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
</Rectangle>
<RichTextBox x:Name="RTB_Main" Margin="20" Background="{x:Null}" BorderBrush="{x:Null}" AcceptsTab="True" BorderThickness="0" IsInactiveSelectionHighlightEnabled="True"
TextChanged="RTB_Main_TextChanged" PreviewKeyDown="RTB_Main_PreviewKeyDown" PreviewMouseWheel="RTB_Main_PreviewMouseWheel"
LostKeyboardFocus="RTB_Main_LostKeyboardFocus" ContextMenuOpening="RTB_Main_ContextMenuOpening" PreviewMouseDown="RTB_Main_PreviewMouseDown">
GotKeyboardFocus="RTB_Main_GotKeyboardFocus" LostKeyboardFocus="RTB_Main_LostKeyboardFocus"
ContextMenuOpening="RTB_Main_ContextMenuOpening" PreviewMouseDown="RTB_Main_PreviewMouseDown">
<!--<wtk:RichTextBoxFormatBarManager.FormatBar>
<wtk:RichTextBoxFormatBar/>
</wtk:RichTextBoxFormatBarManager.FormatBar>-->
Expand Down
82 changes: 35 additions & 47 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/// <returns>Non-null string if there are errors when saving.</returns>
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)
Expand Down Expand Up @@ -583,8 +571,8 @@ private void TrayIcon_TrayMouseDoubleClick(object sender, RoutedEventArgs e)
win.UnDock();
}
}
#endregion

#endregion

}
}
1 change: 1 addition & 0 deletions Resources/StringResources.en.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ Program will exit now.</sys:String>
<sys:String x:Key="label_content_path_txt">Text content path</sys:String>
<sys:String x:Key="menu_newnote">New Note</sys:String>
<sys:String x:Key="msgbox_delete_confirm">Do you also want to delete the note content and backup files?</sys:String>
<sys:String x:Key="msgbox_not_saved_confirm">Current note content is not saved. Changes will be lost after exiting. Exit anyway?</sys:String>
</ResourceDictionary>
1 change: 1 addition & 0 deletions Resources/StringResources.zh.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@
<sys:String x:Key="label_content_path_txt">文本保存路径</sys:String>
<sys:String x:Key="menu_newnote">新笔记</sys:String>
<sys:String x:Key="msgbox_delete_confirm">是否需要一起删除笔记内容和备份文件?</sys:String>
<sys:String x:Key="msgbox_not_saved_confirm">当前笔记内容并未保存,退出会丢失更改的内容。确定要退出吗?</sys:String>
</ResourceDictionary>
Binary file modified bin/Release/DesktopNote.exe
Binary file not shown.

0 comments on commit 3d7d7d9

Please sign in to comment.