Skip to content

Commit

Permalink
Remove LRE/LRO characters from results and error messages (microsoft#…
Browse files Browse the repository at this point in the history
…1161)

* Remove LRE/LRO characters and rely on Xaml to correctly displayed the numbers and error messages RtL

* unit tests
  • Loading branch information
rudyhuyn authored Apr 30, 2020
1 parent 2cafb0d commit 6e521d8
Show file tree
Hide file tree
Showing 20 changed files with 127 additions and 188 deletions.
7 changes: 1 addition & 6 deletions src/CalcManager/CalculatorHistory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,7 @@ unsigned int CalculatorHistory::AddToHistory(

spHistoryItem->historyItemVector.spTokens = tokens;
spHistoryItem->historyItemVector.spCommands = commands;

// to be changed when pszexp is back
wstring generatedExpression = GetGeneratedExpression(*tokens);
// Prefixing and suffixing the special Unicode markers to ensure that the expression
// in the history doesn't get broken for RTL languages
spHistoryItem->historyItemVector.expression = L'\u202d' + generatedExpression + L'\u202c';
spHistoryItem->historyItemVector.expression = GetGeneratedExpression(*tokens);
spHistoryItem->historyItemVector.result = wstring(result);
return AddItem(spHistoryItem);
}
Expand Down
3 changes: 0 additions & 3 deletions src/CalcViewModel/Common/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,6 @@ namespace Utils
};
}

const wchar_t LRE = 0x202a; // Left-to-Right Embedding
const wchar_t PDF = 0x202c; // Pop Directional Formatting
const wchar_t LRO = 0x202d; // Left-to-Right Override

// Regular DependencyProperty
template <typename TOwner, typename TType>
Expand Down
31 changes: 11 additions & 20 deletions src/CalcViewModel/StandardCalculatorViewModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,23 +134,14 @@ StandardCalculatorViewModel::StandardCalculatorViewModel()
AreProgrammerRadixOperatorsEnabled = false;
}

String ^ StandardCalculatorViewModel::LocalizeDisplayValue(_In_ wstring const& displayValue, _In_ bool isError)
String ^ StandardCalculatorViewModel::LocalizeDisplayValue(_In_ wstring const& displayValue)
{
wstring result(displayValue);

LocalizationSettings::GetInstance().LocalizeDisplayValue(&result);

// WINBLUE: 440747 - In BiDi languages, error messages need to be wrapped in LRE/PDF
if (isError && m_isRtlLanguage)
{
result.insert(result.begin(), Utils::LRE);
result.push_back(Utils::PDF);
}

return ref new Platform::String(result.c_str());
}

