Skip to content

Commit

Permalink
Teach tab tool tips to show key bindings (#8810)
Browse files Browse the repository at this point in the history
<!-- Please review the items on the PR checklist before submitting-->
## PR Checklist
* [x] Closes #2886
* [x] CLA signed. 
* [ ] Tests added/passed
* [ ] Documentation updated. 
* [ ] Schema updated.
* [ ] I've discussed this with core contributors already. 

## Detailed Description of the Pull Request / Additional comments
Currently the tab tool tip is the tab's title.
The PR teaches the TabBase to check if there is a switch to tab command 
associated with the current tab index,
if so concatenates the the relevant mapping to the too tip.

Of course, prefers user defined bindings to the default ones.

Moved tool tip logic to TabBase so SettingsTab has tooltip as well.

![TabToolTip](https://user-images.githubusercontent.com/4639110/104823154-a1cb1100-5850-11eb-9dbd-bf23f5e6979d.gif)
  • Loading branch information
Don-Vito committed Jan 19, 2021
1 parent de49cf1 commit 90e7c28
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 9 deletions.
67 changes: 67 additions & 0 deletions src/cascadia/TerminalApp/TabBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,77 @@ namespace winrt::TerminalApp::implementation
TabViewIndex(idx);
TabViewNumTabs(numTabs);
_EnableCloseMenuItems();
_UpdateSwitchToTabKeyChord();
}

void TabBase::SetDispatch(const winrt::TerminalApp::ShortcutActionDispatch& dispatch)
{
_dispatch = dispatch;
}

void TabBase::SetKeyMap(const Microsoft::Terminal::Settings::Model::KeyMapping& keymap)
{
_keymap = keymap;
_UpdateSwitchToTabKeyChord();
}

// Method Description:
// - Sets the key chord resulting in switch to the current tab.
// Updates tool tip if required
// Arguments:
// - keyChord - string representation of the key chord that switches to the current tab
// Return Value:
// - <none>
winrt::fire_and_forget TabBase::_UpdateSwitchToTabKeyChord()
{
SwitchToTabArgs args{ _TabViewIndex };
ActionAndArgs switchToTab{ ShortcutAction::SwitchToTab, args };
const auto keyChord = _keymap ? _keymap.GetKeyBindingForActionWithArgs(switchToTab) : nullptr;
const auto keyChordText = keyChord ? KeyChordSerialization::ToString(keyChord) : L"";

if (_keyChord == keyChordText)
{
return;
}

_keyChord = keyChordText;

auto weakThis{ get_weak() };

co_await winrt::resume_foreground(TabViewItem().Dispatcher());

if (auto tab{ weakThis.get() })
{
_UpdateToolTip();
}
}

// Method Description:
// - Sets tab tool tip to a concatenation of title and key chord
// Arguments:
// - <none>
// Return Value:
// - <none>
void TabBase::_UpdateToolTip()
{
auto titleRun = WUX::Documents::Run();
titleRun.Text(_Title);

auto textBlock = WUX::Controls::TextBlock{};
textBlock.TextAlignment(WUX::TextAlignment::Center);
textBlock.Inlines().Append(titleRun);

if (!_keyChord.empty())
{
auto keyChordRun = WUX::Documents::Run();
keyChordRun.Text(_keyChord);
keyChordRun.FontStyle(winrt::Windows::UI::Text::FontStyle::Italic);
textBlock.Inlines().Append(WUX::Documents::LineBreak{});
textBlock.Inlines().Append(keyChordRun);
}

WUX::Controls::ToolTip toolTip{};
toolTip.Content(textBlock);
WUX::Controls::ToolTipService::SetToolTip(TabViewItem(), toolTip);
}
}
5 changes: 5 additions & 0 deletions src/cascadia/TerminalApp/TabBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace winrt::TerminalApp::implementation
void SetDispatch(const winrt::TerminalApp::ShortcutActionDispatch& dispatch);

