示例#1
0
        void SetUpAddUSSMenu()
        {
            if (m_AddUSSMenu == null)
            {
                return;
            }

            m_AddUSSMenu.menu.MenuItems().Clear();

            if (m_PaneWindow.document.visualTreeAsset.IsEmpty())
            {
                m_AddUSSMenu.menu.AppendAction(
                    BuilderConstants.ExplorerStyleSheetsPanePlusMenuNoElementsMessage,
                    action => {},
                    action => DropdownMenuAction.Status.Disabled);
            }
            else
            {
                m_AddUSSMenu.menu.AppendAction(
                    BuilderConstants.ExplorerStyleSheetsPaneCreateNewUSSMenu,
                    action =>
                {
                    BuilderStyleSheetsUtilities.CreateNewUSSAsset(m_PaneWindow);
                });
                m_AddUSSMenu.menu.AppendAction(
                    BuilderConstants.ExplorerStyleSheetsPaneAddExistingUSSMenu,
                    action =>
                {
                    BuilderStyleSheetsUtilities.AddExistingUSSToAsset(m_PaneWindow);
                });
            }
        }
        public override void BuildElementContextualMenu(ContextualMenuPopulateEvent evt, VisualElement target)
        {
            base.BuildElementContextualMenu(evt, target);

            var documentElement = target.GetProperty(BuilderConstants.ElementLinkedDocumentVisualElementVEPropertyName) as VisualElement;

            var selectedStyleSheet        = documentElement?.GetStyleSheet();
            int selectedStyleSheetIndex   = selectedStyleSheet == null ? -1 : (int)documentElement.GetProperty(BuilderConstants.ElementLinkedStyleSheetIndexVEPropertyName);
            var isStyleSheet              = documentElement != null && BuilderSharedStyles.IsStyleSheetElement(documentElement);
            var styleSheetBelongsToParent = !string.IsNullOrEmpty(documentElement?.GetProperty(BuilderConstants.ExplorerItemLinkedUXMLFileName) as string);

            if (isStyleSheet)
            {
                evt.StopImmediatePropagation();
            }

            evt.menu.AppendSeparator();

            evt.menu.AppendAction(
                BuilderConstants.ExplorerStyleSheetsPaneCreateNewUSSMenu,
                a =>
            {
                BuilderStyleSheetsUtilities.CreateNewUSSAsset(paneWindow);
            },
                DropdownMenuAction.Status.Normal);

            evt.menu.AppendAction(
                BuilderConstants.ExplorerStyleSheetsPaneAddExistingUSSMenu,
                a =>
            {
                BuilderStyleSheetsUtilities.AddExistingUSSToAsset(paneWindow);
            },
                DropdownMenuAction.Status.Normal);

            evt.menu.AppendAction(
                BuilderConstants.ExplorerStyleSheetsPaneRemoveUSSMenu,
                a =>
            {
                BuilderStyleSheetsUtilities.RemoveUSSFromAsset(paneWindow, selection, documentElement);
            },
                isStyleSheet && !styleSheetBelongsToParent
                ? DropdownMenuAction.Status.Normal
                : DropdownMenuAction.Status.Disabled);

            evt.menu.AppendSeparator();

            evt.menu.AppendAction(
                BuilderConstants.ExplorerStyleSheetsPaneSetActiveUSS,
                a =>
            {
                selection.Select(null, documentElement);
                BuilderStyleSheetsUtilities.SetActiveUSS(selection, paneWindow, selectedStyleSheet);
            },
                isStyleSheet && !styleSheetBelongsToParent
                ? DropdownMenuAction.Status.Normal
                : DropdownMenuAction.Status.Disabled);
        }
