-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Expand file tree
/
Copy pathscrolling.cpp
More file actions
333 lines (293 loc) · 10.3 KB
/
Copy pathscrolling.cpp
File metadata and controls
333 lines (293 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include "precomp.h"
#include "scrolling.hpp"
#include "selection.hpp"
#include "../interactivity/inc/ServiceLocator.hpp"
using Microsoft::Console::VirtualTerminal::StateMachine;
using namespace Microsoft::Console::Interactivity;
using namespace Microsoft::Console::Types;
til::CoordType Scrolling::s_ucWheelScrollLines = 0;
til::CoordType Scrolling::s_ucWheelScrollChars = 0;
void Scrolling::s_UpdateSystemMetrics()
{
s_ucWheelScrollLines = ::base::saturated_cast<decltype(s_ucWheelScrollLines)>(ServiceLocator::LocateSystemConfigurationProvider()->GetNumberOfWheelScrollLines());
s_ucWheelScrollChars = ::base::saturated_cast<decltype(s_ucWheelScrollChars)>(ServiceLocator::LocateSystemConfigurationProvider()->GetNumberOfWheelScrollCharacters());
}
bool Scrolling::s_IsInScrollMode()
{
const auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
return WI_IsFlagSet(gci.Flags, CONSOLE_SCROLLING);
}
void Scrolling::s_DoScroll()
{
auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
const auto pWindow = ServiceLocator::LocateConsoleWindow();
if (!s_IsInScrollMode())
{
// clear any selection we may have -- can't scroll and select at the same time
Selection::Instance().ClearSelection();
WI_SetFlag(gci.Flags, CONSOLE_SCROLLING);
if (pWindow != nullptr)
{
pWindow->UpdateWindowText();
}
}
}
void Scrolling::s_ClearScroll()
{
auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
const auto pWindow = ServiceLocator::LocateConsoleWindow();
WI_ClearFlag(gci.Flags, CONSOLE_SCROLLING);
if (pWindow != nullptr)
{
pWindow->UpdateWindowText();
}
}
void Scrolling::s_ScrollIfNecessary(const SCREEN_INFORMATION& ScreenInfo)
{
auto pWindow = ServiceLocator::LocateConsoleWindow();
FAIL_FAST_IF_NULL(pWindow);
const auto pSelection = &Selection::Instance();
if (pSelection->IsInSelectingState() && pSelection->IsMouseButtonDown())
{
til::point CursorPos;
if (!pWindow->GetCursorPosition(&CursorPos))
{
return;
}
til::rect ClientRect;
if (!pWindow->GetClientRectangle(&ClientRect))
{
return;
}
pWindow->MapRect(&ClientRect);
if (!(s_IsPointInRectangle(&ClientRect, CursorPos)))
{
pWindow->ConvertScreenToClient(&CursorPos);
til::point MousePosition;
MousePosition.x = CursorPos.x;
MousePosition.y = CursorPos.y;
auto coordFontSize = ScreenInfo.GetScreenFontSize();
MousePosition.x /= coordFontSize.width;
MousePosition.y /= coordFontSize.height;
MousePosition.x += ScreenInfo.GetViewport().Left();
MousePosition.y += ScreenInfo.GetViewport().Top();
pSelection->ExtendSelection(MousePosition);
}
}
}
void Scrolling::s_HandleMouseWheel(_In_ bool isMouseWheel,
_In_ bool isMouseHWheel,
_In_ short wheelDelta,
_In_ bool hasShift,
SCREEN_INFORMATION& ScreenInfo)
{
auto NewOrigin = ScreenInfo.GetViewport().Origin();
// s_ucWheelScrollLines == 0 means that it is turned off.
if (isMouseWheel && s_ucWheelScrollLines > 0)
{
// Rounding could cause this to be zero if gucWSL is bigger than 240 or so.
const auto ulActualDelta = std::max(WHEEL_DELTA / s_ucWheelScrollLines, 1);
// If we change direction we need to throw away any remainder we may have in the other direction.
if ((ScreenInfo.WheelDelta > 0) == (wheelDelta > 0))
{
ScreenInfo.WheelDelta += wheelDelta;
}
else
{
ScreenInfo.WheelDelta = wheelDelta;
}
if (abs(ScreenInfo.WheelDelta) >= ulActualDelta)
{
/*
* By default, SHIFT + WM_MOUSEWHEEL will scroll 1/2 the
* screen size. A ScrollScale of 1 indicates 1/2 the screen
* size. This value can be modified in the registry.
*/
til::CoordType delta = 1;
if (hasShift)
{
delta = std::max((ScreenInfo.GetViewport().Height() * ::base::saturated_cast<til::CoordType>(ScreenInfo.ScrollScale)) / 2, 1);
// Account for scroll direction changes by adjusting delta if there was a direction change.
delta *= (ScreenInfo.WheelDelta < 0 ? -1 : 1);
ScreenInfo.WheelDelta %= delta;
}
else
{
delta *= (ScreenInfo.WheelDelta / ulActualDelta);
ScreenInfo.WheelDelta %= ulActualDelta;
}
NewOrigin.y -= delta;
const auto coordBufferSize = ScreenInfo.GetBufferSize().Dimensions();
if (NewOrigin.y < 0)
{
NewOrigin.y = 0;
}
else if (NewOrigin.y + ScreenInfo.GetViewport().Height() > coordBufferSize.height)
{
NewOrigin.y = coordBufferSize.height - ScreenInfo.GetViewport().Height();
}
LOG_IF_FAILED(ScreenInfo.SetViewportOrigin(true, NewOrigin, false));
}
}
else if (isMouseHWheel && s_ucWheelScrollChars > 0)
{
const auto ulActualDelta = std::max(WHEEL_DELTA / s_ucWheelScrollChars, 1);
if ((ScreenInfo.HWheelDelta > 0) == (wheelDelta > 0))
{
ScreenInfo.HWheelDelta += wheelDelta;
}
else
{
ScreenInfo.HWheelDelta = wheelDelta;
}
if (abs(ScreenInfo.HWheelDelta) >= ulActualDelta)
{
til::CoordType delta = 1;
if (hasShift)
{
delta = std::max(ScreenInfo.GetViewport().RightInclusive(), 1);
}
delta *= (ScreenInfo.HWheelDelta / ulActualDelta);
ScreenInfo.HWheelDelta %= ulActualDelta;
NewOrigin.x += delta;
const auto coordBufferSize = ScreenInfo.GetBufferSize().Dimensions();
if (NewOrigin.x < 0)
{
NewOrigin.x = 0;
}
else if (NewOrigin.x + ScreenInfo.GetViewport().Width() > coordBufferSize.width)
{
NewOrigin.x = coordBufferSize.width - ScreenInfo.GetViewport().Width();
}
LOG_IF_FAILED(ScreenInfo.SetViewportOrigin(true, NewOrigin, false));
}
}
}
bool Scrolling::s_HandleKeyScrollingEvent(const INPUT_KEY_INFO* const pKeyInfo)
{
auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
auto pWindow = ServiceLocator::LocateConsoleWindow();
FAIL_FAST_IF_NULL(pWindow);
const auto VirtualKeyCode = pKeyInfo->GetVirtualKey();
const auto fIsCtrlPressed = pKeyInfo->IsCtrlPressed();
const auto fIsEditLineEmpty = !gci.HasPendingCookedRead() || gci.CookedReadData().IsEmpty();
// If escape, enter or ctrl-c, cancel scroll.
if (VirtualKeyCode == VK_ESCAPE ||
VirtualKeyCode == VK_RETURN ||
(VirtualKeyCode == 'C' && fIsCtrlPressed))
{
Scrolling::s_ClearScroll();
}
else
{
WORD ScrollCommand;
auto Horizontal = FALSE;
switch (VirtualKeyCode)
{
case VK_UP:
{
ScrollCommand = SB_LINEUP;
break;
}
case VK_DOWN:
{
ScrollCommand = SB_LINEDOWN;
break;
}
case VK_LEFT:
{
ScrollCommand = SB_LINEUP;
Horizontal = TRUE;
break;
}
case VK_RIGHT:
{
ScrollCommand = SB_LINEDOWN;
Horizontal = TRUE;
break;
}
case VK_NEXT:
{
ScrollCommand = SB_PAGEDOWN;
break;
}
case VK_PRIOR:
{
ScrollCommand = SB_PAGEUP;
break;
}
case VK_END:
{
if (fIsCtrlPressed)
{
if (fIsEditLineEmpty)
{
// Ctrl-End when edit line is empty will scroll to last line in the buffer.
gci.GetActiveOutputBuffer().MakeCurrentCursorVisible();
return true;
}
else
{
// If edit line is non-empty, we won't handle this so it can modify the edit line (trim characters to end of line from cursor pos).
return false;
}
}
else
{
ScrollCommand = SB_PAGEDOWN;
Horizontal = TRUE;
}
break;
}
case VK_HOME:
{
if (fIsCtrlPressed)
{
if (fIsEditLineEmpty)
{
// Ctrl-Home when edit line is empty will scroll to top of buffer.
ScrollCommand = SB_TOP;
}
else
{
// If edit line is non-empty, we won't handle this so it can modify the edit line (trim characters to beginning of line from cursor pos).
return false;
}
}
else
{
ScrollCommand = SB_PAGEUP;
Horizontal = TRUE;
}
break;
}
case VK_SHIFT:
case VK_CONTROL:
case VK_MENU:
{
return true;
}
default:
{
pWindow->SendNotifyBeep();
return true;
}
}
if (Horizontal)
{
pWindow->HorizontalScroll(ScrollCommand, 0);
}
else
{
pWindow->VerticalScroll(ScrollCommand, 0);
}
}
return true;
}
BOOL Scrolling::s_IsPointInRectangle(const til::rect* prc, const til::point pt)
{
return ((pt.x >= prc->left) && (pt.x < prc->right) &&
(pt.y >= prc->top) && (pt.y < prc->bottom));
}