public bool SaveNewDocument(
            VisualElement documentRootElement, bool isSaveAs,
            out bool needsFullRefresh,
            string manualUxmlPath = null)
        {
            needsFullRefresh = false;

            ClearUndo();

            // Re-use or ask the user for the UXML path.
            var newUxmlPath = uxmlPath;

            if (string.IsNullOrEmpty(newUxmlPath) || isSaveAs)
            {
                if (!string.IsNullOrEmpty(manualUxmlPath))
                {
                    newUxmlPath = manualUxmlPath;
                }
                else
                {
                    newUxmlPath = BuilderDialogsUtility.DisplaySaveFileDialog("Save UXML", null, null, "uxml");
                    if (newUxmlPath == null) // User cancelled the save dialog.
                    {
                        return(false);
                    }
                }
            }

            // Save USS files.
            foreach (var openUSSFile in m_OpenUSSFiles)
            {
                openUSSFile.SaveToDisk(visualTreeAsset);
            }

            // Save UXML files
            // Saving all open UXML files to ensure references correct upon changes in child documents.
            foreach (var openUXMLFile in openUXMLFiles)
            {
                openUXMLFile.PreSaveSyncBackup();
            }

            WriteUXMLToFile(newUxmlPath);

            // Once we wrote all the files to disk, we refresh the DB and reload
            // the files from the AssetDatabase.
            m_DocumentBeingSavedExplicitly = true;
            try
            {
                AssetDatabase.Refresh();
            }
            finally
            {
                m_DocumentBeingSavedExplicitly = false;
            }

            // Check if any USS assets have changed reload them.
            foreach (var openUSSFile in m_OpenUSSFiles)
            {
                needsFullRefresh |= openUSSFile.PostSaveToDiskChecksAndFixes();
            }

            // Check if any UXML assets have changed and reload them.
            // Saving all open UXML files to ensure references correct upon changes in child subdocuments.
            foreach (var openUXMLFile in openUXMLFiles)
            {
                needsFullRefresh |= openUXMLFile.PostSaveToDiskChecksAndFixes(this == openUXMLFile ? newUxmlPath : null, needsFullRefresh);
            }

            if (needsFullRefresh)
            {
                // Copy previous document settings.
                if (m_Settings != null)
                {
                    m_Settings.UxmlGuid = AssetDatabase.AssetPathToGUID(newUxmlPath);
                    m_Settings.UxmlPath = newUxmlPath;
                    m_Settings.SaveSettingsToDisk();
                }

                // Reset asset name.
                m_VisualTreeAsset.name          = Path.GetFileNameWithoutExtension(newUxmlPath);
                m_OpenendVisualTreeAssetOldPath = newUxmlPath;

                if (documentRootElement != null)
                {
                    ReloadDocumentToCanvas(documentRootElement);
                }
            }

            hasUnsavedChanges = false;

            return(true);
        }
        public bool SaveNewDocument(
            VisualElement documentRootElement, bool isSaveAs,
            out bool needsFullRefresh,
            string manualUxmlPath = null)
        {
            needsFullRefresh = false;

            ClearUndo();

            // Re-use or ask the user for the UXML path.
            var newUxmlPath = uxmlPath;

            if (string.IsNullOrEmpty(newUxmlPath) || isSaveAs)
            {
                if (!string.IsNullOrEmpty(manualUxmlPath))
                {
                    newUxmlPath = manualUxmlPath;
                }
                else
                {
                    newUxmlPath = BuilderDialogsUtility.DisplaySaveFileDialog("Save UXML", null, null, "uxml");
                    if (newUxmlPath == null) // User cancelled the save dialog.
                    {
                        return(false);
                    }
                }
            }

            // Save USS files.
            foreach (var openUSSFile in m_OpenUSSFiles)
            {
                openUSSFile.SaveToDisk(visualTreeAsset);
            }

            { // Save UXML file.
                // Need to save a backup before the AssetDatabase.Refresh().
                if (m_VisualTreeAssetBackup == null)
                {
                    m_VisualTreeAssetBackup = m_VisualTreeAsset.DeepCopy();
                }
                else
                {
                    m_VisualTreeAsset.DeepOverwrite(m_VisualTreeAssetBackup);
                }
                WriteUXMLToFile(newUxmlPath);
            }

            // Once we wrote all the files to disk, we refresh the DB and reload
            // the files from the AssetDatabase.
            m_DocumentBeingSavedExplicitly = true;
            try
            {
                AssetDatabase.Refresh();
            }
            finally
            {
                m_DocumentBeingSavedExplicitly = false;
            }

            // Check if any USS assets have changed reload them.
            foreach (var openUSSFile in m_OpenUSSFiles)
            {
                needsFullRefresh |= openUSSFile.PostSaveToDiskChecksAndFixes();
            }
            { // Check if the UXML asset has changed and reload it.
                m_VisualTreeAsset = AssetDatabase.LoadAssetAtPath <VisualTreeAsset>(newUxmlPath);
                needsFullRefresh |= m_VisualTreeAsset != m_VisualTreeAssetBackup;
            }

            if (needsFullRefresh)
            {
                // Copy previous document settings.
                if (m_Settings != null)
                {
                    m_Settings.UxmlGuid = AssetDatabase.AssetPathToGUID(newUxmlPath);
                    m_Settings.UxmlPath = newUxmlPath;
                    m_Settings.SaveSettingsToDisk();
                }

                { // Fix up UXML asset.
                    // To get all the selection markers into the new assets.
                    m_VisualTreeAssetBackup.DeepOverwrite(m_VisualTreeAsset);

                    // Reset asset name.
                    m_VisualTreeAsset.name = Path.GetFileNameWithoutExtension(newUxmlPath);

                    m_VisualTreeAsset.ConvertAllAssetReferencesToPaths();
                    m_OpenendVisualTreeAssetOldPath = newUxmlPath;
                }

                if (documentRootElement != null)
                {
                    ReloadDocumentToCanvas(documentRootElement);
                }
            }

            hasUnsavedChanges = false;

            return(true);
        }