From a7d9154e5aecb7efa3571df4863346605857c194 Mon Sep 17 00:00:00 2001 From: John Lewin Date: Thu, 25 Oct 2018 08:25:36 -0700 Subject: [PATCH] Add Recent menu item to bed menu to replace history bar - Issue MatterHackers/MCCentral#4376 --- .../ApplicationView/PrinterModels.cs | 10 +++ .../PartPreviewWindow/PopupMenu.cs | 2 +- .../PartPreviewWindow/ViewControls3D.cs | 75 ++++++++++++++++++- .../Settings/ProfileManager.cs | 6 +- 4 files changed, 83 insertions(+), 10 deletions(-) diff --git a/MatterControlLib/ApplicationView/PrinterModels.cs b/MatterControlLib/ApplicationView/PrinterModels.cs index 626b81589..7599607f4 100644 --- a/MatterControlLib/ApplicationView/PrinterModels.cs +++ b/MatterControlLib/ApplicationView/PrinterModels.cs @@ -101,6 +101,16 @@ namespace MatterHackers.MatterControl this.SceneLoaded?.Invoke(this, null); } + public Task LoadLibraryContent(ILibraryItem libraryItem) + { + return this.LoadContent( + new EditContext() + { + ContentStore = ApplicationController.Instance.Library.PartHistory, + SourceItem = libraryItem + }); + } + public async Task LoadContent(EditContext editContext) { // Make sure we don't have a selection diff --git a/MatterControlLib/PartPreviewWindow/PopupMenu.cs b/MatterControlLib/PartPreviewWindow/PopupMenu.cs index dee30feec..e9b197d57 100644 --- a/MatterControlLib/PartPreviewWindow/PopupMenu.cs +++ b/MatterControlLib/PartPreviewWindow/PopupMenu.cs @@ -277,7 +277,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow public void CreateSubMenu(string menuTitle, ThemeConfig menuTheme, Action populateSubMenu, ImageBuffer icon = null) { - GuiWidget content = new TextWidget(menuTitle, pointSize: theme.DefaultFontSize, textColor: theme.Colors.PrimaryTextColor) + var content = new TextWidget(menuTitle, pointSize: theme.DefaultFontSize, textColor: theme.Colors.PrimaryTextColor) { Padding = MenuPadding, }; diff --git a/MatterControlLib/PartPreviewWindow/ViewControls3D.cs b/MatterControlLib/PartPreviewWindow/ViewControls3D.cs index 8542426f7..65972b555 100644 --- a/MatterControlLib/PartPreviewWindow/ViewControls3D.cs +++ b/MatterControlLib/PartPreviewWindow/ViewControls3D.cs @@ -1,5 +1,5 @@ /* -Copyright (c) 2017, Lars Brubaker, John Lewin +Copyright (c) 2018, Lars Brubaker, John Lewin All rights reserved. Redistribution and use in source and binary forms, with or without @@ -32,9 +32,9 @@ using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; using System.Linq; -using System.Threading; using System.Threading.Tasks; using MatterHackers.Agg; +using MatterHackers.Agg.Image; using MatterHackers.Agg.Platform; using MatterHackers.Agg.UI; using MatterHackers.DataConverters3D; @@ -42,7 +42,6 @@ using MatterHackers.Localizations; using MatterHackers.MatterControl.CustomWidgets; using MatterHackers.MatterControl.DataStorage; using MatterHackers.MatterControl.Library; -using MatterHackers.MatterControl.PrinterControls.PrinterConnections; using MatterHackers.MatterControl.PrintLibrary; using MatterHackers.MatterControl.SlicerConfiguration; using MatterHackers.VectorMath; @@ -159,7 +158,75 @@ namespace MatterHackers.MatterControl.PartPreviewWindow bedMenuButton.DynamicPopupContent = () => { - var popupMenu = new PopupMenu(ApplicationController.Instance.MenuTheme); + var menuTheme = ApplicationController.Instance.MenuTheme; + var popupMenu = new PopupMenu(menuTheme); + + int thumbWidth = 24; + + popupMenu.CreateSubMenu("Recent".Localize(), menuTheme, (subMenu) => + { + // Select the 25 most recent files and project onto FileSystemItems + var recentFiles = new DirectoryInfo(ApplicationDataStorage.Instance.PlatingDirectory).GetFiles("*.mcx").OrderByDescending(f => f.LastWriteTime); + foreach (var item in recentFiles.Where(f => f.Length > 500).Select(f => new SceneReplacementFileItem(f.FullName)).Take(10).ToList()) + { + var imageBuffer = new ImageBuffer(thumbWidth, thumbWidth); + + var bedHistory = subMenu.CreateMenuItem(item.Name, imageBuffer); + bedHistory.Click += (s, e) => + { + UiThread.RunOnIdle(async () => + { + await ApplicationController.Instance.Tasks.Execute("Saving changes".Localize() + "...", sceneContext.SaveChanges); + + await sceneContext.LoadLibraryContent(item); + + if (sceneContext.Printer != null) + { + sceneContext.Printer.ViewState.ViewMode = PartViewMode.Model; + } + }); + }; + + ApplicationController.Instance.Library.LoadItemThumbnail( + (icon) => + { + imageBuffer.CopyFrom(icon); + }, + (contentProvider) => + { + if (contentProvider is MeshContentProvider meshContentProvider) + { + ApplicationController.Instance.Thumbnails.QueueForGeneration(async () => + { + // Ask the MeshContentProvider to RayTrace the image + var thumbnail = await meshContentProvider.GetThumbnail(item, thumbWidth, thumbWidth); + if (thumbnail != null) + { + //if (thumbnail.Width != thumbWidth + //|| thumbnail.Height != thumbHeight) + //{ + // this.SetUnsizedThumbnail(thumbnail); + //} + //else + //{ + // this.SetSizedThumbnail(thumbnail); + //} + imageBuffer.CopyFrom(thumbnail); + + popupMenu.Invalidate(); + } + }); + } + }, + item, + ApplicationController.Instance.Library.PlatingHistory, + thumbWidth, + thumbWidth, + menuTheme).ConfigureAwait(false); + } + }); + + popupMenu.CreateSeparator(); var actions = new NamedAction[] { new ActionSeparator(), diff --git a/MatterControlLib/SlicerConfiguration/Settings/ProfileManager.cs b/MatterControlLib/SlicerConfiguration/Settings/ProfileManager.cs index 9d0f34972..34f06e804 100644 --- a/MatterControlLib/SlicerConfiguration/Settings/ProfileManager.cs +++ b/MatterControlLib/SlicerConfiguration/Settings/ProfileManager.cs @@ -88,11 +88,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration { var printer = await LoadPrinter(); - await printer?.Bed?.LoadContent(new EditContext() - { - ContentStore = ApplicationController.Instance.Library.PlatingHistory, - SourceItem = libraryItem - }); + await printer?.Bed?.LoadLibraryContent(libraryItem); } public async Task LoadPrinter()