// ---------------------------------------------- @HoverBackground // --------------------------------------------------------------- /// <summary> /// Adds background hover capability that will not be lost like CSS:hover when programatically setting background color /// </summary> /// <example> /// <code> /// var originalColor = ColorUtil.FromHex("#BABABA"); /// var hoverBGColor = ColorUtil.FromHex("#2F569C"); /// /// label.HoverBackground(originalColor, hoverBGColor); /// </code> /// </example>> /// <param name="target">The element in which this function will be applied</param> /// <param name="original">The original color of the element being changed. Can be obtained and passed via 'visualElement.style.backgroundColor.value'</param> /// <param name="hoverColor">The color to fade to when element is hovered</param> /// <param name="condition">Create a condition to pass to this function. Example: bool Condition(VisualElement sRow) => selectedRow == packageListRow;</param> /// <param name="conditionElement">The element in which the optional condition will be evaluated. Ex. in the example of 'bool Condition(VisualElement sRow) => selectedRow == packageListRow;', the conditionalElement would be 'VisualElement selectedRow'</param> /// <param name="animate">Whether to animate the transition of the background color</param> public static void HoverBackground(this VisualElement target, StyleColor original, Color hoverColor, Func <VisualElement, bool> condition = null, VisualElement conditionElement = null, bool animate = false) { var mouseOver = new ValueAnimation <StyleValues>(); var mouseOut = new ValueAnimation <StyleValues>(); if (animate) { mouseOver = target.AnimateBackgroundColor(original.value, hoverColor, 250); mouseOver.KeepAlive(); mouseOut = target.AnimateBackgroundColor(hoverColor, original.value, 250); mouseOut.KeepAlive(); } target.RegisterCallback <MouseOverEvent>(evt => { if (condition != null && condition(conditionElement)) { return; } if (mouseOut.isRunning) { mouseOut.Stop(); } if (animate) { target.schedule.Execute(() => { mouseOver.Start(); }).StartingIn(50); } else { target.style.backgroundColor = hoverColor; } evt.StopPropagation(); }); target.RegisterCallback <MouseOutEvent>(evt => { if (condition != null && condition(conditionElement)) { return; } if (mouseOver.isRunning) { mouseOver.Stop(); } if (animate) { target.schedule.Execute(() => { mouseOut.Start(); }).StartingIn(50); } else { target.style.backgroundColor = original; } evt.StopPropagation(); }); }