Skip to content

Commit f58b7db

Browse files
reddyashishdnenov
andauthored
[Cherrypick] DYN-7154: missing thumbnail sample files (#15326) (#15334)
Co-authored-by: Deyan Nenov <[email protected]>
1 parent 298a7f3 commit f58b7db

File tree

5 files changed

+148
-27
lines changed

5 files changed

+148
-27
lines changed

src/DynamoCoreWpf/Controls/StartPage.xaml.cs

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ public enum Action
5757
ExternalUrl
5858
}
5959

60-
internal StartPageListItem(string caption)
60+
protected internal StartPageListItem(string caption)
6161
{
6262
this.Caption = caption;
6363
}
6464

65-
internal StartPageListItem(string caption, string iconPath)
65+
protected internal StartPageListItem(string caption, string iconPath)
6666
{
6767
this.Caption = caption;
6868
this.icon = LoadBitmapImage(iconPath);
@@ -95,7 +95,7 @@ public Visibility IconVisibility
9595

9696
#region Private Class Helper Methods
9797

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

129129
this.recentFiles = new ObservableCollection<StartPageListItem>();
130-
sampleFiles = new ObservableCollection<SampleFileEntry>();
131-
backupFiles = new ObservableCollection<StartPageListItem>();
130+
this.sampleFiles = new ObservableCollection<SampleFileEntry>();
131+
this.backupFiles = new ObservableCollection<StartPageListItem>();
132132

133133

134134
#region File Operations
@@ -214,6 +214,7 @@ internal StartPageViewModel(DynamoViewModel dynamoViewModel, bool isFirstRun)
214214
RefreshBackupFileList(dvm.Model.PreferenceSettings.BackupFiles);
215215
dvm.RecentFiles.CollectionChanged += OnRecentFilesChanged;
216216
}
217+
217218
internal void WalkDirectoryTree(System.IO.DirectoryInfo root, SampleFileEntry rootProperty)
218219
{
219220
try
@@ -248,14 +249,24 @@ internal void WalkDirectoryTree(System.IO.DirectoryInfo root, SampleFileEntry ro
248249
{
249250
sampleFolderPath = Path.GetDirectoryName(file.FullName);
250251
}
252+
251253
// Add each file under the root directory property list.
252-
rootProperty.AddChildSampleFile(new SampleFileEntry(file.Name, file.FullName));
254+
var properties = GetFileProperties(file.FullName);
255+
256+
rootProperty.AddChildSampleFile(new SampleFileEntry(
257+
file.Name,
258+
file.FullName,
259+
properties.thumbnail,
260+
properties.author,
261+
properties.description,
262+
properties.date));
253263
}
254264
}
255265
}
256-
catch (Exception)
266+
catch (Exception ex)
257267
{
258268
// Perhaps some permission problems?
269+
DynamoViewModel.Model.Logger.Log("Error loading sample file: " + ex.StackTrace);
259270
}
260271
}
261272

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

388399
// deserializes the file only once
389-
var jsonObject = DeserializeJsonFile(filePath);
390-
var description = jsonObject != null ? GetGraphDescription(jsonObject) : string.Empty;
391-
var thumbnail = jsonObject != null ? GetGraphThumbnail(jsonObject) : string.Empty;
392-
var author = jsonObject != null ? GetGraphAuthor(jsonObject) : Resources.DynamoXmlFileFormat;
393-
394-
var date = DynamoUtilities.PathHelper.GetDateModified(filePath);
400+
var properties = GetFileProperties(filePath);
395401

396402
files.Add(new StartPageListItem(caption)
397403
{
398404
ContextData = filePath,
399405
ToolTip = filePath,
400406
SubScript = subScript,
401-
Description = description,
402-
Thumbnail = thumbnail,
403-
Author = author,
404-
DateModified = date,
407+
Description = properties.description,
408+
Thumbnail = properties.thumbnail,
409+
Author = properties.author,
410+
DateModified = properties.date,
405411
ClickAction = StartPageListItem.Action.FilePath,
406412

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

508+
/// <summary>
509+
/// Attempts to deserialize a dynamo graph file and extract metadata from it
510+
/// </summary>
511+
/// <param name="filePath">The file path to the dynamo file</param>
512+
/// <returns></returns>
513+
internal (string description, string thumbnail, string author, string date) GetFileProperties(string filePath)
514+
{
515+
if (!filePath.ToLower().EndsWith(".dyn") && !filePath.ToLower().EndsWith(".dyf")) return (null, null, null, null);
516+
517+
try
518+
{
519+
var jsonObject = DeserializeJsonFile(filePath);
520+
var description = jsonObject != null ? GetGraphDescription(jsonObject) : string.Empty;
521+
var thumbnail = jsonObject != null ? GetGraphThumbnail(jsonObject) : string.Empty;
522+
var author = jsonObject != null ? GetGraphAuthor(jsonObject) : Resources.DynamoXmlFileFormat;
523+
var date = DynamoUtilities.PathHelper.GetDateModified(filePath);
524+
525+
return (description, thumbnail, author, date);
526+
}
527+
catch (Exception ex)
528+
{
529+
DynamoViewModel.Model.Logger.Log("Error deserializing dynamo graph file: " + ex.StackTrace);
530+
return (null, null, null, null);
531+
}
532+
533+
}
534+
502535
#endregion
503536

504537
}
@@ -623,15 +656,28 @@ private void StartPage_OnDrop(object sender, DragEventArgs e)
623656
#endregion
624657
}
625658

