Skip to content

Commit

Permalink
Fixes to a few NumberBox issues (#1894)
Browse files Browse the repository at this point in the history
* Fixes to a few NumberBox issues.

* PR update

* PR feedback and fixed downlevel missing resource.
  • Loading branch information
teaP committed Feb 3, 2020
1 parent 2df7f39 commit 1c09f51
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 13 deletions.
44 changes: 39 additions & 5 deletions dev/NumberBox/NumberBox.cpp
Expand Up @@ -9,6 +9,7 @@
#include "RuntimeProfiler.h"
#include "ResourceAccessor.h"
#include "Utils.h"
#include "winnls.h"

static constexpr wstring_view c_numberBoxDownButtonName{ L"DownSpinButton"sv };
static constexpr wstring_view c_numberBoxUpButtonName{ L"UpSpinButton"sv };
Expand All @@ -35,11 +36,7 @@ NumberBox::NumberBox()
{
__RP_Marker_ClassById(RuntimeProfiler::ProfId_NumberBox);

// Default values for the number formatter
const auto formatter = winrt::DecimalFormatter();
formatter.IntegerDigits(1);
formatter.FractionDigits(0);
NumberFormatter(formatter);
NumberFormatter(GetRegionalSettingsAwareDecimalFormatter());

PointerWheelChanged({ this, &NumberBox::OnNumberBoxScroll });

Expand All @@ -49,6 +46,43 @@ NumberBox::NumberBox()
SetDefaultStyleKey(this);
}

// This was largely copied from Calculator's GetRegionalSettingsAwareDecimalFormatter()
winrt::DecimalFormatter NumberBox::GetRegionalSettingsAwareDecimalFormatter()
{
winrt::DecimalFormatter formatter = nullptr;

WCHAR currentLocale[LOCALE_NAME_MAX_LENGTH] = {};
if (GetUserDefaultLocaleName(currentLocale, LOCALE_NAME_MAX_LENGTH) != 0)
{
// GetUserDefaultLocaleName may return an invalid bcp47 language tag with trailing non-BCP47 friendly characters,
// which if present would start with an underscore, for example sort order
// (see https://msdn.microsoft.com/en-us/library/windows/desktop/dd373814(v=vs.85).aspx).
// Therefore, if there is an underscore in the locale name, trim all characters from the underscore onwards.
WCHAR* underscore = wcschr(currentLocale, L'_');
if (underscore != nullptr)
{
*underscore = L'\0';
}

if (winrt::Language::IsWellFormed(currentLocale))
{
std::vector<winrt::hstring> languageList;
languageList.push_back(winrt::hstring(currentLocale));
formatter = winrt::DecimalFormatter(languageList, winrt::GlobalizationPreferences::HomeGeographicRegion());
}
}

if (!formatter)
{
formatter = winrt::DecimalFormatter();
}

formatter.IntegerDigits(1);
formatter.FractionDigits(0);

return formatter;
}

void NumberBox::Value(double value)
{
// When using two way bindings to Value using x:Bind, we could end up with a stack overflow because
Expand Down
1 change: 1 addition & 0 deletions dev/NumberBox/NumberBox.h
Expand Up @@ -58,6 +58,7 @@ class NumberBox :
void ValidateNumberFormatter(winrt::INumberFormatter2 value);

private:
winrt::DecimalFormatter GetRegionalSettingsAwareDecimalFormatter();

void OnSpinDownClick(winrt::IInspectable const& sender, winrt::RoutedEventArgs const& args);
void OnSpinUpClick(winrt::IInspectable const& sender, winrt::RoutedEventArgs const& args);
Expand Down
37 changes: 29 additions & 8 deletions dev/NumberBox/NumberBox.xaml
Expand Up @@ -79,24 +79,38 @@
</ResourceDictionary>
</Grid.Resources>

<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>

<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

<ContentPresenter x:Name="HeaderContentPresenter"
Grid.ColumnSpan="3"
Content="{TemplateBinding Header}"
ContentTemplate="{TemplateBinding HeaderTemplate}"
FontWeight="Normal"
Foreground="{ThemeResource TextControlHeaderForeground}"
Margin="{ThemeResource TextBoxTopHeaderMargin}"
TextWrapping="Wrap"
VerticalAlignment="Top"/>

<TextBox x:Name="InputBox"
Grid.Column="0"
Header="{TemplateBinding Header}"
HeaderTemplate="{TemplateBinding HeaderTemplate}"
Grid.Row="1"
PlaceholderText="{TemplateBinding PlaceholderText}"
contract7Present:SelectionFlyout="{TemplateBinding SelectionFlyout}"
SelectionHighlightColor="{TemplateBinding SelectionHighlightColor}"
TextReadingOrder="{TemplateBinding TextReadingOrder}"
PreventKeyboardDisplayOnProgrammaticFocus="{TemplateBinding PreventKeyboardDisplayOnProgrammaticFocus}"
contract7Present:Description="{TemplateBinding Description}"/>
PreventKeyboardDisplayOnProgrammaticFocus="{TemplateBinding PreventKeyboardDisplayOnProgrammaticFocus}"/>

<Popup x:Name="UpDownPopup"
Grid.Row="1"
Grid.Column="1"
VerticalOffset="{ThemeResource NumberBoxPopupVerticalOffset}"
HorizontalOffset="{ThemeResource NumberBoxPopupHorizonalOffset}"
Expand All @@ -116,7 +130,6 @@

<RepeatButton x:Name="PopupUpSpinButton"
Style="{StaticResource NumberBoxPopupSpinButtonStyle}"
Grid.Row="0"
Content="&#xE70E;"/>

<RepeatButton x:Name="PopupDownSpinButton"
Expand All @@ -127,6 +140,7 @@
</Popup>

<RepeatButton x:Name="UpSpinButton"
Grid.Row="1"
Grid.Column="1"
Visibility="Collapsed"
FontSize="{TemplateBinding FontSize}"
Expand All @@ -135,12 +149,20 @@
contract7Present:CornerRadius="0"/>

<RepeatButton x:Name="DownSpinButton"
Grid.Row="1"
Grid.Column="2"
Visibility="Collapsed"
FontSize="{TemplateBinding FontSize}"
Content="&#xE70D;"
Style="{StaticResource NumberBoxSpinButtonStyle}"
contract7Present:CornerRadius="{Binding Source={ThemeResource ControlCornerRadius}, Converter={StaticResource RightCornerRadiusFilterConverter}}" />

<ContentPresenter x:Name="DescriptionPresenter"
Grid.Row="2"
Grid.ColumnSpan="3"
Content="{TemplateBinding Description}"
Foreground="{ThemeResource SystemControlDescriptionTextForegroundBrush}"
AutomationProperties.AccessibilityView="Raw"/>
</Grid>
</ControlTemplate>
</Setter.Value>
Expand All @@ -151,8 +173,7 @@
<Style.Setters>
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="MinWidth" Value="34"/>
<Setter Property="Height" Value="{ThemeResource TextControlThemeMinHeight}"/>
<Setter Property="VerticalAlignment" Value="Bottom"/>
<Setter Property="VerticalAlignment" Value="Stretch"/>
<Setter Property="Background" Value="{ThemeResource TextControlBackground}"/>
<Setter Property="BorderBrush" Value="{ThemeResource TextControlBorderBrush}"/>
<Setter Property="BorderThickness" Value="{ThemeResource NumberBoxSpinButtonBorderThickness}"/>
Expand Down
4 changes: 4 additions & 0 deletions dev/NumberBox/NumberBox_themeresources.xaml
@@ -1,19 +1,23 @@
<!-- Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License. See LICENSE in the project root for license information. -->
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:contract7NotPresent="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractNotPresent(Windows.Foundation.UniversalApiContract,7)"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<StaticResource x:Key="NumberBoxPopupIndicatorForeground" ResourceKey="SystemControlForegroundBaseMediumBrush" />
<contract7NotPresent:StaticResource x:Key="SystemControlDescriptionTextForegroundBrush" ResourceKey="SystemControlPageTextBaseMediumBrush" />
</ResourceDictionary>

<ResourceDictionary x:Key="Dark">
<StaticResource x:Key="NumberBoxPopupIndicatorForeground" ResourceKey="SystemControlForegroundBaseMediumBrush" />
<contract7NotPresent:StaticResource x:Key="SystemControlDescriptionTextForegroundBrush" ResourceKey="SystemControlPageTextBaseMediumBrush" />
</ResourceDictionary>

<ResourceDictionary x:Key="HighContrast">
<StaticResource x:Key="NumberBoxPopupIndicatorForeground" ResourceKey="SystemControlForegroundBaseMediumBrush" />
<contract7NotPresent:StaticResource x:Key="SystemControlDescriptionTextForegroundBrush" ResourceKey="SystemControlPageTextBaseMediumBrush" />
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>

Expand Down
1 change: 1 addition & 0 deletions dev/NumberBox/TestUI/NumberBoxPage.xaml
Expand Up @@ -74,6 +74,7 @@
x:Name="TestNumberBox"
AutomationProperties.Name="TestNumberBox"
Header="NumberBox"
Description="Description text"
PlaceholderText="Text"
ValueChanged="NumberBoxValueChanged"
SmallChange="{x:Bind SmallChangeNumberBox.Value, Mode=OneWay}"
Expand Down
4 changes: 4 additions & 0 deletions dev/inc/CppWinRTIncludes.h
Expand Up @@ -23,6 +23,7 @@
#include <winrt\Windows.ApplicationModel.Resources.h>
#include <winrt\Windows.ApplicationModel.Resources.Core.h>
#include <winrt\Windows.Devices.Input.h>
#include <winrt\Windows.Globalization.h>
#include <winrt\Windows.Globalization.NumberFormatting.h>
#include <winrt\Windows.Graphics.Imaging.h>
#include <winrt\Windows.Graphics.Display.h>
Expand All @@ -32,6 +33,7 @@
#include <winrt\Windows.System.Profile.h>
#include <winrt\Windows.System.h>
#include <winrt\Windows.System.Threading.h>
#include <winrt\Windows.System.UserProfile.h>
#include <winrt\Windows.UI.h>
#include <winrt\Windows.UI.Composition.h>
#include <winrt\Windows.UI.Composition.Effects.h>
Expand Down Expand Up @@ -93,6 +95,7 @@ namespace winrt
using namespace ::winrt::Windows::Foundation::Collections;
using namespace ::winrt::Windows::Foundation::Metadata;
using namespace ::winrt::Windows::Foundation::Numerics;
using namespace ::winrt::Windows::Globalization;
using namespace ::winrt::Windows::Globalization::NumberFormatting;
using namespace ::winrt::Windows::Graphics::Display;
using namespace ::winrt::Windows::Graphics::Imaging;
Expand All @@ -101,6 +104,7 @@ namespace winrt
using namespace ::winrt::Windows::System;
using namespace ::winrt::Windows::System::Power;
using namespace ::winrt::Windows::System::Threading;
using namespace ::winrt::Windows::System::UserProfile;
using namespace ::winrt::Windows::UI;
using namespace ::winrt::Windows::UI::Composition;
using namespace ::winrt::Windows::UI::Composition::Effects;
Expand Down

0 comments on commit 1c09f51

Please sign in to comment.