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

Manually pass mouse wheel messages to TermControls #5131

Merged
20 commits merged into from
Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
dae733a
try naively passing the mouse event through, but that doesn't seem to…
zadjii Mar 24, 2020
892bc58
doing this the right way still isn't right
zadjii Mar 24, 2020
b708a08
start working on manually plumbing the event through, but this feels …
zadjii Mar 24, 2020
68ace1a
This is so dumb ; I love it
zadjii-msft Mar 24, 2020
7252ab6
Hey if your laptop's clock batttery is broken, you're going to have I…
zadjii-msft Mar 24, 2020
b3da428
Boy this is such a dirty hack but hey it works
zadjii-msft Mar 25, 2020
cf9d172
So very much code cleanup
zadjii-msft Mar 25, 2020
b5a6de3
Merge remote-tracking branch 'origin/master' into dev/migrie/b/979-hp…
zadjii-msft Mar 26, 2020
47f8591
runformat
zadjii-msft Mar 26, 2020
10cb133
Appease the spelling god?
zadjii-msft Mar 26, 2020
52c94dc
fix the SA build
zadjii-msft Mar 26, 2020
6e33a0f
this wasn't that scary
zadjii-msft Mar 27, 2020
c0ed7ea
this is a handy helper for converting between the two
zadjii-msft Mar 27, 2020
f3483c2
Some minor code cleanup to make things easier to read
zadjii-msft Mar 27, 2020
c7a1e77
some last changes for making events relative to the control origin
zadjii-msft Mar 27, 2020
0de39a7
good bot
zadjii-msft Mar 27, 2020
216941a
Merge remote-tracking branch 'origin/master' into dev/migrie/b/979-hp…
zadjii-msft Mar 30, 2020
8c3e284
Wrap these exceptions up
zadjii-msft Mar 30, 2020
ff021e0
This wasn't supposed to be on this branch
zadjii-msft Mar 30, 2020
10e7baa
Update src/cascadia/TerminalControl/TermControl.cpp
zadjii-msft Apr 1, 2020
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
3 changes: 3 additions & 0 deletions .github/actions/spell-check/whitelist/whitelist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,7 @@ IIo
IList
ime
Imm
IMouse
Impl
implementingtextandtextrange
inbox
Expand Down Expand Up @@ -2423,6 +2424,7 @@ TOPDOWNDIB
TOPLEFT
TOPRIGHT
tosign
touchpad
tounicodeex
towlower
towupper
Expand All @@ -2434,6 +2436,7 @@ tracelogging
traceloggingprovider
trackbar
TRACKCOMPOSITION
trackpad
trackpads
transcoder
transitioning
Expand Down
5 changes: 5 additions & 0 deletions src/cascadia/TerminalApp/lib/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#define WIN32_LEAN_AND_MEAN

// Manually include til after we include Windows.Foundation to give it winrt superpowers
#define BLOCK_TIL
#include <LibraryIncludes.h>
// This is inexplicable, but for whatever reason, cppwinrt conflicts with the
// SDK definition of this function, so the only fix is to undef it.
Expand Down Expand Up @@ -63,3 +65,6 @@ TRACELOGGING_DECLARE_PROVIDER(g_hTerminalAppProvider);
#include <winrt/Microsoft.Terminal.TerminalConnection.h>

#include <CLI11/CLI11.hpp>

// Manually include til after we include Windows.Foundation to give it winrt superpowers
#include "til.h"
7 changes: 5 additions & 2 deletions src/cascadia/TerminalConnection/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@

#define BLOCK_GSL

// Manually include til after we include Windows.Foundation to give it winrt superpowers
#define BLOCK_TIL
#include <LibraryIncludes.h>

// Must be included before any WinRT headers.
#include <unknwn.h>

#include <winrt/Windows.Foundation.h>
#include <wil/cppwinrt.h>

#include "winrt/Windows.Foundation.h"
#include "winrt/Windows.Security.Credentials.h"
#include "winrt/Windows.Foundation.Collections.h"
#include <Windows.h>

#include <TraceLoggingProvider.h>
TRACELOGGING_DECLARE_PROVIDER(g_hTerminalConnectionProvider);
#include <telemetry/ProjectTelemetry.h>

#include "til.h"
16 changes: 16 additions & 0 deletions src/cascadia/TerminalControl/IMouseWheelListener.idl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

