Use VisualState instead of code behind to change the style of Calculator result.

Remove unnecessary calls to update the layout
This commit is contained in:
Rudy Huyn 2019-03-18 01:52:03 -07:00
commit 77b006d5f5
5 changed files with 32 additions and 69 deletions

View file

@ -125,10 +125,6 @@
<!-- Standard Operators Standard/Scientific in Fill/Full -->
<x:Double x:Key="CalcOperatorCaptionSize">15</x:Double>
<x:Double x:Key="CalcResultFontSizeL">72</x:Double>
<x:Double x:Key="CalcResultFontSizeM">46</x:Double>
<x:Double x:Key="CalcResultFontSizeS">28</x:Double>
<!-- Base style for calc buttons -->
<Style x:Key="CalcButtonStyle" TargetType="Controls:CalculatorButton">
<Setter Property="MinWidth" Value="24"/>

View file

@ -184,7 +184,7 @@ void CalculationResult::UpdateVisualState()
{
VisualStateManager::GoToState(this, "Active", true);
}
else
else
{
VisualStateManager::GoToState(this, "Normal", true);
}
@ -338,17 +338,17 @@ void CalculationResult::ShowHideScrollButtons(::Visibility vLeft, ::Visibility v
void CalculationResult::UpdateScrollButtons()
{
// When the width is smaller than the container, don't show any
if (m_textBlock->ActualWidth < m_textContainer->ActualWidth)
if (m_textBlock->ActualWidth < m_textContainer->ActualWidth)
{
ShowHideScrollButtons(::Visibility::Collapsed, ::Visibility::Collapsed);
}
// We have more number on both side. Show both arrows
else if (m_textContainer->HorizontalOffset > 0 && m_textContainer->HorizontalOffset < (m_textContainer->ExtentWidth - m_textContainer->ViewportWidth))
else if (m_textContainer->HorizontalOffset > 0 && m_textContainer->HorizontalOffset < (m_textContainer->ExtentWidth - m_textContainer->ViewportWidth))
{
ShowHideScrollButtons(::Visibility::Visible, ::Visibility::Visible);
}
// Width is larger than the container and left most part of the number is shown. Should be able to scroll left.
else if (m_textContainer->HorizontalOffset == 0)
else if (m_textContainer->HorizontalOffset == 0)
{
ShowHideScrollButtons(::Visibility::Collapsed, ::Visibility::Visible);
}
@ -375,16 +375,9 @@ void CalculationResult::ModifyFontAndMargin(TextBlock^ textBox, double fontChang
{
scaleFactor = SMALLHEIGHTSCALEFACTOR;
}
if (fontChange < 0)
{
newFontSize = max(cur + fontChange, MinFontSize);
m_textContainer->Padding = Thickness(0, 0, 0, scaleFactor * abs(cur - newFontSize));
}
else
{
newFontSize = min(cur + fontChange, MaxFontSize);
m_textContainer->Padding = Thickness(0, 0, 0, scaleFactor * abs(cur - newFontSize));
}
newFontSize = min(max(cur + fontChange, MinFontSize), MaxFontSize);
m_textContainer->Padding = Thickness(0, 0, 0, scaleFactor * abs(cur - newFontSize));
textBox->FontSize = newFontSize;
}

View file

@ -553,25 +553,34 @@
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowHeight="800"/>
</VisualState.StateTriggers>
<Storyboard Completed="OnResultsLayoutChanged"/>
<VisualState.Setters>
<Setter Target="Results.MaxFontSize" Value="72"/>
<Setter Target="Results.MaxExpressionHistoryCharacters" Value="51"/>
<Setter Target="RowResult.MinHeight" Value="108"/>
<Setter Target="RowResult.Height" Value="72*"/>
</VisualState.Setters>
</VisualState>
<VisualState x:Name="ResultsProgM">
<VisualState x:Name="ResultsM">
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowHeight="640"/>
<AdaptiveTrigger x:Name="ResultsMVisualStateTrigger" MinWindowHeight="640"/>
</VisualState.StateTriggers>
<Storyboard Completed="OnResultsLayoutChanged"/>
</VisualState>
<VisualState x:Name="ResultsSciM">
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowHeight="544"/>
</VisualState.StateTriggers>
<Storyboard Completed="OnResultsLayoutChanged"/>
<VisualState.Setters>
<Setter Target="Results.MaxFontSize" Value="46"/>
<Setter Target="Results.MaxExpressionHistoryCharacters" Value="30"/>
<Setter Target="RowResult.MinHeight" Value="72"/>
<Setter Target="RowResult.Height" Value="72*"/>
</VisualState.Setters>
</VisualState>
<VisualState x:Name="ResultsS">
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowHeight="0"/>
</VisualState.StateTriggers>
<Storyboard Completed="OnResultsLayoutChanged"/>
<VisualState.Setters>
<Setter Target="Results.MaxFontSize" Value="26"/>
<Setter Target="Results.MaxExpressionHistoryCharacters" Value="30"/>
<Setter Target="RowResult.MinHeight" Value="42"/>
<Setter Target="RowResult.Height" Value="42*"/>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>

View file

@ -185,16 +185,19 @@ void Calculator::UpdateViewState()
{
state = L"Programmer";
Model->IsDecimalEnabled = false;
ResultsMVisualStateTrigger->MinWindowHeight = 640;
}
else if (IsScientific)
{
state = L"Scientific";
Model->IsDecimalEnabled = true;
}
ResultsMVisualStateTrigger->MinWindowHeight = 544;
}
else
{
state = L"Standard";
Model->IsDecimalEnabled = true;
ResultsMVisualStateTrigger->MinWindowHeight = 1;
}
CloseHistoryFlyout();
@ -203,35 +206,6 @@ void Calculator::UpdateViewState()
VisualStateManager::GoToState(this, ref new String(state.c_str()), true/*useTransitions*/);
}
void Calculator::SetResultStyles()
{
CoreWindow^ window = CoreWindow::GetForCurrentThread();
if (window)
{
float curHeight = window->Bounds.Height;
if (curHeight >= 800)
{
Results->MaxFontSize = (double)Application::Current->Resources->Lookup("CalcResultFontSizeL");
Results->MaxExpressionHistoryCharacters = 51;
RowResult->MinHeight = 108;
RowResult->Height = GridLength(72, GridUnitType::Star);
}
else if ((IsProgrammer && curHeight >= 640) || (IsScientific && curHeight >= 544) || IsStandard)
{
Results->MaxExpressionHistoryCharacters = 30;
Results->MaxFontSize = (double)Application::Current->Resources->Lookup("CalcResultFontSizeM");
RowResult->MinHeight = 72;
RowResult->Height = GridLength(72, GridUnitType::Star);
}
else
{
Results->MaxFontSize = (double)Application::Current->Resources->Lookup("CalcResultFontSizeS");
Results->MaxExpressionHistoryCharacters = 30;
RowResult->MinHeight = 42;
RowResult->Height = GridLength(42, GridUnitType::Star);
}
}
}
void Calculator::AnimateCalculator(bool resultAnimate)
{
@ -276,7 +250,6 @@ void Calculator::OnContextCanceled(UIElement^ sender, RoutedEventArgs^ e)
void Calculator::OnLayoutStateChanged(_In_ Object^ sender, _In_ Object^ e)
{
UpdateViewState();
UpdatePanelViewState();
}
@ -360,7 +333,6 @@ void Calculator::OnStoryboardCompleted(_In_ Object^ sender, _In_ Object^ e)
AnimateWithoutResult->Begin();
}
}
SetResultStyles();
}
void Calculator::EnsureScientific()
@ -727,11 +699,6 @@ void Calculator::DockPanelTapped(_In_ TappedRoutedEventArgs^ e)
m_IsLastFlyoutHistory = false;
}
void Calculator::OnResultsLayoutChanged(_In_ Object^ sender, _In_ Object^ e)
{
SetResultStyles();
}
void Calculator::UnregisterEventHandlers()
{
expressionText->UnregisterEventHandlers();

View file

@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#pragma once
@ -133,8 +133,6 @@ namespace CalculatorApp
void expressionContainer_LayoutUpdated(_In_ Platform::Object^ sender, _In_ Platform::Object^ e);
bool IsValidRegularExpression(std::wstring str);
void DockPanelTapped(_In_ Windows::UI::Xaml::Input::TappedRoutedEventArgs^ e);
void OnResultsLayoutChanged(_In_ Platform::Object^ sender, _In_ Platform::Object^ e);
void SetResultStyles();
void OnErrorLayoutCompleted(_In_ Platform::Object^ sender, _In_ Platform::Object^ e);
void OnHistoryAccessKeyInvoked(_In_ Windows::UI::Xaml::UIElement^ sender, _In_ Windows::UI::Xaml::Input::AccessKeyInvokedEventArgs^ args);
void OnMemoryAccessKeyInvoked(_In_ Windows::UI::Xaml::UIElement^ sender, _In_ Windows::UI::Xaml::Input::AccessKeyInvokedEventArgs^ args);