Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NumberBox: Fix header foreground color not being properly updated when NumberBox is disabled. #3005

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions dev/NumberBox/APITests/NumberBoxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,43 @@ public void VerifyNumberBoxCornerRadius()
});
}

[TestMethod]
public void VerifyIsEnabledChangeUpdatesVisualState()
{
var numberBox = SetupNumberBox();

VisualStateGroup commonStatesGroup = null;
RunOnUIThread.Execute(() =>
{
// Check 1: Set IsEnabled to true.
numberBox.IsEnabled = true;
Content.UpdateLayout();

var numberBoxLayoutRoot = (FrameworkElement)VisualTreeHelper.GetChild(numberBox, 0);
commonStatesGroup = VisualStateManager.GetVisualStateGroups(numberBoxLayoutRoot).First(vsg => vsg.Name.Equals("CommonStates"));

Verify.AreEqual("Normal", commonStatesGroup.CurrentState.Name);

// Check 2: Set IsEnabled to false.
numberBox.IsEnabled = false;
});
IdleSynchronizer.Wait();

RunOnUIThread.Execute(() =>
{
Verify.AreEqual("Disabled", commonStatesGroup.CurrentState.Name);

// Check 3: Set IsEnabled back to true.
numberBox.IsEnabled = true;
});
IdleSynchronizer.Wait();

RunOnUIThread.Execute(() =>
{
Verify.AreEqual("Normal", commonStatesGroup.CurrentState.Name);
});
}

private NumberBox SetupNumberBox()
{
NumberBox numberBox = null;
Expand Down
16 changes: 15 additions & 1 deletion dev/NumberBox/NumberBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,16 @@ void NumberBox::OnApplyTemplate()
m_popupUpButtonClickRevoker = popupSpinUp.Click(winrt::auto_revoke, { this, &NumberBox::OnSpinUpClick });
}

m_isEnabledChangedRevoker = IsEnabledChanged(winrt::auto_revoke, { this, &NumberBox::OnIsEnabledChanged });

// .NET rounds to 12 significant digits when displaying doubles, so we will do the same.
m_displayRounder.SignificantDigits(12);

UpdateSpinButtonPlacement();
UpdateSpinButtonEnabled();

UpdateVisualStateForIsEnabledChange();

if (ReadLocalValue(s_ValueProperty) == winrt::DependencyProperty::UnsetValue()
&& ReadLocalValue(s_TextProperty) != winrt::DependencyProperty::UnsetValue())
{
Expand All @@ -216,7 +220,7 @@ void NumberBox::OnApplyTemplate()
}
}

void NumberBox::OnCornerRadiusPropertyChanged(const winrt::DependencyObject& /*sender*/, const winrt::DependencyProperty& /*args*/)
void NumberBox::OnCornerRadiusPropertyChanged(const winrt::DependencyObject&, const winrt::DependencyProperty&)
{
if (this->SpinButtonPlacementMode() == winrt::NumberBoxSpinButtonPlacementMode::Inline)
{
Expand Down Expand Up @@ -338,6 +342,16 @@ void NumberBox::OnValidationModePropertyChanged(const winrt::DependencyPropertyC
UpdateSpinButtonEnabled();
}

void NumberBox::OnIsEnabledChanged(const winrt::IInspectable&, const winrt::DependencyPropertyChangedEventArgs&)
{
UpdateVisualStateForIsEnabledChange();
}

void NumberBox::UpdateVisualStateForIsEnabledChange()
{
winrt::VisualStateManager::GoToState(*this, IsEnabled() ? L"Normal" : L"Disabled", false);
}

void NumberBox::OnNumberBoxGotFocus(winrt::IInspectable const& sender, winrt::RoutedEventArgs const& args)
{
// When the control receives focus, select the text
Expand Down
7 changes: 6 additions & 1 deletion dev/NumberBox/NumberBox.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ class NumberBox :
void OnNumberBoxGotFocus(winrt::IInspectable const& sender, winrt::RoutedEventArgs const& args);
void OnNumberBoxLostFocus(winrt::IInspectable const& sender, winrt::RoutedEventArgs const& args);
void OnNumberBoxScroll(winrt::IInspectable const& sender, winrt::PointerRoutedEventArgs const& args);
void OnCornerRadiusPropertyChanged(const winrt::DependencyObject& /*sender*/, const winrt::DependencyProperty& /*args*/);
void OnCornerRadiusPropertyChanged(const winrt::DependencyObject&, const winrt::DependencyProperty&);
void OnIsEnabledChanged(const winrt::IInspectable&, const winrt::DependencyPropertyChangedEventArgs&);

void ValidateInput();
void CoerceMinimum();
Expand All @@ -83,6 +84,8 @@ class NumberBox :

void UpdateHeaderPresenterState();

void UpdateVisualStateForIsEnabledChange();

bool IsInBounds(double value);

void MoveCaretToTextEnd();
Expand All @@ -105,4 +108,6 @@ class NumberBox :
winrt::RepeatButton::Click_revoker m_popupDownButtonClickRevoker{};

PropertyChanged_revoker m_cornerRadiusChangedRevoker{};

winrt::Control::IsEnabledChanged_revoker m_isEnabledChangedRevoker{};
};
8 changes: 8 additions & 0 deletions dev/NumberBox/NumberBox.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
<ControlTemplate TargetType="local:NumberBox">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="Disabled">
<VisualState.Setters>
<Setter Target="HeaderContentPresenter.Foreground" Value="{ThemeResource TextControlHeaderForegroundDisabled}"/>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="SpinButtonStates">
<VisualState x:Name="SpinButtonsCollapsed" />

Expand Down