namespace Microsoft.Terminal.TerminalControl
{

// This interface is a hack for GH#979. Controls should implement this
// interface to be able to be notified of mousewheel events even on devices
// who's trackpads won't scroll inactive windows.
Comment on lines +7 to +9
Copy link
Member

Choose a reason for hiding this comment

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

Should we file a CodeHealth issue to replace this when the XAML Islands bug gets fixed?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, I just filed microsoft/microsoft-ui-xaml#2172 upstream to track them, I'll add another on us.


[uuid("65b8b8c5-988f-43ff-aba9-e89368da1598")]
interface IMouseWheelListener
{
Boolean OnMouseWheel(Windows.Foundation.Point coord, Int32 delta);
}
zadjii-msft marked this conversation as resolved.
Show resolved Hide resolved
}
79 changes: 64 additions & 15 deletions src/cascadia/TerminalControl/TermControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,8 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
// - Event handler for the PointerWheelChanged event. This is raised in
// response to mouse wheel changes. Depending upon what modifier keys are
// pressed, different actions will take place.
// - Primarily just takes the data from the PointerRoutedEventArgs and uses
// it to call _DoMouseWheel, see _DoMouseWheel for more details.
// Arguments:
// - args: the event args containing information about t`he mouse wheel event.
void TermControl::_MouseWheelHandler(Windows::Foundation::IInspectable const& /*sender*/,
Expand All @@ -1180,22 +1182,48 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
}

const auto point = args.GetCurrentPoint(*this);

if (_CanSendVTMouseInput())
auto result = _DoMouseWheel(point.Position(),
ControlKeyStates{ args.KeyModifiers() },
point.Properties().MouseWheelDelta(),
point.Properties().IsLeftButtonPressed());
if (result)
{
_TrySendMouseEvent(point);
args.Handled(true);
return;
}
}

const auto delta = point.Properties().MouseWheelDelta();
// Method Description:
// - Actually handle a scrolling event, wether from a mouse wheel or a
zadjii-msft marked this conversation as resolved.
Show resolved Hide resolved
// touchpad scroll. Depending upon what modifier keys are pressed,
// different actions will take place.
// * Attempts to first dispatch the mouse scroll as a VT event
// * If Ctrl+Shift are pressed, then attempts to change our opacity
// * If just Ctrl is pressed, we'll attempt to "zoom" by changing our font size
// * Otherwise, just scrolls the content of the viewport
// Arguments:
// - point: the location of the mouse during this event
// - modifiers: The modifiers pressed during this event, in the form of a VirtualKeyModifiers
// - delta: the mouse wheel delta that triggered this event.
bool TermControl::_DoMouseWheel(const Windows::Foundation::Point point,
const ControlKeyStates modifiers,
const int32_t delta,
const bool isLeftButtonPressed)
{
if (_CanSendVTMouseInput())
{
// Most mouse event handlers call
// _TrySendMouseEvent(point);
// here with a PointerPoint. However, as of #979, we don't have a
// PointerPoint to work with. So, we're just going to do a
// mousewheel event manually
return _terminal->SendMouseEvent(_GetTerminalPosition(point),
WM_MOUSEWHEEL,
_GetPressedModifierKeys(),
::base::saturated_cast<short>(delta));
}

// Get the state of the Ctrl & Shift keys
// static_cast to a uint32_t because we can't use the WI_IsFlagSet macro
// directly with a VirtualKeyModifiers
const auto modifiers = static_cast<uint32_t>(args.KeyModifiers());
const auto ctrlPressed = WI_IsFlagSet(modifiers, static_cast<uint32_t>(VirtualKeyModifiers::Control));
const auto shiftPressed = WI_IsFlagSet(modifiers, static_cast<uint32_t>(VirtualKeyModifiers::Shift));
const auto ctrlPressed = modifiers.IsCtrlPressed();
const auto shiftPressed = modifiers.IsShiftPressed();

if (ctrlPressed && shiftPressed)
{
Expand All @@ -1207,8 +1235,25 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
}
else
{
_MouseScrollHandler(delta, point);
_MouseScrollHandler(delta, point, isLeftButtonPressed);
}
return false;
}

