Skip to content

Commit

Permalink
reuse ShowOpenDialogAndOpenResult code
Browse files Browse the repository at this point in the history
  • Loading branch information
QilongTang committed Jan 26, 2024
1 parent a157b84 commit 16348fc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 62 deletions.
85 changes: 24 additions & 61 deletions src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<string,bool>(_fileDialog.FileName, _fileDialog.RunManualMode));
var di = new DirectoryInfo(Model.PathManager.TemplatesDirectory);
_fileDialog.InitialDirectory = di.FullName;
}
}
}

private bool CanShowOpenDialogAndOpenResultCommand(object parameter) => CanRunGraph;

/// <summary>
/// Present the open dialog and open the template that is selected.
/// </summary>
/// <param name="parameter"></param>
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
{
Expand All @@ -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<string, bool, bool>(_fileDialog.FileName, _fileDialog.RunManualMode, true));
if (isTemplate)
{
// File opening API which does not modify the original template file
Open(new Tuple<string, bool, bool>(_fileDialog.FileName, _fileDialog.RunManualMode, true));
}
else
{
Open(new Tuple<string, bool>(_fileDialog.FileName, _fileDialog.RunManualMode));
}
}
}
}

private bool CanShowOpenTemplateDialog(object parameter) => CanRunGraph;
private bool CanShowOpenDialogAndOpenResultCommand(object parameter) => CanRunGraph;

/// <summary>
/// Present the open dialog and open the workspace that is selected.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/DynamoCoreWpf/Views/Core/DynamoView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@
<MenuItem Header="{x:Static p:Resources.DynamoViewFileMenuOpenTemplate}"
Name="openTemplateMenuItem"
Command="{Binding ShowOpenTemplateDialogCommand}"
CommandParameter="Template"
InputGestureText="Ctrl + T">
</MenuItem>
</MenuItem>
Expand Down

0 comments on commit 16348fc

Please sign in to comment.