mirror of
https://github.com/Microsoft/calculator.git
synced 2025-07-16 02:02:51 -07:00
Use is operator instead of as + null check (#1911)
This commit is contained in:
parent
b895f5bcea
commit
788dfc7726
8 changed files with 27 additions and 56 deletions
|
@ -484,13 +484,8 @@ namespace CalculatorApp
|
||||||
// Writer lock for the static maps
|
// Writer lock for the static maps
|
||||||
lock (s_keyboardShortcutMapLockMutex)
|
lock (s_keyboardShortcutMapLockMutex)
|
||||||
{
|
{
|
||||||
Control control = (target as ButtonBase);
|
// Handling Ctrl+E shortcut for Date Calc, target would be NavigationView^ in that case
|
||||||
|
Control control = (target as ButtonBase) ?? (Control)(target as MUXC.NavigationView);
|
||||||
if (control == null)
|
|
||||||
{
|
|
||||||
// Handling Ctrl+E shortcut for Date Calc, target would be NavigationView^ in that case
|
|
||||||
control = (target as MUXC.NavigationView);
|
|
||||||
}
|
|
||||||
|
|
||||||
int viewId = Utilities.GetWindowId();
|
int viewId = Utilities.GetWindowId();
|
||||||
|
|
||||||
|
@ -580,7 +575,7 @@ namespace CalculatorApp
|
||||||
private static bool CanNavigateModeByShortcut(MUXC.NavigationView navView, object nvi
|
private static bool CanNavigateModeByShortcut(MUXC.NavigationView navView, object nvi
|
||||||
, ApplicationViewModel vm, ViewMode toMode)
|
, ApplicationViewModel vm, ViewMode toMode)
|
||||||
{
|
{
|
||||||
if (nvi != null && nvi is NavCategory navCategory)
|
if (nvi is NavCategory navCategory)
|
||||||
{
|
{
|
||||||
return navCategory.IsEnabled
|
return navCategory.IsEnabled
|
||||||
&& navView.Visibility == Visibility.Visible
|
&& navView.Visibility == Visibility.Visible
|
||||||
|
@ -601,21 +596,18 @@ namespace CalculatorApp
|
||||||
{
|
{
|
||||||
if (itemRef.Target is MUXC.NavigationView item)
|
if (itemRef.Target is MUXC.NavigationView item)
|
||||||
{
|
{
|
||||||
var navView = item;
|
var menuItems = ((List<object>)item.MenuItemsSource);
|
||||||
|
|
||||||
var menuItems = ((List<object>)navView.MenuItemsSource);
|
|
||||||
if (menuItems != null)
|
if (menuItems != null)
|
||||||
{
|
{
|
||||||
var vm = (navView.DataContext as ApplicationViewModel);
|
if (item.DataContext is ApplicationViewModel vm)
|
||||||
if (null != vm)
|
|
||||||
{
|
{
|
||||||
ViewMode realToMode = toMode.HasValue ? toMode.Value : NavCategoryStates.GetViewModeForVirtualKey(((MyVirtualKey)key));
|
ViewMode realToMode = toMode ?? NavCategoryStates.GetViewModeForVirtualKey(((MyVirtualKey)key));
|
||||||
|
|
||||||
var nvi = menuItems[NavCategoryStates.GetFlatIndex(realToMode)];
|
var nvi = menuItems[NavCategoryStates.GetFlatIndex(realToMode)];
|
||||||
if (CanNavigateModeByShortcut(navView, nvi, vm, realToMode))
|
if (CanNavigateModeByShortcut(item, nvi, vm, realToMode))
|
||||||
{
|
{
|
||||||
vm.Mode = realToMode;
|
vm.Mode = realToMode;
|
||||||
navView.SelectedItem = nvi;
|
item.SelectedItem = nvi;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -685,14 +677,13 @@ namespace CalculatorApp
|
||||||
{
|
{
|
||||||
if (currentHonorShortcuts)
|
if (currentHonorShortcuts)
|
||||||
{
|
{
|
||||||
var myVirtualKey = key;
|
|
||||||
var lookupMap = GetCurrentKeyDictionary(isControlKeyPressed, isShiftKeyPressed, isAltKeyPressed);
|
var lookupMap = GetCurrentKeyDictionary(isControlKeyPressed, isShiftKeyPressed, isAltKeyPressed);
|
||||||
if (lookupMap == null)
|
if (lookupMap == null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var buttons = EqualRange(lookupMap, (MyVirtualKey)myVirtualKey);
|
var buttons = EqualRange(lookupMap, (MyVirtualKey)key);
|
||||||
if (!buttons.Any())
|
if (!buttons.Any())
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
|
@ -701,7 +692,7 @@ namespace CalculatorApp
|
||||||
KeyboardShortcutManagerLocals.RunFirstEnabledButtonCommand(buttons);
|
KeyboardShortcutManagerLocals.RunFirstEnabledButtonCommand(buttons);
|
||||||
|
|
||||||
// Ctrl+C and Ctrl+V shifts focus to some button because of which enter doesn't work after copy/paste. So don't shift focus if Ctrl+C or Ctrl+V
|
// Ctrl+C and Ctrl+V shifts focus to some button because of which enter doesn't work after copy/paste. So don't shift focus if Ctrl+C or Ctrl+V
|
||||||
// is pressed. When drop down is open, pressing escape shifts focus to clear button. So dont's shift focus if drop down is open. Ctrl+Insert is
|
// is pressed. When drop down is open, pressing escape shifts focus to clear button. So don't shift focus if drop down is open. Ctrl+Insert is
|
||||||
// equivalent to Ctrl+C and Shift+Insert is equivalent to Ctrl+V
|
// equivalent to Ctrl+C and Shift+Insert is equivalent to Ctrl+V
|
||||||
//var currentIsDropDownOpen = s_IsDropDownOpen.find(viewId);
|
//var currentIsDropDownOpen = s_IsDropDownOpen.find(viewId);
|
||||||
if (!s_IsDropDownOpen.TryGetValue(viewId, out var currentIsDropDownOpen) || !currentIsDropDownOpen)
|
if (!s_IsDropDownOpen.TryGetValue(viewId, out var currentIsDropDownOpen) || !currentIsDropDownOpen)
|
||||||
|
@ -778,13 +769,13 @@ namespace CalculatorApp
|
||||||
{
|
{
|
||||||
if (altPressed)
|
if (altPressed)
|
||||||
{
|
{
|
||||||
if (!shiftKeyPressed)
|
if (shiftKeyPressed)
|
||||||
{
|
{
|
||||||
return s_VirtualKeyAltChordsForButtons[viewId];
|
return null;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return null;
|
return s_VirtualKeyAltChordsForButtons[viewId];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
|
@ -44,15 +44,12 @@ namespace CalculatorApp
|
||||||
{
|
{
|
||||||
// The value to be valid has to be a boxed int32 value
|
// The value to be valid has to be a boxed int32 value
|
||||||
// extract that value and ensure it is valid, ie >= 0
|
// extract that value and ensure it is valid, ie >= 0
|
||||||
if (value != null)
|
if (value is Windows.Foundation.IPropertyValue box && box.Type == Windows.Foundation.PropertyType.Int32)
|
||||||
{
|
{
|
||||||
if (value is Windows.Foundation.IPropertyValue box && box.Type == Windows.Foundation.PropertyType.Int32)
|
int index = box.GetInt32();
|
||||||
|
if (index >= 0)
|
||||||
{
|
{
|
||||||
int index = box.GetInt32();
|
return value;
|
||||||
if (index >= 0)
|
|
||||||
{
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// The value is not valid therefore stop the binding right here
|
// The value is not valid therefore stop the binding right here
|
||||||
|
|
|
@ -16,8 +16,7 @@ namespace CalculatorApp
|
||||||
{
|
{
|
||||||
protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
|
protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
|
||||||
{
|
{
|
||||||
DisplayExpressionToken token = (item as DisplayExpressionToken);
|
if (item is DisplayExpressionToken token)
|
||||||
if (token != null)
|
|
||||||
{
|
{
|
||||||
CalculatorApp.ViewModel.Common.TokenType type = token.Type;
|
CalculatorApp.ViewModel.Common.TokenType type = token.Type;
|
||||||
|
|
||||||
|
|
|
@ -814,8 +814,7 @@ namespace CalculatorApp
|
||||||
|
|
||||||
private void DockPanelTapped(object sender, TappedRoutedEventArgs e)
|
private void DockPanelTapped(object sender, TappedRoutedEventArgs e)
|
||||||
{
|
{
|
||||||
int index = DockPivot.SelectedIndex;
|
if (DockPivot.SelectedIndex == 1 && !IsProgrammer)
|
||||||
if (index == 1 && !IsProgrammer)
|
|
||||||
{
|
{
|
||||||
SetChildAsMemory();
|
SetChildAsMemory();
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ namespace CalculatorApp
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
Debug.Assert(DataContext as ViewModel.StandardCalculatorViewModel != null, "static_cast result must NOT be null");
|
Debug.Assert(DataContext is ViewModel.StandardCalculatorViewModel, "static_cast result must NOT be null");
|
||||||
return DataContext as ViewModel.StandardCalculatorViewModel;
|
return DataContext as ViewModel.StandardCalculatorViewModel;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -518,7 +518,7 @@ namespace CalculatorApp
|
||||||
|
|
||||||
private void GraphingControl_LosingFocus(UIElement sender, LosingFocusEventArgs args)
|
private void GraphingControl_LosingFocus(UIElement sender, LosingFocusEventArgs args)
|
||||||
{
|
{
|
||||||
if (!(args.NewFocusedElement is FrameworkElement newFocusElement) || newFocusElement.Name == null)
|
if (!(args.NewFocusedElement is FrameworkElement))
|
||||||
{
|
{
|
||||||
// Because clicking on the swap chain panel will try to move focus to a control that can't actually take focus
|
// Because clicking on the swap chain panel will try to move focus to a control that can't actually take focus
|
||||||
// we will get a null destination. So we are going to try and cancel that request.
|
// we will get a null destination. So we are going to try and cancel that request.
|
||||||
|
@ -534,15 +534,7 @@ namespace CalculatorApp
|
||||||
|
|
||||||
private void GraphingControl_GraphViewChangedEvent(object sender, GraphViewChangedReason reason)
|
private void GraphingControl_GraphViewChangedEvent(object sender, GraphViewChangedReason reason)
|
||||||
{
|
{
|
||||||
if (reason == GraphViewChangedReason.Manipulation)
|
IsManualAdjustment = (reason == GraphViewChangedReason.Manipulation);
|
||||||
{
|
|
||||||
IsManualAdjustment = true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
IsManualAdjustment = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
UpdateGraphAutomationName();
|
UpdateGraphAutomationName();
|
||||||
|
|
||||||
var announcement = CalculatorAnnouncement.GetGraphViewChangedAnnouncement(GraphControlAutomationName);
|
var announcement = CalculatorAnnouncement.GetGraphViewChangedAnnouncement(GraphControlAutomationName);
|
||||||
|
|
|
@ -48,11 +48,8 @@ namespace CalculatorApp
|
||||||
|
|
||||||
private void ListView_ItemClick(object sender, ItemClickEventArgs e)
|
private void ListView_ItemClick(object sender, ItemClickEventArgs e)
|
||||||
{
|
{
|
||||||
HistoryViewModel historyVM = (DataContext as HistoryViewModel);
|
|
||||||
HistoryItemViewModel clickedItem = (e.ClickedItem as HistoryItemViewModel);
|
|
||||||
|
|
||||||
// When the user clears the history list in the overlay view and presses enter, the clickedItem is nullptr
|
// When the user clears the history list in the overlay view and presses enter, the clickedItem is nullptr
|
||||||
if (clickedItem != null && historyVM != null)
|
if (e.ClickedItem is HistoryItemViewModel clickedItem && DataContext is HistoryViewModel historyVM)
|
||||||
{
|
{
|
||||||
historyVM.ShowItem(clickedItem);
|
historyVM.ShowItem(clickedItem);
|
||||||
}
|
}
|
||||||
|
@ -60,8 +57,7 @@ namespace CalculatorApp
|
||||||
private void OnCopyMenuItemClicked(object sender, RoutedEventArgs e)
|
private void OnCopyMenuItemClicked(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
var listViewItem = HistoryContextMenu.Target;
|
var listViewItem = HistoryContextMenu.Target;
|
||||||
var itemViewModel = (HistoryListView.ItemFromContainer(listViewItem) as HistoryItemViewModel);
|
if (HistoryListView.ItemFromContainer(listViewItem) is HistoryItemViewModel itemViewModel)
|
||||||
if (itemViewModel != null)
|
|
||||||
{
|
{
|
||||||
CopyPasteManager.CopyToClipboard(itemViewModel.Result);
|
CopyPasteManager.CopyToClipboard(itemViewModel.Result);
|
||||||
}
|
}
|
||||||
|
@ -69,16 +65,14 @@ namespace CalculatorApp
|
||||||
private void OnDeleteMenuItemClicked(object sender, RoutedEventArgs e)
|
private void OnDeleteMenuItemClicked(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
var listViewItem = HistoryContextMenu.Target;
|
var listViewItem = HistoryContextMenu.Target;
|
||||||
var itemViewModel = (HistoryListView.ItemFromContainer(listViewItem) as HistoryItemViewModel);
|
if (HistoryListView.ItemFromContainer(listViewItem) is HistoryItemViewModel itemViewModel)
|
||||||
if (itemViewModel != null)
|
|
||||||
{
|
{
|
||||||
Model.DeleteItem(itemViewModel);
|
Model.DeleteItem(itemViewModel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private void OnDeleteSwipeInvoked(MUXC.SwipeItem sender, MUXC.SwipeItemInvokedEventArgs e)
|
private void OnDeleteSwipeInvoked(MUXC.SwipeItem sender, MUXC.SwipeItemInvokedEventArgs e)
|
||||||
{
|
{
|
||||||
var swipedItem = (e.SwipeControl.DataContext as HistoryItemViewModel);
|
if (e.SwipeControl.DataContext is HistoryItemViewModel swipedItem)
|
||||||
if (swipedItem != null)
|
|
||||||
{
|
{
|
||||||
Model.DeleteItem(swipedItem);
|
Model.DeleteItem(swipedItem);
|
||||||
}
|
}
|
||||||
|
|
|
@ -368,8 +368,7 @@ namespace CalculatorApp
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var item = (e.SelectedItemContainer as MUXC.NavigationViewItem);
|
if (e.SelectedItemContainer is MUXC.NavigationViewItem item)
|
||||||
if (item != null)
|
|
||||||
{
|
{
|
||||||
Model.Mode = (ViewMode)item.Tag;
|
Model.Mode = (ViewMode)item.Tag;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue