Skip to content

Commit

Permalink
let's generalize this for both of these keys
Browse files Browse the repository at this point in the history
  • Loading branch information
zadjii-msft committed Jun 11, 2020
1 parent 1529c27 commit 057fb83
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 129 deletions.
42 changes: 4 additions & 38 deletions src/cascadia/TerminalApp/AppLogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -906,44 +906,10 @@ namespace winrt::TerminalApp::implementation

// Method Description:
// - Implements the F7 handler (per GH#638)
// Return value:
// - whether F7 was handled
bool AppLogic::OnF7Pressed()
{
if (_root)
{
// Manually bubble the OnF7Pressed event up through the focus tree.
auto xamlRoot{ _root->XamlRoot() };
auto focusedObject{ Windows::UI::Xaml::Input::FocusManager::GetFocusedElement(xamlRoot) };
do
{
if (auto f7Listener{ focusedObject.try_as<IF7Listener>() })
{
if (f7Listener.OnF7Pressed())
{
return true;
}
// otherwise, keep walking. bubble the event manually.
}

if (auto focusedElement{ focusedObject.try_as<Windows::UI::Xaml::FrameworkElement>() })
{
focusedObject = focusedElement.Parent();
}
else
{
break; // we hit a non-FE object, stop bubbling.
}
} while (focusedObject);
}
return false;
}

