private void UpdateFallbackBrush() { if (_target.TryGetTarget(out var target)) { if (_micaController.Value == null) { // When not using mica, use the theme and high contrast states to determine the fallback color. ElementTheme GetTheme() { // See other IsRS3OrHigher usage for comment explaining why the version check and QI. if (SharedHelpers.IsRS3OrHigher()) { if (target is FrameworkElement targetTheme) { return(targetTheme.ActualTheme); } } var value = _uiSettings.GetColorValue(UIColorType.Background); if (value.B == 0) { return(ElementTheme.Dark); } return(ElementTheme.Light); } var theme = GetTheme(); Color GetColor() { if (_isHighContrast) { return(_uiSettings.GetColorValue(UIColorType.Background)); } if (theme == ElementTheme.Dark) { return(MicaController.DarkThemeColor); } else { return(MicaController.LightThemeColor); } } var color = GetColor(); target.Background = new SolidColorBrush(color); } else { // When Mica is involved, use transparent for the background (this is so that the hit testing // behavior is consistent with/without the material). target.Background = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0)); } } }
public BackdropMaterialState(Control target) { _dispatcherHelper = new DispatcherHelper(this); _target = new WeakReference <Control>(target); // Track whether we're connected and update the number of connected BackdropMaterial on this thread. _connectedBrushCount.Value++; CreateOrDestroyMicaController(); // Normally QI would be fine, but .NET is lying about implementing this interface (e.g. C# TestFrame derives from Frame and this QI // returns success even on RS2, but it's not implemented by XAML until RS3). if (SharedHelpers.IsRS3OrHigher()) { if (target is FrameworkElement targetThemeChanged) { void OnActualThemeChanged(FrameworkElement sender, object args) { UpdateFallbackBrush(); } targetThemeChanged.ActualThemeChanged += OnActualThemeChanged; _themeChangedRevoker = Disposable.Create(() => targetThemeChanged.ActualThemeChanged -= OnActualThemeChanged); } } void OnColorValuesChanged(UISettings uiSettings, object args) { _dispatcherHelper.RunAsync(() => UpdateFallbackBrush()); } _uiSettings.ColorValuesChanged += OnColorValuesChanged; _colorValuesChangedRevoker = Disposable.Create(() => _uiSettings.ColorValuesChanged -= OnColorValuesChanged); // Listen for High Contrast changes var accessibilitySettings = new AccessibilitySettings(); _isHighContrast = accessibilitySettings.HighContrast; void OnHighContrastChanged(AccessibilitySettings sender, object args) { _dispatcherHelper.RunAsync(() => { _isHighContrast = accessibilitySettings.HighContrast; UpdateFallbackBrush(); }); } accessibilitySettings.HighContrastChanged += OnHighContrastChanged; _highContrastChangedRevoker = Disposable.Create(() => accessibilitySettings.HighContrastChanged -= OnHighContrastChanged); UpdateFallbackBrush(); }
// When focus comes from outside the RadioButtons control we will put focus on the selected radio button. private void OnGettingFocus(object sender, GettingFocusEventArgs args) { var repeater = m_repeater; if (repeater != null) { var inputDevice = args.InputDevice; if (inputDevice == FocusInputDeviceKind.Keyboard) { // If focus is coming from outside the repeater, put focus on the selected item. var oldFocusedElement = args.OldFocusedElement; if (oldFocusedElement == null || repeater != VisualTreeHelper.GetParent(oldFocusedElement)) { var selectedItem = repeater.TryGetElement(m_selectedIndex); if (selectedItem != null) { var argsAsIGettingFocusEventArgs2 = args as GettingFocusEventArgs; if (argsAsIGettingFocusEventArgs2 != null) { if (args.TrySetNewFocusedElement(selectedItem)) { args.Handled = true; } } } } // Focus was already in the repeater: in On RS3+ Selection follows focus unless control is held down. else if (SharedHelpers.IsRS3OrHigher() && (Windows.UI.Xaml.Window.Current.CoreWindow.GetKeyState(VirtualKey.Control) & CoreVirtualKeyStates.Down) != CoreVirtualKeyStates.Down) { var newFocusedElementAsUIE = args.NewFocusedElement as UIElement; if (newFocusedElementAsUIE != null) { Select(repeater.GetElementIndex(newFocusedElementAsUIE)); args.Handled = true; } } } } }