mirror of
https://github.com/Microsoft/calculator.git
synced 2025-07-05 20:51:09 -07:00
Resolve an existing defect when restoring from snapshots (#2249)
* fix bug * fix a comment
This commit is contained in:
parent
80015d227f
commit
2b0521eb59
1 changed files with 44 additions and 21 deletions
|
@ -1813,30 +1813,53 @@ void CalculatorApp::ViewModel::StandardCalculatorViewModel::Snapshot::set(Calcul
|
|||
m_standardCalculatorManager.SetHistoryItems(ToUnderlying(snapshot->CalcManager->HistoryItems));
|
||||
}
|
||||
|
||||
std::vector<int> commands;
|
||||
if (snapshot->ExpressionDisplay != nullptr && snapshot->ExpressionDisplay->Tokens->GetAt(snapshot->ExpressionDisplay->Tokens->Size - 1)->OpCodeName == L"=")
|
||||
{
|
||||
commands = GetCommandsFromExpressionCommands(ToUnderlying(snapshot->ExpressionDisplay->Commands));
|
||||
}
|
||||
if (commands.empty() && snapshot->DisplayCommands->Size > 0)
|
||||
{
|
||||
commands = GetCommandsFromExpressionCommands(ToUnderlying(snapshot->DisplayCommands));
|
||||
}
|
||||
for (auto cmd : commands)
|
||||
{
|
||||
m_standardCalculatorManager.SendCommand(static_cast<Command>(cmd));
|
||||
}
|
||||
if (snapshot->ExpressionDisplay != nullptr)
|
||||
{
|
||||
using RawTokenCollection = std::vector<std::pair<std::wstring, int>>;
|
||||
RawTokenCollection rawTokens;
|
||||
for (CalculatorApp::ViewModel::Snapshot::CalcManagerToken ^ token : snapshot->ExpressionDisplay->Tokens)
|
||||
if (snapshot->DisplayCommands->Size == 0)
|
||||
{
|
||||
rawTokens.push_back(std::pair{ token->OpCodeName->Data(), token->CommandIndex });
|
||||
// use case: the current expression was evaluated before. load from history.
|
||||
assert(!snapshot->PrimaryDisplay->IsError);
|
||||
using RawTokenCollection = std::vector<std::pair<std::wstring, int>>;
|
||||
RawTokenCollection rawTokens;
|
||||
for (CalculatorApp::ViewModel::Snapshot::CalcManagerToken ^ token : snapshot->ExpressionDisplay->Tokens)
|
||||
{
|
||||
rawTokens.push_back(std::pair{ token->OpCodeName->Data(), token->CommandIndex });
|
||||
}
|
||||
auto tokens = std::make_shared<RawTokenCollection>(rawTokens);
|
||||
auto commands = std::make_shared<std::vector<std::shared_ptr<IExpressionCommand>>>(ToUnderlying(snapshot->ExpressionDisplay->Commands));
|
||||
SetHistoryExpressionDisplay(tokens, commands);
|
||||
SetExpressionDisplay(tokens, commands);
|
||||
SetPrimaryDisplay(snapshot->PrimaryDisplay->DisplayValue, false);
|
||||
}
|
||||
else
|
||||
{
|
||||
// use case: the current expression was not evaluated before, or it was an error.
|
||||
auto displayCommands = GetCommandsFromExpressionCommands(ToUnderlying(snapshot->DisplayCommands));
|
||||
for (auto cmd : displayCommands)
|
||||
{
|
||||
m_standardCalculatorManager.SendCommand(static_cast<Command>(cmd));
|
||||
}
|
||||
if (snapshot->PrimaryDisplay->IsError)
|
||||
{
|
||||
SetPrimaryDisplay(snapshot->PrimaryDisplay->DisplayValue, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (snapshot->PrimaryDisplay->IsError)
|
||||
{
|
||||
// use case: user copy-pasted an invalid expression to Calculator and caused an error.
|
||||
SetPrimaryDisplay(snapshot->PrimaryDisplay->DisplayValue, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
// use case: there was no expression but user was inputing some numbers (including negative numbers).
|
||||
auto commands = GetCommandsFromExpressionCommands(ToUnderlying(snapshot->DisplayCommands));
|
||||
for (auto cmd : commands)
|
||||
{
|
||||
m_standardCalculatorManager.SendCommand(static_cast<Command>(cmd));
|
||||
}
|
||||
}
|
||||
SetExpressionDisplay(
|
||||
std::make_shared<RawTokenCollection>(rawTokens),
|
||||
std::make_shared<std::vector<std::shared_ptr<IExpressionCommand>>>(ToUnderlying(snapshot->ExpressionDisplay->Commands)));
|
||||
}
|
||||
SetPrimaryDisplay(snapshot->PrimaryDisplay->DisplayValue, snapshot->PrimaryDisplay->IsError);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue