From 6167f92db3e6fcb622fc6b7d37e141814431c4a8 Mon Sep 17 00:00:00 2001 From: "Aaron (Qilong)" Date: Sun, 21 Jan 2024 22:23:05 -0500 Subject: [PATCH] Open Template UI change --- src/DynamoCore/Configuration/IPathResolver.cs | 5 ++ src/DynamoCore/Configuration/PathManager.cs | 49 +++++++++++++++- .../Properties/Resources.Designer.cs | 18 ++++++ .../Properties/Resources.en-US.resx | 8 ++- src/DynamoCoreWpf/Properties/Resources.resx | 6 ++ .../ViewModels/Core/DynamoViewModel.cs | 56 +++++++++++++++++++ .../Core/DynamoViewModelDelegateCommands.cs | 3 + src/DynamoCoreWpf/Views/Core/DynamoView.xaml | 17 +++++- .../Views/Core/DynamoView.xaml.cs | 2 +- 9 files changed, 158 insertions(+), 6 deletions(-) diff --git a/src/DynamoCore/Configuration/IPathResolver.cs b/src/DynamoCore/Configuration/IPathResolver.cs index 5273787238a..a2d7b854bbd 100644 --- a/src/DynamoCore/Configuration/IPathResolver.cs +++ b/src/DynamoCore/Configuration/IPathResolver.cs @@ -156,6 +156,11 @@ public interface IPathManager /// string SamplesDirectory { get; } + /// + /// The root directory where all template files are stored + /// + string TemplatesDirectory { get; } + /// /// The directory where the automatically saved files will be stored. /// diff --git a/src/DynamoCore/Configuration/PathManager.cs b/src/DynamoCore/Configuration/PathManager.cs index 6a794e8ab04..2c6a27e32a6 100644 --- a/src/DynamoCore/Configuration/PathManager.cs +++ b/src/DynamoCore/Configuration/PathManager.cs @@ -66,6 +66,7 @@ internal static Lazy public const string ViewExtensionsDirectoryName = "viewExtensions"; public const string DefinitionsDirectoryName = "definitions"; public const string SamplesDirectoryName = "samples"; + public const string TemplateDirectoryName = "templates"; public const string BackupDirectoryName = "backup"; public const string PreferenceSettingsFileName = "DynamoSettings.xml"; public const string PythonTemplateFileName = "PythonTemplate.py"; @@ -82,6 +83,7 @@ internal static Lazy private string commonPackages; private string logDirectory; private string samplesDirectory; + private string templatesDirectory; private string backupDirectory; private string defaultBackupDirectory; private string preferenceFilePath; @@ -240,6 +242,11 @@ public string SamplesDirectory get { return samplesDirectory; } } + public string TemplatesDirectory + { + get { return templatesDirectory; } + } + public string BackupDirectory { get { return backupDirectory; } @@ -572,6 +579,7 @@ private void BuildCommonDirectories() commonDefinitions = Path.Combine(commonDataDir, DefinitionsDirectoryName); commonPackages = Path.Combine(commonDataDir, PackagesDirectoryName); samplesDirectory = GetSamplesFolder(commonDataDir); + templatesDirectory = GetTemplateFolder(commonDataDir); rootDirectories = new List { userDataDir }; @@ -715,7 +723,46 @@ private static string GetSamplesFolder(string dataRootDirectory) return sampleDirectory; } - + + private string GetTemplateFolder(string dataRootDirectory) + { + var versionedDirectory = dataRootDirectory; + if (!Directory.Exists(versionedDirectory)) + { + // Try to see if folder "%ProgramData%\{...}\{major}.{minor}" exists, if it + // does not, then root directory would be "%ProgramData%\{...}". + // + dataRootDirectory = Directory.GetParent(versionedDirectory).FullName; + } + else if (!Directory.Exists(Path.Combine(versionedDirectory, TemplateDirectoryName))) + { + // If the folder "%ProgramData%\{...}\{major}.{minor}" exists, then try to see + // if the folder "%ProgramData%\{...}\{major}.{minor}\samples" exists. If it + // doesn't exist, then root directory would be "%ProgramData%\{...}". + // + dataRootDirectory = Directory.GetParent(versionedDirectory).FullName; + } + + var uiCulture = CultureInfo.CurrentUICulture.Name; + var templateDirectory = Path.Combine(dataRootDirectory, TemplateDirectoryName, uiCulture); + + // If the localized template directory does not exist then fall back + // to using the en-US template folder. Do an additional check to see + // if the localized folder is available but is empty. + // + var di = new DirectoryInfo(templateDirectory); + if (!Directory.Exists(templateDirectory) || + !di.GetDirectories().Any() || + !di.GetFiles("*.dyn", SearchOption.AllDirectories).Any()) + { + var neturalCommonTemplates = Path.Combine(dataRootDirectory, TemplateDirectoryName, "en-US"); + if (Directory.Exists(neturalCommonTemplates)) + templateDirectory = neturalCommonTemplates; + } + + return templateDirectory; + } + private IEnumerable LibrarySearchPaths(string library) { // Strip out possible directory from library path. diff --git a/src/DynamoCoreWpf/Properties/Resources.Designer.cs b/src/DynamoCoreWpf/Properties/Resources.Designer.cs index d92937d20cd..41adea8c7f1 100644 --- a/src/DynamoCoreWpf/Properties/Resources.Designer.cs +++ b/src/DynamoCoreWpf/Properties/Resources.Designer.cs @@ -1801,6 +1801,24 @@ public static string DynamoViewFileMenuOpen { } } + /// + /// Looks up a localized string similar to _File. + /// + public static string DynamoViewFileMenuOpenFile { + get { + return ResourceManager.GetString("DynamoViewFileMenuOpenFile", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to _Template. + /// + public static string DynamoViewFileMenuOpenTemplate { + get { + return ResourceManager.GetString("DynamoViewFileMenuOpenTemplate", resourceCulture); + } + } + /// /// Looks up a localized string similar to Open _Recent Files. /// diff --git a/src/DynamoCoreWpf/Properties/Resources.en-US.resx b/src/DynamoCoreWpf/Properties/Resources.en-US.resx index 96504e3ea82..50aecc3915e 100644 --- a/src/DynamoCoreWpf/Properties/Resources.en-US.resx +++ b/src/DynamoCoreWpf/Properties/Resources.en-US.resx @@ -3900,4 +3900,10 @@ In certain complex graphs or host program scenarios, Automatic mode may cause in Your changes will be lost if you proceed. - + + _File + + + _Template + + \ No newline at end of file diff --git a/src/DynamoCoreWpf/Properties/Resources.resx b/src/DynamoCoreWpf/Properties/Resources.resx index 23d8634ccaa..c03ab3f6e93 100644 --- a/src/DynamoCoreWpf/Properties/Resources.resx +++ b/src/DynamoCoreWpf/Properties/Resources.resx @@ -3887,4 +3887,10 @@ In certain complex graphs or host program scenarios, Automatic mode may cause in Your changes will be lost if you proceed. + + _File + + + _Template + \ No newline at end of file diff --git a/src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs b/src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs index 3a9f5248f07..7e993e1ead6 100644 --- a/src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs +++ b/src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs @@ -1939,6 +1939,62 @@ private void ShowOpenDialogAndOpenResult(object parameter) private bool CanShowOpenDialogAndOpenResultCommand(object parameter) => CanRunGraph; + /// + /// Present the open dialog and open the template that is selected. + /// + /// + private void ShowOpenTemplateDialog(object parameter) + { + if (HomeSpace.HasUnsavedChanges) + { + if (!AskUserToSaveWorkspaceOrCancel(HomeSpace)) + return; + } + + DynamoOpenFileDialog _fileDialog = new DynamoOpenFileDialog(this) + { + Filter = string.Format(Resources.FileDialogDynamoDefinitions, + BrandingResourceProvider.ProductName, "*.dyn;*.dyf") + "|" + + string.Format(Resources.FileDialogAllFiles, "*.*"), + Title = string.Format(Resources.OpenDynamoDefinitionDialogTitle, BrandingResourceProvider.ProductName) + }; + + // if you've got the current space path, use it as the inital dir + if (!string.IsNullOrEmpty(Model.PathManager.TemplatesDirectory)) + { + string path = Model.PathManager.TemplatesDirectory; + if (Directory.Exists(path)) + { + var di = new DirectoryInfo(Model.PathManager.TemplatesDirectory); + _fileDialog.InitialDirectory = di.FullName; + } + else + { + _fileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer); + } + } + else // use the samples directory, if it exists + { + Assembly dynamoAssembly = Assembly.GetExecutingAssembly(); + string location = Path.GetDirectoryName(dynamoAssembly.Location); + string UICulture = CultureInfo.CurrentUICulture.Name; + string path = Path.Combine(location, "samples", UICulture); + + if (Directory.Exists(path)) + _fileDialog.InitialDirectory = path; + } + + if (_fileDialog.ShowDialog() == DialogResult.OK) + { + if (CanOpen(_fileDialog.FileName)) + { + // Replace with the template file opening API which does not modify the template file + Open(new Tuple(_fileDialog.FileName, _fileDialog.RunManualMode)); + } + } + } + + private bool CanShowOpenTemplateDialog(object parameter) => CanRunGraph; /// /// Present the open dialog and open the workspace that is selected. diff --git a/src/DynamoCoreWpf/ViewModels/Core/DynamoViewModelDelegateCommands.cs b/src/DynamoCoreWpf/ViewModels/Core/DynamoViewModelDelegateCommands.cs index b8b4a4d1e6b..5200db6e86a 100644 --- a/src/DynamoCoreWpf/ViewModels/Core/DynamoViewModelDelegateCommands.cs +++ b/src/DynamoCoreWpf/ViewModels/Core/DynamoViewModelDelegateCommands.cs @@ -18,6 +18,7 @@ private void InitializeDelegateCommands() SaveCommand = new DelegateCommand(Save, CanSave); SaveAsCommand = new DelegateCommand(SaveAs, CanSaveAs); ShowOpenDialogAndOpenResultCommand = new DelegateCommand(ShowOpenDialogAndOpenResult, CanShowOpenDialogAndOpenResultCommand); + ShowOpenTemplateDialogCommand = new DelegateCommand(ShowOpenTemplateDialog, CanShowOpenTemplateDialog); ShowInsertDialogAndInsertResultCommand = new DelegateCommand(ShowInsertDialogAndInsertResult, CanShowInsertDialogAndInsertResultCommand); ShowSaveDialogAndSaveResultCommand = new DelegateCommand(ShowSaveDialogAndSaveResult, CanShowSaveDialogAndSaveResult); ShowSaveDialogIfNeededAndSaveResultCommand = new DelegateCommand(ShowSaveDialogIfNeededAndSaveResult, CanShowSaveDialogIfNeededAndSaveResultCommand); @@ -98,6 +99,8 @@ private void InitializeDelegateCommands() public DelegateCommand OpenIfSavedCommand { get; set; } public DelegateCommand OpenCommand { get; set; } public DelegateCommand ShowOpenDialogAndOpenResultCommand { get; set; } + public DelegateCommand ShowOpenTemplateDialogCommand { get; set; } + public DelegateCommand ShowInsertDialogAndInsertResultCommand { get; set; } public DelegateCommand WriteToLogCmd { get; set; } public DelegateCommand PostUiActivationCommand { get; set; } diff --git a/src/DynamoCoreWpf/Views/Core/DynamoView.xaml b/src/DynamoCoreWpf/Views/Core/DynamoView.xaml index 3824a34408b..c90ecc59fe7 100644 --- a/src/DynamoCoreWpf/Views/Core/DynamoView.xaml +++ b/src/DynamoCoreWpf/Views/Core/DynamoView.xaml @@ -112,6 +112,9 @@ + @@ -334,9 +337,17 @@ + Name="openButton"> + + + +