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
|
||||
lock (s_keyboardShortcutMapLockMutex)
|
||||
{
|
||||
Control control = (target as ButtonBase);
|
||||
|
||||
if (control == null)
|
||||
{
|
||||
// Handling Ctrl+E shortcut for Date Calc, target would be NavigationView^ in that case
|
||||
control = (target as MUXC.NavigationView);
|
||||
}
|
||||
// Handling Ctrl+E shortcut for Date Calc, target would be NavigationView^ in that case
|
||||
Control control = (target as ButtonBase) ?? (Control)(target as MUXC.NavigationView);
|
||||
|
||||
int viewId = Utilities.GetWindowId();
|
||||
|
||||
|
@ -580,7 +575,7 @@ namespace CalculatorApp
|
|||
private static bool CanNavigateModeByShortcut(MUXC.NavigationView navView, object nvi
|
||||
, ApplicationViewModel vm, ViewMode toMode)
|
||||
{
|
||||
if (nvi != null && nvi is NavCategory navCategory)
|
||||
if (nvi is NavCategory navCategory)
|
||||
{
|
||||
return navCategory.IsEnabled
|
||||
&& navView.Visibility == Visibility.Visible
|
||||
|
@ -601,21 +596,18 @@ namespace CalculatorApp
|
|||
{
|
||||
if (itemRef.Target is MUXC.NavigationView item)
|
||||
{
|
||||
var navView = item;
|
||||
|
||||
var menuItems = ((List<object>)navView.MenuItemsSource);
|
||||
var menuItems = ((List<object>)item.MenuItemsSource);
|
||||
if (menuItems != null)
|
||||
{
|
||||
var vm = (navView.DataContext as ApplicationViewModel);
|
||||
if (null != vm)
|
||||
if (item.DataContext is ApplicationViewModel vm)
|
||||
{
|
||||
ViewMode realToMode = toMode.HasValue ? toMode.Value : NavCategoryStates.GetViewModeForVirtualKey(((MyVirtualKey)key));
|
||||
ViewMode realToMode = toMode ?? NavCategoryStates.GetViewModeForVirtualKey(((MyVirtualKey)key));
|
||||
|
||||
var nvi = menuItems[NavCategoryStates.GetFlatIndex(realToMode)];
|
||||
if (CanNavigateModeByShortcut(navView, nvi, vm, realToMode))
|
||||
if (CanNavigateModeByShortcut(item, nvi, vm, realToMode))
|
||||
{
|
||||
vm.Mode = realToMode;
|
||||
navView.SelectedItem = nvi;
|
||||
item.SelectedItem = nvi;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -685,14 +677,13 @@ namespace CalculatorApp
|
|||
{
|
||||
if (currentHonorShortcuts)
|
||||
{
|
||||
var myVirtualKey = key;
|
||||
var lookupMap = GetCurrentKeyDictionary(isControlKeyPressed, isShiftKeyPressed, isAltKeyPressed);
|
||||
if (lookupMap == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var buttons = EqualRange(lookupMap, (MyVirtualKey)myVirtualKey);
|
||||
var buttons = EqualRange(lookupMap, (MyVirtualKey)key);
|
||||
if (!buttons.Any())
|
||||
{
|
||||
return;
|
||||
|
@ -701,7 +692,7 @@ namespace CalculatorApp
|
|||
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
|
||||
// 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
|
||||
//var currentIsDropDownOpen = s_IsDropDownOpen.find(viewId);
|
||||
if (!s_IsDropDownOpen.TryGetValue(viewId, out var currentIsDropDownOpen) || !currentIsDropDownOpen)
|
||||
|
@ -778,13 +769,13 @@ namespace CalculatorApp
|
|||
{
|
||||
if (altPressed)
|
||||
{
|
||||
if (!shiftKeyPressed)
|
||||
if (shiftKeyPressed)
|
||||
{
|
||||
return s_VirtualKeyAltChordsForButtons[viewId];
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
return s_VirtualKeyAltChordsForButtons[viewId];
|
||||
}
|
||||
}
|
||||
else
|
||||
|
|
|
@ -44,15 +44,12 @@ namespace CalculatorApp
|
|||
{
|
||||
// The value to be valid has to be a boxed int32 value
|
||||
// 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();
|
||||
if (index >= 0)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
// 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)
|
||||
{
|
||||
DisplayExpressionToken token = (item as DisplayExpressionToken);
|
||||
if (token != null)
|
||||
if (item is DisplayExpressionToken token)
|
||||
{
|
||||
CalculatorApp.ViewModel.Common.TokenType type = token.Type;
|
||||
|
||||
|
|
|
@ -814,8 +814,7 @@ namespace CalculatorApp
|
|||
|
||||
private void DockPanelTapped(object sender, TappedRoutedEventArgs e)
|
||||
{
|
||||
int index = DockPivot.SelectedIndex;
|
||||
if (index == 1 && !IsProgrammer)
|
||||
if (DockPivot.SelectedIndex == 1 && !IsProgrammer)
|
||||
{
|
||||
SetChildAsMemory();
|
||||
}
|
||||
|
|
|
@ -36,7 +36,7 @@ namespace CalculatorApp
|
|||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -518,7 +518,7 @@ namespace CalculatorApp
|
|||
|
||||
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
|
||||
// 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)
|
||||
{
|
||||
if (reason == GraphViewChangedReason.Manipulation)
|
||||
{
|
||||
IsManualAdjustment = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
IsManualAdjustment = false;
|
||||
}
|
||||
|
||||
IsManualAdjustment = (reason == GraphViewChangedReason.Manipulation);
|
||||
UpdateGraphAutomationName();
|
||||
|
||||
var announcement = CalculatorAnnouncement.GetGraphViewChangedAnnouncement(GraphControlAutomationName);
|
||||
|
|
|
@ -48,11 +48,8 @@ namespace CalculatorApp
|
|||
|
||||
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
|
||||
if (clickedItem != null && historyVM != null)
|
||||
if (e.ClickedItem is HistoryItemViewModel clickedItem && DataContext is HistoryViewModel historyVM)
|
||||
{
|
||||
historyVM.ShowItem(clickedItem);
|
||||
}
|
||||
|
@ -60,8 +57,7 @@ namespace CalculatorApp
|
|||
private void OnCopyMenuItemClicked(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var listViewItem = HistoryContextMenu.Target;
|
||||
var itemViewModel = (HistoryListView.ItemFromContainer(listViewItem) as HistoryItemViewModel);
|
||||
if (itemViewModel != null)
|
||||
if (HistoryListView.ItemFromContainer(listViewItem) is HistoryItemViewModel itemViewModel)
|
||||
{
|
||||
CopyPasteManager.CopyToClipboard(itemViewModel.Result);
|
||||
}
|
||||
|
@ -69,16 +65,14 @@ namespace CalculatorApp
|
|||
private void OnDeleteMenuItemClicked(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var listViewItem = HistoryContextMenu.Target;
|
||||
var itemViewModel = (HistoryListView.ItemFromContainer(listViewItem) as HistoryItemViewModel);
|
||||
if (itemViewModel != null)
|
||||
if (HistoryListView.ItemFromContainer(listViewItem) is HistoryItemViewModel itemViewModel)
|
||||
{
|
||||
Model.DeleteItem(itemViewModel);
|
||||
}
|
||||
}
|
||||
private void OnDeleteSwipeInvoked(MUXC.SwipeItem sender, MUXC.SwipeItemInvokedEventArgs e)
|
||||
{
|
||||
var swipedItem = (e.SwipeControl.DataContext as HistoryItemViewModel);
|
||||
if (swipedItem != null)
|
||||
if (e.SwipeControl.DataContext is HistoryItemViewModel swipedItem)
|
||||
{
|
||||
Model.DeleteItem(swipedItem);
|
||||
}
|
||||
|
|
|
@ -368,8 +368,7 @@ namespace CalculatorApp
|
|||
return;
|
||||
}
|
||||
|
||||
var item = (e.SelectedItemContainer as MUXC.NavigationViewItem);
|
||||
if (item != null)
|
||||
if (e.SelectedItemContainer is MUXC.NavigationViewItem item)
|
||||
{
|
||||
Model.Mode = (ViewMode)item.Tag;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue