Modify how narrator names the first and last bits of a number in Bit Flip (#675)

* Replace 0th by "least significant bit"

* Add support of Most Significant Bit

* Fix issue with narrator not updating text of some bits
This commit is contained in:
Rudy Huyn 2019-09-27 10:39:50 -07:00 committed by Pepe Rivera
parent 9a099acb3a
commit 5c0785743c
3 changed files with 62 additions and 23 deletions

View File

@ -549,11 +549,11 @@
<value>%1, value %2</value> <value>%1, value %2</value>
<comment>{Locked="%1","%2"}. String used in automation name for each bit in bit flip. %1 will be replaced by the position of the bit (1st bit, 3rd bit), %2 by a binary value (1 or 0)</comment> <comment>{Locked="%1","%2"}. String used in automation name for each bit in bit flip. %1 will be replaced by the position of the bit (1st bit, 3rd bit), %2 by a binary value (1 or 0)</comment>
</data> </data>
<data name="BitPosition" xml:space="preserve"> <data name="BitPosition" xml:space="preserve">
<value>%1 bit</value> <value>%1 bit</value>
<comment>{Locked="%1"}. Sub-string used to indicate the position of a bit (e.g. 1st bit, 2nd bit...)</comment> <comment>{Locked="%1"}. Sub-string used to indicate the position of a bit (e.g. 1st bit, 2nd bit...)</comment>
</data> </data>
<data name="63" xml:space="preserve"> <data name="63" xml:space="preserve">
<value>63rd</value> <value>63rd</value>
<comment>Sub-string used in automation name for 63 bit in bit flip</comment> <comment>Sub-string used in automation name for 63 bit in bit flip</comment>
</data> </data>
@ -805,9 +805,9 @@
<value>1st</value> <value>1st</value>
<comment>Sub-string used in automation name for 1 bit in bit flip</comment> <comment>Sub-string used in automation name for 1 bit in bit flip</comment>
</data> </data>
<data name="0" xml:space="preserve"> <data name="LeastSignificantBit" xml:space="preserve">
<value>0th</value> <value>least significant bit</value>
<comment>Sub-string used in automation name for 0 bit in bit flip</comment> <comment>Used to describe the first bit of a binary number. Used in bit flip</comment>
</data> </data>
<data name="MemoryButton_Open" xml:space="preserve"> <data name="MemoryButton_Open" xml:space="preserve">
<value>Open memory flyout</value> <value>Open memory flyout</value>
@ -3427,4 +3427,8 @@
<value>Calculation failed</value> <value>Calculation failed</value>
<comment>Text displayed when the application is not able to do a calculation</comment> <comment>Text displayed when the application is not able to do a calculation</comment>
</data> </data>
</root> <data name="MostSignificantBit" xml:space="preserve">
<value>most significant bit</value>
<comment>Used to describe the last bit of a binary number. Used in bit flip</comment>
</data>
</root>

View File

@ -50,6 +50,7 @@ void CalculatorProgrammerBitFlipPanel::SubscribePropertyChanged()
if (Model != nullptr) if (Model != nullptr)
{ {
m_propertyChangedToken = Model->PropertyChanged += ref new PropertyChangedEventHandler(this, &CalculatorProgrammerBitFlipPanel::OnPropertyChanged); m_propertyChangedToken = Model->PropertyChanged += ref new PropertyChangedEventHandler(this, &CalculatorProgrammerBitFlipPanel::OnPropertyChanged);
m_currentValueBitLength = Model->ValueBitLength;
UpdateCheckedStates(true); UpdateCheckedStates(true);
} }
} }
@ -68,8 +69,10 @@ void CalculatorProgrammerBitFlipPanel::OnPropertyChanged(Object ^ sender, Proper
if (e->PropertyName == StandardCalculatorViewModel::BinaryDigitsPropertyName) if (e->PropertyName == StandardCalculatorViewModel::BinaryDigitsPropertyName)
{ {
UpdateCheckedStates(false); UpdateCheckedStates(false);
m_currentValueBitLength = Model->ValueBitLength;
} }
else if (e->PropertyName == StandardCalculatorViewModel::IsBitFlipCheckedPropertyName else if (
e->PropertyName == StandardCalculatorViewModel::IsBitFlipCheckedPropertyName
|| e->PropertyName == StandardCalculatorViewModel::IsProgrammerPropertyName) || e->PropertyName == StandardCalculatorViewModel::IsProgrammerPropertyName)
{ {
if (Model->IsBitFlipChecked && Model->IsProgrammer) if (Model->IsBitFlipChecked && Model->IsProgrammer)
@ -190,14 +193,20 @@ void CalculatorProgrammerBitFlipPanel::UpdateCheckedStates(bool updateAutomation
m_updatingCheckedStates = true; m_updatingCheckedStates = true;
auto it = m_flipButtons.begin(); auto it = m_flipButtons.begin();
int index = 0; int index = 0;
bool mustUpdateTextOfMostSignificantDigits = m_currentValueBitLength != Model->ValueBitLength;
int previousMSDPosition = GetIndexOfLastBit(m_currentValueBitLength);
int newMSDPosition = GetIndexOfLastBit(Model->ValueBitLength);
for (bool val : Model->BinaryDigits) for (bool val : Model->BinaryDigits)
{ {
FlipButtons ^ flipButton = *it; FlipButtons ^ flipButton = *it;
if (updateAutomationPropertiesNames) bool hasValueChanged = flipButton->IsChecked->Value != val;
{
flipButton->SetValue(AutomationProperties::NameProperty, GenerateAutomationPropertiesName(index, flipButton->IsChecked->Value));
}
flipButton->IsChecked = val; flipButton->IsChecked = val;
if (updateAutomationPropertiesNames
|| hasValueChanged
|| (mustUpdateTextOfMostSignificantDigits && (index == previousMSDPosition || index == newMSDPosition)))
{
flipButton->SetValue(AutomationProperties::NameProperty, GenerateAutomationPropertiesName(index, val));
}
++it; ++it;
++index; ++index;
} }
@ -215,29 +224,53 @@ void CalculatorProgrammerBitFlipPanel::UpdateAutomationPropertiesNames()
} }
bool CalculatorProgrammerBitFlipPanel::ShouldEnableBit(BitLength length, int index) bool CalculatorProgrammerBitFlipPanel::ShouldEnableBit(BitLength length, int index)
{
return index <= GetIndexOfLastBit(length);
}
int CalculatorProgrammerBitFlipPanel::GetIndexOfLastBit(BitLength length) const
{ {
switch (length) switch (length)
{ {
case BitLength::BitLengthQWord: case BitLength::BitLengthQWord:
return index <= 63; return 63;
case BitLength::BitLengthDWord: case BitLength::BitLengthDWord:
return index <= 31; return 31;
case BitLength::BitLengthWord: case BitLength::BitLengthWord:
return index <= 15; return 15;
case BitLength::BitLengthByte: case BitLength::BitLengthByte:
return index <= 7; return 7;
} }
return false; return -1;
} }
String ^ CalculatorProgrammerBitFlipPanel::GenerateAutomationPropertiesName(int position, bool value) const String ^ CalculatorProgrammerBitFlipPanel::GenerateAutomationPropertiesName(int position, bool value)
{ {
auto resourceLoader = AppResourceProvider::GetInstance(); auto resourceLoader = AppResourceProvider::GetInstance();
String ^ indexName = resourceLoader.GetResourceString(ref new Platform::String(to_wstring(position).c_str()));
String ^ automationNameTemplate = resourceLoader.GetResourceString(L"BitFlipItemAutomationName"); String ^ automationNameTemplate = resourceLoader.GetResourceString(L"BitFlipItemAutomationName");
String ^ bitPositionTemplate = resourceLoader.GetResourceString(L"BitPosition"); wstring bitPosition;
if (position == 0)
{
bitPosition = wstring(resourceLoader.GetResourceString(L"LeastSignificantBit")->Data());
}
else
{
int lastPosition = -1;
if (Model != nullptr)
{
lastPosition = GetIndexOfLastBit(Model->ValueBitLength);
}
wstring bitPosition = LocalizationStringUtil::GetLocalizedString(bitPositionTemplate->Data(), indexName->Data()); if (position == lastPosition)
{
bitPosition = wstring(resourceLoader.GetResourceString(L"MostSignificantBit")->Data());
}
else
{
String ^ indexName = resourceLoader.GetResourceString(ref new Platform::String(to_wstring(position).c_str()));
String ^ bitPositionTemplate = resourceLoader.GetResourceString(L"BitPosition");
bitPosition = LocalizationStringUtil::GetLocalizedString(bitPositionTemplate->Data(), indexName->Data());
}
}
return ref new String(LocalizationStringUtil::GetLocalizedString(automationNameTemplate->Data(), bitPosition.c_str(), value ? L"1" : L"0").c_str()); return ref new String(LocalizationStringUtil::GetLocalizedString(automationNameTemplate->Data(), bitPosition.c_str(), value ? L"1" : L"0").c_str());
} }

View File

@ -22,7 +22,7 @@ namespace CalculatorApp
bool ShouldEnableBit(CalculatorApp::Common::BitLength length, int index); bool ShouldEnableBit(CalculatorApp::Common::BitLength length, int index);
property CalculatorApp::ViewModel::StandardCalculatorViewModel property CalculatorApp::ViewModel::StandardCalculatorViewModel
^ Model { CalculatorApp::ViewModel::StandardCalculatorViewModel ^ get(); } ^ Model { CalculatorApp::ViewModel::StandardCalculatorViewModel ^ get(); }
private : void OnLoaded(Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e); private : void OnLoaded(Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e);
void OnUnloaded(Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e); void OnUnloaded(Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e);
@ -37,12 +37,14 @@ namespace CalculatorApp
private: private:
Windows::Foundation::EventRegistrationToken m_propertyChangedToken; Windows::Foundation::EventRegistrationToken m_propertyChangedToken;
Platform::String ^ GenerateAutomationPropertiesName(int position, bool value) const; Platform::String ^ GenerateAutomationPropertiesName(int position, bool value);
void UpdateCheckedStates(bool updateAutomationPropertiesNames); void UpdateCheckedStates(bool updateAutomationPropertiesNames);
void UpdateAutomationPropertiesNames(); void UpdateAutomationPropertiesNames();
int GetIndexOfLastBit(CalculatorApp::Common::BitLength length) const;
static const unsigned int s_numBits = 64; static const unsigned int s_numBits = 64;
std::array<CalculatorApp::Controls::FlipButtons ^, s_numBits> m_flipButtons; std::array<CalculatorApp::Controls::FlipButtons ^, s_numBits> m_flipButtons;
bool m_updatingCheckedStates; bool m_updatingCheckedStates;
CalculatorApp::Common::BitLength m_currentValueBitLength;
}; };
} }