Example #1
0
 internal void UpdateEnabled()
 {
     if (UseFormsVsm)
     {
         var state = IsEnabled ? "FormsNormal" : "FormsDisabled";
         WVisualStateManager.GoToState(this, state, true);
     }
 }
Example #2
0
        static void FocusPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
        {
            // If we're modifying the properties related to the focus state of the control (e.g.,
            // ForegroundFocusBrush), the changes won't be reflected immediately because they are only applied
            // when the Microsoft.UI.Xaml.VisualStateManager moves to the "Focused" state. So we have to force a
            // "refresh" of the Focused state by going to that state again

            if (!(dependencyObject is Control control) || control.FocusState == FocusState.Unfocused)
            {
                return;
            }

            WVisualStateManager.GoToState(control, "Focused", false);
        }
Example #3
0
        protected override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            _rootGrid = GetTemplateChild("RootGrid") as Microsoft.UI.Xaml.Controls.Grid;
            if (_rootGrid != null)
            {
                var stateGroups = WVisualStateManager.GetVisualStateGroups(_rootGrid).ToList();
                _DeleteButtonVisibleStateGroups = stateGroups.SingleOrDefault(sg => sg.Name == "ButtonStates");
                if (_DeleteButtonVisibleStateGroups != null)
                {
                    _DeleteButtonVisibleState = _DeleteButtonVisibleStateGroups.States.SingleOrDefault(s => s.Name == "ButtonVisible");
                }
                UpdateClearButtonVisible();
            }

            _scrollViewer = GetTemplateChild("ContentElement") as ScrollViewer;
        }
Example #4
0
        /*
         * The below serves as a way to disable the button visually, rather than using IsEnabled. It's a hack
         * but should remain stable as long as the user doesn't change the WinRT Button template too much.
         *
         * The reason we're not using IsEnabled is that the buttons have a click radius that overlap about 40%
         * of the next button. This doesn't cause a problem until one button becomes disabled, then if you think
         * you're still hitting + (haven't noticed its disabled), you will actually hit -. This hack doesn't
         * completely solve the problem, but it drops the overlap to something like 20%. I haven't found the root
         * cause, so this will have to suffice for now.
         */

        void PsuedoEnable(Control control, ref VisualStateCache cache)
        {
            if (cache == null || VisualTreeHelper.GetChildrenCount(control) == 0)
            {
                return;
            }

            var rootElement = (FrameworkElement)VisualTreeHelper.GetChild(control, 0);

            IList <WVisualStateGroup> groups = WVisualStateManager.GetVisualStateGroups(rootElement);

            if (cache.FocusStates != null)
            {
                groups.Add(cache.FocusStates);
            }

            var commonStates = groups.FirstOrDefault(g => g.Name == "CommonStates");

            if (commonStates == null)
            {
                return;
            }

            if (cache.Normal != null)
            {
                commonStates.States.Insert(0, cache.Normal);                 // defensive
            }
            if (cache.Pressed != null)
            {
                commonStates.States.Add(cache.Pressed);
            }
            if (cache.PointerOver != null)
            {
                commonStates.States.Add(cache.PointerOver);
            }

            WVisualStateManager.GoToState(control, "Normal", true);

            cache = null;
        }
Example #5
0
        void UpdateOnColor()
        {
            if (Control == null)
            {
                return;
            }

            var grid = Control.GetFirstDescendant <WGrid>();

            if (grid == null)
            {
                return;
            }

            var groups = WVisualStateManager.GetVisualStateGroups(grid);

            foreach (var group in groups)
            {
                if (group.Name != ToggleSwitchCommonStates)
                {
                    continue;
                }

                foreach (var state in group.States)
                {
                    if (state.Name != ToggleSwitchPointerOver)
                    {
                        continue;
                    }

                    foreach (var timeline in state.Storyboard.Children.OfType <ObjectAnimationUsingKeyFrames>())
                    {
                        var property = Storyboard.GetTargetProperty(timeline);
                        var target   = Storyboard.GetTargetName(timeline);

                        if (target == ToggleSwitchKnobBounds && property == ToggleSwitchFillMode)
                        {
                            var frame = timeline.KeyFrames.FirstOrDefault();

                            if (frame != null)
                            {
                                if (_originalOnHoverColor == null)
                                {
                                    if (frame.Value is WColor color)
                                    {
                                        _originalOnHoverColor = color;
                                    }

                                    if (frame.Value is SolidColorBrush solidColorBrush)
                                    {
                                        _originalOnHoverColor = solidColorBrush;
                                    }
                                }

                                if (!Element.OnColor.IsDefault())
                                {
                                    frame.Value = new WSolidColorBrush(Element.OnColor.ToWindowsColor())
                                    {
                                        Opacity = _originalOnHoverColor is WSolidColorBrush originalOnHoverBrush ? originalOnHoverBrush.Opacity : 1
                                    };
                                }
                                else
                                {
                                    frame.Value = _originalOnHoverColor;
                                }
                            }
                            break;
                        }
                    }
                }
            }

            var rect = Control.GetDescendantsByName <WRectangle>(ToggleSwitchKnobBounds).FirstOrDefault();

            if (rect != null)
            {
                if (_originalOnColorBrush == null)
                {
                    _originalOnColorBrush = rect.Fill;
                }

                if (!Element.OnColor.IsDefault())
                {
                    rect.Fill = new WSolidColorBrush(Element.OnColor.ToWindowsColor());
                }
                else
                {
                    rect.Fill = _originalOnColorBrush;
                }
            }
        }
