[Graph] Replace the Add button by a EquationTextBlock (#826)

* init

* Replace the Add equation button by an EquationTextBox

* Move automatically the focus to the next textbox when users press Enter

* nit

* localize the placeholder

* rebase

* rebased and disable remove context menu when the equation is the last one

* replace storyboards by setters in visual states
This commit is contained in:
Rudy Huyn
2019-12-10 11:06:37 -08:00
committed by Pepe Rivera
parent f1bf3354c4
commit 07d012cf6b
12 changed files with 449 additions and 281 deletions

View File

@@ -3,6 +3,7 @@
#include "pch.h"
#include "EquationInputArea.xaml.h"
#include "Utils/VisualTree.h"
using namespace CalculatorApp;
using namespace CalculatorApp::Common;
@@ -21,6 +22,7 @@ using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Input;
using namespace GraphControl;
using namespace Calculator::Utils;
namespace
{
@@ -31,6 +33,7 @@ EquationInputArea::EquationInputArea()
: m_lastLineColorIndex{ -1 }
, m_AvailableColors{ ref new Vector<SolidColorBrush ^>() }
, m_accessibilitySettings{ ref new AccessibilitySettings() }
, m_equationToFocus{ nullptr }
{
m_accessibilitySettings->HighContrastChanged +=
ref new TypedEventHandler<AccessibilitySettings ^, Object ^>(this, &EquationInputArea::OnHighContrastChanged);
@@ -56,14 +59,15 @@ void EquationInputArea::OnEquationsPropertyChanged()
}
}
void EquationInputArea::AddEquationButton_Click(Object ^ sender, RoutedEventArgs ^ e)
{
AddNewEquation();
}
void EquationInputArea::AddNewEquation()
{
auto eq = ref new EquationViewModel(ref new Equation());
eq->IsLastItemInList = true;
if (Equations->Size > 0)
{
Equations->GetAt(Equations->Size - 1)->IsLastItemInList = false;
}
m_lastLineColorIndex = (m_lastLineColorIndex + 1) % AvailableColors->Size;
@@ -71,7 +75,7 @@ void EquationInputArea::AddNewEquation()
eq->IsLineEnabled = true;
eq->FunctionLabelIndex = ++m_lastFunctionLabelIndex;
Equations->Append(eq);
EquationInputList->ScrollIntoView(eq);
m_equationToFocus = eq;
}
void EquationInputArea::InputTextBox_GotFocus(Object ^ sender, RoutedEventArgs ^ e)
@@ -84,20 +88,65 @@ void EquationInputArea::InputTextBox_LostFocus(Object ^ sender, RoutedEventArgs
KeyboardShortcutManager::HonorShortcuts(true);
}
void EquationInputArea::InputTextBox_Submitted(Object ^ sender, RoutedEventArgs ^ e)
void EquationInputArea::InputTextBox_Submitted(Object ^ sender, EquationSubmissionSource source)
{
auto tb = static_cast<EquationTextBox ^>(sender);
auto eq = static_cast<EquationViewModel ^>(tb->DataContext);
// eq can be null if the equation has been removed
if (eq != nullptr)
if (tb == nullptr)
{
eq->Expression = tb->GetEquationText();
return;
}
auto eq = static_cast<EquationViewModel ^>(tb->DataContext);
if (eq == nullptr)
{
return;
}
if (tb->HasFocus)
auto expressionText = tb->GetEquationText();
if (source == EquationSubmissionSource::FOCUS_LOST && eq->Expression == expressionText)
{
FocusManager::TryMoveFocus(::FocusNavigationDirection::Left);
// The expression didn't change.
return;
}
eq->Expression = expressionText;
if (source == EquationSubmissionSource::ENTER_KEY || eq->Expression != nullptr && eq->Expression->Length() > 0)
{
unsigned int index = 0;
if (Equations->IndexOf(eq, &index) && index == Equations->Size - 1)
{
// If it's the last equation of the list
AddNewEquation();
}
else
{
auto nextEquation = Equations->GetAt(index + 1);
FocusEquationTextBox(nextEquation);
}
}
}
void EquationInputArea::FocusEquationTextBox(EquationViewModel ^ equation)
{
auto nextContainer = EquationInputList->ContainerFromItem(equation);
if (nextContainer == nullptr)
{
return;
}
auto listviewItem = dynamic_cast<ListViewItem ^>(nextContainer);
if (listviewItem == nullptr)
{
return;
}
auto equationInput = VisualTree::FindDescendantByName(nextContainer, "EquationInputButton");
if (equationInput == nullptr)
{
return;
}
auto equationTextBox = dynamic_cast<EquationTextBox ^>(equationInput);
if (equationTextBox != nullptr)
{
equationTextBox->FocusTextBox();
}
}
@@ -113,6 +162,10 @@ void EquationInputArea::EquationTextBox_RemoveButtonClicked(Object ^ sender, Rou
m_lastFunctionLabelIndex--;
}
if (index == Equations->Size - 1 && Equations->Size > 1)
{
Equations->GetAt(Equations->Size - 2)->IsLastItemInList = true;
}
Equations->RemoveAt(index);
}
}
@@ -124,7 +177,7 @@ void EquationInputArea::EquationTextBox_KeyGraphFeaturesButtonClicked(Object ^ s
// ensure the equation has been submitted before trying to get key graph features out of it
if (tb->HasFocus)
{
EquationInputArea::InputTextBox_Submitted(sender, e);
EquationInputArea::InputTextBox_Submitted(sender, EquationSubmissionSource::FOCUS_LOST);
}
auto eq = static_cast<EquationViewModel ^>(tb->DataContext);
@@ -143,10 +196,15 @@ void EquationInputArea::EquationTextBox_EquationButtonClicked(Object ^ sender, R
void EquationInputArea::EquationTextBoxLoaded(Object ^ sender, RoutedEventArgs ^ e)
{
auto tb = static_cast<EquationTextBox ^>(sender);
auto eq = static_cast<EquationViewModel ^>(tb->DataContext);
auto colorChooser = static_cast<EquationStylePanelControl ^>(tb->ColorChooserFlyout->Content);
colorChooser->AvailableColors = AvailableColors;
if (tb->DataContext == m_equationToFocus)
{
m_equationToFocus = nullptr;
tb->FocusTextBox();
}
}
void EquationInputArea::OnHighContrastChanged(AccessibilitySettings ^ sender, Object ^ args)