diff --git a/src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs b/src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs index 9bb02e3d363..d80708af838 100644 --- a/src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs +++ b/src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs @@ -1929,6 +1929,8 @@ private void ShowOpenDialogAndOpenResult(object parameter) return; } + bool isTemplate = (parameter as string).Equals("Template"); + DynamoOpenFileDialog _fileDialog = new DynamoOpenFileDialog(this) { Filter = string.Format(Resources.FileDialogDynamoDefinitions, @@ -1937,70 +1939,24 @@ private void ShowOpenDialogAndOpenResult(object parameter) 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.CurrentWorkspace.FileName)) + // If opening a template, use templates dir as the initial dir + if (isTemplate && !string.IsNullOrEmpty(Model.PathManager.TemplatesDirectory)) { - string path = Model.CurrentWorkspace.FileName; - if (File.Exists(path)) - { - var fi = new FileInfo(Model.CurrentWorkspace.FileName); - _fileDialog.InitialDirectory = fi.DirectoryName; - } - 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); - + string path = Model.PathManager.TemplatesDirectory; if (Directory.Exists(path)) - _fileDialog.InitialDirectory = path; - } - - if (_fileDialog.ShowDialog() == DialogResult.OK) - { - if (CanOpen(_fileDialog.FileName)) { - Open(new Tuple(_fileDialog.FileName, _fileDialog.RunManualMode)); + var di = new DirectoryInfo(Model.PathManager.TemplatesDirectory); + _fileDialog.InitialDirectory = di.FullName; } } - } - - 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)) + // otherwise, if you've got the current space path, use it as the initial dir + else if (!string.IsNullOrEmpty(Model.CurrentWorkspace.FileName)) { - string path = Model.PathManager.TemplatesDirectory; - if (Directory.Exists(path)) + string path = Model.CurrentWorkspace.FileName; + if (File.Exists(path)) { - var di = new DirectoryInfo(Model.PathManager.TemplatesDirectory); - _fileDialog.InitialDirectory = di.FullName; + var fi = new FileInfo(Model.CurrentWorkspace.FileName); + _fileDialog.InitialDirectory = fi.DirectoryName; } else { @@ -2017,18 +1973,25 @@ private void ShowOpenTemplateDialog(object parameter) 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, true)); + if (isTemplate) + { + // File opening API which does not modify the original template file + Open(new Tuple(_fileDialog.FileName, _fileDialog.RunManualMode, true)); + } + else + { + Open(new Tuple(_fileDialog.FileName, _fileDialog.RunManualMode)); + } } } } - private bool CanShowOpenTemplateDialog(object parameter) => CanRunGraph; + private bool CanShowOpenDialogAndOpenResultCommand(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 5200db6e86a..3d9509e52dc 100644 --- a/src/DynamoCoreWpf/ViewModels/Core/DynamoViewModelDelegateCommands.cs +++ b/src/DynamoCoreWpf/ViewModels/Core/DynamoViewModelDelegateCommands.cs @@ -18,7 +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); + ShowOpenTemplateDialogCommand = new DelegateCommand(ShowOpenDialogAndOpenResult, CanShowOpenDialogAndOpenResultCommand); ShowInsertDialogAndInsertResultCommand = new DelegateCommand(ShowInsertDialogAndInsertResult, CanShowInsertDialogAndInsertResultCommand); ShowSaveDialogAndSaveResultCommand = new DelegateCommand(ShowSaveDialogAndSaveResult, CanShowSaveDialogAndSaveResult); ShowSaveDialogIfNeededAndSaveResultCommand = new DelegateCommand(ShowSaveDialogIfNeededAndSaveResult, CanShowSaveDialogIfNeededAndSaveResultCommand); diff --git a/src/DynamoCoreWpf/Views/Core/DynamoView.xaml b/src/DynamoCoreWpf/Views/Core/DynamoView.xaml index 9a8021ce70f..889c07a2ca8 100644 --- a/src/DynamoCoreWpf/Views/Core/DynamoView.xaml +++ b/src/DynamoCoreWpf/Views/Core/DynamoView.xaml @@ -346,6 +346,7 @@