Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug where losing focus removes color; Closes #144; #157

Merged
merged 7 commits into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions XamlControlsGallery/ControlPages/RichEditBoxPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,9 @@
RelativePanel.Below="openFileButton"
RelativePanel.AlignLeftWithPanel="True"
RelativePanel.AlignRightWithPanel="True"
TextChanged="Editor_TextChanged"
GotFocus="Editor_GotFocus"/>
TextChanging="Editor_TextChanging"
GotFocus="Editor_GotFocus"
LosingFocus="Editor_LosingFocus"/>
<StackPanel Orientation="Horizontal"
RelativePanel.Below="editor"
RelativePanel.AlignLeftWith="editor"
Expand Down
38 changes: 34 additions & 4 deletions XamlControlsGallery/ControlPages/RichEditBoxPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//*********************************************************
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
Expand All @@ -25,7 +25,9 @@ namespace AppUIBasics.ControlPages
public sealed partial class RichEditBoxPage : 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
private string LastFormattedText = "";
public RichEditBoxPage()
{
this.InitializeComponent();
Expand Down Expand Up @@ -160,10 +162,34 @@ private void Editor_GotFocus(object sender, RoutedEventArgs e)
SolidColorBrush background = (SolidColorBrush)App.Current.Resources["TextControlBackgroundFocused"];
SolidColorBrush foreground = (SolidColorBrush)App.Current.Resources["TextControlForegroundFocused"];

editor.Document.ApplyDisplayUpdates();

if (background != null && foreground != null)
{
documentRange.CharacterFormat.BackgroundColor = background.Color;
}
// saving selection span
var caretPosition = editor.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 = editor.Document.Selection.Length;
// restoring text styling, unintentionally sets caret position at beginning of text
editor.Document.SetText(TextSetOptions.FormatRtf, LastFormattedText);
// restoring selection position
editor.Document.Selection.SetIndex(TextRangeUnit.Character, caretPosition, false);
editor.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
editor.Document.Selection.CharacterFormat.ForegroundColor = currentColor;
}

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

private void Page_SizeChanged(object sender, SizeChangedEventArgs e)
Expand Down Expand Up @@ -201,9 +227,13 @@ private void REBCustom_Unloaded(object sender, RoutedEventArgs e)
}
}

private void Editor_TextChanged(object sender, RoutedEventArgs e)
private void Editor_TextChanging(object sender, RichEditBoxTextChangingEventArgs e)
{
editor.Document.Selection.CharacterFormat.ForegroundColor = currentColor;
// Fix bug where selected text would get colored when editor loses focus
if (FocusManager.GetFocusedElement() == editor)
{
editor.Document.Selection.CharacterFormat.ForegroundColor = currentColor;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

private Color currentColor = Colors.Black;
private string LastFormattedText = "";

private async void OpenButton_Click(object sender, RoutedEventArgs e)
{
// Open a text file.
Expand Down Expand Up @@ -78,6 +80,7 @@ private void ColorButton_Click(object sender, RoutedEventArgs e)

fontColorButton.Flyout.Hide();
editor.Focus(Windows.UI.Xaml.FocusState.Keyboard);
currentColor = color;
}

private void FindBoxHighlightMatches()
Expand Down Expand Up @@ -116,9 +119,40 @@ private void Editor_GotFocus(object sender, RoutedEventArgs e)
SolidColorBrush background = (SolidColorBrush)App.Current.Resources["TextControlBackgroundFocused"];
SolidColorBrush foreground = (SolidColorBrush)App.Current.Resources["TextControlForegroundFocused"];

editor.Document.ApplyDisplayUpdates();

if (background != null && foreground != null)
{
documentRange.CharacterFormat.BackgroundColor = background.Color;
documentRange.CharacterFormat.ForegroundColor = foreground.Color;
}
// saving selection span
var caretPosition = editor.Document.Selection.GetIndex(TextRangeUnit.Character) - 1;
if(caretPosition <= 0)
{
caretPosition = 1;
}
var selectionLength = editor.Document.Selection.Length;
// restoring text styling, unintentionally sets caret position at beginning of text
editor.Document.SetText(TextSetOptions.FormatRtf, LastFormattedText);
// restoring selection position
editor.Document.Selection.SetIndex(TextRangeUnit.Character, caretPosition, false);
editor.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
editor.Document.Selection.CharacterFormat.ForegroundColor = currentColor;
}

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

private void Editor_TextChanging(object sender, RichEditBoxTextChangingEventArgs e)
{
// Fix bug where selected text would get colored when editor loses focus
if (FocusManager.GetFocusedElement() == editor)
{
editor.Document.Selection.CharacterFormat.ForegroundColor = currentColor;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@
RelativePanel.Below="openFileButton"
RelativePanel.AlignLeftWithPanel="True"
RelativePanel.AlignRightWithPanel="True"
GotFocus="Editor_GotFocus" />
TextChanging="Editor_TextChanging"
GotFocus="Editor_GotFocus"
LosingFocus="Editor_LosingFocus"/>
<StackPanel Orientation="Horizontal"
RelativePanel.Below="editor"
RelativePanel.AlignLeftWith="editor"
Expand Down