String ^ StandardCalculatorViewModel::CalculateNarratorDisplayValue(_In_ wstring const& displayValue, _In_ String ^ localizedDisplayValue, _In_ bool isError)
String ^ StandardCalculatorViewModel::CalculateNarratorDisplayValue(_In_ wstring const& displayValue, _In_ String ^ localizedDisplayValue)
{
String ^ localizedValue = localizedDisplayValue;
String ^ automationFormat = m_localizedCalculationResultAutomationFormat;
Expand All @@ -159,7 +150,7 @@ String ^ StandardCalculatorViewModel::CalculateNarratorDisplayValue(_In_ wstring
if (Utils::IsLastCharacterTarget(displayValue, m_decimalSeparator))
{
// remove the decimal separator, to avoid a long pause between words
localizedValue = LocalizeDisplayValue(displayValue.substr(0, displayValue.length() - 1), isError);
localizedValue = LocalizeDisplayValue(displayValue.substr(0, displayValue.length() - 1));

// Use a format which has a word in the decimal separator's place
// "The Display is 10 point"
Expand Down Expand Up @@ -195,11 +186,11 @@ String ^ StandardCalculatorViewModel::GetNarratorStringReadRawNumbers(_In_ Strin

void StandardCalculatorViewModel::SetPrimaryDisplay(_In_ String ^ displayStringValue, _In_ bool isError)
{
String ^ localizedDisplayStringValue = LocalizeDisplayValue(displayStringValue->Data(), isError);
String ^ localizedDisplayStringValue = LocalizeDisplayValue(displayStringValue->Data());

// Set this variable before the DisplayValue is modified, Otherwise the DisplayValue will
// not match what the narrator is saying
m_CalculationResultAutomationName = CalculateNarratorDisplayValue(displayStringValue->Data(), localizedDisplayStringValue, isError);
m_CalculationResultAutomationName = CalculateNarratorDisplayValue(displayStringValue->Data(), localizedDisplayStringValue);

AreAlwaysOnTopResultsUpdated = false;
if (DisplayValue != localizedDisplayStringValue)
Expand Down Expand Up @@ -418,7 +409,7 @@ void StandardCalculatorViewModel::SetMemorizedNumbers(const vector<wstring>& new
MemoryItemViewModel ^ memorySlot = ref new MemoryItemViewModel(this);
memorySlot->Position = 0;
localizer.LocalizeDisplayValue(&stringValue);
memorySlot->Value = Utils::LRO + ref new String(stringValue.c_str()) + Utils::PDF;
memorySlot->Value = ref new String(stringValue.c_str());

MemorizedNumbers->InsertAt(0, memorySlot);
IsMemoryEmpty = IsAlwaysOnTop;
Expand All @@ -440,7 +431,7 @@ void StandardCalculatorViewModel::SetMemorizedNumbers(const vector<wstring>& new
// If the value is different, update the value
if (MemorizedNumbers->GetAt(i)->Value != StringReference(newStringValue.c_str()))
{
MemorizedNumbers->GetAt(i)->Value = Utils::LRO + ref new String(newStringValue.c_str()) + Utils::PDF;
MemorizedNumbers->GetAt(i)->Value = ref new String(newStringValue.c_str());
}
}
}
Expand Down Expand Up @@ -1576,10 +1567,10 @@ void StandardCalculatorViewModel::UpdateProgrammerPanelDisplay()
localizer.LocalizeDisplayValue(&octalDisplayString);
localizer.LocalizeDisplayValue(&binaryDisplayString);

HexDisplayValue = Utils::LRO + ref new Platform::String(hexDisplayString.c_str()) + Utils::PDF;
DecimalDisplayValue = Utils::LRO + ref new Platform::String(decimalDisplayString.c_str()) + Utils::PDF;
OctalDisplayValue = Utils::LRO + ref new Platform::String(octalDisplayString.c_str()) + Utils::PDF;
BinaryDisplayValue = Utils::LRO + ref new Platform::String(binaryDisplayString.c_str()) + Utils::PDF;
HexDisplayValue = ref new Platform::String(hexDisplayString.c_str());
DecimalDisplayValue = ref new Platform::String(decimalDisplayString.c_str());
OctalDisplayValue = ref new Platform::String(octalDisplayString.c_str());
BinaryDisplayValue = ref new Platform::String(binaryDisplayString.c_str());
HexDisplayValue_AutomationName = GetLocalizedStringFormat(m_localizedHexaDecimalAutomationFormat, GetNarratorStringReadRawNumbers(HexDisplayValue));
DecDisplayValue_AutomationName = GetLocalizedStringFormat(m_localizedDecimalAutomationFormat, DecimalDisplayValue);
OctDisplayValue_AutomationName = GetLocalizedStringFormat(m_localizedOctalAutomationFormat, GetNarratorStringReadRawNumbers(OctalDisplayValue));
Expand Down
4 changes: 2 additions & 2 deletions src/CalcViewModel/StandardCalculatorViewModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,9 @@ namespace CalculatorApp
Platform::String ^ m_selectedExpressionLastData;
Common::DisplayExpressionToken ^ m_selectedExpressionToken;

Platform::String ^ LocalizeDisplayValue(_In_ std::wstring const& displayValue, _In_ bool isError);
Platform::String ^ LocalizeDisplayValue(_In_ std::wstring const& displayValue);
Platform::String
^ CalculateNarratorDisplayValue(_In_ std::wstring const& displayValue, _In_ Platform::String ^ localizedDisplayValue, _In_ bool isError);
^ CalculateNarratorDisplayValue(_In_ std::wstring const& displayValue, _In_ Platform::String ^ localizedDisplayValue);
CalculatorApp::Common::Automation::NarratorAnnouncement ^ GetDisplayUpdatedNarratorAnnouncement();
Platform::String ^ GetCalculatorExpressionAutomationName();
Platform::String ^ GetNarratorStringReadRawNumbers(_In_ Platform::String ^ localizedDisplayValue);
Expand Down
1 change: 0 additions & 1 deletion src/CalcViewModel/UnitConverterViewModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,6 @@ String ^ UnitConverterViewModel::ConvertToLocalizedString(const std::wstring& st
}
result = L"-" + result;
}
result = Utils::LRE + result + Utils::PDF;
return result;
}

Expand Down
4 changes: 3 additions & 1 deletion src/Calculator/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,9 @@
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Controls:CalculationResult">
<Grid x:Name="Border" Background="{TemplateBinding Background}">
<Grid x:Name="Border"
Background="{TemplateBinding Background}"
FlowDirection="LeftToRight">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="12"/>
<ColumnDefinition/>
Expand Down
2 changes: 1 addition & 1 deletion src/Calculator/Controls/CalculationResult.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ void CalculationResult::UpdateTextState()

auto containerSize = m_textContainer->ActualWidth;
String ^ oldText = m_textBlock->Text;
String ^ newText = Utils::LRO + DisplayValue + Utils::PDF;
String ^ newText = DisplayValue;

// Initiate the scaling operation
// UpdateLayout will keep calling us until we make it through the below 2 if-statements
Expand Down
8 changes: 6 additions & 2 deletions src/Calculator/Views/Calculator.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="controls:OverflowTextBlock">
<Grid x:Name="TokenContainer" Background="{TemplateBinding Background}">
<Grid x:Name="TokenContainer"
Background="{TemplateBinding Background}"
FlowDirection="LeftToRight">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="12"/>
<ColumnDefinition/>
Expand Down Expand Up @@ -96,7 +98,9 @@
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="controls:OverflowTextBlock">
<Grid x:Name="TokenContainer" Background="{TemplateBinding Background}">
<Grid x:Name="TokenContainer"
Background="{TemplateBinding Background}"
FlowDirection="LeftToRight">
<Grid.Resources>
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
Expand Down
3 changes: 2 additions & 1 deletion src/Calculator/Views/CalculatorProgrammerOperators.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@
<Grid x:Name="ProgrammerOperators"
x:Uid="RadixGroup"
MaxHeight="244"
AutomationProperties.HeadingLevel="Level1">
AutomationProperties.HeadingLevel="Level1"
FlowDirection="LeftToRight">
<Grid.RowDefinitions>
<RowDefinition Height="1*" MinHeight="0"/>
<RowDefinition Height="1*" MinHeight="0"/>
Expand Down
2 changes: 0 additions & 2 deletions src/Calculator/Views/HistoryList.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:automation="using:CalculatorApp.Common.Automation"
xmlns:controls="using:CalculatorApp.Controls"
xmlns:converters="using:CalculatorApp.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:CalculatorApp"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:model="using:CalculatorApp.ViewModel"
xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
Expand Down
5 changes: 0 additions & 5 deletions src/Calculator/Views/MemoryListItem.xaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
<UserControl x:Class="CalculatorApp.MemoryListItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:common="using:CalculatorApp.Common"
xmlns:controls="using:CalculatorApp.Controls"
xmlns:converters="using:CalculatorApp.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:CalculatorApp"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:model="using:CalculatorApp.ViewModel"
xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
x:Name="MemoryListItem"
FlowDirection="LeftToRight"
Expand Down
1 change: 0 additions & 1 deletion src/CalculatorUnitTests/CalculatorUnitTests.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@
<ClCompile Include="CopyPasteManagerTest.cpp" />
<ClCompile Include="CurrencyConverterUnitTests.cpp" />
<ClCompile Include="DateCalculatorUnitTests.cpp" />
<ClCompile Include="Helper.cpp" />
<ClCompile Include="HistoryTests.cpp" />
<ClCompile Include="LocalizationServiceUnitTests.cpp" />
<ClCompile Include="LocalizationSettingsUnitTests.cpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
<ClCompile Include="LocalizationServiceUnitTests.cpp" />
<ClCompile Include="RationalTest.cpp" />
<ClCompile Include="LocalizationSettingsUnitTests.cpp" />
<ClCompile Include="Helper.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="DateUtils.h" />
Expand Down
14 changes: 0 additions & 14 deletions src/CalculatorUnitTests/Helper.cpp

This file was deleted.

5 changes: 0 additions & 5 deletions src/CalculatorUnitTests/Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ namespace CalculatorUnitTests

namespace UtfUtils
{
constexpr wchar_t LRE = 0x202a; // Left-to-Right Embedding
constexpr wchar_t PDF = 0x202c; // Pop Directional Formatting
constexpr wchar_t LRO = 0x202d; // Left-to-Right Override
constexpr wchar_t MUL = 0x00d7; // Multiplication Symbol
}
}
Expand Down Expand Up @@ -108,5 +105,3 @@ void VERIFY_VECTORS_ARE_EQUAL(Windows::Foundation::Collections::IVector<T> ^ vec
VERIFY_ARE_EQUAL(vecA->GetAt(i), vecB->GetAt(i), __VA_ARGS__);
}
};

Platform::String ^ GetStringValue(Platform::String ^ input);
18 changes: 6 additions & 12 deletions src/CalculatorUnitTests/HistoryTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ namespace CalculatorFunctionalTests
m_standardViewModel->SendCommandToCalcManager(static_cast<int>(Command::CommandEQU));
int sizeAfterItemAdd = m_historyViewModel->ItemSize;
auto historyItem = static_cast<HistoryItemViewModel ^>(m_historyViewModel->Items->GetAt(0));
String ^ expression = UtfUtils::LRO + L"1 + 8 =" + UtfUtils::PDF;
String ^ expression = L"1 + 8 =";
VERIFY_ARE_EQUAL(initialSize + 1, sizeAfterItemAdd);
VERIFY_ARE_EQUAL(historyItem->Expression, expression);
VERIFY_ARE_EQUAL(historyItem->Result, L"9");
Expand All @@ -102,7 +102,7 @@ namespace CalculatorFunctionalTests
m_standardViewModel->SendCommandToCalcManager(static_cast<int>(Command::CommandEQU));
}
VERIFY_ARE_EQUAL((size_t)m_historyViewModel->ItemSize, m_historyViewModel->GetMaxItemSize());
String ^ expression = UtfUtils::LRO + L"1 + 1 =" + UtfUtils::PDF;
String ^ expression = L"1 + 1 =";
int output = 2;
String ^ result = output.ToString();
auto historyItem = static_cast<HistoryItemViewModel ^>(m_historyViewModel->Items->GetAt(m_historyViewModel->ItemSize - 1));
Expand All @@ -112,7 +112,7 @@ namespace CalculatorFunctionalTests
m_standardViewModel->SendCommandToCalcManager(static_cast<int>(Command::CommandADD));
m_standardViewModel->SendCommandToCalcManager(static_cast<int>(Command::Command5));
m_standardViewModel->SendCommandToCalcManager(static_cast<int>(Command::CommandEQU));
expression = UtfUtils::LRO + L"1 + 2 =" + UtfUtils::PDF;
expression = L"1 + 2 =";
output = 3;
result = output.ToString();
historyItem = static_cast<HistoryItemViewModel ^>(m_historyViewModel->Items->GetAt(m_historyViewModel->ItemSize - 1));
Expand Down Expand Up @@ -154,7 +154,6 @@ namespace CalculatorFunctionalTests
for (int i = 0; i < scientificItems; i++)
{
wstring expr = L"1 + " + wstring(i.ToString()->Data()) + L" =";
expr = UtfUtils::LRO + expr + UtfUtils::PDF;
int output = 1 + i;
String ^ result = output.ToString();
auto historyItem = static_cast<HistoryItemViewModel ^>(m_historyViewModel->Items->GetAt(m_historyViewModel->ItemSize - 1 - i));
Expand All @@ -168,7 +167,6 @@ namespace CalculatorFunctionalTests
for (int i = 0; i < standardItems; i++)
{
wstring expr = L"1 + " + wstring(i.ToString()->Data()) + L" =";
expr = UtfUtils::LRO + expr + UtfUtils::PDF;
int output = 1 + i;
String ^ result = output.ToString();
auto historyItem = static_cast<HistoryItemViewModel ^>(m_historyViewModel->Items->GetAt(m_historyViewModel->ItemSize - 1 - i));
Expand Down Expand Up @@ -238,8 +236,6 @@ namespace CalculatorFunctionalTests
m_historyViewModel->ReloadHistory(ViewMode::Scientific);
m_standardViewModel->SendCommandToCalcManager(static_cast<int>(Command::ModeScientific));
wstring expr = L"1 + 8 =";
// add double quotes around the expression
expr = UtfUtils::LRO + expr + UtfUtils::PDF;
String ^ result = StringReference(L"9");
int itemsAfterSaveAndReload = m_historyViewModel->ItemSize;

Expand Down Expand Up @@ -275,7 +271,6 @@ namespace CalculatorFunctionalTests
String ^ expression = m_uiResourceLoader->GetString(commandResource.ToString());
expression += L"( 1 ) =";
wstring expr = wstring(expression->Data());
expr = UtfUtils::LRO + expr + UtfUtils::PDF;
VERIFY_ARE_EQUAL(historyItem->Expression, StringReference(expr.c_str()));
commandResource++;
itemIndex++;
Expand Down Expand Up @@ -309,7 +304,6 @@ namespace CalculatorFunctionalTests
expression += m_uiResourceLoader->GetString(L"79");
expression += L"( 1 ) =";
wstring expr = wstring(expression->Data());
expr = UtfUtils::LRO + expr + UtfUtils::PDF;
VERIFY_ARE_EQUAL(historyItem->Expression, StringReference(expr.c_str()));

Cleanup();
Expand Down Expand Up @@ -406,13 +400,13 @@ namespace CalculatorFunctionalTests
m_standardViewModel->SendCommandToCalcManager(static_cast<int>(Command::Command7));
m_standardViewModel->SendCommandToCalcManager(static_cast<int>(Command::CommandEQU));
String ^ expression = L"HexaDecimal" + L" 8";
String ^ result = L"HexaDecimal " + GetStringValue(m_standardViewModel->HexDisplayValue);
String ^ result = L"HexaDecimal " + m_standardViewModel->HexDisplayValue;
VERIFY_ARE_EQUAL(expression, result);
expression = StringReference(L"Octal 10");
result = L"Octal " + GetStringValue(m_standardViewModel->OctalDisplayValue);
result = L"Octal " + m_standardViewModel->OctalDisplayValue;
VERIFY_ARE_EQUAL(expression, result);
expression = StringReference(L"Binary 1000");
result = L"Binary " + GetStringValue(m_standardViewModel->BinaryDisplayValue);
result = L"Binary " + m_standardViewModel->BinaryDisplayValue;
VERIFY_ARE_EQUAL(expression, result);
Cleanup();
}
Expand Down
Loading

0 comments on commit 6e521d8

Please sign in to comment.