Files
calculator/src/CalcViewModel/Common/Utils.h
Tian L fdae1000fc Hello C# - Migrating the codebase from C++/CX to C# (Phase 1) (#1598)
* Hello C# - Going to an official feature branch (#1544)

* change CalcViewModel into a WindowsRuntimeComponent project (#5)

* change CalcViewModel into a WindowsRuntimeComponent project

* remove the old UI codebase (#6)

* initially migrated C# codebase by tian (#7)

* initial migrated C# codebase by tian

* format the codebase

* resolve comments

* undo: modifications on UI test project

* Remove the blocks that have more than 1 empty line.

* Register DP using keyword 'nameof'

* C# Migration: Initially migrated C# codebase by Han (#8)

* C# Migration: Initially migrated C# codebase by Han

* Resolved comments and misssing asset

* Added three files to Calculator project

* Added TODO comment and updated Object

* NavCategory: temporary resolution of the hang issue (#9)

* Updated CalcViewModel and missing files (#10)

* Updated CalcViewModel and WinMeta

* Added Calculator.rc

* Resolved comment for InitializeLocalizationSettings

* add: views/unitconverter.xaml (#11)

* add: views/unitconverter.xaml

* format the code

* remove the extra empty line

* add an empty line

* check null before invoking event handlers (#12)

* fix problems of the migration of OBSERVABLE_PROPERTY_RW (#13)

* fixes crash in MathRichEditBox.ctor() (#14)

* fixes crash in MathRichEditBox.ctor()

* typo

* Update azure-pipelines.ci.yaml for Azure Pipelines

* Added a link copy of CalcViewModel to temporarily pass Unit Tests (#16)

* Updated CalcViewModelCopyForUT configuration (#17)

* changes output path of the UI project to align with other projects (#15)

* fixes EETypeLoadException issue: export class DelegateCommand (#18)

* fixes EETypeLoadException issue: export class DelegateCommand

* weak-reference in C++/CX

* WeakRef in C# codebase

* UTF-8-BOM

* spaces in macro

* resolve some comments from the offline review

* format

* rename file

* fixes the memory list issue (#20)

* fixes a wrongly migrated property

* UTF-8-BOM

* fixes up the crash of type casting (#21)

* Update localized strings 2021-01-04 (#1458) (#23)

(cherry picked from commit cdcb95656d)

Co-authored-by: Matt Cooley <macool@microsoft.com>

* Fixup tests (#1429) (#24)

- Removed unneeded "ToString" calls
- Fixed typos
- Renamed "fEButtonState" to "FEButtonState"

(cherry picked from commit 66ad328d00)

Co-authored-by: N <71219152+PokeCodec@users.noreply.github.com>

* Update graph internal engine verseion (#1466) (#25)

(cherry picked from commit 0048dcb500)

Co-authored-by: Quentin Al-Timimi <27322516+quentin987@users.noreply.github.com>

* Turn off DFS file shares in internal build system (#1470) (#26)

(cherry picked from commit 885fa23a89)

Co-authored-by: Matt Cooley <macool@microsoft.com>

* Improve clarity of math expressions in history for Standard Calculator (feature #138) (#1453) (#27)

* Implemented feature & added unit tests

* Fixed more unit/ui tests

* Refactored tests

* Update HistoryTests.cpp

* Update HistoryTests.cpp

* Update HistoryTests.cpp

* Update HistoryTests.cpp

* Update HistoryTests.cpp

* Update HistoryTests.cpp

* Update HistoryTests.cpp

* Update HistoryTests.cpp

(cherry picked from commit 565e3e2714)

Co-authored-by: Wei (Waley) Zhang <waley.zhang@microsoft.com>

* Adds unit-test cases for NarratorAnnouncement after fixing issue #1386 (#1469) (#28)

* fix bug: No confirmation is announced by the narrator after activating 'Remove equation' button #1386

* Unit Test: Add NarratorAnnouncementUnitTests

Co-authored-by: tain <tankle_@hotmail.com>
(cherry picked from commit 9d8e2ad18c)

Co-authored-by: Tian L <60599517+MSFT-Tilia@users.noreply.github.com>

* Move localization pipeline sync schedule to the YAML file (#1478) (#30)

(cherry picked from commit 007eccd940)

Co-authored-by: Matt Cooley <macool@microsoft.com>

* remove the strong reference carried from delegate (#32)

* Remove the finalizer of ControlSizeTrigger (#31)

* Normalize the namespace of CalcViewModel (#33)

* ViewMode: arrange namespaces

* UI build pass

* run release

* UT build pass

* pass build

* resolve comment: make the diff results cleaner

* resolve comment: make the diff results cleaner (2)

* resolve comment: make the diff results cleaner (3)

* resolve comment: move impl into a namespace

* update: spaces

* update: CalculatorButtonUser.h

* UTF-8 to UTF-8-BOM

* remove ViewState.h/.cpp from CalcViewModel path

* revert changes for NavCategory.cpp

* remove extra space

* remove UCM

* remove BOM

* Fixed a graphing calculator "permissions" bug caused by PR #1426 (#1471) (#34)

- The PR #1426 can cause a crash when no users are returned via `User::FindAllAsync(UserType::LocalUser)` when subsequently trying to access the first user. The existing code also does not guarantee that the returned user is the currently active user.
- This fix retrieves the user that opened the app and passes this user into a function to check if this user has the proper permissions to access the graphing mode. This makes sense since the active user is indistinguishable (at least from the app's perspective) to the user who opened the app. This user's permissions are then propagated downwards to properly set up the navigation menu of the app.
- Implementation detail worth pointing out: `s_categoryManifest` is what is used to populate the navigation menu of the app, but this variable is static by design, so a separate function was written to override the appropriate `isEnabled` value in `s_categoryManifest`. This function is called by `onLaunched`.

- Manual testing

Co-authored-by: Wei (Waley) Zhang <waley.zhang@microsoft.com>

* fixes up a bug (#35)

* fix csproj (#37)

Co-authored-by: hanzhang54 <zhangh@microsoft.com>
Co-authored-by: Matt Cooley <macool@microsoft.com>
Co-authored-by: N <71219152+PokeCodec@users.noreply.github.com>
Co-authored-by: Quentin Al-Timimi <27322516+quentin987@users.noreply.github.com>
Co-authored-by: Wei (Waley) Zhang <waley.zhang@microsoft.com>
Co-authored-by: Tian L <60599517+MSFT-Tilia@users.noreply.github.com>

* **BYPASS_SECRET_SCANNING** (#1546)

* Fixes a bug about the UI of expression tokens (#1547)

* fix

* [FeatureBranch] Fixes x86/ARM/ARM64 builds for the CI-Pipeline (#1550)

* **BYPASS_SECRET_SCANNING**

* fix

* fixes x86/ARM/ARM64 builds for CI-Pipeline

* Add headers missing for compilation with GCC (#1468) (#1551)

Things that required such update included:
* `wstringstream`
* `setprecision`
* `SCODE_CODE`, `E_BOUNDS`
* Various SAL macros

Co-authored-by: Michał Janiszewski <janisozaur@users.noreply.github.com>

* Update nuget.config file (#1486) (#1552)

Co-authored-by: Matt Cooley <macool@microsoft.com>

* Fixes up some simple miscellaneous TODO items (#1556)

* #DEBUG is a known C# preprocessor directive

* So far, we haven't observed the problem described in the comment from C# async

* fixes misc TODO items

* resolve some warnings (#1564)

* Add internal CI pipeline (#1553) (#1565)

* Add CI-internal pipeline

* No ARM64, to match release

Co-authored-by: Matt Cooley <macool@microsoft.com>

* Temporarily disable PGO NuGet package (#1510) (#1566)

Co-authored-by: Matt Cooley <macool@microsoft.com>

* [C# Calc]Removes WinMeta.cs (#1567)

* remove WinMeta.cs

* undo a trivial change

* UTF-8 BOM

* [C# Calc] Reverts some changes for Currency constants (#1570)

* Update2108release - experimental (#1572)

* adjusts Calculator.csproj (#1571)

* fixes BinSkim problems (#1573)

* fixes an issue around line style (#1575)

* fixes the missed NULLs (#1576) (#1578)

* Fix the Missing Part in Unit Converter Constructor (#1579)

* fixes: calculator doesn't remember its previous mode (#1580)

* Fixes: GraphingNumber doesn't work correctly (#1585)

* fixes: GraphingNumber doesn't work correctly

* Avoid crashing

* fixes binding (#1586)

* resolve TODO items (#1589)

* Improving keyboard support in VariableValueSlider (#1559) (#1595)

Co-authored-by: Dave Grochocki <grochocki@users.noreply.github.com>

* [C# Calc] Fixes: Keep the value away from getting rounded in Graphing Mode (#1596)

* keep the value away from getting rounded

* set the display precision to 6 to align with C++ impl

* fixes the button-light-up time (#1597)

* fixes up merging flaws

* Update2108release

* fixes (#1599)

* keep master for ci pipeline

* remove the Resources filter from CalcViewModel project

* removes `that` since `this` can be captured automatically

* AppxBundlePlatforms

* StampAssemblyInfo

* removes PreferredToolArchitecture

* Change the arg AppVersion into Version

* Change the arg AppVersion into Version

* from Calculator.rc to AssemblyInfo.cs

* Adds assembly-info

Co-authored-by: hanzhang54 <zhangh@microsoft.com>
Co-authored-by: Matt Cooley <macool@microsoft.com>
Co-authored-by: N <71219152+PokeCodec@users.noreply.github.com>
Co-authored-by: Quentin Al-Timimi <27322516+quentin987@users.noreply.github.com>
Co-authored-by: Wei (Waley) Zhang <waley.zhang@microsoft.com>
Co-authored-by: Tian L <60599517+MSFT-Tilia@users.noreply.github.com>
Co-authored-by: Michał Janiszewski <janisozaur@users.noreply.github.com>
Co-authored-by: Dave Grochocki <grochocki@users.noreply.github.com>
2021-07-15 17:12:38 +08:00

740 lines
77 KiB
C++

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#pragma once
#include "CalcManager/ExpressionCommandInterface.h"
#include "DelegateCommand.h"
#include "GraphingInterfaces/GraphingEnums.h"
// Utility macros to make Models easier to write
// generates a member variable called m_<n>
#define SINGLE_ARG(...) __VA_ARGS__
#define PROPERTY_R(t, n) \
property t n \
{ \
t get() \
{ \
return m_##n; \
} \
\
private: \
void set(t value) \
{ \
m_##n = value; \
} \
} \
\
private: \
t m_##n; \
\
public:
#define PROPERTY_RW(t, n) \
property t n \
{ \
t get() \
{ \
return m_##n; \
} \
void set(t value) \
{ \
m_##n = value; \
} \
} \
\
private: \
t m_##n; \
\
public:
#define OBSERVABLE_PROPERTY_R(t, n) \
property t n \
{ \
t get() \
{ \
return m_##n; \
} \
\
private: \
void set(t value) \
{ \
if (m_##n != value) \
{ \
m_##n = value; \
RaisePropertyChanged(L#n); \
} \
} \
} \
\
private: \
t m_##n; \
\
public:
#define OBSERVABLE_PROPERTY_RW(t, n) \
property t n \
{ \
t get() \
{ \
return m_##n; \
} \
void set(t value) \
{ \
if (m_##n != value) \
{ \
m_##n = value; \
RaisePropertyChanged(L#n); \
} \
} \
} \
\
private: \
t m_##n; \
\
public:
#define OBSERVABLE_NAMED_PROPERTY_R(t, n) \
OBSERVABLE_PROPERTY_R(t, n) \
public: \
static property Platform::String ^ n##PropertyName \
{ \
Platform::String ^ get() { return Platform::StringReference(L#n); } \
} \
\
public:
#define OBSERVABLE_NAMED_PROPERTY_RW(t, n) \
OBSERVABLE_PROPERTY_RW(t, n) \
public: \
static property Platform::String ^ n##PropertyName \
{ \
Platform::String ^ get() { return Platform::StringReference(L#n); } \
} \
\
public:
#define OBSERVABLE_PROPERTY_FIELD(n) m_##n
// This variant of the observable object is for objects that don't want to react to property changes
#ifndef UNIT_TESTS
#define OBSERVABLE_OBJECT() \
virtual event Windows::UI::Xaml::Data::PropertyChangedEventHandler ^ PropertyChanged; \
internal: \
void RaisePropertyChanged(Platform::String ^ p) \
{ \
PropertyChanged(this, ref new Windows::UI::Xaml::Data::PropertyChangedEventArgs(p)); \
} \
\
public:
#else
#define OBSERVABLE_OBJECT() \
virtual event Windows::UI::Xaml::Data::PropertyChangedEventHandler ^ PropertyChanged; \
internal: \
void RaisePropertyChanged(Platform::String ^ p) \
{ \
} \
\
public:
#endif
// The callback specified in the macro is a method in the class that will be called every time the object changes
// the callback is supposed to be have a single parameter of type Platform::String^
#ifndef UNIT_TESTS
#define OBSERVABLE_OBJECT_CALLBACK(c) \
virtual event Windows::UI::Xaml::Data::PropertyChangedEventHandler ^ PropertyChanged; \
internal: \
void RaisePropertyChanged(Platform::String ^ p) \
{ \
PropertyChanged(this, ref new Windows::UI::Xaml::Data::PropertyChangedEventArgs(p)); \
c(p); \
} \
\
public:
#else
#define OBSERVABLE_OBJECT_CALLBACK(c) \
virtual event Windows::UI::Xaml::Data::PropertyChangedEventHandler ^ PropertyChanged; \
internal: \
void RaisePropertyChanged(Platform::String ^ p) \
{ \
c(p); \
} \
\
public:
#endif
// The variable member generated by this macro should not be used in the class code, use the
// property getter instead.
#define COMMAND_FOR_METHOD(p, m) \
property Windows::UI::Xaml::Input::ICommand ^ p \
{ \
Windows::UI::Xaml::Input::ICommand ^ get() \
{ \
if (!donotuse_##p) \
{ \
donotuse_##p = ref new CalculatorApp::ViewModel::Common::DelegateCommand( \
CalculatorApp::ViewModel::Common::MakeDelegateCommandHandler(this, &m) \
); \
} \
return donotuse_##p; \
} \
} \
\
private: \
Windows::UI::Xaml::Input::ICommand ^ donotuse_##p; \
\
public:
#define DEPENDENCY_PROPERTY_DECLARATION(t, n) \
property t n \
{ \
t get() \
{ \
return safe_cast<t>(GetValue(s_##n##Property)); \
} \
void set(t value) \
{ \
SetValue(s_##n##Property, value); \
} \
} \
\
private: \
static Windows::UI::Xaml::DependencyProperty ^ s_##n##Property; \
\
private:
// Utilities for DependencyProperties
namespace Utils
{
namespace Details
{
template <typename T>
struct IsRefClass
{
static const bool value = __is_ref_class(T);
};
template <typename T>
struct RemoveHat
{
typedef T type;
};
template <typename T>
struct RemoveHat<T ^>
{
typedef T type;
};
template <typename T>
typename std::enable_if<IsRefClass<T>::value, T ^>::type MakeDefault()
{
return nullptr;
}
template <typename T>
typename std::enable_if<!IsRefClass<T>::value, T>::type MakeDefault()
{
return T();
}
// There's a bug in Xaml in which custom enums are not recognized by the property system/binding
// therefore this template will determine that for enums the type to use to register the
// DependencyProperty is to be Object, for everything else it will use the type
// NOTE: If we are to find more types in which this is broken this template
// will be specialized for those types to return Object
template <typename T>
struct TypeToUseForDependencyProperty
{
typedef typename std::conditional<std::is_enum<T>::value, Platform::Object, T>::type type;
};
}
// Regular DependencyProperty
template <typename TOwner, typename TType>
Windows::UI::Xaml::DependencyProperty^ RegisterDependencyProperty(
_In_ const wchar_t* const name,
_In_ Windows::UI::Xaml::PropertyMetadata^ metadata)
{
typedef typename Details::RemoveHat<TOwner>::type OwnerType;
typedef typename Details::RemoveHat<TType>::type ThisPropertyType;
typedef typename Details::TypeToUseForDependencyProperty<ThisPropertyType>::type ThisDependencyPropertyType;
static_assert(Details::IsRefClass<OwnerType>::value, "The owner of a DependencyProperty must be a ref class");
return Windows::UI::Xaml::DependencyProperty::Register(
Platform::StringReference(name),
ThisDependencyPropertyType::typeid, // Work around bugs in Xaml by using the filtered type
OwnerType::typeid,
metadata);
}
template <typename TOwner, typename TType>
Windows::UI::Xaml::DependencyProperty^ RegisterDependencyProperty(_In_ const wchar_t* const name)
{
typedef typename Details::RemoveHat<TType>::type ThisPropertyType;
return RegisterDependencyProperty<TOwner, TType>(
name,
ref new Windows::UI::Xaml::PropertyMetadata(Details::MakeDefault<ThisPropertyType>()));
}
template <typename TOwner, typename TType>
Windows::UI::Xaml::DependencyProperty^ RegisterDependencyProperty(_In_ const wchar_t* const name, TType defaultValue)
{
return RegisterDependencyProperty<TOwner, TType>(
name,
ref new Windows::UI::Xaml::PropertyMetadata(defaultValue));
}
template <typename TOwner, typename TType, typename TCallback>
Windows::UI::Xaml::DependencyProperty^ RegisterDependencyPropertyWithCallback(
_In_ wchar_t const * const name,
TCallback callback)
{
typedef typename Details::RemoveHat<TType>::type ThisPropertyType;
return RegisterDependencyProperty<TOwner, TType>(
name,
ref new Windows::UI::Xaml::PropertyMetadata(
Details::MakeDefault<ThisPropertyType>(),
ref new Windows::UI::Xaml::PropertyChangedCallback(callback)));
}
template <typename TOwner, typename TType, typename TCallback>
Windows::UI::Xaml::DependencyProperty^ RegisterDependencyPropertyWithCallback(
_In_ wchar_t const * const name,
TType defaultValue,
TCallback callback)
{
typedef typename Details::RemoveHat<TType>::type ThisPropertyType;
return RegisterDependencyProperty<TOwner, TType>(
name,
ref new Windows::UI::Xaml::PropertyMetadata(
defaultValue,
ref new Windows::UI::Xaml::PropertyChangedCallback(callback)));
}
// Attached DependencyProperty
template <typename TOwner, typename TType>
Windows::UI::Xaml::DependencyProperty^ RegisterDependencyPropertyAttached(
_In_ const wchar_t* const name,
_In_ Windows::UI::Xaml::PropertyMetadata^ metadata)
{
typedef typename Details::RemoveHat<TOwner>::type OwnerType;
typedef typename Details::RemoveHat<TType>::type ThisPropertyType;
typedef typename Details::TypeToUseForDependencyProperty<ThisPropertyType>::type ThisDependencyPropertyType;
static_assert(Details::IsRefClass<OwnerType>::value, "The owner of a DependencyProperty must be a ref class");
return Windows::UI::Xaml::DependencyProperty::RegisterAttached(
Platform::StringReference(name),
ThisDependencyPropertyType::typeid, // Work around bugs in Xaml by using the filtered type
OwnerType::typeid,
metadata);
}
template <typename TOwner, typename TType>
Windows::UI::Xaml::DependencyProperty^ RegisterDependencyPropertyAttached(_In_ const wchar_t* const name)
{
typedef typename Details::RemoveHat<TType>::type ThisPropertyType;
return RegisterDependencyPropertyAttached<TOwner, TType>(
name,
ref new Windows::UI::Xaml::PropertyMetadata(Details::MakeDefault<ThisPropertyType>()));
}
template <typename TOwner, typename TType>
Windows::UI::Xaml::DependencyProperty^ RegisterDependencyPropertyAttached(_In_ const wchar_t* const name, TType defaultValue)
{
return RegisterDependencyPropertyAttached<TOwner, TType>(
name,
ref new Windows::UI::Xaml::PropertyMetadata(defaultValue));
}
template <typename TOwner, typename TType, typename TCallback>
Windows::UI::Xaml::DependencyProperty^ RegisterDependencyPropertyAttachedWithCallback(
_In_ wchar_t const * const name,
TCallback callback)
{
typedef typename Details::RemoveHat<TType>::type ThisPropertyType;
return RegisterDependencyPropertyAttached<TOwner, TType>(
name,
ref new Windows::UI::Xaml::PropertyMetadata(
Details::MakeDefault<ThisPropertyType>(),
ref new Windows::UI::Xaml::PropertyChangedCallback(callback)));
}
template <typename TOwner, typename TType, typename TCallback>
Windows::UI::Xaml::DependencyProperty^ RegisterDependencyPropertyAttachedWithCallback(
_In_ wchar_t const * const name,
TType defaultValue,
TCallback callback)
{
typedef typename Details::RemoveHat<TType>::type ThisPropertyType;
return RegisterDependencyPropertyAttached<TOwner, TType>(
name,
ref new Windows::UI::Xaml::PropertyMetadata(
defaultValue,
ref new Windows::UI::Xaml::PropertyChangedCallback(callback)));
}
void IFTPlatformException(HRESULT hr);
bool IsLastCharacterTarget(std::wstring const& input, wchar_t target);
// Return wstring after removing characters specified by unwantedChars array
template <size_t N>
std::wstring RemoveUnwantedCharsFromString(std::wstring inputString, const wchar_t (&unwantedChars)[N])
{
for (const wchar_t unwantedChar : unwantedChars)
{
inputString.erase(std::remove(inputString.begin(), inputString.end(), unwantedChar), inputString.end());
}
return inputString;
}
double GetDoubleFromWstring(std::wstring input);
int GetWindowId();
void RunOnUIThreadNonblocking(std::function<void()>&& function, _In_ Windows::UI::Core::CoreDispatcher ^ currentDispatcher);
Windows::Foundation::DateTime GetUniversalSystemTime();
bool IsDateTimeOlderThan(Windows::Foundation::DateTime dateTime, const long long duration);
concurrency::task<void> WriteFileToFolder(Windows::Storage::IStorageFolder^ folder, Platform::String^ fileName, Platform::String^ contents, Windows::Storage::CreationCollisionOption collisionOption);
concurrency::task<Platform::String^> ReadFileFromFolder(Windows::Storage::IStorageFolder^ folder, Platform::String^ fileName);
bool AreColorsEqual(const Windows::UI::Color& color1, const Windows::UI::Color& color2);
Platform::String^ Trim(Platform::String^ value);
void Trim(std::wstring& value);
void TrimFront(std::wstring& value);
void TrimBack(std::wstring& value);
}
// This goes into the header to define the property, in the public: section of the class
#define DEPENDENCY_PROPERTY_OWNER(owner) \
private: \
typedef owner DependencyPropertiesOwner; \
\
public:
// Normal DependencyProperty
#define DEPENDENCY_PROPERTY(type, name) \
property type name \
{ \
type get() \
{ \
return safe_cast<type>(GetValue(s_##name##Property)); \
} \
void set(type value) \
{ \
SetValue(s_##name##Property, value); \
} \
} \
\
private: \
static Windows::UI::Xaml::DependencyProperty ^ s_##name##Property; \
\
public: \
static property Windows::UI::Xaml::DependencyProperty ^ name##Property \
{ \
Windows::UI::Xaml::DependencyProperty ^ get() { \
assert(s_##name##Property); \
return s_##name##Property; \
} \
} \
\
private: \
static Windows::UI::Xaml::DependencyProperty ^ Initialize##name##Property() \
{ \
return Utils::RegisterDependencyProperty<DependencyPropertiesOwner, type>(L#name); \
} \
\
public:
#define DEPENDENCY_PROPERTY_WITH_DEFAULT(type, name, defaultValue) \
property type name \
{ \
type get() \
{ \
return safe_cast<type>(GetValue(s_##name##Property)); \
} \
void set(type value) \
{ \
SetValue(s_##name##Property, value); \
} \
} \
\
private: \
static Windows::UI::Xaml::DependencyProperty ^ s_##name##Property; \
\
public: \
static property Windows::UI::Xaml::DependencyProperty ^ name##Property \
{ \
Windows::UI::Xaml::DependencyProperty ^ get() { \
assert(s_##name##Property); \
return s_##name##Property; \
} \
} \
\
private: \
static Windows::UI::Xaml::DependencyProperty ^ Initialize##name##Property() \
{ \
return Utils::RegisterDependencyProperty<DependencyPropertiesOwner, type>(L#name, defaultValue); \
} \
\
public:
#define DEPENDENCY_PROPERTY_WITH_CALLBACK(type, name) \
property type name \
{ \
type get() \
{ \
return safe_cast<type>(GetValue(s_##name##Property)); \
} \
void set(type value) \
{ \
SetValue(s_##name##Property, value); \
} \
} \
\
private: \
static Windows::UI::Xaml::DependencyProperty ^ s_##name##Property; \
\
public: \
static property Windows::UI::Xaml::DependencyProperty ^ name##Property \
{ \
Windows::UI::Xaml::DependencyProperty ^ get() { \
assert(s_##name##Property); \
return s_##name##Property; \
} \
} \
\
private: \
static Windows::UI::Xaml::DependencyProperty ^ Initialize##name##Property() \
{ \
return Utils::RegisterDependencyPropertyWithCallback<DependencyPropertiesOwner, type>(L#name, &On##name##PropertyChangedImpl); \
} \
static void On##name##PropertyChangedImpl(Windows::UI::Xaml::DependencyObject ^ sender, Windows::UI::Xaml::DependencyPropertyChangedEventArgs ^ args) \
{ \
auto self = safe_cast<DependencyPropertiesOwner ^>(sender); \
self->On##name##PropertyChanged(safe_cast<type>(args->OldValue), safe_cast<type>(args->NewValue)); \
} \
\
public:
#define DEPENDENCY_PROPERTY_WITH_DEFAULT_AND_CALLBACK(type, name, defaultValue) \
property type name \
{ \
type get() \
{ \
return safe_cast<type>(GetValue(s_##name##Property)); \
} \
void set(type value) \
{ \
SetValue(s_##name##Property, value); \
} \
} \
\
private: \
static Windows::UI::Xaml::DependencyProperty ^ s_##name##Property; \
\
public: \
static property Windows::UI::Xaml::DependencyProperty ^ name##Property \
{ \
Windows::UI::Xaml::DependencyProperty ^ get() { \
assert(s_##name##Property); \
return s_##name##Property; \
} \
} \
\
private: \
static Windows::UI::Xaml::DependencyProperty ^ Initialize##name##Property() \
{ \
return Utils::RegisterDependencyPropertyWithCallback<DependencyPropertiesOwner, type>(L#name, defaultValue, &On##name##PropertyChangedImpl); \
} \
static void On##name##PropertyChangedImpl(Windows::UI::Xaml::DependencyObject ^ sender, Windows::UI::Xaml::DependencyPropertyChangedEventArgs ^ args) \
{ \
auto self = safe_cast<DependencyPropertiesOwner ^>(sender); \
self->On##name##PropertyChanged(safe_cast<type>(args->OldValue), safe_cast<type>(args->NewValue)); \
} \
\
public:
// Attached DependencyProperty
#define DEPENDENCY_PROPERTY_ATTACHED(type, name) \
static type Get##name(Windows::UI::Xaml::DependencyObject ^ target) \
{ \
return safe_cast<type>(target->GetValue(s_##name##Property)); \
} \
static void Set##name(Windows::UI::Xaml::DependencyObject ^ target, type value) \
{ \
target->SetValue(s_##name##Property, value); \
} \
\
private: \
static Windows::UI::Xaml::DependencyProperty ^ s_##name##Property; \
\
public: \
static property Windows::UI::Xaml::DependencyProperty ^ name##Property \
{ \
Windows::UI::Xaml::DependencyProperty ^ get() { \
assert(s_##name##Property); \
return s_##name##Property; \
} \
} \
\
private: \
static Windows::UI::Xaml::DependencyProperty ^ Initialize##name##Property() \
{ \
return Utils::RegisterDependencyPropertyAttached<DependencyPropertiesOwner, type>(L#name); \
} \
\
public:
#define DEPENDENCY_PROPERTY_ATTACHED_WITH_DEFAULT(type, name, defaultValue) \
static type Get##name(Windows::UI::Xaml::DependencyObject ^ target) \
{ \
return safe_cast<type>(target->GetValue(s_##name##Property)); \
} \
static void Set##name(Windows::UI::Xaml::DependencyObject ^ target, type value) \
{ \
target->SetValue(s_##name##Property, value); \
} \
\
private: \
static Windows::UI::Xaml::DependencyProperty ^ s_##name##Property; \
\
public: \
static property Windows::UI::Xaml::DependencyProperty ^ name##Property \
{ \
Windows::UI::Xaml::DependencyProperty ^ get() { \
assert(s_##name##Property); \
return s_##name##Property; \
} \
} \
\
private: \
static Windows::UI::Xaml::DependencyProperty ^ Initialize##name##Property() \
{ \
return Utils::RegisterDependencyPropertyAttached<DependencyPropertiesOwner, type>(L#name, defaultValue); \
} \
\
public:
#define DEPENDENCY_PROPERTY_ATTACHED_WITH_CALLBACK(type, name) \
static type Get##name(Windows::UI::Xaml::DependencyObject ^ target) \
{ \
return safe_cast<type>(target->GetValue(s_##name##Property)); \
} \
static void Set##name(Windows::UI::Xaml::DependencyObject ^ target, type value) \
{ \
target->SetValue(s_##name##Property, value); \
} \
\
private: \
static Windows::UI::Xaml::DependencyProperty ^ s_##name##Property; \
\
public: \
static property Windows::UI::Xaml::DependencyProperty ^ name##Property \
{ \
Windows::UI::Xaml::DependencyProperty ^ get() { \
assert(s_##name##Property); \
return s_##name##Property; \
} \
} \
\
private: \
static Windows::UI::Xaml::DependencyProperty ^ Initialize##name##Property() \
{ \
return Utils::RegisterDependencyPropertyAttachedWithCallback<DependencyPropertiesOwner, type>(L#name, &On##name##PropertyChangedImpl); \
} \
static void On##name##PropertyChangedImpl(Windows::UI::Xaml::DependencyObject ^ sender, Windows::UI::Xaml::DependencyPropertyChangedEventArgs ^ args) \
{ \
On##name##PropertyChanged(sender, safe_cast<type>(args->OldValue), safe_cast<type>(args->NewValue)); \
} \
\
public:
#define DEPENDENCY_PROPERTY_ATTACHED_WITH_DEFAULT_AND_CALLBACK(type, name, defaultValue) \
static type Get##name(Windows::UI::Xaml::DependencyObject ^ target) \
{ \
return safe_cast<type>(target->GetValue(s_##name##Property)); \
} \
static void Set##name(Windows::UI::Xaml::DependencyObject ^ target, type value) \
{ \
target->SetValue(s_##name##Property, value); \
} \
\
private: \
static Windows::UI::Xaml::DependencyProperty ^ s_##name##Property; \
\
public: \
static property Windows::UI::Xaml::DependencyProperty ^ name##Property \
{ \
Windows::UI::Xaml::DependencyProperty ^ get() { \
assert(s_##name##Property); \
return s_##name##Property; \
} \
} \
\
private: \
static Windows::UI::Xaml::DependencyProperty ^ Initialize##name##Property() \
{ \
return Utils::RegisterDependencyPropertyAttachedWithCallback<DependencyPropertiesOwner, type>(L#name, defaultValue, &On##name##PropertyChangedImpl); \
} \
static void On##name##PropertyChangedImpl(Windows::UI::Xaml::DependencyObject ^ sender, Windows::UI::Xaml::DependencyPropertyChangedEventArgs ^ args) \
{ \
On##name##PropertyChanged(sender, safe_cast<type>(args->OldValue), safe_cast<type>(args->NewValue)); \
} \
\
public:
// This goes into the cpp to initialize the static variable
#define DEPENDENCY_PROPERTY_INITIALIZATION(owner, name) Windows::UI::Xaml::DependencyProperty ^ owner::s_##name##Property = owner::Initialize##name##Property();
namespace CalculatorApp
{
template <typename T>
T from_cx(Platform::Object ^ from)
{
T to{ nullptr };
winrt::check_hresult(reinterpret_cast<::IUnknown*>(from)->QueryInterface(winrt::guid_of<T>(), reinterpret_cast<void**>(winrt::put_abi(to))));
return to;
}
namespace ViewModel::Common
{
// below utilities are intended to support interops between C# and C++/CX
// they can be removed if the entire codebase has been migrated to C#
public ref class Utilities sealed
{
public:
static Platform::String ^ EscapeHtmlSpecialCharacters(Platform::String ^ originalString);
static Platform::String^ TrimTrailingZeros(Platform::String^ input);
static bool AreColorsEqual(Windows::UI::Color color1, Windows::UI::Color color2);
static Windows::UI::Xaml::Media::SolidColorBrush ^ GetContrastColor(Windows::UI::Color backgroundColor);
static int GetWindowId();
static long long GetConst_WINEVENT_KEYWORD_RESPONSE_TIME();
static bool GetIntegratedDisplaySize(double* size);
};
}
}
// There's no standard definition of equality for Windows::UI::Color structs.
// Define a template specialization for std::equal_to.
template<>
class std::equal_to<Windows::UI::Color>
{
public:
bool operator()(const Windows::UI::Color& color1, const Windows::UI::Color& color2)
{
return Utils::AreColorsEqual(color1, color2);
}
};
bool operator==(const Windows::UI::Color& color1, const Windows::UI::Color& color2);
bool operator!=(const Windows::UI::Color& color1, const Windows::UI::Color& color2);