Add missing property changed handers

This commit is contained in:
Jérôme Laban 2019-05-24 09:23:10 -04:00
commit ea8b112f94
6 changed files with 829 additions and 753 deletions

File diff suppressed because it is too large Load diff

View file

@ -31,17 +31,17 @@ namespace CalculatorApp.Controls
public static readonly DependencyProperty ButtonIdProperty = public static readonly DependencyProperty ButtonIdProperty =
DependencyProperty.Register( DependencyProperty.Register(
"ButtonId", "ButtonId",
typeof(NumbersAndOperatorsEnum), typeof(NumbersAndOperatorsEnum),
typeof(CalculatorButton), typeof(CalculatorButton),
new PropertyMetadata( new PropertyMetadata(
null, null,
(s, e) => (s as CalculatorButton)?.OnButtonIdPropertyChanged( (s, e) => (s as CalculatorButton)?.OnButtonIdPropertyChanged(
(NumbersAndOperatorsEnum)(e.OldValue ?? NumbersAndOperatorsEnum.None), (NumbersAndOperatorsEnum)(e.OldValue ?? NumbersAndOperatorsEnum.None),
(NumbersAndOperatorsEnum)(e.NewValue ?? NumbersAndOperatorsEnum.None) (NumbersAndOperatorsEnum)(e.NewValue ?? NumbersAndOperatorsEnum.None)
) )
) )
); );
// AuditoryFeedback // AuditoryFeedback
public string AuditoryFeedback public string AuditoryFeedback
@ -51,7 +51,15 @@ namespace CalculatorApp.Controls
} }
public static readonly DependencyProperty AuditoryFeedbackProperty = 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 // HoverBackground
public Brush HoverBackground public Brush HoverBackground
@ -106,7 +114,7 @@ namespace CalculatorApp.Controls
base.OnKeyDown(e); base.OnKeyDown(e);
} }
protected override void OnKeyUp(KeyRoutedEventArgs e) protected override void OnKeyUp(KeyRoutedEventArgs e)
{ {
// Ignore the Enter key // Ignore the Enter key
if (e.Key == VirtualKey.Enter) if (e.Key == VirtualKey.Enter)
@ -114,7 +122,7 @@ namespace CalculatorApp.Controls
return; return;
} }
base.OnKeyUp(e); base.OnKeyUp(e);
} }
// PRIVATE // PRIVATE

View file

