Skip to content

Commit

Permalink
Fix bug where cut and paste would not correctly work on SplitButtonPa…
Browse files Browse the repository at this point in the history
…ge and RichEditBoxPage (#294)
  • Loading branch information
marcelwgn authored and stmoy committed Dec 12, 2019
1 parent 2222fff commit a31f67a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
13 changes: 13 additions & 0 deletions XamlControlsGallery/ControlPages/RichEditBoxPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public sealed partial class RichEditBoxPage : Page
// String used to restore the colors when the focus gets reenabled
// See #144 for more info https://github.com/microsoft/Xaml-Controls-Gallery/issues/144
private string LastFormattedText = "";
private int LastRawTextLength = 0;

public RichEditBoxPage()
{
this.InitializeComponent();
Expand Down Expand Up @@ -157,6 +159,13 @@ private void FindBoxRemoveHighlights()

private void Editor_GotFocus(object sender, RoutedEventArgs e)
{
editor.Document.GetText(TextGetOptions.UseCrlf, out string currentRawText);
if (currentRawText.Length != LastRawTextLength)
{
// User used cut or paste from action command, skip the event
return;
}

// reset colors to correct defaults for Focused state
ITextRange documentRange = editor.Document.GetRange(0, TextConstants.MaxUnitCount);
SolidColorBrush background = (SolidColorBrush)App.Current.Resources["TextControlBackgroundFocused"];
Expand Down Expand Up @@ -189,6 +198,10 @@ private void Editor_GotFocus(object sender, RoutedEventArgs e)

private void Editor_LosingFocus(object sender, RoutedEventArgs e)
{
// Save text length to determine text length change
editor.Document.GetText(TextGetOptions.UseCrlf, out string lastRawText);
LastRawTextLength = lastRawText.Length;

editor.Document.GetText(TextGetOptions.FormatRtf, out LastFormattedText);
}

Expand Down
15 changes: 14 additions & 1 deletion XamlControlsGallery/ControlPages/SplitButtonPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public sealed partial class SplitButtonPage : Page
// (which also applies to this RichEditBox)
private string LastFormattedText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, " +
"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Tempor commodo ullamcorper a lacus.";
private int LastRawTextLength = 0;

public SplitButtonPage()
{
this.InitializeComponent();
Expand All @@ -38,7 +40,7 @@ private void ColorButton_Click(object sender, RoutedEventArgs e)
currentColor = color;
}

private void RevealColorButton_Click(object sender,RoutedEventArgs e)
private void RevealColorButton_Click(object sender, RoutedEventArgs e)
{
myColorButtonReveal.Flyout.Hide();
}
Expand All @@ -65,6 +67,12 @@ private void MyRichEditBox_TextChanging(object sender, RichEditBoxTextChangingEv

private void MyRichEditBox_GotFocus(object sender, RoutedEventArgs e)
{
myRichEditBox.Document.GetText(TextGetOptions.UseCrlf, out string currentRawText);
if (currentRawText.Length != LastRawTextLength)
{
// User used cut or paste from action command, skip the event
return;
}
// reset colors to correct defaults for Focused state
ITextRange documentRange = myRichEditBox.Document.GetRange(0, TextConstants.MaxUnitCount);
SolidColorBrush background = (SolidColorBrush)App.Current.Resources["TextControlBackgroundFocused"];
Expand Down Expand Up @@ -97,6 +105,11 @@ private void MyRichEditBox_GotFocus(object sender, RoutedEventArgs e)

private void MyRichEditBox_LosingFocus(object sender, RoutedEventArgs e)
{
// Save text length to determine text length change
myRichEditBox.Document.GetText(TextGetOptions.UseCrlf, out string lastRawText);
LastRawTextLength = lastRawText.Length;

// Save formatted to restore formatting upon regaining focus
myRichEditBox.Document.GetText(TextGetOptions.FormatRtf, out LastFormattedText);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
private Color currentColor = Colors.Black;
private string LastFormattedText = "";
private int LastRawTextLength = 0;

private async void OpenButton_Click(object sender, RoutedEventArgs e)
{
Expand Down Expand Up @@ -114,6 +115,12 @@ private void FindBoxRemoveHighlights()

private void Editor_GotFocus(object sender, RoutedEventArgs e)
{
editor.Document.GetText(TextGetOptions.UseCrlf, out string currentRawText);
if (currentRawText.Length != LastRawTextLength)
{
// User used cut or paste from action command, skip the event
return;
}
// reset colors to correct defaults for Focused state
ITextRange documentRange = editor.Document.GetRange(0, TextConstants.MaxUnitCount);
SolidColorBrush background = (SolidColorBrush)App.Current.Resources["TextControlBackgroundFocused"];
Expand Down Expand Up @@ -145,6 +152,11 @@ private void Editor_GotFocus(object sender, RoutedEventArgs e)

private void Editor_LosingFocus(object sender, RoutedEventArgs e)
{
// Save text length to determine text length change
editor.Document.GetText(TextGetOptions.UseCrlf, out string lastRawText);
LastRawTextLength = lastRawText.Length;

// Save formatted to restore formatting upon regaining focus
editor.Document.GetText(TextGetOptions.FormatRtf, out LastFormattedText);
}

Expand Down

0 comments on commit a31f67a

Please sign in to comment.