Lazy load the states of GraphingMode Category

This commit is contained in:
tian-lt 2022-04-12 23:02:47 +08:00
commit caba2ec50d
4 changed files with 71 additions and 71 deletions

View file

@ -47,44 +47,53 @@ static constexpr int CURRENCY_ID = 16;
static constexpr int GRAPHING_ID = 17; static constexpr int GRAPHING_ID = 17;
// ^^^ THESE CONSTANTS SHOULD NEVER CHANGE ^^^ // ^^^ THESE CONSTANTS SHOULD NEVER CHANGE ^^^
wchar_t* towchar_t(int number) namespace
{ {
Platform::Agile<Windows::System::User^> CurrentUser;
std::mutex GraphingModeCheckMutex;
wchar_t* towchar_t(int number)
{
auto wstr = std::to_wstring(number); auto wstr = std::to_wstring(number);
return _wcsdup(wstr.c_str()); return _wcsdup(wstr.c_str());
} }
bool IsGraphingModeAvailable() bool IsGraphingModeAvailable()
{ {
static bool supportGraph = Windows::Foundation::Metadata::ApiInformation::IsMethodPresent("Windows.UI.Text.RichEditTextDocument", "GetMath"); static bool supportGraph = Windows::Foundation::Metadata::ApiInformation::IsMethodPresent("Windows.UI.Text.RichEditTextDocument", "GetMath");
return supportGraph; return supportGraph;
}
bool IsGraphingModeEnabled()
{
static bool isChecked = false;
static bool isEnabled = false;
std::scoped_lock<std::mutex> lock(GraphingModeCheckMutex);
if (isChecked)
{
return isEnabled;
}
else
{
if (IsGraphingModeAvailable())
{
auto namedPolicyData = NamedPolicy::GetPolicyFromPathForUser(
CurrentUser.Get(),
L"Education",
L"AllowGraphingCalculator");
isEnabled = namedPolicyData->GetBoolean();
}
else
{
isEnabled = false;
}
isChecked = true;
return isEnabled;
}
}
} }
Box<bool> ^ _isGraphingModeEnabledCached = nullptr;
bool IsGraphingModeEnabled(User ^ currentUser = nullptr)
{
if (!IsGraphingModeAvailable())
{
return false;
}
if (_isGraphingModeEnabledCached != nullptr)
{
return _isGraphingModeEnabledCached->Value;
}
if (!currentUser)
{
return true;
}
auto namedPolicyData = NamedPolicy::GetPolicyFromPathForUser(currentUser, L"Education", L"AllowGraphingCalculator");
_isGraphingModeEnabledCached = namedPolicyData->GetBoolean() == true;
return _isGraphingModeEnabledCached->Value;
}
Platform::Agile<Windows::System::User ^> NavCategoryStates::_currentUser;
// The order of items in this list determines the order of items in the menu. // The order of items in this list determines the order of items in the menu.
static std::vector<NavCategoryInitializer> s_categoryManifest = [] { static std::vector<NavCategoryInitializer> s_categoryManifest = [] {
@ -280,36 +289,6 @@ static std::vector<NavCategoryInitializer> s_categoryManifest = [] {
return res; return res;
}(); }();
void NavCategory::InitializeCategoryManifest(User ^ user)
{
//int i = 0;
//for (NavCategoryInitializer category : s_categoryManifest)
//{
// if (category.viewMode == ViewMode::Graphing)
// {
// auto navCatInit = s_categoryManifest.begin();
// std::advance(navCatInit, i);
// (*navCatInit).isEnabled = IsGraphingModeEnabled(user);
// break;
// }
// else
// {
// i++;
// }
//}
}
void NavCategory::UpdateGraphingModelManifest(Windows::System::User ^ user)
{
static bool isEnabled = IsGraphingModeEnabled(user);
std::find_if(
s_categoryManifest.begin(),
s_categoryManifest.end(),
[](const auto& category) { return category.viewMode == ViewMode::Graphing;})
->isEnabled = isEnabled;
}
bool NavCategory::IsCalculatorViewMode(ViewMode mode) bool NavCategory::IsCalculatorViewMode(ViewMode mode)
{ {
// Historically, Calculator modes are Standard, Scientific, and Programmer. // Historically, Calculator modes are Standard, Scientific, and Programmer.
@ -379,7 +358,8 @@ NavCategoryGroup::NavCategoryGroup(const NavCategoryGroupInitializer& groupIniti
void NavCategoryStates::SetCurrentUser(Windows::System::User^ user) void NavCategoryStates::SetCurrentUser(Windows::System::User^ user)
{ {
_currentUser = user; std::scoped_lock<std::mutex> lock(GraphingModeCheckMutex);
CurrentUser = user;
} }
IObservableVector<NavCategoryGroup ^> ^ NavCategoryStates::CreateMenuOptions() IObservableVector<NavCategoryGroup ^> ^ NavCategoryStates::CreateMenuOptions()
@ -578,11 +558,16 @@ bool NavCategoryStates::IsValidViewMode(ViewMode mode)
bool NavCategoryStates::IsViewModeEnabled(ViewMode mode) bool NavCategoryStates::IsViewModeEnabled(ViewMode mode)
{ {
const auto& citer = find_if( if (mode == ViewMode::Graphing)
cbegin(s_categoryManifest), {
cend(s_categoryManifest), return IsGraphingModeEnabled();
}
else
{
return std::any_of(
s_categoryManifest.cbegin(),
s_categoryManifest.cend(),
[mode](const auto& initializer) { return initializer.viewMode == mode && initializer.isEnabled; }); [mode](const auto& initializer) { return initializer.viewMode == mode && initializer.isEnabled; });
}
return citer != s_categoryManifest.cend();
} }

View file

@ -99,7 +99,7 @@ namespace CalculatorApp::ViewModel
PROPERTY_R(ViewMode, Mode); PROPERTY_R(ViewMode, Mode);
PROPERTY_R(Platform::String ^, AccessKey); PROPERTY_R(Platform::String ^, AccessKey);
PROPERTY_R(bool, SupportsNegative); PROPERTY_R(bool, SupportsNegative);
PROPERTY_R(bool, IsEnabled); PROPERTY_RW(bool, IsEnabled);
property Platform::String property Platform::String
^ AutomationId { Platform::String ^ get() { return m_Mode.ToString(); } } ^ AutomationId { Platform::String ^ get() { return m_Mode.ToString(); } }
@ -109,9 +109,6 @@ namespace CalculatorApp::ViewModel
static bool IsDateCalculatorViewMode(ViewMode mode); static bool IsDateCalculatorViewMode(ViewMode mode);
static bool IsConverterViewMode(ViewMode mode); static bool IsConverterViewMode(ViewMode mode);
static void InitializeCategoryManifest(Windows::System::User ^ user);
static void UpdateGraphingModelManifest(Windows::System::User ^ user);
internal : NavCategory( internal : NavCategory(
Platform::String ^ name, Platform::String ^ name,
Platform::String ^ automationName, Platform::String ^ automationName,
@ -181,9 +178,6 @@ namespace CalculatorApp::ViewModel
// Virtual key related // Virtual key related
static ViewMode GetViewModeForVirtualKey(MyVirtualKey virtualKey); static ViewMode GetViewModeForVirtualKey(MyVirtualKey virtualKey);
static void GetCategoryAcceleratorKeys(Windows::Foundation::Collections::IVector<MyVirtualKey> ^ resutls); static void GetCategoryAcceleratorKeys(Windows::Foundation::Collections::IVector<MyVirtualKey> ^ resutls);
private:
static Platform::Agile<Windows::System::User ^> _currentUser;
}; };
} }
} }

