Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graphing Calculator - Add Shortcuts to zoom in/out #882

Merged
merged 1 commit into from
Dec 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 35 additions & 62 deletions src/CalcViewModel/Common/KeyboardShortcutManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,15 @@ static multimap<int, multimap<MyVirtualKey, WeakReference>> s_VirtualKeyControlS
static multimap<int, multimap<MyVirtualKey, WeakReference>> s_VirtualKeyInverseChordsForButtons;
static multimap<int, multimap<MyVirtualKey, WeakReference>> s_VirtualKeyControlInverseChordsForButtons;

static multimap<int, bool> s_ShiftKeyPressed;
static multimap<int, bool> s_ControlKeyPressed;
static multimap<int, bool> s_ShiftButtonChecked;
static multimap<int, bool> s_IsDropDownOpen;
static map<int, bool> s_ShiftKeyPressed;
static map<int, bool> s_ControlKeyPressed;
static map<int, bool> s_ShiftButtonChecked;
static map<int, bool> s_IsDropDownOpen;

static map<int, bool> s_ignoreNextEscape;
static map<int, bool> s_keepIgnoringEscape;
static map<int, bool> s_fHonorShortcuts;
static map<int, Flyout ^> s_AboutFlyout;

static reader_writer_lock s_keyboardShortcutMapLock;

Expand Down Expand Up @@ -158,12 +163,6 @@ namespace CalculatorApp
}
}

static multimap<int, bool> s_ignoreNextEscape;
static multimap<int, bool> s_keepIgnoringEscape;
static multimap<int, bool> s_fHonorShortcuts;
static multimap<int, bool> s_fHandledEnter;
static multimap<int, Flyout ^> s_AboutFlyout;

void KeyboardShortcutManager::IgnoreEscape(bool onlyOnce)
{
// Writer lock for the static maps
Expand All @@ -173,14 +172,12 @@ void KeyboardShortcutManager::IgnoreEscape(bool onlyOnce)

if (s_ignoreNextEscape.find(viewId) != s_ignoreNextEscape.end())
{
s_ignoreNextEscape.erase(viewId);
s_ignoreNextEscape.insert(std::make_pair(viewId, true));
s_ignoreNextEscape[viewId] = true;
}

if (s_keepIgnoringEscape.find(viewId) != s_keepIgnoringEscape.end())
{
s_keepIgnoringEscape.erase(viewId);
s_keepIgnoringEscape.insert(std::make_pair(viewId, !onlyOnce));
s_keepIgnoringEscape[viewId] = !onlyOnce;
}
}

Expand All @@ -193,14 +190,12 @@ void KeyboardShortcutManager::HonorEscape()

if (s_ignoreNextEscape.find(viewId) != s_ignoreNextEscape.end())
{
s_ignoreNextEscape.erase(viewId);
s_ignoreNextEscape.insert(std::make_pair(viewId, false));
s_ignoreNextEscape[viewId] = false;
}

if (s_keepIgnoringEscape.find(viewId) != s_keepIgnoringEscape.end())
{
s_keepIgnoringEscape.erase(viewId);
s_keepIgnoringEscape.insert(std::make_pair(viewId, false));
s_keepIgnoringEscape[viewId] = false;
}
}

