Skip to content

Commit 13e3179

Browse files
EriWongjoseartrivera
authored andcommitted
Wire up keyboard in graphing calculator (#863)
* Wire up graphing calculator keyboard with math rich edit control * CR feedback * Handle focus bug in flyout
1 parent 38da8d7 commit 13e3179

File tree

9 files changed

+1183
-824
lines changed

9 files changed

+1183
-824
lines changed

src/CalcViewModel/Common/CalculatorButtonUser.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,7 @@ public
120120
RshL = (int)CM::Command::CommandRSHFL,
121121
RolC = (int)CM::Command::CommandROLC,
122122
RorC = (int)CM::Command::CommandRORC,
123-
124-
Plot,
125-
X,
126-
Y,
127-
123+
128124
BINSTART = (int)CM::Command::CommandBINEDITSTART,
129125
BINPOS0 = (int)CM::Command::CommandBINPOS0,
130126
BINPOS1 = (int)CM::Command::CommandBINPOS1,
@@ -198,6 +194,14 @@ public
198194
MemoryRecall = (int)CM::Command::CommandRECALL,
199195
MemoryClear = (int)CM::Command::CommandMCLEAR,
200196
BitflipButton = 1000,
201-
FullKeypadButton = 1001
197+
FullKeypadButton = 1001,
198+
199+
// Buttons used in graphing calculator
200+
LessThan,
201+
LessThanOrEqualTo,
202+
GreaterThan,
203+
GreaterThanOrEqualTo,
204+
X,
205+
Y
202206
};
203207
}

src/Calculator/Controls/MathRichEditBox.cpp

Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -115,41 +115,86 @@ void MathRichEditBox::SetMathTextProperty(String ^ newValue)
115115
}
116116

117117
this->IsReadOnly = readOnlyState;
118-
SetValue(MathTextProperty, newValue);
119118
}
120119

121120
void CalculatorApp::Controls::MathRichEditBox::OnLosingFocus(Windows::UI::Xaml::UIElement ^ sender, Windows::UI::Xaml::Input::LosingFocusEventArgs ^ args)
122121
{
123-
auto newVal = GetMathTextProperty();
124-
if (MathText != newVal)
125-
{
126-
SetValue(MathTextProperty, newVal);
127-
EquationSubmitted(this, ref new MathRichEditBoxSubmission(true, EquationSubmissionSource::FOCUS_LOST));
128-
}
129-
else
130-
{
131-
EquationSubmitted(this, ref new MathRichEditBoxSubmission(false, EquationSubmissionSource::FOCUS_LOST));
132-
}
122+
SubmitEquation(EquationSubmissionSource::FOCUS_LOST);
133123
}
134124

135125
void CalculatorApp::Controls::MathRichEditBox::OnKeyUp(Platform::Object ^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs ^ e)
136126
{
137127
if (e->Key == VirtualKey::Enter)
138128
{
139-
auto newVal = GetMathTextProperty();
140-
if (MathText != newVal)
141-
{
142-
SetValue(MathTextProperty, newVal);
143-
EquationSubmitted(this, ref new MathRichEditBoxSubmission(true, EquationSubmissionSource::ENTER_KEY));
144-
}
145-
else
146-
{
147-
EquationSubmitted(this, ref new MathRichEditBoxSubmission(true, EquationSubmissionSource::ENTER_KEY));
148-
}
129+
SubmitEquation(EquationSubmissionSource::ENTER_KEY);
149130
}
150131
}
151132

