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

Enable PgUp/PgDown and Home/End in the command palette #7835

Merged
21 commits merged into from Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
31 changes: 23 additions & 8 deletions src/cascadia/TerminalApp/CommandPalette.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,21 @@ namespace winrt::TerminalApp::implementation
// - Moves the focus up or down the list of commands. If we're at the top,
// we'll loop around to the bottom, and vice-versa.
// Arguments:
// - moveDown: if true, we're attempting to move to the next item in the
// list. Otherwise, we're attempting to move to the previous.
// - moveDown: if true, we're attempting to move to the next or a few next item in the
// list. Otherwise, we're attempting to move to the previous or not very many previous.
Copy link
Contributor

Choose a reason for hiding this comment

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

Document the new parameter

// Return Value:
// - <none>
void CommandPalette::SelectNextItem(const bool moveDown)
void CommandPalette::SelectNextItem(const bool moveDown, const bool pageButtonPressed)
{
const auto listHeight = ::base::saturated_cast<int>(_filteredActionsView().ActualHeight());
const int numVisibleItems = listHeight / 42;
Copy link
Contributor

Choose a reason for hiding this comment

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

If 42 cannot be computed, consider extracting it to a const and documenting why this value was chosen


const auto selected = _filteredActionsView().SelectedIndex();
const int numItems = ::base::saturated_cast<int>(_filteredActionsView().Items().Size());
// Wraparound math. By adding numItems and then calculating modulo numItems,
// we clamp the values to the range [0, numItems) while still supporting moving
// upward from 0 to numItems - 1.
const auto newIndex = ((numItems + selected + (moveDown ? 1 : -1)) % numItems);
const auto newIndex = ((numItems + selected + (moveDown ? (pageButtonPressed ? numVisibleItems : 1) : (pageButtonPressed ? -numVisibleItems : -1))) % numItems);
Copy link
Contributor

Choose a reason for hiding this comment

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

The logic duplication can be avoided by extracting a "delta" variable: moveDown? delta : -delta

_filteredActionsView().SelectedIndex(newIndex);
_filteredActionsView().ScrollIntoView(_filteredActionsView().SelectedItem());
}
Expand All @@ -134,12 +137,12 @@ namespace winrt::TerminalApp::implementation
auto const state = CoreWindow::GetForCurrentThread().GetKeyState(winrt::Windows::System::VirtualKey::Shift);
if (WI_IsFlagSet(state, CoreVirtualKeyStates::Down))
{
SelectNextItem(false);
SelectNextItem(false, false);
e.Handled(true);
}
else
{
SelectNextItem(true);
SelectNextItem(true, false);
e.Handled(true);
}
}
Expand All @@ -161,13 +164,25 @@ namespace winrt::TerminalApp::implementation
if (key == VirtualKey::Up)
{
// Action Mode: Move focus to the next item in the list.
SelectNextItem(false);
SelectNextItem(false, false);
e.Handled(true);
}
else if (key == VirtualKey::Down)
{
// Action Mode: Move focus to the previous item in the list.
SelectNextItem(true);
SelectNextItem(true, false);
e.Handled(true);
}
else if (key == VirtualKey::PageUp)
{
// Action Mode: Move focus to the previous item in the list.
SelectNextItem(false, true);
e.Handled(true);
}
else if (key == VirtualKey::PageDown)
{
// Action Mode: Move focus to the previous item in the list.
SelectNextItem(true, true);
zadjii-msft marked this conversation as resolved.
Show resolved Hide resolved
e.Handled(true);
}
else if (key == VirtualKey::Enter)
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalApp/CommandPalette.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace winrt::TerminalApp::implementation

bool OnDirectKeyEvent(const uint32_t vkey, const uint8_t scanCode, const bool down);

void SelectNextItem(const bool moveDown);
void SelectNextItem(const bool moveDown, const bool pageButtonPressed);

// Tab Switcher
void EnableTabSwitcherMode(const bool searchMode, const uint32_t startIdx);
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalApp/CommandPalette.idl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace TerminalApp
void SetKeyBindings(Microsoft.Terminal.TerminalControl.IKeyBindings bindings);
void EnableCommandPaletteMode();

void SelectNextItem(Boolean moveDown);
void SelectNextItem(Boolean moveDown, Boolean pageButtonPressed);

void SetDispatch(ShortcutActionDispatch dispatch);

Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalApp/TerminalPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,7 @@ namespace winrt::TerminalApp::implementation
{
if (CommandPalette().Visibility() == Visibility::Visible)
{
CommandPalette().SelectNextItem(bMoveRight);
CommandPalette().SelectNextItem(bMoveRight, false);
}

CommandPalette().EnableTabSwitcherMode(false, newTabIndex);
Expand Down