@ -13,7 +13,6 @@ namespace CalculatorApp.Controls
{ {
public partial class FlipButtons : ToggleButton public partial class FlipButtons : ToggleButton
{ {
// NumbersAndOperatorsEnum // NumbersAndOperatorsEnum
public NumbersAndOperatorsEnum ButtonId public NumbersAndOperatorsEnum ButtonId
{ {
@ -22,7 +21,15 @@ namespace CalculatorApp.Controls
} }
public static readonly DependencyProperty ButtonIdProperty = 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 // HoverBackground
public Brush HoverBackground public Brush HoverBackground

View file

@ -17,252 +17,261 @@ using Windows.UI.Xaml.Media;
namespace CalculatorApp.Controls namespace CalculatorApp.Controls
{ {
public partial class OverflowTextBlock : Control public partial class OverflowTextBlock : Control
{ {
const double scrollRatio = 0.7; const double scrollRatio = 0.7;
bool m_scrollingLeft; bool m_scrollingLeft;
bool m_scrollingRight; bool m_scrollingRight;
ListView m_listView; ListView m_listView;
ScrollViewer m_expressionContainer; ScrollViewer m_expressionContainer;
Button m_scrollLeft; Button m_scrollLeft;
Button m_scrollRight; Button m_scrollRight;
EventRegistrationToken m_scrollLeftClickEventToken; EventRegistrationToken m_scrollLeftClickEventToken;
EventRegistrationToken m_scrollRightClickEventToken; EventRegistrationToken m_scrollRightClickEventToken;
EventRegistrationToken m_pointerEnteredEventToken; EventRegistrationToken m_pointerEnteredEventToken;
EventRegistrationToken m_pointerExitedEventToken; EventRegistrationToken m_pointerExitedEventToken;
private ItemsControl m_itemsControl; private ItemsControl m_itemsControl;
private bool m_isAccessibilityViewControl; private bool m_isAccessibilityViewControl;
public bool TokensUpdated public bool TokensUpdated
{ {
get { return (bool)GetValue(TokensUpdatedProperty); } get { return (bool)GetValue(TokensUpdatedProperty); }
set { SetValue(TokensUpdatedProperty, value); } set { SetValue(TokensUpdatedProperty, value); }
} }
public static readonly DependencyProperty TokensUpdatedProperty = public static readonly DependencyProperty TokensUpdatedProperty =
DependencyProperty.Register("TokensUpdated", typeof(bool), typeof(OverflowTextBlock), new PropertyMetadata(false)); 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 public bool IsActive
{ {
get { return (bool)GetValue(IsActiveProperty); } get { return (bool)GetValue(IsActiveProperty); }
set { SetValue(IsActiveProperty, value); } set { SetValue(IsActiveProperty, value); }
} }
public static readonly DependencyProperty IsActiveProperty = public static readonly DependencyProperty IsActiveProperty =
DependencyProperty.Register("IsActive", typeof(bool), typeof(OverflowTextBlock), new PropertyMetadata(false)); DependencyProperty.Register("IsActive", typeof(bool), typeof(OverflowTextBlock), new PropertyMetadata(false));
public Style TextStyle public Style TextStyle
{ {
get { return (Style)GetValue(TextStyleProperty); } get { return (Style)GetValue(TextStyleProperty); }
set { SetValue(TextStyleProperty, value); } set { SetValue(TextStyleProperty, value); }
} }
public static readonly DependencyProperty TextStyleProperty = public static readonly DependencyProperty TextStyleProperty =
DependencyProperty.Register("TextStyle", typeof(Style), typeof(OverflowTextBlock), new PropertyMetadata(null)); DependencyProperty.Register("TextStyle", typeof(Style), typeof(OverflowTextBlock), new PropertyMetadata(null));
protected override void OnApplyTemplate() protected override void OnApplyTemplate()
{ {
UnregisterEventHandlers(); UnregisterEventHandlers();
var uiElement = GetTemplateChild("ExpressionContainer"); var uiElement = GetTemplateChild("ExpressionContainer");
if (uiElement != null) if (uiElement != null)
{ {
m_expressionContainer = uiElement as ScrollViewer; m_expressionContainer = uiElement as ScrollViewer;
m_expressionContainer.ChangeView(m_expressionContainer.ExtentWidth - m_expressionContainer.ViewportWidth, null, null); m_expressionContainer.ChangeView(m_expressionContainer.ExtentWidth - m_expressionContainer.ViewportWidth, null, null);
m_expressionContainer.ViewChanged += OnViewChanged; m_expressionContainer.ViewChanged += OnViewChanged;
} }
uiElement = GetTemplateChild("ScrollLeft"); uiElement = GetTemplateChild("ScrollLeft");
if (uiElement != null) if (uiElement != null)
{ {
m_scrollLeft = (uiElement as Button); m_scrollLeft = (uiElement as Button);
m_scrollLeft.Click += OnScrollClick; m_scrollLeft.Click += OnScrollClick;
} }
uiElement = GetTemplateChild("ScrollRight"); uiElement = GetTemplateChild("ScrollRight");
if (uiElement != null) if (uiElement != null)
{ {
m_scrollRight = (uiElement as Button); m_scrollRight = (uiElement as Button);
m_scrollRight.Click += OnScrollClick; m_scrollRight.Click += OnScrollClick;
} }
m_scrollingLeft = false; m_scrollingLeft = false;
m_scrollingRight = false; m_scrollingRight = false;
uiElement = GetTemplateChild("TokenList"); uiElement = GetTemplateChild("TokenList");
if (uiElement != null) if (uiElement != null)
{ {
m_itemsControl = (uiElement as ItemsControl); m_itemsControl = (uiElement as ItemsControl);
} }
UpdateAllState(); UpdateAllState();
} }
AutomationPeer OnCreateAutomationPeer() AutomationPeer OnCreateAutomationPeer()
{ {
return new OverflowTextBlockAutomationPeer(this); return new OverflowTextBlockAutomationPeer(this);
} }
void OnTokensUpdatedPropertyChanged(bool oldValue, bool newValue) void OnTokensUpdatedPropertyChanged(bool oldValue, bool newValue)
{ {
if (m_expressionContainer != null && newValue) if (m_expressionContainer != null && newValue)
{ {
m_expressionContainer.UpdateLayout(); m_expressionContainer.UpdateLayout();
m_expressionContainer.ChangeView(m_expressionContainer.ScrollableWidth, null, null, true); m_expressionContainer.ChangeView(m_expressionContainer.ScrollableWidth, null, null, true);
} }
var newIsAccessibilityViewControl = m_itemsControl != null && m_itemsControl.Items.Count > 0; var newIsAccessibilityViewControl = m_itemsControl != null && m_itemsControl.Items.Count > 0;
if (m_isAccessibilityViewControl != newIsAccessibilityViewControl) if (m_isAccessibilityViewControl != newIsAccessibilityViewControl)
{ {
m_isAccessibilityViewControl = newIsAccessibilityViewControl; m_isAccessibilityViewControl = newIsAccessibilityViewControl;
AutomationProperties.SetAccessibilityView(this, newIsAccessibilityViewControl ? AccessibilityView.Control : AccessibilityView.Raw); AutomationProperties.SetAccessibilityView(this, newIsAccessibilityViewControl ? AccessibilityView.Control : AccessibilityView.Raw);
} }
UpdateScrollButtons(); UpdateScrollButtons();
} }
void UpdateAllState() void UpdateAllState()
{ {
UpdateVisualState(); UpdateVisualState();
} }
void UpdateVisualState() void UpdateVisualState()
{ {
if (IsActive) if (IsActive)
{ {
VisualStateManager.GoToState(this, "Active", true); VisualStateManager.GoToState(this, "Active", true);
} }
else else
{ {
VisualStateManager.GoToState(this, "Normal", true); VisualStateManager.GoToState(this, "Normal", true);
} }
} }
void ScrollLeft() void ScrollLeft()
{ {
if (m_expressionContainer != null && m_expressionContainer.HorizontalOffset > 0) if (m_expressionContainer != null && m_expressionContainer.HorizontalOffset > 0)
{ {
m_scrollingLeft = true; m_scrollingLeft = true;
double offset = m_expressionContainer.HorizontalOffset - (scrollRatio * m_expressionContainer.ViewportWidth); double offset = m_expressionContainer.HorizontalOffset - (scrollRatio * m_expressionContainer.ViewportWidth);
m_expressionContainer.ChangeView(offset, null, null); m_expressionContainer.ChangeView(offset, null, null);
m_expressionContainer.UpdateLayout(); m_expressionContainer.UpdateLayout();
UpdateScrollButtons(); UpdateScrollButtons();
} }
} }
void ScrollRight() void ScrollRight()
{ {
if (m_expressionContainer != null && m_expressionContainer.HorizontalOffset < m_expressionContainer.ExtentWidth - m_expressionContainer.ViewportWidth) if (m_expressionContainer != null && m_expressionContainer.HorizontalOffset < m_expressionContainer.ExtentWidth - m_expressionContainer.ViewportWidth)
{ {
m_scrollingRight = true; m_scrollingRight = true;
double offset = m_expressionContainer.HorizontalOffset + (scrollRatio * m_expressionContainer.ViewportWidth); double offset = m_expressionContainer.HorizontalOffset + (scrollRatio * m_expressionContainer.ViewportWidth);
m_expressionContainer.ChangeView(offset, null, null); m_expressionContainer.ChangeView(offset, null, null);
m_expressionContainer.UpdateLayout(); m_expressionContainer.UpdateLayout();
UpdateScrollButtons(); UpdateScrollButtons();
} }
} }
void OnScrollClick(object sender, RoutedEventArgs args) void OnScrollClick(object sender, RoutedEventArgs args)
{ {
var clicked = (sender as Button); var clicked = (sender as Button);
if (clicked == m_scrollLeft) if (clicked == m_scrollLeft)
{ {
ScrollLeft(); ScrollLeft();
} }
else else
{ {
ScrollRight(); ScrollRight();
} }
} }
void UpdateScrollButtons() void UpdateScrollButtons()
{ {
if (m_itemsControl == null || m_expressionContainer == null) if (m_itemsControl == null || m_expressionContainer == null)
{ {
return; return;
} }
// When the width is smaller than the container, don't show any // When the width is smaller than the container, don't show any
if (m_itemsControl.ActualWidth <= m_expressionContainer.ActualWidth) if (m_itemsControl.ActualWidth <= m_expressionContainer.ActualWidth)
{ {
ShowHideScrollButtons(Visibility.Collapsed, Visibility.Collapsed); ShowHideScrollButtons(Visibility.Collapsed, Visibility.Collapsed);
} }
// We have more number on both side. Show both arrows // We have more number on both side. Show both arrows
else if ( else if (
(m_expressionContainer.HorizontalOffset > 0) (m_expressionContainer.HorizontalOffset > 0)
&& (m_expressionContainer.HorizontalOffset < (m_expressionContainer.ExtentWidth - m_expressionContainer.ViewportWidth))) && (m_expressionContainer.HorizontalOffset < (m_expressionContainer.ExtentWidth - m_expressionContainer.ViewportWidth)))
{ {
ShowHideScrollButtons(Visibility.Visible, Visibility.Visible); 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. // 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) else if (m_expressionContainer.HorizontalOffset == 0)
{ {
ShowHideScrollButtons(Visibility.Collapsed, Visibility.Visible); ShowHideScrollButtons(Visibility.Collapsed, Visibility.Visible);
if (m_scrollingLeft) if (m_scrollingLeft)
{ {
m_scrollingLeft = false; m_scrollingLeft = false;
if (m_scrollRight != null) if (m_scrollRight != null)
{ {
m_scrollRight.Focus(FocusState.Programmatic); 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. 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); ShowHideScrollButtons(Visibility.Visible, Visibility.Collapsed);
if (m_scrollingRight) if (m_scrollingRight)
{ {
m_scrollingRight = false; m_scrollingRight = false;
if (m_scrollLeft != null) if (m_scrollLeft != null)
{ {
m_scrollLeft.Focus(FocusState.Programmatic); m_scrollLeft.Focus(FocusState.Programmatic);
} }
} }
} }
} }
void ShowHideScrollButtons(Visibility vLeft, Visibility vRight) void ShowHideScrollButtons(Visibility vLeft, Visibility vRight)
{ {
if (m_scrollLeft != null && m_scrollRight != null) if (m_scrollLeft != null && m_scrollRight != null)
{ {
m_scrollLeft.Visibility = vLeft; m_scrollLeft.Visibility = vLeft;
m_scrollRight.Visibility = vRight; m_scrollRight.Visibility = vRight;
} }
} }
public void UnregisterEventHandlers() public void UnregisterEventHandlers()
{ {
// UNO TODO Unregister // UNO TODO Unregister
// Unregister the event handlers // Unregister the event handlers
//if (m_scrollLeft != null) //if (m_scrollLeft != null)
//{ //{
// m_scrollLeft.Click -= m_scrollLeftClickEventToken; // m_scrollLeft.Click -= m_scrollLeftClickEventToken;
//} //}
//if (m_scrollRight != null) //if (m_scrollRight != null)
//{ //{
// m_scrollRight.Click -= m_scrollRightClickEventToken; // m_scrollRight.Click -= m_scrollRightClickEventToken;
//} //}
//if (m_expressionContainer != null) //if (m_expressionContainer != null)
//{ //{
// m_expressionContainer.ViewChanged -= m_containerViewChangedToken; // m_expressionContainer.ViewChanged -= m_containerViewChangedToken;
//} //}
} }
void OnViewChanged(object sender, ScrollViewerViewChangedEventArgs args) void OnViewChanged(object sender, ScrollViewerViewChangedEventArgs args)
{ {
UpdateScrollButtons(); UpdateScrollButtons();
} }
} }
} }

View file

@ -31,7 +31,16 @@ namespace CalculatorApp
} }
public static readonly DependencyProperty IsErrorVisualStateProperty = 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 public bool IsWideLayout
{ {

View file

@ -18,85 +18,99 @@ using Windows.UI.Xaml.Navigation;
namespace CalculatorApp namespace CalculatorApp
{ {
public sealed partial class OperatorsPanel : UserControl public sealed partial class OperatorsPanel : UserControl
{ {
public bool IsBitFlipChecked public bool IsBitFlipChecked
{ {
get { return (bool)GetValue(IsBitFlipCheckedProperty); } get { return (bool)GetValue(IsBitFlipCheckedProperty); }
set { SetValue(IsBitFlipCheckedProperty, value); } set { SetValue(IsBitFlipCheckedProperty, value); }
} }
public static readonly DependencyProperty IsBitFlipCheckedProperty = public static readonly DependencyProperty IsBitFlipCheckedProperty =
DependencyProperty.Register("IsBitFlipChecked", typeof(bool), typeof(OperatorsPanel), new PropertyMetadata(false)); 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 public OperatorsPanel()
{ {
get { return (bool)GetValue(IsErrorVisualStateProperty); } this.InitializeComponent();
set { SetValue(IsErrorVisualStateProperty, value); } }
}
// Using a DependencyProperty as the backing store for IsErrorVisualState. This enables animation, styling, binding, etc... private StandardCalculatorViewModel Model => (CalculatorApp.ViewModel.StandardCalculatorViewModel)(this.DataContext);
public static readonly DependencyProperty IsErrorVisualStateProperty =
DependencyProperty.Register("IsErrorVisualState", typeof(bool), typeof(OperatorsPanel), new PropertyMetadata(false));
public OperatorsPanel() private void OnIsBitFlipCheckedPropertyChanged(bool oldValue, bool newValue)
{ {
this.InitializeComponent(); 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) internal void EnsureScientificOps()
{ {
if (newValue) if (ScientificOperators == null)
{ {
EnsureProgrammerBitFlipPanel(); this.FindName("ScientificOperators");
} }
} }
void OnIsErrorVisualStatePropertyChanged(bool oldValue, bool newValue) internal void EnsureProgrammerRadixOps()
{ {
if (Model.IsStandard) if (ProgrammerRadixOperators == null)
{ {
StandardOperators.IsErrorVisualState = newValue; this.FindName("ProgrammerRadixOperators");
} }
else if (Model.IsScientific) }
{
ScientificOperators.IsErrorVisualState = newValue;
}
else if (Model.IsProgrammer)
{
ProgrammerRadixOperators.IsErrorVisualState = newValue;
}
}
internal void EnsureScientificOps() internal void EnsureProgrammerBitFlipPanel()
{ {
if (ScientificOperators == null) if (BitFlipPanel == null)
{ {
this.FindName("ScientificOperators"); this.FindName("BitFlipPanel");
} }
} }
internal void EnsureProgrammerRadixOps() }
{
if (ProgrammerRadixOperators == null)
{
this.FindName("ProgrammerRadixOperators");
}
}
internal void EnsureProgrammerBitFlipPanel()
{
if (BitFlipPanel == null)
{
this.FindName("BitFlipPanel");
}
}
}
} }