calculator/src/Calculator/Controls/OverflowTextBlock.h
Rudy Huyn de65db6197 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
2019-04-18 15:21:33 -07:00

60 lines
2.4 KiB
C++

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#pragma once
#include "CalcViewModel/Common/Utils.h"
namespace CalculatorApp
{
namespace Controls
{
public ref class OverflowTextBlock sealed : public Windows::UI::Xaml::Controls::Control
{
public:
OverflowTextBlock()
{
}
DEPENDENCY_PROPERTY_OWNER(OverflowTextBlock);
DEPENDENCY_PROPERTY_WITH_CALLBACK(bool, TokensUpdated);
DEPENDENCY_PROPERTY(bool, IsActive);
DEPENDENCY_PROPERTY(Windows::UI::Xaml::Style^, TextStyle);
void UpdateScrollButtons();
void UnregisterEventHandlers();
protected:
virtual void OnApplyTemplate() override;
virtual Windows::UI::Xaml::Automation::Peers::AutomationPeer^ OnCreateAutomationPeer() override;
private:
void OnScrollClick(_In_ Platform::Object^ sender, _In_ Windows::UI::Xaml::RoutedEventArgs^ e);
void OnPointerEntered(_In_ Platform::Object^ sender, _In_ Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e);
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();
void UpdateAllState();
void ScrollLeft();
void ScrollRight();
double scrollRatio = 0.7;
bool m_scrollingLeft;
bool m_scrollingRight;
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_containerViewChangedToken;
};
}
}