Save and restore the value of EquationInputArea when users scroll (#866)

* Save and restore the value of EquationInputArea when users scroll

* clean up

* replace ->Focus(..) by FocusManager::TryFocus(...)
This commit is contained in:
Rudy Huyn
2019-12-13 16:42:12 -08:00
committed by Eric Wong
parent 534139d67d
commit dbddc7bc86
11 changed files with 101 additions and 103 deletions

View File

@@ -26,6 +26,7 @@ DEPENDENCY_PROPERTY_INITIALIZATION(EquationTextBox, ColorChooserFlyout);
DEPENDENCY_PROPERTY_INITIALIZATION(EquationTextBox, EquationButtonContentIndex);
DEPENDENCY_PROPERTY_INITIALIZATION(EquationTextBox, HasError);
DEPENDENCY_PROPERTY_INITIALIZATION(EquationTextBox, IsAddEquationMode);
DEPENDENCY_PROPERTY_INITIALIZATION(EquationTextBox, MathEquation);
EquationTextBox::EquationTextBox()
{
@@ -51,8 +52,9 @@ void EquationTextBox::OnApplyTemplate()
{
m_richEditBox->GotFocus += ref new RoutedEventHandler(this, &EquationTextBox::OnRichEditBoxGotFocus);
m_richEditBox->LostFocus += ref new RoutedEventHandler(this, &EquationTextBox::OnRichEditBoxLostFocus);
m_richEditBox->TextChanged += ref new RoutedEventHandler(this, &EquationTextBox::OnRichEditBoxTextChanged);
m_richEditBox->SelectionFlyout = nullptr;
m_richEditBox->EquationSubmitted +=
ref new EventHandler<MathRichEditBoxSubmission ^>(this, &EquationTextBox::OnEquationSubmitted);
}
if (m_equationButton != nullptr)
@@ -69,7 +71,7 @@ void EquationTextBox::OnApplyTemplate()
if (m_richEditContextMenu != nullptr)
{
m_richEditContextMenu->Opening += ref new Windows::Foundation::EventHandler<Platform::Object ^>(this, &EquationTextBox::OnRichEditMenuOpening);
m_richEditContextMenu->Opening += ref new EventHandler<Platform::Object ^>(this, &EquationTextBox::OnRichEditMenuOpening);
}
if (m_kgfEquationButton != nullptr)
@@ -150,37 +152,6 @@ void EquationTextBox::OnPointerCaptureLost(PointerRoutedEventArgs ^ e)
UpdateCommonVisualState();
}
void EquationTextBox::OnKeyDown(KeyRoutedEventArgs ^ e)
{
if (e->Key == VirtualKey::Enter)
{
m_sourceSubmission = EquationSubmissionSource::ENTER_KEY;
// We will rely on OnLostFocus to submit the equation to prevent the launch of 2 events
if (!m_HasFocus || !FocusManager::TryMoveFocusAsync(::FocusNavigationDirection::Next))
{
m_sourceSubmission = EquationSubmissionSource::FOCUS_LOST;
EquationSubmitted(this, EquationSubmissionSource::ENTER_KEY);
if (m_functionButton && m_richEditBox->MathText != L"")
{
m_functionButton->IsEnabled = true;
}
}
}
}
void EquationTextBox::OnLostFocus(RoutedEventArgs ^ e)
{
if (m_richEditBox == nullptr || m_richEditBox->ContextFlyout->IsOpen)
{
return;
}
if (m_functionButton && m_richEditBox->MathText != L"")
{
m_functionButton->IsEnabled = true;
}
}
void EquationTextBox::OnColorFlyoutOpened(Object ^ sender, Object ^ e)
{
m_isColorChooserFlyoutOpen = true;
@@ -194,15 +165,9 @@ void EquationTextBox::OnColorFlyoutClosed(Object ^ sender, Object ^ e)
UpdateCommonVisualState();
}
void EquationTextBox::OnRichEditBoxTextChanged(Object ^ sender, RoutedEventArgs ^ e)
{
UpdateButtonsVisualState();
}
void EquationTextBox::OnRichEditBoxGotFocus(Object ^ sender, RoutedEventArgs ^ e)
{
m_HasFocus = true;
m_sourceSubmission = EquationSubmissionSource::FOCUS_LOST;
UpdateCommonVisualState();
UpdateButtonsVisualState();
}
@@ -216,8 +181,6 @@ void EquationTextBox::OnRichEditBoxLostFocus(Object ^ sender, RoutedEventArgs ^
UpdateCommonVisualState();
UpdateButtonsVisualState();
EquationSubmitted(this, m_sourceSubmission);
}
void EquationTextBox::OnDeleteButtonClicked(Object ^ sender, RoutedEventArgs ^ e)
@@ -413,6 +376,19 @@ void EquationTextBox::FocusTextBox()
{
if (m_richEditBox != nullptr)
{
m_richEditBox->Focus(::FocusState::Programmatic);
FocusManager::TryFocusAsync(m_richEditBox, ::FocusState::Programmatic);
}
}
void EquationTextBox::OnEquationSubmitted(Platform::Object ^ sender, MathRichEditBoxSubmission ^ args)
{
if (args->HasTextChanged)
{
if (m_functionButton && m_richEditBox->MathText != L"")
{
m_functionButton->IsEnabled = true;
}
}
EquationSubmitted(this, args);
}

View File

@@ -11,13 +11,6 @@ namespace CalculatorApp
{
namespace Controls
{
public
enum class EquationSubmissionSource
{
FOCUS_LOST,
ENTER_KEY
};
public
ref class EquationTextBox sealed : public Windows::UI::Xaml::Controls::Control
{
@@ -28,6 +21,7 @@ namespace CalculatorApp
DEPENDENCY_PROPERTY(Windows::UI::Xaml::Media::SolidColorBrush ^, EquationColor);
DEPENDENCY_PROPERTY(Windows::UI::Xaml::Controls::Flyout ^, ColorChooserFlyout);
DEPENDENCY_PROPERTY(Platform::String ^, EquationButtonContentIndex);
DEPENDENCY_PROPERTY(Platform::String ^, MathEquation);
DEPENDENCY_PROPERTY_WITH_CALLBACK(bool, HasError);
DEPENDENCY_PROPERTY_WITH_CALLBACK(bool, IsAddEquationMode);
@@ -35,7 +29,7 @@ namespace CalculatorApp
event Windows::UI::Xaml::RoutedEventHandler ^ RemoveButtonClicked;
event Windows::UI::Xaml::RoutedEventHandler ^ KeyGraphFeaturesButtonClicked;
event Windows::Foundation::EventHandler<EquationSubmissionSource> ^ EquationSubmitted;
event Windows::Foundation::EventHandler<MathRichEditBoxSubmission ^> ^ EquationSubmitted;
event Windows::UI::Xaml::RoutedEventHandler ^ EquationButtonClicked;
Platform::String ^ GetEquationText();
@@ -48,8 +42,6 @@ namespace CalculatorApp
virtual void OnPointerExited(Windows::UI::Xaml::Input::PointerRoutedEventArgs ^ e) override;
virtual void OnPointerCanceled(Windows::UI::Xaml::Input::PointerRoutedEventArgs ^ e) override;
virtual void OnPointerCaptureLost(Windows::UI::Xaml::Input::PointerRoutedEventArgs ^ e) override;
virtual void OnKeyDown(Windows::UI::Xaml::Input::KeyRoutedEventArgs ^ e) override;
virtual void OnLostFocus(Windows::UI::Xaml::RoutedEventArgs ^ e) override;
void OnIsAddEquationModePropertyChanged(bool oldValue, bool newValue);
private:
@@ -59,7 +51,6 @@ namespace CalculatorApp
void OnRichEditBoxGotFocus(Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e);
void OnRichEditBoxLostFocus(Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e);
void OnRichEditBoxTextChanged(Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e);
void OnDeleteButtonClicked(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
void OnEquationButtonClicked(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
@@ -89,7 +80,7 @@ namespace CalculatorApp
bool m_isPointerOver;
bool m_isColorChooserFlyoutOpen;
EquationSubmissionSource m_sourceSubmission;
};
void OnEquationSubmitted(Platform::Object ^ sender, CalculatorApp::Controls::MathRichEditBoxSubmission ^ args);
};
}
}

View File

@@ -13,6 +13,7 @@ using namespace Windows::ApplicationModel;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::Foundation::Collections;
using namespace Windows::System;
DEPENDENCY_PROPERTY_INITIALIZATION(MathRichEditBox, MathText);
@@ -75,6 +76,9 @@ MathRichEditBox::MathRichEditBox()
{
throw Exception::CreateException(hr);
}
this->LosingFocus += ref new Windows::Foundation::TypedEventHandler<Windows::UI::Xaml::UIElement ^, Windows::UI::Xaml::Input::LosingFocusEventArgs ^>(
this, &CalculatorApp::Controls::MathRichEditBox::OnLosingFocus);
this->KeyUp += ref new Windows::UI::Xaml::Input::KeyEventHandler(this, &CalculatorApp::Controls::MathRichEditBox::OnKeyUp);
}
String ^ MathRichEditBox::GetMathTextProperty()
@@ -111,4 +115,41 @@ void MathRichEditBox::SetMathTextProperty(String ^ newValue)
}
this->IsReadOnly = readOnlyState;
SetValue(MathTextProperty, newValue);
}
void CalculatorApp::Controls::MathRichEditBox::OnLosingFocus(Windows::UI::Xaml::UIElement ^ sender, Windows::UI::Xaml::Input::LosingFocusEventArgs ^ args)
{
auto newVal = GetMathTextProperty();
if (MathText != newVal)
{
SetValue(MathTextProperty, newVal);
EquationSubmitted(this, ref new MathRichEditBoxSubmission(true, EquationSubmissionSource::FOCUS_LOST));
}
else
{
EquationSubmitted(this, ref new MathRichEditBoxSubmission(false, EquationSubmissionSource::FOCUS_LOST));
}
}
void CalculatorApp::Controls::MathRichEditBox::OnKeyUp(Platform::Object ^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs ^ e)
{
if (e->Key == VirtualKey::Enter)
{
auto newVal = GetMathTextProperty();
if (MathText != newVal)
{
SetValue(MathTextProperty, newVal);
EquationSubmitted(this, ref new MathRichEditBoxSubmission(true, EquationSubmissionSource::ENTER_KEY));
}
else
{
EquationSubmitted(this, ref new MathRichEditBoxSubmission(true, EquationSubmissionSource::ENTER_KEY));
}
}
}
void MathRichEditBox::OnMathTextPropertyChanged(Platform::String ^ oldValue, Platform::String ^ newValue)
{
SetMathTextProperty(newValue);
}