152133
void MathRichEditBox::OnMathTextPropertyChanged(Platform::String ^ oldValue, Platform::String ^ newValue)
153134
{
154135
SetMathTextProperty(newValue);
136+
SetValue(MathTextProperty, newValue);
137+
}
138+
139+
void MathRichEditBox::InsertText(Platform::String ^ text, int cursorOffSet, int selectionLength)
140+
{
141+
// If the rich edit is empty, the math zone may not exist, and so selection (and thus the resulting text) will not be in a math zone.
142+
// If the rich edit has content already, then the mathzone will already be created due to mathonly mode being set and the selection will exist inside the
143+
// math zone. To handle this, we will force a math zone to be created in teh case of the text being empty and then replacing the text inside of the math
144+
// zone with the newly inserted text.
145+
if (GetMathTextProperty() == nullptr)
146+
{
147+
SetMathTextProperty("<math xmlns=\"http://www.w3.org/1998/Math/MathML\"><mi>x</mi></math>");
148+
TextDocument->Selection->StartPosition = 0;
149+
TextDocument->Selection->EndPosition = 1;
150+
}
151+
152+
// insert the text in place of selection
153+
TextDocument->Selection->SetText(Windows::UI::Text::TextSetOptions::FormatRtf, text);
154+
155+
// Move the cursor to the next logical place for users to enter text.
156+
TextDocument->Selection->StartPosition += cursorOffSet;
157+
TextDocument->Selection->EndPosition = TextDocument->Selection->StartPosition + selectionLength;
158+
}
159+
160+
void MathRichEditBox::BackSpace()
161+
{
162+
// if anything is selected, just delete the selection. Note: EndPosition can be before start position.
163+
if (TextDocument->Selection->StartPosition != TextDocument->Selection->EndPosition)
164+
{
165+
TextDocument->Selection->SetText(Windows::UI::Text::TextSetOptions::None, L"");
166+
return;
167+
}
168+
169+
// if we are at the start of the string, do nothing
170+
if (TextDocument->Selection->StartPosition == 0)
171+
{
172+
return;
173+
}
174+
175+
// Select the previous group.
176+
TextDocument->Selection->EndPosition = TextDocument->Selection->StartPosition;
177+
TextDocument->Selection->StartPosition -= 1;
178+
179+
// If the group contains anything complex, we want to give the user a chance to preview the deletion.
180+
// If it's a single character, then just delete it. Otherwise do nothing until the user triggers backspace again.
181+
auto text = TextDocument->Selection->Text;
182+
if (text->Length() == 1)
183+
{
184+
TextDocument->Selection->SetText(Windows::UI::Text::TextSetOptions::None, L"");
185+
}
186+
}
187+
188+
void MathRichEditBox::SubmitEquation(EquationSubmissionSource source)
189+
{
190+
auto newVal = GetMathTextProperty();
191+
if (MathText != newVal)
192+
{
193+
SetValue(MathTextProperty, newVal);
194+
EquationSubmitted(this, ref new MathRichEditBoxSubmission(true, source));
195+
}
196+
else
197+
{
198+
EquationSubmitted(this, ref new MathRichEditBoxSubmission(false, source));
199+
}
155200
}

src/Calculator/Controls/MathRichEditBox.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace CalculatorApp
1313
{
1414
FOCUS_LOST,
1515
ENTER_KEY,
16+
PROGRAMMATIC
1617
};
1718

1819
public
@@ -40,6 +41,9 @@ namespace CalculatorApp
4041

4142
event Windows::Foundation::EventHandler<MathRichEditBoxSubmission^> ^ EquationSubmitted;
4243
void OnMathTextPropertyChanged(Platform::String ^ oldValue, Platform::String ^ newValue);
44+
void InsertText(Platform::String ^ text, int cursorOffSet, int selectionLength);
45+
void SubmitEquation(EquationSubmissionSource source);
46+
void BackSpace();
4347

4448
private:
4549
Platform::String ^ GetMathTextProperty();

src/Calculator/Views/GraphingCalculator/EquationInputArea.xaml.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace CalculatorApp
3131

3232
static Windows::UI::Xaml::Media::SolidColorBrush
3333
^ ToSolidColorBrush(Windows::UI::Color color) { return ref new Windows::UI::Xaml::Media::SolidColorBrush(color); }
34-
34+
3535
private:
3636
void OnPropertyChanged(Platform::String^ propertyName);
3737
void OnEquationsPropertyChanged();
@@ -59,7 +59,6 @@ namespace CalculatorApp
5959
void TextBoxKeyDown(Windows::UI::Xaml::Controls::TextBox ^ textbox, Windows::UI::Xaml::Input::KeyRoutedEventArgs ^ e);
6060
void SubmitTextbox(Windows::UI::Xaml::Controls::TextBox ^ textbox);
6161

62-
private:
6362
Windows::UI::ViewManagement::AccessibilitySettings ^ m_accessibilitySettings;
6463
int m_lastLineColorIndex;
6564
int m_lastFunctionLabelIndex;

src/Calculator/Views/GraphingCalculator/GraphingCalculator.xaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,8 @@
514514
Variables="{x:Bind ViewModel.Variables}"
515515
Visibility="{x:Bind IsKeyGraphFeaturesVisible, Converter={StaticResource BooleanToVisibilityNegationConverter}, Mode=OneWay}"/>
516516

517-
<local:GraphingNumPad Grid.Row="1"
517+
<local:GraphingNumPad x:Name="GraphingNumberPad"
518+
Grid.Row="1"
518519
Margin="2,0,2,2"
519520
Visibility="{x:Bind IsKeyGraphFeaturesVisible, Converter={StaticResource BooleanToVisibilityNegationConverter}, Mode=OneWay}"/>
520521

0 commit comments

Comments
 (0)