示例#3
0
        StyleSheet GetOrCreateOrAddMainStyleSheet()
        {
            // Get StyleSheet.
            var mainStyleSheet = m_PaneWindow.document.activeStyleSheet;

            if (mainStyleSheet == null)
            {
                var option = BuilderDialogsUtility.DisplayDialogComplex(
                    BuilderConstants.ExtractInlineStylesNoUSSDialogTitle,
                    BuilderConstants.ExtractInlineStylesNoUSSDialogMessage,
                    BuilderConstants.ExtractInlineStylesNoUSSDialogNewUSSOption,
                    BuilderConstants.ExtractInlineStylesNoUSSDialogExistingUSSOption,
                    BuilderConstants.DialogCancelOption);

                switch (option)
                {
                // New
                case 0:
                    if (!BuilderStyleSheetsUtilities.CreateNewUSSAsset(m_PaneWindow))
                    {
                        return(null);
                    }
                    break;

                // Existing
                case 1:
                    if (!BuilderStyleSheetsUtilities.AddExistingUSSToAsset(m_PaneWindow))
                    {
                        return(null);
                    }
                    break;

                // Cancel
                case 2:
                    return(null);
                }

                mainStyleSheet = m_PaneWindow.document.activeStyleSheet;
            }
            return(mainStyleSheet);
        }
        void SetUpAddUSSMenu()
        {
            if (m_AddUSSMenu == null)
            {
                return;
            }

            m_AddUSSMenu.menu.MenuItems().Clear();

            {
                m_AddUSSMenu.menu.AppendAction(
                    BuilderConstants.ExplorerStyleSheetsPaneCreateNewUSSMenu,
                    action =>
                {
                    BuilderStyleSheetsUtilities.CreateNewUSSAsset(m_PaneWindow);
                });
                m_AddUSSMenu.menu.AppendAction(
                    BuilderConstants.ExplorerStyleSheetsPaneAddExistingUSSMenu,
                    action =>
                {
                    BuilderStyleSheetsUtilities.AddExistingUSSToAsset(m_PaneWindow);
                });
            }
        }
        void CreateNewSelector(StyleSheet styleSheet)
        {
            var newValue = m_NewSelectorTextField.text;

            if (newValue == BuilderConstants.ExplorerInExplorerNewClassSelectorInfoMessage)
            {
                return;
            }

            if (styleSheet == null)
            {
                if (BuilderStyleSheetsUtilities.CreateNewUSSAsset(m_PaneWindow))
                {
                    styleSheet = m_PaneWindow.document.firstStyleSheet;

                    // The EditorWindow will no longer have Focus after we show the
                    // Save Dialog so even though the New Selector field will appear
                    // focused, typing won't do anything. As such, it's better, in
                    // this one case to remove focus from this field so users know
                    // to re-focus it themselves before they can add more selectors.
                    m_NewSelectorTextField.value = string.Empty;
                    m_NewSelectorTextField.Blur();
                }
                else
                {
                    return;
                }
            }
            else
            {
                m_ShouldRefocusSelectorFieldOnBlur = true;
            }

            var newSelectorStr = newValue;

            if (newSelectorStr.StartsWith(BuilderConstants.UssSelectorClassNameSymbol))
            {
                newSelectorStr = BuilderConstants.UssSelectorClassNameSymbol + newSelectorStr.TrimStart(BuilderConstants.UssSelectorClassNameSymbol[0]);
            }

            if (string.IsNullOrEmpty(newSelectorStr))
            {
                return;
            }

            if (newSelectorStr.Length == 1 && (
                    newSelectorStr.StartsWith(BuilderConstants.UssSelectorClassNameSymbol) ||
                    newSelectorStr.StartsWith("-") ||
                    newSelectorStr.StartsWith("_")))
            {
                return;
            }

            if (!BuilderNameUtilities.styleSelectorRegex.IsMatch(newSelectorStr))
            {
                Builder.ShowWarning(BuilderConstants.StyleSelectorValidationSpacialCharacters);
                m_NewSelectorTextField.schedule.Execute(() =>
                {
                    m_NewSelectorTextField.SetValueWithoutNotify(newValue);
                    m_NewSelectorTextField.SelectAll();
                });
                return;
            }

            var selectorContainerElement = m_Viewport.styleSelectorElementContainer;
            var newComplexSelector       = BuilderSharedStyles.CreateNewSelector(selectorContainerElement, styleSheet, newSelectorStr);

            m_Selection.NotifyOfHierarchyChange();
            m_Selection.NotifyOfStylingChange();

            // Try to selected newly created selector.
            var newSelectorElement =
                m_Viewport.styleSelectorElementContainer.FindElement(
                    (e) => e.GetStyleComplexSelector() == newComplexSelector);

            if (newSelectorElement != null)
            {
                m_Selection.Select(null, newSelectorElement);
            }
        }
        void ExtractLocalStylesToNewClass(string className)
        {
            // Get StyleSheet.
            var mainStyleSheet = m_PaneWindow.document.activeStyleSheet;

            if (mainStyleSheet == null)
            {
                var option = BuilderDialogsUtility.DisplayDialogComplex(
                    BuilderConstants.ExtractInlineStylesNoUSSDialogTitle,
                    BuilderConstants.ExtractInlineStylesNoUSSDialogMessage,
                    BuilderConstants.ExtractInlineStylesNoUSSDialogNewUSSOption,
                    BuilderConstants.ExtractInlineStylesNoUSSDialogExistingUSSOption,
                    BuilderConstants.DialogCancelOption);

                switch (option)
                {
                // New
                case 0:
                    if (!BuilderStyleSheetsUtilities.CreateNewUSSAsset(m_PaneWindow))
                    {
                        return;
                    }
                    break;

                // Existing
                case 1:
                    if (!BuilderStyleSheetsUtilities.AddExistingUSSToAsset(m_PaneWindow))
                    {
                        return;
                    }
                    break;

                // Cancel
                case 2:
                    return;
                }

                mainStyleSheet = m_PaneWindow.document.activeStyleSheet;
            }

            PreAddStyleClass(className);

            // Create new selector in main StyleSheet.
            var selectorString       = BuilderConstants.UssSelectorClassNameSymbol + className;
            var selectorsRootElement = BuilderSharedStyles.GetSelectorContainerElement(m_Selection.documentElement);
            var newSelector          = BuilderSharedStyles.CreateNewSelector(selectorsRootElement, mainStyleSheet, selectorString);

            // Transfer all properties from inline styles rule to new selector.
            mainStyleSheet.TransferRulePropertiesToSelector(
                newSelector, m_Inspector.styleSheet, m_Inspector.currentRule);

            // Update VisualTreeAsset.
            BuilderAssetUtilities.AddStyleClassToElementInAsset(
                m_PaneWindow.document, currentVisualElement, className);

            // Overwrite Undo Message.
            Undo.RegisterCompleteObjectUndo(
                new Object[] { m_PaneWindow.document.visualTreeAsset, mainStyleSheet },
                BuilderConstants.CreateStyleClassUndoMessage);

            // We actually want to get the notification back and refresh ourselves.
            m_Selection.NotifyOfStylingChange(null);
            m_Selection.NotifyOfHierarchyChange(null, currentVisualElement);
        }
        public override void BuildElementContextualMenu(ContextualMenuPopulateEvent evt, VisualElement target)
        {
            base.BuildElementContextualMenu(evt, target);

            var documentElement = target.GetProperty(BuilderConstants.ElementLinkedDocumentVisualElementVEPropertyName) as VisualElement;

            var selectedStyleSheet        = documentElement?.GetStyleSheet();
            int selectedStyleSheetIndex   = selectedStyleSheet == null ? -1 : (int)documentElement.GetProperty(BuilderConstants.ElementLinkedStyleSheetIndexVEPropertyName);
            var isStyleSheet              = documentElement != null && BuilderSharedStyles.IsStyleSheetElement(documentElement);
            var styleSheetBelongsToParent = !string.IsNullOrEmpty(documentElement?.GetProperty(BuilderConstants.ExplorerItemLinkedUXMLFileName) as string);

            if (isStyleSheet)
            {
                evt.StopImmediatePropagation();
            }

            evt.menu.AppendSeparator();

            evt.menu.AppendAction(
                BuilderConstants.ExplorerStyleSheetsPaneCreateNewUSSMenu,
                a =>
            {
                BuilderStyleSheetsUtilities.CreateNewUSSAsset(paneWindow);
            },
#if UNITY_2019_4
                // Cannot add USS to an empty UXML because there's no root element to
                // containe the <Style> tag. This will problem will go away once
                // we support the root <Style> tag but...one problem at a time.
                !document.visualTreeAsset.IsEmpty()
                    ? DropdownMenuAction.Status.Normal
                    : DropdownMenuAction.Status.Disabled);
#else
                DropdownMenuAction.Status.Normal);
