diff --git a/src/CalcViewModel/Common/Utils.cpp b/src/CalcViewModel/Common/Utils.cpp index fdc3f6c1..68a3b020 100644 --- a/src/CalcViewModel/Common/Utils.cpp +++ b/src/CalcViewModel/Common/Utils.cpp @@ -166,93 +166,3 @@ bool operator!=(const Color& color1, const Color& color2) { return !(color1 == color2); } - -String^ CalculatorApp::ViewModelNative::Common::Utilities::EscapeHtmlSpecialCharacters(String^ originalString) -{ - // Construct a default special characters if not provided. - const std::vector specialCharacters {L'&', L'\"', L'\'', L'<', L'>'}; - - bool replaceCharacters = false; - const wchar_t* pCh; - String^ replacementString = nullptr; - - // First step is scanning the string for special characters. - // If there isn't any special character, we simply return the original string - for (pCh = originalString->Data(); *pCh; pCh++) - { - if (std::find(specialCharacters.begin(), specialCharacters.end(), *pCh) != specialCharacters.end()) - { - replaceCharacters = true; - break; - } - } - - if (replaceCharacters) - { - // If we indeed find a special character, we step back one character (the special - // character), and we create a new string where we replace those characters one by one - pCh--; - wstringstream buffer; - buffer << wstring(originalString->Data(), pCh); - - for (; *pCh; pCh++) - { - switch (*pCh) - { - case L'&': - buffer << L"&"; - break; - case L'\"': - buffer << L"""; - break; - case L'\'': - buffer << L"'"; - break; - case L'<': - buffer << L"<"; - break; - case L'>': - buffer << L">"; - break; - default: - buffer << *pCh; - } - } - replacementString = ref new String(buffer.str().c_str()); - } - - return replaceCharacters ? replacementString : originalString; -} - -bool CalculatorApp::ViewModelNative::Common::Utilities::AreColorsEqual(Windows::UI::Color color1, Windows::UI::Color color2) -{ - return Utils::AreColorsEqual(color1, color2); -} - -// This method calculates the luminance ratio between White and the given background color. -// The luminance is calculate using the RGB values and does not use the A value. -// White or Black is returned -SolidColorBrush ^ CalculatorApp::ViewModelNative::Common::Utilities::GetContrastColor(Color backgroundColor) -{ - auto luminance = 0.2126 * backgroundColor.R + 0.7152 * backgroundColor.G + 0.0722 * backgroundColor.B; - - if ((255 + 0.05) / (luminance + 0.05) >= 2.5) - { - return static_cast(Application::Current->Resources->Lookup(L"WhiteBrush")); - } - - return static_cast(Application::Current->Resources->Lookup(L"BlackBrush")); -} - -long long CalculatorApp::ViewModelNative::Common::Utilities::GetConst_WINEVENT_KEYWORD_RESPONSE_TIME() -{ - return WINEVENT_KEYWORD_RESPONSE_TIME; -} - -bool CalculatorApp::ViewModelNative::Common::Utilities::GetIntegratedDisplaySize(double* size) -{ - if (SUCCEEDED(::GetIntegratedDisplaySize(size))) - return true; - return false; -} - diff --git a/src/CalcViewModel/Common/Utils.h b/src/CalcViewModel/Common/Utils.h index af0e8832..1445ccee 100644 --- a/src/CalcViewModel/Common/Utils.h +++ b/src/CalcViewModel/Common/Utils.h @@ -703,21 +703,6 @@ namespace CalculatorApp return to; } - - namespace ViewModelNative::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 bool AreColorsEqual(Windows::UI::Color color1, Windows::UI::Color color2); - static Windows::UI::Xaml::Media::SolidColorBrush ^ GetContrastColor(Windows::UI::Color backgroundColor); - 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. diff --git a/src/Calculator/Common/AppLifecycleLogger.cs b/src/Calculator/Common/AppLifecycleLogger.cs index 0ca101ec..499ba255 100644 --- a/src/Calculator/Common/AppLifecycleLogger.cs +++ b/src/Calculator/Common/AppLifecycleLogger.cs @@ -28,6 +28,9 @@ namespace CalculatorApp public const long MICROSOFT_KEYWORD_LEVEL_3 = 0; public const long MICROSOFT_KEYWORD_RESERVED_44 = 0; #endif + + // From winmeta.h in the Windows SDK + public const long WINEVENT_KEYWORD_RESPONSE_TIME = 0x1000000000000; } internal class AppLifecycleLogger @@ -131,7 +134,7 @@ namespace CalculatorApp private void LogAppLifecycleEvent(string eventName, LoggingFields fields) { m_appLifecycleProvider.LogEvent( - eventName, fields, LoggingLevel.Information, new LoggingOptions(Globals.MICROSOFT_KEYWORD_LEVEL_3 | Utilities.GetConst_WINEVENT_KEYWORD_RESPONSE_TIME())); + eventName, fields, LoggingLevel.Information, new LoggingOptions(Globals.MICROSOFT_KEYWORD_LEVEL_3 | Globals.WINEVENT_KEYWORD_RESPONSE_TIME)); } private void PopulateAppInfo(LoggingFields fields) diff --git a/src/Calculator/Views/GraphingCalculator/EquationInputArea.xaml.cs b/src/Calculator/Views/GraphingCalculator/EquationInputArea.xaml.cs index ab1c767d..9ebaa7c5 100644 --- a/src/Calculator/Views/GraphingCalculator/EquationInputArea.xaml.cs +++ b/src/Calculator/Views/GraphingCalculator/EquationInputArea.xaml.cs @@ -7,6 +7,7 @@ using CalculatorApp.Controls; using CalculatorApp.ViewModelNative; using CalculatorApp.ViewModelNative.Common; using CalculatorApp.ViewModelNative.Common.Automation; +using Utilities = CalculatorApp.ViewModel.Common.Utilities; using GraphControl; diff --git a/src/Calculator/Views/GraphingCalculator/EquationStylePanelControl.xaml.cs b/src/Calculator/Views/GraphingCalculator/EquationStylePanelControl.xaml.cs index ef971d20..9e19fa5f 100644 --- a/src/Calculator/Views/GraphingCalculator/EquationStylePanelControl.xaml.cs +++ b/src/Calculator/Views/GraphingCalculator/EquationStylePanelControl.xaml.cs @@ -266,7 +266,7 @@ namespace CalculatorApp continue; } - if (Utilities.AreColorsEqual(brush.Color, selectedColor)) + if (brush.Color == selectedColor) { gridViewItem.IsSelected = true; SelectedColorIndex = i; diff --git a/src/Calculator/Views/GraphingCalculator/GraphingCalculator.xaml.cs b/src/Calculator/Views/GraphingCalculator/GraphingCalculator.xaml.cs index 28969628..a9cd249a 100644 --- a/src/Calculator/Views/GraphingCalculator/GraphingCalculator.xaml.cs +++ b/src/Calculator/Views/GraphingCalculator/GraphingCalculator.xaml.cs @@ -11,7 +11,7 @@ using CalculatorApp.ViewModelNative.Common.Automation; using GraphControl; using System; - +using System.Web; using Windows.ApplicationModel.DataTransfer; using Windows.ApplicationModel.Resources; using Windows.Foundation; @@ -434,7 +434,7 @@ namespace CalculatorApp equationHtml += "
" - + Utilities.EscapeHtmlSpecialCharacters(expression) + "
"; + + HttpUtility.HtmlEncode(expression) + ""; } equationHtml += ""; diff --git a/src/Calculator/Views/MainPage.xaml.cs b/src/Calculator/Views/MainPage.xaml.cs index 7a8c34fe..96ab2ed1 100644 --- a/src/Calculator/Views/MainPage.xaml.cs +++ b/src/Calculator/Views/MainPage.xaml.cs @@ -3,6 +3,7 @@ using CalculatorApp.Converters; using CalculatorApp.ViewModelNative; using CalculatorApp.ViewModelNative.Common; using CalculatorApp.ViewModelNative.Common.Automation; +using Utilities = CalculatorApp.ViewModel.Common.Utilities; using System; using System.Collections.Generic; diff --git a/src/CalculatorApp.ViewModel/Common/Utilities.cs b/src/CalculatorApp.ViewModel/Common/Utilities.cs index 513fac4b..5aae6363 100644 --- a/src/CalculatorApp.ViewModel/Common/Utilities.cs +++ b/src/CalculatorApp.ViewModel/Common/Utilities.cs @@ -1,13 +1,44 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +using System.Runtime.InteropServices; +using Windows.UI; using Windows.UI.Core; using Windows.UI.ViewManagement; +using Windows.UI.Xaml; +using Windows.UI.Xaml.Media; namespace CalculatorApp.ViewModel.Common { public static class Utilities { + private static class NativeMethods + { + [DllImport("kernelbase.dll", ExactSpelling = true, PreserveSig = true)] + public static extern int GetIntegratedDisplaySize(out double sizeInInches); + } + + public static bool GetIntegratedDisplaySize(out double sizeInInches) + { + var hresult = NativeMethods.GetIntegratedDisplaySize(out sizeInInches); + return hresult == 0; + } + + // This method calculates the luminance ratio between White and the given background color. + // The luminance is calculate using the RGB values and does not use the A value. + // White or Black is returned + public static SolidColorBrush GetContrastColor(Color backgroundColor) + { + var luminance = 0.2126 * backgroundColor.R + 0.7152 * backgroundColor.G + 0.0722 * backgroundColor.B; + + if ((255 + 0.05) / (luminance + 0.05) >= 2.5) + { + return (SolidColorBrush)Application.Current.Resources["WhiteBrush"]; + } + + return (SolidColorBrush)Application.Current.Resources["BlackBrush"]; + } + public static int GetWindowId() { int windowId = -1;