mirror of
https://github.com/Microsoft/calculator.git
synced 2025-08-22 06:13:14 -07:00
Convert utilities to C#
This commit is contained in:
parent
62ca1ddf1c
commit
9e5b5d01b4
8 changed files with 40 additions and 109 deletions
|
@ -166,93 +166,3 @@ bool operator!=(const Color& color1, const Color& color2)
|
||||||
{
|
{
|
||||||
return !(color1 == color2);
|
return !(color1 == color2);
|
||||||
}
|
}
|
||||||
|
|
||||||
String^ CalculatorApp::ViewModelNative::Common::Utilities::EscapeHtmlSpecialCharacters(String^ originalString)
|
|
||||||
{
|
|
||||||
// Construct a default special characters if not provided.
|
|
||||||
const std::vector<wchar_t> 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<SolidColorBrush ^>(Application::Current->Resources->Lookup(L"WhiteBrush"));
|
|
||||||
}
|
|
||||||
|
|
||||||
return static_cast<SolidColorBrush ^>(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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
|
@ -703,21 +703,6 @@ namespace CalculatorApp
|
||||||
|
|
||||||
return to;
|
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.
|
// There's no standard definition of equality for Windows::UI::Color structs.
|
||||||
|
|
|
@ -28,6 +28,9 @@ namespace CalculatorApp
|
||||||
public const long MICROSOFT_KEYWORD_LEVEL_3 = 0;
|
public const long MICROSOFT_KEYWORD_LEVEL_3 = 0;
|
||||||
public const long MICROSOFT_KEYWORD_RESERVED_44 = 0;
|
public const long MICROSOFT_KEYWORD_RESERVED_44 = 0;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// From winmeta.h in the Windows SDK
|
||||||
|
public const long WINEVENT_KEYWORD_RESPONSE_TIME = 0x1000000000000;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class AppLifecycleLogger
|
internal class AppLifecycleLogger
|
||||||
|
@ -131,7 +134,7 @@ namespace CalculatorApp
|
||||||
private void LogAppLifecycleEvent(string eventName, LoggingFields fields)
|
private void LogAppLifecycleEvent(string eventName, LoggingFields fields)
|
||||||
{
|
{
|
||||||
m_appLifecycleProvider.LogEvent(
|
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)
|
private void PopulateAppInfo(LoggingFields fields)
|
||||||
|
|
|
@ -7,6 +7,7 @@ using CalculatorApp.Controls;
|
||||||
using CalculatorApp.ViewModelNative;
|
using CalculatorApp.ViewModelNative;
|
||||||
using CalculatorApp.ViewModelNative.Common;
|
using CalculatorApp.ViewModelNative.Common;
|
||||||
using CalculatorApp.ViewModelNative.Common.Automation;
|
using CalculatorApp.ViewModelNative.Common.Automation;
|
||||||
|
using Utilities = CalculatorApp.ViewModel.Common.Utilities;
|
||||||
|
|
||||||
using GraphControl;
|
using GraphControl;
|
||||||
|
|
||||||
|
|
|
@ -266,7 +266,7 @@ namespace CalculatorApp
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Utilities.AreColorsEqual(brush.Color, selectedColor))
|
if (brush.Color == selectedColor)
|
||||||
{
|
{
|
||||||
gridViewItem.IsSelected = true;
|
gridViewItem.IsSelected = true;
|
||||||
SelectedColorIndex = i;
|
SelectedColorIndex = i;
|
||||||
|
|
|
@ -11,7 +11,7 @@ using CalculatorApp.ViewModelNative.Common.Automation;
|
||||||
using GraphControl;
|
using GraphControl;
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Web;
|
||||||
using Windows.ApplicationModel.DataTransfer;
|
using Windows.ApplicationModel.DataTransfer;
|
||||||
using Windows.ApplicationModel.Resources;
|
using Windows.ApplicationModel.Resources;
|
||||||
using Windows.Foundation;
|
using Windows.Foundation;
|
||||||
|
@ -434,7 +434,7 @@ namespace CalculatorApp
|
||||||
|
|
||||||
equationHtml += "<tr style=\"margin: 0pt 0pt 0pt 0pt; padding: 0pt 0pt 0pt 0pt; \"><td><span style=\"font-size: 22pt; line-height: 0;"
|
equationHtml += "<tr style=\"margin: 0pt 0pt 0pt 0pt; padding: 0pt 0pt 0pt 0pt; \"><td><span style=\"font-size: 22pt; line-height: 0;"
|
||||||
+ equationColorHtml + "\">■</span></td><td><div style=\"margin: 4pt 0pt 0pt 6pt;\">"
|
+ equationColorHtml + "\">■</span></td><td><div style=\"margin: 4pt 0pt 0pt 6pt;\">"
|
||||||
+ Utilities.EscapeHtmlSpecialCharacters(expression) + "</div></td>";
|
+ HttpUtility.HtmlEncode(expression) + "</div></td>";
|
||||||
}
|
}
|
||||||
equationHtml += "</table>";
|
equationHtml += "</table>";
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ using CalculatorApp.Converters;
|
||||||
using CalculatorApp.ViewModelNative;
|
using CalculatorApp.ViewModelNative;
|
||||||
using CalculatorApp.ViewModelNative.Common;
|
using CalculatorApp.ViewModelNative.Common;
|
||||||
using CalculatorApp.ViewModelNative.Common.Automation;
|
using CalculatorApp.ViewModelNative.Common.Automation;
|
||||||
|
using Utilities = CalculatorApp.ViewModel.Common.Utilities;
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
|
@ -1,13 +1,44 @@
|
||||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||||
// Licensed under the MIT License.
|
// Licensed under the MIT License.
|
||||||
|
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
using Windows.UI;
|
||||||
using Windows.UI.Core;
|
using Windows.UI.Core;
|
||||||
using Windows.UI.ViewManagement;
|
using Windows.UI.ViewManagement;
|
||||||
|
using Windows.UI.Xaml;
|
||||||
|
using Windows.UI.Xaml.Media;
|
||||||
|
|
||||||
namespace CalculatorApp.ViewModel.Common
|
namespace CalculatorApp.ViewModel.Common
|
||||||
{
|
{
|
||||||
public static class Utilities
|
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()
|
public static int GetWindowId()
|
||||||
{
|
{
|
||||||
int windowId = -1;
|
int windowId = -1;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue