Skip to content

Commit

Permalink
Switch to RichEdit math mode in EquationTextBox (#672)
Browse files Browse the repository at this point in the history
* Use RichEdit Math Mode

* Add comment

Co-Authored-By: Stephanie Anderl <[email protected]>
  • Loading branch information
joseartrivera and sanderl committed Sep 23, 2019
1 parent 41fbcfe commit b2dd55a
Show file tree
Hide file tree
Showing 15 changed files with 126 additions and 72 deletions.
2 changes: 1 addition & 1 deletion src/CalcManager/CalcManager.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<AppContainerApplication>true</AppContainerApplication>
<ApplicationType>Windows Store</ApplicationType>
<ApplicationTypeRevision>10.0</ApplicationTypeRevision>
<WindowsTargetPlatformVersion Condition="'$(WindowsTargetPlatformVersion)' == ''">10.0.18362.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion Condition="'$(WindowsTargetPlatformVersion)' == ''">10.0.18970.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformMinVersion>10.0.17134.0</WindowsTargetPlatformMinVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
Expand Down
2 changes: 1 addition & 1 deletion src/CalcViewModel/CalcViewModel.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion>
<AppContainerApplication>true</AppContainerApplication>
<ApplicationType>Windows Store</ApplicationType>
<WindowsTargetPlatformVersion>10.0.18362.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion>10.0.18970.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformMinVersion>10.0.17134.0</WindowsTargetPlatformMinVersion>
<ApplicationTypeRevision>10.0</ApplicationTypeRevision>
</PropertyGroup>
Expand Down
3 changes: 2 additions & 1 deletion src/Calculator/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -1628,6 +1628,7 @@
<Button x:Name="EquationButton"
MinWidth="44"
MinHeight="44"
VerticalAlignment="Stretch"
Background="{TemplateBinding EquationColor}"
Foreground="{StaticResource SystemChromeWhiteColor}"
Content="ƒₓ">
Expand All @@ -1651,7 +1652,7 @@
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<RichEditBox x:Name="EquationTextBox"
MinHeight="42"
MinHeight="44"
Padding="{TemplateBinding Padding}"
VerticalAlignment="Stretch"
Style="{StaticResource EquationTextBoxStyle}"
Expand Down
2 changes: 1 addition & 1 deletion src/Calculator/Calculator.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<MinimumVisualStudioVersion>15.0</MinimumVisualStudioVersion>
<AppContainerApplication>true</AppContainerApplication>
<ApplicationType>Windows Store</ApplicationType>
<WindowsTargetPlatformVersion Condition="'$(WindowsTargetPlatformVersion)' == ''">10.0.18362.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion Condition="'$(WindowsTargetPlatformVersion)' == ''">10.0.18970.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformMinVersion>10.0.18362.0</WindowsTargetPlatformMinVersion>
<!-- We want to manually control the MinVersion/MaxVersionTested in the manifest so turn of the replacement. -->
<AppxOSMinVersionReplaceManifestVersion>false</AppxOSMinVersionReplaceManifestVersion>
Expand Down
123 changes: 84 additions & 39 deletions src/Calculator/Controls/EquationTextBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,35 @@ using namespace CalculatorApp;
using namespace CalculatorApp::Common;
using namespace CalculatorApp::Controls;
using namespace CalculatorApp::ViewModel;
using namespace Windows::System;
using namespace Windows::Foundation;
using namespace Windows::ApplicationModel;
using namespace Windows::UI::Text;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Text;

DEPENDENCY_PROPERTY_INITIALIZATION(EquationTextBox, EquationColor);
DEPENDENCY_PROPERTY_INITIALIZATION(EquationTextBox, KeyGraphFeaturesContent);
DEPENDENCY_PROPERTY_INITIALIZATION(EquationTextBox, ColorChooserFlyout);

void EquationTextBox::OnApplyTemplate()
{
m_equationButton = dynamic_cast<Button^>(GetTemplateChild("EquationButton"));
m_richEditBox = dynamic_cast<RichEditBox^>(GetTemplateChild("EquationTextBox"));
m_deleteButton = dynamic_cast<Button^>(GetTemplateChild("DeleteButton"));
m_removeButton = dynamic_cast<Button^>(GetTemplateChild("RemoveButton"));
m_functionButton = dynamic_cast<Button^>(GetTemplateChild("FunctionButton"));
m_colorChooserButton = dynamic_cast<ToggleButton^>(GetTemplateChild("ColorChooserButton"));
m_equationButton = dynamic_cast<Button ^>(GetTemplateChild("EquationButton"));
m_richEditBox = dynamic_cast<RichEditBox ^>(GetTemplateChild("EquationTextBox"));
m_deleteButton = dynamic_cast<Button ^>(GetTemplateChild("DeleteButton"));
m_removeButton = dynamic_cast<Button ^>(GetTemplateChild("RemoveButton"));
m_functionButton = dynamic_cast<Button ^>(GetTemplateChild("FunctionButton"));
m_colorChooserButton = dynamic_cast<ToggleButton ^>(GetTemplateChild("ColorChooserButton"));

if (m_richEditBox != nullptr)
{
m_richEditBox->Loaded += ref new RoutedEventHandler(this, &EquationTextBox::OnRichEditBoxLoaded);
m_richEditBox->GotFocus += ref new RoutedEventHandler(this, &EquationTextBox::OnRichEditBoxGotFocus);
m_richEditBox->LostFocus += ref new RoutedEventHandler(this, &EquationTextBox::OnRichEditBoxLostFocus);
m_richEditBox->TextChanged += ref new RoutedEventHandler(this, &EquationTextBox::OnRichEditBoxTextChanged);
m_richEditBox->SelectionFlyout = nullptr;
}

if (m_equationButton != nullptr)
Expand Down Expand Up @@ -65,103 +69,131 @@ void EquationTextBox::OnApplyTemplate()

if (ColorChooserFlyout != nullptr)
{
ColorChooserFlyout->Opened += ref new EventHandler<Object^>(this, &EquationTextBox::OnColorFlyoutOpened);
ColorChooserFlyout->Closed += ref new EventHandler<Object^>(this, &EquationTextBox::OnColorFlyoutClosed);
ColorChooserFlyout->Opened += ref new EventHandler<Object ^>(this, &EquationTextBox::OnColorFlyoutOpened);
ColorChooserFlyout->Closed += ref new EventHandler<Object ^>(this, &EquationTextBox::OnColorFlyoutClosed);
}
}

void EquationTextBox::OnPointerEntered(PointerRoutedEventArgs^ e)
void EquationTextBox::OnPointerEntered(PointerRoutedEventArgs ^ e)
{
m_isPointerOver = true;
UpdateCommonVisualState();
}

void EquationTextBox::OnPointerExited(PointerRoutedEventArgs^ e)
void EquationTextBox::OnPointerExited(PointerRoutedEventArgs ^ e)
{
m_isPointerOver = false;
UpdateCommonVisualState();
}

void EquationTextBox::OnPointerCanceled(PointerRoutedEventArgs^ e)
void EquationTextBox::OnPointerCanceled(PointerRoutedEventArgs ^ e)
{
m_isPointerOver = false;
UpdateCommonVisualState();
}

void EquationTextBox::OnPointerCaptureLost(PointerRoutedEventArgs^ e)
void EquationTextBox::OnPointerCaptureLost(PointerRoutedEventArgs ^ e)
{
m_isPointerOver = false;
UpdateCommonVisualState();
}

void EquationTextBox::OnColorFlyoutOpened(Object^ sender, Object^ e)
void EquationTextBox::OnKeyDown(KeyRoutedEventArgs ^ e)
{
if (e->Key == VirtualKey::Enter)
{
EquationSubmitted(this, ref new RoutedEventArgs());
}
}

void EquationTextBox::OnLostFocus(RoutedEventArgs ^ e)
{
if (!m_richEditBox->ContextFlyout->IsOpen)
{
EquationSubmitted(this, ref new RoutedEventArgs());
}
}

void EquationTextBox::OnColorFlyoutOpened(Object ^ sender, Object ^ e)
{
m_isColorChooserFlyoutOpen = true;
UpdateCommonVisualState();
}

void EquationTextBox::OnColorFlyoutClosed(Object^ sender, Object^ e)
void EquationTextBox::OnColorFlyoutClosed(Object ^ sender, Object ^ e)
{
m_colorChooserButton->IsChecked = false;
m_isColorChooserFlyoutOpen = false;
UpdateCommonVisualState();
}

void EquationTextBox::OnRichEditBoxTextChanged(Object^ sender, RoutedEventArgs^ e)
void EquationTextBox::OnRichEditBoxLoaded(Object ^ sender, RoutedEventArgs ^ e)
{
LimitedAccessFeatures::TryUnlockFeature(
"com.microsoft.windows.richeditmath",
"H6wflFFz3gkOsAHtG/D9Tg==",
"8wekyb3d8bbwe has registered their use of com.microsoft.windows.richeditmath with Microsoft and agrees to the terms of use.");
m_richEditBox->TextDocument->SetMathMode(::RichEditMathMode::MathOnly);
}

void EquationTextBox::OnRichEditBoxTextChanged(Object ^ sender, RoutedEventArgs ^ e)
{
UpdateDeleteButtonVisualState();
}

void EquationTextBox::OnRichEditBoxGotFocus(Object^ sender, RoutedEventArgs^ e)
void EquationTextBox::OnRichEditBoxGotFocus(Object ^ sender, RoutedEventArgs ^ e)
{
m_isFocused = true;
UpdateCommonVisualState();
UpdateDeleteButtonVisualState();
}

void EquationTextBox::OnRichEditBoxLostFocus(Object^ sender, RoutedEventArgs^ e)
void EquationTextBox::OnRichEditBoxLostFocus(Object ^ sender, RoutedEventArgs ^ e)
{
m_isFocused = false;
if (!m_richEditBox->ContextFlyout->IsOpen)
{
m_isFocused = false;
}
UpdateCommonVisualState();
UpdateDeleteButtonVisualState();
}

void EquationTextBox::OnDeleteButtonClicked(Object^ sender, RoutedEventArgs^ e)
void EquationTextBox::OnDeleteButtonClicked(Object ^ sender, RoutedEventArgs ^ e)
{
if (m_richEditBox != nullptr)
{
m_richEditBox->TextDocument->SetText(::TextSetOptions::None, L"");
m_richEditBox->TextDocument->SetMath(L"");
}
}

void EquationTextBox::OnEquationButtonClicked(Object^ sender, RoutedEventArgs^ e)
void EquationTextBox::OnEquationButtonClicked(Object ^ sender, RoutedEventArgs ^ e)
{

}

void EquationTextBox::OnRemoveButtonClicked(Object^ sender, RoutedEventArgs^ e)
void EquationTextBox::OnRemoveButtonClicked(Object ^ sender, RoutedEventArgs ^ e)
{
RemoveButtonClicked(this, ref new RoutedEventArgs());
}

void EquationTextBox::OnColorChooserButtonClicked(Object^ sender, RoutedEventArgs^ e)
void EquationTextBox::OnColorChooserButtonClicked(Object ^ sender, RoutedEventArgs ^ e)
{
if (ColorChooserFlyout != nullptr && m_richEditBox != nullptr)
{
ColorChooserFlyout->ShowAt(m_richEditBox);
}
}

void EquationTextBox::OnFunctionButtonClicked(Object^ sender, RoutedEventArgs^ e)
void EquationTextBox::OnFunctionButtonClicked(Object ^ sender, RoutedEventArgs ^ e)
{
auto equationViewModel = static_cast<EquationViewModel^>(DataContext);
equationViewModel->KeyGraphFeaturesVisibility = (equationViewModel->KeyGraphFeaturesVisibility == ::Visibility::Collapsed) ? ::Visibility::Visible : ::Visibility::Collapsed;
auto equationViewModel = static_cast<EquationViewModel ^>(DataContext);
equationViewModel->KeyGraphFeaturesVisibility =
(equationViewModel->KeyGraphFeaturesVisibility == ::Visibility::Collapsed) ? ::Visibility::Visible : ::Visibility::Collapsed;
UpdateCommonVisualState();
}

void EquationTextBox::UpdateDeleteButtonVisualState()
{
String^ state;
String ^ state;

if (ShouldDeleteButtonBeVisible())
{
Expand All @@ -177,7 +209,7 @@ void EquationTextBox::UpdateDeleteButtonVisualState()

void EquationTextBox::UpdateCommonVisualState()
{
String^ state = "Normal";
String ^ state = "Normal";

if (m_isFocused)
{
Expand All @@ -191,30 +223,43 @@ void EquationTextBox::UpdateCommonVisualState()
VisualStateManager::GoToState(this, state, true);
}


Platform::String^ EquationTextBox::GetEquationText()
Platform::String ^ EquationTextBox::GetEquationText()
{
String^ text;

String ^ text;
if (m_richEditBox != nullptr)
{
m_richEditBox->TextDocument->GetText(::TextGetOptions::NoHidden, &text);
// Clear formatting since the graph control doesn't work with bold/italic/underlines
ITextRange ^ range = m_richEditBox->TextDocument->GetRange(0, m_richEditBox->TextDocument->Selection->EndPosition);

if (range != nullptr)
{
range->CharacterFormat->Bold = FormatEffect::Off;
range->CharacterFormat->Italic = FormatEffect::Off;
range->CharacterFormat->Underline = UnderlineType::None;
}

m_richEditBox->TextDocument->GetMath(&text);
}

return text;
}

void EquationTextBox::SetEquationText(Platform::String^ equationText)
void EquationTextBox::SetEquationText(Platform::String ^ equationText)
{
if (m_richEditBox != nullptr)
{
m_richEditBox->TextDocument->SetText(::TextSetOptions::None, equationText);
m_richEditBox->TextDocument->SetMath(equationText);
}
}


bool EquationTextBox::ShouldDeleteButtonBeVisible()
{
return (!GetEquationText()->IsEmpty() && m_isFocused);
String ^ text;

if (m_richEditBox != nullptr)
{
m_richEditBox->TextDocument->GetMath(&text);
}

return (!text->IsEmpty() && m_isFocused);
}
7 changes: 6 additions & 1 deletion src/Calculator/Controls/EquationTextBox.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ namespace CalculatorApp
DEPENDENCY_PROPERTY(Windows::UI::Xaml::UIElement^, KeyGraphFeaturesContent);
DEPENDENCY_PROPERTY(Windows::UI::Xaml::Controls::Flyout^, ColorChooserFlyout);

event Windows::UI::Xaml::RoutedEventHandler^ RemoveButtonClicked;
event Windows::UI::Xaml::RoutedEventHandler ^ RemoveButtonClicked;
event Windows::UI::Xaml::RoutedEventHandler ^ EquationSubmitted;

Platform::String^ GetEquationText();
void SetEquationText(Platform::String^ equationText);
Expand All @@ -32,14 +33,18 @@ namespace CalculatorApp
virtual void OnPointerExited(Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e) override;
virtual void OnPointerCanceled(Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e) override;
virtual void OnPointerCaptureLost(Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e) override;
virtual void OnKeyDown(Windows::UI::Xaml::Input::KeyRoutedEventArgs^ e) override;
virtual void OnLostFocus(Windows::UI::Xaml::RoutedEventArgs^ e) override;

private:
void UpdateCommonVisualState();
void UpdateDeleteButtonVisualState();
bool ShouldDeleteButtonBeVisible();

void OnRichEditBoxLoaded(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
void OnRichEditBoxGotFocus(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
void OnRichEditBoxLostFocus(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
void OnRichEditBoxLosingFocus(Windows::UI::Xaml::UIElement ^ sender, Windows::UI::Xaml::Input::LosingFocusEventArgs ^ e);
void OnRichEditBoxTextChanged(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);

void OnDeleteButtonClicked(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
Grid.Column="1"
Margin="0,0,3,0"
Style="{StaticResource EquationTextBoxStyle}"
EquationSubmitted="InputTextBox_Submitted"
GotFocus="InputTextBox_GotFocus"
KeyUp="InputTextBox_KeyUp"
LostFocus="InputTextBox_LostFocus"
RemoveButtonClicked="EquationTextBox_RemoveButtonClicked">
<controls:EquationTextBox.EquationColor>
Expand Down
17 changes: 4 additions & 13 deletions src/Calculator/Views/GraphingCalculator/EquationInputArea.xaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,13 @@ void EquationInputArea::InputTextBox_GotFocus(Object^ sender, RoutedEventArgs^ e
void EquationInputArea::InputTextBox_LostFocus(Object^ sender, RoutedEventArgs^ e)
{
KeyboardShortcutManager::HonorShortcuts(true);

auto tb = static_cast<EquationTextBox^>(sender);
auto eq = static_cast<EquationViewModel^>(tb->DataContext);
tb->SetEquationText(eq->Expression);
}

void EquationInputArea::InputTextBox_KeyUp(Object^ sender, KeyRoutedEventArgs^ e)
void EquationInputArea::InputTextBox_Submitted(Object ^ sender, RoutedEventArgs ^ e)
{
if (e->Key == VirtualKey::Enter)
{
auto tb = static_cast<EquationTextBox^>(sender);
auto eq = static_cast<EquationViewModel^>(tb->DataContext);
eq->Expression = tb->GetEquationText();

e->Handled = true;
}
auto tb = static_cast<EquationTextBox^>(sender);
auto eq = static_cast<EquationViewModel^>(tb->DataContext);
eq->Expression = tb->GetEquationText();
}

Color EquationInputArea::GetNextLineColor()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace CalculatorApp

void InputTextBox_GotFocus(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
void InputTextBox_LostFocus(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
void InputTextBox_KeyUp(Platform::Object^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs^ e);
void InputTextBox_Submitted(Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e);

Windows::UI::Color GetNextLineColor();

Expand Down
2 changes: 1 addition & 1 deletion src/CalculatorUnitTests/CalculatorUnitTests.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<MinimumVisualStudioVersion>15.0</MinimumVisualStudioVersion>
<AppContainerApplication>true</AppContainerApplication>
<ApplicationType>Windows Store</ApplicationType>
<WindowsTargetPlatformVersion Condition=" '$(WindowsTargetPlatformVersion)' == '' ">10.0.18362.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion Condition=" '$(WindowsTargetPlatformVersion)' == '' ">10.0.18970.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformMinVersion>10.0.17134.0</WindowsTargetPlatformMinVersion>
<ApplicationTypeRevision>10.0</ApplicationTypeRevision>
<UnitTestPlatformVersion Condition="'$(UnitTestPlatformVersion)' == ''">15.0</UnitTestPlatformVersion>
Expand Down
2 changes: 1 addition & 1 deletion src/CalculatorUnitTests/NavCategoryUnitTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ namespace CalculatorUnitTests
void NavCategoryUnitTests::GetPosition()
{
// Position is the 1-based ordering of modes
vector<ViewMode> orderedModes = { ViewMode::Standard, ViewMode::Scientific, ViewMode::Programmer, ViewMode::Date, ViewMode::Graphing
vector<ViewMode> orderedModes = { ViewMode::Standard, ViewMode::Scientific, ViewMode::Programmer, ViewMode::Date, ViewMode::Graphing,
ViewMode::Currency, ViewMode::Volume, ViewMode::Length, ViewMode::Weight, ViewMode::Temperature,
ViewMode::Energy, ViewMode::Area, ViewMode::Speed, ViewMode::Time, ViewMode::Power,
ViewMode::Data, ViewMode::Pressure, ViewMode::Angle };
Expand Down
Loading

0 comments on commit b2dd55a

Please sign in to comment.