Skip to content

Commit

Permalink
Fix crash and high contrast (#773)
Browse files Browse the repository at this point in the history
  • Loading branch information
joseartrivera committed Nov 5, 2019
1 parent 65c3046 commit 433d54d
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 62 deletions.
22 changes: 5 additions & 17 deletions src/Calculator/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -150,23 +150,11 @@
<SolidColorBrush x:Key="EquationBoxHoverButtonForegroundBrush" Color="{ThemeResource SystemColorButtonTextColor}"/>
<SolidColorBrush x:Key="AppControlTransparentButtonBackgroundBrush" Color="{ThemeResource SystemColorButtonFaceColor}"/>

<!-- TODO: Add high contrast colors mapping -->
<SolidColorBrush x:Key="EquationBrush1" Color="#FFFF0000"/>
<SolidColorBrush x:Key="EquationBrush2" Color="#FFFFB900"/>
<SolidColorBrush x:Key="EquationBrush3" Color="#FFFF8C00"/>
<SolidColorBrush x:Key="EquationBrush4" Color="#FFF7630C"/>
<SolidColorBrush x:Key="EquationBrush5" Color="#FFCA5010"/>
<SolidColorBrush x:Key="EquationBrush6" Color="#FFE3008C"/>
<SolidColorBrush x:Key="EquationBrush7" Color="#FFBF0077"/>
<SolidColorBrush x:Key="EquationBrush8" Color="#FFC239B3"/>
<SolidColorBrush x:Key="EquationBrush9" Color="#FF0063B1"/>
<SolidColorBrush x:Key="EquationBrush10" Color="#FF2D7D9A"/>
<SolidColorBrush x:Key="EquationBrush11" Color="#FF00B7C3"/>
<SolidColorBrush x:Key="EquationBrush12" Color="#FF00CC6A"/>
<SolidColorBrush x:Key="EquationBrush13" Color="#FF00B294"/>
<SolidColorBrush x:Key="EquationBrush14" Color="#FF018574"/>
<SolidColorBrush x:Key="EquationBrush15" Color="#FF10893E"/>
<SolidColorBrush x:Key="EquationBrush16" Color="#FF000000"/>
<!-- TODO: Figure out what colors we can use in high contrast -->
<SolidColorBrush x:Key="EquationBrush1" Color="{ThemeResource SystemColorGrayTextColor}"/>
<SolidColorBrush x:Key="EquationBrush2" Color="{ThemeResource SystemColorHighlightColor}"/>
<SolidColorBrush x:Key="EquationBrush3" Color="{ThemeResource SystemColorHotlightColor}"/>
<SolidColorBrush x:Key="EquationBrush4" Color="{ThemeResource SystemColorWindowColor}"/>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>

Expand Down
30 changes: 7 additions & 23 deletions src/Calculator/EquationStylePanelControl.xaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,11 @@ using namespace Windows::UI::Xaml::Navigation;
using namespace Windows::UI::Xaml::Shapes;

DEPENDENCY_PROPERTY_INITIALIZATION(EquationStylePanelControl, SelectedColor);
DEPENDENCY_PROPERTY_INITIALIZATION(EquationStylePanelControl, AvailableColors);

EquationStylePanelControl::EquationStylePanelControl()
{
InitializeComponent();
InitializeAvailableColors();
}

void EquationStylePanelControl::InitializeAvailableColors()
{
// TODO: Handle dynamically switching these to high contrast equivalents
m_AvailableColors = ref new Vector<SolidColorBrush ^>();
m_AvailableColors->Append(safe_cast<SolidColorBrush ^>(Application::Current->Resources->Lookup(L"EquationBrush1")));
m_AvailableColors->Append(safe_cast<SolidColorBrush ^>(Application::Current->Resources->Lookup(L"EquationBrush2")));
m_AvailableColors->Append(safe_cast<SolidColorBrush ^>(Application::Current->Resources->Lookup(L"EquationBrush3")));
m_AvailableColors->Append(safe_cast<SolidColorBrush ^>(Application::Current->Resources->Lookup(L"EquationBrush4")));
m_AvailableColors->Append(safe_cast<SolidColorBrush ^>(Application::Current->Resources->Lookup(L"EquationBrush5")));
m_AvailableColors->Append(safe_cast<SolidColorBrush ^>(Application::Current->Resources->Lookup(L"EquationBrush6")));
m_AvailableColors->Append(safe_cast<SolidColorBrush ^>(Application::Current->Resources->Lookup(L"EquationBrush7")));
m_AvailableColors->Append(safe_cast<SolidColorBrush ^>(Application::Current->Resources->Lookup(L"EquationBrush8")));
m_AvailableColors->Append(safe_cast<SolidColorBrush ^>(Application::Current->Resources->Lookup(L"EquationBrush9")));
m_AvailableColors->Append(safe_cast<SolidColorBrush ^>(Application::Current->Resources->Lookup(L"EquationBrush10")));
m_AvailableColors->Append(safe_cast<SolidColorBrush ^>(Application::Current->Resources->Lookup(L"EquationBrush11")));
m_AvailableColors->Append(safe_cast<SolidColorBrush ^>(Application::Current->Resources->Lookup(L"EquationBrush12")));
m_AvailableColors->Append(safe_cast<SolidColorBrush ^>(Application::Current->Resources->Lookup(L"EquationBrush13")));
m_AvailableColors->Append(safe_cast<SolidColorBrush ^>(Application::Current->Resources->Lookup(L"EquationBrush14")));
m_AvailableColors->Append(safe_cast<SolidColorBrush ^>(Application::Current->Resources->Lookup(L"EquationBrush15")));
m_AvailableColors->Append(safe_cast<SolidColorBrush ^>(Application::Current->Resources->Lookup(L"EquationBrush16")));
}

void EquationStylePanelControl::SelectionChanged(Object ^ /*sender */, SelectionChangedEventArgs ^ e)
Expand All @@ -70,6 +48,11 @@ void EquationStylePanelControl::ColorChooserLoaded(Object ^ sender, RoutedEventA

void EquationStylePanelControl::SelectColor(SolidColorBrush ^ selectedColor)
{
if (selectedColor == nullptr)
{
return;
}

for (auto item : ColorChooser->Items->GetView())
{
auto brush = static_cast<SolidColorBrush ^>(item);
Expand All @@ -83,6 +66,7 @@ void EquationStylePanelControl::SelectColor(SolidColorBrush ^ selectedColor)
if (brush->Color == selectedColor->Color)
{
gridViewItem->IsSelected = true;
return;
}
else
{
Expand Down
11 changes: 1 addition & 10 deletions src/Calculator/EquationStylePanelControl.xaml.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,12 @@ namespace CalculatorApp
DEPENDENCY_PROPERTY_OWNER(EquationStylePanelControl);

DEPENDENCY_PROPERTY_WITH_CALLBACK(Windows::UI::Xaml::Media::SolidColorBrush ^, SelectedColor);

property Windows::Foundation::Collections::IVector<Windows::UI::Xaml::Media::SolidColorBrush^>^ AvailableColors
{
Windows::Foundation::Collections::IVector<Windows::UI::Xaml::Media::SolidColorBrush^>^ get() {
return m_AvailableColors;
}
}
DEPENDENCY_PROPERTY_WITH_DEFAULT(Windows::Foundation::Collections::IVector<Windows::UI::Xaml::Media::SolidColorBrush ^> ^, AvailableColors, nullptr);

private:
void InitializeAvailableColors();
void SelectionChanged(Platform::Object ^ sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs ^ e);
void OnSelectedColorPropertyChanged(Windows::UI::Xaml::Media::SolidColorBrush ^ oldValue, Windows::UI::Xaml::Media::SolidColorBrush ^ newValue);
void ColorChooserLoaded(Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e);
void SelectColor(Windows::UI::Xaml::Media::SolidColorBrush ^ selectedColor);

Windows::Foundation::Collections::IVector<Windows::UI::Xaml::Media::SolidColorBrush ^>^ m_AvailableColors;
};
}
87 changes: 75 additions & 12 deletions src/Calculator/Views/GraphingCalculator/EquationInputArea.xaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ using namespace CalculatorApp::Common;
using namespace CalculatorApp::ViewModel;
using namespace CalculatorApp::Controls;
using namespace Platform;
using namespace Platform::Collections;
using namespace std;
using namespace Windows::Foundation;
using namespace Windows::System;
using namespace Windows::UI;
using namespace Windows::UI::ViewManagement;
Expand All @@ -24,11 +26,18 @@ namespace

EquationInputArea::EquationInputArea()
: m_lastLineColorIndex{ -1 }
, m_AvailableColors{ ref new Vector<SolidColorBrush ^>() }
, m_accessibilitySettings{ ref new AccessibilitySettings() }
{
InitializeComponent();
m_accessibilitySettings->HighContrastChanged +=
ref new TypedEventHandler<AccessibilitySettings ^, Object ^>(this, &EquationInputArea::OnHighContrastChanged);

ReloadAvailableColors(m_accessibilitySettings->HighContrast);

InitializeComponent();
}

void EquationInputArea::OnPropertyChanged(String^ propertyName)
void EquationInputArea::OnPropertyChanged(String ^ propertyName)
{
if (propertyName == EquationsPropertyName)
{
Expand All @@ -44,39 +53,44 @@ void EquationInputArea::OnEquationsPropertyChanged()
}
}

void EquationInputArea::AddEquationButton_Click(Object^ sender, RoutedEventArgs^ e)
void EquationInputArea::AddEquationButton_Click(Object ^ sender, RoutedEventArgs ^ e)
{
AddNewEquation();
}

void EquationInputArea::AddNewEquation()
{
auto eq = ref new EquationViewModel();

m_lastLineColorIndex = (m_lastLineColorIndex + 1) % AvailableColors->Size;

eq->LineColor = AvailableColors->GetAt(m_lastLineColorIndex);

Equations->Append(eq);
}

void EquationInputArea::InputTextBox_GotFocus(Object^ sender, RoutedEventArgs^ e)
void EquationInputArea::InputTextBox_GotFocus(Object ^ sender, RoutedEventArgs ^ e)
{
KeyboardShortcutManager::HonorShortcuts(false);
}

void EquationInputArea::InputTextBox_LostFocus(Object^ sender, RoutedEventArgs^ e)
void EquationInputArea::InputTextBox_LostFocus(Object ^ sender, RoutedEventArgs ^ e)
{
KeyboardShortcutManager::HonorShortcuts(true);
}

void EquationInputArea::InputTextBox_Submitted(Object ^ sender, RoutedEventArgs ^ e)
{
auto tb = static_cast<EquationTextBox^>(sender);
auto eq = static_cast<EquationViewModel^>(tb->DataContext);
auto tb = static_cast<EquationTextBox ^>(sender);
auto eq = static_cast<EquationViewModel ^>(tb->DataContext);
eq->Expression = tb->GetEquationText();
FocusManager::TryMoveFocus(::FocusNavigationDirection::Left);
}

void EquationInputArea::EquationTextBox_RemoveButtonClicked(Object^ sender, RoutedEventArgs^ e)
void EquationInputArea::EquationTextBox_RemoveButtonClicked(Object ^ sender, RoutedEventArgs ^ e)
{
auto tb = static_cast<EquationTextBox^>(sender);
auto eq = static_cast<EquationViewModel^>(tb->DataContext);
auto tb = static_cast<EquationTextBox ^>(sender);
auto eq = static_cast<EquationViewModel ^>(tb->DataContext);
unsigned int index;
if (Equations->IndexOf(eq, &index))
{
Expand All @@ -90,8 +104,57 @@ void EquationInputArea::EquationTextBoxLoaded(Object ^ sender, RoutedEventArgs ^
auto eq = static_cast<EquationViewModel ^>(tb->DataContext);

auto colorChooser = static_cast<EquationStylePanelControl ^>(tb->ColorChooserFlyout->Content);
colorChooser->AvailableColors = AvailableColors;
}

void EquationInputArea::OnHighContrastChanged(AccessibilitySettings ^ sender, Object ^ args)
{
ReloadAvailableColors(sender->HighContrast);
}

void EquationInputArea::ReloadAvailableColors(bool isHighContrast)
{
m_AvailableColors->Clear();

m_AvailableColors->Append(safe_cast<SolidColorBrush ^>(Application::Current->Resources->Lookup(L"EquationBrush1")));
m_AvailableColors->Append(safe_cast<SolidColorBrush ^>(Application::Current->Resources->Lookup(L"EquationBrush2")));
m_AvailableColors->Append(safe_cast<SolidColorBrush ^>(Application::Current->Resources->Lookup(L"EquationBrush3")));
m_AvailableColors->Append(safe_cast<SolidColorBrush ^>(Application::Current->Resources->Lookup(L"EquationBrush4")));

// If this is not high contrast, we have all 16 colors, otherwise we will restrict this to a subset of high contrast colors
if (!isHighContrast)
{
m_AvailableColors->Append(safe_cast<SolidColorBrush ^>(Application::Current->Resources->Lookup(L"EquationBrush5")));
m_AvailableColors->Append(safe_cast<SolidColorBrush ^>(Application::Current->Resources->Lookup(L"EquationBrush6")));
m_AvailableColors->Append(safe_cast<SolidColorBrush ^>(Application::Current->Resources->Lookup(L"EquationBrush7")));
m_AvailableColors->Append(safe_cast<SolidColorBrush ^>(Application::Current->Resources->Lookup(L"EquationBrush8")));
m_AvailableColors->Append(safe_cast<SolidColorBrush ^>(Application::Current->Resources->Lookup(L"EquationBrush9")));
m_AvailableColors->Append(safe_cast<SolidColorBrush ^>(Application::Current->Resources->Lookup(L"EquationBrush10")));
m_AvailableColors->Append(safe_cast<SolidColorBrush ^>(Application::Current->Resources->Lookup(L"EquationBrush11")));
m_AvailableColors->Append(safe_cast<SolidColorBrush ^>(Application::Current->Resources->Lookup(L"EquationBrush12")));
m_AvailableColors->Append(safe_cast<SolidColorBrush ^>(Application::Current->Resources->Lookup(L"EquationBrush13")));
m_AvailableColors->Append(safe_cast<SolidColorBrush ^>(Application::Current->Resources->Lookup(L"EquationBrush14")));
m_AvailableColors->Append(safe_cast<SolidColorBrush ^>(Application::Current->Resources->Lookup(L"EquationBrush15")));
m_AvailableColors->Append(safe_cast<SolidColorBrush ^>(Application::Current->Resources->Lookup(L"EquationBrush16")));
}

// If there are no equations to reload, quit early
if (Equations == nullptr || Equations->Size == 0)
{
return;
}

m_lastLineColorIndex = (m_lastLineColorIndex + 1) % colorChooser->AvailableColors->Size;
// Use a blank brush to clear out the color before setting it. This is needed because going
// from High Contrast White -> High Contrast Black, the high contrast colors seem to be equivalent,
// causing the change to not take place.
auto blankBrush = ref new SolidColorBrush();

eq->LineColor = colorChooser->AvailableColors->GetAt(m_lastLineColorIndex);
// Reassign colors for each equation
m_lastLineColorIndex = -1;
for (auto equationViewModel : Equations)
{
m_lastLineColorIndex = (m_lastLineColorIndex + 1) % AvailableColors->Size;
equationViewModel->LineColor = blankBrush;
equationViewModel->LineColor = AvailableColors->GetAt(m_lastLineColorIndex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ namespace CalculatorApp

OBSERVABLE_OBJECT_CALLBACK(OnPropertyChanged);
OBSERVABLE_PROPERTY_RW(Windows::Foundation::Collections::IObservableVector< ViewModel::EquationViewModel^ >^, Equations);
OBSERVABLE_PROPERTY_RW(Windows::Foundation::Collections::IObservableVector<Windows::UI::Xaml::Media::SolidColorBrush ^> ^, AvailableColors);


private:
void OnPropertyChanged(Platform::String^ propertyName);
Expand All @@ -28,7 +30,11 @@ namespace CalculatorApp
void InputTextBox_LostFocus(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
void InputTextBox_Submitted(Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e);

void OnHighContrastChanged(Windows::UI::ViewManagement::AccessibilitySettings ^ sender, Platform::Object ^ args);
void ReloadAvailableColors(bool isHighContrast);

private:
Windows::UI::ViewManagement::AccessibilitySettings ^ m_accessibilitySettings;
int m_lastLineColorIndex;
void EquationTextBox_RemoveButtonClicked(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
void EquationTextBoxLoaded(Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e);
Expand Down

0 comments on commit 433d54d

Please sign in to comment.