@@ -1813,30 +1813,53 @@ void CalculatorApp::ViewModel::StandardCalculatorViewModel::Snapshot::set(Calcul
1813
1813
m_standardCalculatorManager.SetHistoryItems (ToUnderlying (snapshot->CalcManager ->HistoryItems ));
1814
1814
}
1815
1815
1816
- std::vector<int > commands;
1817
- if (snapshot->ExpressionDisplay != nullptr && snapshot->ExpressionDisplay ->Tokens ->GetAt (snapshot->ExpressionDisplay ->Tokens ->Size - 1 )->OpCodeName == L" =" )
1818
- {
1819
- commands = GetCommandsFromExpressionCommands (ToUnderlying (snapshot->ExpressionDisplay ->Commands ));
1820
- }
1821
- if (commands.empty () && snapshot->DisplayCommands ->Size > 0 )
1822
- {
1823
- commands = GetCommandsFromExpressionCommands (ToUnderlying (snapshot->DisplayCommands ));
1824
- }
1825
- for (auto cmd : commands)
1816
+ if (snapshot->ExpressionDisplay != nullptr )
1826
1817
{
1827
- m_standardCalculatorManager.SendCommand (static_cast <Command>(cmd));
1818
+ if (snapshot->DisplayCommands ->Size == 0 )
1819
+ {
1820
+ // use case: the current expression was evaluated before. load from history.
1821
+ assert (!snapshot->PrimaryDisplay ->IsError );
1822
+ using RawTokenCollection = std::vector<std::pair<std::wstring, int >>;
1823
+ RawTokenCollection rawTokens;
1824
+ for (CalculatorApp::ViewModel::Snapshot::CalcManagerToken ^ token : snapshot->ExpressionDisplay ->Tokens )
1825
+ {
1826
+ rawTokens.push_back (std::pair{ token->OpCodeName ->Data (), token->CommandIndex });
1827
+ }
1828
+ auto tokens = std::make_shared<RawTokenCollection>(rawTokens);
1829
+ auto commands = std::make_shared<std::vector<std::shared_ptr<IExpressionCommand>>>(ToUnderlying (snapshot->ExpressionDisplay ->Commands ));
1830
+ SetHistoryExpressionDisplay (tokens, commands);
1831
+ SetExpressionDisplay (tokens, commands);
1832
+ SetPrimaryDisplay (snapshot->PrimaryDisplay ->DisplayValue , false );
1833
+ }
1834
+ else
1835
+ {
1836
+ // use case: the current expression was not evaluated before, or it was an error.
1837
+ auto displayCommands = GetCommandsFromExpressionCommands (ToUnderlying (snapshot->DisplayCommands ));
1838
+ for (auto cmd : displayCommands)
1839
+ {
1840
+ m_standardCalculatorManager.SendCommand (static_cast <Command>(cmd));
1841
+ }
1842
+ if (snapshot->PrimaryDisplay ->IsError )
1843
+ {
1844
+ SetPrimaryDisplay (snapshot->PrimaryDisplay ->DisplayValue , true );
1845
+ }
1846
+ }
1828
1847
}
1829
- if (snapshot-> ExpressionDisplay != nullptr )
1848
+ else
1830
1849
{
1831
- using RawTokenCollection = std::vector<std::pair<std::wstring, int >>;
1832
- RawTokenCollection rawTokens;
1833
- for (CalculatorApp::ViewModel::Snapshot::CalcManagerToken ^ token : snapshot->ExpressionDisplay ->Tokens )
1850
+ if (snapshot->PrimaryDisplay ->IsError )
1834
1851
{
1835
- rawTokens.push_back (std::pair{ token->OpCodeName ->Data (), token->CommandIndex });
1852
+ // use case: user copy-pasted an invalid expression to Calculator and caused an error.
1853
+ SetPrimaryDisplay (snapshot->PrimaryDisplay ->DisplayValue , true );
1854
+ }
1855
+ else
1856
+ {
1857
+ // use case: there was no expression but user was inputing some numbers (including negative numbers).
1858
+ auto commands = GetCommandsFromExpressionCommands (ToUnderlying (snapshot->DisplayCommands ));
1859
+ for (auto cmd : commands)
1860
+ {
1861
+ m_standardCalculatorManager.SendCommand (static_cast <Command>(cmd));
1862
+ }
1836
1863
}
1837
- SetExpressionDisplay (
1838
- std::make_shared<RawTokenCollection>(rawTokens),
1839
- std::make_shared<std::vector<std::shared_ptr<IExpressionCommand>>>(ToUnderlying (snapshot->ExpressionDisplay ->Commands )));
1840
1864
}
1841
- SetPrimaryDisplay (snapshot->PrimaryDisplay ->DisplayValue , snapshot->PrimaryDisplay ->IsError );
1842
1865
}
0 commit comments