626-
public class SampleFileEntry
659+
public class SampleFileEntry : StartPageListItem
627660
{
628661
List<SampleFileEntry> childSampleFiles = null;
629662

630663
public SampleFileEntry(string name, string path)
664+
: base(name)
631665
{
632666
this.FileName = name;
633667
this.FilePath = path;
634668
}
669+
670+
public SampleFileEntry(string name, string path, string thumbnail, string author, string description, string dateModified)
671+
: base(name)
672+
{
673+
this.FileName = name;
674+
this.FilePath = path;
675+
this.Thumbnail = thumbnail;
676+
this.Author = author;
677+
this.Description = description;
678+
this.DateModified = dateModified;
679+
}
680+
635681
public void AddChildSampleFile(SampleFileEntry childSampleFile)
636682
{
637683
if (null == childSampleFiles)

src/DynamoCoreWpf/DynamoCoreWpf.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
<!--This command updates the npm registry configuration if necessary-->
5454
<Exec Command="$(PowerShellCommand) -ExecutionPolicy ByPass -Command $(SolutionDir)\setnpmreg.ps1" />
5555
<!--Download a specific build of the Dynamo Home package from npm-->
56-
<Exec Command="npm pack @dynamods/[email protected].14" />
56+
<Exec Command="npm pack @dynamods/[email protected].15"/>
5757
</Target>
5858

5959
<Target Name="ExtractTGZFileDynamoHome" DependsOnTargets="NpmRunBuildHomePage" BeforeTargets="BeforeBuild">

src/DynamoCoreWpf/PublicAPI.Unshipped.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,7 @@ Dynamo.UI.Controls.SampleFileEntry.Children.get -> System.Collections.Generic.IE
14271427
Dynamo.UI.Controls.SampleFileEntry.FileName.get -> string
14281428
Dynamo.UI.Controls.SampleFileEntry.FilePath.get -> string
14291429
Dynamo.UI.Controls.SampleFileEntry.SampleFileEntry(string name, string path) -> void
1430+
Dynamo.UI.Controls.SampleFileEntry.SampleFileEntry(string name, string path, string thumbnail, string author, string description, string dateModified) -> void
14301431
Dynamo.UI.Controls.ShortcutBarItem
14311432
Dynamo.UI.Controls.ShortcutBarItem.ImgDisabledSource.get -> string
14321433
Dynamo.UI.Controls.ShortcutBarItem.ImgDisabledSource.set -> void
@@ -1465,6 +1466,9 @@ Dynamo.UI.Controls.StartPageListItem.DateModified.set -> void
14651466
Dynamo.UI.Controls.StartPageListItem.Description.get -> string
14661467
Dynamo.UI.Controls.StartPageListItem.Icon.get -> System.Windows.Media.ImageSource
14671468
Dynamo.UI.Controls.StartPageListItem.IconVisibility.get -> System.Windows.Visibility
1469+
Dynamo.UI.Controls.StartPageListItem.LoadBitmapImage(string iconPath) -> System.Windows.Media.Imaging.BitmapImage
1470+
Dynamo.UI.Controls.StartPageListItem.StartPageListItem(string caption) -> void
1471+
Dynamo.UI.Controls.StartPageListItem.StartPageListItem(string caption, string iconPath) -> void
14681472
Dynamo.UI.Controls.StartPageListItem.SubScript.get -> string
14691473
Dynamo.UI.Controls.StartPageListItem.SubScript.set -> void
14701474
Dynamo.UI.Controls.StartPageListItem.Thumbnail.get -> string

test/DynamoCoreWpfTests/HomePageTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,23 @@ public void CanOpenGraphOnDragAndDrop()
617617
}
618618
#endregion
619619

620+
[Test]
621+
public void TestDeserializeDynamoGraphProperties()
622+
{
623+
// Arrange
624+
var filePath = Path.Combine(GetTestDirectory(ExecutingDirectory), @"core\Home.dyn");
625+
var vm = View.DataContext as DynamoViewModel;
626+
var startPage = new StartPageViewModel(vm, true);
627+
628+
// Act
629+
var properties = startPage.GetFileProperties(filePath);
630+
631+
// Assert
632+
Assert.AreEqual(properties.description, "Test description");
633+
Assert.AreEqual(properties.author, "John Doe");
634+
Assert.IsFalse(string.IsNullOrEmpty(properties.thumbnail));
635+
}
636+
620637
#region helpers
621638

622639
/// <summary>

0 commit comments

Comments
 (0)