Skip to content

Commit

Permalink
[Cherrypick] DYN-7154: missing thumbnail sample files (#15326) (#15334)
Browse files Browse the repository at this point in the history
Co-authored-by: Deyan Nenov <dnenov@archilizer.com>
  • Loading branch information
reddyashish and dnenov committed Jun 20, 2024
1 parent 298a7f3 commit f58b7db
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 27 deletions.
82 changes: 64 additions & 18 deletions src/DynamoCoreWpf/Controls/StartPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ public enum Action
ExternalUrl
}

internal StartPageListItem(string caption)
protected internal StartPageListItem(string caption)
{
this.Caption = caption;
}

internal StartPageListItem(string caption, string iconPath)
protected internal StartPageListItem(string caption, string iconPath)
{
this.Caption = caption;
this.icon = LoadBitmapImage(iconPath);
Expand Down Expand Up @@ -95,7 +95,7 @@ public Visibility IconVisibility

#region Private Class Helper Methods

private BitmapImage LoadBitmapImage(string iconPath)
protected BitmapImage LoadBitmapImage(string iconPath)
{
var format = @"pack://application:,,,/DynamoCoreWpf;component/UI/Images/StartPage/{0}";
iconPath = string.Format(format, iconPath);
Expand Down Expand Up @@ -127,8 +127,8 @@ internal StartPageViewModel(DynamoViewModel dynamoViewModel, bool isFirstRun)
this.isFirstRun = isFirstRun;

this.recentFiles = new ObservableCollection<StartPageListItem>();
sampleFiles = new ObservableCollection<SampleFileEntry>();
backupFiles = new ObservableCollection<StartPageListItem>();
this.sampleFiles = new ObservableCollection<SampleFileEntry>();
this.backupFiles = new ObservableCollection<StartPageListItem>();


#region File Operations
Expand Down Expand Up @@ -214,6 +214,7 @@ internal StartPageViewModel(DynamoViewModel dynamoViewModel, bool isFirstRun)
RefreshBackupFileList(dvm.Model.PreferenceSettings.BackupFiles);
dvm.RecentFiles.CollectionChanged += OnRecentFilesChanged;
}

internal void WalkDirectoryTree(System.IO.DirectoryInfo root, SampleFileEntry rootProperty)
{
try
Expand Down Expand Up @@ -248,14 +249,24 @@ internal void WalkDirectoryTree(System.IO.DirectoryInfo root, SampleFileEntry ro
{
sampleFolderPath = Path.GetDirectoryName(file.FullName);
}

// Add each file under the root directory property list.
rootProperty.AddChildSampleFile(new SampleFileEntry(file.Name, file.FullName));
var properties = GetFileProperties(file.FullName);

rootProperty.AddChildSampleFile(new SampleFileEntry(
file.Name,
file.FullName,
properties.thumbnail,
properties.author,
properties.description,
properties.date));
}
}
}
catch (Exception)
catch (Exception ex)
{
// Perhaps some permission problems?
DynamoViewModel.Model.Logger.Log("Error loading sample file: " + ex.StackTrace);
}
}

Expand Down Expand Up @@ -386,22 +397,17 @@ private void RefreshFileList(ObservableCollection<StartPageListItem> files,
var caption = Path.GetFileNameWithoutExtension(filePath);

// deserializes the file only once
var jsonObject = DeserializeJsonFile(filePath);
var description = jsonObject != null ? GetGraphDescription(jsonObject) : string.Empty;
var thumbnail = jsonObject != null ? GetGraphThumbnail(jsonObject) : string.Empty;
var author = jsonObject != null ? GetGraphAuthor(jsonObject) : Resources.DynamoXmlFileFormat;

var date = DynamoUtilities.PathHelper.GetDateModified(filePath);
var properties = GetFileProperties(filePath);

files.Add(new StartPageListItem(caption)
{
ContextData = filePath,
ToolTip = filePath,
SubScript = subScript,
Description = description,
Thumbnail = thumbnail,
Author = author,
DateModified = date,
Description = properties.description,
Thumbnail = properties.thumbnail,
Author = properties.author,
DateModified = properties.date,
ClickAction = StartPageListItem.Action.FilePath,

});
Expand Down Expand Up @@ -499,6 +505,33 @@ private void HandleExternalUrl(StartPageListItem item)
System.Diagnostics.Process.Start(new ProcessStartInfo(item.ContextData) { UseShellExecute = true });
}