Example #6
0
        void UpdateThumbColor()
        {
            if (Control == null)
            {
                return;
            }

            var grid = Control.GetFirstDescendant <WGrid>();

            if (grid == null)
            {
                return;
            }

            var groups = WVisualStateManager.GetVisualStateGroups(grid);

            foreach (var group in groups)
            {
                if (group.Name != ToggleSwitchCommonStates)
                {
                    continue;
                }

                foreach (var state in group.States)
                {
                    if (state.Name != ToggleSwitchPointerOver)
                    {
                        continue;
                    }

                    foreach (var timeline in state.Storyboard.Children.OfType <ObjectAnimationUsingKeyFrames>())
                    {
                        var property = Storyboard.GetTargetProperty(timeline);
                        var target   = Storyboard.GetTargetName(timeline);

                        if ((target == ToggleSwitchKnobOn) && (property == ToggleSwitchFillMode))
                        {
                            var frame = timeline.KeyFrames.FirstOrDefault();

                            if (frame != null)
                            {
                                if (_originalThumbOnBrush == null)
                                {
                                    if (frame.Value is Windows.UI.Color color)
                                    {
                                        _originalOnColorBrush = new WSolidColorBrush(color);
                                    }

                                    if (frame.Value is WBrush brush)
                                    {
                                        _originalThumbOnBrush = brush;
                                    }
                                }

                                if (!Element.ThumbColor.IsDefault())
                                {
                                    var brush = Maui.ColorExtensions.ToNative(Element.ThumbColor);
                                    brush.Opacity = _originalThumbOnBrush.Opacity;
                                    frame.Value   = brush;
                                }
                                else
                                {
                                    frame.Value = _originalThumbOnBrush;
                                }
                            }
                            break;
                        }
                    }
                }
            }

            if (grid.FindName(ToggleSwitchKnobOn) is WEllipse thumb)
            {
                if (_originalThumbOnBrush == null)
                {
                    _originalThumbOnBrush = thumb.Fill;
                }

                if (!Element.ThumbColor.IsDefault())
                {
                    thumb.Fill = Maui.ColorExtensions.ToNative(Element.ThumbColor);
                }
                else
                {
                    thumb.Fill = _originalThumbOnBrush;
                }
            }
        }
Example #7
0
        VisualStateCache PseudoDisable(Control control)
        {
            if (VisualTreeHelper.GetChildrenCount(control) == 0)
            {
                control.ApplyTemplate();
            }

            WVisualStateManager.GoToState(control, "Disabled", true);

            var rootElement = (FrameworkElement)VisualTreeHelper.GetChild(control, 0);

            var cache = new VisualStateCache();
            IList <WVisualStateGroup> groups = WVisualStateManager.GetVisualStateGroups(rootElement);

            WVisualStateGroup common = null;

            foreach (var group in groups)
            {
                if (group.Name == "CommonStates")
                {
                    common = group;
                }
                else if (group.Name == "FocusStates")
                {
                    cache.FocusStates = group;
                }
                else if (cache.FocusStates != null && common != null)
                {
                    break;
                }
            }

            if (cache.FocusStates != null)
            {
                groups.Remove(cache.FocusStates);
            }

            if (common != null)
            {
                foreach (WVisualState state in common.States)
                {
                    if (state.Name == "Normal")
                    {
                        cache.Normal = state;
                    }
                    else if (state.Name == "Pressed")
                    {
                        cache.Pressed = state;
                    }
                    else if (state.Name == "PointerOver")
                    {
                        cache.PointerOver = state;
                    }
                }

                if (cache.Normal != null)
                {
                    common.States.Remove(cache.Normal);
                }
                if (cache.Pressed != null)
                {
                    common.States.Remove(cache.Pressed);
                }
                if (cache.PointerOver != null)
                {
                    common.States.Remove(cache.PointerOver);
                }
            }

            return(cache);
        }