From 1645cfddcd46807aea0fb9ac6c25d323a602202f Mon Sep 17 00:00:00 2001 From: Tian Liao Date: Thu, 24 Oct 2024 11:32:26 +0800 Subject: [PATCH] stash changes --- src/CalcViewModel/ApplicationViewModel.cpp | 6 +- src/CalcViewModel/ApplicationViewModel.h | 6 +- src/CalcViewModel/Snapshots.cpp | 78 ++++++++----------- src/CalcViewModel/Snapshots.h | 60 +++++++++++++- .../StandardCalculatorViewModel.cpp | 20 ++--- .../StandardCalculatorViewModel.h | 8 +- src/Calculator/Calculator.csproj | 4 + src/Calculator/Common/LaunchArguments.cs | 4 +- src/Calculator/Utils/SerdeUtils.cs | 49 ++++++++++++ src/Calculator/Views/MainPage.xaml.cs | 3 + 10 files changed, 167 insertions(+), 71 deletions(-) create mode 100644 src/Calculator/Utils/SerdeUtils.cs diff --git a/src/CalcViewModel/ApplicationViewModel.cpp b/src/CalcViewModel/ApplicationViewModel.cpp index 1d4e6e12..190e7271 100644 --- a/src/CalcViewModel/ApplicationViewModel.cpp +++ b/src/CalcViewModel/ApplicationViewModel.cpp @@ -512,9 +512,9 @@ void ApplicationViewModel::Categories::set(IObservableVector } } -ApplicationSnapshot ^ ApplicationViewModel::Snapshot::get() +CalculatorApp::ViewModel::Snapshot::ApplicationSnapshot ^ ApplicationViewModel::Snapshot::get() { - auto snapshot = ref new ApplicationSnapshot(); + auto snapshot = ref new CalculatorApp::ViewModel::Snapshot::ApplicationSnapshot(); snapshot->Mode = static_cast(Mode); if (m_CalculatorViewModel != nullptr && m_mode == ViewMode::Standard) { @@ -556,7 +556,7 @@ void ApplicationViewModel::Initialize(ViewMode mode) } } -void ApplicationViewModel::Initialize(ApplicationSnapshot ^ snapshot) +void ApplicationViewModel::Initialize(CalculatorApp::ViewModel::Snapshot::ApplicationSnapshot ^ snapshot) { // TODO: restore Initialize(static_cast(snapshot->Mode)); diff --git a/src/CalcViewModel/ApplicationViewModel.h b/src/CalcViewModel/ApplicationViewModel.h index 05cd1fbd..1cb2f2df 100644 --- a/src/CalcViewModel/ApplicationViewModel.h +++ b/src/CalcViewModel/ApplicationViewModel.h @@ -20,7 +20,7 @@ namespace CalculatorApp [Windows::Foundation::Metadata::DefaultOverload] void Initialize(CalculatorApp::ViewModel::Common::ViewMode mode); // Use for first init, use deserialize for rehydration - void Initialize(ApplicationSnapshot^ snapshot); + void Initialize(CalculatorApp::ViewModel::Snapshot::ApplicationSnapshot^ snapshot); OBSERVABLE_OBJECT(); OBSERVABLE_PROPERTY_RW(StandardCalculatorViewModel ^, CalculatorViewModel); @@ -73,9 +73,9 @@ namespace CalculatorApp } } - property ApplicationSnapshot ^ Snapshot + property CalculatorApp::ViewModel::Snapshot::ApplicationSnapshot ^ Snapshot { - ApplicationSnapshot ^ get(); + CalculatorApp::ViewModel::Snapshot::ApplicationSnapshot ^ get(); } static property Platform::String ^ LaunchedLocalSettings diff --git a/src/CalcViewModel/Snapshots.cpp b/src/CalcViewModel/Snapshots.cpp index a7ffccea..ff651dd4 100644 --- a/src/CalcViewModel/Snapshots.cpp +++ b/src/CalcViewModel/Snapshots.cpp @@ -9,78 +9,64 @@ #include "CalcManager/ExpressionCommand.h" #include "Snapshots.h" -namespace +namespace CalculatorApp::ViewModel::Snapshot { - ref struct UnaryCommand sealed : public CalculatorApp::ViewModel::ICalcManagerIExprCommand + UnaryCommand::UnaryCommand(Windows::Foundation::Collections::IVectorView ^ cmds) { - internal :; - std::vector Commands; - }; + for (auto cmd : cmds) + { + m_cmds.push_back(cmd); + } + } - ref struct BinaryCommand sealed : public CalculatorApp::ViewModel::ICalcManagerIExprCommand + Windows::Foundation::Collections::IVectorView ^ UnaryCommand::Commands::get() { - internal :; - int Command; - }; + return ref new Platform::Collections::VectorView(m_cmds); + } - ref struct OperandCommand sealed : public CalculatorApp::ViewModel::ICalcManagerIExprCommand + OperandCommand::OperandCommand(bool isNegative, bool isDecimal, bool isSciFmt, Windows::Foundation::Collections::IVectorView ^ cmds) { - internal :; - bool IsNegative; - bool IsDecimalPresent; - bool IsSciFmt; - std::vector Commands; - }; + IsNegative = isNegative; + IsDecimalPresent = isDecimal; + IsSciFmt = isSciFmt; + for (auto cmd : cmds) + { + m_cmds.push_back(cmd); + } + } - ref struct Parentheses sealed : public CalculatorApp::ViewModel::ICalcManagerIExprCommand - { - internal :; - int Command; - }; - -} // namespace - -namespace CalculatorApp::ViewModel -{ ICalcManagerIExprCommand ^ CreateExprCommand(const IExpressionCommand* exprCmd) { switch (exprCmd->GetCommandType()) { case CalculationManager::CommandType::UnaryCommand: { auto cmd = static_cast(exprCmd); - auto result = ref new UnaryCommand(); + std::vector cmdlist; for (auto& subcmd : *cmd->GetCommands()) { - result->Commands.push_back(subcmd); + cmdlist.push_back(subcmd); } - return result; + return ref new UnaryCommand(std::move(cmdlist)); } case CalculationManager::CommandType::BinaryCommand: { auto cmd = static_cast(exprCmd); - auto result = ref new BinaryCommand(); - result->Command = cmd->GetCommand(); - return result; + return ref new BinaryCommand(cmd->GetCommand()); } case CalculationManager::CommandType::OperandCommand: { auto cmd = static_cast(exprCmd); - auto result = ref new OperandCommand(); - result->IsNegative = cmd->IsNegative(); - result->IsDecimalPresent = cmd->IsDecimalPresent(); - result->IsSciFmt = cmd->IsSciFmt(); + std::vector cmdlist; for (auto& subcmd : *cmd->GetCommands()) { - result->Commands.push_back(subcmd); + cmdlist.push_back(subcmd); } - return result; + return ref new OperandCommand(cmd->IsNegative(), cmd->IsDecimalPresent(), cmd->IsSciFmt(), std::move(cmdlist)); } case CalculationManager::CommandType::Parentheses: { auto cmd = static_cast(exprCmd); - auto result = ref new Parentheses(); - result->Command = cmd->GetCommand(); - return result; + return ref new Parentheses(cmd->GetCommand()); } default: throw std::logic_error{ "unhandled command type." }; @@ -161,13 +147,13 @@ namespace CalculatorApp::ViewModel { if (auto unary = dynamic_cast(cmdEntry); unary != nullptr) { - if (unary->Commands.size() == 1) + if (unary->m_cmds.size() == 1) { - result.push_back(std::make_shared(unary->Commands[0])); + result.push_back(std::make_shared(unary->m_cmds[0])); } - else if (unary->Commands.size() == 2) + else if (unary->m_cmds.size() == 2) { - result.push_back(std::make_shared(unary->Commands[0], unary->Commands[1])); + result.push_back(std::make_shared(unary->m_cmds[0], unary->m_cmds[1])); } else { @@ -184,7 +170,7 @@ namespace CalculatorApp::ViewModel } else if (auto operand = dynamic_cast(cmdEntry); operand != nullptr) { - auto subcmds = std::make_shared>(operand->Commands); + auto subcmds = std::make_shared>(operand->m_cmds); result.push_back(std::make_shared(std::move(subcmds), operand->IsNegative, operand->IsDecimalPresent, operand->IsSciFmt)); } } diff --git a/src/CalcViewModel/Snapshots.h b/src/CalcViewModel/Snapshots.h index 558e302f..b75d424c 100644 --- a/src/CalcViewModel/Snapshots.h +++ b/src/CalcViewModel/Snapshots.h @@ -7,20 +7,70 @@ #include "CalcManager/CalculatorManager.h" -namespace CalculatorApp::ViewModel +namespace CalculatorApp::ViewModel::Snapshot { public interface struct ICalcManagerIExprCommand { }; + ref struct UnaryCommand sealed : public ICalcManagerIExprCommand + { + property Windows::Foundation::Collections::IVectorView ^ Commands { Windows::Foundation::Collections::IVectorView ^ get(); }; + + explicit UnaryCommand(Windows::Foundation::Collections::IVectorView ^ cmds); + + internal :; + explicit UnaryCommand(std::vector cmds) + : m_cmds(std::move(cmds)) + { + } + std::vector m_cmds; + }; + + ref struct BinaryCommand sealed : public ICalcManagerIExprCommand + { + property int Command; + explicit BinaryCommand(int cmd) + { + Command = cmd; + } + }; + + ref struct OperandCommand sealed : public ICalcManagerIExprCommand + { + property bool IsNegative; + property bool IsDecimalPresent; + property bool IsSciFmt; + property Windows::Foundation::Collections::IVectorView ^ Commands { Windows::Foundation::Collections::IVectorView ^ get(); }; + + explicit OperandCommand(bool isNegative, bool isDecimal, bool isSciFmt, Windows::Foundation::Collections::IVectorView ^ cmds); + internal :; + explicit OperandCommand(bool isNegative, bool isDecimal, bool isSciFmt, std::vector cmds) + { + IsNegative = isNegative; + IsDecimalPresent = isDecimal; + IsSciFmt = isSciFmt; + m_cmds = std::move(cmds); + } + std::vector m_cmds; + }; + + ref struct Parentheses sealed : public ICalcManagerIExprCommand + { + property int Command; + explicit Parentheses(int cmd) + { + Command = cmd; + } + }; + public ref struct CalcManagerHistoryToken sealed { property Platform::String ^ OpCodeName; // mandatory property int CommandIndex; - internal :; explicit CalcManagerHistoryToken(Platform::String ^ opCodeName, int cmdIndex); }; @@ -32,6 +82,12 @@ public property Platform::String ^ Expression; // mandatory property Platform::String ^ Result; // mandatory + // explicit CalcManagerHistoryItem( + // Windows::Foundation::Collections::IVector ^ tokens, + // Windows::Foundation::Collections::IVector ^ commands, + // Platform::String ^ expression, + // Platform::String ^ result); + internal :; explicit CalcManagerHistoryItem(const CalculationManager::HISTORYITEM& item); }; diff --git a/src/CalcViewModel/StandardCalculatorViewModel.cpp b/src/CalcViewModel/StandardCalculatorViewModel.cpp index 844a063c..9d773974 100644 --- a/src/CalcViewModel/StandardCalculatorViewModel.cpp +++ b/src/CalcViewModel/StandardCalculatorViewModel.cpp @@ -115,7 +115,7 @@ namespace CalculatorResourceKeys StringReference DisplayCopied(L"Display_Copied"); } -StandardCalculatorViewModel::StandardCalculatorViewModel(StandardCalculatorSnapshot ^ snapshot) +StandardCalculatorViewModel::StandardCalculatorViewModel(CalculatorApp::ViewModel::Snapshot::StandardCalculatorSnapshot ^ snapshot) : m_DisplayValue(L"0") , m_DecimalDisplayValue(L"0") , m_HexDisplayValue(L"0") @@ -208,7 +208,7 @@ StandardCalculatorViewModel::StandardCalculatorViewModel(StandardCalculatorSnaps { using RawTokenCollection = std::vector>; RawTokenCollection rawTokens; - for (CalcManagerHistoryToken ^ token : snapshot->ExpressionDisplay->Tokens) + for (CalculatorApp::ViewModel::Snapshot::CalcManagerHistoryToken ^ token : snapshot->ExpressionDisplay->Tokens) { rawTokens.push_back(std::pair{ token->OpCodeName->Data(), token->CommandIndex }); } @@ -1823,18 +1823,18 @@ void StandardCalculatorViewModel::SetBitshiftRadioButtonCheckedAnnouncement(Plat Announcement = CalculatorAnnouncement::GetBitShiftRadioButtonCheckedAnnouncement(announcement); } -StandardCalculatorSnapshot ^ StandardCalculatorViewModel::GetSnapshot() const +CalculatorApp::ViewModel::Snapshot::StandardCalculatorSnapshot ^ StandardCalculatorViewModel::GetSnapshot() const { - auto result = ref new StandardCalculatorSnapshot(); - result->CalcManager = ref new CalcManagerSnapshot(m_standardCalculatorManager); - result->PrimaryDisplay = ref new PrimaryDisplaySnapshot(m_DisplayValue, m_IsInError); - result->ExpressionDisplay = ref new ExpressionDisplaySnapshot(*m_tokens, *m_commands); - result->DisplayCommands = ref new Platform::Collections::Vector(); + auto result = ref new CalculatorApp::ViewModel::Snapshot::StandardCalculatorSnapshot(); + result->CalcManager = ref new CalculatorApp::ViewModel::Snapshot::CalcManagerSnapshot(m_standardCalculatorManager); + result->PrimaryDisplay = ref new CalculatorApp::ViewModel::Snapshot::PrimaryDisplaySnapshot(m_DisplayValue, m_IsInError); + result->ExpressionDisplay = ref new CalculatorApp::ViewModel::Snapshot::ExpressionDisplaySnapshot(*m_tokens, *m_commands); + result->DisplayCommands = ref new Platform::Collections::Vector(); for (auto cmd : m_standardCalculatorManager.GetDisplayCommandsSnapshot()) { - result->DisplayCommands->Append(CreateExprCommand(cmd.get())); + result->DisplayCommands->Append(CalculatorApp::ViewModel::Snapshot::CreateExprCommand(cmd.get())); } - return nullptr; + return result; } // StandardCalculatorSnapshot StandardCalculatorViewModel::GetStandardCalculatorSnapshot() const diff --git a/src/CalcViewModel/StandardCalculatorViewModel.h b/src/CalcViewModel/StandardCalculatorViewModel.h index 5e17eee4..8026e709 100644 --- a/src/CalcViewModel/StandardCalculatorViewModel.h +++ b/src/CalcViewModel/StandardCalculatorViewModel.h @@ -268,8 +268,8 @@ namespace CalculatorApp } } - property StandardCalculatorSnapshot - ^ Snapshot { StandardCalculatorSnapshot ^ get() { return GetSnapshot(); } } + property CalculatorApp::ViewModel::Snapshot::StandardCalculatorSnapshot + ^ Snapshot { CalculatorApp::ViewModel::Snapshot::StandardCalculatorSnapshot ^ get() { return GetSnapshot(); } } // Used by unit tests void @@ -327,10 +327,10 @@ namespace CalculatorApp } internal :; - explicit StandardCalculatorViewModel(StandardCalculatorSnapshot ^ snapshot = nullptr); + explicit StandardCalculatorViewModel(CalculatorApp::ViewModel::Snapshot::StandardCalculatorSnapshot ^ snapshot = nullptr); private: - StandardCalculatorSnapshot ^ GetSnapshot() const; + CalculatorApp::ViewModel::Snapshot::StandardCalculatorSnapshot ^ GetSnapshot() const; void SetMemorizedNumbers(const std::vector& memorizedNumbers); void UpdateProgrammerPanelDisplay(); void HandleUpdatedOperandData(CalculationManager::Command cmdenum); diff --git a/src/Calculator/Calculator.csproj b/src/Calculator/Calculator.csproj index 6c73ba68..589e2fe8 100644 --- a/src/Calculator/Calculator.csproj +++ b/src/Calculator/Calculator.csproj @@ -167,6 +167,7 @@ + EquationStylePanelControl.xaml @@ -799,6 +800,9 @@ 6.2.14 + + 8.0.5 + diff --git a/src/Calculator/Common/LaunchArguments.cs b/src/Calculator/Common/LaunchArguments.cs index ee643fab..1906c14f 100644 --- a/src/Calculator/Common/LaunchArguments.cs +++ b/src/Calculator/Common/LaunchArguments.cs @@ -1,7 +1,5 @@ -using System; using Windows.ApplicationModel.Activation; -using Windows.ApplicationModel.UserActivities; -using CalculatorApp.ViewModel; +using CalculatorApp.ViewModel.Snapshot; namespace CalculatorApp { diff --git a/src/Calculator/Utils/SerdeUtils.cs b/src/Calculator/Utils/SerdeUtils.cs new file mode 100644 index 00000000..053f2a54 --- /dev/null +++ b/src/Calculator/Utils/SerdeUtils.cs @@ -0,0 +1,49 @@ +using System.Collections.Generic; +using System.Linq; +using System.Text.Json.Serialization; +using CalculatorApp.ViewModel.Snapshot; + +namespace CalculatorApp +{ + internal class CalcManagerHistoryTokenAlias + { + [JsonIgnore] + public CalcManagerHistoryToken Value; + + [JsonPropertyName("t")] + public string OpCodeName + { + get => Value.OpCodeName; + set => Value.OpCodeName = value; + } + [JsonPropertyName("c")] + public int CommandIndex { get => Value.CommandIndex; } + } + + internal class CalcManagerIExprCommandAlias + { + [JsonPropertyName("t")] + public string Type; + + [JsonPropertyName("c")] + public string CmdString; + } + + internal class CalcManagerHistoryItemAlias + { + [JsonIgnore] + public CalcManagerHistoryItem Value; + + [JsonPropertyName("t")] + public IList Tokens + { + get => Value.Tokens.Select(x => new CalcManagerHistoryTokenAlias { Value = x }).ToList(); + set => Value.Tokens = value.Select(x => new CalcManagerHistoryToken(x.OpCodeName, x.CommandIndex)).ToList(); + } + + //public IList Commands + //{ + // get => Value.Commands; + //} + } +} diff --git a/src/Calculator/Views/MainPage.xaml.cs b/src/Calculator/Views/MainPage.xaml.cs index 0a3035fd..5f656db2 100644 --- a/src/Calculator/Views/MainPage.xaml.cs +++ b/src/Calculator/Views/MainPage.xaml.cs @@ -23,6 +23,7 @@ using CalculatorApp.ViewModel.Common; using CalculatorApp.ViewModel.Common.Automation; using wuxc = Windows.UI.Xaml.Controls; +using System.Text.Json; namespace CalculatorApp { @@ -69,6 +70,8 @@ namespace CalculatorApp } var channel = UserActivityChannel.GetDefault(); var activity = await channel.GetOrCreateUserActivityAsync($"{Guid.NewGuid()}"); + var s = Model.Snapshot; + var j = JsonSerializer.Serialize(s); activity.ActivationUri = new Uri($"ms-calculator:snapshot/TODO"); activity.IsRoamable = false; var resProvider = AppResourceProvider.GetInstance();