Skip to content

Commit

Permalink
Update samples that use the RichEditBox (#524)
Browse files Browse the repository at this point in the history
* Revert PR #157

* Fix samples

* Switch default to green
  • Loading branch information
marcelwgn authored and bpulliam committed Jan 26, 2022
1 parent fc09e50 commit 88f7f9c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 61 deletions.
2 changes: 1 addition & 1 deletion XamlControlsGallery/ControlPages/RichEditBoxPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
RelativePanel.AlignLeftWithPanel="True"
RelativePanel.AlignRightWithPanel="True"
TextChanged="Editor_TextChanged"
GotFocus="Editor_GotFocus" />
GotFocus="Editor_GotFocus"/>
<StackPanel Orientation="Horizontal"
RelativePanel.Below="editor"
RelativePanel.AlignLeftWith="editor"
Expand Down
2 changes: 2 additions & 0 deletions XamlControlsGallery/ControlPages/RichEditBoxPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ private void FindBoxRemoveHighlights()

private void Editor_GotFocus(object sender, RoutedEventArgs e)
{
editor.Document.GetText(TextGetOptions.UseCrlf, out string currentRawText);

// 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
58 changes: 8 additions & 50 deletions XamlControlsGallery/ControlPages/SplitButtonPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
using Microsoft.UI;
using Microsoft.UI.Text;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using System.Threading.Tasks;

namespace AppUIBasics.ControlPages
{
public sealed partial class SplitButtonPage : Page
{
private Windows.UI.Color currentColor = Colors.Black;
private Windows.UI.Color currentColor = Colors.Green;

// 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();
myRichEditBox.Document.SetText(TextSetOptions.None,

myRichEditBox.Document.Selection.CharacterFormat.ForegroundColor = currentColor;
myRichEditBox.Document.Selection.SetText(Microsoft.UI.Text.TextSetOptions.None,
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, " +
"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Tempor commodo ullamcorper a lacus.");
}
Expand Down Expand Up @@ -51,53 +50,12 @@ private void myColorButton_Click(Microsoft.UI.Xaml.Controls.SplitButton sender,
currentColor = color;
}

private void MyRichEditBox_TextChanging(object sender, RichEditBoxTextChangingEventArgs e)
private void MyRichEditBox_TextChanged(object sender, RoutedEventArgs e)
{
// Hitting control+b and similar 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)
if(myRichEditBox.Document.Selection.CharacterFormat.ForegroundColor != currentColor)
{
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ private void FindBoxRemoveHighlights()

private void Editor_GotFocus(object sender, RoutedEventArgs e)
{
editor.Document.GetText(TextGetOptions.UseCrlf, out string currentRawText);

// 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
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,17 @@
<Rectangle Fill="Gray"/>
</Button.Content>
</Button>
<Button Click="ColorButton_Click" AutomationProperties.Name="Black">
<Button.Content>
<Rectangle Fill="Black"/>
</Button.Content>
</Button>
</VariableSizedWrapGrid>
</Flyout>
</DropDownButton.Flyout>
</DropDownButton>

<RichEditBox x:Name="editor" Height="200"
RelativePanel.Below="openFileButton"
RelativePanel.AlignLeftWithPanel="True"
RelativePanel.AlignRightWithPanel="True"
GotFocus="Editor_GotFocus" />
<RichEditBox x:Name="editor" Height="200" AutomationProperties.Name="Custom editor"
RelativePanel.Below="openFileButton"
RelativePanel.AlignLeftWithPanel="True"
RelativePanel.AlignRightWithPanel="True"
TextChanged="Editor_TextChanged"
GotFocus="Editor_GotFocus"/>
<StackPanel Orientation="Horizontal"
RelativePanel.Below="editor"
RelativePanel.AlignLeftWith="editor"
Expand Down

0 comments on commit 88f7f9c

Please sign in to comment.