// Method Description:
// - This is part of the solution to GH#979
// - Manually handle a scrolling event. This is used to help support
// scrolling on devices where the touchpad doesn't correctly handle
// scrolling inactive windows.
// Arguments:
// - location: the location of the mouse during this event. This location is
// relative to the origin of the control
// - delta: the mouse wheel delta that triggered this event.
bool TermControl::OnMouseWheel(const Windows::Foundation::Point location,
const int32_t delta)
{
const auto modifiers = _GetPressedModifierKeys();
return _DoMouseWheel(location, modifiers, delta, false);
}

// Method Description:
Expand Down Expand Up @@ -1281,7 +1326,11 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
// - Scroll the visible viewport in response to a mouse wheel event.
// Arguments:
// - mouseDelta: the mouse wheel delta that triggered this event.
void TermControl::_MouseScrollHandler(const double mouseDelta, Windows::UI::Input::PointerPoint const& pointerPoint)
// - point: the location of the mouse during this event
// - isLeftButtonPressed: true iff the left mouse button was pressed during this event.
void TermControl::_MouseScrollHandler(const double mouseDelta,
const Windows::Foundation::Point point,
const bool isLeftButtonPressed)
{
const auto currentOffset = ScrollBar().Value();

Expand All @@ -1298,11 +1347,11 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
// for us.
ScrollBar().Value(newValue);

if (_terminal->IsSelectionActive() && pointerPoint.Properties().IsLeftButtonPressed())
if (_terminal->IsSelectionActive() && isLeftButtonPressed)
{
// If user is mouse selecting and scrolls, they then point at new character.
// Make sure selection reflects that immediately.
_SetEndSelectionPointAtCursor(pointerPoint.Position());
_SetEndSelectionPointAtCursor(point);
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/cascadia/TerminalControl/TermControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation

bool OnF7Pressed();

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

~TermControl();

Windows::UI::Xaml::Automation::Peers::AutomationPeer OnCreateAutomationPeer();
Expand Down Expand Up @@ -197,9 +199,10 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
void _TerminalTitleChanged(const std::wstring_view& wstr);
winrt::fire_and_forget _TerminalScrollPositionChanged(const int viewTop, const int viewHeight, const int bufferSize);

void _MouseScrollHandler(const double delta, Windows::UI::Input::PointerPoint const& pointerPoint);
void _MouseScrollHandler(const double mouseDelta, const Windows::Foundation::Point point, const bool isLeftButtonPressed);
void _MouseZoomHandler(const double delta);
void _MouseTransparencyHandler(const double delta);
bool _DoMouseWheel(const Windows::Foundation::Point point, const ::Microsoft::Terminal::Core::ControlKeyStates modifiers, const int32_t delta, const bool isLeftButtonPressed);

bool _CapturePointer(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs const& e);
bool _ReleasePointerCapture(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs const& e);
Expand Down
4 changes: 3 additions & 1 deletion src/cascadia/TerminalControl/TermControl.idl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import "IMouseWheelListener.idl";

namespace Microsoft.Terminal.TerminalControl
{
delegate void TitleChangedEventArgs(String newTitle);
Expand Down Expand Up @@ -30,7 +32,7 @@ namespace Microsoft.Terminal.TerminalControl
void HandleClipboardData(String data);
}

[default_interface] runtimeclass TermControl : Windows.UI.Xaml.Controls.UserControl, IF7Listener
[default_interface] runtimeclass TermControl : Windows.UI.Xaml.Controls.UserControl, IF7Listener, IMouseWheelListener
{
TermControl();
TermControl(Microsoft.Terminal.Settings.IControlSettings settings, Microsoft.Terminal.TerminalConnection.ITerminalConnection connection);
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalControl/TerminalControl.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<DependentUpon>TermControl.xaml</DependentUpon>
</Midl>
<Midl Include="TermControlAutomationPeer.idl" />
<Midl Include="IMouseWheelListener.idl" />
<Midl Include="TSFInputControl.idl">
<DependentUpon>TSFInputControl.xaml</DependentUpon>
</Midl>
Expand Down
5 changes: 5 additions & 0 deletions src/cascadia/TerminalControl/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#define WIN32_LEAN_AND_MEAN

// Manually include til after we include Windows.Foundation to give it winrt superpowers
#define BLOCK_TIL
#include <LibraryIncludes.h>
// This is inexplicable, but for whatever reason, cppwinrt conflicts with the
// SDK definition of this function, so the only fix is to undef it.
Expand All @@ -23,6 +25,7 @@
#include <unknwn.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.system.h>
#include <winrt/Windows.Graphics.Display.h>
#include <winrt/windows.ui.core.h>
#include <winrt/Windows.ui.input.h>
Expand All @@ -46,3 +49,5 @@
#include <TraceLoggingProvider.h>
TRACELOGGING_DECLARE_PROVIDER(g_hTerminalControlProvider);
#include <telemetry/ProjectTelemetry.h>

#include "til.h"
21 changes: 21 additions & 0 deletions src/cascadia/TerminalCore/ControlKeyStates.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,27 @@ class Microsoft::Terminal::Core::ControlKeyStates
return *this;
}

#ifdef WINRT_Windows_System_H
ControlKeyStates(const winrt::Windows::System::VirtualKeyModifiers& modifiers) noexcept :
_value{ 0 }
{
// static_cast to a uint32_t because we can't use the WI_IsFlagSet
// macro directly with a VirtualKeyModifiers
const auto m = static_cast<uint32_t>(modifiers);
_value |= WI_IsFlagSet(m, static_cast<uint32_t>(winrt::Windows::System::VirtualKeyModifiers::Shift)) ?
SHIFT_PRESSED :
0;

// Since we can't differentiate between the left & right versions of Ctrl & Alt in a VirtualKeyModifiers
_value |= WI_IsFlagSet(m, static_cast<uint32_t>(winrt::Windows::System::VirtualKeyModifiers::Menu)) ?
LEFT_ALT_PRESSED :
0;
_value |= WI_IsFlagSet(m, static_cast<uint32_t>(winrt::Windows::System::VirtualKeyModifiers::Control)) ?
LEFT_CTRL_PRESSED :
0;
}
#endif

constexpr DWORD Value() const noexcept
{
return _value;
Expand Down
47 changes: 46 additions & 1 deletion src/cascadia/WindowsTerminal/AppHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ AppHost::AppHost() noexcept :
_logic,
std::placeholders::_1,
std::placeholders::_2));

_window->MouseScrolled({ this, &AppHost::_WindowMouseWheeled });
_window->MakeWindow();
}

