Decrease CPU usage of OverflowTextBlock + optimization (#403)

Fixes #402 and #414
Divide by 4 the CPU usage of OverflowTextBlock when buttons are pressed very quickly.

Description of the changes:

Xaml-side:
OverflowTextBlock has some performance issues:
double scrollviewer: the listview was in a scrollviewer, while the control already containing one -> it breaks the virtualization of the listview and impacts on UI performance.
The listview used a StackPanel, this panel doesn't support virtualization of ListViewItems contrary to ItemsStackPanel

No ListView-specific features were used, an ItemsControl is more efficient and lighter.
refactor how we manage the visibility of the left/right buttons in OverflowTextBlock, the new version is more reactive and will not display the right arrow when not necessary (see GIF below).
remove the ItemContainerSelector ExpressionItemContainerStyle, not really used by OverflowTextBlock

remove UI glitches generated by ChangeView when users type fast (control partially hidden and scrolling issues, see the GIF below).
only modify the accessibility view when it's necessary

ViewModel-side:
stop fully refreshing ExpressionTokens in StandardCalculatorViewModel when a new command were sent, instead, use a IObservableVector to only send new tokens to the UI (in average only 1 or 2 UI items are refreshed while the full expression was refreshed before)

How changes were validated:
Manually
This commit is contained in:
Rudy Huyn
2019-04-18 15:21:33 -07:00
committed by Daniel Belcher
parent d21a47d5a1
commit de65db6197
11 changed files with 145 additions and 322 deletions

View File

@@ -30,25 +30,38 @@ DEPENDENCY_PROPERTY_INITIALIZATION(OverflowTextBlock, TokensUpdated);
void OverflowTextBlock::OnApplyTemplate()
{
assert(((m_scrollLeft == nullptr) && (m_scrollRight == nullptr)) || ((m_scrollLeft != nullptr) && (m_scrollRight != nullptr)));
UnregisterEventHandlers();
m_expressionContainer = safe_cast<ScrollViewer^>(GetTemplateChild("expressionContainer"));
m_expressionContainer->ChangeView(m_expressionContainer->ExtentWidth - m_expressionContainer->ViewportWidth, nullptr, nullptr);
auto uiElement = GetTemplateChild("expressionContainer");
if (uiElement != nullptr)
{
m_expressionContainer = safe_cast<ScrollViewer^>(uiElement);
m_expressionContainer->ChangeView(m_expressionContainer->ExtentWidth - m_expressionContainer->ViewportWidth, nullptr, nullptr);
m_containerViewChangedToken = m_expressionContainer->ViewChanged += ref new EventHandler<ScrollViewerViewChangedEventArgs ^>(this, &OverflowTextBlock::OnViewChanged);
}
m_scrollLeft = safe_cast<Button^>(GetTemplateChild("scrollLeft"));
m_scrollRight = safe_cast<Button^>(GetTemplateChild("scrollRight"));
uiElement = GetTemplateChild("scrollLeft");
if (uiElement != nullptr)
{
m_scrollLeft = safe_cast<Button^>(uiElement);
m_scrollLeftClickEventToken = m_scrollLeft->Click += ref new RoutedEventHandler(this, &OverflowTextBlock::OnScrollClick);
}
m_scrollLeftClickEventToken = m_scrollLeft->Click += ref new RoutedEventHandler(this, &OverflowTextBlock::OnScrollClick);
m_scrollRightClickEventToken = m_scrollRight->Click += ref new RoutedEventHandler(this, &OverflowTextBlock::OnScrollClick);
uiElement = GetTemplateChild("scrollRight");
if (uiElement != nullptr)
{
m_scrollRight = safe_cast<Button^>(GetTemplateChild("scrollRight"));
m_scrollRightClickEventToken = m_scrollRight->Click += ref new RoutedEventHandler(this, &OverflowTextBlock::OnScrollClick);
}
m_scrollingLeft = false;
m_scrollingRight = false;
auto borderContainer = safe_cast<Border^>(GetTemplateChild("expressionborder"));
m_pointerEnteredEventToken = borderContainer->PointerEntered += ref new PointerEventHandler(this, &OverflowTextBlock::OnPointerEntered);
m_pointerExitedEventToken = borderContainer->PointerExited += ref new PointerEventHandler(this, &OverflowTextBlock::OnPointerExited);
m_listView = safe_cast<ListView^>(GetTemplateChild("TokenList"));
uiElement = GetTemplateChild("TokenList");
if (uiElement != nullptr)
{
m_itemsControl = safe_cast<ItemsControl^>(uiElement);
}
UpdateAllState();
}
@@ -60,18 +73,19 @@ AutomationPeer^ OverflowTextBlock::OnCreateAutomationPeer()
void OverflowTextBlock::OnTokensUpdatedPropertyChanged(bool /*oldValue*/, bool newValue)
{
if ((m_listView != nullptr) && (newValue))
if (m_expressionContainer != nullptr && newValue)
{
unsigned int tokenCount = m_listView->Items->Size;
if (tokenCount > 0)
{
m_listView->UpdateLayout();
m_listView->ScrollIntoView(m_listView->Items->GetAt(tokenCount - 1));
m_expressionContainer->ChangeView(m_expressionContainer->ExtentWidth - m_expressionContainer->ViewportWidth, nullptr, nullptr);
}
m_expressionContainer->UpdateLayout();
m_expressionContainer->ChangeView(m_expressionContainer->ScrollableWidth, nullptr, nullptr, true);
}
AutomationProperties::SetAccessibilityView(this,
m_listView != nullptr && m_listView->Items->Size > 0 ? AccessibilityView::Control : AccessibilityView::Raw);
auto newIsAccessibilityViewControl = m_itemsControl != nullptr && m_itemsControl->Items->Size > 0;
if (m_isAccessibilityViewControl != newIsAccessibilityViewControl)
{
m_isAccessibilityViewControl = newIsAccessibilityViewControl;
AutomationProperties::SetAccessibilityView(this,
newIsAccessibilityViewControl ? AccessibilityView::Control : AccessibilityView::Raw);
}
UpdateScrollButtons();
}
void OverflowTextBlock::UpdateAllState()
@@ -93,7 +107,7 @@ void OverflowTextBlock::UpdateVisualState()
void OverflowTextBlock::ScrollLeft()
{
if (m_expressionContainer->HorizontalOffset > 0)
if (m_expressionContainer != nullptr && m_expressionContainer->HorizontalOffset > 0)
{
m_scrollingLeft = true;
double offset = m_expressionContainer->HorizontalOffset - (scrollRatio * m_expressionContainer->ViewportWidth);
@@ -105,7 +119,7 @@ void OverflowTextBlock::ScrollLeft()
void OverflowTextBlock::ScrollRight()
{
if (m_expressionContainer->HorizontalOffset < m_expressionContainer->ExtentWidth - m_expressionContainer->ViewportWidth)
if (m_expressionContainer != nullptr && m_expressionContainer->HorizontalOffset < m_expressionContainer->ExtentWidth - m_expressionContainer->ViewportWidth)
{
m_scrollingRight = true;
double offset = m_expressionContainer->HorizontalOffset + (scrollRatio * m_expressionContainer->ViewportWidth);
@@ -128,26 +142,15 @@ void OverflowTextBlock::OnScrollClick(_In_ Object^ sender, _In_ RoutedEventArgs^
}
}
void OverflowTextBlock::OnPointerEntered(_In_ Object^, _In_ PointerRoutedEventArgs^ e)
{
if (e->Pointer->PointerDeviceType == PointerDeviceType::Mouse)
{
UpdateScrollButtons();
}
}
void OverflowTextBlock::OnPointerExited(_In_ Object^, _In_ PointerRoutedEventArgs^ e)
{
if (e->Pointer->PointerDeviceType == PointerDeviceType::Mouse)
{
UpdateScrollButtons();
}
}
void OverflowTextBlock::UpdateScrollButtons()
{
if (m_itemsControl == nullptr || m_expressionContainer == nullptr)
{
return;
}
// When the width is smaller than the container, don't show any
if (m_listView->ActualWidth <= m_expressionContainer->ActualWidth)
if (m_itemsControl->ActualWidth <= m_expressionContainer->ActualWidth)
{
ShowHideScrollButtons(::Visibility::Collapsed, ::Visibility::Collapsed);
}
@@ -163,7 +166,10 @@ void OverflowTextBlock::UpdateScrollButtons()
if (m_scrollingLeft)
{
m_scrollingLeft = false;
m_scrollRight->Focus(::FocusState::Programmatic);
if (m_scrollRight != nullptr)
{
m_scrollRight->Focus(::FocusState::Programmatic);
}
}
}
else // Width is larger than the container and right most part of the number is shown. Should be able to scroll left.
@@ -172,7 +178,10 @@ void OverflowTextBlock::UpdateScrollButtons()
if (m_scrollingRight)
{
m_scrollingRight = false;
m_scrollLeft->Focus(::FocusState::Programmatic);
if (m_scrollLeft != nullptr)
{
m_scrollLeft->Focus(::FocusState::Programmatic);
}
}
}
}
@@ -199,12 +208,13 @@ void OverflowTextBlock::UnregisterEventHandlers()
m_scrollRight->Click -= m_scrollRightClickEventToken;
}
auto borderContainer = safe_cast<Border^>(GetTemplateChild("expressionborder"));
// Adding an extra check, in case the returned template is null
if (borderContainer != nullptr)
if (m_expressionContainer != nullptr)
{
borderContainer->PointerEntered -= m_pointerEnteredEventToken;
borderContainer->PointerExited -= m_pointerExitedEventToken;
m_expressionContainer->ViewChanged -= m_containerViewChangedToken;
}
}
void OverflowTextBlock::OnViewChanged(_In_opt_ Object^ /*sender*/, _In_opt_ ScrollViewerViewChangedEventArgs^ /*args*/)
{
UpdateScrollButtons();
}

