-
Notifications
You must be signed in to change notification settings - Fork 703
/
Copy pathTabViewItem.cpp
596 lines (504 loc) · 19.9 KB
/
TabViewItem.cpp
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
#include "pch.h"
#include "common.h"
#include "TabView.h"
#include "TabViewItem.h"
#include "RuntimeProfiler.h"
#include "ResourceAccessor.h"
#include "XamlControlsResources.h"
#include "SharedHelpers.h"
static constexpr auto c_overlayCornerRadiusKey = L"OverlayCornerRadius"sv;
static constexpr int c_targetRectWidthIncrement = 2;
TabViewItem::TabViewItem()
{
__RP_Marker_ClassById(RuntimeProfiler::ProfId_TabViewItem);
SetDefaultStyleKey(this);
SetValue(s_TabViewTemplateSettingsProperty, winrt::make<TabViewItemTemplateSettings>());
Loaded({ this, &TabViewItem::OnLoaded });
SizeChanged({ this, &TabViewItem::OnSizeChanged });
RegisterPropertyChangedCallback(winrt::SelectorItem::IsSelectedProperty(), { this, &TabViewItem::OnIsSelectedPropertyChanged });
RegisterPropertyChangedCallback(winrt::Control::ForegroundProperty(), { this, &TabViewItem::OnForegroundPropertyChanged });
}
void TabViewItem::OnApplyTemplate()
{
__super::OnApplyTemplate();
winrt::IControlProtected controlProtected{ *this };
m_selectedBackgroundPathSizeChangedRevoker.revoke();
m_closeButtonClickRevoker.revoke();
m_tabDragStartingRevoker.revoke();
m_tabDragCompletedRevoker.revoke();
if (SharedHelpers::Is19H1OrHigher()) // UIElement.ActualOffset introduced in Win10 1903.
{
m_selectedBackgroundPath.set(GetTemplateChildT<winrt::Path>(L"SelectedBackgroundPath", controlProtected));
if (const auto selectedBackgroundPath = m_selectedBackgroundPath.get())
{
m_selectedBackgroundPathSizeChangedRevoker = selectedBackgroundPath.SizeChanged(winrt::auto_revoke,
{
[this](auto const&, auto const&)
{
UpdateSelectedBackgroundPathTranslateTransform();
}
});
}
}
m_headerContentPresenter.set(GetTemplateChildT<winrt::ContentPresenter>(L"ContentPresenter", controlProtected));
auto tabView = SharedHelpers::GetAncestorOfType<winrt::TabView>(winrt::VisualTreeHelper::GetParent(*this));
auto internalTabView = tabView
? winrt::get_self<TabView>(tabView)
: nullptr;
m_closeButton.set([this, controlProtected, internalTabView]() {
auto closeButton = GetTemplateChildT<winrt::Button>(L"CloseButton", controlProtected);
if (closeButton)
{
// Do localization for the close button automation name
if (winrt::AutomationProperties::GetName(closeButton).empty())
{
auto const closeButtonName = ResourceAccessor::GetLocalizedStringResource(SR_TabViewCloseButtonName);
winrt::AutomationProperties::SetName(closeButton, closeButtonName);
}
if (internalTabView)
{
// Setup the tooltip for the close button
auto tooltip = winrt::ToolTip();
tooltip.Content(box_value(internalTabView->GetTabCloseButtonTooltipText()));
winrt::ToolTipService::SetToolTip(closeButton, tooltip);
}
m_closeButtonClickRevoker = closeButton.Click(winrt::auto_revoke, { this, &TabViewItem::OnCloseButtonClick });
}
return closeButton;
}());
OnHeaderChanged();
OnIconSourceChanged();
if (tabView)
{
if (SharedHelpers::IsThemeShadowAvailable())
{
if (internalTabView)
{
winrt::ThemeShadow shadow;
if (!SharedHelpers::Is21H1OrHigher())
{
if (auto shadowReceiver = internalTabView->GetShadowReceiver())
{
shadow.Receivers().Append(shadowReceiver);
}
}
m_shadow = shadow;
double shadowDepth = unbox_value<double>(SharedHelpers::FindInApplicationResources(c_tabViewShadowDepthName, box_value(c_tabShadowDepth)));
const auto currentTranslation = Translation();
const auto translation = winrt::float3{ currentTranslation.x, currentTranslation.y, (float)shadowDepth };
Translation(translation);
UpdateShadow();
}
}
m_tabDragStartingRevoker = tabView.TabDragStarting(winrt::auto_revoke, { this, &TabViewItem::OnTabDragStarting });
m_tabDragCompletedRevoker = tabView.TabDragCompleted(winrt::auto_revoke, { this, &TabViewItem::OnTabDragCompleted });
}
UpdateCloseButton();
UpdateForeground();
UpdateWidthModeVisualState();
UpdateTabGeometry();
}
void TabViewItem::UpdateTabGeometry()
{
auto const templateSettings = winrt::get_self<TabViewItemTemplateSettings>(TabViewTemplateSettings());
auto const scaleFactor = SharedHelpers::Is19H1OrHigher() ?
static_cast<float>(XamlRoot().RasterizationScale()) :
static_cast<float>(winrt::DisplayInformation::GetForCurrentView().RawPixelsPerViewPixel());
auto const height = ActualHeight();
auto const popupRadius = unbox_value<winrt::CornerRadius>(ResourceAccessor::ResourceLookup(*this, box_value(c_overlayCornerRadiusKey)));
auto const leftCorner = popupRadius.TopLeft;
auto const rightCorner = popupRadius.TopRight;
// Assumes 4px curving-out corners, which are hardcoded in the markup
auto data = L"<Geometry xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>F1 M0,%f a 4,4 0 0 0 4,-4 L 4,%f a %f,%f 0 0 1 %f,-%f l %f,0 a %f,%f 0 0 1 %f,%f l 0,%f a 4,4 0 0 0 4,4 Z</Geometry>";
WCHAR strOut[1024];
StringCchPrintf(strOut, ARRAYSIZE(strOut), data,
height - 1.0,
leftCorner, leftCorner, leftCorner, leftCorner, leftCorner,
ActualWidth() - (leftCorner + rightCorner + 1.0f / scaleFactor),
rightCorner, rightCorner, rightCorner, rightCorner,
height - (5.0 + rightCorner));
const auto geometry = winrt::XamlReader::Load(strOut).try_as<winrt::Geometry>();
templateSettings->TabGeometry(geometry);
}
void TabViewItem::OnLoaded(const winrt::IInspectable& sender, const winrt::RoutedEventArgs& args)
{
if (const auto tabView = GetParentTabView())
{
const auto internalTabView = winrt::get_self<TabView>(tabView);
const auto index = internalTabView->IndexFromContainer(*this);
internalTabView->SetTabSeparatorOpacity(index);
}
}
void TabViewItem::OnSizeChanged(const winrt::IInspectable&, const winrt::SizeChangedEventArgs& args)
{
m_dispatcherHelper.RunAsync([strongThis = get_strong()]()
{
strongThis->UpdateTabGeometry();
});
}
void TabViewItem::OnIsSelectedPropertyChanged(const winrt::DependencyObject& sender, const winrt::DependencyProperty& args)
{
if (const auto peer = winrt::FrameworkElementAutomationPeer::CreatePeerForElement(*this))
{
peer.RaiseAutomationEvent(winrt::AutomationEvents::SelectionItemPatternOnElementSelected);
}
if (IsSelected())
{
SetValue(winrt::Canvas::ZIndexProperty(), box_value(20));
StartBringTabIntoView();
}
else
{
SetValue(winrt::Canvas::ZIndexProperty(), box_value(0));
}
UpdateShadow();
UpdateWidthModeVisualState();
UpdateCloseButton();
UpdateForeground();
}
void TabViewItem::OnForegroundPropertyChanged(const winrt::DependencyObject&, const winrt::DependencyProperty&)
{
UpdateForeground();
}
void TabViewItem::UpdateForeground()
{
// We only need to set the foreground state when the TabViewItem is in rest state and not selected.
if (!IsSelected() && !m_isPointerOver)
{
// If Foreground is set, then change icon and header foreground to match.
winrt::VisualStateManager::GoToState(
*this,
ReadLocalValue(winrt::Control::ForegroundProperty()) == winrt::DependencyProperty::UnsetValue() ? L"ForegroundNotSet" : L"ForegroundSet",
false /*useTransitions*/);
}
}
void TabViewItem::UpdateShadow()
{
if (SharedHelpers::IsThemeShadowAvailable() && !XamlControlsResources::IsUsingControlsResourcesVersion2())
{
if (IsSelected() && !m_isDragging )
{
Shadow(m_shadow.as<winrt::ThemeShadow>());
}
else
{
Shadow(nullptr);
}
}
}
void TabViewItem::UpdateSelectedBackgroundPathTranslateTransform()
{
MUX_ASSERT(SharedHelpers::Is19H1OrHigher());
if (const auto selectedBackgroundPath = m_selectedBackgroundPath.get())
{
const auto selectedBackgroundPathActualOffset = selectedBackgroundPath.ActualOffset();
const auto roundedSelectedBackgroundPathActualOffsetY = std::round(selectedBackgroundPathActualOffset.y);
if (roundedSelectedBackgroundPathActualOffsetY > selectedBackgroundPathActualOffset.y)
{
// Move the SelectedBackgroundPath element down by a fraction of a pixel to avoid a faint gap line
// between the selected TabViewItem and its content.
winrt::TranslateTransform translateTransform;
translateTransform.Y(roundedSelectedBackgroundPathActualOffsetY - selectedBackgroundPathActualOffset.y);
selectedBackgroundPath.RenderTransform(translateTransform);
}
else if (selectedBackgroundPath.RenderTransform())
{
// Reset any TranslateTransform that may have been set above.
selectedBackgroundPath.RenderTransform(nullptr);
}
}
}
void TabViewItem::OnTabDragStarting(const winrt::IInspectable& sender, const winrt::TabViewTabDragStartingEventArgs& args)
{
m_isDragging = true;
UpdateShadow();
}
void TabViewItem::OnTabDragCompleted(const winrt::IInspectable& sender, const winrt::TabViewTabDragCompletedEventArgs& args)
{
m_isDragging = false;
UpdateShadow();
UpdateForeground();
}
winrt::AutomationPeer TabViewItem::OnCreateAutomationPeer()
{
return winrt::make<TabViewItemAutomationPeer>(*this);
}
void TabViewItem::OnCloseButtonOverlayModeChanged(winrt::TabViewCloseButtonOverlayMode const& mode)
{
m_closeButtonOverlayMode = mode;
UpdateCloseButton();
}
winrt::TabView TabViewItem::GetParentTabView()
{
return m_parentTabView.get();
}
void TabViewItem::SetParentTabView(winrt::TabView const& tabView)
{
m_parentTabView = winrt::make_weak(tabView);
}
void TabViewItem::OnTabViewWidthModeChanged(winrt::TabViewWidthMode const& mode)
{
m_tabViewWidthMode = mode;
UpdateWidthModeVisualState();
}
void TabViewItem::UpdateCloseButton()
{
if (!IsClosable())
{
winrt::VisualStateManager::GoToState(*this, L"CloseButtonCollapsed", false);
}
else
{
switch (m_closeButtonOverlayMode)
{
case winrt::TabViewCloseButtonOverlayMode::OnPointerOver:
{
// If we only want to show the button on hover, we also show it when we are selected, otherwise hide it
if (IsSelected() || m_isPointerOver)
{
winrt::VisualStateManager::GoToState(*this, L"CloseButtonVisible", false);
}
else
{
winrt::VisualStateManager::GoToState(*this, L"CloseButtonCollapsed", false);
}
break;
}
default:
{
// Default, use "Auto"
winrt::VisualStateManager::GoToState(*this, L"CloseButtonVisible", false);
break;
}
}
}
}
void TabViewItem::UpdateWidthModeVisualState()
{
// Handling compact/non compact width mode
if (!IsSelected() && m_tabViewWidthMode == winrt::TabViewWidthMode::Compact)
{
winrt::VisualStateManager::GoToState(*this, L"Compact", false);
}
else
{
winrt::VisualStateManager::GoToState(*this, L"StandardWidth", false);
}
}
void TabViewItem::RequestClose()
{
if (auto tabView = SharedHelpers::GetAncestorOfType<winrt::TabView>(winrt::VisualTreeHelper::GetParent(*this)))
{
if (auto internalTabView = winrt::get_self<TabView>(tabView))
{
internalTabView->RequestCloseTab(*this, false);
}
}
}
void TabViewItem::RaiseRequestClose(TabViewTabCloseRequestedEventArgs const& args)
{
// This should only be called from TabView, to ensure that both this event and the TabView TabRequestedClose event are raised
m_closeRequestedEventSource(*this, args);
}
void TabViewItem::OnCloseButtonClick(const winrt::IInspectable&, const winrt::RoutedEventArgs&)
{
RequestClose();
}
void TabViewItem::OnIsClosablePropertyChanged(const winrt::DependencyPropertyChangedEventArgs&)
{
UpdateCloseButton();
}
void TabViewItem::OnHeaderPropertyChanged(const winrt::DependencyPropertyChangedEventArgs& args)
{
OnHeaderChanged();
}
void TabViewItem::OnHeaderChanged()
{
if (const auto headerContentPresenter = m_headerContentPresenter.get())
{
headerContentPresenter.Content(Header());
}
if (m_firstTimeSettingToolTip)
{
m_firstTimeSettingToolTip = false;
if (!winrt::ToolTipService::GetToolTip(*this))
{
// App author has not specified a tooltip; use our own
m_toolTip.set([this]() {
auto toolTip = winrt::ToolTip();
toolTip.Placement(winrt::Controls::Primitives::PlacementMode::Mouse);
winrt::ToolTipService::SetToolTip(*this, toolTip);
return toolTip;
}());
}
}
if (auto&& toolTip = m_toolTip.get())
{
// Update tooltip text to new header text
auto headerContent = Header();
auto potentialString = headerContent.try_as<winrt::IPropertyValue>();
// Only show tooltip if header is a non-empty string.
if (potentialString && potentialString.Type() == winrt::PropertyType::String && !potentialString.GetString().empty())
{
toolTip.Content(headerContent);
toolTip.IsEnabled(true);
}
else
{
toolTip.Content(nullptr);
toolTip.IsEnabled(false);
}
}
}
void TabViewItem::OnPointerPressed(winrt::PointerRoutedEventArgs const& args)
{
if (IsSelected() && args.Pointer().PointerDeviceType() == winrt::PointerDeviceType::Mouse)
{
auto pointerPoint = args.GetCurrentPoint(*this);
if (pointerPoint.Properties().IsLeftButtonPressed())
{
auto isCtrlDown = (winrt::Window::Current().CoreWindow().GetKeyState(winrt::VirtualKey::Control) & winrt::CoreVirtualKeyStates::Down) == winrt::CoreVirtualKeyStates::Down;
if (isCtrlDown)
{
// Return here so the base class will not pick it up, but let it remain unhandled so someone else could handle it.
return;
}
}
}
__super::OnPointerPressed(args);
if (args.GetCurrentPoint(nullptr).Properties().PointerUpdateKind() == winrt::PointerUpdateKind::MiddleButtonPressed)
{
if (CapturePointer(args.Pointer()))
{
m_hasPointerCapture = true;
m_isMiddlePointerButtonPressed = true;
}
}
}
void TabViewItem::OnPointerReleased(winrt::PointerRoutedEventArgs const& args)
{
__super::OnPointerReleased(args);
if (m_hasPointerCapture)
{
if (args.GetCurrentPoint(nullptr).Properties().PointerUpdateKind() == winrt::PointerUpdateKind::MiddleButtonReleased)
{
const bool wasPressed = m_isMiddlePointerButtonPressed;
m_isMiddlePointerButtonPressed = false;
ReleasePointerCapture(args.Pointer());
if (wasPressed)
{
if (IsClosable())
{
RequestClose();
}
}
}
}
}
void TabViewItem::HideLeftAdjacentTabSeparator()
{
if (const auto tabView = GetParentTabView())
{
const auto internalTabView = winrt::get_self<TabView>(tabView);
const auto index = internalTabView->IndexFromContainer(*this);
internalTabView->SetTabSeparatorOpacity(index - 1, 0);
}
}
void TabViewItem::RestoreLeftAdjacentTabSeparatorVisibility()
{
if (const auto tabView = GetParentTabView())
{
const auto internalTabView = winrt::get_self<TabView>(tabView);
const auto index = internalTabView->IndexFromContainer(*this);
internalTabView->SetTabSeparatorOpacity(index - 1);
}
}
void TabViewItem::OnPointerEntered(winrt::PointerRoutedEventArgs const& args)
{
__super::OnPointerEntered(args);
m_isPointerOver = true;
if (m_hasPointerCapture)
{
m_isMiddlePointerButtonPressed = true;
}
UpdateCloseButton();
HideLeftAdjacentTabSeparator();
}
void TabViewItem::OnPointerExited(winrt::PointerRoutedEventArgs const& args)
{
__super::OnPointerExited(args);
m_isPointerOver = false;
m_isMiddlePointerButtonPressed = false;
UpdateCloseButton();
UpdateForeground();
RestoreLeftAdjacentTabSeparatorVisibility();
}
void TabViewItem::OnPointerCanceled(winrt::PointerRoutedEventArgs const& args)
{
__super::OnPointerCanceled(args);
if (m_hasPointerCapture)
{
ReleasePointerCapture(args.Pointer());
m_isMiddlePointerButtonPressed = false;
}
RestoreLeftAdjacentTabSeparatorVisibility();
}
void TabViewItem::OnPointerCaptureLost(winrt::PointerRoutedEventArgs const& args)
{
__super::OnPointerCaptureLost(args);
m_hasPointerCapture = false;
m_isMiddlePointerButtonPressed = false;
RestoreLeftAdjacentTabSeparatorVisibility();
}
// Note that the ItemsView will handle the left and right arrow keys if we don't do so before it does,
// so this needs to be handled below the items view. That's why we can't put this in TabView's OnKeyDown.
void TabViewItem::OnKeyDown(winrt::KeyRoutedEventArgs const& args)
{
if (!args.Handled() && (args.Key() == winrt::VirtualKey::Left || args.Key() == winrt::VirtualKey::Right))
{
// Alt+Shift+Arrow reorders tabs, so we don't want to handle that combination.
// ListView also handles Alt+Arrow (no Shift) by just doing regular XY focus,
// same as how it handles Arrow without any modifier keys, so in that case
// we do want to handle things so we get the improved keyboarding experience.
auto isAltDown = (winrt::Window::Current().CoreWindow().GetKeyState(winrt::VirtualKey::Menu) & winrt::CoreVirtualKeyStates::Down) == winrt::CoreVirtualKeyStates::Down;
auto isShiftDown = (winrt::Window::Current().CoreWindow().GetKeyState(winrt::VirtualKey::Shift) & winrt::CoreVirtualKeyStates::Down) == winrt::CoreVirtualKeyStates::Down;
if (!isAltDown || !isShiftDown)
{
const bool moveForward =
(FlowDirection() == winrt::FlowDirection::LeftToRight && args.Key() == winrt::VirtualKey::Right) ||
(FlowDirection() == winrt::FlowDirection::RightToLeft && args.Key() == winrt::VirtualKey::Left);
args.Handled(winrt::get_self<TabView>(GetParentTabView())->MoveFocus(moveForward));
}
}
if (!args.Handled())
{
__super::OnKeyDown(args);
}
}
void TabViewItem::OnIconSourcePropertyChanged(const winrt::DependencyPropertyChangedEventArgs& args)
{
OnIconSourceChanged();
}
void TabViewItem::OnIconSourceChanged()
{
auto const templateSettings = winrt::get_self<TabViewItemTemplateSettings>(TabViewTemplateSettings());
if (auto const source = IconSource())
{
templateSettings->IconElement(SharedHelpers::MakeIconElementFrom(source));
winrt::VisualStateManager::GoToState(*this, L"Icon"sv, false);
}
else
{
templateSettings->IconElement(nullptr);
winrt::VisualStateManager::GoToState(*this, L"NoIcon"sv, false);
}
}
void TabViewItem::StartBringTabIntoView()
{
// we need to set the TargetRect to be slightly wider than the TabViewItem size in order to avoid cutting off the end of the Tab
winrt::BringIntoViewOptions options;
options.TargetRect(winrt::Rect{ 0, 0, DesiredSize().Width + c_targetRectWidthIncrement, DesiredSize().Height});
StartBringIntoView(options);
}