Expand Down Expand Up @@ -338,3 +338,48 @@ void AppHost::_ToggleFullscreen(const winrt::Windows::Foundation::IInspectable&,
{
_window->ToggleFullscreen();
}

// Method Description:
// - Called when the IslandWindow has received a WM_MOUSEWHEEL message. This can
// happen on some laptops, where their trackpads won't scroll inactive windows
// _ever_.
// - We're going to take that message and manually plumb it through to our
// TermControl's, or anything else that implements IMouseWheelListener.
// - See GH#979 for more details.
// Arguments:
// - coord: The Window-relative, logical coordinates location of the mouse during this event.
// - delta: the wheel delta that triggered this event.
// Return Value:
// - <none>
void AppHost::_WindowMouseWheeled(const til::point coord, const int32_t delta)
{
if (_logic)
{
// Find all the elements that are underneath the mouse
auto elems = winrt::Windows::UI::Xaml::Media::VisualTreeHelper::FindElementsInHostCoordinates(coord, _logic.GetRoot());
for (const auto& e : elems)
{
// If that element has implemented IMouseWheelListener, call OnMouseWheel on that element.
if (auto control{ e.try_as<winrt::Microsoft::Terminal::TerminalControl::IMouseWheelListener>() })
{
try
{
// Translate the event to the coordinate space of the control
// we're attempting to dispatch it to
const auto transform = e.TransformToVisual(nullptr);
const til::point controlOrigin{ til::math::flooring, transform.TransformPoint(til::point{ 0, 0 }) };

const til::point offsetPoint = coord - controlOrigin;

if (control.OnMouseWheel(offsetPoint, delta))
{
// If the element handled the mouse wheel event, don't
// continue to iterate over the remaining controls.
break;
}
}
CATCH_LOG();
}
}
}
}
1 change: 1 addition & 0 deletions src/cascadia/WindowsTerminal/AppHost.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ class AppHost
const winrt::Windows::UI::Xaml::ElementTheme& arg);
void _ToggleFullscreen(const winrt::Windows::Foundation::IInspectable& sender,
const winrt::TerminalApp::ToggleFullscreenEventArgs& arg);
void _WindowMouseWheeled(const til::point coord, const int32_t delta);
};
Loading