Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/CalcViewModel/Common/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

// Utility macros to make Models easier to write
// generates a member variable called m_<n>

#define PROPERTY_R(t, n) \
property t n \
{ \
Expand Down Expand Up @@ -72,6 +73,25 @@ private:
\
public:

#define OBSERVABLE_PROPERTY_RW_ALWAYS_NOTIFY(t, n) \
property t n \
{ \
t get() \
{ \
return m_##n; \
} \
void set(t value) \
{ \
m_##n = value; \
RaisePropertyChanged(L#n); \
} \
} \
\
private: \
t m_##n; \
\
public:

#define OBSERVABLE_PROPERTY_RW(t, n) \
property t n \
{ \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,37 @@
#include "GraphingCalculatorViewModel.h"

using namespace CalculatorApp::ViewModel;
using namespace Platform;
using namespace Platform::Collections;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Xaml::Data;

namespace CalculatorApp::ViewModel
{
GraphingCalculatorViewModel::GraphingCalculatorViewModel()
: m_IsDecimalEnabled{ true }
, m_Equations{ ref new Vector< EquationViewModel^ >() }
, m_Variables{ ref new Vector< VariableViewModel^ >() }
{
}

void GraphingCalculatorViewModel::OnButtonPressed(Object^ parameter)
{
}

void GraphingCalculatorViewModel::UpdateVariables(IMap<String^, double>^ variables)
{
Variables->Clear();
for (auto var : variables)
{
auto variable = ref new VariableViewModel(var->Key, var->Value);
variable->VariableUpdated += ref new EventHandler<VariableChangedEventArgs>([this, variable](Object^ sender, VariableChangedEventArgs e)
{
VariableUpdated(variable, VariableChangedEventArgs{ e.variableName, e.newValue });
});
Variables->Append(variable);

}
}
}
60 changes: 60 additions & 0 deletions src/CalcViewModel/GraphingCalculator/GraphingCalculatorViewModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,62 @@

namespace CalculatorApp::ViewModel
{
public value struct VariableChangedEventArgs sealed
{
Platform::String^ variableName;
double newValue;
};

[Windows::UI::Xaml::Data::Bindable]
public ref class VariableViewModel sealed : public Windows::UI::Xaml::Data::INotifyPropertyChanged
{
public:
VariableViewModel(Platform::String^ name, double value) :
m_Name(name),
m_Value(value),
m_SliderSettingsVisible(false),
m_Min(0.0),
m_Step(0.1),
m_Max(2.0)
{ }

OBSERVABLE_OBJECT_CALLBACK(OnPropertyChanged);

OBSERVABLE_PROPERTY_R(Platform::String^, Name);

// TODO: Consider removing this work around and manually set the textbox text.
OBSERVABLE_PROPERTY_RW_ALWAYS_NOTIFY(double, Value);
OBSERVABLE_PROPERTY_RW_ALWAYS_NOTIFY(double, Min);
OBSERVABLE_PROPERTY_RW_ALWAYS_NOTIFY(double, Step);
OBSERVABLE_PROPERTY_RW_ALWAYS_NOTIFY(double, Max);
OBSERVABLE_PROPERTY_RW(bool, SliderSettingsVisible);

event Windows::Foundation::EventHandler<VariableChangedEventArgs>^ VariableUpdated;

void SetValue(double value)
{
if (value < Min)
{
value = Min;
}
else if (value > Max)
{
value = Max;
}

Value = value;
}

private:
void OnPropertyChanged(Platform::String^ propertyName)
{
if (propertyName == "Value")
{
VariableUpdated(this, VariableChangedEventArgs{ Name, Value });
}
}
};

[Windows::UI::Xaml::Data::Bindable]
public ref class GraphingCalculatorViewModel sealed : public Windows::UI::Xaml::Data::INotifyPropertyChanged
{
Expand All @@ -14,9 +70,13 @@ namespace CalculatorApp::ViewModel
OBSERVABLE_OBJECT();
OBSERVABLE_PROPERTY_R(bool, IsDecimalEnabled);
OBSERVABLE_PROPERTY_R(Windows::Foundation::Collections::IObservableVector< EquationViewModel^ >^, Equations);
OBSERVABLE_PROPERTY_R(Windows::Foundation::Collections::IObservableVector< VariableViewModel^ >^, Variables);

COMMAND_FOR_METHOD(ButtonPressed, GraphingCalculatorViewModel::OnButtonPressed);

event Windows::Foundation::EventHandler<VariableChangedEventArgs>^ VariableUpdated;

void UpdateVariables(Windows::Foundation::Collections::IMap<Platform::String^, double>^ variables);
private:
void OnButtonPressed(Platform::Object^ parameter);
};
Expand Down
10 changes: 10 additions & 0 deletions src/Calculator/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -1725,6 +1725,16 @@
</Setter.Value>
</Setter>
</Style>

<Style x:Key="VariableTextBoxStyle" TargetType="TextBox">
<Setter Property="Margin" Value="12,4,4,4"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderThickness" Value="0,0,0,1"/>
<Setter Property="FontSize" Value="14"/>
<Setter Property="AcceptsReturn" Value="False"/>
<Setter Property="InputScope" Value="Number"/>
<Setter Property="TextWrapping" Value="NoWrap"/>
</Style>
</ResourceDictionary>
</Application.Resources>
</Application>
16 changes: 16 additions & 0 deletions src/Calculator/Resources/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -3439,4 +3439,20 @@
<value>Add Equation</value>
<comment>Placeholder text for the equation input button</comment>
</data>
<data name="VaiablesHeader.Text" xml:space="preserve">
<value>Variables</value>
<comment>Header text for variables area</comment>
</data>
<data name="StepTextBlock.Text" xml:space="preserve">
<value>Step</value>
<comment>Label text for the step text box</comment>
</data>
<data name="MinTextBlock.Text" xml:space="preserve">
<value>Min</value>
<comment>Label text for the min text box</comment>
</data>
<data name="MaxTextBlock.Text" xml:space="preserve">
<value>Max</value>
<comment>Label text for the max text box</comment>
</data>
</root>
Loading