Optimize and simplify CalculationResult to be able to update the FontSize and the DisplayMargin without fully updating the Style. (#217)

- Merge the 3 CalculationResultStyle(S|M|L) in App.xaml
- Only modify CalculationResult::*FontSize in Calculator.xaml instead of fully updating the style of the control.
- Create a new property MaxFontSize in order to be able to update it without being forced to fully update the Style (because m_startingFontSize was set in OnApplyTemplate)
- Modify how DisplayMargin is managed to prevent the textblock Margin to shift when we update its value (without fully updating the Style).
This commit is contained in:
Rudy Huyn
2019-04-05 13:04:39 -07:00
committed by Daniel Belcher
parent e7eace57f8
commit af41a183a7
7 changed files with 60 additions and 308 deletions

View File

@@ -28,6 +28,7 @@ using namespace std;
DEPENDENCY_PROPERTY_INITIALIZATION(CalculationResult, IsActive);
DEPENDENCY_PROPERTY_INITIALIZATION(CalculationResult, AccentColor);
DEPENDENCY_PROPERTY_INITIALIZATION(CalculationResult, MinFontSize);
DEPENDENCY_PROPERTY_INITIALIZATION(CalculationResult, MaxFontSize);
DEPENDENCY_PROPERTY_INITIALIZATION(CalculationResult, DisplayMargin);
DEPENDENCY_PROPERTY_INITIALIZATION(CalculationResult, MaxExpressionHistoryCharacters);
DEPENDENCY_PROPERTY_INITIALIZATION(CalculationResult, ExpressionVisibility);
@@ -50,7 +51,6 @@ StringReference CalculationResult::s_FocusedState(L"Focused");
StringReference CalculationResult::s_UnfocusedState(L"Unfocused");
CalculationResult::CalculationResult():
m_startingFontSize(0.0),
m_isScalingText(false),
m_haveCalculatedMax(false)
{
@@ -95,7 +95,6 @@ void CalculationResult::OnApplyTemplate()
if (m_textBlock)
{
m_textBlock->Visibility = ::Visibility::Visible;
m_startingFontSize = m_textBlock->FontSize;
}
}
UpdateAllState();
@@ -143,6 +142,16 @@ void CalculationResult::OnDisplayValuePropertyChanged(String^ /*oldValue*/, Stri
UpdateTextState();
}
void CalculationResult::OnMinFontSizePropertyChanged(double /*oldValue*/, double /*newValue*/)
{
UpdateTextState();
}
void CalculationResult::OnMaxFontSizePropertyChanged(double /*oldValue*/, double /*newValue*/)
{
UpdateTextState();
}
void CalculationResult::OnIsInErrorPropertyChanged(bool /*oldValue*/, bool newValue)
{
// We need to have a good template for this to work
@@ -212,7 +221,7 @@ void CalculationResult::UpdateTextState()
{
fontSizeChange = min<double>(max<double>(floor(WIDTHTOFONTSCALAR * widthDiff) - WIDTHTOFONTOFFSET, INCREMENTOFFSET), MAXFONTINCREMENT);
}
if (m_textBlock->ActualWidth < containerSize && abs(m_textBlock->FontSize - m_startingFontSize) > FONTTOLERANCE && !m_haveCalculatedMax)
if (m_textBlock->ActualWidth < containerSize && abs(m_textBlock->FontSize - MaxFontSize) > FONTTOLERANCE && !m_haveCalculatedMax)
{
ModifyFontAndMargin(m_textBlock, fontSizeChange);
m_textBlock->InvalidateArrange();
@@ -228,7 +237,7 @@ void CalculationResult::UpdateTextState()
m_textBlock->InvalidateArrange();
return;
}
assert(m_textBlock->FontSize >= MinFontSize && m_textBlock->FontSize <= m_startingFontSize);
assert(m_textBlock->FontSize >= MinFontSize && m_textBlock->FontSize <= MaxFontSize);
m_isScalingText = false;
if (IsOperatorCommand)
{
@@ -361,24 +370,15 @@ void CalculationResult::ModifyFontAndMargin(TextBlock^ textBox, double fontChang
{
double cur = textBox->FontSize;
double newFontSize = 0.0;
Thickness t = textBox->Margin;
double scaleFactor = SCALEFACTOR;
if (m_textContainer->ActualHeight <= HEIGHTCUTOFF)
{
scaleFactor = SMALLHEIGHTSCALEFACTOR;
}
if (fontChange < 0)
{
newFontSize = max(cur + fontChange, MinFontSize);
t.Bottom += scaleFactor * abs(cur - newFontSize);
}
else
{
newFontSize = min(cur + fontChange, m_startingFontSize);
t.Bottom -= 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;
textBox->Margin = t;
}
void CalculationResult::UpdateAllState()

View File

@@ -19,7 +19,8 @@ namespace CalculatorApp
DEPENDENCY_PROPERTY_OWNER(CalculationResult);
DEPENDENCY_PROPERTY(Windows::UI::Xaml::Visibility, ExpressionVisibility);
DEPENDENCY_PROPERTY(double, MinFontSize);
DEPENDENCY_PROPERTY_WITH_DEFAULT_AND_CALLBACK(double, MinFontSize, 0.0);
DEPENDENCY_PROPERTY_WITH_DEFAULT_AND_CALLBACK(double, MaxFontSize, 30.0);
DEPENDENCY_PROPERTY(Windows::UI::Xaml::Thickness, DisplayMargin);
DEPENDENCY_PROPERTY(int, MaxExpressionHistoryCharacters);
DEPENDENCY_PROPERTY_WITH_CALLBACK(bool, IsActive);
@@ -52,6 +53,8 @@ namespace CalculatorApp
void OnAccentColorPropertyChanged(Windows::UI::Xaml::Media::Brush^ oldValue, Windows::UI::Xaml::Media::Brush^ newValue);
void OnDisplayValuePropertyChanged(Platform::String^ oldValue, Platform::String^ newValue);
void OnIsInErrorPropertyChanged(bool oldValue, bool newValue);
void OnMinFontSizePropertyChanged(double oldValue, double newValue);
void OnMaxFontSizePropertyChanged(double oldValue, double newValue);
void TextContainerSizeChanged(Object^ sender, Windows::UI::Xaml::SizeChangedEventArgs^ e);
void OnTextContainerLayoutUpdated(Object^ sender, Object^ e);
void UpdateVisualState();
@@ -74,7 +77,6 @@ namespace CalculatorApp
Windows::UI::Xaml::Controls::TextBlock^ m_textBlock;
Windows::UI::Xaml::Controls::HyperlinkButton^ m_scrollLeft;
Windows::UI::Xaml::Controls::HyperlinkButton^ m_scrollRight;
double m_startingFontSize;
double scrollRatio = 0.7;
bool m_isScalingText;
bool m_haveCalculatedMax;