From 37d287373fdaab0bf0c3cee8149b810e90ac4707 Mon Sep 17 00:00:00 2001 From: greedyAI Date: Wed, 20 Jan 2021 14:54:24 -0800 Subject: [PATCH] Better fix --- src/CalcViewModel/Common/NavCategory.cpp | 48 ++++++++++++------------ src/CalcViewModel/Common/NavCategory.h | 40 ++++++++++---------- src/Calculator/App.xaml.cpp | 2 +- 3 files changed, 46 insertions(+), 44 deletions(-) diff --git a/src/CalcViewModel/Common/NavCategory.cpp b/src/CalcViewModel/Common/NavCategory.cpp index 1bd0d15c..d567f1dc 100644 --- a/src/CalcViewModel/Common/NavCategory.cpp +++ b/src/CalcViewModel/Common/NavCategory.cpp @@ -62,34 +62,29 @@ bool IsGraphingModeAvailable() Box ^ _isGraphingModeEnabledCached = nullptr; bool IsGraphingModeEnabled(User ^ firstUser = nullptr) { - if (firstUser) + if (!IsGraphingModeAvailable()) { - if (!IsGraphingModeAvailable()) - { - return false; - } - - if (_isGraphingModeEnabledCached != nullptr) - { - return _isGraphingModeEnabledCached->Value; - } - - auto namedPolicyData = NamedPolicy::GetPolicyFromPathForUser(firstUser, L"Education", L"AllowGraphingCalculator"); - _isGraphingModeEnabledCached = namedPolicyData->GetBoolean() == true; - - return _isGraphingModeEnabledCached->Value; + return false; } - else + + if (_isGraphingModeEnabledCached != nullptr) { return _isGraphingModeEnabledCached->Value; } + + if (!firstUser) + { + return false; + } + + auto namedPolicyData = NamedPolicy::GetPolicyFromPathForUser(firstUser, L"Education", L"AllowGraphingCalculator"); + _isGraphingModeEnabledCached = namedPolicyData->GetBoolean() == true; + + return _isGraphingModeEnabledCached->Value; } // The order of items in this list determines the order of items in the menu. -static list s_categoryManifest; - -void NavCategory::CreateCategoryManifest(User ^ user) -{ +static list s_categoryManifest = [] { auto res = list{ NavCategoryInitializer{ ViewMode::Standard, STANDARD_ID, L"Standard", @@ -115,7 +110,7 @@ void NavCategory::CreateCategoryManifest(User ^ user) bool supportGraphingCalculator = IsGraphingModeAvailable(); if (supportGraphingCalculator) { - bool isEnabled = IsGraphingModeEnabled(user); + bool isEnabled = IsGraphingModeEnabled(); res.push_back(NavCategoryInitializer{ ViewMode::Graphing, GRAPHING_ID, L"Graphing", @@ -280,8 +275,15 @@ void NavCategory::CreateCategoryManifest(User ^ user) nullptr, SUPPORTS_NEGATIVE, true } }); - s_categoryManifest = res; -} + return res; +}(); + +void NavCategory::InitializeCategoryManifest(User ^ user) +{ + auto navCatInit = s_categoryManifest.begin(); + std::advance(navCatInit, 2); + (*navCatInit).isEnabled = IsGraphingModeEnabled(user); + } // This function should only be used when storing the mode to app data. int NavCategory::Serialize(ViewMode mode) diff --git a/src/CalcViewModel/Common/NavCategory.h b/src/CalcViewModel/Common/NavCategory.h index 3d9e2296..c0ce95f0 100644 --- a/src/CalcViewModel/Common/NavCategory.h +++ b/src/CalcViewModel/Common/NavCategory.h @@ -59,15 +59,15 @@ namespace CalculatorApp private struct NavCategoryInitializer { - NavCategoryInitializer( + constexpr NavCategoryInitializer( ViewMode mode, int id, - wchar_t * name, - wchar_t * nameKey, - wchar_t * glyph, + wchar_t const* name, + wchar_t const* nameKey, + wchar_t const* glyph, CategoryGroupType group, MyVirtualKey vKey, - wchar_t * aKey, + wchar_t const* aKey, bool categorySupportsNegative, bool enabled) : viewMode(mode) @@ -83,22 +83,22 @@ namespace CalculatorApp { } - ViewMode viewMode; - int serializationId; - wchar_t* friendlyName; - wchar_t* nameResourceKey; - wchar_t* glyph; - CategoryGroupType groupType; - MyVirtualKey virtualKey; - wchar_t* accessKey; - bool supportsNegative; + const ViewMode viewMode; + const int serializationId; + const wchar_t* const friendlyName; + const wchar_t* const nameResourceKey; + const wchar_t* const glyph; + const CategoryGroupType groupType; + const MyVirtualKey virtualKey; + const wchar_t* const accessKey; + const bool supportsNegative; bool isEnabled; }; private struct NavCategoryGroupInitializer { - NavCategoryGroupInitializer(CategoryGroupType t, wchar_t * h, wchar_t * n, wchar_t * a) + constexpr NavCategoryGroupInitializer(CategoryGroupType t, wchar_t const* h, wchar_t const* n, wchar_t const* a) : type(t) , headerResourceKey(h) , modeResourceKey(n) @@ -106,10 +106,10 @@ namespace CalculatorApp { } - CategoryGroupType type; - wchar_t* headerResourceKey; - wchar_t* modeResourceKey; - wchar_t* automationResourceKey; + const CategoryGroupType type; + const wchar_t* headerResourceKey; + const wchar_t* modeResourceKey; + const wchar_t* automationResourceKey; }; [Windows::UI::Xaml::Data::Bindable] public ref class NavCategory sealed : public Windows::UI::Xaml::Data::INotifyPropertyChanged @@ -140,7 +140,7 @@ namespace CalculatorApp static bool IsDateCalculatorViewMode(ViewMode mode); static bool IsConverterViewMode(ViewMode mode); - static void CreateCategoryManifest(Windows::System::User ^ user); + static void InitializeCategoryManifest(Windows::System::User ^ user); static Platform::String ^ GetFriendlyName(ViewMode mode); static Platform::String ^ GetNameResourceKey(ViewMode mode); diff --git a/src/Calculator/App.xaml.cpp b/src/Calculator/App.xaml.cpp index f11c1f7f..39f8e7fc 100644 --- a/src/Calculator/App.xaml.cpp +++ b/src/Calculator/App.xaml.cpp @@ -198,7 +198,7 @@ void App::OnLaunched(LaunchActivatedEventArgs ^ args) // If the app got pre-launch activated, then save that state in a flag m_preLaunched = true; } - NavCategory::CreateCategoryManifest(args->User); + NavCategory::InitializeCategoryManifest(args->User); OnAppLaunch(args, args->Arguments); }