Expand Down Expand Up @@ -474,8 +469,8 @@ const std::multimap<MyVirtualKey, WeakReference>& GetCurrentKeyDictionary(MyVirt
}
else
{
auto iterViewMap = s_VirtualKeyControlInverseChordsForButtons.find(viewId);
if (iterViewMap != s_VirtualKeyControlInverseChordsForButtons.end())
auto iterViewMap = s_VirtualKeyInverseChordsForButtons.find(viewId);
rudyhuyn marked this conversation as resolved.
Show resolved Hide resolved
if (iterViewMap != s_VirtualKeyInverseChordsForButtons.end())
{
for (auto iterator = iterViewMap->second.begin(); iterator != iterViewMap->second.end(); ++iterator)
{
Expand Down Expand Up @@ -558,8 +553,7 @@ void KeyboardShortcutManager::OnKeyDownHandler(CoreWindow ^ sender, KeyEventArgs

if (currControlKeyPressed != s_ControlKeyPressed.end())
{
s_ControlKeyPressed.erase(viewId);
s_ControlKeyPressed.insert(std::make_pair(viewId, true));
s_ControlKeyPressed[viewId] = true;
}
return;
}
Expand All @@ -572,26 +566,24 @@ void KeyboardShortcutManager::OnKeyDownHandler(CoreWindow ^ sender, KeyEventArgs

if (currShiftKeyPressed != s_ShiftKeyPressed.end())
{
s_ShiftKeyPressed.erase(viewId);
s_ShiftKeyPressed.insert(std::make_pair(viewId, true));
s_ShiftKeyPressed[viewId] = true;
}
return;
}

const auto& lookupMap = GetCurrentKeyDictionary(static_cast<MyVirtualKey>(key));
auto buttons = lookupMap.equal_range(static_cast<MyVirtualKey>(key));

auto currentIsDropDownOpen = s_IsDropDownOpen.find(viewId);

if (currentHonorShortcuts != s_fHonorShortcuts.end())
{
if (currentHonorShortcuts->second)
{
const auto myVirtualKey = static_cast<MyVirtualKey>(key);
const auto& lookupMap = GetCurrentKeyDictionary(myVirtualKey);
auto buttons = lookupMap.equal_range(myVirtualKey);
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
// equivalent to Ctrl+C and Shift+Insert is equivalent to Ctrl+V
auto currentIsDropDownOpen = s_IsDropDownOpen.find(viewId);
if (currentIsDropDownOpen != s_IsDropDownOpen.end() && !currentIsDropDownOpen->second)
{
// Do not Light Up Buttons when Ctrl+C, Ctrl+V, Ctrl+Insert or Shift+Insert is pressed
Expand Down Expand Up @@ -620,8 +612,7 @@ void KeyboardShortcutManager::OnKeyUpHandler(CoreWindow ^ sender, KeyEventArgs ^

if (currentShiftKeyPressed != s_ShiftKeyPressed.end())
{
s_ShiftKeyPressed.erase(viewId);
s_ShiftKeyPressed.insert(std::make_pair(viewId, false));
s_ShiftKeyPressed[viewId] = false;
}
}
else if (key == VirtualKey::Control)
Expand All @@ -633,8 +624,7 @@ void KeyboardShortcutManager::OnKeyUpHandler(CoreWindow ^ sender, KeyEventArgs ^

if (currControlKeyPressed != s_ControlKeyPressed.end())
{
s_ControlKeyPressed.erase(viewId);
s_ControlKeyPressed.insert(std::make_pair(viewId, false));
s_ControlKeyPressed[viewId] = false;
}
}
}
Expand Down Expand Up @@ -712,8 +702,7 @@ void KeyboardShortcutManager::ShiftButtonChecked(bool checked)

if (s_ShiftButtonChecked.find(viewId) != s_ShiftButtonChecked.end())
{
s_ShiftButtonChecked.erase(viewId);
s_ShiftButtonChecked.insert(std::make_pair(viewId, checked));
s_ShiftButtonChecked[viewId] = checked;
}
}

Expand All @@ -723,8 +712,7 @@ void KeyboardShortcutManager::UpdateDropDownState(bool isOpen)

if (s_IsDropDownOpen.find(viewId) != s_IsDropDownOpen.end())
{
s_IsDropDownOpen.erase(viewId);
s_IsDropDownOpen.insert(std::make_pair(viewId, isOpen));
s_IsDropDownOpen[viewId] = isOpen;
}
}

Expand All @@ -734,8 +722,7 @@ void KeyboardShortcutManager::UpdateDropDownState(Flyout ^ aboutPageFlyout)

if (s_AboutFlyout.find(viewId) != s_AboutFlyout.end())
{
s_AboutFlyout.erase(viewId);
s_AboutFlyout.insert(std::make_pair(viewId, aboutPageFlyout));
s_AboutFlyout[viewId] = aboutPageFlyout;
}
}

Expand All @@ -748,19 +735,7 @@ void KeyboardShortcutManager::HonorShortcuts(bool allow)

if (s_fHonorShortcuts.find(viewId) != s_fHonorShortcuts.end())
{
s_fHonorShortcuts.erase(viewId);
s_fHonorShortcuts.insert(std::make_pair(viewId, allow));
}
}

void KeyboardShortcutManager::HandledEnter(bool ishandled)
{
int viewId = Utils::GetWindowId();

if (s_fHandledEnter.find(viewId) != s_fHandledEnter.end())
{
s_fHandledEnter.erase(viewId);
s_fHandledEnter.insert(std::make_pair(viewId, ishandled));
s_fHonorShortcuts[viewId] = allow;
}
}

Expand Down Expand Up @@ -812,15 +787,14 @@ void KeyboardShortcutManager::RegisterNewAppViewId()
s_VirtualKeyControlInverseChordsForButtons.insert(std::make_pair(appViewId, std::multimap<MyVirtualKey, WeakReference>()));
}

