-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScrollView.cpp
More file actions
690 lines (615 loc) · 23.2 KB
/
Copy pathScrollView.cpp
File metadata and controls
690 lines (615 loc) · 23.2 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
#include "ScrollView.h"
#include "Backplate.h"
#include "Util.h"
#include <algorithm>
#include <float.h>
#include <cmath>
namespace FD2D
{
bool ScrollView::IsPointInViewport(int x, int y) const
{
return Util::RectContainsPoint(LayoutRect(), POINT { x, y });
}
ScrollView::ScrollView()
: Wnd()
{
}
ScrollView::ScrollView(const std::wstring& name)
: Wnd(name)
{
}
void ScrollView::SetHorizontalScrollEnabled(bool enabled)
{
m_enableHScroll = enabled;
if (!m_enableHScroll)
{
m_scrollX = 0.0f;
}
ClampScroll();
Invalidate();
}
void ScrollView::SetVerticalScrollEnabled(bool enabled)
{
m_enableVScroll = enabled;
if (!m_enableVScroll)
{
m_scrollY = 0.0f;
}
ClampScroll();
Invalidate();
}
void ScrollView::SetScrollBarsVisible(bool visible)
{
m_showScrollBars = visible;
Invalidate();
}
namespace
{
constexpr float kBarThick = 9.0f; // scrollbar thickness
constexpr float kBarPad = 2.0f; // inset from the viewport edges
constexpr float kBarMinThumb = 28.0f;
}
bool ScrollView::HScrollBarRects(D2D1_RECT_F& track, D2D1_RECT_F& thumb) const
{
if (!m_showScrollBars || !m_enableHScroll)
return false;
const float maxScrollX = (std::max)(0.0f, m_contentSize.w - m_viewportSize.w);
if (maxScrollX <= 0.5f)
return false;
const bool vBar = m_enableVScroll && (m_contentSize.h - m_viewportSize.h) > 0.5f;
const D2D1_RECT_F vp = LayoutRect();
const float rightInset = vBar ? (kBarThick + kBarPad) : 0.0f;
track = D2D1::RectF(vp.left + kBarPad, vp.bottom - kBarThick - kBarPad,
vp.right - kBarPad - rightInset, vp.bottom - kBarPad);
const float trackW = track.right - track.left;
if (trackW <= kBarMinThumb)
return false;
const float thumbW = (std::max)(kBarMinThumb, trackW * m_viewportSize.w / m_contentSize.w);
const float thumbX = track.left + (trackW - thumbW) * (m_scrollX / maxScrollX);
thumb = D2D1::RectF(thumbX, track.top, thumbX + thumbW, track.bottom);
return true;
}
bool ScrollView::VScrollBarRects(D2D1_RECT_F& track, D2D1_RECT_F& thumb) const
{
if (!m_showScrollBars || !m_enableVScroll)
return false;
const float maxScrollY = (std::max)(0.0f, m_contentSize.h - m_viewportSize.h);
if (maxScrollY <= 0.5f)
return false;
const bool hBar = m_enableHScroll && (m_contentSize.w - m_viewportSize.w) > 0.5f;
const D2D1_RECT_F vp = LayoutRect();
const float bottomInset = hBar ? (kBarThick + kBarPad) : 0.0f;
track = D2D1::RectF(vp.right - kBarThick - kBarPad, vp.top + kBarPad,
vp.right - kBarPad, vp.bottom - kBarPad - bottomInset);
const float trackH = track.bottom - track.top;
if (trackH <= kBarMinThumb)
return false;
const float thumbH = (std::max)(kBarMinThumb, trackH * m_viewportSize.h / m_contentSize.h);
const float thumbY = track.top + (trackH - thumbH) * (m_scrollY / maxScrollY);
thumb = D2D1::RectF(track.left, thumbY, track.right, thumbY + thumbH);
return true;
}
void ScrollView::SetContent(const std::shared_ptr<Wnd>& content)
{
m_content = content;
if (m_content && !m_content->Name().empty())
{
if (Children().find(m_content->Name()) == Children().end())
{
AddChild(m_content);
}
}
Invalidate();
}
void ScrollView::SetScrollY(float y)
{
if (!m_enableVScroll)
{
m_scrollY = 0.0f;
m_targetScrollY = 0.0f;
return;
}
m_scrollY = (std::max)(0.0f, y);
m_targetScrollY = m_scrollY;
ClampScroll();
ClampTargetScroll();
Invalidate();
}
void ScrollView::SetScrollX(float x)
{
if (!m_enableHScroll)
{
m_scrollX = 0.0f;
m_targetScrollX = 0.0f;
return;
}
m_scrollX = (std::max)(0.0f, x);
m_targetScrollX = m_scrollX;
ClampScroll();
ClampTargetScroll();
Invalidate();
}
void ScrollView::SetScrollStep(float step)
{
m_scrollStep = (std::max)(1.0f, step);
}
void ScrollView::SetSmoothTimeMs(unsigned int timeMs)
{
m_smoothTimeMs = (std::max)(1U, timeMs);
}
void ScrollView::EnsureCentered(const D2D1_RECT_F& rect, bool Immediate)
{
const D2D1_RECT_F viewportOuter = LayoutRect();
float newScrollX = m_scrollX;
float newScrollY = m_scrollY;
if (m_enableHScroll)
{
const float viewportW = (std::max)(0.0f, m_viewportSize.w);
const float viewportCenter = (viewportOuter.left + viewportOuter.right) * 0.5f;
const float rectCenter = (rect.left + rect.right) * 0.5f;
const float maxScrollX = (std::max)(0.0f, m_contentSize.w - m_viewportSize.w);
// Content is arranged into the padded child area.
const float contentStart = viewportOuter.left + m_padding;
const float contentEnd = contentStart + m_contentSize.w;
// Items near the edges should NOT be centered: snap to start/end zones.
const float edgeZone = 0.5f * viewportW;
if (rect.left <= (contentStart + edgeZone))
{
newScrollX = 0.0f;
}
else if (rect.right >= (contentEnd - edgeZone))
{
newScrollX = maxScrollX;
}
else
{
newScrollX = rectCenter - viewportCenter;
newScrollX = (std::max)(0.0f, (std::min)(maxScrollX, newScrollX));
}
}
if (m_enableVScroll)
{
const float viewportCenter = (viewportOuter.top + viewportOuter.bottom) * 0.5f;
const float rectCenter = (rect.top + rect.bottom) * 0.5f;
const float maxScrollY = (std::max)(0.0f, m_contentSize.h - m_viewportSize.h);
newScrollY = rectCenter - viewportCenter;
newScrollY = (std::max)(0.0f, (std::min)(maxScrollY, newScrollY));
}
const float eps = 0.5f;
if (m_enableHScroll && std::fabs(newScrollX - m_scrollX) > eps)
{
if (Immediate)
{
SetScrollX(newScrollX);
}
else
{
SetTargetScrollX(newScrollX);
}
}
if (m_enableVScroll && std::fabs(newScrollY - m_scrollY) > eps)
{
if (Immediate)
{
SetScrollY(newScrollY);
}
else
{
SetTargetScrollY(newScrollY);
}
}
}
void ScrollView::SetPropagateMinSize(bool propagate)
{
m_propagateMinSize = propagate;
Invalidate();
}
Size ScrollView::Measure(Size available)
{
// ScrollView itself wants to take whatever space the parent gives.
m_desired = available;
// Measure content with the same available as a hint (we'll do "infinite" measure in Arrange).
if (m_content)
{
(void)m_content->Measure(available);
}
return m_desired;
}
Size ScrollView::MinSize() const
{
if (!m_propagateMinSize)
{
// Block upward constraint propagation.
return { 0.0f, 0.0f };
}
// Propagate content min size (plus padding/margin like default Wnd does).
if (!m_content)
{
return { 0.0f, 0.0f };
}
Size ms = m_content->MinSize();
ms.w += 2.0f * m_margin + 2.0f * m_padding;
ms.h += 2.0f * m_margin + 2.0f * m_padding;
return ms;
}
void ScrollView::Arrange(Rect finalRect)
{
Rect inset = Inset(finalRect, m_margin);
m_bounds = inset;
m_layoutRect = ToD2D(inset);
Rect childArea = Inset(inset, m_padding);
m_viewportSize = { childArea.w, childArea.h };
if (m_content)
{
// Measure content with "infinite only on scrollable axes" to learn intrinsic size without exploding
// desired size for controls that scale to available space.
Size probeAvailable
{
m_enableHScroll ? FLT_MAX : childArea.w,
m_enableVScroll ? FLT_MAX : childArea.h
};
Size desired = m_content->Measure(probeAvailable);
const float arrangedW = m_enableHScroll ? (std::max)(childArea.w, desired.w) : childArea.w;
const float arrangedH = m_enableVScroll ? (std::max)(childArea.h, desired.h) : childArea.h;
m_contentSize = { arrangedW, arrangedH };
// Arrange content within the viewport (scrolling will translate during render).
Rect contentRect { childArea.x, childArea.y, arrangedW, arrangedH };
m_content->Arrange(contentRect);
}
ClampScroll();
ClampTargetScroll();
}
void ScrollView::ClampScroll()
{
const float maxScrollX = m_enableHScroll ? (std::max)(0.0f, m_contentSize.w - m_viewportSize.w) : 0.0f;
const float maxScrollY = m_enableVScroll ? (std::max)(0.0f, m_contentSize.h - m_viewportSize.h) : 0.0f;
m_scrollX = (std::max)(0.0f, (std::min)(maxScrollX, m_scrollX));
m_scrollY = (std::max)(0.0f, (std::min)(maxScrollY, m_scrollY));
if (!m_enableHScroll)
{
m_scrollX = 0.0f;
}
if (!m_enableVScroll)
{
m_scrollY = 0.0f;
}
}
void ScrollView::ClampTargetScroll()
{
const float maxScrollX = m_enableHScroll ? (std::max)(0.0f, m_contentSize.w - m_viewportSize.w) : 0.0f;
const float maxScrollY = m_enableVScroll ? (std::max)(0.0f, m_contentSize.h - m_viewportSize.h) : 0.0f;
m_targetScrollX = (std::max)(0.0f, (std::min)(maxScrollX, m_targetScrollX));
m_targetScrollY = (std::max)(0.0f, (std::min)(maxScrollY, m_targetScrollY));
if (!m_enableHScroll)
{
m_targetScrollX = 0.0f;
}
if (!m_enableVScroll)
{
m_targetScrollY = 0.0f;
}
}
void ScrollView::SetTargetScrollX(float x)
{
if (!m_enableHScroll)
{
m_targetScrollX = 0.0f;
return;
}
m_targetScrollX = (std::max)(0.0f, x);
ClampTargetScroll();
if (BackplateRef() != nullptr)
{
BackplateRef()->RequestAnimationFrame();
}
Invalidate();
}
void ScrollView::SetTargetScrollY(float y)
{
if (!m_enableVScroll)
{
m_targetScrollY = 0.0f;
return;
}
m_targetScrollY = (std::max)(0.0f, y);
ClampTargetScroll();
if (BackplateRef() != nullptr)
{
BackplateRef()->RequestAnimationFrame();
}
Invalidate();
}
void ScrollView::AdvanceSmoothScroll(unsigned long long nowMs)
{
const float dx = m_targetScrollX - m_scrollX;
const float dy = m_targetScrollY - m_scrollY;
const float eps = 0.25f;
if (std::fabs(dx) < eps && std::fabs(dy) < eps)
{
m_scrollX = m_targetScrollX;
m_scrollY = m_targetScrollY;
m_lastSmoothAnimMs = nowMs;
return;
}
if (m_lastSmoothAnimMs == 0)
{
m_lastSmoothAnimMs = nowMs;
}
const unsigned long long dt = nowMs - m_lastSmoothAnimMs;
m_lastSmoothAnimMs = nowMs;
const float tauMs = (m_smoothTimeMs > 0) ? static_cast<float>(m_smoothTimeMs) : 90.0f;
const float a = 1.0f - std::exp(-static_cast<float>(dt) / tauMs);
m_scrollX += dx * a;
m_scrollY += dy * a;
ClampScroll();
if (BackplateRef() != nullptr)
{
BackplateRef()->RequestAnimationFrame();
}
}
void ScrollView::OnRender(ID2D1RenderTarget* target)
{
if (!target)
{
return;
}
AdvanceSmoothScroll(Util::NowMs());
// Clip to viewport + apply translation
const D2D1_RECT_F clip = LayoutRect();
// Debug: Check if clip rect is valid
if (Name() == L"thumbScroll")
{
}
target->PushAxisAlignedClip(clip, D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);
D2D1_MATRIX_3X2_F oldTransform {};
target->GetTransform(&oldTransform);
const D2D1_MATRIX_3X2_F scrollTransform = D2D1::Matrix3x2F::Translation(-m_scrollX, -m_scrollY);
target->SetTransform(oldTransform * scrollTransform);
if (m_content)
{
m_content->OnRender(target);
}
else
{
// fallback
Wnd::OnRender(target);
}
target->SetTransform(oldTransform);
target->PopAxisAlignedClip();
// Scrollbars are drawn in viewport space (not scrolled), on top of the
// content, for whichever enabled axis actually overflows.
if (m_showScrollBars)
{
Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> brush;
auto drawBar = [&](const D2D1_RECT_F& tr, const D2D1_RECT_F& th, bool active)
{
const float r = 0.5f * (std::min)(th.right - th.left, th.bottom - th.top);
if (SUCCEEDED(target->CreateSolidColorBrush(D2D1::ColorF(1.0f, 1.0f, 1.0f, 0.06f), &brush)))
target->FillRoundedRectangle(D2D1::RoundedRect(tr, r, r), brush.Get());
const D2D1_COLOR_F c = active ? D2D1::ColorF(0.56f, 0.61f, 0.70f, 0.95f)
: D2D1::ColorF(0.42f, 0.45f, 0.52f, 0.85f);
if (SUCCEEDED(target->CreateSolidColorBrush(c, &brush)))
target->FillRoundedRectangle(D2D1::RoundedRect(th, r, r), brush.Get());
};
D2D1_RECT_F track {}, thumb {};
if (HScrollBarRects(track, thumb))
drawBar(track, thumb, m_barDragAxis == 0 || m_barHover);
if (VScrollBarRects(track, thumb))
drawBar(track, thumb, m_barDragAxis == 1);
}
}
void ScrollView::RenderChildOverlays(ID2D1RenderTarget* target, OverlayLayer layer)
{
if (target == nullptr)
{
return;
}
D2D1_MATRIX_3X2_F previous {};
target->GetTransform(&previous);
target->SetTransform(
previous *
D2D1::Matrix3x2F::Translation(-m_scrollX, -m_scrollY));
Wnd::RenderChildOverlays(target, layer);
target->SetTransform(previous);
}
bool ScrollView::RouteChildOverlayInput(const InputEvent& event, OverlayLayer layer)
{
InputEvent translated = event;
if (translated.hasPoint)
{
translated.point.x = static_cast<int>(
std::lround(
static_cast<double>(translated.point.x) +
static_cast<double>(m_scrollX)));
translated.point.y = static_cast<int>(
std::lround(
static_cast<double>(translated.point.y) +
static_cast<double>(m_scrollY)));
}
return Wnd::RouteChildOverlayInput(translated, layer);
}
bool ScrollView::OnInputEvent(const InputEvent& event)
{
// Draggable scrollbars (opt-in). Handled before the content dispatch so a
// grab on a thumb never falls through to the content beneath it. All of
// this is skipped entirely unless SetScrollBarsVisible(true) was called.
if (m_showScrollBars)
{
if (event.type == InputEventType::MouseDown && event.button == MouseButton::Left && event.hasPoint)
{
D2D1_RECT_F track {}, thumb {};
for (int axis = 0; axis < 2; ++axis)
{
const bool ok = (axis == 0) ? HScrollBarRects(track, thumb) : VScrollBarRects(track, thumb);
if (!ok)
continue;
if (Util::RectContainsPoint(thumb, event.point))
{
m_barDragAxis = axis;
m_barDragMouse = (axis == 0) ? static_cast<float>(event.point.x) : static_cast<float>(event.point.y);
m_barDragScroll = (axis == 0) ? m_scrollX : m_scrollY;
if (BackplateRef() != nullptr)
SetCapture(BackplateRef()->Window());
Invalidate();
return true;
}
if (Util::RectContainsPoint(track, event.point))
{
const float page = 0.9f * ((axis == 0) ? m_viewportSize.w : m_viewportSize.h);
const bool before = (axis == 0) ? (event.point.x < thumb.left) : (event.point.y < thumb.top);
const float d = before ? -page : page;
if (axis == 0) SetScrollX(m_scrollX + d); else SetScrollY(m_scrollY + d);
return true;
}
}
}
else if (event.type == InputEventType::MouseMove && m_barDragAxis >= 0 && event.hasPoint)
{
D2D1_RECT_F track {}, thumb {};
const bool ok = (m_barDragAxis == 0) ? HScrollBarRects(track, thumb) : VScrollBarRects(track, thumb);
if (ok)
{
if (m_barDragAxis == 0)
{
const float span = (track.right - track.left) - (thumb.right - thumb.left);
const float maxScrollX = (std::max)(0.0f, m_contentSize.w - m_viewportSize.w);
if (span > 0.5f)
SetScrollX(m_barDragScroll + (static_cast<float>(event.point.x) - m_barDragMouse) / span * maxScrollX);
}
else
{
const float span = (track.bottom - track.top) - (thumb.bottom - thumb.top);
const float maxScrollY = (std::max)(0.0f, m_contentSize.h - m_viewportSize.h);
if (span > 0.5f)
SetScrollY(m_barDragScroll + (static_cast<float>(event.point.y) - m_barDragMouse) / span * maxScrollY);
}
}
return true;
}
else if (event.type == InputEventType::MouseUp && m_barDragAxis >= 0)
{
m_barDragAxis = -1;
if (BackplateRef() != nullptr)
ReleaseCapture();
Invalidate();
return true;
}
else if (event.type == InputEventType::MouseMove && m_barDragAxis < 0 && event.hasPoint)
{
D2D1_RECT_F track {}, thumb {};
const bool over = HScrollBarRects(track, thumb) && Util::RectContainsPoint(thumb, event.point);
if (over != m_barHover)
{
m_barHover = over;
Invalidate();
}
}
}
switch (event.type)
{
case InputEventType::MouseWheel:
{
if (!event.hasPoint)
{
break;
}
// Only scroll when the cursor is over this viewport.
const int x = event.point.x;
const int y = event.point.y;
const bool inViewport = IsPointInViewport(x, y);
// If cursor is over viewport, handle scrolling
if (inViewport)
{
const short delta = event.wheelDelta;
const float ticks = static_cast<float>(delta) / static_cast<float>(WHEEL_DELTA);
const bool shift = event.modifiers.shift;
const float step = -ticks * m_scrollStep;
if (m_enableHScroll && (!m_enableVScroll || shift))
{
SetTargetScrollX(m_targetScrollX + step);
return true;
}
else if (m_enableVScroll)
{
SetTargetScrollY(m_targetScrollY + step);
return true;
}
}
// If not handled (not in viewport or no scroll enabled), fall through to forward to content
break;
}
case InputEventType::MouseHWheel:
{
if (!event.hasPoint)
{
break;
}
const int x = event.point.x;
const int y = event.point.y;
if (!IsPointInViewport(x, y))
{
break;
}
const short delta = event.wheelDelta;
const float ticks = static_cast<float>(delta) / static_cast<float>(WHEEL_DELTA);
const float step = -ticks * m_scrollStep;
if (m_enableHScroll)
{
SetTargetScrollX(m_targetScrollX + step);
return true;
}
break;
}
default:
break;
}
// Forward mouse events to content using scrolled coordinates so hit-testing matches rendering.
if (m_content && Util::IsMouseInputEventType(event.type))
{
if (event.type == InputEventType::CaptureChanged)
{
m_forwardCapture = false;
return m_content->OnInputEvent(event);
}
// If we're not in an active drag/capture sequence, ignore events outside the viewport.
if (!m_forwardCapture)
{
if (!event.hasPoint)
{
return false;
}
const int x = event.point.x;
const int y = event.point.y;
if (!IsPointInViewport(x, y))
{
return false;
}
}
InputEvent translated = event;
// For client-coordinate mouse messages, translate by scroll offset.
if (translated.hasPoint &&
translated.type != InputEventType::MouseWheel &&
translated.type != InputEventType::MouseHWheel &&
translated.type != InputEventType::CaptureChanged)
{
translated.point.x = static_cast<int>(std::lround(static_cast<double>(translated.point.x) + static_cast<double>(m_scrollX)));
translated.point.y = static_cast<int>(std::lround(static_cast<double>(translated.point.y) + static_cast<double>(m_scrollY)));
}
const bool handled = m_content->OnInputEvent(translated);
// Track capture-like sequences so we keep forwarding move/up even if cursor exits the viewport.
if (event.type == InputEventType::MouseDown)
{
if (handled)
{
m_forwardCapture = true;
}
}
if (event.type == InputEventType::MouseUp)
{
m_forwardCapture = false;
}
return handled;
}
return Wnd::OnInputEvent(event);
}
}