// Method Description:
// - Implements the Alt handler (per GH#6421)
// Return value:
// - whether Alt was handled
bool AppLogic::OnAltReleased()
// - whether the key was handled
bool AppLogic::OnGotAKey(const uint32_t vkey, const bool down)
{
if (_root)
{
Expand All @@ -952,9 +918,9 @@ namespace winrt::TerminalApp::implementation
auto focusedObject{ Windows::UI::Xaml::Input::FocusManager::GetFocusedElement(xamlRoot) };
do
{
if (auto f7Listener{ focusedObject.try_as<IAltListener>() })
if (auto keyListener{ focusedObject.try_as<IDirectKeyListener>() })
{
if (f7Listener.OnAltReleased())
if (keyListener.OnGotAKey(vkey, down))
{
return true;
}
Expand Down
3 changes: 1 addition & 2 deletions src/cascadia/TerminalApp/AppLogic.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ namespace winrt::TerminalApp::implementation

hstring Title();
void TitlebarClicked();
bool OnF7Pressed();
bool OnAltReleased();
bool OnGotAKey(const uint32_t vkey, const bool down);

void WindowCloseButtonClicked();

Expand Down
5 changes: 2 additions & 3 deletions src/cascadia/TerminalApp/AppLogic.idl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

import "../TerminalPage.idl";
import "../ShortcutActionDispatch.idl";
import "../IF7Listener.idl";
import "../IAltListener.idl";
import "../IDirectKeyListener.idl";

namespace TerminalApp
{
Expand All @@ -15,7 +14,7 @@ namespace TerminalApp
FullscreenMode,
};

[default_interface] runtimeclass AppLogic : IF7Listener, IAltListener
[default_interface] runtimeclass AppLogic : IDirectKeyListener
{
AppLogic();

Expand Down
15 changes: 0 additions & 15 deletions src/cascadia/TerminalApp/IAltListener.idl

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ namespace TerminalApp
// Instead, we just pin the uuid and include it in both TermControl and App
// If you update this one, please update the one in TerminalControl\TermControl.idl
// If you change this interface, please update the guid.
// If you press F7 and get a runtime error, go make sure both copies are the same.
// If you press F7 or Alt and get a runtime error, go make sure both copies are the same.
[uuid("339e1a87-5315-4da6-96f0-565549b6472b")]
interface IF7Listener
{
Boolean OnF7Pressed();
interface IDirectKeyListener {
Boolean OnGotAKey(UInt32 vkey, Boolean down);
}
}
3 changes: 1 addition & 2 deletions src/cascadia/TerminalApp/lib/TerminalAppLib.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,7 @@
<ItemGroup>
<!-- If you add idl files here, make sure to include their implementation's
header in TerminalApp.vcxproj (as well as in this file) -->
<Midl Include="../IF7Listener.idl" />
<Midl Include="../IAltListener.idl" />
<Midl Include="../IDirectKeyListener.idl" />
<Midl Include="../App.idl">
<DependentUpon>../App.xaml</DependentUpon>
</Midl>
Expand Down
69 changes: 33 additions & 36 deletions src/cascadia/TerminalControl/TermControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -683,50 +683,47 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
}

// Method Description:
// - Manually generate an F7 event into the key bindings or terminal.
// This is required as part of GH#638.
// - Manually handles key events for certain keys that can't be passed to us
// normally. Namely, the keys we're concerned with are F7 down and Alt up.
// Return value:
// - Whether F7 was handled.
bool TermControl::OnF7Pressed()
// - Whether the key was handled.
bool TermControl::OnGotAKey(const uint32_t vkey, const bool down)
{
bool handled{ false };
auto bindings{ _settings.KeyBindings() };

const auto modifiers{ _GetPressedModifierKeys() };

if (bindings)
{
handled = bindings.TryKeyChord({
modifiers.IsCtrlPressed(),
modifiers.IsAltPressed(),
modifiers.IsShiftPressed(),
VK_F7,
});
}

if (!handled)
auto handled = false;
if (vkey == VK_MENU && !down)
{
// _TrySendKeyEvent pretends it didn't handle F7 for some unknown reason.
(void)_TrySendKeyEvent(VK_F7, 0, modifiers, true);
// GH#6438: Note that we're _not_ sending the key up here - that'll
// get passed through XAML to our KeyUp handler normally.
// Manually generate an Alt KeyUp event into the key bindings or terminal.
// This is required as part of GH#6421.
(void)_TrySendKeyEvent(VK_MENU, 0, modifiers, false);
handled = true;
}
else if (vkey == VK_F7 && down)
{
// Manually generate an F7 event into the key bindings or terminal.
// This is required as part of GH#638.
auto bindings{ _settings.KeyBindings() };

return handled;
}
if (bindings)
{
handled = bindings.TryKeyChord({
modifiers.IsCtrlPressed(),
modifiers.IsAltPressed(),
modifiers.IsShiftPressed(),
VK_F7,
});
}

// Method Description:
// - Manually generate an Alt KeyUp event into the key bindings or terminal.
// This is required as part of GH#6421.
// - This is basically the same thing as the F7 hack above, but with Alt KeyUp instead.
// Return value:
// - Whether Alt was handled.
bool TermControl::OnAltReleased()
{
const auto modifiers{ _GetPressedModifierKeys() };
(void)_TrySendKeyEvent(VK_MENU, 0, modifiers, false);
return true;
if (!handled)
{
// _TrySendKeyEvent pretends it didn't handle F7 for some unknown reason.
(void)_TrySendKeyEvent(VK_F7, 0, modifiers, true);
// GH#6438: Note that we're _not_ sending the key up here - that'll
// get passed through XAML to our KeyUp handler normally.
handled = true;
}
}
return handled;
}

void TermControl::_KeyDownHandler(winrt::Windows::Foundation::IInspectable const& /*sender*/,
Expand Down
3 changes: 1 addition & 2 deletions src/cascadia/TerminalControl/TermControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation

void CreateSearchBoxControl();

bool OnF7Pressed();
bool OnAltReleased();
bool OnGotAKey(const uint32_t vkey, const bool down);

bool OnMouseWheel(const Windows::Foundation::Point location, const int32_t delta);

Expand Down
17 changes: 5 additions & 12 deletions src/cascadia/TerminalControl/TermControl.idl
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,12 @@ namespace Microsoft.Terminal.TerminalControl

// C++/winrt makes it difficult to share this idl between two projects,
// Instead, we just pin the uuid and include it in both TermControl and App
// If you update this one, please update TerminalApp\IF7Listener.idl.
// If you update this one, please update TerminalApp\IDirectKeyListener.idl.
// If you change this interface, please update the guid.
// If you press F7 and get a runtime error, go make sure both copies are the same.
// If you press F7 or Alt and get a runtime error, go make sure both copies are the same.
[uuid("339e1a87-5315-4da6-96f0-565549b6472b")]
interface IF7Listener {
Boolean OnF7Pressed();
}

// Same as the above, but with Alt
[uuid("36a6554d-07e9-4861-8d8b-5d882de31e75")]
interface IAltListener
{
Boolean OnAltReleased();
interface IDirectKeyListener {
Boolean OnGotAKey(UInt32 vkey, Boolean down);
}

runtimeclass CopyToClipboardEventArgs
Expand All @@ -38,7 +31,7 @@ namespace Microsoft.Terminal.TerminalControl
void HandleClipboardData(String data);
}

[default_interface] runtimeclass TermControl : Windows.UI.Xaml.Controls.UserControl, IF7Listener, IAltListener, IMouseWheelListener
[default_interface] runtimeclass TermControl : Windows.UI.Xaml.Controls.UserControl, IDirectKeyListener, IMouseWheelListener
{
TermControl();
TermControl(Microsoft.Terminal.Settings.IControlSettings settings, Microsoft.Terminal.TerminalConnection.ITerminalConnection connection);
Expand Down
13 changes: 2 additions & 11 deletions src/cascadia/WindowsTerminal/AppHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,11 @@ AppHost::~AppHost()
_app = nullptr;
}

bool AppHost::OnF7Pressed()
bool AppHost::OnGotAKey(const uint32_t vkey, const bool down)
{
if (_logic)
{
return _logic.OnF7Pressed();
}
return false;
}

bool AppHost::OnAltReleased()
{
if (_logic)
{
return _logic.OnAltReleased();
return _logic.OnGotAKey(vkey, down);
}
return false;
}
Expand Down
3 changes: 1 addition & 2 deletions src/cascadia/WindowsTerminal/AppHost.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ class AppHost
void AppTitleChanged(const winrt::Windows::Foundation::IInspectable& sender, winrt::hstring newTitle);
void LastTabClosed(const winrt::Windows::Foundation::IInspectable& sender, const winrt::TerminalApp::LastTabClosedEventArgs& args);
void Initialize();
bool OnF7Pressed();
bool OnAltReleased();
bool OnGotAKey(const uint32_t vkey, const bool down);

private:
bool _useNonClientArea;
Expand Down
4 changes: 2 additions & 2 deletions src/cascadia/WindowsTerminal/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ int __stdcall wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int)
// been handled we can discard the message before we even translate it.
if (_messageIsF7Keypress(message))
{
if (host.OnF7Pressed())
if (host.OnGotAKey(VK_F7, true))
{
// The application consumed the F7. Don't let Xaml get it.
continue;
Expand All @@ -154,7 +154,7 @@ int __stdcall wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int)
if (_messageIsAltKeyup(message))
{
// Let's pass <Alt> to the application
if (host.OnAltReleased())
if (host.OnGotAKey(VK_MENU, false))
{
// The application consumed the Alt. Don't let Xaml get it.
continue;
Expand Down

1 comment on commit 057fb83

@github-actions

This comment was marked as resolved.

Please sign in to comment.