View File

@@ -8,6 +8,27 @@ namespace CalculatorApp
{
namespace Controls
{
public
enum class EquationSubmissionSource
{
FOCUS_LOST,
ENTER_KEY,
};
public
ref class MathRichEditBoxSubmission sealed
{
public:
PROPERTY_R(bool, HasTextChanged);
PROPERTY_R(EquationSubmissionSource, Source);
public:
MathRichEditBoxSubmission(bool hasTextChanged, EquationSubmissionSource source)
: m_HasTextChanged(hasTextChanged)
, m_Source(source)
{
}
};
public
ref class MathRichEditBox sealed : Windows::UI::Xaml::Controls::RichEditBox
{
@@ -15,35 +36,17 @@ namespace CalculatorApp
MathRichEditBox();
DEPENDENCY_PROPERTY_OWNER(MathRichEditBox);
DEPENDENCY_PROPERTY_WITH_DEFAULT_AND_CALLBACK(Platform::String ^, MathText, L"");
static property Windows::UI::Xaml::DependencyProperty ^ MathTextProperty
{
Windows::UI::Xaml::DependencyProperty ^ get() {
return s_MathTextProperty;
}
}
property Platform::String ^ MathText
{
Platform::String ^ get()
{
return GetMathTextProperty();
}
void set(Platform::String^ value)
{
SetMathTextProperty(value);
}
}
event Windows::Foundation::EventHandler<MathRichEditBoxSubmission^> ^ EquationSubmitted;
void OnMathTextPropertyChanged(Platform::String ^ oldValue, Platform::String ^ newValue);
private :
private:
Platform::String ^ GetMathTextProperty();
void SetMathTextProperty(Platform::String ^ newValue);
static Windows::UI::Xaml::DependencyProperty ^ s_MathTextProperty;
static Windows::UI::Xaml::DependencyProperty ^ InitializeMathTextProperty()
{
return Utils::RegisterDependencyProperty<DependencyPropertiesOwner, Platform::String ^>(L"MathText");
}
void OnLosingFocus(Windows::UI::Xaml::UIElement ^ sender, Windows::UI::Xaml::Input::LosingFocusEventArgs ^ args);
void OnKeyUp(Platform::Object ^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs ^ e);
};
}
}