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); }
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 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); }