/// <summary>
/// Attempts to deserialize a dynamo graph file and extract metadata from it
/// </summary>
/// <param name="filePath">The file path to the dynamo file</param>
/// <returns></returns>
internal (string description, string thumbnail, string author, string date) GetFileProperties(string filePath)
{
if (!filePath.ToLower().EndsWith(".dyn") && !filePath.ToLower().EndsWith(".dyf")) return (null, null, null, null);

try
{
var jsonObject = DeserializeJsonFile(filePath);
var description = jsonObject != null ? GetGraphDescription(jsonObject) : string.Empty;
var thumbnail = jsonObject != null ? GetGraphThumbnail(jsonObject) : string.Empty;
var author = jsonObject != null ? GetGraphAuthor(jsonObject) : Resources.DynamoXmlFileFormat;
var date = DynamoUtilities.PathHelper.GetDateModified(filePath);

return (description, thumbnail, author, date);
}
catch (Exception ex)
{
DynamoViewModel.Model.Logger.Log("Error deserializing dynamo graph file: " + ex.StackTrace);
return (null, null, null, null);
}

}

#endregion

}
Expand Down Expand Up @@ -623,15 +656,28 @@ private void StartPage_OnDrop(object sender, DragEventArgs e)
#endregion
}

public class SampleFileEntry
public class SampleFileEntry : StartPageListItem
{
List<SampleFileEntry> childSampleFiles = null;

public SampleFileEntry(string name, string path)
: base(name)
{
this.FileName = name;
this.FilePath = path;
}

public SampleFileEntry(string name, string path, string thumbnail, string author, string description, string dateModified)
: base(name)
{
this.FileName = name;
this.FilePath = path;
this.Thumbnail = thumbnail;
this.Author = author;
this.Description = description;
this.DateModified = dateModified;
}

public void AddChildSampleFile(SampleFileEntry childSampleFile)
{
if (null == childSampleFiles)
Expand Down
2 changes: 1 addition & 1 deletion src/DynamoCoreWpf/DynamoCoreWpf.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<!--This command updates the npm registry configuration if necessary-->
<Exec Command="$(PowerShellCommand) -ExecutionPolicy ByPass -Command $(SolutionDir)\setnpmreg.ps1" />
<!--Download a specific build of the Dynamo Home package from npm-->
<Exec Command="npm pack @dynamods/dynamo-home@1.0.14" />
<Exec Command="npm pack @dynamods/dynamo-home@1.0.15"/>
</Target>

<Target Name="ExtractTGZFileDynamoHome" DependsOnTargets="NpmRunBuildHomePage" BeforeTargets="BeforeBuild">
Expand Down
4 changes: 4 additions & 0 deletions src/DynamoCoreWpf/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1427,6 +1427,7 @@ Dynamo.UI.Controls.SampleFileEntry.Children.get -> System.Collections.Generic.IE
Dynamo.UI.Controls.SampleFileEntry.FileName.get -> string
Dynamo.UI.Controls.SampleFileEntry.FilePath.get -> string
Dynamo.UI.Controls.SampleFileEntry.SampleFileEntry(string name, string path) -> void
Dynamo.UI.Controls.SampleFileEntry.SampleFileEntry(string name, string path, string thumbnail, string author, string description, string dateModified) -> void
Dynamo.UI.Controls.ShortcutBarItem
Dynamo.UI.Controls.ShortcutBarItem.ImgDisabledSource.get -> string
Dynamo.UI.Controls.ShortcutBarItem.ImgDisabledSource.set -> void
Expand Down Expand Up @@ -1465,6 +1466,9 @@ Dynamo.UI.Controls.StartPageListItem.DateModified.set -> void
Dynamo.UI.Controls.StartPageListItem.Description.get -> string
Dynamo.UI.Controls.StartPageListItem.Icon.get -> System.Windows.Media.ImageSource
Dynamo.UI.Controls.StartPageListItem.IconVisibility.get -> System.Windows.Visibility
Dynamo.UI.Controls.StartPageListItem.LoadBitmapImage(string iconPath) -> System.Windows.Media.Imaging.BitmapImage
Dynamo.UI.Controls.StartPageListItem.StartPageListItem(string caption) -> void
Dynamo.UI.Controls.StartPageListItem.StartPageListItem(string caption, string iconPath) -> void
Dynamo.UI.Controls.StartPageListItem.SubScript.get -> string
Dynamo.UI.Controls.StartPageListItem.SubScript.set -> void
Dynamo.UI.Controls.StartPageListItem.Thumbnail.get -> string
Expand Down
17 changes: 17 additions & 0 deletions test/DynamoCoreWpfTests/HomePageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,23 @@ public void CanOpenGraphOnDragAndDrop()
}
#endregion

[Test]
public void TestDeserializeDynamoGraphProperties()
{
// Arrange
var filePath = Path.Combine(GetTestDirectory(ExecutingDirectory), @"core\Home.dyn");
var vm = View.DataContext as DynamoViewModel;
var startPage = new StartPageViewModel(vm, true);

// Act
var properties = startPage.GetFileProperties(filePath);

// Assert
Assert.AreEqual(properties.description, "Test description");
Assert.AreEqual(properties.author, "John Doe");
Assert.IsFalse(string.IsNullOrEmpty(properties.thumbnail));
}

#region helpers

/// <summary>
Expand Down
Loading

0 comments on commit f58b7db

Please sign in to comment.