Create EquationTextBox control (#547)
This commit is contained in:
220
src/Calculator/Controls/EquationTextBox.cpp
Normal file
220
src/Calculator/Controls/EquationTextBox.cpp
Normal file
@@ -0,0 +1,220 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
#include "pch.h"
|
||||
#include "EquationTextBox.h"
|
||||
#include "CalcViewModel/GraphingCalculator/EquationViewModel.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace Platform;
|
||||
using namespace CalculatorApp;
|
||||
using namespace CalculatorApp::Common;
|
||||
using namespace CalculatorApp::Controls;
|
||||
using namespace CalculatorApp::ViewModel;
|
||||
using namespace Windows::Foundation;
|
||||
using namespace Windows::UI::Xaml;
|
||||
using namespace Windows::UI::Xaml::Controls;
|
||||
using namespace Windows::UI::Xaml::Input;
|
||||
using namespace Windows::UI::Xaml::Controls::Primitives;
|
||||
using namespace Windows::UI::Text;
|
||||
|
||||
DEPENDENCY_PROPERTY_INITIALIZATION(EquationTextBox, EquationColor);
|
||||
DEPENDENCY_PROPERTY_INITIALIZATION(EquationTextBox, KeyGraphFeaturesContent);
|
||||
DEPENDENCY_PROPERTY_INITIALIZATION(EquationTextBox, ColorChooserFlyout);
|
||||
|
||||
void EquationTextBox::OnApplyTemplate()
|
||||
{
|
||||
m_equationButton = dynamic_cast<Button^>(GetTemplateChild("EquationButton"));
|
||||
m_richEditBox = dynamic_cast<RichEditBox^>(GetTemplateChild("EquationTextBox"));
|
||||
m_deleteButton = dynamic_cast<Button^>(GetTemplateChild("DeleteButton"));
|
||||
m_removeButton = dynamic_cast<Button^>(GetTemplateChild("RemoveButton"));
|
||||
m_functionButton = dynamic_cast<Button^>(GetTemplateChild("FunctionButton"));
|
||||
m_colorChooserButton = dynamic_cast<ToggleButton^>(GetTemplateChild("ColorChooserButton"));
|
||||
|
||||
if (m_richEditBox != nullptr)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
if (m_equationButton != nullptr)
|
||||
{
|
||||
m_equationButton->Click += ref new RoutedEventHandler(this, &EquationTextBox::OnEquationButtonClicked);
|
||||
}
|
||||
|
||||
if (m_deleteButton != nullptr)
|
||||
{
|
||||
m_deleteButton->Click += ref new RoutedEventHandler(this, &EquationTextBox::OnDeleteButtonClicked);
|
||||
}
|
||||
|
||||
if (m_removeButton != nullptr)
|
||||
{
|
||||
m_removeButton->Click += ref new RoutedEventHandler(this, &EquationTextBox::OnRemoveButtonClicked);
|
||||
}
|
||||
|
||||
if (m_colorChooserButton != nullptr)
|
||||
{
|
||||
m_colorChooserButton->Click += ref new RoutedEventHandler(this, &EquationTextBox::OnColorChooserButtonClicked);
|
||||
}
|
||||
|
||||
if (m_functionButton != nullptr)
|
||||
{
|
||||
m_functionButton->Click += ref new RoutedEventHandler(this, &EquationTextBox::OnFunctionButtonClicked);
|
||||
}
|
||||
|
||||
if (ColorChooserFlyout != nullptr)
|
||||
{
|
||||
ColorChooserFlyout->Opened += ref new EventHandler<Object^>(this, &EquationTextBox::OnColorFlyoutOpened);
|
||||
ColorChooserFlyout->Closed += ref new EventHandler<Object^>(this, &EquationTextBox::OnColorFlyoutClosed);
|
||||
}
|
||||
}
|
||||
|
||||
void EquationTextBox::OnPointerEntered(PointerRoutedEventArgs^ e)
|
||||
{
|
||||
m_isPointerOver = true;
|
||||
UpdateCommonVisualState();
|
||||
}
|
||||
|
||||
void EquationTextBox::OnPointerExited(PointerRoutedEventArgs^ e)
|
||||
{
|
||||
m_isPointerOver = false;
|
||||
UpdateCommonVisualState();
|
||||
}
|
||||
|
||||
void EquationTextBox::OnPointerCanceled(PointerRoutedEventArgs^ e)
|
||||
{
|
||||
m_isPointerOver = false;
|
||||
UpdateCommonVisualState();
|
||||
}
|
||||
|
||||
void EquationTextBox::OnPointerCaptureLost(PointerRoutedEventArgs^ e)
|
||||
{
|
||||
m_isPointerOver = false;
|
||||
UpdateCommonVisualState();
|
||||
}
|
||||
|
||||
void EquationTextBox::OnColorFlyoutOpened(Object^ sender, Object^ e)
|
||||
{
|
||||
m_isColorChooserFlyoutOpen = true;
|
||||
UpdateCommonVisualState();
|
||||
}
|
||||
|
||||
void EquationTextBox::OnColorFlyoutClosed(Object^ sender, Object^ e)
|
||||
{
|
||||
m_colorChooserButton->IsChecked = false;
|
||||
m_isColorChooserFlyoutOpen = false;
|
||||
UpdateCommonVisualState();
|
||||
}
|
||||
|
||||
void EquationTextBox::OnRichEditBoxTextChanged(Object^ sender, RoutedEventArgs^ e)
|
||||
{
|
||||
UpdateDeleteButtonVisualState();
|
||||
}
|
||||
|
||||
void EquationTextBox::OnRichEditBoxGotFocus(Object^ sender, RoutedEventArgs^ e)
|
||||
{
|
||||
m_isFocused = true;
|
||||
UpdateCommonVisualState();
|
||||
UpdateDeleteButtonVisualState();
|
||||
}
|
||||
|
||||
void EquationTextBox::OnRichEditBoxLostFocus(Object^ sender, RoutedEventArgs^ e)
|
||||
{
|
||||
m_isFocused = false;
|
||||
UpdateCommonVisualState();
|
||||
UpdateDeleteButtonVisualState();
|
||||
}
|
||||
|
||||
void EquationTextBox::OnDeleteButtonClicked(Object^ sender, RoutedEventArgs^ e)
|
||||
{
|
||||
if (m_richEditBox != nullptr)
|
||||
{
|
||||
m_richEditBox->TextDocument->SetText(::TextSetOptions::None, L"");
|
||||
}
|
||||
}
|
||||
|
||||
void EquationTextBox::OnEquationButtonClicked(Object^ sender, RoutedEventArgs^ e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void EquationTextBox::OnRemoveButtonClicked(Object^ sender, RoutedEventArgs^ e)
|
||||
{
|
||||
RemoveButtonClicked(this, ref new RoutedEventArgs());
|
||||
}
|
||||
|
||||
void EquationTextBox::OnColorChooserButtonClicked(Object^ sender, RoutedEventArgs^ e)
|
||||
{
|
||||
if (ColorChooserFlyout != nullptr && m_richEditBox != nullptr)
|
||||
{
|
||||
ColorChooserFlyout->ShowAt(m_richEditBox);
|
||||
}
|
||||
}
|
||||
|
||||
void EquationTextBox::OnFunctionButtonClicked(Object^ sender, RoutedEventArgs^ e)
|
||||
{
|
||||
auto equationViewModel = static_cast<EquationViewModel^>(DataContext);
|
||||
equationViewModel->KeyGraphFeaturesVisibility = (equationViewModel->KeyGraphFeaturesVisibility == ::Visibility::Collapsed) ? ::Visibility::Visible : ::Visibility::Collapsed;
|
||||
UpdateCommonVisualState();
|
||||
}
|
||||
|
||||
void EquationTextBox::UpdateDeleteButtonVisualState()
|
||||
{
|
||||
String^ state;
|
||||
|
||||
if (ShouldDeleteButtonBeVisible())
|
||||
{
|
||||
state = "ButtonVisible";
|
||||
}
|
||||
else
|
||||
{
|
||||
state = "ButtonCollapsed";
|
||||
}
|
||||
|
||||
VisualStateManager::GoToState(this, state, true);
|
||||
}
|
||||
|
||||
void EquationTextBox::UpdateCommonVisualState()
|
||||
{
|
||||
String^ state = "Normal";
|
||||
|
||||
if (m_isFocused)
|
||||
{
|
||||
state = "Focused";
|
||||
}
|
||||
else if (m_isPointerOver || m_isColorChooserFlyoutOpen)
|
||||
{
|
||||
state = "PointerOver";
|
||||
}
|
||||
|
||||
VisualStateManager::GoToState(this, state, true);
|
||||
}
|
||||
|
||||
|
||||
Platform::String^ EquationTextBox::GetEquationText()
|
||||
{
|
||||
String^ text;
|
||||
|
||||
if (m_richEditBox != nullptr)
|
||||
{
|
||||
m_richEditBox->TextDocument->GetText(::TextGetOptions::NoHidden, &text);
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
void EquationTextBox::SetEquationText(Platform::String^ equationText)
|
||||
{
|
||||
if (m_richEditBox != nullptr)
|
||||
{
|
||||
m_richEditBox->TextDocument->SetText(::TextSetOptions::None, equationText);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool EquationTextBox::ShouldDeleteButtonBeVisible()
|
||||
{
|
||||
return (!GetEquationText()->IsEmpty() && m_isFocused);
|
||||
|
||||
}
|
66
src/Calculator/Controls/EquationTextBox.h
Normal file
66
src/Calculator/Controls/EquationTextBox.h
Normal file
@@ -0,0 +1,66 @@
|
||||
// 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 EquationTextBox sealed : public Windows::UI::Xaml::Controls::Control
|
||||
{
|
||||
public:
|
||||
EquationTextBox()
|
||||
{
|
||||
}
|
||||
|
||||
DEPENDENCY_PROPERTY_OWNER(EquationTextBox);
|
||||
DEPENDENCY_PROPERTY(Windows::UI::Xaml::Media::SolidColorBrush^, EquationColor);
|
||||
DEPENDENCY_PROPERTY(Windows::UI::Xaml::UIElement^, KeyGraphFeaturesContent);
|
||||
DEPENDENCY_PROPERTY(Windows::UI::Xaml::Controls::Flyout^, ColorChooserFlyout);
|
||||
|
||||
event Windows::UI::Xaml::RoutedEventHandler^ RemoveButtonClicked;
|
||||
|
||||
Platform::String^ GetEquationText();
|
||||
void SetEquationText(Platform::String^ equationText);
|
||||
|
||||
protected:
|
||||
virtual void OnApplyTemplate() override;
|
||||
virtual void OnPointerEntered(Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e) override;
|
||||
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;
|
||||
|
||||
private:
|
||||
void UpdateCommonVisualState();
|
||||
void UpdateDeleteButtonVisualState();
|
||||
bool ShouldDeleteButtonBeVisible();
|
||||
|
||||
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);
|
||||
void OnRemoveButtonClicked(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
|
||||
void OnColorChooserButtonClicked(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
|
||||
void OnFunctionButtonClicked(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
|
||||
|
||||
void OnColorFlyoutOpened(Platform::Object^ sender, Platform::Object^ e);
|
||||
void OnColorFlyoutClosed(Platform::Object^ sender, Platform::Object^ e);
|
||||
|
||||
Windows::UI::Xaml::Controls::RichEditBox^ m_richEditBox;
|
||||
Windows::UI::Xaml::Controls::Button^ m_equationButton;
|
||||
Windows::UI::Xaml::Controls::Button^ m_deleteButton;
|
||||
Windows::UI::Xaml::Controls::Button^ m_removeButton;
|
||||
Windows::UI::Xaml::Controls::Button^ m_functionButton;
|
||||
Windows::UI::Xaml::Controls::Primitives::ToggleButton^ m_colorChooserButton;
|
||||
|
||||
bool m_isFocused;
|
||||
bool m_isPointerOver;
|
||||
bool m_isColorChooserFlyoutOpen;
|
||||
};
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user