View file

@ -19,6 +19,7 @@ using Windows.ApplicationModel.Activation;
using Windows.ApplicationModel.Core; using Windows.ApplicationModel.Core;
using Windows.Foundation; using Windows.Foundation;
using Windows.Storage; using Windows.Storage;
using Windows.System;
using Windows.UI.Core; using Windows.UI.Core;
using Windows.UI.StartScreen; using Windows.UI.StartScreen;
using Windows.UI.ViewManagement; using Windows.UI.ViewManagement;
@ -86,6 +87,10 @@ namespace CalculatorApp
m_preLaunched = true; m_preLaunched = true;
} }
NavCategoryStates.SetCurrentUser(args.User); NavCategoryStates.SetCurrentUser(args.User);
// It takes time to check GraphingMode at the 1st time. So, do it in a background thread
Task.Run(() => NavCategoryStates.IsViewModeEnabled(ViewMode.Graphing));
OnAppLaunch(args, args.Arguments); OnAppLaunch(args, args.Arguments);
} }
@ -411,7 +416,7 @@ namespace CalculatorApp
foreach (NavCategory option in calculatorOptions.Categories) foreach (NavCategory option in calculatorOptions.Categories)
{ {
if (!option.IsEnabled) if (!NavCategoryStates.IsViewModeEnabled(option.Mode))
{ {
continue; continue;
} }

View file

@ -147,6 +147,22 @@ namespace CalculatorApp
NavViewCategoriesSource.Clear(); NavViewCategoriesSource.Clear();
NavViewCategoriesSource = ExpandNavViewCategoryGroups(Model.Categories); NavViewCategoriesSource = ExpandNavViewCategoryGroups(Model.Categories);
}; };
_ = Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
{
var graphCategory = (NavCategory)NavViewCategoriesSource.Find(x =>
{
if(x is NavCategory category)
{
return category.Mode == ViewMode.Graphing;
}
else
{
return false;
}
});
graphCategory.IsEnabled = NavCategoryStates.IsViewModeEnabled(ViewMode.Graphing);
});
} }
private List<object> ExpandNavViewCategoryGroups(IEnumerable<NavCategoryGroup> groups) private List<object> ExpandNavViewCategoryGroups(IEnumerable<NavCategoryGroup> groups)