s_ShiftKeyPressed.insert(std::make_pair(appViewId, false));
s_ControlKeyPressed.insert(std::make_pair(appViewId, false));
s_ShiftButtonChecked.insert(std::make_pair(appViewId, false));
s_IsDropDownOpen.insert(std::make_pair(appViewId, false));
s_ignoreNextEscape.insert(std::make_pair(appViewId, false));
s_keepIgnoringEscape.insert(std::make_pair(appViewId, false));
s_fHonorShortcuts.insert(std::make_pair(appViewId, true));
s_fHandledEnter.insert(std::make_pair(appViewId, true));
s_AboutFlyout.insert(std::make_pair(appViewId, nullptr));
s_ShiftKeyPressed[appViewId] = false;
s_ControlKeyPressed[appViewId] = false;
s_ShiftButtonChecked[appViewId] = false;
s_IsDropDownOpen[appViewId] = false;
s_ignoreNextEscape[appViewId] = false;
s_keepIgnoringEscape[appViewId] = false;
s_fHonorShortcuts[appViewId] = true;
s_AboutFlyout[appViewId] = nullptr;
}

void KeyboardShortcutManager::OnWindowClosed(int viewId)
Expand All @@ -845,6 +819,5 @@ void KeyboardShortcutManager::OnWindowClosed(int viewId)
s_ignoreNextEscape.erase(viewId);
s_keepIgnoringEscape.erase(viewId);
s_fHonorShortcuts.erase(viewId);
s_fHandledEnter.erase(viewId);
s_AboutFlyout.erase(viewId);
}
1 change: 0 additions & 1 deletion src/CalcViewModel/Common/KeyboardShortcutManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ namespace CalculatorApp
static void IgnoreEscape(bool onlyOnce);
static void HonorEscape();
static void HonorShortcuts(bool allow);
static void HandledEnter(bool ishandled);
static void UpdateDropDownState(bool);
static void ShiftButtonChecked(bool checked);
static void UpdateDropDownState(Windows::UI::Xaml::Controls::Flyout ^ aboutPageFlyout);
Expand Down
8 changes: 0 additions & 8 deletions src/Calculator/Resources/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -3883,10 +3883,6 @@
<value>Reset View</value>
<comment>Screen reader prompt for the reset zoom button.</comment>
</data>
<data name="zoomInButton.[using:CalculatorApp.Common]KeyboardShortcutManager.VirtualKeyControlChord" xml:space="preserve">
<value>Add</value>
<comment>{Locked}This is the shortcut for the zoom in button.</comment>
</data>
<data name="zoomInButton.[using:Windows.UI.Xaml.Controls]ToolTipService.ToolTip" xml:space="preserve">
<value>Zoom In</value>
<comment>This is the tool tip automation name for the Calculator zoom in button.</comment>
Expand All @@ -3895,10 +3891,6 @@
<value>Zoom In</value>
<comment>Screen reader prompt for the zoom in button.</comment>
</data>
<data name="zoomOutButton.[using:CalculatorApp.Common]KeyboardShortcutManager.VirtualKeyControlChord" xml:space="preserve">
<value>Subtract</value>
<comment>{Locked}This is the shortcut for the zoom out button.</comment>
</data>
<data name="zoomOutButton.[using:Windows.UI.Xaml.Controls]ToolTipService.ToolTip" xml:space="preserve">
<value>Zoom Out</value>
<comment>This is the tool tip automation name for the Calculator zoom out button.</comment>
Expand Down
17 changes: 12 additions & 5 deletions src/Calculator/Views/GraphingCalculator/GraphingCalculator.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,8 @@
VerticalAlignment="Top"
Style="{ThemeResource GraphControlCommandPanel}"
RequestedTheme="Light">
<StackPanel Orientation="Horizontal">
<ToggleButton x:Name="ActiveTracing"
<StackPanel Orientation="Horizontal">
<ToggleButton x:Name="ActiveTracing"
MinWidth="40"
Margin="0,-1"
Style="{ThemeResource ThemedGraphToggleButtonStyle}"
Expand Down Expand Up @@ -405,8 +405,8 @@
Style="{ThemeResource GraphControlCommandPanel}"
RequestedTheme="Light">
<StackPanel Orientation="Vertical">

<RepeatButton x:Uid="zoomInButton"
<RepeatButton x:Name="ZoomInButton"
x:Uid="zoomInButton"
MinHeight="40"
HorizontalAlignment="Stretch"
Style="{ThemeResource ThemedGraphRepeatButtonStyle}"
Expand All @@ -417,9 +417,13 @@
<FontIcon FontFamily="{StaticResource SymbolThemeFontFamily}"
FontSize="14"
Glyph="&#xE710;"/>
<RepeatButton.KeyboardAccelerators>
<KeyboardAccelerator Key="Add" Modifiers="Control"/>
</RepeatButton.KeyboardAccelerators>
</RepeatButton>

<RepeatButton x:Uid="zoomOutButton"
<RepeatButton x:Name="ZoomOutButton"
x:Uid="zoomOutButton"
MinHeight="40"
HorizontalAlignment="Stretch"
Style="{ThemeResource ThemedGraphRepeatButtonStyle}"
Expand All @@ -429,6 +433,9 @@
<FontIcon FontFamily="{StaticResource SymbolThemeFontFamily}"
FontSize="14"
Glyph="&#xE738;"/>
<RepeatButton.KeyboardAccelerators>
<KeyboardAccelerator Key="Subtract" Modifiers="Control"/>
</RepeatButton.KeyboardAccelerators>
</RepeatButton>

<Button x:Uid="zoomResetButton"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ constexpr auto sc_ViewModelPropertyName = L"ViewModel";

DEPENDENCY_PROPERTY_INITIALIZATION(GraphingCalculator, IsSmallState);

GraphingCalculator::GraphingCalculator()
GraphingCalculator::GraphingCalculator()
{
InitializeComponent();

Expand All @@ -66,6 +66,17 @@ GraphingCalculator::GraphingCalculator()

// And when the actual trace value changes
GraphingControl->TracingValueChangedEvent += ref new TracingValueChangedEventHandler(this, &GraphingCalculator::OnTracePointChanged);

// OemMinus and OemAdd aren't declared in the VirtualKey enum, we can't add this accelerator XAML-side
auto virtualKey = ref new KeyboardAccelerator();
virtualKey->Key = (VirtualKey)187; //OemMinus key
virtualKey->Modifiers = VirtualKeyModifiers::Control;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the need for Ctrl instead of just +/- directly? I think it might be more intuitive and discoverable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specs :D

I suppose to have the same behavior than Maps.

cc @grochocki

ZoomOutButton->KeyboardAccelerators->Append(virtualKey);

virtualKey = ref new KeyboardAccelerator();
virtualKey->Key = (VirtualKey)189; //OemAdd key
virtualKey->Modifiers = VirtualKeyModifiers::Control;
ZoomInButton->KeyboardAccelerators->Append(virtualKey);
}

void GraphingCalculator::OnShowTracePopupChanged(bool newValue)
Expand Down