< Summary

Class:UIComponents.Scripts.Components.TooltipComponentView
Assembly:UIComponents
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/Tooltip/TooltipComponentView.cs
Covered lines:27
Uncovered lines:15
Coverable lines:42
Total lines:100
Line coverage:64.2% (27 of 42)
Covered branches:0
Total branches:0
Covered methods:7
Total methods:10
Method coverage:70% (7 of 10)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Awake()0%2.032080%
OnEnable()0%2.062075%
OnFocus()0%6200%
OnLoseFocus()0%2.062075%
OnDeselect(...)0%6200%
Show(...)0%2100%
RefreshControl()0%110100%
SetText(...)0%220100%
SetAutoAdaptModeActive(...)0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/Tooltip/TooltipComponentView.cs

#LineLine coverage
 1using TMPro;
 2using UIComponents.Scripts.Components.Tooltip;
 3using UnityEngine;
 4using UnityEngine.EventSystems;
 5
 6namespace UIComponents.Scripts.Components
 7{
 8    [SelectionBase, DisallowMultipleComponent]
 9    public class TooltipComponentView : BaseComponentView<TooltipComponentModel>, IDeselectHandler
 10    {
 11        private const int OFFSET = 16;
 12
 13        [SerializeField] private TMP_Text text;
 14        [SerializeField] private BaseComponentView activatorComponent;
 15
 16        private bool mouseIsOver;
 17
 18        private RectTransform rectTransform;
 7419        private RectTransform rect => rectTransform ??= GetComponent<RectTransform>();
 20
 21        public override void Awake()
 22        {
 6923            base.Awake();
 24
 6925            if (activatorComponent == null)
 026                return;
 27
 6928            activatorComponent.onFocused += isFocused =>
 29            {
 7030                if (isFocused)
 031                    Show();
 32                else
 7033                    Hide();
 7034            };
 6935        }
 36
 37        public override void OnEnable()
 38        {
 7039            base.OnEnable();
 40
 7041            if (EventSystem.current)
 042                EventSystem.current.SetSelectedGameObject(gameObject);
 7043        }
 44
 45        public override void OnFocus()
 46        {
 047            base.OnFocus();
 48
 049            if (EventSystem.current)
 050                EventSystem.current.SetSelectedGameObject(gameObject);
 051        }
 52
 53        public override void OnLoseFocus()
 54        {
 7055            base.OnLoseFocus();
 56
 7057            if (EventSystem.current)
 058                EventSystem.current.SetSelectedGameObject(gameObject);
 7059        }
 60
 61        public void OnDeselect(BaseEventData eventData)
 62        {
 063            if (!isFocused)
 064                Hide();
 065        }
 66
 67        public override void Show(bool instant = false)
 68        {
 069            gameObject.SetActive(true);
 070            base.Show(instant);
 071        }
 72
 73        public override void RefreshControl()
 74        {
 3775            SetText(model.message);
 3776            SetAutoAdaptModeActive(model.autoAdaptContainerSize);
 3777            AdjustTooltipSizeToText();
 78
 79            void AdjustTooltipSizeToText()
 80            {
 3781                if (!model.autoAdaptContainerSize)
 082                    return;
 83
 3784                rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, text.preferredWidth + (OFFSET * 2));
 3785                rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, text.preferredHeight + (OFFSET * 2));
 3786            }
 3787        }
 88
 89        private void SetText(string newText)
 90        {
 3791            model.message = newText;
 92
 3793            if (text)
 3794                text.text = newText;
 3795        }
 96
 97        private void SetAutoAdaptModeActive(bool isActive) =>
 3798            model.autoAdaptContainerSize = isActive;
 99    }
 100}