Skip to content

Commit

Permalink
Fix bug with changing focus and apply changes from microsoft#157, Closes
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelwgn committed Sep 10, 2019
1 parent f0d4907 commit c98fb90
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
6 changes: 5 additions & 1 deletion XamlControlsGallery/ControlPages/SplitButtonPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@
</muxcontrols:SplitButton.Flyout>
</muxcontrols:SplitButton>

<RichEditBox x:Name="myRichEditBox" Grid.Column="1" MinWidth="240" MinHeight="96" PlaceholderText="Type something here" TextChanged="MyRichEditBox_TextChanged"/>
<RichEditBox x:Name="myRichEditBox" Grid.Column="1" MinWidth="240" MinHeight="96"
PlaceholderText="Type something here"
GotFocus="MyRichEditBox_GotFocus"
LosingFocus="MyRichEditBox_LosingFocus"
TextChanging="MyRichEditBox_TextChanging"/>
</Grid>
</local:ControlExample>
</StackPanel>
Expand Down
52 changes: 51 additions & 1 deletion XamlControlsGallery/ControlPages/SplitButtonPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Windows.UI;
using Windows.UI.Text;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
Expand All @@ -9,6 +10,11 @@ public sealed partial class SplitButtonPage : Page
{
private Color currentColor = Colors.Black;

// 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
// (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.";
public SplitButtonPage()
{
this.InitializeComponent();
Expand Down Expand Up @@ -41,9 +47,53 @@ private void myColorButton_Click(Microsoft.UI.Xaml.Controls.SplitButton sender,
currentColor = color;
}

private void MyRichEditBox_TextChanged(object sender, RoutedEventArgs e)
private void MyRichEditBox_TextChanging(object sender, RichEditBoxTextChangingEventArgs e)
{
// Hitting control+b and similiar commands my overwrite the color,
// which result to black text on black background when losing focus on dark theme.
// Solution: check if text actually changed
if (e.IsContentChanging)
{
myRichEditBox.Document.Selection.CharacterFormat.ForegroundColor = currentColor;
}
}


private void MyRichEditBox_GotFocus(object sender, RoutedEventArgs e)
{
// reset colors to correct defaults for Focused state
ITextRange documentRange = myRichEditBox.Document.GetRange(0, TextConstants.MaxUnitCount);
SolidColorBrush background = (SolidColorBrush)App.Current.Resources["TextControlBackgroundFocused"];
SolidColorBrush foreground = (SolidColorBrush)App.Current.Resources["TextControlForegroundFocused"];

myRichEditBox.Document.ApplyDisplayUpdates();

if (background != null && foreground != null)
{
documentRange.CharacterFormat.BackgroundColor = background.Color;
}
// saving selection span
var caretPosition = myRichEditBox.Document.Selection.GetIndex(TextRangeUnit.Character) - 1;
if (caretPosition <= 0)
{
// User has not entered text, prevent invalid values and just set index to 1
caretPosition = 1;
}
var selectionLength = myRichEditBox.Document.Selection.Length;
// restoring text styling, unintentionally sets caret position at beginning of text
myRichEditBox.Document.SetText(TextSetOptions.FormatRtf, LastFormattedText);
// restoring selection position
myRichEditBox.Document.Selection.SetIndex(TextRangeUnit.Character, caretPosition, false);
myRichEditBox.Document.Selection.SetRange(caretPosition, caretPosition + selectionLength);
// Editor might have gained focus because user changed color.
// Change selection color
// Note that only way to regain with selection containing text is using the change color button
myRichEditBox.Document.Selection.CharacterFormat.ForegroundColor = currentColor;
}

private void MyRichEditBox_LosingFocus(object sender, RoutedEventArgs e)
{
myRichEditBox.Document.GetText(TextGetOptions.FormatRtf, out LastFormattedText);
}
}
}

0 comments on commit c98fb90

Please sign in to comment.