View File

@@ -34,6 +34,7 @@ namespace CalculatorApp
void OnPointerExited(_In_ Platform::Object^ sender, _In_ Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e);
void ShowHideScrollButtons(Windows::UI::Xaml::Visibility vLeft, Windows::UI::Xaml::Visibility vRight);
void OnTokensUpdatedPropertyChanged(bool oldValue, bool newValue);
void OnViewChanged(_In_opt_ Platform::Object ^sender, _In_opt_ Windows::UI::Xaml::Controls::ScrollViewerViewChangedEventArgs ^args);
void UpdateVisualState();
void UpdateExpressionState();
@@ -44,15 +45,15 @@ namespace CalculatorApp
double scrollRatio = 0.7;
bool m_scrollingLeft;
bool m_scrollingRight;
Windows::UI::Xaml::Controls::ListView^ m_listView;
bool m_isAccessibilityViewControl;
Windows::UI::Xaml::Controls::ItemsControl^ m_itemsControl;
Windows::UI::Xaml::Controls::ScrollViewer^ m_expressionContainer;
Windows::UI::Xaml::Controls::Button^ m_scrollLeft;
Windows::UI::Xaml::Controls::Button^ m_scrollRight;
Windows::Foundation::EventRegistrationToken m_scrollLeftClickEventToken;
Windows::Foundation::EventRegistrationToken m_scrollRightClickEventToken;
Windows::Foundation::EventRegistrationToken m_pointerEnteredEventToken;
Windows::Foundation::EventRegistrationToken m_pointerExitedEventToken;
Windows::Foundation::EventRegistrationToken m_containerViewChangedToken;
};
}
}