public void ChangeCanvasTheme(BuilderDocument.CanvasTheme theme, ThemeStyleSheet themeStyleSheet = null)
        {
            ApplyCanvasTheme(m_Viewport.sharedStylesAndDocumentElement, theme, themeStyleSheet);
            ApplyCanvasTheme(m_Viewport.documentRootElement, theme, themeStyleSheet);
            ApplyCanvasBackground(m_Viewport.canvas.defaultBackgroundElement, theme, themeStyleSheet);
            ApplyCanvasTheme(m_TooltipPreview, theme, themeStyleSheet);
            ApplyCanvasBackground(m_TooltipPreview, theme, themeStyleSheet);

            document.ChangeDocumentTheme(m_Viewport.documentRootElement, theme, themeStyleSheet);
            m_Inspector?.selection.NotifyOfStylingChange(null, null, BuilderStylingChangeType.RefreshOnly);
        }
Exemple #2
0
        public void ChangeDocumentTheme(VisualElement documentElement, CanvasTheme canvasTheme, ThemeStyleSheet themeSheet)
        {
            m_CurrentCanvasTheme           = canvasTheme;
            m_CurrentCanvasThemeStyleSheet = themeSheet;

            if (themeSheet)
            {
                themeSheet.isDefaultStyleSheet = true;
            }
            RefreshStyle(documentElement);
        }
        void ApplyCanvasBackground(VisualElement element, BuilderDocument.CanvasTheme theme, ThemeStyleSheet themeStyleSheet)
        {
            if (element == null)
            {
                return;
            }

            element.RemoveFromClassList(BuilderConstants.CanvasContainerDarkStyleClassName);
            element.RemoveFromClassList(BuilderConstants.CanvasContainerLightStyleClassName);
            element.RemoveFromClassList(BuilderConstants.CanvasContainerRuntimeStyleClassName);

            switch (theme)
            {
            case BuilderDocument.CanvasTheme.Dark:
                element.AddToClassList(BuilderConstants.CanvasContainerDarkStyleClassName);
                break;

            case BuilderDocument.CanvasTheme.Light:
                element.AddToClassList(BuilderConstants.CanvasContainerLightStyleClassName);
                break;

            case BuilderDocument.CanvasTheme.Runtime:
                element.AddToClassList(BuilderConstants.CanvasContainerRuntimeStyleClassName);
                break;

            case BuilderDocument.CanvasTheme.Default:
                string defaultClass = EditorGUIUtility.isProSkin
                        ? BuilderConstants.CanvasContainerDarkStyleClassName
                        : BuilderConstants.CanvasContainerLightStyleClassName;
                element.AddToClassList(defaultClass);
                break;

            case BuilderDocument.CanvasTheme.Custom:
                element.AddToClassList(BuilderConstants.CanvasContainerRuntimeStyleClassName);
                break;
            }
        }
        void ApplyCanvasTheme(VisualElement element, BuilderDocument.CanvasTheme theme, ThemeStyleSheet customThemeSheet)
        {
            if (element == null)
            {
                return;
            }

            // Remove any null stylesheet. This may occur if an used theme has been deleted.
            // This should be handle by ui toolkit
            var i = 0;

            if (element.styleSheetList != null)
            {
                while (i < element.styleSheetList.Count)
                {
                    var sheet = element.styleSheetList[i];
                    if (sheet == null)
                    {
                        element.styleSheetList?.Remove(sheet);
                        if (element.styleSheetList.Count == 0)
                        {
                            element.styleSheetList = null;
                            break;
                        }
                    }
                    else
                    {
                        i++;
                    }
                }
            }

            // Should remove the previous custom theme stylesheet
            if (m_LastCustomTheme)
            {
                element.styleSheets.Remove(m_LastCustomTheme);
                m_LastCustomTheme = null;
            }
            // We verify whether the styles are loaded beforehand because calling GetCommonXXXStyleSheet() will load them unecessarily in this case
            if (UIElementsEditorUtility.IsCommonDarkStyleSheetLoaded())
            {
                element.styleSheets.Remove(UIElementsEditorUtility.GetCommonDarkStyleSheet());
            }
            if (UIElementsEditorUtility.IsCommonLightStyleSheetLoaded())
            {
                element.styleSheets.Remove(UIElementsEditorUtility.GetCommonLightStyleSheet());
            }
            m_Viewport.canvas.defaultBackgroundElement.style.display = DisplayStyle.Flex;

            StyleSheet themeStyleSheet = null;

            m_Viewport.canvas.checkerboardBackgroundElement.style.display = DisplayStyle.None;

            switch (theme)
            {
            case BuilderDocument.CanvasTheme.Dark:
                themeStyleSheet = UIElementsEditorUtility.GetCommonDarkStyleSheet();
                break;

            case BuilderDocument.CanvasTheme.Light:
                themeStyleSheet = UIElementsEditorUtility.GetCommonLightStyleSheet();
                break;

            case BuilderDocument.CanvasTheme.Default:
                themeStyleSheet = EditorGUIUtility.isProSkin ? UIElementsEditorUtility.GetCommonDarkStyleSheet() : UIElementsEditorUtility.GetCommonLightStyleSheet();
                break;

            case BuilderDocument.CanvasTheme.Custom:
                m_Viewport.canvas.defaultBackgroundElement.style.display      = DisplayStyle.None;
                m_Viewport.canvas.checkerboardBackgroundElement.style.display = DisplayStyle.Flex;
                m_LastCustomTheme = customThemeSheet;
                themeStyleSheet   = customThemeSheet;
                break;

            default:
                break;
            }

            if (themeStyleSheet != null)
            {
                element.styleSheets.Add(themeStyleSheet);
            }

            element.SetProperty(BuilderConstants.ElementLinkedActiveThemeStyleSheetVEPropertyName, themeStyleSheet);
        }