|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +#include "pch.h" |
| 5 | + |
| 6 | +#include "ResourceBrushUtils.h" |
| 7 | +#include "StandardControlResourceKeyNames.h" |
| 8 | + |
| 9 | +#include <winrt/Windows.UI.Xaml.h> |
| 10 | + |
| 11 | +namespace winrt { |
| 12 | +using namespace Windows::UI::Xaml; |
| 13 | +using namespace Windows::UI::Xaml::Controls; |
| 14 | +} // namespace winrt |
| 15 | + |
| 16 | +namespace react { |
| 17 | +namespace uwp { |
| 18 | + |
| 19 | +void UpdateResourceBrush( |
| 20 | + const winrt::FrameworkElement &element, |
| 21 | + const std::wstring &resourceName, |
| 22 | + const winrt::Brush brush) { |
| 23 | + const auto resources = element.Resources(); |
| 24 | + if (resources != nullptr) { |
| 25 | + if (brush != nullptr) { |
| 26 | + resources.Insert(winrt::box_value(resourceName), brush); |
| 27 | + } else { |
| 28 | + resources.Remove(winrt::box_value(resourceName)); |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +void UpdateTextControlBackgroundResourceBrushes(const winrt::FrameworkElement &element, const winrt::Brush brush) { |
| 34 | + UpdateResourceBrush(element, c_textControlBackground, brush); |
| 35 | + UpdateResourceBrush(element, c_textControlBackgroundPointerOver, brush); |
| 36 | + UpdateResourceBrush(element, c_textControlBackgroundFocused, brush); |
| 37 | + UpdateResourceBrush(element, c_textControlBackgroundDisabled, brush); |
| 38 | + |
| 39 | + UpdateResourceBrush(element, c_textControlButtonForegroundPressed, brush); |
| 40 | +} |
| 41 | + |
| 42 | +void UpdateTextControlForegroundResourceBrushes(const winrt::FrameworkElement element, const winrt::Brush brush) { |
| 43 | + UpdateResourceBrush(element, c_textControlForeground, brush); |
| 44 | + UpdateResourceBrush(element, c_textControlForegroundPointerOver, brush); |
| 45 | + UpdateResourceBrush(element, c_textControlForegroundFocused, brush); |
| 46 | + UpdateResourceBrush(element, c_textControlForegroundDisabled, brush); |
| 47 | + |
| 48 | + UpdateResourceBrush(element, c_textControlButtonForeground, brush); |
| 49 | + UpdateResourceBrush(element, c_textControlButtonForegroundPointerOver, brush); |
| 50 | + UpdateResourceBrush(element, c_textControlButtonBackgroundPressed, brush); |
| 51 | +} |
| 52 | + |
| 53 | +void UpdateTextControlBorderResourceBrushes(const winrt::FrameworkElement &element, const winrt::Brush brush) { |
| 54 | + UpdateResourceBrush(element, c_textControlBorderBrush, brush); |
| 55 | + UpdateResourceBrush(element, c_textControlBorderBrushPointerOver, brush); |
| 56 | + UpdateResourceBrush(element, c_textControlBorderBrushFocused, brush); |
| 57 | + UpdateResourceBrush(element, c_textControlBorderBrushDisabled, brush); |
| 58 | +} |
| 59 | + |
| 60 | +void UpdateToggleSwitchBorderResourceBrushes(const winrt::ToggleSwitch &toggleSwitch, const winrt::Brush brush) { |
| 61 | + UpdateResourceBrush(toggleSwitch, c_toggleSwitchStrokeOff, brush); |
| 62 | + UpdateResourceBrush(toggleSwitch, c_toggleSwitchStrokeOffPointerOver, brush); |
| 63 | + UpdateResourceBrush(toggleSwitch, c_toggleSwitchStrokeOffPressed, brush); |
| 64 | + UpdateResourceBrush(toggleSwitch, c_toggleSwitchStrokeOffDisabled, brush); |
| 65 | + UpdateResourceBrush(toggleSwitch, c_toggleSwitchStrokeOn, brush); |
| 66 | + UpdateResourceBrush(toggleSwitch, c_toggleSwitchStrokeOnPointerOver, brush); |
| 67 | + UpdateResourceBrush(toggleSwitch, c_toggleSwitchStrokeOnPressed, brush); |
| 68 | + UpdateResourceBrush(toggleSwitch, c_toggleSwitchStrokeOnDisabled, brush); |
| 69 | +} |
| 70 | + |
| 71 | +void UpdateToggleSwitchThumbResourceBrushes(const winrt::ToggleSwitch &toggleSwitch, const winrt::Brush thumbBrush) { |
| 72 | + UpdateResourceBrush(toggleSwitch, c_toggleSwitchKnobFillOff, thumbBrush); |
| 73 | + UpdateResourceBrush(toggleSwitch, c_toggleSwitchKnobFillOffPointerOver, thumbBrush); |
| 74 | + UpdateResourceBrush(toggleSwitch, c_toggleSwitchKnobFillOffPressed, thumbBrush); |
| 75 | + UpdateResourceBrush(toggleSwitch, c_toggleSwitchKnobFillOffDisabled, thumbBrush); |
| 76 | + UpdateResourceBrush(toggleSwitch, c_toggleSwitchKnobFillOn, thumbBrush); |
| 77 | + UpdateResourceBrush(toggleSwitch, c_toggleSwitchKnobFillOnPointerOver, thumbBrush); |
| 78 | + UpdateResourceBrush(toggleSwitch, c_toggleSwitchKnobFillOnPressed, thumbBrush); |
| 79 | + UpdateResourceBrush(toggleSwitch, c_toggleSwitchKnobFillOnDisabled, thumbBrush); |
| 80 | +} |
| 81 | + |
| 82 | +void UpdateToggleSwitchTrackResourceBrushes( |
| 83 | + const winrt::ToggleSwitch &toggleSwitch, |
| 84 | + const winrt::Brush onTrackBrush, |
| 85 | + const winrt::Brush offTrackBrush) { |
| 86 | + UpdateResourceBrush(toggleSwitch, c_toggleSwitchFillOn, onTrackBrush); |
| 87 | + UpdateResourceBrush(toggleSwitch, c_toggleSwitchFillOnPointerOver, onTrackBrush); |
| 88 | + UpdateResourceBrush(toggleSwitch, c_toggleSwitchFillOnPressed, onTrackBrush); |
| 89 | + UpdateResourceBrush(toggleSwitch, c_toggleSwitchFillOnDisabled, onTrackBrush); |
| 90 | + |
| 91 | + UpdateResourceBrush(toggleSwitch, c_toggleSwitchFillOff, offTrackBrush); |
| 92 | + UpdateResourceBrush(toggleSwitch, c_toggleSwitchFillOffPointerOver, offTrackBrush); |
| 93 | + UpdateResourceBrush(toggleSwitch, c_toggleSwitchFillOffPressed, offTrackBrush); |
| 94 | + UpdateResourceBrush(toggleSwitch, c_toggleSwitchFillOffDisabled, offTrackBrush); |
| 95 | +} |
| 96 | + |
| 97 | +bool IsObjectATextControl(const winrt::DependencyObject &object) { |
| 98 | + return object.try_as<winrt::TextBox>() != nullptr || object.try_as<winrt::PasswordBox>() != nullptr || |
| 99 | + object.try_as<winrt::RichEditBox>() != nullptr || object.try_as<winrt::AutoSuggestBox>() != nullptr; |
| 100 | +} |
| 101 | + |
| 102 | +void UpdateControlBackgroundResourceBrushes( |
| 103 | + const winrt::Windows::UI::Xaml::FrameworkElement &element, |
| 104 | + const winrt::Media::Brush brush) { |
| 105 | + if (IsObjectATextControl(element)) { |
| 106 | + UpdateTextControlBackgroundResourceBrushes(element, brush); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +void UpdateControlForegroundResourceBrushes( |
| 111 | + const winrt::Windows::UI::Xaml::DependencyObject object, |
| 112 | + const winrt::Media::Brush brush) { |
| 113 | + if (IsObjectATextControl(object)) { |
| 114 | + const auto element = object.try_as<winrt::FrameworkElement>(); |
| 115 | + UpdateTextControlForegroundResourceBrushes(element, brush); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +void UpdateControlBorderResourceBrushes( |
| 120 | + const winrt::Windows::UI::Xaml::FrameworkElement &element, |
| 121 | + const winrt::Media::Brush brush) { |
| 122 | + if (IsObjectATextControl(element)) { |
| 123 | + UpdateTextControlBorderResourceBrushes(element, brush); |
| 124 | + } else if (const auto toggleSwitch = element.try_as<winrt::ToggleSwitch>()) { |
| 125 | + UpdateToggleSwitchBorderResourceBrushes(toggleSwitch, brush); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +} // namespace uwp |
| 130 | +} // namespace react |
0 commit comments