Skip to content

Commit

Permalink
Open Template UI change
Browse files Browse the repository at this point in the history
  • Loading branch information
QilongTang committed Jan 22, 2024
1 parent c7e42fd commit 6167f92
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/DynamoCore/Configuration/IPathResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ public interface IPathManager
/// </summary>
string SamplesDirectory { get; }

/// <summary>
/// The root directory where all template files are stored
/// </summary>
string TemplatesDirectory { get; }

/// <summary>
/// The directory where the automatically saved files will be stored.
/// </summary>
Expand Down
49 changes: 48 additions & 1 deletion src/DynamoCore/Configuration/PathManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ internal static Lazy<PathManager>
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";
Expand All @@ -82,6 +83,7 @@ internal static Lazy<PathManager>
private string commonPackages;
private string logDirectory;
private string samplesDirectory;
private string templatesDirectory;
private string backupDirectory;
private string defaultBackupDirectory;
private string preferenceFilePath;
Expand Down Expand Up @@ -240,6 +242,11 @@ public string SamplesDirectory
get { return samplesDirectory; }
}

public string TemplatesDirectory
{
get { return templatesDirectory; }
}

public string BackupDirectory
{
get { return backupDirectory; }
Expand Down Expand Up @@ -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<string> { userDataDir };

Expand Down Expand Up @@ -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<string> LibrarySearchPaths(string library)
{
// Strip out possible directory from library path.
Expand Down
18 changes: 18 additions & 0 deletions src/DynamoCoreWpf/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion src/DynamoCoreWpf/Properties/Resources.en-US.resx
Original file line number Diff line number Diff line change
Expand Up @@ -3900,4 +3900,10 @@ In certain complex graphs or host program scenarios, Automatic mode may cause in
<data name="ResetChangesWarningPopupMessage" xml:space="preserve">
<value>Your changes will be lost if you proceed.</value>
</data>
</root>
<data name="DynamoViewFileMenuOpenFile" xml:space="preserve">
<value>_File</value>
</data>
<data name="DynamoViewFileMenuOpenTemplate" xml:space="preserve">
<value>_Template</value>
</data>
</root>
6 changes: 6 additions & 0 deletions src/DynamoCoreWpf/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -3887,4 +3887,10 @@ In certain complex graphs or host program scenarios, Automatic mode may cause in
<data name="ResetChangesWarningPopupMessage" xml:space="preserve">
<value>Your changes will be lost if you proceed.</value>
</data>
<data name="DynamoViewFileMenuOpenFile" xml:space="preserve">
<value>_File</value>
</data>
<data name="DynamoViewFileMenuOpenTemplate" xml:space="preserve">
<value>_Template</value>
</data>
</root>
56 changes: 56 additions & 0 deletions src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1939,6 +1939,62 @@ private void ShowOpenDialogAndOpenResult(object parameter)

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))
{
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<string, bool>(_fileDialog.FileName, _fileDialog.RunManualMode));
}
}
}

private bool CanShowOpenTemplateDialog(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,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);
Expand Down Expand Up @@ -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; }
Expand Down
17 changes: 14 additions & 3 deletions src/DynamoCoreWpf/Views/Core/DynamoView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@
<KeyBinding Key="O"
Modifiers="Control"
Command="{Binding Path=DataContext.ShowOpenDialogAndOpenResultCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:DynamoView}}}" />
<KeyBinding Key="T"
Modifiers="Control"
Command="{Binding Path=DataContext.ShowOpenTemplateDialogCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:DynamoView}}}" />
<KeyBinding Key="I"
Modifiers="Control+Shift"
Command="{Binding Path=DataContext.ShowInsertDialogAndInsertResultCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:DynamoView}}}" />
Expand Down Expand Up @@ -334,9 +337,17 @@
</MenuItem>
</MenuItem>
<MenuItem Header="{x:Static p:Resources.DynamoViewFileMenuOpen}"
Command="{Binding ShowOpenDialogAndOpenResultCommand}"
Name="openButton"
InputGestureText="Ctrl + O">
Name="openButton">
<MenuItem Header="{x:Static p:Resources.DynamoViewFileMenuOpenFile}"
Name="openFileMenuItem"
Command="{Binding ShowOpenDialogAndOpenResultCommand}"
InputGestureText="Ctrl + O">
</MenuItem>
<MenuItem Header="{x:Static p:Resources.DynamoViewFileMenuOpenTemplate}"
Name="openTemplateMenuItem"
Command="{Binding ShowOpenTemplateDialogCommand}"
InputGestureText="Ctrl + T">
</MenuItem>
</MenuItem>
<MenuItem Header="{x:Static p:Resources.DynamoViewFileMenuInsert}"
Command="{Binding ShowInsertDialogAndInsertResultCommand}"
Expand Down
2 changes: 1 addition & 1 deletion src/DynamoCoreWpf/Views/Core/DynamoView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2166,7 +2166,7 @@ private void LoadSamplesMenu()
string[] filePaths = Directory.GetFiles(samplesDirectory, "*.dyn");

// handle top-level files
if (filePaths.Any())
if (filePaths.Length != 0)
{
foreach (string path in filePaths)
{
Expand Down

0 comments on commit 6167f92

Please sign in to comment.