#endif

            evt.menu.AppendAction(
                BuilderConstants.ExplorerStyleSheetsPaneAddExistingUSSMenu,
                a =>
            {
                BuilderStyleSheetsUtilities.AddExistingUSSToAsset(paneWindow);
            },
#if UNITY_2019_4
                // Cannot add USS to an empty UXML because there's no root element to
                // containe the <Style> tag. This will problem will go away once
                // we support the root <Style> tag but...one problem at a time.
                !document.visualTreeAsset.IsEmpty()
                    ? DropdownMenuAction.Status.Normal
                    : DropdownMenuAction.Status.Disabled);
#else
                DropdownMenuAction.Status.Normal);
#endif

            evt.menu.AppendAction(
                BuilderConstants.ExplorerStyleSheetsPaneRemoveUSSMenu,
                a =>
            {
                BuilderStyleSheetsUtilities.RemoveUSSFromAsset(paneWindow, selectedStyleSheetIndex);
            },
                isStyleSheet && !styleSheetBelongsToParent
                    ? DropdownMenuAction.Status.Normal
                    : DropdownMenuAction.Status.Disabled);

            evt.menu.AppendSeparator();

            evt.menu.AppendAction(
                BuilderConstants.ExplorerStyleSheetsPaneSetActiveUSS,
                a =>
            {
                selection.Select(null, documentElement);
                BuilderStyleSheetsUtilities.SetActiveUSS(selection, paneWindow, selectedStyleSheet);
            },
                isStyleSheet && !styleSheetBelongsToParent
                    ? DropdownMenuAction.Status.Normal
                    : DropdownMenuAction.Status.Disabled);
        }