void UpdateTabViewIndex(const uint32_t idx, const uint32_t numTabs);
void SetKeyMap(const Microsoft::Terminal::Settings::Model::KeyMapping& keymap);

WINRT_CALLBACK(Closed, winrt::Windows::Foundation::EventHandler<winrt::Windows::Foundation::IInspectable>);
WINRT_CALLBACK(PropertyChanged, Windows::UI::Xaml::Data::PropertyChangedEventHandler);
Expand All @@ -43,12 +44,16 @@ namespace winrt::TerminalApp::implementation
winrt::Windows::UI::Xaml::Controls::MenuFlyoutItem _closeOtherTabsMenuItem{};
winrt::Windows::UI::Xaml::Controls::MenuFlyoutItem _closeTabsAfterMenuItem{};
winrt::TerminalApp::ShortcutActionDispatch _dispatch;
Microsoft::Terminal::Settings::Model::KeyMapping _keymap{ nullptr };
winrt::hstring _keyChord{};

virtual void _CreateContextMenu();
winrt::Windows::UI::Xaml::Controls::MenuFlyoutSubItem _CreateCloseSubMenu();
void _EnableCloseMenuItems();
void _CloseTabsAfter();
void _CloseOtherTabs();
winrt::fire_and_forget _UpdateSwitchToTabKeyChord();
void _UpdateToolTip();

friend class ::TerminalAppLocalTests::TabTests;
};
Expand Down
5 changes: 5 additions & 0 deletions src/cascadia/TerminalApp/TerminalPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,7 @@ namespace winrt::TerminalApp::implementation
_mruTabs.Append(*newTabImpl);

newTabImpl->SetDispatch(*_actionDispatch);
newTabImpl->SetKeyMap(_settings.KeyMap());

// Give the tab its index in the _tabs vector so it can manage its own SwitchToTab command.
_UpdateTabIndices();
Expand Down Expand Up @@ -2341,6 +2342,9 @@ namespace winrt::TerminalApp::implementation
{
settingsTab.UpdateSettings(_settings);
}

auto tabImpl{ winrt::get_self<TabBase>(tab) };
tabImpl->SetKeyMap(_settings.KeyMap());
}

auto weakThis{ get_weak() };
Expand Down Expand Up @@ -2827,6 +2831,7 @@ namespace winrt::TerminalApp::implementation
_mruTabs.Append(*newTabImpl);

newTabImpl->SetDispatch(*_actionDispatch);
newTabImpl->SetKeyMap(_settings.KeyMap());

// Give the tab its index in the _tabs vector so it can manage its own SwitchToTab command.
_UpdateTabIndices();
Expand Down
9 changes: 1 addition & 8 deletions src/cascadia/TerminalApp/TerminalTab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,6 @@ namespace winrt::TerminalApp::implementation
}
}

void TerminalTab::_SetToolTip(const winrt::hstring& tabTitle)
{
WUX::Controls::ToolTip toolTip{};
toolTip.Content(winrt::box_value(tabTitle));
WUX::Controls::ToolTipService::SetToolTip(TabViewItem(), toolTip);
}

// Method Description:
// - Returns nullptr if no children of this tab were the last control to be
// focused, or the TermControl that _was_ the last control to be focused (if
Expand Down Expand Up @@ -299,7 +292,7 @@ namespace winrt::TerminalApp::implementation

// Update the control to reflect the changed title
_headerControl.Title(activeTitle);
_SetToolTip(activeTitle);
_UpdateToolTip();
}
}

Expand Down
1 change: 0 additions & 1 deletion src/cascadia/TerminalApp/TerminalTab.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ namespace winrt::TerminalApp::implementation
void _MakeTabViewItem();

winrt::fire_and_forget _UpdateHeaderControlMaxWidth();
void _SetToolTip(const winrt::hstring& tabTitle);

void _CreateContextMenu() override;

Expand Down

0 comments on commit 90e7c28

Please sign in to comment.