From ea8b112f94c9f8edd5096330937af591433a9ee4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Laban?= Date: Fri, 24 May 2019 09:23:10 -0400 Subject: [PATCH] Add missing property changed handers --- .../Controls/CalculationResult.cs | 947 +++++++++--------- .../Controls/CalculatorButton.cs | 36 +- src/Calculator.Shared/Controls/FlipButtons.cs | 11 +- .../Controls/OverflowTextBlock.cs | 427 ++++---- .../CalculatorScientificOperators.xaml.cs | 11 +- .../Views/OperatorsPanel.xaml.cs | 150 +-- 6 files changed, 829 insertions(+), 753 deletions(-) diff --git a/src/Calculator.Shared/Controls/CalculationResult.cs b/src/Calculator.Shared/Controls/CalculationResult.cs index 11f5b406..4dd734ff 100644 --- a/src/Calculator.Shared/Controls/CalculationResult.cs +++ b/src/Calculator.Shared/Controls/CalculationResult.cs @@ -14,526 +14,555 @@ using CalculatorApp.ViewModel; namespace CalculatorApp { - namespace Controls - { - public delegate void SelectedEventHandler(object sender); + namespace Controls + { + public delegate void SelectedEventHandler(object sender); - public sealed partial class CalculationResult : Windows.UI.Xaml.Controls.Control, IActivatable - { - public Visibility ExpressionVisibility - { - get { return (Visibility)GetValue(ExpressionVisibilityProperty); } - set { SetValue(ExpressionVisibilityProperty, value); } - } + public sealed partial class CalculationResult : Windows.UI.Xaml.Controls.Control, IActivatable + { + public Visibility ExpressionVisibility + { + get => (Visibility)GetValue(ExpressionVisibilityProperty); + set => SetValue(ExpressionVisibilityProperty, value); + } - public static readonly DependencyProperty ExpressionVisibilityProperty = - DependencyProperty.Register("ExpressionVisibility", typeof(Visibility), typeof(CalculationResult), new PropertyMetadata(Visibility.Collapsed)); + public static readonly DependencyProperty ExpressionVisibilityProperty = + DependencyProperty.Register("ExpressionVisibility", typeof(Visibility), typeof(CalculationResult), new PropertyMetadata(Visibility.Collapsed)); - public double MinFontSize - { - get { return (double)GetValue(MinFontSizeProperty); } - set { SetValue(MinFontSizeProperty, value); } - } + public double MinFontSize + { + get => (double)GetValue(MinFontSizeProperty); + set => SetValue(MinFontSizeProperty, value); + } - public static readonly DependencyProperty MinFontSizeProperty = - DependencyProperty.Register("MinFontSize", typeof(double), typeof(CalculationResult), new PropertyMetadata(0.0)); + public static readonly DependencyProperty MinFontSizeProperty = + DependencyProperty.Register( + name: "MinFontSize", + propertyType: typeof(double), + ownerType: typeof(CalculationResult), + typeMetadata: new PropertyMetadata( + defaultValue: 0.0, + propertyChangedCallback: (s, e) => (s as CalculationResult)?.OnMinFontSizePropertyChanged( + (double)e.OldValue, + (double)e.NewValue))); + + public double MaxFontSize + { + get => (double)GetValue(MaxFontSizeProperty); + set => SetValue(MaxFontSizeProperty, value); + } + + public static readonly DependencyProperty MaxFontSizeProperty = + DependencyProperty.Register( + name: "MaxFontSize", + propertyType: typeof(double), + ownerType: typeof(CalculationResult), + typeMetadata: new PropertyMetadata( + defaultValue: 30.0, + propertyChangedCallback: (s, e) => (s as CalculationResult)?.OnMaxFontSizePropertyChanged( + (double)e.OldValue, + (double)e.NewValue))); + + public Thickness DisplayMargin + { + get => (Thickness)GetValue(DisplayMarginProperty); + set => SetValue(DisplayMarginProperty, value); + } + + public static readonly DependencyProperty DisplayMarginProperty = + DependencyProperty.Register("DisplayMargin", typeof(Thickness), typeof(CalculationResult), new PropertyMetadata(default(Thickness))); + public int MaxExpressionHistoryCharacters + { + get => (int)GetValue(MaxExpressionHistoryCharactersProperty); + set => SetValue(MaxExpressionHistoryCharactersProperty, value); + } - public double MaxFontSize - { - get { return (double)GetValue(MaxFontSizeProperty); } - set { SetValue(MaxFontSizeProperty, value); } - } + public static readonly DependencyProperty MaxExpressionHistoryCharactersProperty = + DependencyProperty.Register("MaxExpressionHistoryCharacters", typeof(int), typeof(CalculationResult), new PropertyMetadata(0)); - public static readonly DependencyProperty MaxFontSizeProperty = - DependencyProperty.Register("MaxFontSize", typeof(double), typeof(CalculationResult), new PropertyMetadata(30.0)); + public bool IsActive + { + get => (bool)GetValue(IsActiveProperty); + set => SetValue(IsActiveProperty, value); + } - - - public Thickness DisplayMargin - { - get { return (Thickness)GetValue(DisplayMarginProperty); } - set { SetValue(DisplayMarginProperty, value); } - } - - public static readonly DependencyProperty DisplayMarginProperty = - DependencyProperty.Register("DisplayMargin", typeof(Thickness), typeof(CalculationResult), new PropertyMetadata(default(Thickness))); - - - - public int MaxExpressionHistoryCharacters - { - get { return (int)GetValue(MaxExpressionHistoryCharactersProperty); } - set { SetValue(MaxExpressionHistoryCharactersProperty, value); } - } - - public static readonly DependencyProperty MaxExpressionHistoryCharactersProperty = - DependencyProperty.Register("MaxExpressionHistoryCharacters", typeof(int), typeof(CalculationResult), new PropertyMetadata(0)); - - - - public bool IsActive - { - get { return (bool)GetValue(IsActiveProperty); } - set { SetValue(IsActiveProperty, value); } - } - - public static readonly DependencyProperty IsActiveProperty = - DependencyProperty.Register("IsActive", typeof(bool), typeof(CalculationResult), new PropertyMetadata(false)); + public static readonly DependencyProperty IsActiveProperty = + DependencyProperty.Register( + name: "IsActive", + propertyType: typeof(bool), + ownerType: typeof(CalculationResult), + typeMetadata: new PropertyMetadata( + defaultValue: false, + propertyChangedCallback: (s, e) => (s as CalculationResult)?.OnIsActivePropertyChanged( + (bool)e.OldValue, + (bool)e.NewValue))); - public string DisplayValue - { - get { return (string)GetValue(DisplayValueProperty); } - set { SetValue(DisplayValueProperty, value); } - } + public string DisplayValue + { + get => (string)GetValue(DisplayValueProperty); + set => SetValue(DisplayValueProperty, value); + } - // Using a DependencyProperty as the backing store for DisplayValue. This enables animation, styling, binding, etc... - public static readonly DependencyProperty DisplayValueProperty = - DependencyProperty.Register("DisplayValue", typeof(string), typeof(CalculationResult), new PropertyMetadata("")); + // Using a DependencyProperty as the backing store for DisplayValue. This enables animation, styling, binding, etc... + public static readonly DependencyProperty DisplayValueProperty = + DependencyProperty.Register( + name: "DisplayValue", + propertyType: typeof(string), + ownerType: typeof(CalculationResult), + typeMetadata: new PropertyMetadata( + defaultValue: "", + propertyChangedCallback: (s, e) => (s as CalculationResult)?.OnDisplayValuePropertyChanged( + oldValue: e.OldValue as string, + newValue: e.NewValue as string))); + + public string DisplayStringExpression + { + get => (string)GetValue(DisplayStringExpressionProperty); + set => SetValue(DisplayStringExpressionProperty, value); + } + + // Using a DependencyProperty as the backing store for DisplayStringExpression. This enables animation, styling, binding, etc... + public static readonly DependencyProperty DisplayStringExpressionProperty = + DependencyProperty.Register("DisplayStringExpression", typeof(string), typeof(CalculationResult), new PropertyMetadata("")); + + public bool IsInError + { + get => (bool)GetValue(IsInErrorProperty); + set => SetValue(IsInErrorProperty, value); + } + + // Using a DependencyProperty as the backing store for IsInError. This enables animation, styling, binding, etc... + public static readonly DependencyProperty IsInErrorProperty = + DependencyProperty.Register( + name: "IsInError", + propertyType: typeof(bool), + ownerType: typeof(CalculationResult), + typeMetadata: new PropertyMetadata( + defaultValue: false, + propertyChangedCallback: (s, e) => (s as CalculationResult)?.OnIsInErrorPropertyChanged( + oldValue: (bool)e.OldValue, + newValue: (bool)e.NewValue))); + + public bool IsOperatorCommand + { + get => (bool)GetValue(IsOperatorCommandProperty); + set => SetValue(IsOperatorCommandProperty, value); + } + + // Using a DependencyProperty as the backing store for IsOperatorCommand. This enables animation, styling, binding, etc... + public static readonly DependencyProperty IsOperatorCommandProperty = + DependencyProperty.Register("IsOperatorCommand", typeof(bool), typeof(CalculationResult), new PropertyMetadata(false)); + + public event SelectedEventHandler ValueSelected; + + + private Windows.UI.Xaml.Controls.ScrollViewer m_textContainer; + private Windows.UI.Xaml.Controls.TextBlock m_textBlock; + private Windows.UI.Xaml.Controls.HyperlinkButton m_scrollLeft; + private Windows.UI.Xaml.Controls.HyperlinkButton m_scrollRight; + private double scrollRatio = 0.7; + private bool m_isScalingText; + private bool m_haveCalculatedMax; - public string DisplayStringExpression - { - get { return (string)GetValue(DisplayStringExpressionProperty); } - set { SetValue(DisplayStringExpressionProperty, value); } - } + const double SCALEFACTOR = 0.357143; + const int SMALLHEIGHTSCALEFACTOR = 0; + const int HEIGHTCUTOFF = 100; + const int INCREMENTOFFSET = 1; + const int MAXFONTINCREMENT = 5; + const double WIDTHTOFONTSCALAR = 0.0556513; + const int WIDTHTOFONTOFFSET = 3; + const int WIDTHCUTOFF = 50; + const double FONTTOLERANCE = 0.001; - // Using a DependencyProperty as the backing store for DisplayStringExpression. This enables animation, styling, binding, etc... - public static readonly DependencyProperty DisplayStringExpressionProperty = - DependencyProperty.Register("DisplayStringExpression", typeof(string), typeof(CalculationResult), new PropertyMetadata("")); + // Visual states for focused + static string s_FocusedState = "Focused"; + static string s_UnfocusedState = "Unfocused"; - public bool IsInError - { - get { return (bool)GetValue(IsInErrorProperty); } - set { SetValue(IsInErrorProperty, value); } - } - - // Using a DependencyProperty as the backing store for IsInError. This enables animation, styling, binding, etc... - public static readonly DependencyProperty IsInErrorProperty = - DependencyProperty.Register("IsInError", typeof(bool), typeof(CalculationResult), new PropertyMetadata(false)); - - - - public bool IsOperatorCommand - { - get { return (bool)GetValue(IsOperatorCommandProperty); } - set { SetValue(IsOperatorCommandProperty, value); } - } - - // Using a DependencyProperty as the backing store for IsOperatorCommand. This enables animation, styling, binding, etc... - public static readonly DependencyProperty IsOperatorCommandProperty = - DependencyProperty.Register("IsOperatorCommand", typeof(bool), typeof(CalculationResult), new PropertyMetadata(false)); - - public event SelectedEventHandler ValueSelected; - - - private Windows.UI.Xaml.Controls.ScrollViewer m_textContainer; - private Windows.UI.Xaml.Controls.TextBlock m_textBlock; - private Windows.UI.Xaml.Controls.HyperlinkButton m_scrollLeft; - private Windows.UI.Xaml.Controls.HyperlinkButton m_scrollRight; - private double scrollRatio = 0.7; - private bool m_isScalingText; - private bool m_haveCalculatedMax; - - - - const double SCALEFACTOR = 0.357143; - const int SMALLHEIGHTSCALEFACTOR = 0; - const int HEIGHTCUTOFF = 100; - const int INCREMENTOFFSET = 1; - const int MAXFONTINCREMENT = 5; - const double WIDTHTOFONTSCALAR = 0.0556513; - const int WIDTHTOFONTOFFSET = 3; - const int WIDTHCUTOFF = 50; - const double FONTTOLERANCE = 0.001; - - // Visual states for focused - static string s_FocusedState = "Focused"; - static string s_UnfocusedState = "Unfocused"; - - public CalculationResult() - { - m_isScalingText = false; - m_haveCalculatedMax = false; - } + public CalculationResult() + { + m_isScalingText = false; + m_haveCalculatedMax = false; + } public string GetRawDisplayValue() - { - string rawValue = null; + { + string rawValue = null; - LocalizationSettings.GetInstance().RemoveGroupSeparators(DisplayValue, DisplayValue.Length, ref rawValue); + LocalizationSettings.GetInstance().RemoveGroupSeparators(DisplayValue, DisplayValue.Length, ref rawValue); - return rawValue; - } + return rawValue; + } - protected override void OnApplyTemplate() - { - System.Diagnostics.Debug.Assert((m_scrollLeft == null && m_scrollRight == null) || (m_scrollLeft != null && m_scrollRight != null)); - if (m_textContainer != null) - { - // UNO TODO - // m_textContainer.LayoutUpdated -= m_textContainerLayoutChangedToken; - } - m_textContainer = (ScrollViewer)(GetTemplateChild("TextContainer")); - if (m_textContainer != null) - { - m_textContainer.SizeChanged += TextContainerSizeChanged; - // We want to know when the size of the container changes so - // we can rescale the textbox - m_textContainer.LayoutUpdated += OnTextContainerLayoutUpdated; + protected override void OnApplyTemplate() + { + System.Diagnostics.Debug.Assert((m_scrollLeft == null && m_scrollRight == null) || (m_scrollLeft != null && m_scrollRight != null)); + if (m_textContainer != null) + { + // UNO TODO + // m_textContainer.LayoutUpdated -= m_textContainerLayoutChangedToken; + } + m_textContainer = (ScrollViewer)(GetTemplateChild("TextContainer")); + if (m_textContainer != null) + { + m_textContainer.SizeChanged += TextContainerSizeChanged; + // We want to know when the size of the container changes so + // we can rescale the textbox + m_textContainer.LayoutUpdated += OnTextContainerLayoutUpdated; - m_textContainer.ChangeView(m_textContainer.ExtentWidth - m_textContainer.ViewportWidth, null, null); - m_scrollLeft = (HyperlinkButton)(GetTemplateChild("ScrollLeft")); - m_scrollRight = (HyperlinkButton)(GetTemplateChild("ScrollRight")); - var borderContainer = (UIElement)(GetTemplateChild("Border")); - if (m_scrollLeft != null && m_scrollRight != null) - { - m_scrollLeft.Click += OnScrollClick; - m_scrollRight.Click += OnScrollClick; - borderContainer.PointerEntered += OnPointerEntered; - borderContainer.PointerExited += OnPointerExited; - } - m_textBlock = (TextBlock)(m_textContainer.FindName("NormalOutput")); - if (m_textBlock != null) - { - m_textBlock.Visibility = Visibility.Visible; - } - } - UpdateAllState(); - VisualStateManager.GoToState(this, s_UnfocusedState, false); - } + m_textContainer.ChangeView(m_textContainer.ExtentWidth - m_textContainer.ViewportWidth, null, null); + m_scrollLeft = (HyperlinkButton)(GetTemplateChild("ScrollLeft")); + m_scrollRight = (HyperlinkButton)(GetTemplateChild("ScrollRight")); + var borderContainer = (UIElement)(GetTemplateChild("Border")); + if (m_scrollLeft != null && m_scrollRight != null) + { + m_scrollLeft.Click += OnScrollClick; + m_scrollRight.Click += OnScrollClick; + borderContainer.PointerEntered += OnPointerEntered; + borderContainer.PointerExited += OnPointerExited; + } + m_textBlock = (TextBlock)(m_textContainer.FindName("NormalOutput")); + if (m_textBlock != null) + { + m_textBlock.Visibility = Visibility.Visible; + } + } + UpdateAllState(); + VisualStateManager.GoToState(this, s_UnfocusedState, false); + } - void OnPointerPressed(PointerRoutedEventArgs e) - { - if (m_scrollLeft != null && m_scrollRight != null && e.Pointer.PointerDeviceType == PointerDeviceType.Touch) - { - ShowHideScrollButtons(Visibility.Collapsed, Visibility.Collapsed); - } - } + void OnPointerPressed(PointerRoutedEventArgs e) + { + if (m_scrollLeft != null && m_scrollRight != null && e.Pointer.PointerDeviceType == PointerDeviceType.Touch) + { + ShowHideScrollButtons(Visibility.Collapsed, Visibility.Collapsed); + } + } - void OnTextContainerLayoutUpdated(object sender, object e) - { - if (m_isScalingText) - { - UpdateTextState(); - } - } + void OnTextContainerLayoutUpdated(object sender, object e) + { + if (m_isScalingText) + { + UpdateTextState(); + } + } - void TextContainerSizeChanged(object sender, SizeChangedEventArgs e) - { - UpdateTextState(); - } + void TextContainerSizeChanged(object sender, SizeChangedEventArgs e) + { + UpdateTextState(); + } - void OnIsActivePropertyChanged(bool oldValue, bool newValue) - { - UpdateVisualState(); - } + void OnIsActivePropertyChanged(bool oldValue, bool newValue) + { + UpdateVisualState(); + } - void OnAccentColorPropertyChanged(Brush oldValue, Brush newValue) - { - // Force the "Active" transition to happen again - if (IsActive) - { - VisualStateManager.GoToState(this, "Normal", true); - VisualStateManager.GoToState(this, "Active", true); - } - } + void OnAccentColorPropertyChanged(Brush oldValue, Brush newValue) + { + // Force the "Active" transition to happen again + if (IsActive) + { + VisualStateManager.GoToState(this, "Normal", true); + VisualStateManager.GoToState(this, "Active", true); + } + } - void OnDisplayValuePropertyChanged(string oldValue, string newValue) - { - UpdateTextState(); - } + void OnDisplayValuePropertyChanged(string oldValue, string newValue) + { + UpdateTextState(); + } - void OnMinFontSizePropertyChanged(double oldValue, double newValue) - { - UpdateTextState(); - } + void OnMinFontSizePropertyChanged(double oldValue, double newValue) + { + UpdateTextState(); + } - void OnMaxFontSizePropertyChanged(double oldValue, double newValue) - { - UpdateTextState(); - } + void OnMaxFontSizePropertyChanged(double oldValue, double newValue) + { + UpdateTextState(); + } - void OnIsInErrorPropertyChanged(bool oldValue, bool newValue) - { - // We need to have a good template for this to work - if (m_textBlock == null) - { - return; - } + void OnIsInErrorPropertyChanged(bool oldValue, bool newValue) + { + // We need to have a good template for this to work + if (m_textBlock == null) + { + return; + } - if (newValue) - { - // If there's an error message we need to override the normal display font - // with the font appropriate for this language. This is because the error - // message is localized and therefore can contain characters that are not - // available in the normal font. - // We use UIText as the font type because this is the most common font type to use - m_textBlock.FontFamily = LocalizationService.GetInstance().GetLanguageFontFamilyForType(LanguageFontType.UIText); - } - else - { - // The error result is no longer an error so we will restore the - // value to FontFamily property to the value provided in the style - // for the TextBlock in the template. - m_textBlock.ClearValue(TextBlock.FontFamilyProperty); - } - } + if (newValue) + { + // If there's an error message we need to override the normal display font + // with the font appropriate for this language. This is because the error + // message is localized and therefore can contain characters that are not + // available in the normal font. + // We use UIText as the font type because this is the most common font type to use + m_textBlock.FontFamily = LocalizationService.GetInstance().GetLanguageFontFamilyForType(LanguageFontType.UIText); + } + else + { + // The error result is no longer an error so we will restore the + // value to FontFamily property to the value provided in the style + // for the TextBlock in the template. + m_textBlock.ClearValue(TextBlock.FontFamilyProperty); + } + } - void UpdateVisualState() - { - if (IsActive) - { - VisualStateManager.GoToState(this, "Active", true); - } - else - { - VisualStateManager.GoToState(this, "Normal", true); - } - } + void UpdateVisualState() + { + if (IsActive) + { + VisualStateManager.GoToState(this, "Active", true); + } + else + { + VisualStateManager.GoToState(this, "Normal", true); + } + } public void UpdateTextState() - { - if ((m_textContainer == null) || (m_textBlock == null)) - { - return; - } + { + if ((m_textContainer == null) || (m_textBlock == null)) + { + return; + } - var containerSize = m_textContainer.ActualWidth; - string oldText = m_textBlock.Text; - string newText = Utils.LRO + DisplayValue + Utils.PDF; + var containerSize = m_textContainer.ActualWidth; + string oldText = m_textBlock.Text; + string newText = Utils.LRO + DisplayValue + Utils.PDF; - // Initiate the scaling operation - // UpdateLayout will keep calling us until we make it through the below 2 if-statements - if (!m_isScalingText || oldText != newText) - { - m_textBlock.Text = newText; + // Initiate the scaling operation + // UpdateLayout will keep calling us until we make it through the below 2 if-statements + if (!m_isScalingText || oldText != newText) + { + m_textBlock.Text = newText; - m_isScalingText = true; - m_haveCalculatedMax = false; - m_textBlock.InvalidateArrange(); - return; - } - if (containerSize > 0) - { - double widthDiff = Math.Abs(m_textBlock.ActualWidth - containerSize); - double fontSizeChange = INCREMENTOFFSET; + m_isScalingText = true; + m_haveCalculatedMax = false; + m_textBlock.InvalidateArrange(); + return; + } + if (containerSize > 0) + { + double widthDiff = Math.Abs(m_textBlock.ActualWidth - containerSize); + double fontSizeChange = INCREMENTOFFSET; - if (widthDiff > WIDTHCUTOFF) - { - fontSizeChange = Math.Min(Math.Max(Math.Floor(WIDTHTOFONTSCALAR * widthDiff) - WIDTHTOFONTOFFSET, INCREMENTOFFSET), MAXFONTINCREMENT); - } - if (m_textBlock.ActualWidth < containerSize && Math.Abs(m_textBlock.FontSize - MaxFontSize) > FONTTOLERANCE && !m_haveCalculatedMax) - { - ModifyFontAndMargin(m_textBlock, fontSizeChange); - m_textBlock.InvalidateArrange(); - return; - } - if (fontSizeChange < 5) - { - m_haveCalculatedMax = true; - } - if (m_textBlock.ActualWidth >= containerSize && Math.Abs(m_textBlock.FontSize - MinFontSize) > FONTTOLERANCE) - { - ModifyFontAndMargin(m_textBlock, -1 * fontSizeChange); - m_textBlock.InvalidateArrange(); - return; - } - System.Diagnostics.Debug.Assert(m_textBlock.FontSize >= MinFontSize && m_textBlock.FontSize <= MaxFontSize); - m_isScalingText = false; - if (IsOperatorCommand) - { - m_textContainer.ChangeView(0.0, null, null); - } - else - { - m_textContainer.ChangeView(m_textContainer.ExtentWidth - m_textContainer.ViewportWidth, null, null); - } + if (widthDiff > WIDTHCUTOFF) + { + fontSizeChange = Math.Min(Math.Max(Math.Floor(WIDTHTOFONTSCALAR * widthDiff) - WIDTHTOFONTOFFSET, INCREMENTOFFSET), MAXFONTINCREMENT); + } + if (m_textBlock.ActualWidth < containerSize && Math.Abs(m_textBlock.FontSize - MaxFontSize) > FONTTOLERANCE && !m_haveCalculatedMax) + { + ModifyFontAndMargin(m_textBlock, fontSizeChange); + m_textBlock.InvalidateArrange(); + return; + } + if (fontSizeChange < 5) + { + m_haveCalculatedMax = true; + } + if (m_textBlock.ActualWidth >= containerSize && Math.Abs(m_textBlock.FontSize - MinFontSize) > FONTTOLERANCE) + { + ModifyFontAndMargin(m_textBlock, -1 * fontSizeChange); + m_textBlock.InvalidateArrange(); + return; + } + System.Diagnostics.Debug.Assert(m_textBlock.FontSize >= MinFontSize && m_textBlock.FontSize <= MaxFontSize); + m_isScalingText = false; + if (IsOperatorCommand) + { + m_textContainer.ChangeView(0.0, null, null); + } + else + { + m_textContainer.ChangeView(m_textContainer.ExtentWidth - m_textContainer.ViewportWidth, null, null); + } - if (m_scrollLeft != null && m_scrollRight != null) - { - if (m_textBlock.ActualWidth < containerSize) - { - ShowHideScrollButtons(Visibility.Collapsed, Visibility.Collapsed); - } - else - { - if (IsOperatorCommand) - { - ShowHideScrollButtons(Visibility.Collapsed, Visibility.Visible); - } - else - { - ShowHideScrollButtons(Visibility.Visible, Visibility.Collapsed); - } - } - } - m_textBlock.InvalidateArrange(); - } - } + if (m_scrollLeft != null && m_scrollRight != null) + { + if (m_textBlock.ActualWidth < containerSize) + { + ShowHideScrollButtons(Visibility.Collapsed, Visibility.Collapsed); + } + else + { + if (IsOperatorCommand) + { + ShowHideScrollButtons(Visibility.Collapsed, Visibility.Visible); + } + else + { + ShowHideScrollButtons(Visibility.Visible, Visibility.Collapsed); + } + } + } + m_textBlock.InvalidateArrange(); + } + } - void ScrollLeft() - { - if (m_textContainer.HorizontalOffset > 0) - { - double offset = m_textContainer.HorizontalOffset - (scrollRatio * m_textContainer.ViewportWidth); - m_textContainer.ChangeView(offset, null, null); - m_textContainer.UpdateLayout(); - UpdateScrollButtons(); - } - } + void ScrollLeft() + { + if (m_textContainer.HorizontalOffset > 0) + { + double offset = m_textContainer.HorizontalOffset - (scrollRatio * m_textContainer.ViewportWidth); + m_textContainer.ChangeView(offset, null, null); + m_textContainer.UpdateLayout(); + UpdateScrollButtons(); + } + } - void ScrollRight() - { - if (m_textContainer.HorizontalOffset < m_textContainer.ExtentWidth - m_textContainer.ViewportWidth) - { - double offset = m_textContainer.HorizontalOffset + (scrollRatio * m_textContainer.ViewportWidth); - m_textContainer.ChangeView(offset, null, null); - m_textContainer.UpdateLayout(); - UpdateScrollButtons(); - } - } + void ScrollRight() + { + if (m_textContainer.HorizontalOffset < m_textContainer.ExtentWidth - m_textContainer.ViewportWidth) + { + double offset = m_textContainer.HorizontalOffset + (scrollRatio * m_textContainer.ViewportWidth); + m_textContainer.ChangeView(offset, null, null); + m_textContainer.UpdateLayout(); + UpdateScrollButtons(); + } + } - void OnKeyDown(KeyRoutedEventArgs e) - { - if (m_scrollLeft != null && m_scrollRight != null) - { - var key = e.Key; - if (key == Windows.System.VirtualKey.Left) - { - this.ScrollLeft(); - } - else if (key == Windows.System.VirtualKey.Right) - { - this.ScrollRight(); - } - } - } + void OnKeyDown(KeyRoutedEventArgs e) + { + if (m_scrollLeft != null && m_scrollRight != null) + { + var key = e.Key; + if (key == Windows.System.VirtualKey.Left) + { + this.ScrollLeft(); + } + else if (key == Windows.System.VirtualKey.Right) + { + this.ScrollRight(); + } + } + } - void OnScrollClick(object sender, RoutedEventArgs e) - { - var clicked = (HyperlinkButton)(sender); - if (clicked == m_scrollLeft) - { - this.ScrollLeft(); - } - else - { - this.ScrollRight(); - } - } + void OnScrollClick(object sender, RoutedEventArgs e) + { + var clicked = (HyperlinkButton)(sender); + if (clicked == m_scrollLeft) + { + this.ScrollLeft(); + } + else + { + this.ScrollRight(); + } + } - void OnPointerEntered(object sender, PointerRoutedEventArgs e) - { - if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse && m_textBlock.ActualWidth >= m_textContainer.ActualWidth) - { - UpdateScrollButtons(); - } - } + void OnPointerEntered(object sender, PointerRoutedEventArgs e) + { + if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse && m_textBlock.ActualWidth >= m_textContainer.ActualWidth) + { + UpdateScrollButtons(); + } + } - void ShowHideScrollButtons(Visibility vLeft, Visibility vRight) - { - m_scrollLeft.Visibility = vLeft; - m_scrollRight.Visibility = vRight; - } + void ShowHideScrollButtons(Visibility vLeft, Visibility vRight) + { + m_scrollLeft.Visibility = vLeft; + m_scrollRight.Visibility = vRight; + } - void UpdateScrollButtons() - { - // When the width is smaller than the container, don't show any - 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)) - { - 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) - { - ShowHideScrollButtons(Visibility.Collapsed, Visibility.Visible); - } - else // Width is larger than the container and right most part of the number is shown. Should be able to scroll left. - { - ShowHideScrollButtons(Visibility.Visible, Visibility.Collapsed); - } - } + void UpdateScrollButtons() + { + // When the width is smaller than the container, don't show any + 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)) + { + 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) + { + ShowHideScrollButtons(Visibility.Collapsed, Visibility.Visible); + } + else // Width is larger than the container and right most part of the number is shown. Should be able to scroll left. + { + ShowHideScrollButtons(Visibility.Visible, Visibility.Collapsed); + } + } - void OnPointerExited(object sender, PointerRoutedEventArgs e) - { - if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse) - { - UpdateScrollButtons(); - } - } + void OnPointerExited(object sender, PointerRoutedEventArgs e) + { + if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse) + { + UpdateScrollButtons(); + } + } - void ModifyFontAndMargin(TextBlock textBox, double fontChange) - { - double cur = textBox.FontSize; - double newFontSize = 0.0; - double scaleFactor = SCALEFACTOR; - if (m_textContainer.ActualHeight <= HEIGHTCUTOFF) - { - scaleFactor = SMALLHEIGHTSCALEFACTOR; - } + void ModifyFontAndMargin(TextBlock textBox, double fontChange) + { + double cur = textBox.FontSize; + double newFontSize = 0.0; + double scaleFactor = SCALEFACTOR; + if (m_textContainer.ActualHeight <= HEIGHTCUTOFF) + { + scaleFactor = SMALLHEIGHTSCALEFACTOR; + } - newFontSize = Math.Min(Math.Max(cur + fontChange, MinFontSize), MaxFontSize); - m_textContainer.Padding = new Thickness(0, 0, 0, scaleFactor * Math.Abs(cur - newFontSize)); - textBox.FontSize = newFontSize; - } + newFontSize = Math.Min(Math.Max(cur + fontChange, MinFontSize), MaxFontSize); + m_textContainer.Padding = new Thickness(0, 0, 0, scaleFactor * Math.Abs(cur - newFontSize)); + textBox.FontSize = newFontSize; + } - void UpdateAllState() - { - UpdateVisualState(); - UpdateTextState(); - } + void UpdateAllState() + { + UpdateVisualState(); + UpdateTextState(); + } - protected override void OnTapped(TappedRoutedEventArgs e) - { - this.Focus(FocusState.Programmatic); - RaiseSelectedEvent(); - } + protected override void OnTapped(TappedRoutedEventArgs e) + { + this.Focus(FocusState.Programmatic); + RaiseSelectedEvent(); + } - protected override void OnRightTapped(RightTappedRoutedEventArgs e) - { - this.Focus(FocusState.Programmatic); - } + protected override void OnRightTapped(RightTappedRoutedEventArgs e) + { + this.Focus(FocusState.Programmatic); + } - protected override void OnGotFocus(RoutedEventArgs e) - { - if (this.FocusState == FocusState.Keyboard) - { - VisualStateManager.GoToState(this, s_FocusedState, true); - } - } + protected override void OnGotFocus(RoutedEventArgs e) + { + if (this.FocusState == FocusState.Keyboard) + { + VisualStateManager.GoToState(this, s_FocusedState, true); + } + } - protected override void OnLostFocus(RoutedEventArgs e) - { - VisualStateManager.GoToState(this, s_UnfocusedState, true); - } + protected override void OnLostFocus(RoutedEventArgs e) + { + VisualStateManager.GoToState(this, s_UnfocusedState, true); + } - protected override AutomationPeer OnCreateAutomationPeer() - { - return new CalculationResultAutomationPeer(this); - } + protected override AutomationPeer OnCreateAutomationPeer() + { + return new CalculationResultAutomationPeer(this); + } - void ProgrammaticSelect() - { - RaiseSelectedEvent(); - } + void ProgrammaticSelect() + { + RaiseSelectedEvent(); + } - void RaiseSelectedEvent() - { - ValueSelected?.Invoke(this); - } - }; - } + void RaiseSelectedEvent() + { + ValueSelected?.Invoke(this); + } + }; + } } diff --git a/src/Calculator.Shared/Controls/CalculatorButton.cs b/src/Calculator.Shared/Controls/CalculatorButton.cs index 669e7e32..fd2e878e 100644 --- a/src/Calculator.Shared/Controls/CalculatorButton.cs +++ b/src/Calculator.Shared/Controls/CalculatorButton.cs @@ -31,17 +31,17 @@ namespace CalculatorApp.Controls public static readonly DependencyProperty ButtonIdProperty = DependencyProperty.Register( - "ButtonId", - typeof(NumbersAndOperatorsEnum), - typeof(CalculatorButton), - new PropertyMetadata( - null, - (s, e) => (s as CalculatorButton)?.OnButtonIdPropertyChanged( - (NumbersAndOperatorsEnum)(e.OldValue ?? NumbersAndOperatorsEnum.None), - (NumbersAndOperatorsEnum)(e.NewValue ?? NumbersAndOperatorsEnum.None) - ) - ) - ); + "ButtonId", + typeof(NumbersAndOperatorsEnum), + typeof(CalculatorButton), + new PropertyMetadata( + null, + (s, e) => (s as CalculatorButton)?.OnButtonIdPropertyChanged( + (NumbersAndOperatorsEnum)(e.OldValue ?? NumbersAndOperatorsEnum.None), + (NumbersAndOperatorsEnum)(e.NewValue ?? NumbersAndOperatorsEnum.None) + ) + ) + ); // AuditoryFeedback public string AuditoryFeedback @@ -51,7 +51,15 @@ namespace CalculatorApp.Controls } public static readonly DependencyProperty AuditoryFeedbackProperty = - DependencyProperty.Register("AuditoryFeedback", typeof(string), typeof(CalculatorButton), new PropertyMetadata(null)); + DependencyProperty.Register( + name: "AuditoryFeedback", + propertyType: typeof(string), + ownerType: typeof(CalculatorButton), + typeMetadata: new PropertyMetadata( + defaultValue: null, + propertyChangedCallback: (s, e) => (s as CalculatorButton)?.OnAuditoryFeedbackPropertyChanged( + oldValue: e.OldValue as string, + newValue: e.NewValue as string))); // HoverBackground public Brush HoverBackground @@ -106,7 +114,7 @@ namespace CalculatorApp.Controls base.OnKeyDown(e); } - protected override void OnKeyUp(KeyRoutedEventArgs e) + protected override void OnKeyUp(KeyRoutedEventArgs e) { // Ignore the Enter key if (e.Key == VirtualKey.Enter) @@ -114,7 +122,7 @@ namespace CalculatorApp.Controls return; } - base.OnKeyUp(e); + base.OnKeyUp(e); } // PRIVATE diff --git a/src/Calculator.Shared/Controls/FlipButtons.cs b/src/Calculator.Shared/Controls/FlipButtons.cs index ea6f8373..142ed1f8 100644 --- a/src/Calculator.Shared/Controls/FlipButtons.cs +++ b/src/Calculator.Shared/Controls/FlipButtons.cs @@ -13,7 +13,6 @@ namespace CalculatorApp.Controls { public partial class FlipButtons : ToggleButton { - // NumbersAndOperatorsEnum public NumbersAndOperatorsEnum ButtonId { @@ -22,7 +21,15 @@ namespace CalculatorApp.Controls } public static readonly DependencyProperty ButtonIdProperty = - DependencyProperty.Register("ButtonId", typeof(NumbersAndOperatorsEnum), typeof(FlipButtons), new PropertyMetadata(null)); + DependencyProperty.Register( + name: "ButtonId", + propertyType: typeof(NumbersAndOperatorsEnum), + ownerType: typeof(FlipButtons), + typeMetadata: new PropertyMetadata( + defaultValue: NumbersAndOperatorsEnum.None, + propertyChangedCallback: (s, e) => (s as FlipButtons)?.OnButtonIdPropertyChanged( + (NumbersAndOperatorsEnum)(e.OldValue ?? NumbersAndOperatorsEnum.None), + (NumbersAndOperatorsEnum)(e.NewValue ?? NumbersAndOperatorsEnum.None)))); // HoverBackground public Brush HoverBackground diff --git a/src/Calculator.Shared/Controls/OverflowTextBlock.cs b/src/Calculator.Shared/Controls/OverflowTextBlock.cs index c2f48218..c070e706 100644 --- a/src/Calculator.Shared/Controls/OverflowTextBlock.cs +++ b/src/Calculator.Shared/Controls/OverflowTextBlock.cs @@ -17,252 +17,261 @@ using Windows.UI.Xaml.Media; namespace CalculatorApp.Controls { - public partial class OverflowTextBlock : Control - { - const double scrollRatio = 0.7; + public partial class OverflowTextBlock : Control + { + const double scrollRatio = 0.7; - bool m_scrollingLeft; - bool m_scrollingRight; + bool m_scrollingLeft; + bool m_scrollingRight; - ListView m_listView; - ScrollViewer m_expressionContainer; - Button m_scrollLeft; - Button m_scrollRight; + ListView m_listView; + ScrollViewer m_expressionContainer; + Button m_scrollLeft; + Button m_scrollRight; - EventRegistrationToken m_scrollLeftClickEventToken; - EventRegistrationToken m_scrollRightClickEventToken; - EventRegistrationToken m_pointerEnteredEventToken; - EventRegistrationToken m_pointerExitedEventToken; - private ItemsControl m_itemsControl; - private bool m_isAccessibilityViewControl; + EventRegistrationToken m_scrollLeftClickEventToken; + EventRegistrationToken m_scrollRightClickEventToken; + EventRegistrationToken m_pointerEnteredEventToken; + EventRegistrationToken m_pointerExitedEventToken; + private ItemsControl m_itemsControl; + private bool m_isAccessibilityViewControl; - public bool TokensUpdated - { - get { return (bool)GetValue(TokensUpdatedProperty); } - set { SetValue(TokensUpdatedProperty, value); } - } + public bool TokensUpdated + { + get { return (bool)GetValue(TokensUpdatedProperty); } + set { SetValue(TokensUpdatedProperty, value); } + } - public static readonly DependencyProperty TokensUpdatedProperty = - DependencyProperty.Register("TokensUpdated", typeof(bool), typeof(OverflowTextBlock), new PropertyMetadata(false)); + public static readonly DependencyProperty TokensUpdatedProperty = + DependencyProperty.Register( + name: "TokensUpdated", + propertyType: typeof(bool), + ownerType: typeof(OverflowTextBlock), + typeMetadata: new PropertyMetadata( + false, + (s, e) => (s as OverflowTextBlock)?.OnTokensUpdatedPropertyChanged( + oldValue: (bool)e.OldValue, + newValue: (bool)e.NewValue + ))); - public bool IsActive - { - get { return (bool)GetValue(IsActiveProperty); } - set { SetValue(IsActiveProperty, value); } - } + public bool IsActive + { + get { return (bool)GetValue(IsActiveProperty); } + set { SetValue(IsActiveProperty, value); } + } - public static readonly DependencyProperty IsActiveProperty = - DependencyProperty.Register("IsActive", typeof(bool), typeof(OverflowTextBlock), new PropertyMetadata(false)); + public static readonly DependencyProperty IsActiveProperty = + DependencyProperty.Register("IsActive", typeof(bool), typeof(OverflowTextBlock), new PropertyMetadata(false)); - public Style TextStyle - { - get { return (Style)GetValue(TextStyleProperty); } - set { SetValue(TextStyleProperty, value); } - } + public Style TextStyle + { + get { return (Style)GetValue(TextStyleProperty); } + set { SetValue(TextStyleProperty, value); } + } - public static readonly DependencyProperty TextStyleProperty = - DependencyProperty.Register("TextStyle", typeof(Style), typeof(OverflowTextBlock), new PropertyMetadata(null)); + public static readonly DependencyProperty TextStyleProperty = + DependencyProperty.Register("TextStyle", typeof(Style), typeof(OverflowTextBlock), new PropertyMetadata(null)); - protected override void OnApplyTemplate() - { - UnregisterEventHandlers(); + protected override void OnApplyTemplate() + { + UnregisterEventHandlers(); - var uiElement = GetTemplateChild("ExpressionContainer"); - if (uiElement != null) - { - m_expressionContainer = uiElement as ScrollViewer; - m_expressionContainer.ChangeView(m_expressionContainer.ExtentWidth - m_expressionContainer.ViewportWidth, null, null); - m_expressionContainer.ViewChanged += OnViewChanged; - } + var uiElement = GetTemplateChild("ExpressionContainer"); + if (uiElement != null) + { + m_expressionContainer = uiElement as ScrollViewer; + m_expressionContainer.ChangeView(m_expressionContainer.ExtentWidth - m_expressionContainer.ViewportWidth, null, null); + m_expressionContainer.ViewChanged += OnViewChanged; + } - uiElement = GetTemplateChild("ScrollLeft"); - if (uiElement != null) - { - m_scrollLeft = (uiElement as Button); - m_scrollLeft.Click += OnScrollClick; - } + uiElement = GetTemplateChild("ScrollLeft"); + if (uiElement != null) + { + m_scrollLeft = (uiElement as Button); + m_scrollLeft.Click += OnScrollClick; + } - uiElement = GetTemplateChild("ScrollRight"); - if (uiElement != null) - { - m_scrollRight = (uiElement as Button); - m_scrollRight.Click += OnScrollClick; - } + uiElement = GetTemplateChild("ScrollRight"); + if (uiElement != null) + { + m_scrollRight = (uiElement as Button); + m_scrollRight.Click += OnScrollClick; + } - m_scrollingLeft = false; - m_scrollingRight = false; + m_scrollingLeft = false; + m_scrollingRight = false; - uiElement = GetTemplateChild("TokenList"); - if (uiElement != null) - { - m_itemsControl = (uiElement as ItemsControl); - } + uiElement = GetTemplateChild("TokenList"); + if (uiElement != null) + { + m_itemsControl = (uiElement as ItemsControl); + } - UpdateAllState(); - } + UpdateAllState(); + } - AutomationPeer OnCreateAutomationPeer() - { - return new OverflowTextBlockAutomationPeer(this); - } + AutomationPeer OnCreateAutomationPeer() + { + return new OverflowTextBlockAutomationPeer(this); + } - void OnTokensUpdatedPropertyChanged(bool oldValue, bool newValue) - { - if (m_expressionContainer != null && newValue) - { - m_expressionContainer.UpdateLayout(); - m_expressionContainer.ChangeView(m_expressionContainer.ScrollableWidth, null, null, true); - } - var newIsAccessibilityViewControl = m_itemsControl != null && m_itemsControl.Items.Count > 0; - if (m_isAccessibilityViewControl != newIsAccessibilityViewControl) - { - m_isAccessibilityViewControl = newIsAccessibilityViewControl; - AutomationProperties.SetAccessibilityView(this, newIsAccessibilityViewControl ? AccessibilityView.Control : AccessibilityView.Raw); - } - UpdateScrollButtons(); - } + void OnTokensUpdatedPropertyChanged(bool oldValue, bool newValue) + { + if (m_expressionContainer != null && newValue) + { + m_expressionContainer.UpdateLayout(); + m_expressionContainer.ChangeView(m_expressionContainer.ScrollableWidth, null, null, true); + } + var newIsAccessibilityViewControl = m_itemsControl != null && m_itemsControl.Items.Count > 0; + if (m_isAccessibilityViewControl != newIsAccessibilityViewControl) + { + m_isAccessibilityViewControl = newIsAccessibilityViewControl; + AutomationProperties.SetAccessibilityView(this, newIsAccessibilityViewControl ? AccessibilityView.Control : AccessibilityView.Raw); + } + UpdateScrollButtons(); + } - void UpdateAllState() - { - UpdateVisualState(); - } + void UpdateAllState() + { + UpdateVisualState(); + } - void UpdateVisualState() - { - if (IsActive) - { - VisualStateManager.GoToState(this, "Active", true); - } - else - { - VisualStateManager.GoToState(this, "Normal", true); - } - } + void UpdateVisualState() + { + if (IsActive) + { + VisualStateManager.GoToState(this, "Active", true); + } + else + { + VisualStateManager.GoToState(this, "Normal", true); + } + } - void ScrollLeft() - { - if (m_expressionContainer != null && m_expressionContainer.HorizontalOffset > 0) - { - m_scrollingLeft = true; - double offset = m_expressionContainer.HorizontalOffset - (scrollRatio * m_expressionContainer.ViewportWidth); - m_expressionContainer.ChangeView(offset, null, null); - m_expressionContainer.UpdateLayout(); - UpdateScrollButtons(); - } - } + void ScrollLeft() + { + if (m_expressionContainer != null && m_expressionContainer.HorizontalOffset > 0) + { + m_scrollingLeft = true; + double offset = m_expressionContainer.HorizontalOffset - (scrollRatio * m_expressionContainer.ViewportWidth); + m_expressionContainer.ChangeView(offset, null, null); + m_expressionContainer.UpdateLayout(); + UpdateScrollButtons(); + } + } - void ScrollRight() - { - if (m_expressionContainer != null && m_expressionContainer.HorizontalOffset < m_expressionContainer.ExtentWidth - m_expressionContainer.ViewportWidth) - { - m_scrollingRight = true; - double offset = m_expressionContainer.HorizontalOffset + (scrollRatio * m_expressionContainer.ViewportWidth); - m_expressionContainer.ChangeView(offset, null, null); - m_expressionContainer.UpdateLayout(); - UpdateScrollButtons(); - } - } + void ScrollRight() + { + if (m_expressionContainer != null && m_expressionContainer.HorizontalOffset < m_expressionContainer.ExtentWidth - m_expressionContainer.ViewportWidth) + { + m_scrollingRight = true; + double offset = m_expressionContainer.HorizontalOffset + (scrollRatio * m_expressionContainer.ViewportWidth); + m_expressionContainer.ChangeView(offset, null, null); + m_expressionContainer.UpdateLayout(); + UpdateScrollButtons(); + } + } - void OnScrollClick(object sender, RoutedEventArgs args) - { - var clicked = (sender as Button); - if (clicked == m_scrollLeft) - { - ScrollLeft(); - } - else - { - ScrollRight(); - } - } + void OnScrollClick(object sender, RoutedEventArgs args) + { + var clicked = (sender as Button); + if (clicked == m_scrollLeft) + { + ScrollLeft(); + } + else + { + ScrollRight(); + } + } - void UpdateScrollButtons() - { - if (m_itemsControl == null || m_expressionContainer == null) - { - return; - } + void UpdateScrollButtons() + { + if (m_itemsControl == null || m_expressionContainer == null) + { + return; + } - // When the width is smaller than the container, don't show any - if (m_itemsControl.ActualWidth <= m_expressionContainer.ActualWidth) - { - ShowHideScrollButtons(Visibility.Collapsed, Visibility.Collapsed); - } - // We have more number on both side. Show both arrows - else if ( - (m_expressionContainer.HorizontalOffset > 0) - && (m_expressionContainer.HorizontalOffset < (m_expressionContainer.ExtentWidth - m_expressionContainer.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_expressionContainer.HorizontalOffset == 0) - { - ShowHideScrollButtons(Visibility.Collapsed, Visibility.Visible); - if (m_scrollingLeft) - { - m_scrollingLeft = false; - if (m_scrollRight != null) - { - m_scrollRight.Focus(FocusState.Programmatic); - } - } - } - else // Width is larger than the container and right most part of the number is shown. Should be able to scroll left. - { - ShowHideScrollButtons(Visibility.Visible, Visibility.Collapsed); - if (m_scrollingRight) - { - m_scrollingRight = false; - if (m_scrollLeft != null) - { - m_scrollLeft.Focus(FocusState.Programmatic); - } - } - } - } + // When the width is smaller than the container, don't show any + if (m_itemsControl.ActualWidth <= m_expressionContainer.ActualWidth) + { + ShowHideScrollButtons(Visibility.Collapsed, Visibility.Collapsed); + } + // We have more number on both side. Show both arrows + else if ( + (m_expressionContainer.HorizontalOffset > 0) + && (m_expressionContainer.HorizontalOffset < (m_expressionContainer.ExtentWidth - m_expressionContainer.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_expressionContainer.HorizontalOffset == 0) + { + ShowHideScrollButtons(Visibility.Collapsed, Visibility.Visible); + if (m_scrollingLeft) + { + m_scrollingLeft = false; + if (m_scrollRight != null) + { + m_scrollRight.Focus(FocusState.Programmatic); + } + } + } + else // Width is larger than the container and right most part of the number is shown. Should be able to scroll left. + { + ShowHideScrollButtons(Visibility.Visible, Visibility.Collapsed); + if (m_scrollingRight) + { + m_scrollingRight = false; + if (m_scrollLeft != null) + { + m_scrollLeft.Focus(FocusState.Programmatic); + } + } + } + } - void ShowHideScrollButtons(Visibility vLeft, Visibility vRight) - { - if (m_scrollLeft != null && m_scrollRight != null) - { - m_scrollLeft.Visibility = vLeft; - m_scrollRight.Visibility = vRight; - } - } + void ShowHideScrollButtons(Visibility vLeft, Visibility vRight) + { + if (m_scrollLeft != null && m_scrollRight != null) + { + m_scrollLeft.Visibility = vLeft; + m_scrollRight.Visibility = vRight; + } + } - public void UnregisterEventHandlers() - { - // UNO TODO Unregister + public void UnregisterEventHandlers() + { + // UNO TODO Unregister - // Unregister the event handlers - //if (m_scrollLeft != null) - //{ - // m_scrollLeft.Click -= m_scrollLeftClickEventToken; - //} + // Unregister the event handlers + //if (m_scrollLeft != null) + //{ + // m_scrollLeft.Click -= m_scrollLeftClickEventToken; + //} - //if (m_scrollRight != null) - //{ - // m_scrollRight.Click -= m_scrollRightClickEventToken; - //} + //if (m_scrollRight != null) + //{ + // m_scrollRight.Click -= m_scrollRightClickEventToken; + //} - //if (m_expressionContainer != null) - //{ - // m_expressionContainer.ViewChanged -= m_containerViewChangedToken; - //} - } + //if (m_expressionContainer != null) + //{ + // m_expressionContainer.ViewChanged -= m_containerViewChangedToken; + //} + } - void OnViewChanged(object sender, ScrollViewerViewChangedEventArgs args) - { - UpdateScrollButtons(); - } + void OnViewChanged(object sender, ScrollViewerViewChangedEventArgs args) + { + UpdateScrollButtons(); + } - } + } } diff --git a/src/Calculator.Shared/Views/CalculatorScientificOperators.xaml.cs b/src/Calculator.Shared/Views/CalculatorScientificOperators.xaml.cs index 77c0a894..f83ccfac 100644 --- a/src/Calculator.Shared/Views/CalculatorScientificOperators.xaml.cs +++ b/src/Calculator.Shared/Views/CalculatorScientificOperators.xaml.cs @@ -31,7 +31,16 @@ namespace CalculatorApp } public static readonly DependencyProperty IsErrorVisualStateProperty = - DependencyProperty.Register("IsErrorVisualState", typeof(bool), typeof(CalculatorScientificOperators), new PropertyMetadata(false)); + DependencyProperty.Register( + name: "IsErrorVisualState", + propertyType: typeof(bool), + ownerType: typeof(CalculatorScientificOperators), + typeMetadata: new PropertyMetadata( + defaultValue: false, + propertyChangedCallback: (s, e) => (s as CalculatorScientificOperators)?.OnIsErrorVisualStatePropertyChanged( + (bool)e.OldValue, + (bool)e.NewValue + ))); public bool IsWideLayout { diff --git a/src/Calculator.Shared/Views/OperatorsPanel.xaml.cs b/src/Calculator.Shared/Views/OperatorsPanel.xaml.cs index 2f16be96..dd995187 100644 --- a/src/Calculator.Shared/Views/OperatorsPanel.xaml.cs +++ b/src/Calculator.Shared/Views/OperatorsPanel.xaml.cs @@ -18,85 +18,99 @@ using Windows.UI.Xaml.Navigation; namespace CalculatorApp { - public sealed partial class OperatorsPanel : UserControl - { + public sealed partial class OperatorsPanel : UserControl + { - public bool IsBitFlipChecked - { - get { return (bool)GetValue(IsBitFlipCheckedProperty); } - set { SetValue(IsBitFlipCheckedProperty, value); } - } + public bool IsBitFlipChecked + { + get { return (bool)GetValue(IsBitFlipCheckedProperty); } + set { SetValue(IsBitFlipCheckedProperty, value); } + } - public static readonly DependencyProperty IsBitFlipCheckedProperty = - DependencyProperty.Register("IsBitFlipChecked", typeof(bool), typeof(OperatorsPanel), new PropertyMetadata(false)); + public static readonly DependencyProperty IsBitFlipCheckedProperty = + DependencyProperty.Register( + name: "IsBitFlipChecked", + propertyType: typeof(bool), + ownerType: typeof(OperatorsPanel), + typeMetadata: new PropertyMetadata( + defaultValue: false, + propertyChangedCallback: (s, e) => (s as OperatorsPanel)?.OnIsBitFlipCheckedPropertyChanged( + oldValue: (bool)e.OldValue, + newValue: (bool)e.NewValue))); + public bool IsErrorVisualState + { + get => (bool)GetValue(IsErrorVisualStateProperty); + set => SetValue(IsErrorVisualStateProperty, value); + } + // Using a DependencyProperty as the backing store for IsErrorVisualState. This enables animation, styling, binding, etc... + public static readonly DependencyProperty IsErrorVisualStateProperty = + DependencyProperty.Register( + name: "IsErrorVisualState", + propertyType: typeof(bool), + ownerType: typeof(OperatorsPanel), + typeMetadata: new PropertyMetadata( + defaultValue: false, + propertyChangedCallback: (s, e) => (s as OperatorsPanel)?.OnIsErrorVisualStatePropertyChanged( + oldValue: (bool)e.OldValue, + newValue: (bool)e.NewValue))); - public bool IsErrorVisualState - { - get { return (bool)GetValue(IsErrorVisualStateProperty); } - set { SetValue(IsErrorVisualStateProperty, value); } - } + public OperatorsPanel() + { + this.InitializeComponent(); + } - // Using a DependencyProperty as the backing store for IsErrorVisualState. This enables animation, styling, binding, etc... - public static readonly DependencyProperty IsErrorVisualStateProperty = - DependencyProperty.Register("IsErrorVisualState", typeof(bool), typeof(OperatorsPanel), new PropertyMetadata(false)); + private StandardCalculatorViewModel Model => (CalculatorApp.ViewModel.StandardCalculatorViewModel)(this.DataContext); - public OperatorsPanel() - { - this.InitializeComponent(); - } + private void OnIsBitFlipCheckedPropertyChanged(bool oldValue, bool newValue) + { + if (newValue) + { + EnsureProgrammerBitFlipPanel(); + } + } - StandardCalculatorViewModel Model => (CalculatorApp.ViewModel.StandardCalculatorViewModel)(this.DataContext); + private void OnIsErrorVisualStatePropertyChanged(bool oldValue, bool newValue) + { + if (Model.IsStandard) + { + StandardOperators.IsErrorVisualState = newValue; + } + else if (Model.IsScientific) + { + ScientificOperators.IsErrorVisualState = newValue; + } + else if (Model.IsProgrammer) + { + ProgrammerRadixOperators.IsErrorVisualState = newValue; + } + } - void OnIsBitFlipCheckedPropertyChanged(bool oldValue, bool newValue) - { - if (newValue) - { - EnsureProgrammerBitFlipPanel(); - } - } + internal void EnsureScientificOps() + { + if (ScientificOperators == null) + { + this.FindName("ScientificOperators"); + } + } - void OnIsErrorVisualStatePropertyChanged(bool oldValue, bool newValue) - { - if (Model.IsStandard) - { - StandardOperators.IsErrorVisualState = newValue; - } - else if (Model.IsScientific) - { - ScientificOperators.IsErrorVisualState = newValue; - } - else if (Model.IsProgrammer) - { - ProgrammerRadixOperators.IsErrorVisualState = newValue; - } - } + internal void EnsureProgrammerRadixOps() + { + if (ProgrammerRadixOperators == null) + { + this.FindName("ProgrammerRadixOperators"); + } + } - internal void EnsureScientificOps() - { - if (ScientificOperators == null) - { - this.FindName("ScientificOperators"); - } - } + internal void EnsureProgrammerBitFlipPanel() + { + if (BitFlipPanel == null) + { + this.FindName("BitFlipPanel"); + } + } - internal void EnsureProgrammerRadixOps() - { - if (ProgrammerRadixOperators == null) - { - this.FindName("ProgrammerRadixOperators"); - } - } - - internal void EnsureProgrammerBitFlipPanel() - { - if (BitFlipPanel == null) - { - this.FindName("BitFlipPanel"); - } - } - - } + } }